Functional Weave
Code in Python

finance.tax.remove-vat@1.0.1

impl/python.py

922 bytes · the Python 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.

from .finance_tax_add_vat import VatBreakdown  ← from finance.tax.add-vat ^1.0.0 · built alongside by fune
from .finance_tax_vat_rate import vat_rate  ← from finance.tax.vat-rate ^1.0.0 · built alongside by fune
from .math_round_div import round_div  ← from math.round-div ^1.0.0 · built alongside by fune
from .money_amount import Money, money  ← from money.amount ^1.0.0 · built alongside by fune


def remove_vat(gross: Money, jurisdiction: str, category: str, on_date: str) -> VatBreakdown:
    """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.
    """
    rate = vat_rate(jurisdiction, category, on_date)
    net = money(round_div(gross.minor * 10000, 10000 + rate.basis_points, "half-up"), gross.currency)
    return VatBreakdown(
        net=net,
        tax=money(gross.minor - net.minor, gross.currency),
        gross=gross,
        basis_points=rate.basis_points,
    )