from decimal import Decimal, ROUND_HALF_UP def money(value): return Decimal(str(value)).quantize(Decimal("0.01"), rounding=ROUND_HALF_UP) def calculate_costs(gross, vat_rate=23, vat_deduction_percent=50): gross = Decimal(str(gross)); rate = Decimal(str(vat_rate)) deduction = Decimal(str(vat_deduction_percent)) / Decimal("100") net = gross / (Decimal("1") + rate / Decimal("100")); vat = gross - net deductible_vat = vat * deduction return {"gross": money(gross), "net": money(net), "vat": money(vat), "deductible_vat": money(deductible_vat), "final_cost": money(gross - deductible_vat)} def calculate_net_price_costs(net_price, vat_rate=23, vat_deduction_percent=50): """Return per-unit invoice and effective costs for a net fuel price. A 50% VAT deduction means the company bears half of the VAT, not a 50% VAT rate. """ net = Decimal(str(net_price)) rate = Decimal(str(vat_rate)) / Decimal("100") deduction = Decimal(str(vat_deduction_percent)) / Decimal("100") deduction = min(max(deduction, Decimal("0")), Decimal("1")) vat = net * rate invoice_gross = net + vat deductible_vat = vat * deduction effective_cost = invoice_gross - deductible_vat return { "net": net, "vat": vat, "invoice_gross": invoice_gross, "deductible_vat": deductible_vat, "effective_cost": effective_cost, }