PaymentCalculator
Utility class for precise payment calculations using decimal arithmetic.
Definition
class PaymentCalculator {
constructor();
validateAmount(value: string | number): Decimal;
validateSplitsSum(splits: PaymentSplit[], total: Decimal): void;
validatePercentagesSum(percentages: Record): void;
calculatePercentageSplits(total: Decimal, percentages: Record): PaymentSplit[];
validatePermitRule(effect: string): void;
}
Constructor
Methods
validateAmount
Validates and converts a payment amount to Decimal with proper error handling.
validateAmount(value: string | number): Decimal
Parameters:
value(string | number) - Amount as string or number
Returns: Decimal
Validated Decimal amount
Throws:
- Error if the value is invalid, negative, or NaN
validateSplitsSum
Validates that payment splits sum exactly to the total amount.
validateSplitsSum(splits: PaymentSplit[], total: Decimal): void
Parameters:
splits(PaymentSplit[]) - Array of payment splits to validatetotal(Decimal) - Total amount that splits should sum to
Throws:
- Error if splits don't sum to total within tolerance
validatePercentagesSum
Validates that percentages sum to exactly 100%.
validatePercentagesSum(percentages: Record): void
Parameters:
percentages(Record) - Record of recipient to percentage mappings
Throws:
- Error if percentages don't sum to 100 within tolerance
calculatePercentageSplits
Calculates payment splits from percentage allocations with precise arithmetic.
calculatePercentageSplits(total: Decimal, percentages: Record): PaymentSplit[]
Parameters:
total(Decimal) - Total amount to splitpercentages(Record) - Record mapping recipients to percentage allocations
Returns: PaymentSplit[]
Array of PaymentSplit objects with precise amounts
validatePermitRule
Validates that a rule effect allows payment duties.
validatePermitRule(effect: string): void
Parameters:
effect(string) - The rule effect to validate
Throws:
- Error if the effect is not 'permit'