<?php

ini_set('display_errors',1);
error_reporting(E_ALL);

require_once __DIR__.'/../../includes/auth.php';
requireLogin();

require_once __DIR__.'/../../config/database.php';

require_once __DIR__.'/../../includes/header.php';
require_once __DIR__.'/../../includes/sidebar.php';

$company_id = $_SESSION['company_id'];

/* ===============================
TOTAL INVOICES
================================ */

$stmt = $pdo->prepare("
SELECT SUM(total_amount) as total
FROM supplier_invoices
WHERE company_id = ?
");

$stmt->execute([$company_id]);
$total_invoices = $stmt->fetch()['total'] ?? 0;

/* ===============================
TOTAL PAID
================================ */

$stmt = $pdo->prepare("
SELECT SUM(paid_amount) as paid
FROM supplier_invoices
WHERE company_id = ?
");

$stmt->execute([$company_id]);
$total_paid = $stmt->fetch()['paid'] ?? 0;

/* ===============================
OUTSTANDING
================================ */

$outstanding = $total_invoices - $total_paid;

/* ===============================
FETCH OPEN INVOICES
================================ */

$stmt = $pdo->prepare("
SELECT si.*, s.supplier_name
FROM supplier_invoices si
LEFT JOIN suppliers s ON si.supplier_id = s.id
WHERE si.company_id = ?
AND si.status != 'paid'
ORDER BY si.invoice_date DESC
");

$stmt->execute([$company_id]);
$open_invoices = $stmt->fetchAll();

?>

<div class="main-content">

<div class="page-header">

<h2>Supplier Payables</h2>

</div>

<div class="stats-grid">

<div class="stat-card">

<h3>Total Supplier Invoices</h3>

<p style="font-size:28px;font-weight:bold;">
R <?php echo number_format($total_invoices,2); ?>
</p>

</div>

<div class="stat-card">

<h3>Total Paid</h3>

<p style="font-size:28px;font-weight:bold;color:green;">
R <?php echo number_format($total_paid,2); ?>
</p>

</div>

<div class="stat-card">

<h3>Outstanding Payables</h3>

<p style="font-size:28px;font-weight:bold;color:red;">
R <?php echo number_format($outstanding,2); ?>
</p>

</div>

</div>

<div class="stat-card">

<h3>Open Supplier Invoices</h3>

<div class="table-responsive">

<table class="table table-striped">

<thead>

<tr>
<th>Invoice</th>
<th>Supplier</th>
<th>Total</th>
<th>Paid</th>
<th>Balance</th>
<th>Status</th>
<th>Action</th>
</tr>

</thead>

<tbody>

<?php if(count($open_invoices)==0){ ?>

<tr>
<td colspan="7" style="text-align:center;">
No outstanding invoices
</td>
</tr>

<?php } ?>

<?php foreach($open_invoices as $inv){ ?>

<tr>

<td><?php echo htmlspecialchars($inv['invoice_number']); ?></td>

<td><?php echo htmlspecialchars($inv['supplier_name']); ?></td>

<td>R <?php echo number_format($inv['total_amount'],2); ?></td>

<td>R <?php echo number_format($inv['paid_amount'],2); ?></td>

<td>
R <?php echo number_format($inv['total_amount'] - $inv['paid_amount'],2); ?>
</td>

<td><?php echo htmlspecialchars($inv['status']); ?></td>

<td>

<a href="../supplier_invoices/view.php?id=<?php echo $inv['id']; ?>" 
class="btn btn-sm btn-primary">
Pay
</a>

</td>

</tr>

<?php } ?>

</tbody>

</table>

</div>

</div>

</div>

<?php require_once __DIR__.'/../../includes/footer.php'; ?>