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

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


    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);

    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));


    $filterReportOpening = " WHERE DATE_FORMAT(create_date,'%Y-%m-%d')>='2016-01-01' and DATE_FORMAT(create_date,'%Y-%m-%d')<'$qryDate' and status <> 'Cancel' ";
    $filterReport = " WHERE DATE_FORMAT(create_date,'%Y-%m-%d')>='$qryDate' and DATE_FORMAT(create_date,'%Y-%m-%d')<='$qryTo' and status <> 'Cancel' ";

    if (isset($bank_name) && $bank_name != 'All') {
        $filterReport .= " AND statement_bank_id ='$bank_name'";
        $filterReportOpening .= " AND statement_bank_id ='$bank_name'";
      
    }
   // $filterReport .= " ORDER BY id, create_date";
    $filterReport .= " ORDER BY create_date, id";
    $qry = BankTransactionSetup::GetBankTransactionSetupList($filterReport);
    if (!empty($qry)) {

        if($bank_name == 'All'){
            $bankname = 'All';
        }else{
            $bank = StatementBankSetup::GetBankSetupById($bank_name);
            $bankname = $bank->bank_name;
        }

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

        $pdf = new FPDFWithOutWaterMark('P', 'mm', 'legal');
        $pdf->SetMargins(0, 0, 0);

        $pdf->SetTitle('Bank Transaction 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', '', 8);
        $pdf->Cell(0, 8, 'Printed Date: ' . date('d-m-Y h:i A'), 0, 0, 'L');
        $pdf->Ln();
        $pdf->SetFont('Arial', '', 14);
        $pdf->Cell(0, 6, $bankname.' Bank Transaction Report', 0, 0, 'C');

        $pdf->Ln();
        $pdf->SetFont('Arial', '', 8);
        $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->SetFont('Arial', '', 12);
        $pdf->Cell(0, 6, ucwords($custname->name), 0, 0, 'C');

        $pdf->Ln();
        $pdf->Ln();
        $pdf->SetX($setx);
        $header = array('Date', 'Particulars', 'Pay Type', 'Bank Name', 'Cheque No.', 'Debit', 'Credit', 'Balance');

//        $filterProductInvoice = " WHERE invoice_id = '$invoice_id'";
//        $productData = Invoice::GetInvoiceProducts($filterProductInvoice);

        function FancyTable($header, $data, $setx, $filterReportOpening, $qryDate)
        {
            global $pdf;


            // 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', 9);
            // Header
            $wr = array(18, 55, 22, 26, 22, 18, 18, 18);
            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 = 1;
            $counter_pagebreak = 1;
            $balanceOpening = 0;
            $balance = 0;

            $qryOpening = BankTransactionSetup::GetOpeningbalance($filterReportOpening);
            $balanceOpening = 0;
            $balance = 0;
            if($qryOpening){
                foreach($qryOpening AS $qryOpeningRow){
                    if($qryOpeningRow->type == 'debit'){
                        $balanceOpening -= $qryOpeningRow->amount;
                    }else{
                        $balanceOpening += $qryOpeningRow->amount;
                    }
                }
                $balance = $balanceOpening;
            }

            $pdf->SetX($setx);
            $pdf->SetFont('Arial', '', 8);
            $pdf->Cell($wr[0], 7, date('d - M', strtotime($qryDate)), 'LRBT', 0, 'C', $fill);
            $pdf->Cell($wr[1], 7, 'Balance', 'LRBT', 0, 'L', $fill);
            $pdf->Cell($wr[2], 7, '', 'LRBT', 0, 'R', $fill);
            $pdf->Cell($wr[3], 7, '', 'LRBT', 0, 'R', $fill);
            $pdf->Cell($wr[4], 7, '', 'LRBT', 0, 'R', $fill);
            $pdf->Cell($wr[5], 7, '', 'LRBT', 0, 'R', $fill);
            $pdf->Cell($wr[6], 7, '', 'LRBT', 0, 'R', $fill);
            $pdf->Cell($wr[7], 7, $balance, 'LRBT', 0, 'R', $fill);
            $pdf->Ln();

            if ($data) {
                foreach ($data as $qery) {
                    if ($qery->type == 'debit') {
                        $balance -= $qery->amount;
                    } else {
                        $balance += $qery->amount;
                    }
                    $pdf->SetX($setx);
                    $pdf->SetFont('Arial', '', 8);
                    $pdf->Cell($wr[0], 7, date('d - M', strtotime($qery->create_date)), 'LRBT', 0, 'C', $fill);
                    $pdf->Cell($wr[1], 7, $qery->particulars, 'LRBT', 0, 'L', $fill);
                    $bank_name = "";
                    if($qery->payment_type == 'cash'){
                        $payment_type = 'Cash';
                    }else if($qery->payment_type == 'cheque'){
                        $payment_type = 'Cheque';
                        $bank_name = $qery->cheque_bank;
                    }else if($qery->payment_type == 'online'){
                        $payment_type = 'Online';
                        $bank_name = $qery->online_bank;
                    }
                    $pdf->Cell($wr[2], 7, $payment_type, 'LRBT', 0, 'L', $fill);
                    $pdf->Cell($wr[3], 7, $bank_name, 'LRBT', 0, 'L', $fill);
                    $pdf->Cell($wr[4], 7, $qery->cheque_number, 'LRBT', 0, 'L', $fill);
                    $pdf->Cell($wr[5], 7, ($qery->type == 'debit' ? $qery->amount : ''), 'LRBT', 0, 'R', $fill);
                    $pdf->Cell($wr[6], 7, ($qery->type == 'credit' ? $qery->amount : ''), 'LRBT', 0, 'R', $fill);
                    $pdf->Cell($wr[7], 7, $balance, 'LRBT', 0, 'R', $fill);

                    if ($counter == 40 || ($counter != 40 && ($counter_pagebreak) % 44 == 0)) {
                        $counter_pagebreak = 1;
                        $pdf->AddPage();
                        $pdf->SetX($setx);
                        $pdf->SetFont('Arial', 'B', 9);
                        for ($i = 0; $i < count($header); $i++)
                            $pdf->Cell($wr[$i], 7, $header[$i], 1, 0, 'C', true);
                    }


                    $pdf->Ln();
                    $fill = !$fill;
                    $counter++;
                    $counter_pagebreak++;
                }
            }
        }

        FancyTable($header, $qry, $setx, $filterReportOpening, $qryDate);
    } else {
        header('location: ' . PROJECT_ADMIN_URL);
        exit;
    }

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