from .finance_tax_add_vat import VatBreakdown from .finance_tax_vat_rate import vat_rate from .math_round_div import round_div from .money_amount import Money, money 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, )