<?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];
mysqli_select_db($conn, $zdb);
$frmdate = $_POST['frmdate'] ?? date('Y-m-01');
$todate  = $_POST['todate']  ?? date('Y-m-d');
$supplier = (int)($_POST['supplier'] ?? 0);
$customer = (int)($_POST['customer'] ?? 0);
// BC confirmed but NOT yet in purchase.billconfref
$where = "WHERE b.vouchdt BETWEEN '$frmdate' AND '$todate'
          AND b.vouchno IN (SELECT DISTINCT billconfref FROM purchase WHERE billconfref>0)
          AND b.vouchno NOT IN (SELECT DISTINCT billconfref FROM purchase p2 WHERE billconfref>0 AND p2.vouchno IS NOT NULL)";
// Simpler: BCs that have been picked in purchase
$where = "WHERE b.vouchdt BETWEEN '$frmdate' AND '$todate'
          AND b.vouchno NOT IN (SELECT DISTINCT billconfref FROM purchase WHERE billconfref>0)";
if ($supplier) $where .= " AND b.supplier='$supplier'";
if ($customer) $where .= " AND b.party='$customer'";
$res = mysqli_query($conn,
    "SELECT b.vouchno, b.vouchdt, b.shipmark, b.origin,
            s.name AS supname, c.name AS custname,
            b.totctn, b.tottqty, b.totamt, b.advance,
            DATEDIFF(CURDATE(), b.vouchdt) AS days_old
     FROM billconf b
     LEFT JOIN accmast s ON s.accode=b.supplier
     LEFT JOIN accmast c ON c.accode=b.party
     $where ORDER BY b.vouchno DESC") or die(mysqli_error($conn));
$rows=[]; while($r=mysqli_fetch_assoc($res)) $rows[]=$r;
?>
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>Pending Purchase</title>
<?php include("includes/rpt_style.php"); ?>
</head><body>
<div class="rpt-wrap">
<div class="rpt-header">
    <div class="rpt-title"><i class="fa fa-cart-plus" style="color:#3b82f6;"></i> Pending Purchase</div>
    <div class="rpt-meta"><?php echo date('d-m-Y',strtotime($frmdate)); ?> to <?php echo date('d-m-Y',strtotime($todate)); ?> &nbsp;·&nbsp; <?php echo count($rows); ?> records</div>
</div>
<?php if (!$rows): ?><div class="empty">No pending purchases found.</div><?php else: ?>
<table>
    <thead><tr><th>#</th><th>BC#</th><th>Date</th><th>Days Old</th><th>Supplier</th><th>Customer</th><th>Ship Mark</th><th class="num">CTN</th><th class="num">T.QTY</th><th class="num">Amount</th><th class="num">Advance</th></tr></thead>
    <tbody>
    <?php $tC=$tQ=$tA=$tAdv=0; foreach($rows as $i=>$r):
        $tC+=(float)$r['totctn']; $tQ+=(float)$r['tottqty']; $tA+=(float)$r['totamt']; $tAdv+=(float)$r['advance'];
        $age=(int)$r['days_old']; $ageCls=$age>30?'br':($age>14?'ba':'bg');
    ?>
    <tr>
        <td><?php echo $i+1; ?></td>
        <td><span class="badge bb">BC#<?php echo $r['vouchno']; ?></span></td>
        <td><?php echo $r['vouchdt']?date('d-m-Y',strtotime($r['vouchdt'])):''; ?></td>
        <td class="ctr"><span class="badge <?php echo $ageCls; ?>"><?php echo $age; ?> days</span></td>
        <td><?php echo htmlspecialchars($r['supname']??''); ?></td>
        <td><?php echo htmlspecialchars($r['custname']??''); ?></td>
        <td><?php echo htmlspecialchars($r['shipmark']??''); ?></td>
        <td class="num"><?php echo number_format((float)$r['totctn'],0); ?></td>
        <td class="num"><?php echo number_format((float)$r['tottqty'],0); ?></td>
        <td class="num">¥<?php echo number_format((float)$r['totamt'],2); ?></td>
        <td class="num">¥<?php echo number_format((float)$r['advance'],2); ?></td>
    </tr>
    <?php endforeach; ?>
    </tbody>
    <tfoot><tr><td colspan="7" class="num">Total</td><td class="num"><?php echo number_format($tC,0); ?></td><td class="num"><?php echo number_format($tQ,0); ?></td><td class="num">¥<?php echo number_format($tA,2); ?></td><td class="num">¥<?php echo number_format($tAdv,2); ?></td></tr></tfoot>
</table>
<?php endif; ?>
</div>
<div class="rpt-actions">
    <button class="btn-print" onclick="window.print()"><i class="fa fa-print"></i> Print</button>
    <button class="btn-back" onclick="if(window.parent&&window.parent!==window){window.parent.document.getElementById('rptframe').src='about:blank';}else{window.close();}"><i class="fa fa-arrow-left"></i> Go Back</button>
</div>
</body></html>
