<?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);

$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.totqty, b.tottqty, b.totamt, b.advance
     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 BC</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-clock" style="color:#f59e0b;"></i> Pending Bill Confirmation</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 bill confirmings found.</div>
<?php else: ?>
<table>
    <thead><tr>
        <th>#</th><th>BC#</th><th>Date</th><th>Supplier</th><th>Customer</th>
        <th>Ship Mark</th><th>Origin</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 $tCtn=$tTqty=$tAmt=$tAdv=0; foreach ($rows as $i=>$r):
        $tCtn+=(float)$r['totctn']; $tTqty+=(float)$r['tottqty'];
        $tAmt+=(float)$r['totamt']; $tAdv+=(float)$r['advance'];
    ?>
    <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><?php echo htmlspecialchars($r['supname']??''); ?></td>
        <td><?php echo htmlspecialchars($r['custname']??''); ?></td>
        <td><?php echo htmlspecialchars($r['shipmark']??''); ?></td>
        <td><?php echo htmlspecialchars($r['origin']??''); ?></td>
        <td class="num"><?php echo $r['totctn']?number_format((float)$r['totctn'],0):''; ?></td>
        <td class="num"><?php echo $r['tottqty']?number_format((float)$r['tottqty'],0):''; ?></td>
        <td class="num">¥<?php echo number_format((float)$r['totamt'],2); ?></td>
        <td class="num"><?php echo $r['advance']?'¥'.number_format((float)$r['advance'],2):''; ?></td>
    </tr>
    <?php endforeach; ?>
    </tbody>
    <tfoot><tr>
        <td colspan="7" class="num">Total (<?php echo count($rows); ?> records)</td>
        <td class="num"><?php echo number_format($tCtn,0); ?></td>
        <td class="num"><?php echo number_format($tTqty,0); ?></td>
        <td class="num">¥<?php echo number_format($tAmt,2); ?></td>
        <td class="num">¥<?php echo number_format($tAdv,2); ?></td>
    </tr></tfoot>
</table>
<?php endif; ?>
</div>
</body></html>
