<?php
// hps_sale_report.php
error_reporting(E_ALL);
ini_set('display_errors', 1);

session_start();
require "db.php";
require "hps_core.php";

$from = $_GET['from'] ?? '';
$to   = $_GET['to'] ?? '';

$where = "WHERE s.status='Approved'";
if ($from && $to) {
    $f = mysqli_real_escape_string($conn, $from);
    $t = mysqli_real_escape_string($conn, $to);
    $where .= " AND DATE(s.sale_date) BETWEEN '$f' AND '$t'";
}

$res = mysqli_query($conn, "
  SELECT s.id, s.sale_no, s.sale_date, c.name customer
  FROM hps_sales s
  JOIN hps_customers c ON c.id=s.customer_id
  $where
  ORDER BY s.id DESC
  LIMIT 1000
");

$total_sales = 0; // HP total (460000)
$total_doc   = 0; // doc total
?>
<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>Sales Report</title>
  <link href="assets/bootstrap.min.css" rel="stylesheet">
</head>
<body class="p-3">
<div class="container">

  <div class="card border-dark">
    <div class="card-header bg-light fw-bold">Sales Report</div>
    <div class="card-body">

      <form class="row g-2 mb-3">
        <div class="col-md-3">
          <input type="date" name="from" value="<?= htmlspecialchars($from) ?>" class="form-control">
        </div>
        <div class="col-md-3">
          <input type="date" name="to" value="<?= htmlspecialchars($to) ?>" class="form-control">
        </div>
        <div class="col-md-2 d-grid">
          <button class="btn btn-dark">Filter</button>
        </div>
      </form>

      <div class="table-responsive">
        <table class="table table-sm">
          <thead class="table-light">
            <tr>
              <th>Sale#</th>
              <th>Date</th>
              <th>Customer</th>
              <th class="text-end">HP Sale</th>
              <th class="text-end">Doc</th>
              <th class="text-end">Total Receivable</th>
            </tr>
          </thead>
          <tbody>
          <?php while($r=mysqli_fetch_assoc($res)): ?>
            <?php
              $calc = hps_sale_calc($conn, (int)$r['id']);
              $total_sales += $calc['hp_total'];
              $total_doc   += $calc['doc_charges'];
            ?>
            <tr>
              <td><?= htmlspecialchars($r['sale_no'] ?? $r['id']) ?></td>
              <td><?= htmlspecialchars($r['sale_date'] ?? '') ?></td>
              <td><?= htmlspecialchars($r['customer']) ?></td>
              <td class="text-end"><?= number_format($calc['hp_total']) ?></td>
              <td class="text-end"><?= number_format($calc['doc_charges']) ?></td>
              <td class="text-end"><?= number_format($calc['total_receivable']) ?></td>
            </tr>
          <?php endwhile; ?>
          </tbody>

          <tfoot class="table-light">
            <tr>
              <th colspan="3" class="text-end">TOTAL</th>
              <th class="text-end"><?= number_format($total_sales) ?></th>
              <th class="text-end"><?= number_format($total_doc) ?></th>
              <th class="text-end"><?= number_format($total_sales + $total_doc) ?></th>
            </tr>
          </tfoot>

        </table>
      </div>

    </div>
  </div>

</div>
</body>
</html>
