mirror of
https://github.com/tomru/pfadi-bussle.git
synced 2026-03-03 06:27:11 +01:00
use fixed tarifs and add additional costs
This commit is contained in:
65
helpers/bill.ts
Normal file
65
helpers/bill.ts
Normal file
@@ -0,0 +1,65 @@
|
||||
import { MILAGE_TARIFS } from '../db/enums'
|
||||
import { AdditionalCost } from '../db/bill'
|
||||
|
||||
function roundToCent(amount: number) {
|
||||
return Math.round(amount * 100) / 100
|
||||
}
|
||||
|
||||
export function getMilageCosts(tarif: MILAGE_TARIFS, km: number): number {
|
||||
if (tarif === MILAGE_TARIFS.NOCHARGE) {
|
||||
return 0
|
||||
}
|
||||
|
||||
if (km <= 0) {
|
||||
return 0
|
||||
}
|
||||
|
||||
let rate: number
|
||||
|
||||
if (tarif === MILAGE_TARIFS.EXTERN) {
|
||||
if (km <= 200) {
|
||||
rate = 0.42
|
||||
} else if (km <= 1000) {
|
||||
rate = 0.25
|
||||
} else if (km <= 2000) {
|
||||
rate = 0.2
|
||||
} else {
|
||||
rate = 0.18
|
||||
}
|
||||
}
|
||||
|
||||
if (tarif === MILAGE_TARIFS.INTERN) {
|
||||
if (km <= 200) {
|
||||
rate = 0.37
|
||||
} else if (km <= 1000) {
|
||||
rate = 0.22
|
||||
} else if (km <= 2000) {
|
||||
rate = 0.15
|
||||
} else {
|
||||
rate = 0.13
|
||||
}
|
||||
}
|
||||
|
||||
if (rate === undefined) {
|
||||
throw Error('Unable to determine rate')
|
||||
}
|
||||
|
||||
return roundToCent(km * rate)
|
||||
}
|
||||
|
||||
export function getBillTotal({
|
||||
tarif,
|
||||
milage,
|
||||
additionalCosts,
|
||||
}: {
|
||||
tarif: MILAGE_TARIFS
|
||||
milage?: number
|
||||
additionalCosts: AdditionalCost[]
|
||||
}): number {
|
||||
const milageCosts = getMilageCosts(tarif, milage)
|
||||
const additionalCostsSum = additionalCosts
|
||||
.map(({ value }) => value)
|
||||
.reduce((acc: number, value: number) => acc + value, 0)
|
||||
|
||||
return roundToCent(milageCosts + additionalCostsSum)
|
||||
}
|
||||
Reference in New Issue
Block a user