<?php
error_reporting('E_ALL');
ini_set('display_errors', '1');
//$invoice_date = null;
//$check_date = null;
//$customer_name = null;
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/fpdfwlogo.php'); 





    $m = new Manager();
  $setx  = 10;                 // left padding
    $wPage = 195;                // usable width (210 - 2*10)

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

    $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;
        $add_discount = $invoice->add_discount;

        $customer = CustomerSetup::GetCustomerSetupById($customer_id);
        $customer_name = $customer->name;
        $customer_address = $customer->location;
        $customer_ntn = $customer->ntn;
        $customer_catid = $customer->category_id; 
        $customer_category = CustomerCategorySetup::GetCustomerCategorySetupById($customer_catid);
        $customer_cat_name = $customer_category->name;
      	$check_date = new DateTime('2024-09-01');  // $create_date ko bhi ek date object mein convert karen // 31 August 2024 ko ek date object mein convert karen

		$invoice_date = new DateTime($create_date);  // Agar $invoice_date, $check_date say baad ka hai tu ye condition chalain gi
		
        $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('Invoice ' . $code . ' / INVOICE');
        $pdf->AddPage();
        $pdf->Ln();
        $pdf->SetFont('Arial', 'B', 16);
        $pdf->Cell(0, 8, 'INVOICE', 0, 0, 'C');
        $pdf->Ln();
        $pdf->SetFont('Arial', 'B', 15);
        
 // if ($invoice_date > $check_date) && ($customer_catid != 2) {
  if ($invoice_date > $check_date && strpos($customer_name, 'IMTIAZ GROUP') === false) {
     
    // Yeh code tab chale ga agar $create_date, 31 August 2024 say baad ka ho
    	$pdf->SetFont('Arial', 'B', 18);
        $pdf->Ln(4);
        $pdf->Cell(0, 6, 'The Adam Company', 0, 0, 'C');
        
    	$pdf->Ln();
    	$pdf->Ln();

} 
       
        $pdf->SetX($setx);
        $pdf->SetFont('Arial', 'B', 11);
        $pdf->Cell(25, 4, 'Invoice No. : ', 0, 0, '');
        $pdf->Cell(15, 4, $code, 'B', 0, 'C');
        $pdf->Cell(120, 4, '', '');
        $pdf->Cell(15, 4, 'Dated : ', 0, 0, '');
        $pdf->Cell(19, 4, date('d-m-Y', strtotime($create_date)), 'B', 0, 'C');
      
      
      
      
     if(!empty($customer_ntn)){
        $pdf->Ln();
        $pdf->Ln();
        $pdf->SetX($setx);
        $pdf->SetFont('Arial', 'B', 11);
       // $pdf->Cell(50, 4, $customer_cat_name.' NTN : ', 0, 0, '');
       $pdf->Cell(175, 4, $customer_cat_name . ' NTN : ', 0, 0, '');

        $pdf->Cell(20, 4, $customer_ntn, 'B', 0, 'R');
        //$pdf->Cell(18, 4, '', '');
       $pdf->Ln();
       $pdf->Ln();
       $pdf->SetX($setx);
        $pdf->Cell(175, 4, PROJECT_NAME ." NTN : ", 0, 0, '');
        $pdf->Cell(20, 4, PROJECT_NTN, 'B', 0, 'R');
     }

        
      
      
        $pdf->Ln();
        $pdf->Ln();
        $pdf->SetX($setx);
        $pdf->SetFont('Arial', 'B', 12);
       // $pdf->Cell(39, 5, 'Name & Address : ', 0, 0, '');
         $pdf->Cell(16, 5, 'Name : ', 0, 0, '');
      // $pdf->Cell(157, 5, $customer_name . " " . $customer_address, 'B', 0, '');
   	      $pdf->Cell(179, 5, $customer_name. ' - ' .$customer_address, 'B', 0, '');

        $pdf->Ln();
        $pdf->Ln();
        $pdf->SetX($setx);
        $pdf->SetFont('Arial', 'B', 10);
        if(!empty($discount)){
            $header = array('S.No.', 'PARTICULARS', 'WEIGHT', 'CARTON', 'QTY', 'TP RATE', 'AMOUNT');
        }else{
            $header = array('S.No.', 'PARTICULARS', 'WEIGHT', 'CARTON', 'QTY', 'NET RATE', 'AMOUNT');
        }

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

        function FancyTable($header, $data, $discount, $setx, $add_discount)
        {
            global $pdf;
            // Colors, line width and bold font
            $pdf->SetFillColor(224, 235, 255);
            $pdf->SetTextColor(0);
            $pdf->SetDrawColor(0, 0, 0);
            $pdf->SetLineWidth(.2);
            $pdf->SetFont('', 'B');
            // Header
            $wr = array(13, 65, 20, 21, 20, 29, 27);
            for ($i = 0; $i < count($header); $i++)
                $pdf->Cell($wr[$i], 9, $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;
            $totalCarton = 0;
            if($data){
                $totalQty = 0;
                foreach ($data AS $row) {
                    $productSetup = ProductSetup::GetProductSetupById($row->product_id);

                    $pdf->SetX($setx);
                    $pdf->SetFont('Arial', '', 12);
                    $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, $productSetup->weight . " " . $productSetup->unit, 'LRB', 0, 'C', $fill);
                    $totalCarton += ($row->carton_piece != 0?(number_format(($row->product_qty / $row->carton_piece),0)):0);
                    $pdf->Cell($wr[3], 7, ($row->carton_piece != 0?(number_format(($row->product_qty / $row->carton_piece),0)):"-"), 'LRB', 0, 'C', $fill);
                    $pdf->Cell($wr[4], 7, $row->product_qty, 'LRB', 0, 'C', $fill);
                    $pdf->Cell($wr[5], 7, $row->unit_sale_price, 'LRB', 0, 'C', $fill);

                    $subTotal = ($row->product_qty * $row->unit_sale_price);

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

                        $pdf->SetX($setx);
                        $pdf->SetFont('Arial', '', 9);
                        $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->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->SetFont('Arial', 'B', 18);
                        $pdf->Cell(0, 8, 'INVOICE', 0, 0, 'C');
                        $pdf->Ln();
                        $pdf->SetFont('Arial', 'B', 16);
                        $pdf->Cell(0, 6, PROJECT_NAME, 0, 0, 'C');
                      
                      	$pdf->Ln();
        				$pdf->SetFont('Arial', 'B', 11);
        				$pdf->Cell(55, 8, '', '');
				        $pdf->Cell(105, 8, 'Contact Person : M. RASHID HUSSAIN - 0300-2130852' , 'TLR', 0, 'L');
				        $pdf->Cell(50, 8, '', '');

                        $pdf->Ln();
                        $pdf->SetFont('Arial', 'B', 11);
                        $pdf->Cell(55, 8, '', '');
                        $pdf->Cell(105, 8, 'Contact Person : M. ASIF HUSSAIN - 0300-2219949' , 'TLR', 0, 'L');
                        $pdf->Cell(50, 8, '', '');

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

                        $pdf->Ln();
                        $pdf->SetFont('Arial', 'B', 11);
                        $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;
                    $totalQty += $row->product_qty;
                }
            }

            if (PROJECT_DISCOUNT == 1 && !empty($discount)) {
//                $pdf->Ln();
                $pdf->SetX($setx);
                $pdf->SetFont('Arial', 'B', 12);
                $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, 'Total: '.$totalCarton, 'LRB', 0, 'R');
                $pdf->Cell($wr[4], 7, $totalQty, 'LRB', 0, 'C');
                $pdf->Cell($wr[5], 7, 'Sub Total', 'LRB', 0, 'L', "");
                $pdf->Cell($wr[6], 7, number_format($totalAmount, 2), 'LRB', 0, 'R', "");

                $pdf->Ln();

                $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, 'Discount (%)', 'LB', 0, 'L');
                $pdf->Cell($wr[5], 7, '', 'RB', 0, 'L', "");
                $pdf->Cell($wr[6], 7, number_format($discount, 2), 'LRB', 0, 'R', "");

                

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

                $pdf->Ln();

                $pdf->SetX($setx);
                $pdf->SetFont('Arial', 'B', 10.5);
                $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, 'Discount (price)', 'LB', 0, 'L');
                $pdf->Cell($wr[5], 7, '', 'RB', 0, 'L', "");
                $pdf->Cell($wr[6], 7, number_format($discountAmount, 2), 'LRB', 0, 'R', "");

                $add_discountAmount = 0;
                $totalAmount -= $discountAmount;
                if(!empty($add_discount) && $add_discount != 0){
                    $pdf->Ln();
                    $pdf->SetX($setx);
                    $pdf->SetFont('Arial', 'B', 10.5);
                    $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, 'Sub Total', 'LB', 0, 'R');
                    $pdf->Cell($wr[5], 7, 'Discounted', 'RB', 0, 'L', "");
                    $pdf->Cell($wr[6], 7, number_format($totalAmount, 2), 'LRB', 0, 'R', "");

                    $pdf->Ln();
                    $pdf->SetX($setx);
                    $pdf->SetFont('Arial', 'B', 10.5);
                    $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, 'Additional', 'LB', 0, 'R');
                    $pdf->Cell($wr[5], 7, 'Discount(%)', 'RB', 0, 'L', "");
                    $pdf->Cell($wr[6], 7, number_format($add_discount, 2), 'LRB', 0, 'R', "");
    
                    $add_discountAmount = ($totalAmount *($add_discount/100));
    
                    $pdf->Ln();
    
                    $pdf->SetX($setx);
                    $pdf->SetFont('Arial', 'B', 10.5);
                    $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, 'Additional', 'LB', 0, 'R');
                    $pdf->Cell($wr[5], 7, 'Discount(Pr.)', 'RB', 0, 'L', "");
                    $pdf->Cell($wr[6], 7, number_format($add_discountAmount, 2), 'LRB', 0, 'R', "");
                    $totalAmount -= $add_discountAmount;
                }

                $pdf->Ln();

                $pdf->SetX($setx);
                $pdf->SetFont('Arial', 'B', 11);
                $pdf->Cell(($wr[1]+13), 7, "", '', 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, 'Total Amount', 'LRB', 0, 'L', "");
                $pdf->Cell($wr[6], 7, number_format($totalAmount, 2), 'LRB', 0, 'R', "");

                $filterPayment = " WHERE invoice_reference_code = '$row->invoice_id' AND status <> 'Cancel'";
                $paymentData = Payment::GetPaymentList($filterPayment);

                $pycounter = 1;
                $pyBalance = 0;
                foreach($paymentData AS $paymentDataRow){
                    $pdf->Ln();
                    $pdf->SetX($setx);
                    $pdf->SetFont('Arial', 'B', 11);
                    $pdf->Cell(($wr[1]+13), 7, "", '', 0, 'C');
                    $pdf->Cell($wr[2], 7, "", '', 0, 'C');
                    $pdf->Cell($wr[3], 7, '', 'LTB', 0, 'R');
                    $pdf->Cell($wr[4], 7, date('d-M-Y', strtotime($paymentDataRow->datetime)), 'RTB', 0, 'R');
                    $pdf->SetFont('Arial', 'B', 11);
                    $pdf->Cell($wr[5], 7, 'Payment', 'LRB', 0, 'L', "");
                    $pdf->Cell($wr[6], 7, number_format($paymentDataRow->amount, 2), 'LRB', 0, 'R', "");
                    $pyBalance += $paymentDataRow->amount;
                }

                $pdf->Ln();
                $pdf->SetX($setx);
                $pdf->SetFont('Arial', 'B', 12);
                $pdf->Cell(($wr[1]+13), 7, "", '', 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, 'Total Balance', 'LRB', 0, 'L', "");
                $pdf->Cell($wr[6], 7, number_format(($totalAmount-$pyBalance), 2), 'LRB', 0, 'R', "");


                $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', "");


            } else {
                //$pdf->SetFillColor(255, 204, 0);
//                $pdf->Ln();
                $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, 'Total: '.$totalCarton, 'LRB', 0, 'R');
                $pdf->Cell($wr[4], 7, $totalQty, 'LRB', 0, 'C');
                $pdf->Cell($wr[5], 7, 'Total Amount', 'LRB', 0, 'R', "");
                $pdf->Cell($wr[6], 7, number_format($totalAmount, 2), 'LRB', 0, 'R', "");

                $filterPayment = " WHERE invoice_reference_code = '$row->invoice_id' AND status <> 'Cancel'";
                $paymentData = Payment::GetPaymentList($filterPayment);

                $pycounter = 1;
                $pyBalance = 0;
                foreach($paymentData AS $paymentDataRow){
                    $pdf->Ln();
                    $pdf->SetX($setx);
                    $pdf->SetFont('Arial', 'B', 11);
                    $pdf->Cell(($wr[1]+13), 7, "", '', 0, 'C');
                    $pdf->Cell($wr[2], 7, "", '', 0, 'C');
                    $pdf->Cell($wr[3], 7, '', 'LB', 0, 'R');
                    $pdf->Cell($wr[4], 7, date('d-M-Y', strtotime($paymentDataRow->datetime)), 'RB', 0, 'R');
                    $pdf->SetFont('Arial', 'B', 11);
                    $pdf->Cell($wr[5], 7, 'Payment', 'LRB', 0, 'L', "");
                    $pdf->Cell($wr[6], 7, number_format($paymentDataRow->amount, 2), 'LRB', 0, 'R', "");
                    $pyBalance += $paymentDataRow->amount;
                }

                $pdf->Ln();
                $pdf->SetX($setx);
                $pdf->SetFont('Arial', 'B', 12);
                $pdf->Cell(($wr[1]+13), 7, "", '', 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, 'Total Balance', 'LRB', 0, 'L', "");
                $pdf->Cell($wr[6], 7, number_format(($totalAmount-$pyBalance), 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, $add_discount);
    } else {
        header('location: ' . PROJECT_ADMIN_URL);
        exit;
    }
  
  
  $pdf->Ln;
  // --- Notes (full width), then Remarks UNDER Notes ---
    $pdf->Ln(8);

    // NOTES
    $notes = [
        'For Purchase Orders Please contact at +92 328 8954957',
        'Payment Terms are 7-15 Days from Delivery. '
    ];
    $pdf->SetFont('Arial','B',10);
    $pdf->SetFillColor(224,235,240);
    $pdf->SetX($setx);
    $pdf->Cell($wPage, 7, 'Note:', 1, 1, 'L', true);

    $noteText = '';
    foreach ($notes as $i => $line) { $noteText .= ($i+1).'. '.$line."\n"; }

    $pdf->SetFont('Arial','',10);
    $pdf->SetX($setx);
    $pdf->MultiCell($wPage, 6, $noteText, 1, 'L');
  
  
  

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