<?php
require_once '../../../resources/base_path_include.php';

require_once $base_path_to_resources . "includes/config.php"; // use require for mandatory files
require_once $base_path_to_resources . 'lib/function.php';
require_once $base_path_to_resources . 'lib/export/print/book_template/template_1/header.php';
require_once $base_path . "vendor/autoload.php";

require_once $base_path_to_resources . 'lib/bucket_functions/bucket_functions.php';

require_once $base_path_to_resources . 'includes/aws_bucket_config.php';

use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;

use \Spipu\Html2Pdf\Html2Pdf;
use \Spipu\Html2Pdf\Exception\Html2PdfException;
use \Spipu\Html2Pdf\Exception\ExceptionFormatter;

require_once $base_path . 'modules/accounting/groups_ledgers/functions/ledger_functions.php';
require_once $base_path . 'modules/accounting/groups_ledgers/functions/display_transaction_functions.php';
require_once $base_path . 'modules/accounting/functions/basic_balance_function.php';


$narration_bool = (isset($postdata['narration_bool']) and $postdata['narration_bool'] == 1) ? 1 : 0;
$book_info = GetBasicBook($specific_db_conn, $workspace_id, $company_id, $branch_id, $book_id);

$book_name = $book_info['book_basic_info']['book_name'];
$book_opening_date = $book_info['book_basic_info']['book_opening_date'];
$book_closing_date = $book_info['book_basic_info']['book_closing_date'];
$data = displayTransaction($specific_db_conn, $ledgerid, $workspace_id, $company_id, $branch_id, $book_id, $mindate, $maxdate, $limit, $offset,$narration_bool, $book_info,$transactiontype);


createDisplayTransactionsPrint($specific_db_conn, $data, $ledgerid, $workspace_id, $company_id, $branch_id, $book_id, $function_code);


function createDisplayTransactionsPrint($specific_db_conn, $data, $ledgerid, $workspace_id, $company_id, $branch_id, $book_id, $function_code)
{

    $book_info = GetBasicBook($specific_db_conn, $workspace_id, $company_id, $branch_id, $book_id);

    $currencySystem = $book_info['book_basic_info']['currency_format'];
    $number_of_decimal_places = $book_info['book_basic_info']['number_of_decimal_places'];
    $base_currency_symbol = $book_info['book_basic_info']['base_currency_symbol'];
    $additionaldata = array();
    $additionaldata = ['ledger_name'];
    $ledgername = GetBasicLedgerDetail($specific_db_conn, $ledgerid, $workspace_id, $company_id, $branch_id, $book_id, $additionaldata);

    $sql = "SELECT * FROM act_book_settings WHERE workspace_id = ? AND company_id = ? AND branch_id = ? AND book_id = ?";
    $stmt = $specific_db_conn->prepare($sql);
    $stmt->execute(array($workspace_id, $company_id, $branch_id, $book_id));
    $result = $stmt->fetchAll();
    // print_R($result);
    if (count($result) == 1) {

        $report_print_company_name = $result[0]['report_print_company_name'];
        $report_print_branch_name = $result[0]['report_print_branch_name'];
        $report_print_book_name = $result[0]['report_print_book_name'];
        $logo = $result[0]['logo'];
        $print_logo_bool = $result[0]['print_logo_bool'];
        if ($result[0]['logo'] !=  null and $print_logo_bool == 1) {
              $keyName = $logo;
            $file = displayFileFromS3($GLOBALS['s3_finware_bucket'], $GLOBALS['finware_bucketName'], $keyName);
            
        } else {

            $file =  null;
        }

        // print_R($file);
        if ($report_print_company_name == 1) {
            $company_name = $book_info['book_basic_info']['company_name'];
        } else {

            $company_name =  null;
        }

        if ($report_print_branch_name == 1) {
            $branch_name = $book_info['book_basic_info']['branch_name'];
        } else {

            $branch_name =  null;
        }
        if ($report_print_book_name == 1) {

            $book_name = $book_info['book_basic_info']['book_name'];
            $book_opening_date = $book_info['book_basic_info']['book_opening_date'];
            $book_closing_date = $book_info['book_basic_info']['book_closing_date'];
        } else {

            $book_name =  null;
            $book_opening_date =  null;
            $book_closing_date =  null;
        }
    } else {
        errorreturn('Branch or company not found.');
    }

    $book_name = $book_info['book_basic_info']['book_name'];
    $book_opening_date = $book_info['book_basic_info']['book_opening_date'];
    $book_closing_date = $book_info['book_basic_info']['book_closing_date'];


    $table = '<table style=" border-collapse: collapse;border-spacing: 0;  width: 100%;display: block;table-layout: fixed;margin-top:20px;">
            <thead  style="display: table-header-group;border-bottom: none;">
            <tr style="color: inherit;display: table-row;vertical-align: middle;outline: 0px;">
            <th style="padding: 15px 16px;font-family:calibri;padding: 15px 16px;border-bottom: none;background-color: rgb(248, 249, 250);color:#333;font-size: 12px;font-weight: 700;line-height: 1;letter-spacing: 0.5px;text-transform: uppercase;text-align: left"><strong>Date</strong></th>
            <th colspan="2" style="padding: 15px 16px;font-family:calibri;border-bottom: none;background-color: rgb(248, 249, 250);color:#333;font-size: 12px;font-weight: 700;line-height: 1;letter-spacing: 0.5px;text-transform: uppercase;text-align: left" ><strong>Particulars</strong></th>
            <th style="padding: 15px 16px;font-family:calibri;border-bottom: none;background-color: rgb(248, 249, 250);color:#333;font-size: 12px;font-weight: 700;line-height: 1;letter-spacing: 0.5px;text-transform: uppercase;"><strong>Debit </strong></th>
            <th style="padding: 15px 16px;font-family:calibri;border-bottom: none;background-color: rgb(248, 249, 250);color:#333;font-size: 12px;font-weight: 700;line-height: 1;letter-spacing: 0.5px;text-transform: uppercase;"><strong>Credit</strong></th>
            <th style="padding: 15px 16px;font-family:calibri;;border-bottom: none;background-color: rgb(248, 249, 250);color:#333;font-size: 12px;font-weight: 700;line-height: 1;letter-spacing: 0.5px;text-transform: uppercase;"><strong>Closing</strong></th>
            </tr>
            </thead>';

    // Loop through your data and generate table rows dynamically
    $values = $data['transaction_info'];

    $stats = $data['stats'];
    // print_R($stats);
    $table .= '<tbody style="display: table-row-group;">';

    foreach ($values as $item) {

        if ($item['particulars'] !=  'as per details') {
            $date = (isset($item['date']) and !empty($item['date'])) ? trim(date('d-m-Y', strtotime($item['date']))) : null;

            $table .= '<tr style="color: inherit;display: table-row;vertical-align: middle;outline: 0px;">';
            $table .= '<td style="border-bottom: 1px solid #ddd;font-family:calibri;
            text-align: left;color:#333;padding: 15px 16px;">' . $date . '</td>';
            $table .= '<td colspan="2" style="display: table-cell;vertical-align: inherit;border-bottom: 1px solid #ddd;
            text-align: left;color:#333;padding: 15px 16px;font-weight:700">' . $item['particulars'] . '</td>';


            if (isset($item['balancetype']) && $item['balancetype'] == 'dr' && !empty($item['amount'])) {
                $table .= '<td style="border-bottom: 1px solid #ddd;font-family:calibri;
                     text-align: left;color:#333;padding: 15px 16px;">' .  formatCurrency($item['amount'], $currencySystem, $number_of_decimal_places, null) . '</td>';
            } else {
                $table .= '<td style="font-size: 0.875rem;font-weight: 400;line-height: 1.57;display: table-cell;vertical-align: inherit;border-bottom: 1px solid #ddd;
                     text-align: left;color:#333;padding: 15px 16px;"></td>';
            }
            if (isset($item['balancetype']) && $item['balancetype'] == 'cr' && !empty($item['amount'])) {
                $table .= '<td style="border-bottom: 1px solid #ddd;font-family:calibri;
                     text-align: left;color:#333;padding: 15px 16px;">' . formatCurrency($item['amount'], $currencySystem, $number_of_decimal_places, null)  . '</td>';
            } else {
                $table .= '<td style="font-size: 0.875rem;font-weight: 400;line-height: 1.57;display: table-cell;vertical-align: inherit;border-bottom: 1px solid #ddd;
                     text-align: left;color:#333;padding: 15px 16px;"></td>';
            }


            if (isset($item['closing_balancetype']) && in_array($item['closing_balancetype'], ['cr', 'dr']) && !empty($item['closing_amount'])) {
                $table .= '<td style="font-size: 0.875rem;font-weight: 400;line-height: 1.57;display: table-cell;vertical-align: inherit;border-bottom: 1px solid #ddd;
                     text-align: left;color:#333;padding: 15px 16px;">' . formatCurrency($item['closing_amount'], $currencySystem, $number_of_decimal_places, null) . '  ' . $item['closing_balancetype'];
                '</td>';
            } else {
                $table .= '<td style="font-size: 0.875rem;font-weight: 400;line-height: 1.57;display: table-cell;vertical-align: inherit;border-bottom: 1px solid #ddd;
                     text-align: left;color:#333;padding: 15px 16px;"></td>';
            }


            if (!isset($item['closing_amount'])) {
                $table .= '<td style="font-size: 0.875rem;font-weight: 400;line-height: 1.57;display: table-cell;vertical-align: inherit;border-bottom: 1px solid #ddd;
            text-align: left;color:#333;padding: 15px 16px;"></td>';
            }
            $table .= '</tr>';
        } else {

            // $rowspan = count($item['transaction_info']);
            $table .= '<tr style="color: inherit;display: table-row;vertical-align: middle;outline: 1px;border :1px">';
            $table .= '<td style="border-bottom: 1px solid #ddd;font-family:calibri;
                text-align: left;color:#333;padding: 15px 16px;">' . date('d-m-Y', strtotime($item['date'])) . '</td>';

            $table .= '<td colspan="2" style="font-size: 0.875rem;font-weight: 400;line-height: 1.57;display: table-cell;vertical-align: inherit;border-bottom: 1px solid #ddd;
            text-align: left;color:#333;padding: 15px 16px;font-weight:700" >' . $item['particulars'];



            $rows =  $item['child_records'];

            foreach ($rows as $records) {


                $table .= '<table border ="0" width= "100%"><tr >';
                //  $table .='<td ><strong></strong></td>';
                $table .= '<td  >' . $records['name'] . '&nbsp;&nbsp;&nbsp;</td>
                      <td style = "text-align:right">' .  formatCurrency($records['amount'], $currencySystem, $number_of_decimal_places, null) . '  ' . $records['balancetype'];
                '</td>';


                $table .= '</tr></table>';
            }

            $table .= '</td>';


            if (isset($item['balancetype']) && $item['balancetype'] == 'dr' && !empty($item['amount'])) {
                $table .= '<td style="font-size: 0.875rem;font-weight: 400;border-bottom: 1px solid #ddd;
                     padding: 15px 16px;text-align:right">' .   formatCurrency($item['amount'], $currencySystem, $number_of_decimal_places, null)  . '</td>';
            } else {
                $table .= '<td style="border-bottom: 1px solid #ddd;font-family:calibri;
                    color:#333;padding: 15px 16px;"></td>';
            }

            if (isset($item['balancetype']) && $item['balancetype'] == 'cr' && !empty($item['amount'])) {
                $table .= '<td style="font-size: 0.875rem;border-bottom: 1px solid #ddd;
                     color:#333;padding: 15px 16px;text-align:right">' . formatCurrency($item['amount'], $currencySystem, $number_of_decimal_places, null) . '</td>';
            } else {
                $table .= '<td style="border-bottom: 1px solid #ddd;font-family:calibri;
                    color:#333;padding: 15px 16px;"></td>';
            }

            if (isset($item['closing_balancetype']) && in_array($item['closing_balancetype'], ['cr', 'dr']) && !empty($item['closing_amount'])) {
                $table .= '<td style="border-bottom: 1px solid #ddd;font-family:calibri;
                     text-align: left;color:#333;padding: 15px 16px;">'  . formatCurrency($item['closing_amount'], $currencySystem, $number_of_decimal_places, null) . '  ' . $item['closing_balancetype'];
                '</td>';
            } else {
                $table .= '<td style="font-size: 0.875rem;font-weight: 400;line-height: 1.57;display: table-cell;vertical-align: inherit;border-bottom: 1px solid #ddd;
                     text-align: left;color:#333;padding: 15px 16px;"></td>';
            }


            $table .= '</tr>';
        }
    }

    $table .= '<tr style="color: inherit;display: table-row;vertical-align: middle;outline: 0px;border-bottom: 1px solid #ddd;">';
    $table .= '<td style="font-size: 0.875rem;font-weight: 400;line-height: 1.57;display: table-cell;vertical-align: inherit;border-bottom: 1px solid #ddd;
            text-align: right;color:#333;padding: 15px 16px;"><strong></strong></td>';
    $table .= '<td style="font-size: 0.875rem;font-weight: 400;line-height: 1.57;display: table-cell;vertical-align: inherit;border-bottom: 1px solid #ddd;
            text-align: right;color:#333;padding: 15px 16px;"><strong></strong></td>';
    $table .= '<td style="font-size: 0.875rem;font-weight: 400;line-height: 1.57;display: table-cell;vertical-align: inherit;border-bottom: 1px solid #ddd;
            text-align: right;color:#333;padding: 15px 16px;"><strong>Grand Total</strong></td>';

    $table .= '<td style="font-size: 0.875rem;font-weight: 400;line-height: 1.57;display: table-cell;vertical-align: inherit;border-bottom: 1px solid #ddd;
            text-align: left;color:#333;padding: 15px 16px;"> <strong>' . displayAmount($stats['transaction_total']['debit_total'], $currencySystem, $number_of_decimal_places, null) . '</strong></td>';
    $table .= '<td style="font-size: 0.875rem;font-weight: 400;line-height: 1.57;display: table-cell;vertical-align: inherit;border-bottom: 1px solid #ddd;
            text-align: right;color:#333;padding: 15px 16px;"><strong>' . displayAmount($stats['transaction_total']['credit_total'], $currencySystem, $number_of_decimal_places, null) . '</strong></td>';

    $table .= '</tr>';
    $table .= '</tbody>';
    $table .= '</table>';

    $book_template = createHeaderTemplate($specific_db_conn, $book_name, $company_name, $branch_name, $book_opening_date, $book_closing_date, $report_print_company_name, $report_print_branch_name, $report_print_book_name, $file, $function_code);

    $html = '
                ' . $book_template . '
                </div>
                <div style="padding: 10px;margin-top:10px; padding-bottom:10px;font-size:17px;">' . $ledgername[0]['ledger_name'] . '
                 </div>
                 
                 <div>
                   ' . $table . '
                </div>';

    echo $html;
}
