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

    $cat_nameString = "";
    if (!empty($cat_name) && (isset($cat_name[1]) || $cat_name[0] != "All")) {
        if (in_array("All", $cat_name)) {
            $catKey = array_search('All', $cat_name);
            unset($cat_name[$catKey]);
        }
        $cat_nameString = implode("','", $cat_name);
    }

    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 = " LEFT JOIN customer_setup ON invoice.customer_id = customer_setup.id WHERE DATE_FORMAT(invoice.create_date,'%Y-%m-%d')>='$qryDate' and DATE_FORMAT(invoice.create_date,'%Y-%m-%d')<='$qryTo' and invoice.status <> 'Cancel' ";
    if (isset($cat_name) && (isset($cat_name[1]) || $cat_name[0] != "All")) {
        $filterReport .= " AND customer_setup.category_id IN ('$cat_nameString')";
    }
//    if (isset($cat_name) && $cat_name != 'All') {
//        $filterReport .= " AND customer_setup.category_id ='$cat_name'";
//    }
    if (isset($customer_name) && $customer_name != 'All') {
        $filterReport .= " AND invoice.customer_id ='$customer_name'";
    }
    $filterReport .= " ORDER BY invoice.code ";
    $qry = Invoice::GetInvoiceListJoinCustomer($filterReport);
    $custname = CustomerCategorySetup::GetCustomerCategorySetupById($cat_name);
    if($qry){
        foreach ($qry AS $qryRow) {
            $invoices[] = $qryRow->invoiceid;
            $invoices_discount[] = $qryRow->discount;
        }
    }

    if (!empty($invoices)) {

        $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 Product 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 Product 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('S.No', 'Product', 'Qty', 'Amount');

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

        function FancyTable($header, $data, $setx, $data_discount)
        {
            $products = array();
            $products_amount = array();
            if($data){
                foreach($data AS $invoiceKey => $invoicesRow){
                    $filterReportProduct = " WHERE invoice_id = '$invoicesRow'";
                    $qrys = Invoice::GetInvoiceProducts($filterReportProduct);
                    $discount = $data_discount[$invoiceKey];
                    if($qrys){
                        foreach($qrys AS $qrysRow){
                            $products[$qrysRow->product_id] += $qrysRow->product_qty;
                            $products_amount[$qrysRow->product_id] += (($qrysRow->product_qty*$qrysRow->unit_sale_price)-(($qrysRow->product_qty*$qrysRow->unit_sale_price)*$discount/100));
                        }
                    }
                }
            }
            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
            $wr = array(20, 130, 20, 27);
            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;
            $totalAmount = 0;
            $productAmountTotal = 0;
            if($products){
                foreach ($products AS $productsqeryKey => $productsqery) {
                    $productName = ProductSetup::GetProductSetupById($productsqeryKey);
                    $productQtyTotal += $productsqery;
                    $productAmountTotal += $products_amount[$productsqeryKey];

                    $pdf->SetX($setx);
                    $pdf->SetFont('Arial', '', 8);
                    $pdf->Cell($wr[0], 7, $counter, 'LRBT', 0, 'C', $fill);
                    $pdf->Cell($wr[1], 7, $productName->name, 'LRBT', 0, 'L', $fill);
                    $pdf->Cell($wr[2], 7, $productsqery, 'LRBT', 0, 'R', $fill);
                    $pdf->Cell($wr[3], 7, $products_amount[$productsqeryKey], 'LRBT', 0, 'R', $fill);

                    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++;
                    $counter_pagebreak++;
                }
            }
            //$pdf->SetFillColor(255, 204, 0);
            $pdf->SetX($setx);
            $pdf->SetFont('Arial', '', 9);
            $pdf->Cell($wr[0], 7, '', 'LB', 0, 'L');
            $pdf->Cell($wr[1], 7, 'Total', 'LRB', 0, 'L');
            $pdf->Cell($wr[2], 7, $productQtyTotal, 'LRB', 0, 'R');
            $pdf->Cell($wr[3], 7, $productAmountTotal, 'LRB', 0, 'R');
        }

        FancyTable($header, $invoices, $setx, $invoices_discount);
    } else {
        header('location: ' . PROJECT_ADMIN_URL);
        exit;
    }

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