Files
wms-app/app/dashboard/index.php
T
Thanakorn c89b28da4c Fix QA review findings: server-side validation, notes encoding, dashboard totals
Validate document lines on the server and recompute their totals, store notes with quotes/markup/emoji (utf8mb4, idempotent escaping, decode in form fields), exclude transfers from company-wide stock in/out, count revenue from confirmed orders only, one low-stock rule everywhere, list unapproved lots, natural bin sort, stable order/PO sort, status tiles that add up.
2026-09-19 10:58:42 +07:00

718 lines
32 KiB
PHP

<?php
session_start();
require '../config.php';
require '../include_header.php';
?>
<body>
<?php require '../include_topbar.php';?>
<?php require '../include_sidebar.php';?>
<!-- MAIN CONTENT -->
<main id="content" class="content py-15">
<div class="container-fluid">
<!-- Page header -->
<div class="row">
<div class="col-12">
<div class="d-flex flex-column flex-md-row justify-content-between align-items-md-center mb-6 gap-3">
<div>
<h1 class="fs-3 mb-1">Dashboard</h1>
<p class="mb-0">Inventory overview across all warehouses</p>
</div>
</div>
</div>
</div>
<!-- Stat cards -->
<div class="row g-5 mb-5">
<div class="col-lg-3 col-sm-6 col-12">
<div class="card p-4 bg-warning bg-opacity-10 border border-warning border-opacity-25 rounded-2" style="position:relative;">
<div class="d-flex gap-3">
<div class="icon-shape icon-md bg-warning text-white rounded-2">
<i class="ti ti-trending-up fs-4"></i>
</div>
<div>
<h2 class="mb-3 fs-6">Total Stock In</h2>
<h3 class="fw-bold mb-0" id="stat_total_in">—</h3>
<p class="text-warning mb-0 small">Total units received</p>
</div>
</div>
<div class="card-loader-overlay" id="overlay_in">
<div class="spinner-border spinner-border-sm text-warning"></div>
</div>
</div>
</div>
<div class="col-lg-3 col-sm-6 col-12">
<div class="card p-4 bg-danger bg-opacity-10 border border-danger border-opacity-25 rounded-2" style="position:relative;">
<div class="d-flex gap-3">
<div class="icon-shape icon-md bg-danger text-white rounded-2">
<i class="ti ti-trending-down fs-4"></i>
</div>
<div>
<h2 class="mb-3 fs-6">Total Stock Out</h2>
<h3 class="fw-bold mb-0" id="stat_total_out">—</h3>
<p class="text-danger mb-0 small">Total units dispatched</p>
</div>
</div>
<div class="card-loader-overlay" id="overlay_out">
<div class="spinner-border spinner-border-sm text-primary"></div>
</div>
</div>
</div>
<div class="col-lg-3 col-sm-6 col-12">
<div class="card p-4 bg-success bg-opacity-10 border border-success border-opacity-25 rounded-2" style="position:relative;">
<div class="d-flex gap-3">
<div class="icon-shape icon-md bg-success text-white rounded-2">
<i class="ti ti-shopping-cart fs-4"></i>
</div>
<div>
<h2 class="mb-3 fs-6">Total Orders</h2>
<h3 class="fw-bold mb-0" id="stat_total_orders">—</h3>
<p class="text-success mb-0 small">All orders to date</p>
</div>
</div>
<div class="card-loader-overlay" id="overlay_orders">
<div class="spinner-border spinner-border-sm text-primary"></div>
</div>
</div>
</div>
<div class="col-lg-3 col-sm-6 col-12">
<div class="card p-4 bg-info bg-opacity-10 border border-info border-opacity-25 rounded-2" style="position:relative;">
<div class="d-flex gap-3">
<div class="icon-shape icon-md bg-info text-white rounded-2">
<i class="ti ti-currency-baht fs-4"></i>
</div>
<div>
<h2 class="mb-3 fs-6">Total Revenue</h2>
<h3 class="fw-bold mb-0" id="stat_total_revenue">—</h3>
<p class="text-info mb-0 small">Subtotal, excl. VAT</p>
</div>
</div>
<div class="card-loader-overlay" id="overlay_revenue">
<div class="spinner-border spinner-border-sm text-primary"></div>
</div>
</div>
</div>
</div>
<!-- Secondary stat cards -->
<div class="row g-5 mb-5">
<div class="col-lg-4 col-sm-6 col-12">
<div class="card" style="position:relative;">
<div class="card-body p-4">
<div class="d-flex justify-content-between border-bottom pb-3 mb-3">
<div>
<h3 class="fw-bold h4 mb-1" id="stat_low_stock_count">—</h3>
<span class="text-muted small">Low Stock Alert</span>
</div>
<i class="ti ti-alert-triangle fs-1 text-warning"></i>
</div>
<div class="small">
<a href="<?php echo $server_url?>dashboard/low_stock_products.php" class="link-primary text-decoration-underline">View All</a>
</div>
</div>
<div class="card-loader-overlay" id="overlay_ls"><div class="spinner-border spinner-border-sm text-primary"></div></div>
</div>
</div>
<div class="col-lg-4 col-sm-6 col-12">
<div class="card" style="position:relative;">
<div class="card-body p-4">
<div class="d-flex justify-content-between border-bottom pb-3 mb-3">
<div>
<h3 class="fw-bold h4 mb-1" id="stat_unpaid_invoices">—</h3>
<span class="text-muted small">Unpaid Invoices</span>
</div>
<i class="ti ti-file-invoice fs-1 text-danger"></i>
</div>
<div class="small">
<a href="<?php echo $server_url?>order/invoice.php" class="link-primary text-decoration-underline">View All</a>
</div>
</div>
<div class="card-loader-overlay" id="overlay_ui"><div class="spinner-border spinner-border-sm text-primary"></div></div>
</div>
</div>
<div class="col-lg-4 col-sm-6 col-12">
<div class="card" style="position:relative;">
<div class="card-body p-4">
<div class="d-flex justify-content-between border-bottom pb-3 mb-3">
<div>
<h3 class="fw-bold h4 mb-1" id="stat_pending_returns">—</h3>
<span class="text-muted small">Pending Returns</span>
</div>
<i class="ti ti-arrows-exchange fs-1 text-info"></i>
</div>
<div class="small">
<a href="<?php echo $server_url?>order/return.php" class="link-primary text-decoration-underline">View All</a>
</div>
</div>
<div class="card-loader-overlay" id="overlay_pr"><div class="spinner-border spinner-border-sm text-primary"></div></div>
</div>
</div>
</div>
<!-- Stock Movement Chart + Overall Information -->
<div class="row g-5 mb-5">
<div class="col-12 col-lg-6">
<div class="card" style="position:relative;">
<div class="card-body p-5">
<h3 class="h5 mb-0">Stock Movement</h3>
<p class="mb-4 small text-secondary">Last 12 months across all warehouses</p>
<div id="ics_dashboardMovementChart"></div>
</div>
<div class="card-loader-overlay" id="overlay_chart">
<div class="spinner-border spinner-border-sm text-primary"></div>
</div>
</div>
</div>
<div class="col-12 col-lg-6">
<div class="card" style="position:relative;">
<div class="card-header d-flex justify-content-between align-items-center bg-transparent px-4 py-3">
<h3 class="h5 mb-0">Overall Information</h3>
</div>
<div class="card-body p-6">
<h3 class="h6">Stock Health Overview</h3>
<div class="row align-items-center">
<div class="col-sm-6">
<div id="ics_overallChart"></div>
</div>
<div class="col-sm-6">
<div class="row">
<div class="col-6 border-end">
<div class="text-center">
<h2 class="mb-1" id="stat_health_healthy">—</h2>
<p class="text-success mb-2">Healthy</p>
<span class="badge bg-success d-inline-flex align-items-center"><i class="ti ti-arrow-up-left me-1"></i><span id="stat_health_healthy_pct">—</span></span>
</div>
</div>
<div class="col-6">
<div class="text-center">
<h2 class="mb-1" id="stat_health_critical">—</h2>
<p class="text-warning mb-2">Warning</p>
<span class="badge bg-success badge-xs d-inline-flex align-items-center"><i class="ti ti-arrow-up-left me-1"></i><span id="stat_health_critical_pct">—</span></span>
</div>
</div>
</div>
</div>
</div>
<div class="row text-center border-top mt-4 pt-4">
<div class="col-4 border-end">
<h3 class="fw-bold mb-2" id="stat_total_products">—</h3>
<small class="text-secondary">Products</small>
</div>
<div class="col-4 border-end">
<h3 class="fw-bold mb-2" id="stat_total_contacts">—</h3>
<small class="text-secondary">Contacts</small>
</div>
<div class="col-4">
<h3 class="fw-bold mb-2" id="stat_total_wh">—</h3>
<small class="text-secondary">Warehouses</small>
</div>
</div>
</div>
<div class="card-loader-overlay" id="overlay_overall">
<div class="spinner-border spinner-border-sm text-primary"></div>
</div>
</div>
</div>
</div>
<!-- Bottom 3 cards -->
<div class="row g-5">
<!-- Most Moved Products -->
<div class="col-lg-4 col-12">
<div class="card h-100" style="position:relative;">
<div class="card-header bg-white d-flex justify-content-between align-items-center px-4 py-3">
<h4 class="mb-0 h5">Most Moved Products</h4>
<span class="small text-muted">This Month</span>
</div>
<ul class="list-group list-group-flush" id="list_most_moved">
<li class="list-group-item text-center text-muted py-5">Loading...</li>
</ul>
<div class="card-loader-overlay" id="overlay_moved">
<div class="spinner-border spinner-border-sm text-primary"></div>
</div>
</div>
</div>
<!-- Low Stock Products -->
<div class="col-lg-4 col-12">
<div class="card h-100" style="position:relative;">
<div class="card-header bg-white d-flex justify-content-between align-items-center px-4 py-3">
<h4 class="mb-0 h5">Low Stock Products</h4>
<a href="<?php echo $server_url?>dashboard/low-stock-products.php" class="small text-primary text-decoration-underline">View All</a>
</div>
<ul class="list-group list-group-flush" id="list_low_stock">
<li class="list-group-item text-center text-muted py-5">Loading...</li>
</ul>
<div class="card-loader-overlay" id="overlay_low_list">
<div class="spinner-border spinner-border-sm text-primary"></div>
</div>
</div>
</div>
<!-- Recent Activity -->
<div class="col-lg-4 col-12">
<div class="card h-100" style="position:relative;">
<div class="card-header bg-white d-flex justify-content-between align-items-center px-4 py-3">
<h4 class="mb-0 h5">Recent Activity</h4>
<span class="small text-muted">All warehouses</span>
</div>
<ul class="list-group list-group-flush" id="list_recent_activity">
<li class="list-group-item text-center text-muted py-5">Loading...</li>
</ul>
<div class="card-loader-overlay" id="overlay_activity">
<div class="spinner-border spinner-border-sm text-primary"></div>
</div>
</div>
</div>
</div>
</div>
</main>
<?php require "../include_ending.php";?>
<style>
@keyframes card-flash {
0% { box-shadow: 0 0 0 3px rgba(250,204,21,0.8); }
100% { box-shadow: none; }
}
.card-flash { animation: card-flash 1s ease-out; }
</style>
<script>
var movementChart = null;
var overallChart = null;
function flash_card(el) {
if (!el) return;
el.classList.remove('card-flash');
void el.offsetWidth;
el.classList.add('card-flash');
setTimeout(function() { el.classList.remove('card-flash'); }, 1000);
}
// ── Show all overlays ─────────────────────────────────────────────────
function show_overlays() {
$('#overlay_in, #overlay_out, #overlay_orders, #overlay_revenue, #overlay_chart, #overlay_moved, #overlay_low_list, #overlay_activity, #overlay_ls, #overlay_ui, #overlay_pr, #overlay_overall').show();
}
// ── Render all sections ───────────────────────────────────────────────
async function render(d) {
// Stats — top row
$('#stat_total_in').text(format_number(d.total_in, 2));
$('#stat_total_out').text(format_number(d.total_out, 2));
$('#stat_total_orders').text(format_number(d.total_orders));
$('#stat_total_revenue').text(format_number(d.total_revenue, 2));
$('#overlay_in, #overlay_out, #overlay_orders, #overlay_revenue').hide();
// Stats — secondary row
$('#stat_low_stock_count').text(format_number(d.low_stock));
$('#stat_unpaid_invoices').text(format_number(d.unpaid_invoices));
$('#stat_pending_returns').text(format_number(d.pending_returns));
$('#overlay_ls, #overlay_ui, #overlay_pr').hide();
// Chart
var mv = d.stock_movement;
if (movementChart) {
await movementChart.updateOptions({ xaxis: { categories: mv.labels || [] } }, false, false);
await movementChart.updateSeries([
{ name: 'Stock In', data: mv.stock_in || [] },
{ name: 'Stock Out', data: mv.stock_out || [] }
]);
} else {
$('#ics_dashboardMovementChart').empty();
movementChart = new ApexCharts(document.querySelector('#ics_dashboardMovementChart'), {
series: [
{ name: 'Stock In', data: mv.stock_in || [] },
{ name: 'Stock Out', data: mv.stock_out || [] }
],
chart: { type: 'bar', height: 300, toolbar: { show: false } },
plotOptions: { bar: { horizontal: false, columnWidth: '55%', borderRadius: 5, borderRadiusApplication: 'end' } },
colors: ['#00C951', '#E66239'],
dataLabels: { enabled: false },
stroke: { show: true, width: 2, colors: ['transparent'] },
xaxis: { categories: mv.labels || [] },
yaxis: { title: { text: 'Quantity' } },
fill: { opacity: 1 },
tooltip: { y: { formatter: function(a) { return Number(a).toLocaleString() + ' units'; } } }
});
await movementChart.render();
}
$('#overlay_chart').hide();
// Overall Information
var sh = d.stock_health || { healthy: 0, warning: 0, critical: 0, total: 0 };
var sh_count = sh.healthy + sh.warning + sh.critical;
var healthy_pct = sh_count > 0 ? Math.round(sh.healthy / sh_count * 100) : 0;
var warning_pct = sh_count > 0 ? Math.round(sh.warning / sh_count * 100) : 0;
var critical_pct = sh_count > 0 ? Math.round(sh.critical / sh_count * 100) : 0;
$('#stat_health_healthy').text(format_number(sh.healthy));
$('#stat_health_critical').text(format_number(sh.warning + sh.critical));
$('#stat_health_healthy_pct').text(healthy_pct + '%');
$('#stat_health_critical_pct').text((warning_pct + critical_pct) + '%');
$('#stat_total_products').text(format_number(d.total_products || 0));
$('#stat_total_contacts').text(format_number(d.total_contacts || 0));
$('#stat_total_wh').text(format_number(d.total_warehouses || 0));
var sh_series = [healthy_pct, warning_pct + critical_pct];
if (overallChart) {
await overallChart.updateSeries(sh_series);
} else {
$('#ics_overallChart').empty();
overallChart = new ApexCharts(document.querySelector('#ics_overallChart'), {
series: sh_series,
chart: { height: 200, type: 'radialBar', toolbar: { show: false } },
colors: ['#00C951', '#E66239'],
plotOptions: {
radialBar: {
dataLabels: {
name: { fontSize: '13px', offsetY: -5 },
value: { fontSize: '16px', offsetY: 5 },
total: {
show: true,
label: 'Healthy',
color: '#00C951',
formatter: function(w) {
return w.globals.series[0] + '%';
}
}
},
hollow: {
margin: 3,
size: '50%',
background: 'transparent',
},
track: {
show: true,
background: '#f0f0f0',
strokeWidth: '45%',
opacity: 1,
margin: 5,
}
}
},
fill: {
type: 'gradient',
gradient: {
shade: 'dark',
type: 'vertical',
gradientToColors: ['#007867', '#FFAC82'],
stops: [0, 100]
}
},
stroke: { lineCap: 'round' },
labels: ['Healthy', 'Warning']
});
await overallChart.render();
}
$('#overlay_overall').hide();
// Most Moved Products
var moved_html = '';
if (!d.most_moved || d.most_moved.length === 0) {
moved_html = '<li class="list-group-item text-center text-muted py-4">No data for this month.</li>';
} else {
$.each(d.most_moved.slice(0, 5), function(i, item) {
moved_html += `<li class="list-group-item d-flex align-items-center gap-3 py-3">
<div class="icon-shape icon-sm bg-danger bg-opacity-10 text-danger rounded-2 flex-shrink-0">
<i class="ti ti-trending-down"></i>
</div>
<div class="flex-grow-1">
<p class="mb-0 fw-semibold">${item.product_name || item.product_sku}</p>
<small class="text-muted">${item.product_sku}</small>
</div>
<div class="text-end">
<span class="fw-bold text-danger">Out ${format_quantity(item.total_out)}</span><br>
<small class="text-success">In ${format_quantity(item.total_in)}</small>
</div>
</li>`;
});
}
$('#list_most_moved').html(moved_html);
$('#overlay_moved').hide();
// Low Stock Products
var low_html = '';
if (!d.low_stock_items || d.low_stock_items.length === 0) {
low_html = '<li class="list-group-item text-center text-muted py-4">No low stock items.</li>';
} else {
$.each(d.low_stock_items.slice(0, 5), function(i, item) {
var status_cls = item.status === 'critical' ? 'danger' : 'warning';
low_html += `<li class="list-group-item d-flex align-items-center gap-3 py-3">
<div class="icon-shape icon-sm bg-${status_cls} bg-opacity-10 text-${status_cls} rounded-2 flex-shrink-0">
<i class="ti ti-alert-triangle"></i>
</div>
<div class="flex-grow-1">
<p class="mb-0 fw-semibold">${item.product_name || item.product_sku}</p>
<small class="text-muted">${item.product_sku}</small>
</div>
<div class="text-end">
<span class="fw-bold text-${status_cls}">${Number(item.balance).toLocaleString()}</span><br>
<small class="text-muted">min: ${Number(item.min_stock).toLocaleString()}</small>
</div>
</li>`;
});
}
$('#list_low_stock').html(low_html);
$('#overlay_low_list').hide();
// Recent Activity
var act_html = '';
if (!d.recent_activity || d.recent_activity.length === 0) {
act_html = '<li class="list-group-item text-center text-muted py-4">No recent activity.</li>';
} else {
$.each(d.recent_activity.slice(0, 5), function(i, item) {
var is_in = item.direction === 'in';
var color = is_in ? 'success' : 'danger';
var icon = is_in ? 'ti-trending-up' : 'ti-trending-down';
var sign = is_in ? '+' : '-';
act_html += `<li class="list-group-item d-flex align-items-center gap-3 py-3">
<div class="icon-shape icon-sm bg-${color} bg-opacity-10 text-${color} rounded-2 flex-shrink-0">
<i class="ti ${icon}"></i>
</div>
<div class="flex-grow-1">
<p class="mb-0 fw-semibold">${item.product_name}</p>
<small class="text-muted">${item.warehouse_name}</small>
</div>
<div class="text-end">
<span class="fw-bold text-${color}">${sign}${format_quantity(item.qty)}</span><br>
<small class="text-muted">${time_ago(item.date)}</small>
</div>
</li>`;
});
}
$('#list_recent_activity').html(act_html);
$('#overlay_activity').hide();
}
// ── Fetch + render (full) ─────────────────────────────────────────────
function retrieve_dashboard() {
show_overlays();
return ajax_request({
url: "<?php echo $server_url?>dashboard/api/engine_report/reports_stats.php",
autoPrepare: true,
checkRequired: 0,
action: 'read',
data: {},
onSuccess: async function(res) {
await render(res.output);
}
});
}
// ── Fetch + render (stock sections only) ─────────────────────────────
// Called by socket stock_updated — skips orders/revenue/invoices/returns.
function retrieve_stock_sections() {
$('#overlay_in, #overlay_out, #overlay_ls, #overlay_chart, #overlay_moved, #overlay_low_list, #overlay_activity, #overlay_overall').show();
return ajax_request({
url: "<?php echo $server_url?>dashboard/api/engine_report/reports_stats.php",
autoPrepare: true,
checkRequired: 0,
noLoading: true,
action: 'read',
data: {},
onSuccess: async function(res) {
var d = res.output;
// Stat cards — stock only
$('#stat_total_in').text(format_number(d.total_in, 2));
$('#stat_total_out').text(format_number(d.total_out, 2));
$('#stat_low_stock_count').text(format_number(d.low_stock));
$('#overlay_in, #overlay_out, #overlay_ls').hide();
flash_card(document.getElementById('overlay_in').closest('.card'));
flash_card(document.getElementById('overlay_out').closest('.card'));
flash_card(document.getElementById('overlay_ls').closest('.card'));
// Stock Movement chart
var mv = d.stock_movement;
if (movementChart) {
await movementChart.updateOptions({ xaxis: { categories: mv.labels || [] } }, false, false);
await movementChart.updateSeries([
{ name: 'Stock In', data: mv.stock_in || [] },
{ name: 'Stock Out', data: mv.stock_out || [] }
]);
}
$('#overlay_chart').hide();
flash_card(document.getElementById('overlay_chart').closest('.card'));
// Stock Health
var sh = d.stock_health || { healthy: 0, warning: 0, critical: 0, total: 0 };
var sh_count = sh.healthy + sh.warning + sh.critical;
var healthy_pct = sh_count > 0 ? Math.round(sh.healthy / sh_count * 100) : 0;
var warning_pct = sh_count > 0 ? Math.round(sh.warning / sh_count * 100) : 0;
var critical_pct = sh_count > 0 ? Math.round(sh.critical / sh_count * 100) : 0;
$('#stat_health_healthy').text(format_number(sh.healthy));
$('#stat_health_critical').text(format_number(sh.warning + sh.critical));
$('#stat_health_healthy_pct').text(healthy_pct + '%');
$('#stat_health_critical_pct').text((warning_pct + critical_pct) + '%');
if (overallChart) await overallChart.updateSeries([healthy_pct, warning_pct + critical_pct]);
$('#overlay_overall').hide();
flash_card(document.getElementById('overlay_overall').closest('.card'));
// Most Moved
var moved_html = '';
if (!d.most_moved || d.most_moved.length === 0) {
moved_html = '<li class="list-group-item text-center text-muted py-4">No data for this month.</li>';
} else {
$.each(d.most_moved.slice(0, 5), function(i, item) {
moved_html += `<li class="list-group-item d-flex align-items-center gap-3 py-3">
<div class="icon-shape icon-sm bg-danger bg-opacity-10 text-danger rounded-2 flex-shrink-0"><i class="ti ti-trending-down"></i></div>
<div class="flex-grow-1"><p class="mb-0 fw-semibold">${item.product_name || item.product_sku}</p><small class="text-muted">${item.product_sku}</small></div>
<div class="text-end"><span class="fw-bold text-danger">Out ${format_quantity(item.total_out)}</span><br><small class="text-success">In ${format_quantity(item.total_in)}</small></div>
</li>`;
});
}
$('#list_most_moved').html(moved_html);
$('#overlay_moved').hide();
flash_card(document.getElementById('overlay_moved').closest('.card'));
// Low Stock list
var low_html = '';
if (!d.low_stock_items || d.low_stock_items.length === 0) {
low_html = '<li class="list-group-item text-center text-muted py-4">No low stock items.</li>';
} else {
$.each(d.low_stock_items.slice(0, 5), function(i, item) {
var sc = item.status === 'critical' ? 'danger' : 'warning';
low_html += `<li class="list-group-item d-flex align-items-center gap-3 py-3">
<div class="icon-shape icon-sm bg-${sc} bg-opacity-10 text-${sc} rounded-2 flex-shrink-0"><i class="ti ti-alert-triangle"></i></div>
<div class="flex-grow-1"><p class="mb-0 fw-semibold">${item.product_name || item.product_sku}</p><small class="text-muted">${item.product_sku}</small></div>
<div class="text-end"><span class="fw-bold text-${sc}">${Number(item.balance).toLocaleString()}</span><br><small class="text-muted">min: ${Number(item.min_stock).toLocaleString()}</small></div>
</li>`;
});
}
$('#list_low_stock').html(low_html);
$('#overlay_low_list').hide();
flash_card(document.getElementById('overlay_low_list').closest('.card'));
// Recent Activity
var act_html = '';
if (!d.recent_activity || d.recent_activity.length === 0) {
act_html = '<li class="list-group-item text-center text-muted py-4">No recent activity.</li>';
} else {
$.each(d.recent_activity.slice(0, 5), function(i, item) {
var is_in = item.direction === 'in';
var color = is_in ? 'success' : 'danger';
var icon = is_in ? 'ti-trending-up' : 'ti-trending-down';
var sign = is_in ? '+' : '-';
act_html += `<li class="list-group-item d-flex align-items-center gap-3 py-3">
<div class="icon-shape icon-sm bg-${color} bg-opacity-10 text-${color} rounded-2 flex-shrink-0"><i class="ti ${icon}"></i></div>
<div class="flex-grow-1"><p class="mb-0 fw-semibold">${item.product_name}</p><small class="text-muted">${item.warehouse_name}</small></div>
<div class="text-end"><span class="fw-bold text-${color}">${sign}${format_quantity(item.qty)}</span><br><small class="text-muted">${time_ago(item.date)}</small></div>
</li>`;
});
}
$('#list_recent_activity').html(act_html);
$('#overlay_activity').hide();
flash_card(document.getElementById('overlay_activity').closest('.card'));
}
});
}
// ── Fetch + render (order sections only) ─────────────────────────────
function retrieve_order_sections() {
$('#overlay_orders').show();
return ajax_request({
url: "<?php echo $server_url?>dashboard/api/engine_report/reports_stats.php",
autoPrepare: true,
checkRequired: 0,
noLoading: true,
action: 'read',
data: {},
onSuccess: function(res) {
var d = res.output;
$('#stat_total_orders').text(format_number(d.total_orders));
$('#overlay_orders').hide();
flash_card(document.getElementById('overlay_orders').closest('.card'));
}
});
}
// ── Fetch + render (invoice sections only) ────────────────────────────
function retrieve_invoice_sections() {
$('#overlay_revenue, #overlay_ui').show();
return ajax_request({
url: "<?php echo $server_url?>dashboard/api/engine_report/reports_stats.php",
autoPrepare: true,
checkRequired: 0,
noLoading: true,
action: 'read',
data: {},
onSuccess: function(res) {
var d = res.output;
$('#stat_total_revenue').text(format_number(d.total_revenue, 2));
$('#stat_unpaid_invoices').text(format_number(d.unpaid_invoices));
$('#overlay_revenue, #overlay_ui').hide();
flash_card(document.getElementById('overlay_revenue').closest('.card'));
flash_card(document.getElementById('overlay_ui').closest('.card'));
}
});
}
// ── Fetch + render (return sections only) ─────────────────────────────
function retrieve_return_sections() {
$('#overlay_pr').show();
return ajax_request({
url: "<?php echo $server_url?>dashboard/api/engine_report/reports_stats.php",
autoPrepare: true,
checkRequired: 0,
noLoading: true,
action: 'read',
data: {},
onSuccess: function(res) {
var d = res.output;
$('#stat_pending_returns').text(format_number(d.pending_returns));
$('#overlay_pr').hide();
flash_card(document.getElementById('overlay_pr').closest('.card'));
}
});
}
// ── Helpers ───────────────────────────────────────────────────────────
function time_ago(dateStr) {
if (!dateStr) return '';
var diff = Math.floor((new Date() - new Date(dateStr.replace(/-/g,'/'))) / 1000);
if (diff < 60) return 'Just now';
if (diff < 3600) return Math.floor(diff / 60) + ' min ago';
if (diff < 86400) return Math.floor(diff / 3600) + ' hr ago';
if (diff < 2592000) return Math.floor(diff / 86400) + ' day ago';
return Math.floor(diff / 2592000) + ' mo ago';
}
// ── Boot ─────────────────────────────────────────────────────────────
$(async function() {
try {
show_overlays();
await retrieve_dashboard();
} catch(e) {
console.log(e);
}
});
if (window._socket) {
window._socket.on('stock_updated', function() { retrieve_stock_sections(); });
window._socket.on('order_updated', function() { retrieve_order_sections(); });
window._socket.on('invoice_updated', function() { retrieve_invoice_sections(); });
window._socket.on('return_updated', function() { retrieve_return_sections(); });
}
</script>
</body>
</html>