<?php
require_once '../db.php';
require_once '../includes/company.php';

$id   = (int)($_GET['id'] ?? 0);
$type = $_GET['type'] ?? 'invoice';   // invoice | transport

if ($id <= 0) {
    die("Invalid Invoice");
}

$inv = $conn->query("SELECT * FROM sales_invoices WHERE id=$id")->fetch_assoc();
if (!$inv) {
    die("Invoice not found");
}

$company = getCompany($conn);

$client = $conn->query("
    SELECT * FROM clients 
    WHERE id = {$inv['client_id']}
")->fetch_assoc();

$items = $conn->query("
    SELECT 
        si.*,
        si.description AS item_description,
        p.name AS product_name,
        p.hsn_code,
        p.unit,
        pi.mrp
    FROM sales_items si
    JOIN products p ON p.id = si.product_id
    LEFT JOIN purchase_items pi ON pi.id = si.purchase_item_id
    WHERE si.sales_invoice_id = $id
");

if ($type === 'transport') {
    require 'layout_thermal_transport.php';
} else {
    require 'layout_thermal.php';
}
