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

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

    $id = $_GET['id'];

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

    $qryee = InvoiceRate::GetInvoiceRateSetupById($id);
    $customer_category_id = $qryee->customer_category_id;

    $filterProduct = " WHERE status = 'active'";
    $qry = ProductSetup::GetProductSetupList($filterProduct);
    $filterInvoiceRateDetail = " WHERE invoice_rate_id = '$id' ";
    $invoice = InvoiceRate::GetInvoiceRateDetails($filterInvoiceRateDetail);

    if (!empty($invoice)) {

        $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('Invoice Rate');
        $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, 'Invoice Rate', 0, 0, 'C');

        $customersCat = CustomerCategorySetup::GetCustomerCategorySetupList();
        foreach ($customersCat AS $customersCatRow) {
            if ($customersCatRow->id == $customer_category_id) {
                $pdf->Ln();
                $pdf->SetFont('Arial', '', 14);
                $pdf->Cell(0, 6, $customersCatRow->name, 0, 0, 'C');
            }
        }

        $pdf->Ln();
        $pdf->Ln();
        $pdf->SetX($setx);
        $header = array('S.No', 'Product Name', 'Cost Price', 'Distr. Price', 'Sale Price');

        function FancyTable($header, $data, $id, $qry, $setx)
        {
            global $pdf;

            // Colors, line width and bold font
            $pdf->SetFillColor(224, 235, 255);
            $pdf->SetTextColor(0);
            $pdf->SetDrawColor(128, 0, 0);
            $pdf->SetLineWidth(.3);
            $pdf->SetFont('Arial', 'B', 9);
            // Header
            $wr = array(20, 95, 25, 25, 25);
            for ($i = 0; $i < count($header); $i++)
                $pdf->Cell($wr[$i], 7, $header[$i], 1, 0, 'C', true);
            // Color and font restoration
            $pdf->SetFillColor(224, 235, 255);
            $pdf->SetTextColor(0);
            $pdf->SetFont('');
            // Data
            $fill = false;

            $counter = 1;
            $counter_pagebreak = 1;
            if ($data) {
                $pdf->Ln();
                foreach ($qry AS $qryRow) {
                    if ($data) {
                        foreach ($data AS $row) {
                            if ($row->product_id == $qryRow->id && ($row->unit_cost_price != 0 && $row->unit_sale_price != 0)) {
                                $pdf->SetX($setx);
                                $pdf->SetFont('Arial', '', 8);
                                $pdf->Cell($wr[0], 7, $counter, 'LRBT', 0, 'C', $fill);
                                $pdf->Cell($wr[1], 7, $qryRow->name, 'LRBT', 0, 'L', $fill);
                                $pdf->Cell($wr[2], 7, $row->unit_cost_price, 'LRBT', 0, 'L', $fill);
                                $pdf->Cell($wr[3], 7, $row->unit_distributor_price, 'LRBT', 0, 'L', $fill);
                                $pdf->Cell($wr[4], 7, $row->unit_sale_price, 'LRBT', 0, 'R', $fill);

                                if ($counter == 25 || ($counter != 25 && ($counter_pagebreak) % 29 == 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->Ln();
            $pdf->Ln();
            $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');
            $pdf->Cell($wr[4], 7, "", '', 0, 'R');
        }

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

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