diff --git a/webapp/src/routes/dashboard.js b/webapp/src/routes/dashboard.js index 6566d10..ecbdb05 100644 --- a/webapp/src/routes/dashboard.js +++ b/webapp/src/routes/dashboard.js @@ -7,8 +7,10 @@ const { formatINR, getStatusColor } = require('../lib/india'); // GET / — Dashboard router.get('/', requireAuth, asyncHandler(async (req, res) => { - // Fetch summary stats - const { data: loads } = await supabase.from('loads').select('*'); + // Fetch all loads with shipper info + const { data: loads } = await supabase + .from('loads') + .select('*, shipper:shippers(name)'); const allLoads = loads || []; const totalFreight = allLoads.reduce((s, l) => s + (l.freight_charged || 0), 0); @@ -17,11 +19,15 @@ router.get('/', requireAuth, asyncHandler(async (req, res) => { const totalPendingDriver = allLoads.reduce((s, l) => s + (l.pending_to_driver || 0), 0); const settledCount = allLoads.filter(l => ['settled', 'completed', 'commission received', 'reconciled'].includes(l.status)).length; - // Recent loads (last 10) + // Recent loads (last 10) with shipper name const recentLoads = allLoads .filter(l => l.date) .sort((a, b) => new Date(b.date) - new Date(a.date)) - .slice(0, 10); + .slice(0, 10) + .map(l => ({ + ...l, + shipper_name: l.shipper?.name || l.shipper_id || '—', + })); // Status breakdown const statusCounts = {}; @@ -30,19 +36,20 @@ router.get('/', requireAuth, asyncHandler(async (req, res) => { statusCounts[s] = (statusCounts[s] || 0) + 1; } - // Monthly data (last 6 months) - const monthlyData = {}; + // Monthly data (last 6 months) for trend chart + const monthlyMap = {}; for (const l of allLoads) { if (!l.date) continue; const d = new Date(l.date); const key = `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}`; - if (!monthlyData[key]) monthlyData[key] = { freight: 0, commission: 0, count: 0 }; - monthlyData[key].freight += l.freight_charged || 0; - monthlyData[key].commission += l.commission || 0; - monthlyData[key].count++; + if (!monthlyMap[key]) monthlyMap[key] = { month: key, freight: 0, commission: 0, count: 0 }; + monthlyMap[key].freight += l.freight_charged || 0; + monthlyMap[key].commission += l.commission || 0; + monthlyMap[key].count++; } + const monthlyData = Object.values(monthlyMap).sort((a, b) => a.month.localeCompare(b.month)).slice(-6); - // Recent payments needed + // Pending collections const pendingCollection = allLoads .filter(l => ['pending collection', 'partially pending', 'fully pending from shipper', 'delivered / pending collection'].includes(l.status)) .slice(0, 5); diff --git a/webapp/src/views/pages/dashboard.ejs b/webapp/src/views/pages/dashboard.ejs index 653b6ab..fc299c4 100644 --- a/webapp/src/views/pages/dashboard.ejs +++ b/webapp/src/views/pages/dashboard.ejs @@ -41,6 +41,41 @@ + +