diff --git a/src/app/dashboard/plant/equipment/page.tsx b/src/app/dashboard/plant/equipment/page.tsx index 361444a..966f6f9 100644 --- a/src/app/dashboard/plant/equipment/page.tsx +++ b/src/app/dashboard/plant/equipment/page.tsx @@ -78,10 +78,13 @@ export default async function PlantPage({ ...allFuelLogs.filter(l=>activeIds.has(l.site)).flatMap(l=>l.expand?.vehicle ? [l.expand.vehicle] : []), ...periods.filter(p=>activeIds.has(p.site ?? "")).flatMap(p=>p.expand?.vehicle ? [p.expand.vehicle] : []), ].filter(isPlant).map(v=>[v.id,v])).values()].sort((a,b)=>a.description.localeCompare(b.description)); + const monthStart = `${today.slice(0, 8)}01`; const aggByVehicle = new Map(allVehicles.map(v=> { - const points = plantUsage(allFuelLogs.filter(l=>l.vehicle===v.id), periods.filter(p=>p.vehicle===v.id), v.cost_per_hour ?? 0, today) - .filter(p=>activeIds.has(p.site) && p.date.slice(0,10) <= today && p.date.slice(0,10) >= rangeStart); - return [v.id, {totalHours:points.reduce((s,p)=>s+p.hours,0),totalValue:points.reduce((s,p)=>s+p.value,0),logCount:points.length,reviewCount:points.filter(p=>p.rejected).length,projectCodes:[...new Set(points.map(p=>siteById[p.site]).filter(Boolean))].join(" · ")}]; + const visible = plantUsage(allFuelLogs.filter(l=>l.vehicle===v.id), periods.filter(p=>p.vehicle===v.id), v.cost_per_hour ?? 0, today) + .filter(p=>activeIds.has(p.site) && p.date.slice(0,10) <= today); + const points = visible.filter(p=>p.date.slice(0,10) >= rangeStart); + const monthHours = visible.filter(p=>p.date.slice(0,10) >= monthStart).reduce((s,p)=>s+p.hours,0); + return [v.id, {monthHours,totalHours:points.reduce((s,p)=>s+p.hours,0),totalValue:points.reduce((s,p)=>s+p.value,0),logCount:points.length,reviewCount:points.filter(p=>p.rejected).length,projectCodes:[...new Set(points.map(p=>siteById[p.site]).filter(Boolean))].join(" · ")}]; })); // All plant vehicles show up — even those without a cost_per_hour @@ -99,6 +102,7 @@ export default async function PlantPage({ historical: !activeIds.has(v.site), canManage: scope.allowed.some(s=>s.id===v.site), totalHours: agg?.totalHours ?? 0, + monthHours: agg?.monthHours ?? 0, totalValue: agg?.totalValue ?? 0, logCount: agg?.logCount ?? 0, overBudgetCount: 0, // no per-period budget field on fuel_logs @@ -106,6 +110,12 @@ export default async function PlantPage({ }; }); + // Default order: rated equipment first, then most hours this month, then name. + rows.sort((a, b) => + Number((b.costPerHour ?? 0) > 0) - Number((a.costPerHour ?? 0) > 0) || + (b.monthHours ?? 0) - (a.monthHours ?? 0) || + a.description.localeCompare(b.description)); + const sites: PlantSiteOption[] = allSites.map((s) => ({ id: s.id, project_code: s.project_code, diff --git a/src/app/dashboard/plant/equipment/plant-view.tsx b/src/app/dashboard/plant/equipment/plant-view.tsx index b697efe..3d661cc 100644 --- a/src/app/dashboard/plant/equipment/plant-view.tsx +++ b/src/app/dashboard/plant/equipment/plant-view.tsx @@ -52,6 +52,8 @@ export interface PlantRow { fuelLimits: string; /** Per-vehicle totals computed from fuel_logs (odo diffs for plant). */ totalHours: number; + /** Hours in the current calendar month; drives the default sort. */ + monthHours?: number; costPerHour?: number; reviewCount?: number; historical?: boolean;