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

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

    $product_id = $_GET['product_id'];
    $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);

    if (isset($customer_name) && $customer_name == 'customer') {
        $filterReport = " WHERE is_mart = '0' AND status = 'active' ORDER BY name ";
        $filterReport2 = " WHERE is_mart = '1' AND status = 'active' ORDER BY name ";
    } else if (isset($customer_name) && $customer_name == 'mart') {
        $filterReport1 = " WHERE status = 'active' ORDER BY name ";
    } else {
        $filterReport = " WHERE is_mart = '0' AND status = 'active' ORDER BY name ";
        $filterReport2 = " WHERE is_mart = '1' AND status = 'active' ORDER BY name ";
        $filterReport1 = " WHERE status = 'active' ORDER BY name";
    }

    $customer = CustomerSetup::GetCustomerSetupList($filterReport);
    $customer2 = CustomerSetup::GetCustomerSetupList($filterReport2);
    if (!empty($customer)) {

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

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

        $pdf->SetTitle('Customer List 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, 'Customer List Report', 0, 0, 'C');



        $pdf->Ln();
        $pdf->Ln();
        $pdf->SetX($setx);
        $header = array('S.No', 'Name', 'Phone', 'Location');


        function FancyTable($header, $data, $data2,  $setx, $customer_name, $filterReport1)
        {
            global $pdf;

            // Colors, line width and bold font
            $pdf->SetFillColor(224, 235, 255);
            $pdf->SetTextColor(0);
            $pdf->SetDrawColor(128, 0, 0);
            $pdf->SetLineWidth(.3);
            // Header
            $wr = array(10, 60,40,82);

            if (isset($data) && ($customer_name == 'customer' || $customer_name == 'All')) {
                $pdf->SetX($setx);
                $pdf->SetFont('Arial', 'B', 16);
                $pdf->Cell(200, 7, 'Local Customers', '', 0, 'C', '');
                $pdf->Ln();
                $pdf->SetFont('Arial', 'B', 9);
                $pdf->SetX($setx);
                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;

                foreach ($data as $row) {
                    $pdf->SetX($setx);
                    $pdf->SetFont('Arial', '', 8);
                    $pdf->Cell($wr[0], 7, $counter, 'LRBT', 0, 'C', $fill);
                    $pdf->Cell($wr[1], 7, $row->name, 'LRBT', 0, 'L', $fill);
                    $pdf->Cell($wr[2], 7, $row->contact, 'LRBT', 0, 'L', $fill);
                    $pdf->Cell($wr[3], 7, $row->location, 'LRBT', 0, 'L', $fill);
                    if($counter == 33 || ($counter != 33 && ($counter_pagebreak)%37==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++;
                }


                //Local Mart Customers
                $pdf->AddPage();
                $pdf->SetX($setx);
                $pdf->SetX($setx);
                $pdf->SetFont('Arial', 'B', 16);
                $pdf->Cell(200, 7, 'Local Mart', '', 0, 'C', '');
                $pdf->Ln();
                $pdf->SetFont('Arial', 'B', 9);
                $pdf->SetX($setx);
                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;

                foreach ($data2 as $row) {
                    $pdf->SetX($setx);
                    $pdf->SetFont('Arial', '', 8);
                    $pdf->Cell($wr[0], 7, $counter, 'LRBT', 0, 'C', $fill);
                    $pdf->Cell($wr[1], 7, $row->name, 'LRBT', 0, 'L', $fill);
                    $pdf->Cell($wr[2], 7, $row->contact, 'LRBT', 0, 'L', $fill);
                    $pdf->Cell($wr[3], 7, $row->location, 'LRBT', 0, 'L', $fill);
                    if($counter == 33 || ($counter != 33 && ($counter_pagebreak)%37==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++;
                }
            }

            if ($customer_name == 'mart' || $customer_name == 'All') {
                if ($_SESSION['clone'] == 1) {
                    $wr = array(10, 60,72,50);

                    global $db;
                    $db = new Database(DB_SERVER, DB_USER, DB_PASS, DB_DATABASE);
                    $db->connect();
                    $dataa = CustomerSetup::GetCustomerSetupList($filterReport1);
                    if ($dataa) {
                        if ($customer_name == 'customer' || $customer_name == 'All') {
                            $pdf->AddPage();
                        }
                        $pdf->SetX($setx);
                        $pdf->SetFont('Arial', 'B', 16);
                        $pdf->Cell(200, 7, 'Marts', '', 0, 'C', '');
                        $pdf->Ln();
                        $pdf->SetFont('Arial', 'B', 9);
                        $pdf->SetX($setx);
                        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;

                        foreach ($dataa as $row) {
                            $pdf->SetX($setx);
                            $pdf->SetFont('Arial', '', 8);
                            $pdf->Cell($wr[0], 7, $counter, 'LRBT', 0, 'C', $fill);
                            $pdf->Cell($wr[1], 7, $row->name, 'LRBT', 0, 'L', $fill);
                            $pdf->Cell($wr[2], 7, $row->contact, 'LRBT', 0, 'L', $fill);
                            $pdf->Cell($wr[3], 7, $row->location, 'LRBT', 0, 'L', $fill);
                            if($counter == 33 || ($counter != 33 && ($counter_pagebreak)%37==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++;
                        }
                    }
                    global $db;
                    $db = new Database(DB_SERVERS, DB_USERS, DB_PASS, DB_DATABASES);
                    $db->connect();
                }
            }

            $pdf->SetX($setx);
            $pdf->SetFont('Arial', '', 8);
            $pdf->Cell($wr[0], 7, '', '', 0, 'L');
            $pdf->Cell($wr[1], 7, '', '', 0, 'L');
            $pdf->Cell($wr[2], 7, "", '', 0, 'R');
            $pdf->Cell($wr[3], 7, "", '', 0, 'R');
        }

        FancyTable($header, $customer, $customer2, $setx, $customer_name, $filterReport1);
    } else {
        header('location: ' . PROJECT_ADMIN_URL);
        exit;
    }

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