Functional Weave
Code in Rust

finance.tax.remove-vat@1.0.2

impl/typescript.ts

952 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 VatBreakdown } from "./finance_tax_add_vat.ts";  ← from finance.tax.add-vat ^1.0.0 · built alongside by fune
import { vatRate } from "./finance_tax_vat_rate.ts";  ← from finance.tax.vat-rate ^1.0.0 · built alongside by fune
import { roundDiv } from "./math_round_div.ts";  ← from math.round-div ^1.0.0 · built alongside by fune
import { type Money, money } from "./money_amount.ts";  ← from money.amount ^1.0.0 · built alongside by fune

/**
 * Extract the VAT already included in a gross amount.
 *
 * Taking 20% off a gross price is the classic error: the VAT inside a 120.00
 * gross price is 20.00, not 24.00. The net is gross x 10000 / (10000 + rate),
 * and the tax is the remainder, so the parts always add back to exactly the
 * gross with no stray penny.
 */
export function removeVat(gross: Money, jurisdiction: string, category: string, onDate: string): VatBreakdown {
  const rate = vatRate(jurisdiction, category, onDate);
  const net = money(roundDiv(gross.minor * 10000, 10000 + rate.basisPoints, "half-up"), gross.currency);
  return {
    net,
    tax: money(gross.minor - net.minor, gross.currency),
    gross,
    basisPoints: rate.basisPoints,
  };
}