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

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

    include_once('../../../classes/manager.class.php');
    include_once('../../../classes/PHPMailer/PHPMailerAutoload.php');
    include_once('../../../fpdf/fpdf1New.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_NTN', $param_data->ntn);
    define('PROJECT_GST', $param_data->gst);

//    $invoice = Invoice::GetInvoiceSetupById($id);
    $invoice = PurchaseSetup::GetPurchaseSetupById($id);
    if (!empty($invoice)) {
        $invoice_id = $invoice->id;
        $create_date = $invoice->create_date;
        $code = $invoice->code;
//        $customer_id = $invoice->customer_id;
        $discount = $invoice->discount;
        $tax = $invoice->tax;
        $tax_add = 1;

        $supplier_id = $invoice->supplier_id;
        $supplier = SupplierSetup::GetSupplierSetupById($supplier_id);


//        $customer = CustomerSetup::GetCustomerSetupById($customer_id);
        $customer_name = $supplier->name;
        $customer_address = $supplier->location;
        $customer_ntn = $supplier->ntn;
        $customer_gst = $supplier->gst;
        $customer_no = $supplier->code . "-" . $supplier->id;

        $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('Purchase ' . $code);
        $pdf->AddPage();
        $pdf->Ln();
        $pdf->SetFont('Arial', 'B', 18);
        $pdf->Cell(50, 5, '', '');
        $pdf->Cell(153, 5, PROJECT_NAME, 0, 0, 'L');
        $pdf->Cell(50, 5, '', '');
        $pdf->Ln();
        $pdf->Ln();
        $pdf->SetFont('Arial', '', 11);
        $pdf->Cell(50, 5, '', '');
        $pdf->Cell(92, 5, 'NTN: ' . PROJECT_NTN . ", GST: " . PROJECT_GST, '', 0, 'L');
        $pdf->Cell(50, 5, '', '');

      

        $pdf->Ln();
        $pdf->SetFont('Arial', '', 11);
        $pdf->Cell(50, 5, '', '');
        $pdf->Cell(63, 5, 'UAN : ' . PROJECT_TEL, '', 0, 'L');
        $pdf->Cell(50, 5, '', '');

        $pdf->Ln();
        $pdf->SetFont('Arial', '', 11);
        $pdf->Cell(50, 5, '', '');
        $pdf->Cell(58, 5, PROJECT_EMAIL . " / " . PROJECT_WEBSITE, '', 0, 'L');
        $pdf->Cell(50, 5, '', '');

        $pdf->Ln();
        $pdf->SetX($setx);
        $pdf->SetFont('Arial', 'B', 10);
        $pdf->Cell(45, 5, '', 'B', '');
        $pdf->Cell(105, 5, '', 'B', 0, 'L');
        $pdf->Cell(45, 5, 'PURCHASE RECEIPT', 'BTLR', 0, 'C');
        $pdf->Ln();
        $pdf->Ln();
        $pdf->SetX($setx);
        $pdf->SetFont('Arial', 'B', 10);
        $pdf->Cell(23, 4, 'Supplier No.', 0, 0, '');
        $pdf->SetFont('Arial', '', 10);
        $pdf->Cell(32, 4, $customer_no, '', 0, '');
        $pdf->Cell(100, 4, '', '');
        $pdf->SetFont('Arial', 'B', 10);
        $pdf->Cell(20, 4, 'Receipt No.', 0, 0, '');
        $pdf->SetFont('Arial', '', 10);
        $pdf->Cell(15, 4, $code, '', 0, '');

        $pdf->Ln();
        $pdf->SetX($setx);
        $pdf->SetFont('Arial', 'B', 10);
        $pdf->Cell(82, 4, 'NTN# / GST#:    ' . $customer_ntn . " / " . $customer_gst, 0, 0, '');
        $pdf->Cell(73, 4, '', '');
        $pdf->Cell(20, 4, 'Dated', 0, 0, '');
        $pdf->SetFont('Arial', '', 10);
        $pdf->Cell(20, 4, date('d-m-Y', strtotime($create_date)), '', 0, '');

        $pdf->Ln();
        $pdf->SetX($setx);
        $pdf->SetFont('Arial', 'B', 10);
        $pdf->Cell(23, 4, 'Name : ', 0, 0, '');
        $pdf->SetFont('Arial', '', 10);
        $pdf->Cell(172, 4, $customer_name, '', 0, '');

        $pdf->Ln();
        $pdf->SetX($setx);
        $pdf->SetFont('Arial', 'B', 10);
        $pdf->Cell(23, 4, 'Address : ', 0, 0, '');
        $pdf->SetFont('Arial', '', 10);
        $pdf->Cell(172, 4, $customer_address, '', 0, '');

        $pdf->Ln();
        $pdf->Ln();
        $pdf->SetX($setx);
        $header = array('S.No.', 'ITEMS', 'QTY', 'RATE', 'EX. TAX', 'S. TAX%', 'S. T. AMOUNT', 'TOTAL');

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

        $filteritems = " WHERE purchase_id = '$invoice_id'";
        $productData = PurchaseSetup::GetPurchaseItems($filteritems);

        function FancyTable($header, $data, $discount, $setx, $tax, $tax_add)
        {
            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('', 'B');
            // Header
            $wr = array(12, 50, 22, 22, 19, 22, 28, 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;
            $totalAmountTax = 0;
            $totalAmountExTax = 0;
            if ($data) {
                $totalQty = 0;
                foreach ($data AS $row) {
                    $productSetup = IngSetup::GetIngSetupById($row->ing_id);

                    $pdf->SetX($setx);
                    $pdf->SetFont('Arial', '', 9);
                    $pdf->Cell($wr[0], 7, $count, 'LRB', 0, 'C', $fill);
                    $pdf->Cell($wr[1], 7, $productSetup->name, 'LRB', 0, 'L', $fill);
                    $pdf->Cell($wr[2], 7, $row->qty, 'LRB', 0, 'R', $fill);
                    $pdf->Cell($wr[3], 7, $row->rate, 'LRB', 0, 'R', $fill);
                    $pdf->Cell($wr[4], 7, ($row->qty * $row->rate), 'LRB', 0, 'R', $fill);
                    $pdf->Cell($wr[5], 7, $row->tax . '%', 'LRB', 0, 'R', $fill);
                    $pdf->Cell($wr[6], 7, $row->tax_amount, 'LRB', 0, 'R', $fill);

                    $subTotal = (($row->qty * $row->rate) + $row->tax_amount);

                    $pdf->Cell($wr[7], 7, number_format($subTotal, 2), 'LRB', 0, 'R', $fill);
                    $pdf->Ln();
              //      $fill = !$fill;
                    $count++;
                    if ($count == 15) {

                        $pdf->SetX($setx);
                        $pdf->SetFont('Arial', '', 9);
                        //$pdf->Cell($wr[1], 7, "Goods once sold will not be back or exchanged", '', 0, 'L');
                        $pdf->Cell($wr[1], 7, "", '', 0, 'L');
                        $pdf->Cell($wr[2], 7, "", '', 0, 'C');
                        $pdf->Cell($wr[3], 7, "", '', 0, 'C');
                        $pdf->Cell($wr[4], 7, '', '', 0, 'R');
                        $pdf->Cell($wr[5], 7, '', '', 0, 'R', "");
                        $pdf->Cell($wr[6], 7, '', '', 0, 'R', "");

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

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

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

                        // $pdf->SetTitle('Invoice ' . $code . ' / Cash Memo');
                        //$pdf->AddPage();
                        $pdf->Ln();
                        $pdf->SetFont('Arial', 'B', 18);
                        $pdf->Cell(50, 5, '', '');
                        $pdf->Cell(153, 5, PROJECT_NAME, 0, 0, 'L');
                        $pdf->Cell(50, 5, '', '');
                        $pdf->Ln();
                        $pdf->Ln();
                        $pdf->SetFont('Arial', '', 11);
                        $pdf->Cell(50, 5, '', '');
                        $pdf->Cell(92, 5, 'NTN: ' . PROJECT_NTN . ", GST: " . PROJECT_GST, '', 0, 'L');
                        $pdf->Cell(50, 5, '', '');

                        $pdf->Ln();
                        $pdf->SetFont('Arial', '', 11);
                        $pdf->Cell(50, 5, '', '');
                        $pdf->Cell(63, 5, 'UAN : ' . PROJECT_TEL, '', 0, 'L');
                        $pdf->Cell(50, 5, '', '');

                        $pdf->Ln();
                        $pdf->SetFont('Arial', '', 11);
                        $pdf->Cell(50, 5, '', '');
                        $pdf->Cell(58, 5, PROJECT_EMAIL . " / " . PROJECT_WEBSITE, '', 0, 'L');
                        $pdf->Cell(50, 5, '', '');
                        $pdf->Ln();
                        $pdf->SetX($setx);
                        $pdf->SetFont('Arial', 'B', 10);
                        $pdf->Cell(45, 5, '', 'B', '');
                        $pdf->Cell(105, 5, '', 'B', 0, 'L');
                        $pdf->Cell(45, 5, 'PURCHASE RECEIPT', 'BTLR', 0, 'C');
                      
                      
                 


                        /*  $pdf->SetFont('Arial', 'B', 18);
                          $pdf->Cell(0, 8, 'CASH MEMO', 0, 0, 'C');
                          $pdf->Ln();
                          $pdf->SetFont('Arial', '', 16);
                          $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', '', 10);
                          $pdf->Cell(55, 8, '', '');
                          $pdf->Cell(105, 8, 'UAN : ' . PROJECT_TEL, 'TLR', 0, 'C');
                          $pdf->Cell(50, 8, '', '');

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

                        $pdf->Ln();
                        $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;
                    $totalAmountTax += $row->tax_amount;
                    $totalAmountExTax += ($row->qty * $row->rate);
                    $totalQty += $row->qty;
                }
            }


            //$pdf->SetFillColor(255, 204, 0);
            $pdf->SetX($setx);
            $pdf->SetFont('Arial', 'B', 10);
            $pdf->Cell($wr[0], 7, '', 'LB', 0, 'L');
            $pdf->Cell($wr[1], 7, 'Total', 'RB', 0, 'C');
            $pdf->Cell($wr[2], 7, $totalQty, 'LRB', 0, 'R');
            $pdf->Cell($wr[3], 7, "--", 'LRB', 0, 'C');
            $pdf->Cell($wr[4], 7, $totalAmountExTax, 'LRB', 0, 'R');
            $pdf->Cell($wr[5], 7, '--', 'LRB', 0, 'C');
            $pdf->Cell($wr[6], 7, $totalAmountTax, 'LRB', 0, 'R', "");
            $pdf->Cell($wr[7], 7, number_format($totalAmount, 2), 'LRB', 0, 'R', "");

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

        FancyTable($header, $productData, $discount, $setx, $tax, $tax_add);
    } else {
        header('location: ' . PROJECT_ADMIN_URL);
        exit;
    }

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