<?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/fpdf1dc.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);
  

    $deliveryChallan = Deliverychallan::GetDeliveryChallanSetupById($id);
    if (!empty($deliveryChallan)) {
        $dc_id = $deliveryChallan->id;
        $create_date = $deliveryChallan->created_date;
        $code = $deliveryChallan->challan_code;
        $customer_id = $deliveryChallan->customer_id;
        
        $tax_add = 1;
      	$po_no= $deliveryChallan->po_number;

        $customer = CustomerSetup::GetCustomerSetupById($customer_id);
      	$customer_category = $customer->category_id;
        $customer_name = $customer->name;
        $customer_address = $customer->location;
        $customer_ntn = $customer->ntn;
      	$customer_strn = $customer->strn;
        $customer_gst = $customer->gst;
        $customer_no = $customer->code . "-" . $customer->id;
		$customer_discount_perc = $customer->discount_perc;
        // 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('Delivery Challan ' . $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, 'Delivery Challan', '', 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(20, 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(30, 4, 'Delivery Challan 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(100, 4, $customer_name, '', 0, '');
        $pdf->Cell(30, 4, '', '');
        $pdf->SetFont('Arial', 'B', 8);
        $pdf->Cell(30, 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(100, 4, $customer_address, '', 0, '');
        $pdf->Cell(30, 4, '', '');
        $pdf->SetFont('Arial', 'B', 8);
        $pdf->Cell(30, 4, 'PO No:', 0, 0, '');
        $pdf->SetFont('Arial', '', 8);
        $pdf->Cell(20, 4, $po_no, 'R', 0, '');

        // Blank row to replace NTN line (keeps full-width border)
//$pdf->Ln();
$pdf->SetX($setx);
$pdf->SetFont('Arial', '', 8);
$pdf->Cell(195, 4, '', 'LR', 0, ''); // left and right borders only

// Blank row to replace STRN line (keeps bottom border too)
$pdf->Ln();
$pdf->SetX($setx);
$pdf->SetFont('Arial', '', 8);
$pdf->Cell(195, 4, '', 'LBR', 0, ''); // left, bottom, right borders
		$pdf->Ln();
        $pdf->Ln();
        $pdf->Ln();
        $pdf->SetX($setx);
        
      // Table Header
$header = array('S.No', 'Product Name', 'Cartons');
$widths = array(20, 120, 55);
$pdf->SetX($setx);
$pdf->SetFillColor(200, 200, 200);
$pdf->SetFont('Arial', 'B', 9);
for ($i = 0; $i < count($header); $i++) {
$pdf->Cell($widths[$i], 7, $header[$i], 1, 0, 'C');
}
$pdf->Ln();


$productData = Deliverychallan::GetDeliverychallanProducts("WHERE challan_id = '$dc_id'");


$pdf->SetFont('Arial', '', 9);
$count = 1;
$totalCartons = 0;


foreach ($productData as $row) {
$product = ProductSetup::GetProductSetupById($row->product_id);
$cartons = intval($row->quantity);
$totalCartons += $cartons;


$pdf->SetX($setx);
$pdf->Cell($widths[0], 7, $count, 1);
$pdf->Cell($widths[1], 7, $product->name, 1);
$pdf->Cell($widths[2], 7, $cartons, 1, 0, 'C');
$pdf->Ln();
$count++;
}


// Total Cartons
$pdf->SetX($setx);
$pdf->SetFont('Arial', 'B', 9);
$pdf->Cell($widths[0] + $widths[1], 7, 'Total Cartons', 1);
$pdf->Cell($widths[2], 7, $totalCartons, 1, 0, 'C');
      
    $pdf->Ln(10); // space after the main table

$colWidth = 97.5;  // Match total table width of 195mm
$rowHeight = 10;
$pdf->SetX($setx);

$pdf->SetFont('Arial', 'B', 10);
$pdf->Cell($colWidth, $rowHeight, 'Delivery Details', 1, 0, 'C');
$pdf->Cell($colWidth, $rowHeight, 'Signatures', 1, 1, 'C');

$pdf->SetFont('Arial', '', 9);

// Row 1
$pdf->SetX($setx);
$pdf->Cell($colWidth, $rowHeight, 'Vehicle No: ', 1, 0, 'L');
$pdf->Cell($colWidth, $rowHeight, 'Prepared By: ', 1, 1, 'L');

// Row 2
$pdf->SetX($setx);
$pdf->Cell($colWidth, $rowHeight, 'Driver Name: ', 1, 0, 'L');
$pdf->Cell($colWidth, $rowHeight, 'Checked By: ', 1, 1, 'L');

// Row 3
$pdf->SetX($setx);
$pdf->Cell($colWidth, $rowHeight, 'Dispatch From: ', 1, 0, 'L');
$pdf->Cell($colWidth, $rowHeight, 'Authorized Signatory: ', 1, 1, 'L');

// Row 4
$pdf->SetX($setx);
$pdf->Cell($colWidth, $rowHeight, 'Dispatch To:', 1, 0, 'L');
$pdf->Cell($colWidth, $rowHeight, 'Receivers Signature and Stamp: ', 1, 1, 'L');




$pdf->Output();
ob_end_flush();
exit;
} else {
header('location: ' . PROJECT_ADMIN_URL);
exit;
}
} catch (Exception $e) {
echo $e->getMessage();
}
