All files / src/pricing discount.ts

0% Statements 0/9
0% Branches 0/5
0% Functions 0/3
0% Lines 0/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18                                   
import { DiscountGroup } from './types.js';
import { roundHalfEven } from './round.js';
 
export function applyDiscount(
  price: number,
  qty: number,
  group: DiscountGroup | null
): number {
  Iif (!group || group.discounts.length === 0) {
    return roundHalfEven(price, 3);
  }
  const applicable = group.discounts
    .filter((d) => d.lowerLimit <= qty)
    .sort((a, b) => b.lowerLimit - a.lowerLimit);
  const amount = applicable.length ? applicable[0].amount : 0;
  return roundHalfEven(price * (1 - amount / 100), 3);
}