import type { EtcBillResponse, EtcOverviewResponse, EtcTollRecordResponse } from './types'; export interface EtcRecordReconciliation { matches: boolean; passageCountDifference: number; vehicleCountDifference: number; tollAmountDifference: number; serviceFeeDifference: number; totalAmountDifference: number; } export interface EtcBillReconciliation { matches: boolean; billCountDifference: number; receivableAmountDifference: number; paidAmountDifference: number; } function hundredths(value: number): number { return Math.round(value * 100); } export function reconcileEtcRecords( kpi: EtcOverviewResponse['kpi'], records: EtcTollRecordResponse, ): EtcRecordReconciliation { const tollDifference = hundredths(records.summary.tollAmount) - hundredths(kpi.tollAmount); const serviceFeeDifference = hundredths(records.summary.serviceFee) - hundredths(kpi.serviceFee); const totalDifference = hundredths(records.summary.totalAmount) - hundredths(kpi.totalAmount); const passageCountDifference = records.total - kpi.passageCount; const vehicleCountDifference = records.summary.vehicleCount - kpi.vehicleCount; return { matches: passageCountDifference === 0 && vehicleCountDifference === 0 && tollDifference === 0 && serviceFeeDifference === 0 && totalDifference === 0, passageCountDifference, vehicleCountDifference, tollAmountDifference: tollDifference / 100, serviceFeeDifference: serviceFeeDifference / 100, totalAmountDifference: totalDifference / 100, }; } export function reconcileEtcBills( kpi: EtcOverviewResponse['kpi'], bills: EtcBillResponse, ): EtcBillReconciliation { const receivableDifference = hundredths(bills.summary.receivableAmount) - hundredths(kpi.receivableAmount); const paidDifference = hundredths(bills.summary.paidAmount) - hundredths(kpi.paidAmount); const billCountDifference = bills.total - kpi.billCount; return { matches: billCountDifference === 0 && receivableDifference === 0 && paidDifference === 0, billCountDifference, receivableAmountDifference: receivableDifference / 100, paidAmountDifference: paidDifference / 100, }; }