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

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

    }
//    echo "<pre>";
//    print_r($_REQUEST);
//    echo "</pre>";
//    exit;
    include_once('../../../classes/manager.class.php');
    include_once('../../../classes/PHPMailer/PHPMailerAutoload.php');
    include_once('../../../fpdf/fpdf1.php');

    $m = new Manager();

    $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);
    define('PROJECT_TAX_AMOUNT',$param_data->tax);

    if (PROJECT_TAX == 1) {
        $tax = PROJECT_TAX_AMOUNT;
        $is_tax = PROJECT_TAX;
    }

    $rateqry = RateList::GetRateListSetupById($_REQUEST['id']);
    $customer_name = $rateqry->customer_name;
    $type = $rateqry->type;
    $rate_date = date('d-m-Y', strtotime($rateqry->rate_date));

    $filterRatelist = " WHERE rate_list_id = '".$_REQUEST['id']."'";
    $ratelistdetail = RateList::GetRateListDetails($filterRatelist);
    foreach($ratelistdetail AS $ratelistdetails){
        $products[] = $ratelistdetails->product_id;
        $tpRate[] = $ratelistdetails->tp_rate;
        $discount_input = $ratelistdetails->discount;
        $piece_carton[] = $ratelistdetails->piece_carton;
    }
    $customer_name = $customer_name;
    $date = $rate_date;
    //print_r($products);
    $product = implode(',',$products);

    if (!empty($products)) {

//        $invoice_id = $invoice->id;
//        $create_date = $invoice->create_date;
//        $code = $invoice->code;
//        $customer_id = $invoice->customer_id;
//        $discount = $invoice->discount;
//
//        $customer = CustomerSetup::GetCustomerSetupById($customer_id);
//        $customer_name = $customer->name;
//        $customer_address = $customer->location;

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

        //$pdf = new FPDF('P', 'mm', array('8.5','11'));

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

        $pdf->SetTitle('Rate List');
        $pdf->AddPage();
        $pdf->SetFont('Arial', 'B', 18);
        $pdf->Cell(0, 6, DANIYAL FOOD INDUSTRIES (PVT) LTD., 0, 0, 'C');

      
        //$pdf->Cell(0, 6, PROJECT_NAME, 0, 0, 'C');

        $pdf->Ln();
        $pdf->SetFont('Arial', 'B', 10);
        $pdf->Cell(55, 8, '', '');
        $pdf->Cell(105, 8, 'Contact Person : MUHAMMAD ASIF HUSSAIN - 0300-2219949' , 'TLR', 0, 'C');
        $pdf->Cell(50, 8, '', '');

        $pdf->Ln();
        $pdf->SetFont('Arial', 'B', 10);
        $pdf->Cell(55, 8, '', '');
        $pdf->Cell(105, 8, 'UAN : ' . PROJECT_TEL, 'TLR', 0, 'C');
        $pdf->Cell(50, 8, '', '');

        $pdf->Ln();
        $pdf->SetFont('Arial', 'B', 10);
        $pdf->Cell(55, 8, '', '');
        $pdf->Cell(105, 6, PROJECT_EMAIL . " / " . PROJECT_WEBSITE, 'BLR', 0, 'C');
        $pdf->Cell(50, 8, '', '');

        $pdf->Ln();
        $pdf->SetFont('Arial', 'B', 14);
        $pdf->Cell(0, 8, 'RATE LIST', 0, 0, 'C');

        $pdf->Ln();
        $pdf->SetX($setx);
        $pdf->SetFont('Arial', 'B', 10);
        $pdf->Cell(10, 4, '', 0, 0, '');
        $pdf->Cell(30, 4, '', '', 0, '');
        $pdf->Cell(124, 4, '', '');
        $pdf->Cell(10, 4, 'Date : ', 0, 0, '');
        $pdf->SetFont('Arial', 'B', 10);
        $pdf->Cell(21, 4, date('d-m-Y', strtotime($date)), 'B', 0, 'R');

        $pdf->Ln();
        $pdf->Ln();
        $pdf->SetX($setx);
        $pdf->SetFont('Arial', 'B', 10);
        $pdf->Cell(33, 5, 'Name & Address : ', 0, 0, '');
        $pdf->SetFont('Arial', 'B', 10);
        $pdf->Cell(162, 5, $customer_name . " " . $customer_address, 'B', 0, '');

        $pdf->Ln();
        $pdf->Ln();
        $pdf->SetX($setx);
        if($discount_input == 0){
            if($type == 'net') {
                $header = array('S.No.', 'PARTICULARS', 'BARCODE', 'WEIGHT', 'PIECES', 'NET', 'GST (' . $tax . '%)');
            }else if($type == 'tp'){
                $header = array('S.No.', 'PARTICULARS', 'BARCODE', 'WEIGHT', 'PIECES', 'T.P', 'GST (' . $tax . '%)');
            }
        }else{
            if($type == 'net') {
     //           $header = array('S.No.', 'PARTICULARS', 'BARCODE', 'WEIGHT', 'PIECES', 'NET', 'Discount (' . $discount_input . '%)', 'GST (' . $tax . '%)');
              $header = array('S.No.', 'PARTICULARS', 'BARCODE', 'WEIGHT', 'PIECES', 'NET', 'DISC (' . $discount_input . '%)', 'GST (' . $tax . '%)');
            }else if($type == 'tp'){
     //           $header = array('S.No.', 'PARTICULARS', 'BARCODE', 'WEIGHT', 'PIECES', 'T.P', 'Discount (' . $discount_input . '%)', 'GST (' . $tax . '%)');
              $header = array('S.No.', 'PARTICULARS', 'BARCODE', 'WEIGHT', 'PIECES', 'T.P', 'DISC (' . $discount_input . '%)', 'GST (' . $tax . '%)');
            }
        }

//        print_r($product);
//        echo '<br>';
        $filterProduct = " WHERE id IN ($product) order by product_order";
        $productData = ProductSetup::GetProductSetupList($filterProduct);

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

        function FancyTable($header, $data, $discount, $setx, $tpRate, $piece_carton, $tax)
        {
            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', 11);
            //$pdf->SetFont('', 'B');
            // Header
            if($discount == 0){
                $wr = array(13, 57, 28, 25, 22, 25, 25);
            }else{
                $wr = array(13, 59, 28, 20, 18, 15, 24, 22);
            }
            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;

            $count = 1;
            $totalAmount = 0;
            if ($data) {
                $totalCount = count($data);
                $totalQty = 0;
                foreach ($data AS $key => $row) {
                    //$productSetup = ProductSetup::GetProductSetupById($row->product_id);

                    $pdf->SetX($setx);
                    $pdf->SetFont('Arial', '', 10);
                    $pdf->Cell($wr[0], 7, $count, 'LRB', 0, 'C', $fill);
                    $pdf->Cell($wr[1], 7, $row->name, 'LRB', 0, 'L', $fill);
                    $pdf->Cell($wr[2], 7, $row->barcode, 'LRB', 0, 'C', $fill);
                    $pdf->Cell($wr[3], 7, $row->weight . " " . $row->unit, 'LRB', 0, 'C', $fill);
                    $pdf->Cell($wr[4], 7, $piece_carton[$key], 'LRB', 0, 'C', $fill);
                    $pdf->Cell($wr[5], 7, $tpRate[$key], 'LRB', 0, 'C', $fill);

                    if($discount == 0){
                        $subTotal = $tpRate[$key];
                        $subTotalWGST = $subTotal + ($subTotal*($tax/100));
                        $pdf->Cell($wr[6], 7, number_format($subTotalWGST, 2), 'LRB', 0, 'C', $fill);
                    }else{
                        $subTotal = ($tpRate[$key] - ($tpRate[$key] * ($discount/100)));
                        $pdf->Cell($wr[6], 7, number_format($subTotal, 2), 'LRB', 0, 'C', $fill);
                        $subTotalWGST = $subTotal + ($subTotal*($tax/100));
                        $pdf->Cell($wr[7], 7, number_format($subTotalWGST, 2), 'LRB', 0, 'C', $fill);
                    }
                    $pdf->Ln();

                    $fill = !$fill;
                    $count++;
                    if($totalCount == $count-1){
                        $pdf->Ln();
                        $pdf->Ln();
                        $pdf->Ln();
                        $pdf->Ln();
                        $pdf->SetX($setx);
                        $pdf->SetFont('Arial', '', 9);
                        $pdf->Cell($wr[1], 8, "Signature's", 'T', 0, 'C');
                        $pdf->Cell($wr[2], 8, "", '', 0, 'C');
                        $pdf->Cell($wr[3], 8, '', '', 0, 'R');
                        $pdf->Cell($wr[4], 8, '', '', 0, 'R', "");
                    }

                    if ($count == 31) {

                        $pdf->Ln();
                        $pdf->Ln();
                        $pdf->Ln();
                        $pdf->Ln();
                        $pdf->SetX($setx);
                        $pdf->SetFont('Arial', '', 9);
                        $pdf->Cell($wr[1], 8, "Signature's", 'T', 0, 'C');
                        $pdf->Cell($wr[2], 8, "", '', 0, 'C');
                        $pdf->Cell($wr[3], 8, '', '', 0, 'R');
                        $pdf->Cell($wr[4], 8, '', '', 0, 'R', "");

                        $pdf->AddPage();
                        $pdf->SetFont('Arial', 'B', 18);
                        $pdf->Cell(0, 6, PROJECT_NAME, 0, 0, 'C');

                        $pdf->Ln();
                        $pdf->SetFont('Arial', 'B', 10);
                        $pdf->Cell(55, 8, '', '');
                        $pdf->Cell(105, 8, 'Contact Person : MUHAMMAD ASIF HUSSAIN - 0300-2219949' , 'TLR', 0, 'C');
                        $pdf->Cell(50, 8, '', '');

                        $pdf->Ln();
                        $pdf->SetFont('Arial', 'B', 10);
                        $pdf->Cell(55, 8, '', '');
                        $pdf->Cell(105, 8, 'UAN : ' . PROJECT_TEL, 'TLR', 0, 'C');
                        $pdf->Cell(50, 8, '', '');

                        $pdf->Ln();
                        $pdf->SetFont('Arial', 'B', 10);
                        $pdf->Cell(55, 8, '', '');
                        $pdf->Cell(105, 6, PROJECT_EMAIL . " / " . PROJECT_WEBSITE, 'BLR', 0, 'C');
                        $pdf->Cell(50, 8, '', '');

                        $pdf->Ln();
                        $pdf->SetFont('Arial', 'B', 14);
                        $pdf->Cell(0, 8, 'RATE LIST', 0, 0, 'C');

                        $pdf->Ln();

                        $pdf->SetX($setx);
                        $pdf->SetFont('Arial', 'B', 10);
                        for ($i = 0; $i < count($header); $i++)
                            $pdf->Cell($wr[$i], 7, $header[$i], 1, 0, 'C', true);
                        $pdf->Ln();
                    }
                    $totalAmount += $subTotal;
                    $totalQty += $row->product_qty;
                }
            }

        }

        FancyTable($header, $productData, $discount_input, $setx, $tpRate, $piece_carton, $tax);
    } else {
        header('location: ' . PROJECT_ADMIN_URL);
        exit;
    }

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