/** * Break-bulk (PER_ITEM) bulk bookings overload `cargoTotalWeightVgm` with the * ITEM COUNT; their real tonnage lives in `bulkTotalWeightTons`. Container * bookings never store a total at all — `cargoTotalWeightVgm` stays 0 and the * weight lives per line (`quantity × vgmPerUnitTons`). Every other booking * stores tons in `cargoTotalWeightVgm` directly. Rendering the raw VGM column * showed a 20-item / 100T booking as "20 tons" and every container booking as * "0 tons". */ export function cargoTonsAndItems(booking: { freightType?: string | null; cargoTotalWeightVgm?: number | string | null; bulkTotalWeightTons?: number | string | null; bookingContainers?: Array<{ quantity?: number | string | null; vgmPerUnitTons?: number | string | null; }> | null; }): { tons: number; items: number | null } { const bulkTons = Number(booking.bulkTotalWeightTons ?? 0); const vgm = Number(booking.cargoTotalWeightVgm ?? 0); if (booking.freightType === "BULK" && bulkTons > 0) { return { tons: bulkTons, items: vgm > 0 ? vgm : null }; } if (vgm <= 0 && booking.bookingContainers?.length) { const lineTons = booking.bookingContainers.reduce( (sum, c) => sum + Number(c.quantity ?? 0) * Number(c.vgmPerUnitTons ?? 0), 0, ); return { tons: Math.round(lineTons * 1000) / 1000, items: null }; } return { tons: vgm, items: null }; }