<?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 . 'modules/accounting/groups_ledgers/functions/view_month_ledger_functions.php';
require_once $base_path . 'modules/accounting/functions/basic_balance_function.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;

 
   $ledgerFields = array('month'=>'Month',
    'debit_total'=>'Debit',
    'credit_total'=>'Credit',
    'month_closing'=>'Month Closing',
  
    );
    $additionaldata = array();
    $additionaldata = ['ledger_opening_bal', 'ledger_parent_group'];
    $ledger_detail = GetBasicLedgerDetail($specific_db_conn, $ledgerid, $workspace_id, $company_id, $branch_id, $book_id, $additionaldata);
    $ledger_opening_bal = $ledger_detail[0]['ledger_opening_bal'];
    $data =  viewLedgerMonthly($specific_db_conn, $ledgerid, $workspace_id, $company_id, $branch_id, $book_id, $ledger_opening_bal, $mindate, $maxdate, $book_opening_date);
    // print_R($data);
    $ledgers = $data['monthly_data'];
    $ledgers_remove_keys=removeKeys2($ledgers);
    $values=getArrayValues($ledgers_remove_keys);
    // print_r($values);die();
    $keys = getArrayKeys($ledgers[0]);
    $formattedKeys = getArrayKeys($ledgers[0]);
    
    foreach ($keys as $index => $k) {
        $formattedKeys[$index] = getCorrespondingValue($ledgerFields, $k);
    }
    // return array of keys and values

    $data = array("values" => $values,"keys" => $formattedKeys);

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

function createTable($keys, $values, $specific_db_conn,$workspace_id, $company_id, $branch_id, $book_id)
{
     $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'];
    //print_r($keys,$values);
    $count = 1;

    $thead = '<tr>';
    
    foreach ($keys as $item) {
        $thead .= '
            <th>' . $item . '</th>
        ';
    }
    $thead .= '</tr>';
    $tbody = '';
    foreach ($values as $v) {
        $tbody .= '<tr >';
        

         foreach ($v as $item) {
        // Check if it's the 'Current Balance' or 'Opening Balance' column
       if (is_numeric($item)) { // Check if the item is a numeric value (assumed to be an amount)
            // Apply right alignment to the amount columns
            
            $tbody .= '<td style="text-align: right;font-family:calibri">' . displayAmount($item,$currencySystem,$number_of_decimal_places,null) . '</td>';
        } else {
            $tbody .= '<td style="font-family:calibri">' . $item . '</td>';
        }
    }
        $tbody .= '</tr>';
    }

$table = '
    <table style="width: 100%; border-collapse: collapse;" border="1" cellspacing="10" cellpadding="5">
        <thead>
            ' . $thead . '
        </thead>
        <tbody>
            ' . $tbody . '
        </tbody>
    </table>';

    return $table;
}
 


function createPrint( $data,$specific_db_conn,$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.');
    }
     
    
    
     // create header for book
     $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);

    //  print_R($book_template);
     //generate table
     $table = createTable($data['keys'], $data['values'], $specific_db_conn,$workspace_id, $company_id, $branch_id, $book_id);
        
     // Initial HTML content
    $initialHtml = '<div>' . $book_template . '</div>
                      <div style="margin-top:10px; padding-bottom:10px;font-size:20pxl;font-family:calibri">' . $ledgername[0]['ledger_name'] . '
        </div>
                   </div>';
    
    // Concatenate the table HTML with the initial HTML
    $html = '<div>' . $initialHtml . '<div>' . $table . '</div></div>';
    
    // Printing the generated HTML
    echo $html;
    

}
?>
