<?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/fpdf1Newtx.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);
    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;
      	$po_no= $invoice->po_number;

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

        // Fetch customer category
        $customercategory_id = $customer->category_id; // Ensure this field is in your database
        $customercategory = CustomerCategorySetup::GetCustomerCategorySetupById($customercategory_id);
        $customercategory_name = $customercategory ? $customercategory->name : 'N/A';

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

        $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', 12);
        $pdf->Cell(10, 5, '', '');
        $pdf->Cell(153, 5, PROJECT_NAME, 0, 0, 'L');
        $pdf->Cell(50, 5, '', '');
        $pdf->Ln();
        $pdf->SetFont('Arial', '', 11);
        $pdf->Cell(10, 5, '', '');
        $pdf->Cell(92, 5, 'National Tax No: ' . PROJECT_NTN, '', 0, 'L');
        $pdf->Cell(50, 5, '', '');
      
        $pdf->Ln();
        $pdf->SetFont('Arial', '', 11);
        $pdf->Cell(10, 5, '', '');
        $pdf->Cell(92, 5, 'Sales Tax Registration No: ' . PROJECT_GST, '', 0, 'L');
        $pdf->Cell(50, 5, '', '');
                  
        $pdf->Ln();
        $pdf->SetFont('Arial', '', 11);
        $pdf->Cell(10, 5, '', '');
        $pdf->Cell(92, 5, '1A/4, Shahnawaz Bhutto Colony Karachi - 75530', '', 0, 'L');
        $pdf->Cell(25, 5, '', '');

        $pdf->SetFont('Arial', 'B', 10);
        $pdf->Cell(78, 5, 'SALES TAX INVOICE', '', 0, 'R');
        $pdf->Ln(4);
        $pdf->SetX($setx);

         $pdf->Ln(3);
        $pdf->SetX($setx);
        $pdf->SetFont('Arial', 'B', 8);
        $pdf->Cell(195, 4, '', 'TLR', 0, '');
      
      
        $pdf->Ln(1);
        $pdf->SetX($setx);
        $pdf->SetFont('Arial', 'B', 8);
        $pdf->Cell(23, 4, 'Sold to Buyer:', 'L', 0, '');
        $pdf->SetFont('Arial', '', 8);
        $pdf->Cell(30, 4,  $customercategory_name, '', 0, ''); // Display customer category here

        $pdf->Cell(16, 4, '', '', '');
        $pdf->SetFont('Arial', 'B', 8);
        $pdf->Cell(14, 4, '', '', 0, '');
        $pdf->SetFont('Arial', '', 8);
        $pdf->Cell(72, 4, '', '', 0, ''); // Display customer name here
        $pdf->SetFont('Arial', 'B', 8);
        $pdf->Cell(20, 4, 'Invoice No.', '', 0, '');
        $pdf->SetFont('Arial', '', 8);
        $pdf->Cell(20, 4, $code, 'R', 0, '');

        $pdf->Ln();
        $pdf->SetX($setx);
        $pdf->SetFont('Arial', 'B', 8);
        $pdf->Cell(15, 4, 'Ship to:' , 'L', 0, '');
        $pdf->SetFont('Arial', '', 8);
        $pdf->Cell(110, 4, $customer_name, '', 0, '');
        $pdf->Cell(30, 4, '', '');
        $pdf->SetFont('Arial', 'B', 8);
        $pdf->Cell(20, 4, 'Dated', 0, 0, '');
        $pdf->SetFont('Arial', '', 8);
        $pdf->Cell(20, 4, date('d-m-Y', strtotime($create_date)), 'R', 0, '');
      
        $pdf->Ln();
        $pdf->SetX($setx);
        $pdf->SetFont('Arial', 'B', 8);
        $pdf->Cell(15, 4, 'Address:' , 'L', 0, '');
        $pdf->SetFont('Arial', '', 8);
        $pdf->Cell(110, 4, $customer_address, '', 0, '');
        $pdf->Cell(30, 4, '', '');
        $pdf->SetFont('Arial', 'B', 8);
        $pdf->Cell(20, 4, 'PO No:', 0, 0, '');
        $pdf->SetFont('Arial', '', 8);
        $pdf->Cell(20, 4, $po_no, 'R', 0, '');

        $pdf->Ln();
        $pdf->SetX($setx);
        $pdf->SetFont('Arial', 'B', 8);
        $pdf->Cell(10, 4, 'NTN #:', 'L', 0, '');
        $pdf->SetFont('Arial', '', 8);
        $pdf->Cell(95, 4, $customer_ntn , '', 0, '');
      
      $pdf->Cell(50, 4, '', '');
      $pdf->SetFont('Arial', 'B', 8);
        $pdf->Cell(20, 4, '', 0, 0, '');
        $pdf->SetFont('Arial', '', 8);
        $pdf->Cell(20, 4, '', 'R', 0, '');

        $pdf->Ln();
        $pdf->SetX($setx);
        $pdf->SetFont('Arial', 'B', 8);
        $pdf->Cell(12, 4, 'STRN #:', 'BL', 0, '');
        $pdf->SetFont('Arial', '', 8);
        $pdf->Cell(183, 4, $customer_gst, 'BR', 0, '');

        $pdf->Ln();
        $pdf->Ln();
        $pdf->SetX($setx);
        if (!empty($discount)) {      
            $header = array('S.No.   ',
                            'PARTICULARS                                                   ', 
                            'WEIGHT            ',
                            'CARTON        ',
                            'QUANTITY               ',
                          //  'T.P. / W.O    G.S.T.      ',
                           'GROSS VALUE ' ,
                            'DISC. VALUE' ,
                            'VALUE EXCL. SALES TAX',
                            'SALES TAX VALUE '. $tax.' %' ,
                            ' VALUE INCL. SALES TAX      ');
          
          
        } else {
                   
           $header = array('S.No.   ',
                           'PARTICULARS                                                                                   ',
                           'WEIGHT            ', 
                           'CARTON        ', 
                           'QUANTITY               ',
                           //'T.P. / W.O    G.S.T.      ',
                         //  'GROSS      VALUE    ' ,
                            'VALUE EXCL. SALES TAX',
                            'SALES TAX VALUE '. $tax.' %' ,
                            ' VALUE INCL.  SALES TAX ');
          
        }

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

        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(.2);
            $pdf->SetFont('', 'B',9);
          
           // Header
          if (!empty($discount)) { 
            $wr = array(10, 53, 14, 14, 16, 16, 14, 20, 18,20);
          } else {
           $wr = array(11, 58, 17, 17, 20, 25, 22,25);
          }
            
            $x = $setx;
            $y = 62;
            for ($i = 0; $i < count($header); $i++){
                $x += $wr[$i];
                $pdf->MultiCell($wr[$i], 8, $header[$i], 1, 'C', true);
                $pdf->SetXY($x, $y);
            }
            $pdf->SetX($setx);
            $pdf->Ln();

           $pdf->Ln();

          
            // Color and font restoration
            $pdf->SetFillColor(224, 235, 255);
            $pdf->SetTextColor(0);
            $pdf->SetFont('');
            
            // Data
            $fill = false;

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

                    $pdf->SetX($setx);
                    $pdf->SetFont('Arial', '', 11);
                    $pdf->Cell($wr[0], 8, $count, 'LRB', 0, 'C', $fill);
                    $pdf->Cell($wr[1], 8, $productSetup->name, 'LRB', 0, 'L', $fill);
                    $pdf->Cell($wr[2], 8, $productSetup->weight . " " . $productSetup->unit, 'LRB', 0, 'R', $fill);
                    $totalCarton += ($row->carton_piece != 0 ? (number_format(($row->product_qty / $row->carton_piece), 0)) : 0);
                    $pdf->Cell($wr[3], 8, ($row->carton_piece != 0 ? (number_format(($row->product_qty / $row->carton_piece), 0)) : "-"), 'LRB', 0, 'C', $fill);
                    $pdf->Cell($wr[4], 8, $row->product_qty, 'LRB', 0, 'C', $fill);
               
                    //gross value 
                    $subTotal = ($row->product_qty * $row->unit_sale_price);
                   
                  
                    if (!empty($discount)){ 
                      
                       $pdf->Cell($wr[5], 8, number_format($subTotal, 2), 'LRB', 0, 'R', $fill);
                      // discount calculation
                    $discValue = ($subTotal *( $discount /100)); 
                    $pdf->Cell($wr[6], 8, number_format($discValue, 2), 'LRB', 0, 'R', $fill);
                     // value after discount  
                    $valueAfterDisc = ($subTotal - $discValue); 
                    $pdf->Cell($wr[7], 8, number_format($valueAfterDisc, 2), 'LRB', 0, 'R', $fill);
                   // sale tax value  
                    $saleTaxvalue = ($valueAfterDisc * ( $tax /100)); 
                    $pdf->Cell($wr[8], 8, number_format($saleTaxvalue, 2), 'LRB', 0, 'R', $fill);
                  
                    // Net Value  
                    $netValue = ($valueAfterDisc + $saleTaxvalue); 
                  
                    $pdf->Cell($wr[9], 8, number_format($netValue, 2), 'LRB', 0, 'R', $fill); }
                  
                  else{
                 
                      $pdf->Cell($wr[5], 8, number_format($subTotal, 2), 'LRB', 0, 'R', $fill);
                   // sale tax value  
                    $saleTaxvalue = ($subTotal * ( $tax /100)); 
                    $pdf->Cell($wr[6], 8, number_format($saleTaxvalue, 2), 'LRB', 0, 'R', $fill);
                  
                    // Net Value  
                    $netValue = ($subTotal + $saleTaxvalue); 
                  
                    $pdf->Cell($wr[7], 8, number_format($netValue, 2), 'LRB', 0, 'R', $fill); 
                  
                  
                  }
                    $pdf->Ln();
                    $fill = !$fill;
                    $count++;
                    if ($count == 27) {

                        $pdf->SetX($setx);
                        $pdf->SetFont('Arial', '', 11);
                       
                        $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->AddPage();

                      
                        $pdf->Ln();
                        $pdf->Ln();
                      
                      
                       $pdf->SetX($setx);
                        $pdf->SetFont('', 'B', 9);
                        $x = $setx;
                        $y = 29;
                        for ($i = 0; $i < count($header); $i++){
                            $x += $wr[$i];
                            $pdf->MultiCell($wr[$i], 8, $header[$i], 1, 'C', true);
                            $pdf->SetXY($x, $y);
                        }
                        $pdf->Ln();
                        $pdf->Ln();
                      
                      
                      

                
                    }
                    $totalAmount += $subTotal;
                  
                    $totalQty += $row->product_qty;
                    $totalDiscValue += $discValue;
                    $totalValueAfterDisc += $valueAfterDisc;
                    $totalsaleTaxvalue += $saleTaxvalue;
                    $totalnetValue += $netValue;   
                  
                }
            }

            if (PROJECT_DISCOUNT == 1 && !empty($discount)) {
                $pdf->SetX($setx);
                $pdf->SetFont('Arial', 'B', 10);
                $pdf->Cell($wr[0], 7, '', '', 0, 'L');
                $pdf->Cell($wr[1], 7, '', '', 0, 'L');
                $pdf->Cell($wr[2], 7, 'Total', 'LRB', 0, 'R');
                $pdf->Cell($wr[3], 7, '' . $totalCarton, 'LRB', 0, 'C');
                $pdf->Cell($wr[4], 7, '' . $totalQty, 'LRB', 0, 'C');
               
                $pdf->Cell($wr[5], 7, number_format($totalAmount, 2), 'LRB', 0, 'R', "");
                $pdf->Cell($wr[6], 7, number_format($totalDiscValue, 2), 'LRB', 0, 'R');
                $pdf->Cell($wr[7], 7, number_format($totalValueAfterDisc,2), 'LRB', 0, 'R');
                $pdf->Cell($wr[8], 7, number_format($totalsaleTaxvalue,2), 'LRB', 0, 'R');
                $pdf->Cell($wr[9], 7, number_format($totalnetValue,2), 'LRB', 0, 'R');
               

                $pdf->Ln();

                $discountAmount = ($totalAmount * ($discount / 100));

                $pdf->SetX($setx);
                $pdf->SetFont('Arial', 'B', 11);
                $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');
             
                $pdf->Cell($wr[5], 7, '', '', 0, 'R');
                $pdf->Cell($wr[6], 7, '', 'LB', 0, 'R');
                $pdf->Cell($wr[7], 7, '', 'B', 0, 'R');
                $pdf->Cell($wr[8], 7, 'Value EXCL. Sales Tax ', 'RB', 0, 'R', "");
                $pdf->Cell($wr[9], 7, number_format($totalValueAfterDisc, 2), 'LRB', 0, 'R', "");
               

            
                $totalAmount -= $discountAmount;

                if (PROJECT_TAX == 1 && !empty($tax)) {
               

                    $taxAmount = ($totalAmount * ($tax / 100));
                    if ($tax_add == 1) {
                        $totaltaxAmount = $totalAmount + $taxAmount;
                        $taxaddsign = "(+)";
                    } else {
                        $totaltaxAmount = $totalAmount - $taxAmount;
                        $taxaddsign = "(-)";
                    }

                    $pdf->Ln();

                    $pdf->SetX($setx);
                    $pdf->SetFont('Arial', 'B', 10);
                    $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');
             
                    $pdf->Cell($wr[5], 7, '', '', 0, 'R');
                    $pdf->Cell($wr[6], 7, '', 'LB', 0, 'R');                  
                    $pdf->Cell($wr[7], 7, '', 'B', 0, 'R');
                    $pdf->Cell($wr[8], 7, 'Total Sales Tax value ', 'RB', 0, 'R', "");
                    $pdf->Cell($wr[9], 7, number_format($totalsaleTaxvalue, 2), 'LRB', 0, 'R', "");

                    $pdf->Ln();

                    $pdf->SetX($setx);
                    $pdf->SetFont('Arial', 'B', 10);
                    $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');
                
                    $pdf->Cell($wr[5], 7, '', '', 0, 'R');
                    $pdf->Cell($wr[6], 7, '', 'LB', 0, 'R');
                    $pdf->Cell($wr[7], 7, '', 'B', 0, 'R');
                    $pdf->Cell($wr[8], 7, 'Total Amount before Adv Tax ', 'RB', 0, 'R', "");
                    $pdf->Cell($wr[9], 7, number_format($totalnetValue, 2), 'LRB', 0, 'R', "");
                    
                    $pdf->Ln();
					// advance tax calculation                  
                  
					$advanceTaxvalue = ($totalnetValue * (0.1 /100));
                    $pdf->SetX($setx);
                    $pdf->SetFont('Arial', 'B', 10);
                    $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');
              
                    $pdf->Cell($wr[5], 7, '', '', 0, 'R');
                    $pdf->Cell($wr[6], 7, '', '', 0, 'R');
                    $pdf->Cell($wr[7], 7, '', 'LB', 0, 'R');
                    $pdf->Cell($wr[8], 7, 'Advance Tax @ 0.1 % ', 'RB', 0, 'R', "");
                    $pdf->Cell($wr[9], 7, number_format($advanceTaxvalue, 2), 'LRB', 0, 'R', "");

                    $pdf->Ln();
                   // Total Amount Payable
                    $totalAmountPayable = $advanceTaxvalue + $totalnetValue ;
                    $pdf->SetX($setx);
                    $pdf->SetFont('Arial', 'B', 10);
                    $pdf->Cell($wr[0], 8, '', '', 0, 'L');
                    $pdf->Cell($wr[1], 8, '', '', 0, 'L');
                    $pdf->Cell($wr[2], 8, '', '', 0, 'R');
                    $pdf->Cell($wr[3], 8, '', '', 0, 'R');
                    $pdf->Cell($wr[4], 8, '', '', 0, 'R');
                
                    $pdf->Cell($wr[5], 8, '', '', 0, 'R');
                    $pdf->Cell($wr[6], 8, '', '', 0, 'R');
                    $pdf->Cell($wr[7], 8, '', 'LB', 0, 'R');
                    $pdf->Cell($wr[8], 8, 'Total Amount Payable', 'RB', 0, 'R', "");
                    $pdf->Cell($wr[9], 8, number_format($totalAmountPayable, 2), 'LRB', 0, 'R', "");

                    $pdf->Ln();
                }

                if (PROJECT_TAX != 1 && empty($tax)) {

                    $pdf->SetX($setx);
                    $pdf->SetFont('Arial', '', 10);
                    $pdf->Cell(($wr[1] + 13), 8, "", '', 0, 'C');
                    $pdf->Cell($wr[2], 8, "", '', 0, 'C');
                    $pdf->Cell($wr[3], 8, '', '', 0, 'R');
                    $pdf->Cell($wr[4], 8, '', 'LB', 0, 'R');
                    $pdf->Cell($wr[5], 8, 'Total Amount', 'RB', 0, 'R', "");
                    $pdf->Cell($wr[6], 8, number_format($totalAmount, 2), 'LRB', 0, 'R', "");
                }

           


            } else {
             $pdf->SetX($setx);
                $pdf->SetFont('Arial', 'B', 10);
                $pdf->Cell($wr[0], 8, '', '', 0, 'L');
                $pdf->Cell($wr[1], 8, '', '', 0, 'L');
                $pdf->Cell($wr[2], 8, 'Total', 'LRB', 0, 'C');
                $pdf->Cell($wr[3], 8, '' . $totalCarton, 'LRB', 0, 'C');
                $pdf->Cell($wr[4], 8, '' . $totalQty, 'LRB', 0, 'C');
             
               // $pdf->Cell($wr[5], 8, number_format($totalAmount, 2), 'LRB', 0, 'R', "");
             
                $pdf->Cell($wr[5], 8, number_format($totalAmount,2), 'LRB', 0, 'R');
                $pdf->Cell($wr[6], 8, number_format($totalsaleTaxvalue,2), 'LRB', 0, 'R');
                $pdf->Cell($wr[7], 8, number_format($totalnetValue,2), 'LRB', 0, 'R');
               
/*
                $pdf->Ln();

             

                $pdf->SetX($setx);
                $pdf->SetFont('Arial', 'B', 9);
                $pdf->Cell($wr[0], 8, '', '', 0, 'L');
                $pdf->Cell($wr[1], 8, '', '', 0, 'L');
                $pdf->Cell($wr[2], 8, '', '', 0, 'R');
                $pdf->Cell($wr[3], 8, '', '', 0, 'R');
                $pdf->Cell($wr[4], 8, '', '', 0, 'R');
              
                $pdf->Cell($wr[5], 8, '', 'LB', 0, 'R');
                $pdf->Cell($wr[6], 8, '', 'B', 0, 'R');
                $pdf->Cell($wr[7], 8, 'Value EXCL. Sales Tax ', 'RB', 0, 'R', "");
                $pdf->Cell($wr[8], 8, number_format($totalAmount, 2), 'LRB', 0, 'R', ""); */
               


                if (PROJECT_TAX == 1 && !empty($tax)) {
               

                    $taxAmount = ($totalAmount * ($tax / 100));
                    if ($tax_add == 1) {
                        $totaltaxAmount = $totalAmount + $taxAmount;
                        $taxaddsign = "(+)";
                    } else {
                        $totaltaxAmount = $totalAmount - $taxAmount;
                        $taxaddsign = "(-)";
                    }

                /*    $pdf->Ln();

                    $pdf->SetX($setx);
                    $pdf->SetFont('Arial', 'B', 9);
                    $pdf->Cell($wr[0], 8, '', '', 0, 'L');
                    $pdf->Cell($wr[1], 8, '', '', 0, 'L');
                    $pdf->Cell($wr[2], 8, '', '', 0, 'R');
                    $pdf->Cell($wr[3], 8, '', '', 0, 'R');
					$pdf->Cell($wr[4], 8, '', '', 0, 'R');
               
                    $pdf->Cell($wr[5], 8, '', 'LB', 0, 'R');                  
                    $pdf->Cell($wr[6], 8, '', 'B', 0, 'R');
                    $pdf->Cell($wr[7], 8, 'Total Sales Tax ', 'RB', 0, 'R', "");
                    $pdf->Cell($wr[8], 8, number_format($totalsaleTaxvalue, 2), 'LRB', 0, 'R', "");  

                    $pdf->Ln();

                    $pdf->SetX($setx);
                    $pdf->SetFont('Arial', 'B', 9);
                    $pdf->Cell($wr[0], 8, '', '', 0, 'L');
                    $pdf->Cell($wr[1], 8, '', '', 0, 'L');
                    $pdf->Cell($wr[2], 8, '', '', 0, 'R');
                    $pdf->Cell($wr[3], 8, '', '', 0, 'R');
                    $pdf->Cell($wr[4], 8, '', '', 0, 'R');
                
                    $pdf->Cell($wr[5], 8, '', 'LB', 0, 'R');
                    $pdf->Cell($wr[6], 8, '', 'B', 0, 'R');
                    $pdf->Cell($wr[7], 8, 'Total Amount before Adv Tax ', 'RB', 0, 'R', "");
                    $pdf->Cell($wr[8], 8, number_format($totalnetValue, 2), 'LRB', 0, 'R', "");      
                    
                    */
                    
                    $pdf->Ln();
					// advance tax calculation                  
                  
					$advanceTaxvalue = ($totalnetValue * (0.1 /100));
                    $pdf->SetX($setx);
                    $pdf->SetFont('Arial', 'B', 10);
                    $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');
                
                    //$pdf->Cell($wr[5], 7, '', 'LB', 0, 'R');
                    $pdf->Cell($wr[5], 7, '', 'LB', 0, 'R');
                    $pdf->Cell($wr[6], 7, 'Advance Tax @ 0.1 %', 'RB', 0, 'R', "");
                    $pdf->Cell($wr[7], 7, number_format($advanceTaxvalue, 2), 'LRB', 0, 'R', "");

                    $pdf->Ln();
                   // Total Amount Payable
                    $totalAmountPayable = $advanceTaxvalue + $totalnetValue ;
                    $pdf->SetX($setx);
                    $pdf->SetFont('Arial', 'B', 10);
                    $pdf->Cell($wr[0], 8, '', '', 0, 'L');
                    $pdf->Cell($wr[1], 8, '', '', 0, 'L');
                    $pdf->Cell($wr[2], 8, '', '', 0, 'R');
                    $pdf->Cell($wr[3], 8, '', '', 0, 'R');
                    $pdf->Cell($wr[4], 8, '', '', 0, 'R');
                 
                  //  $pdf->Cell($wr[5], 8, '', 'LB', 0, 'R');
                    $pdf->Cell($wr[5], 8, '', 'LB', 0, 'R');
                    $pdf->Cell($wr[6], 8, 'Total Amount Payable', 'RB', 0, 'R', "");
                    $pdf->Cell($wr[7], 8, number_format($totalAmountPayable, 2), 'LRB', 0, 'R', "");

                    $pdf->Ln();
                }

                if (PROJECT_TAX != 1 && empty($tax)) {

                    $pdf->SetX($setx);
                    $pdf->SetFont('Arial', '', 10);
                    $pdf->Cell(($wr[1] + 13), 8, "", '', 0, 'C');
                    $pdf->Cell($wr[2], 8, "", '', 0, 'C');
                    $pdf->Cell($wr[3], 8, '', '', 0, 'R');
                    $pdf->Cell($wr[4], 8, '', 'LB', 0, 'R');
                    $pdf->Cell($wr[5], 8, 'Total Amount', 'RB', 0, 'R', "");
                    $pdf->Cell($wr[6], 8, number_format($totalAmount, 2), 'LRB', 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();
}
?>
