Files
Mateusz Gruszczyński 72757cad55 fix w kalkulacji last price
2026-07-14 14:22:08 +02:00

59 lines
2.4 KiB
Python

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,
}
def calculate_net_price_comparison(retail_gross_price, settlement_net_price, liters=1, vat_rate=23):
"""Compare retail and settlement prices on a net-to-net basis.
Retail price is supplied gross (pump price), while settlement/Last Price is net.
VAT is excluded from the saving because recoverable tax is not a commercial saving.
"""
retail_gross = Decimal(str(retail_gross_price))
settlement_net = Decimal(str(settlement_net_price))
quantity = Decimal(str(liters))
rate = Decimal(str(vat_rate)) / Decimal("100")
retail_net = retail_gross / (Decimal("1") + rate)
saving_per_liter = max(retail_net - settlement_net, Decimal("0"))
return {
"retail_net_price": retail_net,
"settlement_net_price": settlement_net,
"saving_net_per_liter": saving_per_liter,
"retail_net_total": retail_net * quantity,
"settlement_net_total": settlement_net * quantity,
"saving_net_total": saving_per_liter * quantity,
}