<?php
error_reporting(E_ALL);
ini_set('display_errors', '0');
try {

    if (!isset($_REQUEST['datefrom'])) {
        header('location: ../invoice.php');
        exit;
    } else {
        //$id = $_GET['print'];
    }


    $customer_name = $_GET['customer_name'];

    include_once('../../../classes/manager.class.php');
    include_once('../../../classes/PHPMailer/PHPMailerAutoload.php');
    include_once('../../../fpdf/fpdf1.php');


    $m = new Manager();
    extract($_REQUEST);
    $param_data = ControlConfifguration::GetConfiguration();
    define('PROJECT_NAME', $param_data->name);
    define('PROJECT_ADDRESS', $param_data->address);
    define('PROJECT_TEL', $param_data->contact);
    define('PROJECT_EMAIL', $param_data->email);
    define('PROJECT_WEBSITE', $param_data->website);
    define('PROJECT_COLOR', $param_data->color);
    define('PROJECT_DISCOUNT', $param_data->is_discount);
    define('PROJECT_TAX', $param_data->is_tax);

    $balanceAmount = 0;
    $totalCreditAmount = 0;
    $totalDebitAmount = 0;

    if ($datefrom == "") {
        $datefrom = date('Y-m-d', strtotime("-30 days"));
    } else {
        $datefrom = date("Y-m-d", strtotime($datefrom));
    }
    if ($dateto == "") {
        $dateto = date('Y-m-d');
    } else {
        $dateto = date("Y-m-d", strtotime($dateto));
    }
    $datefrom = date("d-m-Y", strtotime($datefrom));
    $dateto = date("d-m-Y", strtotime($dateto));

    $qryDate = date("Y-m-d", strtotime($datefrom));
    $qryTo = date("Y-m-d", strtotime($dateto));
    $filterReport = " JOIN customer_setup AS cs ON cs.id = inv.customer_id WHERE DATE_FORMAT(inv.create_date,'%Y-%m-%d')>='$qryDate' and DATE_FORMAT(inv.create_date,'%Y-%m-%d')<='$qryTo' ";
    if (isset($cat_name) && $cat_name != 'All') {
        if (isset($customer_name) && $customer_name != 'All') {
            $filterReport .= " AND inv.customer_id ='$customer_name'";
        } else {
            $cat = CustomerCategorySetup::GetCustomerCategorySetupById($cat_name);
            $filterCat = " WHERE category_id = '$cat->id'";
            $cust = CustomerSetup::GetCustomerSetupList($filterCat);
            $custArray = array();
            foreach ($cust AS $custRow) {
                $custArray[] = $custRow->id;
            }
            $customers = implode(',', $custArray);
            $filterReport .= " AND inv.customer_id IN (" . $customers . ")";
        }
    }
    if (isset($invoice_status) && $invoice_status != 'All') {
        $filterReport .= " AND inv.status = '$invoice_status'";
    } else {
        $filterReport .= " AND inv.status not in('Draft', 'Cancel')";
    }
    $filterReport .= " AND cs.status ='active'";

    $filterReport .= " UNION SELECT 'payment' AS nameTable, pay.create_date, pay.code, pay.customer_id, pay.invoice_reference_code, pay.payment_type, pay.amount,
pay.cheque_bank_name, pay.cheque_number, pay.cheque_payee_name, pay.cheque_date, pay.bank_name, pay.bank_account_number, pay.party_name, pay.from_bank_name,
pay.withholding_tax, pay.sales_tax, pay.goods_return, pay.shelf_rent, pay.builty_charges, pay.other_expense,'', '', '','', '', '', pay.description

 from " . TABLE_PAYMENT . " AS pay 
 JOIN customer_setup AS cs ON cs.id = pay.customer_id
 WHERE DATE_FORMAT(pay.create_date,'%Y-%m-%d')>='$qryDate' and DATE_FORMAT(pay.create_date,'%Y-%m-%d')<='$qryTo' AND pay.status <> 'Cancel' ";

    if (isset($cat_name) && $cat_name != 'All') {
        if (isset($customer_name) && $customer_name != 'All') {
            $filterReport .= " AND pay.customer_id ='$customer_name'";
        } else {
            $filterReport .= " AND pay.customer_id IN (" . $customers . ")";
        }
    }

    $filterReport .= " AND cs.status ='active'";
    $filterReport .= " ORDER BY create_date";

//Opening Balance
    $qryDatePrevious = date('Y-m-d', strtotime('-1 day', strtotime($qryDate)));

    $filterReportOpening = " JOIN customer_setup AS cs ON cs.id = inv.customer_id WHERE DATE_FORMAT(inv.create_date,'%Y-%m-%d')>='2016-01-01' and DATE_FORMAT(inv.create_date,'%Y-%m-%d')<='$qryDatePrevious' ";

    if (isset($cat_name) && $cat_name != 'All') {
        if (isset($customer_name) && $customer_name != 'All') {
            $filterReportOpening .= " AND inv.customer_id ='$customer_name'";
        } else {
            $filterReportOpening .= " AND inv.customer_id IN (" . $customers . ")";
        }
    }
    if (isset($invoice_status) && $invoice_status != 'All') {
        $filterReportOpening .= " AND inv.status = '$invoice_status'";
    } else {
        $filterReportOpening .= " AND inv.status not in('Draft', 'Cancel')";
    }
    $filterReportOpening .= " AND cs.status ='active'";

    $filterReportOpening .= " UNION SELECT 'payment' AS nameTable, pay.create_date, pay.code, pay.customer_id, pay.invoice_reference_code, pay.payment_type, pay.amount,
pay.cheque_bank_name, pay.cheque_number, pay.cheque_payee_name, pay.cheque_date, pay.bank_name, pay.bank_account_number, pay.party_name, pay.from_bank_name,
pay.withholding_tax, pay.sales_tax, pay.goods_return, pay.shelf_rent, pay.builty_charges, pay.other_expense,'', '', '','', '', '', pay.description
 from " . TABLE_PAYMENT . " AS pay 
 JOIN customer_setup AS cs ON cs.id = pay.customer_id
 WHERE DATE_FORMAT(pay.create_date,'%Y-%m-%d')>='2016-01-01' and DATE_FORMAT(pay.create_date,'%Y-%m-%d')<='$qryDatePrevious' AND pay.status <> 'Cancel' ";

    if (isset($cat_name) && $cat_name != 'All') {
        if (isset($customer_name) && $customer_name != 'All') {
            $filterReportOpening .= " AND pay.customer_id ='$customer_name'";
        } else {
            $filterReportOpening .= " AND pay.customer_id IN (" . $customers . ")";
        }
    }
    $filterReportOpening .= " AND cs.status ='active'";
    $filterReportOpening .= " ORDER BY create_date";
    //Opening Balance

    $qryOpening = Invoice::GetInvoicePaymentReportLists123($filterReportOpening);
    
    $opBalance = 0;
    $customerIds = array();
    if ($qryOpening) {
        foreach ($qryOpening as $qeryOpening) {
            if ($qeryOpening->nameTable == 'invoice') {
                $balanceAmount -= $qeryOpening->total_amount;
                $totalCreditAmount += $qeryOpening->total_amount;
            } else {
                //$payment_amount = intval($qeryOpening->amount) + intval($qeryOpening->withholding_tax) + intval($qeryOpening->sales_tax) + intval($qeryOpening->goods_return) + intval($qeryOpening->shelf_rent) + intval($qeryOpening->builty_charges) + intval($qeryOpening->other_expense);
                $payment_amount = intval($qeryOpening->amount);
                $balanceAmount += $payment_amount;
                $totalDebitAmount += $payment_amount;
            }
            if (!in_array($qeryOpening->customer_id, $customerIds)) {
                $customer = CustomerSetup::GetCustomerSetupById($qeryOpening->customer_id);
                $opBalance += $customer->balance;
                $customerIds[] = $qeryOpening->customer_id;
            }
        }
    }

    $invoice = Invoice::GetInvoicePaymentReportLists123($filterReport);

    foreach ($invoice as $qery) {
        if (!in_array($qery->customer_id, $customerIds)) {
            $customer = CustomerSetup::GetCustomerSetupById($qery->customer_id);
            $opBalance += $customer->balance;
            $customerIds[] = $qery->customer_id;
        }
    }
    $balanceAmount -= $opBalance;
    $totalCreditAmount += $opBalance;

    if (!empty($invoice)) {

        $font = 10;
        $picx = 100;
        $setx = 5;
        $multi = 210;
        $picsetx = 150;
        $w = array(50, 80, 50, 80);
        $path = "../../../";

        $pdf = new FPDFWithOutWaterMark('L', 'mm', 'A4');
        $pdf->SetMargins(0, 0, 0);

        $pdf->SetTitle('Category Customer Ledger Report');
        $pdf->AddPage();
        $pdf->Ln();
        $pdf->SetFont('Arial', 'B', 16);
        $pdf->Cell(0, 8, PROJECT_NAME, 0, 0, 'C');
        $pdf->SetX($setx);
        $pdf->SetFont('Arial', '', 9);
        $pdf->Cell(0, 8, 'Printed Date: ' . date('d-m-Y h:i A'), 0, 0, 'L');
        $pdf->Ln();
        $pdf->SetFont('Arial', 'B', 16);
        $pdf->Cell(0, 6, 'Ledger Report', 0, 0, 'C');

        if ($customer_name != 'All') {
            $customer = CustomerSetup::GetCustomerSetupById($customer_name);
            $pdf->Ln(2);
            $pdf->Ln();
            $pdf->SetFont('Arial', 'B', 14);
            $pdf->Cell(0, 6, $customer->name, 0, 0, 'C');
        }

        $pdf->Ln();
        $pdf->SetFont('Arial', '', 10);
        $pdf->Cell(0, 4, 'For the period of: ' . date('d-m-Y', strtotime($datefrom)) . ' - ' . date('d-m-Y', strtotime($dateto)), 0, 0, 'C');

        $pdf->Ln();
        $pdf->Ln();
        $pdf->SetX($setx);
        $header = array('S.No', 'Head', 'Date', 'Inv/Pay', 'Customer', 'Status', 'Pay. Type', 'Bank', 'Cheque', 'Chq date', 'Debit', 'Credit', 'Balance');

        function FancyTable($header, $data, $customer_name, $setx, $totalDebitAmount, $totalCreditAmount, $balanceAmount)
        {
            global $pdf;

            $totalDiscount = 0;
            $productQtyTotal = 0;
            $costPriceTotal = 0;
            $unitPriceTotal = 0;
            $totalProfit = 0;

            // Colors, line width and bold font
            $pdf->SetFillColor(224, 235, 255);
            $pdf->SetTextColor(0);
            $pdf->SetDrawColor(128, 0, 0);
            $pdf->SetLineWidth(.1);
            $pdf->SetFont('Arial', 'B', 10);
            // Header
            $wr = array(10, 17, 18, 16, 63, 15, 19, 28, 19, 19, 19, 20, 22);

            for ($i = 0; $i < count($header); $i++)
                $pdf->Cell($wr[$i], 7, $header[$i], 1, 0, 'C', true);
            $pdf->Ln();
            // Color and font restoration
            $pdf->SetFillColor(224, 235, 255);
            $pdf->SetTextColor(0);
            $pdf->SetFont('');
            // Data
            $fill = false;

            $counter = 0;
            $counter_pagebreak = 1;
            if ($data) {
                $countdown = 1;
                foreach ($data AS $row) {
                    $counter++;
                    if ($counter == 1) {
                        $pdf->SetX($setx);
                        $pdf->SetFont('Arial', 'B', 9);
                        $pdf->Cell($wr[0], 6, $counter, 'LRBT', 0, 'C', $fill);
                        $pdf->Cell($wr[1], 6, 'Opening', 'LRBT', 0, 'C', $fill);
                        $pdf->Cell($wr[2], 6, '-', 'LRBT', 0, 'R', $fill);
                        $pdf->Cell($wr[3], 6, '-', 'LRBT', 0, 'R', $fill);
                        $pdf->Cell($wr[4], 6, '-', 'LRBT', 0, 'R', $fill);
                        $pdf->Cell($wr[5], 6, '-', 'LRBT', 0, 'R', $fill);
                        $pdf->Cell($wr[6], 6, '-', 'LRBT', 0, 'R', $fill);
                        $pdf->Cell($wr[7], 6, '-', 'LRBT', 0, 'R', $fill);
                        $pdf->Cell($wr[8], 6, '-', 'LRBT', 0, 'R', $fill);
                        $pdf->Cell($wr[9], 6, '-', 'LRBT', 0, 'R', $fill);
                        $pdf->Cell($wr[10], 6, $totalDebitAmount, 'LRBT', 0, 'R', $fill);
                        $pdf->Cell($wr[11], 6, $totalCreditAmount, 'LRBT', 0, 'R', $fill);
                        $pdf->Cell($wr[12], 6, $balanceAmount, 'LRBT', 0, 'R', $fill);
                        $pdf->Ln();
                        $countdown++;
                        $counter++;
                    }

                    if ($row->nameTable == 'invoice') {
                        $invoiceNumber = $row->code;
                        $balanceAmount -= $row->total_amount;
                        $totalCreditAmount += $row->total_amount;
                        $customer = CustomerSetup::GetCustomerSetupById($row->customer_id);

                        $pdf->SetX($setx);
                        $pdf->SetFont('Arial', 'B', 9);
                        $pdf->Cell($wr[0], 6, $counter, 'LRBT', 0, 'C', $fill);
                        $pdf->Cell($wr[1], 6, 'Invoice', 'LRBT', 0, 'C', $fill);
                        $pdf->Cell($wr[2], 6, date('Y-m-d', strtotime($row->create_date)), 'LRBT', 0, 'R', $fill);
                        $pdf->Cell($wr[3], 6, $row->code, 'LRBT', 0, 'R', $fill);
                       // $pdf->Cell($wr[4], 6, ucwords(strtolower($customer->name)), 'LRBT', 0, 'L', $fill);
                      $pdf->Cell($wr[4], 6, ($customer->name), 'LRBT', 0, 'L', $fill);
                        $pdf->Cell($wr[5], 6, $row->status, 'LRBT', 0, 'L', $fill);
                        $pdf->Cell($wr[6], 6, '-', 'LRBT', 0, 'L', $fill);
                        $pdf->Cell($wr[7], 6, '-', 'LRBT', 0, 'R', $fill);
                        $pdf->Cell($wr[8], 6, '-', 'LRBT', 0, 'R', $fill);
                        $pdf->Cell($wr[9], 6, '-', 'LRBT', 0, 'R', $fill);
                        $pdf->Cell($wr[10], 6, 0, 'LRBT', 0, 'R', $fill);
                        $pdf->Cell($wr[11], 6, $row->total_amount, 'LRBT', 0, 'R', $fill);
                        $pdf->Cell($wr[12], 6, $balanceAmount, 'LRBT', 0, 'R', $fill);
                    }
                    $balanceAmount += intval($row->amount);
                    $totalDebitAmount += intval($row->amount);
                    if ($row->nameTable == 'payment') {
                        $customer = CustomerSetup::GetCustomerSetupById($row->customer_id);
                        $pdf->SetX($setx);
                        $pdf->SetFont('Arial', 'B', 9);
                        $pdf->Cell($wr[0], 6, $counter, 'LRBT', 0, 'C', $fill);
                        $pdf->Cell($wr[1], 6, 'Payment', 'LRBT', 0, 'C', $fill);
                        $pdf->Cell($wr[2], 6, date('Y-m-d', strtotime($row->create_date)), 'LRBT', 0, 'R', $fill);
                        $pdf->Cell($wr[3], 6, $row->code, 'LRBT', 0, 'R', $fill);
                       // $pdf->Cell($wr[4], 6, ucwords(strtolower($customer->name)), 'LRBT', 0, 'L', $fill);
                      $pdf->Cell($wr[4], 6, ($customer->name), 'LRBT', 0, 'L', $fill);
                        $pdf->Cell($wr[5], 6, '-', 'LRBT', 0, 'L', $fill);
                        $pdf->Cell($wr[6], 6, strtoupper($row->payment_type), 'LRBT', 0, 'L', $fill);
                        $pdf->Cell($wr[7], 6, $row->cheque_bank_name, 'LRBT', 0, 'L', $fill);
                        $pdf->Cell($wr[8], 6, $row->cheque_number, 'LRBT', 0, 'L', $fill);
                        $pdf->Cell($wr[9], 6, (($row->payment_type=='bank')?date('Y-m-d', strtotime($row->cheque_date)):''), 'LRBT', 0, 'L', $fill);
                        $pdf->Cell($wr[10], 6, $row->amount, 'LRBT', 0, 'R', $fill);
                        $pdf->Cell($wr[11], 6, 0, 'LRBT', 0, 'R', $fill);
                        $pdf->Cell($wr[12], 6, $balanceAmount, 'LRBT', 0, 'R', $fill);
                        
                        if($row->description != ''){
                            $fill = !$fill;
                            $pdf->Ln();
                            $pdf->SetX($setx);
                            $pdf->SetFont('Arial', 'B', 9);
                            $pdf->Cell($wr[0], 6, '', 'LBT', 0, 'C', $fill);
                            $pdf->Cell(275, 6, $row->description, 'RBT', 0, 'L', $fill);
                        }

                        // if($row->withholding_tax != 0){ 
                        //     $counter++;
                        //     $balanceAmount += intval($row->withholding_tax);
                        //     $totalDebitAmount += intval($row->withholding_tax);

                        //     $pdf->Ln();
                        //     $pdf->SetX($setx);
                        //     $pdf->SetFont('Arial', 'B', 9);
                        //     $pdf->Cell($wr[0], 6, $counter, 'LRBT', 0, 'C', $fill);
                        //     $pdf->Cell($wr[1], 6, 'Payment', 'LRBT', 0, 'C', $fill);
                        //     $pdf->Cell($wr[2], 6, date('Y-m-d', strtotime($row->create_date)), 'LRBT', 0, 'R', $fill);
                        //     $pdf->Cell($wr[3], 6, $row->code, 'LRBT', 0, 'R', $fill);
                        //   //  $pdf->Cell($wr[4], 6, ucwords(strtolower($customer->name)), 'LRBT', 0, 'L', $fill);
                        //   $pdf->Cell($wr[4], 6, ($customer->name), 'LRBT', 0, 'L', $fill);
                        //     $pdf->Cell($wr[5], 6, '-', 'LRBT', 0, 'L', $fill);
                        //     $pdf->Cell($wr[6], 6, "WH TAX", 'LRBT', 0, 'L', $fill);
                        //     $pdf->Cell($wr[7], 6, "", 'LRBT', 0, 'L', $fill);
                        //     $pdf->Cell($wr[8], 6, "", 'LRBT', 0, 'L', $fill);
                        //     $pdf->Cell($wr[9], 6, "", 'LRBT', 0, 'L', $fill);
                        //     $pdf->Cell($wr[10], 6, $row->withholding_tax, 'LRBT', 0, 'R', $fill);
                        //     $pdf->Cell($wr[11], 6, 0, 'LRBT', 0, 'R', $fill);
                        //     $pdf->Cell($wr[12], 6, $balanceAmount, 'LRBT', 0, 'R', $fill);
                        // }
                        // if($row->sales_tax != 0){ 
                        //     $counter++;
                        //     $balanceAmount += intval($row->sales_tax);
                        //     $totalDebitAmount += intval($row->sales_tax);

                        //     $pdf->Ln();
                        //     $pdf->SetX($setx);
                        //     $pdf->SetFont('Arial', 'B', 9);
                        //     $pdf->Cell($wr[0], 6, $counter, 'LRBT', 0, 'C', $fill);
                        //     $pdf->Cell($wr[1], 6, 'Payment', 'LRBT', 0, 'C', $fill);
                        //     $pdf->Cell($wr[2], 6, date('Y-m-d', strtotime($row->create_date)), 'LRBT', 0, 'R', $fill);
                        //     $pdf->Cell($wr[3], 6, $row->code, 'LRBT', 0, 'R', $fill);
                        //     //$pdf->Cell($wr[4], 6, ucwords(strtolower($customer->name)), 'LRBT', 0, 'L', $fill);
                        //     $pdf->Cell($wr[4], 6, ($customer->name), 'LRBT', 0, 'L', $fill);
                        //     $pdf->Cell($wr[5], 6, '-', 'LRBT', 0, 'L', $fill);
                        //     $pdf->Cell($wr[6], 6, "SALES TAX", 'LRBT', 0, 'L', $fill);
                        //     $pdf->Cell($wr[7], 6, "", 'LRBT', 0, 'L', $fill);
                        //     $pdf->Cell($wr[8], 6, "", 'LRBT', 0, 'L', $fill);
                        //     $pdf->Cell($wr[9], 6, "", 'LRBT', 0, 'L', $fill);
                        //     $pdf->Cell($wr[10], 6, $row->sales_tax, 'LRBT', 0, 'R', $fill);
                        //     $pdf->Cell($wr[11], 6, 0, 'LRBT', 0, 'R', $fill);
                        //     $pdf->Cell($wr[12], 6, $balanceAmount, 'LRBT', 0, 'R', $fill);
                        // }
                        // if($row->goods_return != 0){ 
                        //     $counter++;
                        //     $balanceAmount += intval($row->goods_return);
                        //     $totalDebitAmount += intval($row->goods_return);

                        //     $pdf->Ln();
                        //     $pdf->SetX($setx);
                        //     $pdf->SetFont('Arial', 'B', 9);
                        //     $pdf->Cell($wr[0], 6, $counter, 'LRBT', 0, 'C', $fill);
                        //     $pdf->Cell($wr[1], 6, 'Payment', 'LRBT', 0, 'C', $fill);
                        //     $pdf->Cell($wr[2], 6, date('Y-m-d', strtotime($row->create_date)), 'LRBT', 0, 'R', $fill);
                        //     $pdf->Cell($wr[3], 6, $row->code, 'LRBT', 0, 'R', $fill);
                        //     //$pdf->Cell($wr[4], 6, ucwords(strtolower($customer->name)), 'LRBT', 0, 'L', $fill);
                        //     $pdf->Cell($wr[4], 6, ($customer->name), 'LRBT', 0, 'L', $fill);
                        //     $pdf->Cell($wr[5], 6, '-', 'LRBT', 0, 'L', $fill);
                        //     $pdf->Cell($wr[6], 6, "GOODS RT", 'LRBT', 0, 'L', $fill);
                        //     $pdf->Cell($wr[7], 6, "", 'LRBT', 0, 'L', $fill);
                        //     $pdf->Cell($wr[8], 6, "", 'LRBT', 0, 'L', $fill);
                        //     $pdf->Cell($wr[9], 6, "", 'LRBT', 0, 'L', $fill);
                        //     $pdf->Cell($wr[10], 6, $row->goods_return, 'LRBT', 0, 'R', $fill);
                        //     $pdf->Cell($wr[11], 6, 0, 'LRBT', 0, 'R', $fill);
                        //     $pdf->Cell($wr[12], 6, $balanceAmount, 'LRBT', 0, 'R', $fill);
                        // }
                        // if($row->shelf_rent != 0){ 
                        //     $counter++;
                        //     $balanceAmount += intval($row->shelf_rent);
                        //     $totalDebitAmount += intval($row->shelf_rent);

                        //     $pdf->Ln();
                        //     $pdf->SetX($setx);
                        //     $pdf->SetFont('Arial', 'B', 9);
                        //     $pdf->Cell($wr[0], 6, $counter, 'LRBT', 0, 'C', $fill);
                        //     $pdf->Cell($wr[1], 6, 'Payment', 'LRBT', 0, 'C', $fill);
                        //     $pdf->Cell($wr[2], 6, date('Y-m-d', strtotime($row->create_date)), 'LRBT', 0, 'R', $fill);
                        //     $pdf->Cell($wr[3], 6, $row->code, 'LRBT', 0, 'R', $fill);
                        //     //$pdf->Cell($wr[4], 6, ucwords(strtolower($customer->name)), 'LRBT', 0, 'L', $fill);
                        //     $pdf->Cell($wr[4], 6, ($customer->name), 'LRBT', 0, 'L', $fill);
                        //     $pdf->Cell($wr[5], 6, '-', 'LRBT', 0, 'L', $fill);
                        //     $pdf->Cell($wr[6], 6, "SH RENT", 'LRBT', 0, 'L', $fill);
                        //     $pdf->Cell($wr[7], 6, "", 'LRBT', 0, 'L', $fill);
                        //     $pdf->Cell($wr[8], 6, "", 'LRBT', 0, 'L', $fill);
                        //     $pdf->Cell($wr[9], 6, "", 'LRBT', 0, 'L', $fill);
                        //     $pdf->Cell($wr[10], 6, $row->shelf_rent, 'LRBT', 0, 'R', $fill);
                        //     $pdf->Cell($wr[11], 6, 0, 'LRBT', 0, 'R', $fill);
                        //     $pdf->Cell($wr[12], 6, $balanceAmount, 'LRBT', 0, 'R', $fill);
                        // }
                        // if($row->builty_charges != 0){ 
                        //     $counter++;
                        //     $balanceAmount += intval($row->builty_charges);
                        //     $totalDebitAmount += intval($row->builty_charges);

                        //     $pdf->Ln();
                        //     $pdf->SetX($setx);
                        //     $pdf->SetFont('Arial', 'B', 9);
                        //     $pdf->Cell($wr[0], 6, $counter, 'LRBT', 0, 'C', $fill);
                        //     $pdf->Cell($wr[1], 6, 'Payment', 'LRBT', 0, 'C', $fill);
                        //     $pdf->Cell($wr[2], 6, date('Y-m-d', strtotime($row->create_date)), 'LRBT', 0, 'R', $fill);
                        //     $pdf->Cell($wr[3], 6, $row->code, 'LRBT', 0, 'R', $fill);
                        //     //$pdf->Cell($wr[4], 6, ucwords(strtolower($customer->name)), 'LRBT', 0, 'L', $fill);
                        //     $pdf->Cell($wr[4], 6, ($customer->name), 'LRBT', 0, 'L', $fill);
                        //     $pdf->Cell($wr[5], 6, '-', 'LRBT', 0, 'L', $fill);
                        //     $pdf->Cell($wr[6], 6, "BUILTY CHR", 'LRBT', 0, 'L', $fill);
                        //     $pdf->Cell($wr[7], 6, "", 'LRBT', 0, 'L', $fill);
                        //     $pdf->Cell($wr[8], 6, "", 'LRBT', 0, 'L', $fill);
                        //     $pdf->Cell($wr[9], 6, "", 'LRBT', 0, 'L', $fill);
                        //     $pdf->Cell($wr[10], 6, $row->builty_charges, 'LRBT', 0, 'R', $fill);
                        //     $pdf->Cell($wr[11], 6, 0, 'LRBT', 0, 'R', $fill);
                        //     $pdf->Cell($wr[12], 6, $balanceAmount, 'LRBT', 0, 'R', $fill);
                        // }
                        // if($row->other_expense != 0){ 
                        //     $counter++;
                        //     $balanceAmount += intval($row->other_expense);
                        //     $totalDebitAmount += intval($row->other_expense);

                        //     $pdf->Ln();
                        //     $pdf->SetX($setx);
                        //     $pdf->SetFont('Arial', 'B', 9);
                        //     $pdf->Cell($wr[0], 6, $counter, 'LRBT', 0, 'C', $fill);
                        //     $pdf->Cell($wr[1], 6, 'Payment', 'LRBT', 0, 'C', $fill);
                        //     $pdf->Cell($wr[2], 6, date('Y-m-d', strtotime($row->create_date)), 'LRBT', 0, 'R', $fill);
                        //     $pdf->Cell($wr[3], 6, $row->code, 'LRBT', 0, 'R', $fill);
                        //     //$pdf->Cell($wr[4], 6, ucwords(strtolower($customer->name)), 'LRBT', 0, 'L', $fill);
                        //     $pdf->Cell($wr[4], 6, ($customer->name), 'LRBT', 0, 'L', $fill);
                        //     $pdf->Cell($wr[5], 6, '-', 'LRBT', 0, 'L', $fill);
                        //     $pdf->Cell($wr[6], 6, "OT EXP", 'LRBT', 0, 'L', $fill);
                        //     $pdf->Cell($wr[7], 6, "", 'LRBT', 0, 'L', $fill);
                        //     $pdf->Cell($wr[8], 6, "", 'LRBT', 0, 'L', $fill);
                        //     $pdf->Cell($wr[9], 6, "", 'LRBT', 0, 'L', $fill);
                        //     $pdf->Cell($wr[10], 6, $row->other_expense, 'LRBT', 0, 'R', $fill);
                        //     $pdf->Cell($wr[11], 6, 0, 'LRBT', 0, 'R', $fill);
                        //     $pdf->Cell($wr[12], 6, $balanceAmount, 'LRBT', 0, 'R', $fill);
                        // }

                    }
                    if ($counter == 22 || ($counter != 22 && ($counter_pagebreak) % 25 == 0)) {
                        $counter_pagebreak = 1;
                        $pdf->AddPage();
                        $pdf->SetX($setx);
                        $pdf->SetFont('Arial', 'B', 10);
                        for ($i = 0; $i < count($header); $i++)
                            $pdf->Cell($wr[$i], 7, $header[$i], 1, 0, 'C', true);
                    }
                    $pdf->Ln();
                    $fill = !$fill;
                    $counter_pagebreak++;
                    $countdown++;
                }
            }
            //$pdf->SetFillColor(255, 204, 0);
            $pdf->SetX($setx);
            $pdf->SetFont('Arial', 'B', 9);
            $pdf->Cell($wr[0], 6, '', '', 0, 'L');
            $pdf->Cell($wr[1], 6, '', '', 0, 'L');
            $pdf->Cell($wr[2], 6, '', '', 0, 'L');
            $pdf->Cell($wr[3], 6, "", '', 0, 'R');
            $pdf->Cell($wr[4], 6, "", '', 0, 'R');

            $pdf->Cell($wr[5], 6, "", '', 0, 'R');
            $pdf->Cell($wr[6], 6, "", '', 0, 'R');
            $pdf->Cell($wr[7], 6, "", '', 0, 'R');
            $pdf->Cell($wr[8], 6, "Total", 'LBT', 0, 'R');
            $pdf->Cell($wr[9], 6, "", 'RBT', 0, 'R');
            $pdf->Cell($wr[10], 6, $totalDebitAmount, 'LRBT', 0, 'R');
            $pdf->Cell($wr[11], 6, $totalCreditAmount, 'LRBT', 0, 'R', "");
            if ($totalDebitAmount > $totalCreditAmount) {
                $cashType = 'Db';
            } else {
                $cashType = 'Cr';
            }
            $pdf->Cell($wr[12], 6, ($totalCreditAmount - $totalDebitAmount) . ' ' . $cashType, 'LRBT', 0, 'R', "");
        }

        FancyTable($header, $invoice, $customer_name, $setx, $totalDebitAmount, $totalCreditAmount, $balanceAmount);
    } else {
        header('location: ' . PROJECT_ADMIN_URL);
        exit;
    }

    $pdf->Output();
    if (ob_get_level() > 0) { ob_end_flush(); }
    exit;
} catch (Exception $e) {
    echo $e->getMessage();
}
?>