<?php
ob_start();
require("includes/check_auth_user.php");
require("includes/dbconnect.php");
require("includes/config.php");
require("includes/function.php");

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

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

$result = mysqli_query($conn,
    "SELECT p.*, a.name AS suppliername, g.name AS godownname
     FROM `purchreturn` p
     LEFT JOIN `accmast` a ON a.accode = p.supplier
     LEFT JOIN `godown` g ON g.code = p.godown
     WHERE p.vouchno='$vouchno'") or die(mysqli_error($conn));
$get = mysqli_fetch_array($result);
if (!$get) $get = [];

$tres = mysqli_query($conn,
    "SELECT d.*, b.name AS brandname, pr.name AS productname
     FROM `purchreturndtl` d
     LEFT JOIN `brand` b ON b.code = d.brand
     LEFT JOIN `product` pr ON pr.id = d.product
     WHERE d.`vouchno`='$vouchno' ORDER BY d.`srl`") or die(mysqli_error($conn));
$lines = [];
while ($r = mysqli_fetch_array($tres)) { $lines[] = $r; }

$compres  = mysqli_query($conn, "SELECT `name` 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 pf($v) {
    if ($v === null || $v === '') return '';
    $v = (float)$v;
    return $v == 0 ? '' : (($v == (int)$v) ? (string)(int)$v : number_format($v, 2));
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Purchase Entry - <?php echo $vouchno; ?></title>
<style>
    body { font-family: Arial, sans-serif; font-size:13px; color:#1e293b; margin:20px; }
    h2 { text-align:center; margin:0 0 4px; }
    .sub { text-align:center; color:#64748b; margin-bottom:16px; }
    table { width:100%; border-collapse:collapse; margin-top:10px; }
    th, td { border:1px solid #cbd5e1; padding:6px 8px; font-size:11.5px; }
    th { background:#f1f5f9; text-align:center; }
    td.num { text-align:right; }
    .hdr-table td { border:none; padding:3px 6px; font-size:12.5px; }
    .hdr-label { color:#64748b; font-weight:600; width:120px; }
    tfoot td { font-weight:700; background:#f8fafc; }
    .charges-table { width:320px; margin-left:auto; margin-top:10px; }
    .charges-table td { border:none; padding:3px 6px; }
    .charges-table .lbl { color:#64748b; text-align:right; }
    .charges-table .amt { text-align:right; font-weight:600; width:120px; }
    .charges-table .net td { border-top:2px solid #1e293b; font-weight:700; font-size:13px; }
    @media print { .no-print { display:none; } }
</style>
</head>
<body>
    <h2><?php echo htmlspecialchars($compname); ?></h2>
    <div class="sub">Purchase Entry</div>

    <table class="hdr-table">
        <tr>
            <td class="hdr-label">Voucher No</td><td>PUR#<?php echo $vouchno; ?></td>
            <td class="hdr-label">Date</td><td><?php echo htmlspecialchars($vouchdt_disp); ?></td>
        </tr>
        <tr>
            <td class="hdr-label">Supplier</td><td><?php echo htmlspecialchars($get['suppliername'] ?? ''); ?></td>
            <td class="hdr-label">Godown</td><td><?php echo htmlspecialchars($get['godownname'] ?? ''); ?></td>
        </tr>
        <tr>
            <td class="hdr-label">Remarks</td><td colspan="3"><?php echo htmlspecialchars($get['remarks'] ?? ''); ?></td>
        </tr>
    </table>

    <table>
        <thead>
            <tr>
                <th>#</th>
                <th>Brand</th>
                <th>Product</th>
                <th>Qty</th>
                <th>Cost</th>
                <th>Amount</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($lines as $i => $ln): ?>
            <tr>
                <td style="text-align:center;"><?php echo $i + 1; ?></td>
                <td><?php echo htmlspecialchars($ln['brandname'] ?? ''); ?></td>
                <td><?php echo htmlspecialchars($ln['productname'] ?? ''); ?></td>
                <td class="num"><?php echo pf($ln['qty']); ?></td>
                <td class="num"><?php echo pf($ln['cost']); ?></td>
                <td class="num"><?php echo pf($ln['amount']); ?></td>
            </tr>
            <?php endforeach; ?>
        </tbody>
        <tfoot>
            <tr>
                <td colspan="5" style="text-align:right;">Subtotal</td>
                <td class="num"><?php echo pf($get['subtotal'] ?? 0); ?></td>
            </tr>
        </tfoot>
    </table>

    <table class="charges-table">
        <tr><td class="lbl">Packing Charges</td><td class="amt"><?php echo pf($get['packingcharges'] ?? 0); ?></td></tr>
        <tr><td class="lbl">Transport Charges</td><td class="amt"><?php echo pf($get['transport_charges'] ?? 0); ?></td></tr>
        <tr><td class="lbl">Other Charges</td><td class="amt"><?php echo pf($get['other_charges'] ?? 0); ?></td></tr>
        <tr class="net"><td class="lbl">Net Amount</td><td class="amt"><?php echo pf($get['netamount'] ?? 0); ?></td></tr>
    </table>

    <div class="no-print" style="margin-top:16px;text-align:center;">
        <button onclick="window.print()" style="padding:8px 18px;border:none;border-radius:6px;background:#2563eb;color:#fff;cursor:pointer;">Print</button>
    </div>
</body>
</html>
