38960-vm/includes/pages/inventory_reports.php
2026-03-21 17:39:37 +00:00

112 lines
5.3 KiB
PHP

<?php
$report_type = $_GET['report'] ?? 'low_stock';
// Data Fetching based on report type
$data = [];
$columns = [];
if ($report_type === 'low_stock') {
$title = __('low_stock_report');
$columns = ['item_name', 'sku', 'min_level', 'current_stock', 'status'];
$data = $db->query("
SELECT i.name_en as item_name, i.sku, i.min_level,
COALESCE((SELECT SUM(quantity) FROM inventory_batches b WHERE b.item_id = i.id), 0) as current_stock
FROM inventory_items i
HAVING current_stock <= min_level
ORDER BY current_stock ASC
")->fetchAll(PDO::FETCH_ASSOC);
} elseif ($report_type === 'expiry') {
$title = __('expiry_report');
$columns = ['item_name', 'batch_number', 'expiry_date', 'quantity', 'days_remaining'];
$data = $db->query("
SELECT i.name_en as item_name, b.batch_number, b.expiry_date, b.quantity,
DATEDIFF(b.expiry_date, CURDATE()) as days_remaining
FROM inventory_batches b
JOIN inventory_items i ON b.item_id = i.id
WHERE b.quantity > 0 AND b.expiry_date IS NOT NULL
AND b.expiry_date <= DATE_ADD(CURDATE(), INTERVAL 90 DAY)
ORDER BY b.expiry_date ASC
")->fetchAll(PDO::FETCH_ASSOC);
} elseif ($report_type === 'valuation') {
$title = __('stock_valuation_report');
$columns = ['item_name', 'total_quantity', 'avg_cost', 'total_value'];
$data = $db->query("
SELECT i.name_en as item_name,
SUM(b.quantity) as total_quantity,
AVG(b.cost_price) as avg_cost,
SUM(b.quantity * b.cost_price) as total_value
FROM inventory_batches b
JOIN inventory_items i ON b.item_id = i.id
WHERE b.quantity > 0
GROUP BY i.id
ORDER BY total_value DESC
")->fetchAll(PDO::FETCH_ASSOC);
}
?>
<div class="d-flex justify-content-between align-items-center mb-4">
<h2 class="mb-0 fw-bold text-dark"><?php echo __('inventory_reports'); ?></h2>
<div class="btn-group">
<a href="?report=low_stock" class="btn btn-outline-primary <?php echo $report_type === 'low_stock' ? 'active' : ''; ?>"><?php echo __('low_stock'); ?></a>
<a href="?report=expiry" class="btn btn-outline-primary <?php echo $report_type === 'expiry' ? 'active' : ''; ?>"><?php echo __('expiry_dates'); ?></a>
<a href="?report=valuation" class="btn btn-outline-primary <?php echo $report_type === 'valuation' ? 'active' : ''; ?>"><?php echo __('valuation'); ?></a>
</div>
</div>
<div class="card shadow-sm border-0">
<div class="card-header bg-white">
<h5 class="mb-0 fw-bold"><?php echo htmlspecialchars($title); ?></h5>
</div>
<div class="card-body">
<div class="table-responsive">
<table class="table table-hover align-middle">
<thead class="table-light">
<tr>
<?php foreach ($columns as $col): ?>
<th><?php echo __(str_replace('_', ' ', $col)); ?></th>
<?php endforeach; ?>
</tr>
</thead>
<tbody>
<?php if (empty($data)): ?>
<tr>
<td colspan="<?php echo count($columns); ?>" class="text-center py-4 text-muted"><?php echo __('no_data_available'); ?></td>
</tr>
<?php else: ?>
<?php foreach ($data as $row): ?>
<tr>
<?php foreach ($columns as $col): ?>
<td>
<?php
if ($col === 'current_stock' || $col === 'total_quantity') {
echo number_format($row[$col]);
} elseif ($col === 'total_value' || $col === 'avg_cost') {
echo formatCurrency($row[$col]);
} elseif ($col === 'status') {
if ($row['current_stock'] == 0) echo '<span class="badge bg-danger">Out of Stock</span>';
else echo '<span class="badge bg-warning text-dark">Low Stock</span>';
} elseif ($col === 'days_remaining') {
$days = $row['days_remaining'];
if ($days < 0) echo '<span class="badge bg-danger">Expired</span>';
elseif ($days < 30) echo '<span class="badge bg-warning text-dark">' . $days . ' days</span>';
else echo '<span class="badge bg-info">' . $days . ' days</span>';
} else {
echo htmlspecialchars($row[$col]);
}
?>
</td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
</div>
<div class="mt-3 text-end">
<button onclick="window.print()" class="btn btn-outline-secondary"><i class="bi bi-printer me-2"></i> <?php echo __('print_report'); ?></button>
</div>
</div>
</div>