<?php
// Delivery Challan PDF for a Sale Invoice — rebuilt as a real PDF (TCPDF),
// modeled on saleorder_pdf.php's layout/style (helvetica font, bordered
// header rows, item table, footer), instead of the old browser-print HTML
// page. Sourced from `sale`/`saletr` (the Sale Invoice module). Quantities
// only — no pricing, per standard delivery challan convention.
ob_start();
require("includes/check_auth_user.php");
require("includes/dbconnect.php");
require("includes/config.php");
require("includes/function.php");

ini_set("display_errors", 1);
error_reporting(E_ERROR);

$zdb     = $_SESSION["zdbyear_" . $dbname];
$compcd  = $_SESSION["comp_"    . $dbname];
$zdbroot = $_SESSION["zdbroot"] = $dbname;   // sim_root
mysqli_select_db($conn, $zdb);

$vouchno = isset($_GET['vouchno']) ? (int)$_GET['vouchno'] : 0;

$result = mysqli_query($conn,
    "SELECT s.*, a.name AS customername, a.add1, a.add2, a.add3, a.city, a.pin, a.mobile,
            g.name AS godownname
     FROM `sale` s
     LEFT JOIN `accmast` a ON a.accode = s.customer
     LEFT JOIN `godown`  g ON g.code   = s.godown
     WHERE s.vouchno='$vouchno'") or die(mysqli_error($conn));
$get = mysqli_fetch_array($result);
if (!$get) { die("Invoice not found."); }

$tres = mysqli_query($conn,
    "SELECT d.*, p.name AS prodname
     FROM `saletr` d
     LEFT JOIN `product` p ON p.id = d.product
     WHERE d.vouchno='$vouchno' ORDER BY d.srl") or die(mysqli_error($conn));
$lines = [];
$totqty = 0;
while ($r = mysqli_fetch_array($tres)) { $lines[] = $r; $totqty += (float)$r['tqty']; }

$compres = mysqli_query($conn, "SELECT * FROM $zdbroot.`company` WHERE `code`='$compcd'");
$comprow = $compres ? mysqli_fetch_assoc($compres) : null;
$compname = $comprow ? $comprow['name'] : '';

$vouchdt_disp = (!empty($get['vouchdt']) && $get['vouchdt'] != '0000-00-00') ? date('d-m-Y', strtotime($get['vouchdt'])) : '';

function pf2($v) {
    if ($v === null || $v === '') return '';
    $v = (float)$v;
    if ($v == 0) return '';
    return ($v == (int)$v) ? number_format($v, 0) : number_format($v, 2);
}

require_once("library/TCPDF/tcpdf.php");

$pdf = new TCPDF('P', 'mm', 'A4', true, 'UTF-8', false);
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
$pdf->AddPage();

$pdf->SetFont('helvetica', 'B', 20);
$pdf->Cell(190, 8, "Delivery Challan", 'TRL', 0, 'C');
$pdf->Ln(8);

$pdf->SetFont('helvetica', 'B', 12);
$pdf->Cell(18, 6, "M/s.", 'TL', 0, 'L');
$pdf->Cell(2, 6, ":", 'T', 0, 'L');
$pdf->SetFont('helvetica', '', 10);
$pdf->Cell(100, 6, $get['customername'] ?? '', "T", 0, 'L');

$pdf->SetFont('helvetica', 'B', 12);
$pdf->Cell(20, 6, "Invoice", "T", 0, 'R');
$pdf->Cell(2, 6, ":", 'T', 0, 'L');
$pdf->SetFont('helvetica', 'B', 10);
$pdf->Cell(48, 6, "SAL#" . $vouchno, 'TR', 0, 'L');
$pdf->Ln(6);

$pdf->SetFont('helvetica', 'B', 12);
$pdf->Cell(18, 6, "Address", 'L', 0, 'L');
$pdf->Cell(2, 6, ":", '', 0, 'L');
$pdf->SetFont('helvetica', '', 10);
$pdf->Cell(100, 6, trim(($get['add1'] ?? '') . ' ' . ($get['add2'] ?? '')), 0, 0, 'L');

$pdf->SetFont('helvetica', 'B', 12);
$pdf->Cell(20, 6, "Date", '', 0, 'R');
$pdf->Cell(2, 6, ":", '', 0, 'L');
$pdf->SetFont('helvetica', 'B', 10);
$pdf->Cell(48, 6, $vouchdt_disp, 'R', 0, 'L');
$pdf->Ln(6);

$pdf->SetFont('helvetica', 'B', 12);
$pdf->Cell(18, 6, "", 'L', 0, 'L');
$pdf->Cell(2, 6, "", '', 0, 'L');
$pdf->SetFont('helvetica', '', 10);
$pdf->Cell(100, 6, trim(($get['add3'] ?? '') . ' ' . ($get['city'] ?? '') . ' ' . ($get['pin'] ?? '')), 0, 0, 'L');
$pdf->SetFont('helvetica', 'B', 12);
$pdf->Cell(20, 6, "Godown", 0, 0, 'R');
$pdf->Cell(2, 6, ":", '', 0, 'L');
$pdf->SetFont('helvetica', '', 10);
$pdf->Cell(48, 6, $get['godownname'] ?? '', 'R', 0, 'L');
$pdf->Ln(6);

$pdf->SetFont('helvetica', 'B', 12);
$pdf->Cell(18, 6, "Mobile.", 'LB', 0, 'L');
$pdf->Cell(2, 6, ":", 'B', 0, 'L');
$pdf->SetFont('helvetica', '', 10);
$pdf->Cell(100, 6, trim(($get['mobile'] ?? '')), 'B', 0, 'L');
$pdf->SetFont('helvetica', 'B', 12);
$pdf->Cell(20, 6, "Remarks", 'B', 0, 'R');
$pdf->Cell(2, 6, ":", 'B', 0, 'L');
$pdf->SetFont('helvetica', '', 10);
$pdf->Cell(48, 6, $get['remarks'] ?? '', 'RB', 0, 'L');
$pdf->Ln(10);

// ── Item table (Product/Qty only — no pricing) ─────────────────────────────
$pdf->SetFont('helvetica', 'B', 10);
$pdf->Cell(15, 6, "Sr.", 'TLB', 0, 'C');
$pdf->Cell(145, 6, "Product", 'TLB', 0, 'C');
$pdf->Cell(30, 6, "Qty.", 'TLRB', 0, 'R');
$pdf->Ln(6);

$a = 0;
foreach ($lines as $ln) {
    $a++;
    $pdf->SetFont('helvetica', '', 10);
    $pdf->Cell(15, 6, (string)$a, 'BL', 0, 'C');
    $pdf->Cell(145, 6, $ln['prodname'] ?? $ln['description'] ?? '', 'LB', 0, 'L');
    $pdf->Cell(30, 6, pf2($ln['tqty'] ?? 0), 'BLR', 0, 'R');
    $pdf->Ln(6);
}

$newctr = 18 - $a;
for ($i = 0; $i <= $newctr; $i++) {
    $pdf->Cell(15, 6, "", 'BL', 0, 'C');
    $pdf->Cell(145, 6, "", 'LB', 0, 'C');
    $pdf->Cell(30, 6, "", 'BLR', 0, 'C');
    $pdf->Ln(6);
}

$pdf->SetFont('helvetica', 'B', 12);
$pdf->Cell(160, 6, "Total Qty", 'BL', 0, 'R');
$pdf->Cell(30, 6, pf2($totqty), 'BLR', 0, 'R');
$pdf->Ln(10);

$pdf->SetFont('helvetica', '', 9);
$pdf->MultiCell(190, 5, "This Delivery Challan is issued for goods dispatched against Sale Invoice SAL#$vouchno. Not a demand for payment.", 0, 'L');
$pdf->Ln(20);

$pdf->SetFont('helvetica', '', 10);
$pdf->Cell(95, 6, "", 'T', 0, 'C');
$pdf->Cell(95, 6, "", 'T', 0, 'C');
$pdf->Ln(6);
$pdf->Cell(95, 6, "Receiver's Signature", 0, 0, 'C');
$pdf->Cell(95, 6, "For " . $compname, 0, 0, 'C');

if (ob_get_level() > 0) { ob_end_clean(); }
$pdf->Output("Delivery Challan-" . $vouchno . ".pdf", "I");
