<?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'];

/* ===============================
FETCH INVENTORY VALUE
================================ */

$stmt = $pdo->prepare("
SELECT *,
(stock_qty * cost_price) as stock_value
FROM inventory_products
WHERE company_id = ?
AND track_inventory = 1
ORDER BY product_name ASC
");

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

/* ===============================
TOTAL STOCK VALUE
================================ */

$total_value = 0;

foreach($products as $p){

$total_value += $p['stock_value'];

}

?>

<div class="main-content">

<div class="page-header">

<h2>Inventory Stock Value</h2>

</div>

<div class="stat-card">

<h3>Total Inventory Value</h3>

<p style="font-size:30px;font-weight:bold;color:#f2531e;">
R <?php echo number_format($total_value,2); ?>
</p>

</div>

<div class="stat-card">

<div class="table-responsive">

<table class="table table-striped">

<thead>

<tr>
<th>Product</th>
<th>SKU</th>
<th>Stock Qty</th>
<th>Cost Price</th>
<th>Stock Value</th>
</tr>

</thead>

<tbody>

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

<tr>
<td colspan="5" style="text-align:center;">
No inventory found
</td>
</tr>

<?php } ?>

<?php foreach($products as $p){ ?>

<tr>

<td><?php echo htmlspecialchars($p['product_name']); ?></td>

<td><?php echo htmlspecialchars($p['sku']); ?></td>

<td><?php echo $p['stock_qty']; ?></td>

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

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

</tr>

<?php } ?>

</tbody>

</table>

</div>

</div>

</div>

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