<?php
// hps_supplier_ledger.php
require "db.php";

function esc($v){
    return htmlspecialchars($v ?? '', ENT_QUOTES, 'UTF-8');
}

/* -----------------------------
   FILTERS
------------------------------*/
$supplier_id = (int)($_GET['supplier_id'] ?? 0);
$from        = $_GET['from'] ?? '';
$to          = $_GET['to'] ?? '';

$where = [];
if ($supplier_id > 0) $where[] = "l.supplier_id=$supplier_id";
if ($from) $where[] = "l.trans_date>='$from'";
if ($to)   $where[] = "l.trans_date<='$to'";
$whereSql = count($where) ? "WHERE ".implode(" AND ",$where) : "";

/* -----------------------------
   SUPPLIERS
------------------------------*/
$suppliers = mysqli_query($conn,"
    SELECT id, name FROM hps_suppliers ORDER BY name
");

/* -----------------------------
   LEDGER DATA
------------------------------*/
$ledger = mysqli_query($conn,"
    SELECT l.*, s.name AS supplier
    FROM hps_supplier_ledger l
    JOIN hps_suppliers s ON s.id=l.supplier_id
    $whereSql
    ORDER BY l.supplier_id, l.trans_date, l.id
");
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Supplier Ledger</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">

<style>
body{background:#f1f3f5}
.main-card{
    background:#fff;
    max-width:1100px;
    margin:30px auto;
    padding:25px;
    border-radius:20px;
    box-shadow:0 10px 25px rgba(0,0,0,.1);
}
@media print{
    .no-print{display:none}
    body{background:#fff}
}
</style>
</head>
<body>

<div class="main-card">

<h4 class="mb-3">Supplier Ledger</h4>

<!-- FILTERS -->
<form method="get" class="row g-2 mb-3 no-print">

<div class="col-md-4">
<select name="supplier_id" class="form-select form-select-sm">
<option value="">All Suppliers</option>
<?php while($s=mysqli_fetch_assoc($suppliers)): ?>
<option value="<?=$s['id']?>" <?= $supplier_id==$s['id']?'selected':'' ?>>
<?=esc($s['name'])?>
</option>
<?php endwhile; ?>
</select>
</div>

<div class="col-md-3">
<input type="date" name="from" value="<?=$from?>" class="form-control form-control-sm">
</div>

<div class="col-md-3">
<input type="date" name="to" value="<?=$to?>" class="form-control form-control-sm">
</div>

<div class="col-md-2 d-flex gap-1">
<button class="btn btn-primary btn-sm w-100">Filter</button>
<button type="button" onclick="window.print()"
class="btn btn-secondary btn-sm w-100">Print</button>
</div>

</form>

<!-- LEDGER -->
<table class="table table-sm table-bordered">
<thead class="table-dark">
<tr>
<th>Date</th>
<th>Supplier</th>
<th>Description</th>
<th class="text-end">Dr</th>
<th class="text-end">Cr</th>
<th class="text-end">Balance</th>
</tr>
</thead>
<tbody>

<?php
$balance = [];
while($r=mysqli_fetch_assoc($ledger)):
$sid = $r['supplier_id'];
$balance[$sid] = ($balance[$sid] ?? 0) + $r['dr'] - $r['cr'];
?>
<tr>
<td><?=date('d-M-Y',strtotime($r['trans_date']))?></td>
<td><?=esc($r['supplier'])?></td>
<td><?=esc($r['description'])?></td>
<td class="text-end"><?= $r['dr']>0?number_format($r['dr'],2):'' ?></td>
<td class="text-end"><?= $r['cr']>0?number_format($r['cr'],2):'' ?></td>
<td class="text-end <?= $balance[$sid]>0?'text-danger':'text-success' ?>">
<?=number_format($balance[$sid],2)?>
</td>
</tr>
<?php endwhile; ?>

<?php if(mysqli_num_rows($ledger)==0): ?>
<tr><td colspan="6" class="text-center text-muted">No records</td></tr>
<?php endif; ?>

</tbody>
</table>

</div>

</body>
</html>
