<?php
error_reporting('E_ALL');
ini_set('display_errors', '1');

try {

    if (!isset($_REQUEST['datefrom'])) {
        header('location: ../invoice.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);

   

    $datefrom = "";
    $dateto = "";
    $customer_name = "";
    $totalProfitPrevious = 0;
    $totalPaymentsPrevious = 0;
    $costPriceTotal = 0;
    $unitPriceTotal = 0;
    $totalDiscount = 0;
    $totalProfit = 0;
    $productQtyTotal = 0;
    $totalPayments = 0;
    $openingBalance = 0;

    extract($_REQUEST);
    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));

    if (isset($customer_name) && $customer_name != 'All') {
        $filterReport = " JOIN " . TABLE_CUSTOMERSETUP . " AS cust ON pay.customer_id = cust.id
    WHERE DATE_FORMAT(pay.create_date,'%Y-%m-%d')>='$qryDate' and DATE_FORMAT(pay.create_date,'%Y-%m-%d')<='$qryTo'  AND pay.status not in('Draft', 'Cancel') ";
        $filterReport .= " AND pay.customer_id ='$customer_name'";
        $filterReport .= " GROUP BY pay.code ";
    }else{
        $filterReport = " JOIN " . TABLE_CUSTOMERSETUP . " AS cust ON pay.customer_id = cust.id WHERE DATE_FORMAT(pay.create_date,'%Y-%m-%d')>='$qryDate' and DATE_FORMAT(pay.create_date,'%Y-%m-%d')<='$qryTo'  AND pay.status not in('Draft', 'Cancel') ";
        $filterReport .= " GROUP BY pay.customer_id ";
    }
    $filterReport .= " ORDER BY cust.name, customer_id, create_date, code ";



    if (!empty($filterReport)) {

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

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

        $pdf->SetTitle('Monthly Payment 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, 'Monthly Payment 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');

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

        $pdf->Ln();
        $pdf->Ln();
        $pdf->SetX($setx);
        if ($customer_name == '' || $customer_name == 'All') {
            $header = array('S.No', 'Customer', '# of Payments', 'Total');
        }else{
            $header = array('S.No', 'Date', 'Invoice #', 'Payment Type', 'Total');
        }


        function FancyTable($header, $filterReport, $setx, $customer_name, $m)
        {
            global $pdf;

            $productQtyTotal = 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', 9);
            // Header
            if ($customer_name == '' || $customer_name == 'All') {
                $wr = array(20, 117, 30, 30);
            }else{
                $wr = array(20, 85, 30, 30, 32);
            }
            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_pagebreak = 1;
            if ($customer_name != '' && $customer_name != 'All') {
                $qry = Invoice::GetInvoiceCustomerReportListMonthlyPayment($filterReport);
                $counter = 0;
                if ($qry) {
                    foreach ($qry as $qery) {
                        $counter++;

                        $pdf->SetX($setx);
                        $pdf->SetFont('Arial', '', 8);
                        $pdf->Cell($wr[0], 7, $counter, 'LRBT', 0, 'C', $fill);
                        $pdf->Cell($wr[1], 7, date('Y-m-d h:i A', strtotime($qery->create_date)), 'LRBT', 0, 'L', $fill);
                        $pdf->Cell($wr[2], 7, $qery->code, 'LRBT', 0, 'R', $fill);
                        $pdf->Cell($wr[3], 7, $qery->payment_type, 'LRBT', 0, 'R', $fill);
                        $pdf->Cell($wr[4], 7, number_format($qery->amount, 2, '.', ''), 'LRBT', 0, 'R', $fill);
                        $totalProfit += $qery->amount;

                        if ($counter == 38 || ($counter != 38 && ($counter_pagebreak) % 42 == 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_pagebreak++;
                    }
                }
            }else{
                $qry = Invoice::GetInvoiceCustomerReportListMonthlyPaymentCustomers($filterReport);
                $counter = 0;
                if ($qry) {
                    foreach ($qry as $qery) {
                        $counter++;
                        $customer = CustomerSetup::GetCustomerSetupById($qery->customer_id);
                        $pdf->SetX($setx);
                        $pdf->SetFont('Arial', '', 8);
                        $pdf->Cell($wr[0], 7, $counter, 'LRBT', 0, 'C', $fill);
                        $pdf->Cell($wr[1], 7, $customer->name, 'LRBT', 0, 'L', $fill);
                        $pdf->Cell($wr[2], 7, $qery->total_invoices, 'LRBT', 0, 'R', $fill);
                        $pdf->Cell($wr[3], 7, number_format($qery->total_amount, 2, '.', ''), 'LRBT', 0, 'R', $fill);
                        $totalProfit += $qery->total_amount;

                        if ($counter == 38 || ($counter != 38 && ($counter_pagebreak) % 42 == 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_pagebreak++;
                    }
                }
            }

            $pdf->SetX($setx);
            $pdf->SetFont('Arial', '', 9);
            $pdf->Cell($wr[0], 7, '', 'LB', 0, 'L');
            $pdf->Cell($wr[1], 7, '', 'LB', 0, 'L');        
            if ($customer_name != '' && $customer_name != 'All') {
                $pdf->Cell($wr[2], 7, '', 'LB', 0, 'L');
                $pdf->Cell($wr[3], 7, 'Total', 'LRB', 0, 'L');
                $pdf->Cell($wr[4], 7, number_format(($totalProfit), 2, '.', ''), 'LRB', 0, 'R');
            }else{
                $pdf->Cell($wr[2], 7, 'Total', 'LRB', 0, 'L');
                $pdf->Cell($wr[3], 7, number_format(($totalProfit), 2, '.', ''), 'LRB', 0, 'R');
            }
        }

        FancyTable($header, $filterReport, $setx, $customer_name, $m);
    } else {
        header('location: ' . PROJECT_ADMIN_URL);
        exit;
    }

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