<?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/transaction_template/template_1/header.php';
require_once $base_path . "vendor/autoload.php";
require_once $base_path_to_resources . "lib/currency_functions.php";

//valdation file
require_once $base_path . 'modules/accounting/stock_management/functions/stock_function.php';
require_once $base_path . 'modules/accounting/stock_management/inventory_regions/region_functions.php';
require_once $base_path . 'modules/accounting/book_management/basic_book_info_function.php';
require_once $base_path . 'modules/accounting/reports/valuation_methods/closing_stock_summary/avg_cp_summary_function.php';
require_once $base_path . 'modules/accounting/functions/basic_balance_function.php';

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

$book_info = GetBasicBook($conn, $workspace_id, $company_id, $branch_id, $book_id);
$sql = "SELECT * FROM act_book_settings WHERE workspace_id = ? AND company_id = ? AND branch_id = ? AND book_id = ?";
$stmt = $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_bank_details_1 = $result[0]['print_bank_details_1'];
    $print_bank_details_2 = $result[0]['print_bank_details_2'];
    $print_logo_bool = $result[0]['print_logo_bool'];
    if ($print_logo_bool == 1) {
        if ($result[0]['logo'] !=  null) {

            $link = 'https://rejoicebilldesk.in/rjlbilldesk_development/';

            $file = $link . $logo;
        }
    } else {

        $file =  null;
    }
    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;
    }

    if ($print_bank_details_1 == 1) {
        if ($result[0]['bank_details_1'] !=  null) {
            $bank_details_1 = $result[0]['bank_details_1'];
            $unserializedbank1 = unserialize($bank_details_1);
            $data = json_decode($unserializedbank1, true);

            $bank_name = $data['bank_name'];
            $account_number = $data['account_number'];
            $ifsc_code = $data['ifsc_code'];
            // print_r($unserializedbank1);
        } else {
            $bank_details_1 =  null;
        }
    }

    if ($print_bank_details_2 == 1) {
        if ($result[0]['bank_details_2'] !=  null) {
            $bank_details_2 = $result[0]['bank_details_2'];
            $unserializedbank2 = unserialize($bank_details_2);
            $data = json_decode($unserializedbank2, true);
            $bank_name = $data['bank_name'];
            $account_number = $data['account_number'];
            $ifsc_code = $data['ifsc_code'];
        } else {
            $bank_details_2 =  null;
        }
    }
} else {
    errorreturn('Branch or company not found.');
}

$result = (['assets' => $assets, 'liabilities' => $liabilities, 'assets_total' => $assets_total, 'liabilities_total' => $liabilities_total]);
// print_r($result);die();
$assets = $result['assets'];

$liabilities = $result['liabilities'];
// $liabilities_data = $liabilities['data'];
// print_r($liabilities);die();
$assets_total = $result['assets_total'];
// print_R($liabilities);die();
$liabilities_total = $result['liabilities_total'];
$table = '<div style="width: 100%;">
    <table style=" width: 100%;" cellpadding="0px" cellspacing="0px">
        <tr style="width: 50%; border-right: 1px solid #000;">
        <td style="border-right:1px solid #333">
            <h5 style="background: #ddd; height: 30px; padding: 5px; padding-top: 10px; margin: 0px;">Liabilities</h5>';

foreach ($liabilities as $data) {
    foreach ($data['data'] as $innerData1) {
        $table .= '
            <h5 style="padding: 5px 0px 3px 8px;">' . $data['bs_group_name'] . ': <span style="float: right; padding-right: 10px;">' . $data['total_closing'] . '</span></h5>
            <h5 style="padding: 5px 0px 10px 8px; color: #A0A0A0; border-bottom: 1px solid #ddd;">' . $innerData1['name'] . ': <span style="float: right; padding-right: 10px;">' . $innerData1['net_closing_balance'] . ' </span></h5>
            ';
    }
}

$table .= ' <h5 style="padding: 5px 0px 3px 8px;">' . $data['bs_group_name'] . ': <span style="float: right; padding-right: 10px;">' . $data['total_closing'] . '</span></h5>
       <h5 style="padding:5px 0px 3px 8px;height: 30px;background: #ddd;margin: 0px;text-align:right;padding-right:10px">
             ' . $liabilities_total . '
            </h5> ';

$table .= '</td>
<td style="width: 50%; border-right: 1px solid #000;">

        <h5 style="background: #ddd; height: 30px; padding: 5px; padding-top: 10px; margin: 0px;">Assets</h5>';

foreach ($assets as $data) {
    foreach ($data['data'] as $innerData1) {
        $table .= '
            <h5 style="padding: 5px 0px 3px 8px;">' . $data['bs_group_name'] . ': <span style="float: right; padding-right: 10px;">' . $data['total_closing'] . '</span></h5>
            <h5 style="padding: 5px 0px 10px 8px; color: #A0A0A0; border-bottom: 1px solid #ddd;">' . $innerData1['name'] . ': <span style="float: right; padding-right: 10px;">' . $innerData1['net_closing_balance'] . ' </span></h5>
            ';
    }
}

$table .= ' <h5 style="padding: 5px 0px 3px 8px;">' . $data['bs_group_name'] . ': <span style="float: right; padding-right: 10px;">' . $data['total_closing'] . '</span></h5>
   <h5 style="padding:5px 0px 3px 8px;height: 30px;background: #ddd;margin: 0px;text-align:right;padding-right:10px">
             ' . $assets_total . '
            </h5>';

$table .= '</td></tr>
</table></div>';



try {
    ob_start();
    $html = ob_get_clean();
    $html2pdf = new Html2Pdf('P', 'A4', 'fr');
    $html2pdf->pdf->SetDisplayMode('fullpage');
    $html2pdf->setDefaultFont('Arial');

    // $book_template = createHeaderTemplate($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 = '<div style="width:60%;margin:0px auto">
         <div style="margin-right: -15px; margin-left: -15px;">
        <div  style="background: #ffffff none repeat scroll 0 0;border-bottom: 12px; solid #333333;margin-top: 50px;margin-bottom: 50px;position: relative;box-shadow: 0 1px 21px #acacac;color: #333333;">
        
         ' . $table . '
         </div></div></div></div>';
    print_r($html);
    die();

    $html2pdf->WriteHTML($html); // in $content you put your content to have in pdf
    $html2pdf->Output('Manage Balancesheet - Rejoice Finware.pdf');
} catch (Html2PdfException $e) {
    $html2pdf->clean();
    $formatter = new ExceptionFormatter($e);
    echo $formatter->getHtmlMessage();
}
