feat(reports): add a total wagons column to charged vs actual volume

Loaded and empty wagons were both shown but never summed, so the train's
actual consist had to be added up by hand on every row. MAX() on the empty
count for the same reason the distance uses it: the value is constant within
a group that includes ts.id and the leg.
This commit is contained in:
Nathnael
2026-08-24 07:12:40 +00:00
parent a3f06597cc
commit e8f46675dc

View File

@@ -53,6 +53,7 @@ const LEG_TO = 'COALESCE(leg.to_yard_id, ts.destination_station_id)';
*/ */
const LEG_KM = `MAX(${distanceKmBetween(LEG_FROM, LEG_TO)})`; const LEG_KM = `MAX(${distanceKmBetween(LEG_FROM, LEG_TO)})`;
const EMPTY_WAGONS = `MAX(${SCHEDULE_EMPTY_WAGONS})`; const EMPTY_WAGONS = `MAX(${SCHEDULE_EMPTY_WAGONS})`;
const TOTAL_WAGONS = `(COUNT(DISTINCT tsw.id) + ${EMPTY_WAGONS})::int`;
/** /**
* Ton/Km and Vehicle-Km are NULL — not zero — when the yard pair has no * Ton/Km and Vehicle-Km are NULL — not zero — when the yard pair has no
@@ -105,6 +106,7 @@ export const chargedVsActualVolumeReport: ReportDefinition = {
{ key: 'teu', label: 'TEU', type: 'number' }, { key: 'teu', label: 'TEU', type: 'number' },
{ key: 'wagons', label: 'Loaded wagons', type: 'number' }, { key: 'wagons', label: 'Loaded wagons', type: 'number' },
{ key: 'emptyWagons', label: 'Empty wagons', type: 'number' }, { key: 'emptyWagons', label: 'Empty wagons', type: 'number' },
{ key: 'totalWagons', label: 'Total wagons', type: 'number', sortable: true },
{ key: 'distanceKm', label: 'Distance (km)', type: 'number' }, { key: 'distanceKm', label: 'Distance (km)', type: 'number' },
{ key: 'tonKm', label: 'Ton/Km', type: 'number', sortable: true }, { key: 'tonKm', label: 'Ton/Km', type: 'number', sortable: true },
{ key: 'vehicleKm', label: 'Vehicle-Km', type: 'number' }, { key: 'vehicleKm', label: 'Vehicle-Km', type: 'number' },
@@ -121,6 +123,7 @@ export const chargedVsActualVolumeReport: ReportDefinition = {
.addSelect(TEU_EXPR, 'teu') .addSelect(TEU_EXPR, 'teu')
.addSelect(LOADED_WAGONS_EXPR, 'wagons') .addSelect(LOADED_WAGONS_EXPR, 'wagons')
.addSelect(`${EMPTY_WAGONS}::int`, 'emptyWagons') .addSelect(`${EMPTY_WAGONS}::int`, 'emptyWagons')
.addSelect(TOTAL_WAGONS, 'totalWagons')
.addSelect(`${LEG_KM}::float8`, 'distanceKm') .addSelect(`${LEG_KM}::float8`, 'distanceKm')
.addSelect(TON_KM, 'tonKm') .addSelect(TON_KM, 'tonKm')
.addSelect(VEHICLE_KM, 'vehicleKm') .addSelect(VEHICLE_KM, 'vehicleKm')