Files
wms-app/app/dashboard/index.php
T
2026-05-12 13:55:39 +07:00

564 lines
24 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 class="d-flex align-items-center gap-3">
<label class="mb-0 text-nowrap fw-semibold">Month</label>
<div class="input-group" style="min-width:160px;">
<span class="input-group-text"><i class="ti ti-calendar"></i></span>
<input type="text" id="month_filter" class="form-control" readonly placeholder="Select month" style="cursor:pointer;">
</div>
</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">Units received this month</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">Units dispatched this month</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">Orders this month</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">Grand total this month</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" id="most_moved_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";?>
<script>
var movementChart = null;
var overallChart = null;
// ── 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, month) {
// 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_active_products').text(format_number(d.active_products));
$('#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, 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', 'Critical']
});
await overallChart.render();
}
$('#overlay_overall').hide();
// Most Moved Products
$('#most_moved_month').text(format_month(month));
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">-${Number(item.total_out).toLocaleString()}</span><br>
<small class="text-success">+${Number(item.total_in).toLocaleString()}</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}${Number(item.qty).toLocaleString()}</span><br>
<small class="text-muted">${time_ago(item.date)}</small>
</div>
</li>`;
});
}
$('#list_recent_activity').html(act_html);
$('#overlay_activity').hide();
}
// ── Fetch + render ────────────────────────────────────────────────────
function retrieve_dashboard() {
var fpDate = monthPicker.selectedDates[0];
var month = fpDate
? fpDate.getFullYear() + '-' + String(fpDate.getMonth() + 1).padStart(2, '0')
: '<?php echo date('Y-m'); ?>';
show_overlays();
return ajax_request({
url: "<?php echo $server_url?>dashboard/api/engine_report/reports_stats.php",
autoPrepare: true,
checkRequired: 0,
action: 'read',
data: { "month": month },
onSuccess: async function(res) {
await render(res.output, month);
}
});
}
// ── Helpers ───────────────────────────────────────────────────────────
function format_month(ym) {
if (!ym) return '';
var parts = ym.split('-');
var months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
return months[parseInt(parts[1]) - 1] + ' ' + parts[0];
}
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 ─────────────────────────────────────────────────────────────
var monthPicker = flatpickr('#month_filter', {
plugins: [
new monthSelectPlugin({
shorthand: true,
dateFormat: 'M Y',
altFormat: 'M Y',
theme: 'light'
})
],
disableMobile: true,
onChange: function() {
retrieve_dashboard();
}
});
monthPicker.setDate('<?php echo date('M Y'); ?>', false); // false = don't trigger onChange
$(async function() {
try {
show_overlays();
await retrieve_dashboard();
} catch(e) {
console.log(e);
}
});
</script>
</body>
</html>