import { Money } from "bigint-money/dist" ; import { NumberFormatter } from "../NumberFormatter" ; import { Fee } from "./Fee" ; export class TieredFee implements Fee { constructor ( private tiers ) { } calculateFor ( amount ) { let totalAmount = new Money ( 0 , 'EUR' ) ; let previousTierLimit = new Money ( 0 , 'EUR' ) ; for ( let tier of this . tiers ) { let tierAmount ; if ( tier . tierLimit !== null ) { tierAmount = tier . tierLimit . isLesserThan ( amount ) ? tier . tierLimit : amount ; } else { tierAmount = amount ; } tierAmount = tierAmount . subtract ( previousTierLimit ) ; if ( amount . isLesserThanOrEqual ( previousTierLimit ) ) { break ; } let tierFee = tier . fee . calculateFor ( tierAmount ) ; totalAmount = totalAmount . add ( tierFee . toFixed ( 2 ) ) ; previousTierLimit = tier . tierLimit ; } return totalAmount ; } public describe ( ) { return '' ; } public getExtendedDescription ( ) { const numberFormatter = new NumberFormatter ( ) ; return this . tiers . map ( ( tier ) : string => { if ( tier . tierLimit === null ) { return 'daarboven: ' + tier . fee . describe ( ) ; } return 't/m ' + numberFormatter . formatMoney ( tier . tierLimit , 0 ) + ': ' + tier . fee . describe ( ) ; } ) ; } } export class Tier { constructor ( public tierLimit , public fee ) { } }