Functional Weave
Code in Rust

finance.invoice.line-total@1.0.0

impl/typescript.ts

995 bytes · the TypeScript implementation · view raw

Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.

import { type Money, money } from "./money_amount.ts";  ← from money.amount ^1.0.0 · built alongside by fune
import { applyRate } from "./money_apply_rate.ts";  ← from money.apply-rate ^1.0.0 · built alongside by fune

/**
 * Net total for one invoice line.
 *
 * The discount is rounded once, here, before any tax is applied. Rounding the
 * discount and the tax in the other order produces answers that differ by a
 * penny on roughly one line in five.
 */
export function lineTotal(unitPrice: Money, quantity: number, discountBasisPoints: number = 0): Money {
  if (!Number.isInteger(quantity)) {
    throw new TypeError(`quantity must be an integer, received ${quantity}`);
  }
  if (!Number.isInteger(discountBasisPoints) || discountBasisPoints < 0 || discountBasisPoints > 10000) {
    throw new RangeError(`discountBasisPoints must be between 0 and 10000, received ${discountBasisPoints}`);
  }

  const gross = money(unitPrice.minor * quantity, unitPrice.currency);
  const discount = applyRate(gross, discountBasisPoints, "half-up");
  return money(gross.minor - discount.minor, gross.currency);
}