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

$formname = 'Discount Taken';
$maindb   = 'discounttaken';
$bccol    = 'cashac';
$haschq   = false;

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

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

$result = mysqli_query($conn,
    "SELECT m.*, a.`name` AS partyname, b.`name` AS bankname
     FROM `$maindb` m
     LEFT JOIN `accmast` a ON a.`accode` = m.`accode`
     LEFT JOIN `accmast` b ON b.`accode` = m.`$bccol`
     WHERE m.`vouchno`='$vouchno'") or die(mysqli_error($conn));
$get = mysqli_fetch_array($result);
if (!$get) $get = [];

$compres  = mysqli_query($conn, "SELECT `name` FROM `$dbname`.`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'])) : '';
$chqdt_disp   = (!empty($get['chqdt'])   && $get['chqdt']   != '0000-00-00') ? date('d-m-Y', strtotime($get['chqdt']))   : '';

function pf($v) {
    if ($v === null || $v === '') return '';
    $v = (float)$v;
    return ($v == (int)$v) ? (string)(int)$v : number_format($v, 2);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title><?php echo $formname; ?> - <?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:12px; }
    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:140px; }
    tfoot td { font-weight:700; background:#f8fafc; }
    @media print { .no-print { display:none; } }
</style>
</head>
<body>
    <h2><?php echo htmlspecialchars($compname); ?></h2>
    <div class="sub"><?php echo $formname; ?> Voucher</div>

    <table class="hdr-table">
        <tr>
            <td class="hdr-label">Voucher No</td><td><?php echo $vouchno; ?></td>
            <td class="hdr-label">Date</td><td><?php echo htmlspecialchars($vouchdt_disp); ?></td>
        </tr>
        <tr>
            <td class="hdr-label">Account</td><td><?php echo htmlspecialchars($get['partyname'] ?? ''); ?></td>
            <td class="hdr-label">Amount</td><td><?php echo pf($get['amount'] ?? 0); ?></td>
        </tr>
        <?php if ($haschq): ?>
        <tr>
            <td class="hdr-label">Cheque No</td><td><?php echo htmlspecialchars($get['chqno'] ?? ''); ?></td>
            <td class="hdr-label">Cheque Date</td><td><?php echo htmlspecialchars($chqdt_disp); ?></td>
        </tr>
        <?php endif; ?>
        <tr>
            <td class="hdr-label">Narration</td><td colspan="3"><?php echo htmlspecialchars($get['narr'] ?? ''); ?></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>
