fix w kalkulacji last price

This commit is contained in:
Mateusz Gruszczyński
2026-07-14 13:43:27 +02:00
parent d2f0077e86
commit 6f46508da4
4 changed files with 73 additions and 16 deletions
+22
View File
@@ -12,3 +12,25 @@ def calculate_costs(gross, vat_rate=23, vat_deduction_percent=50):
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,
}
+36 -14
View File
@@ -120,29 +120,51 @@ def dashboard():
totals = {"gross": Decimal("0"), "deductible_vat": Decimal("0"), "final_cost": Decimal("0"), "liters": Decimal("0")}
by_vehicle = defaultdict(lambda: {"gross": 0, "payable": 0, "liters": 0}); invoices = defaultdict(lambda: {"gross": 0, "payable": 0, "count": 0}); settlements = {}
for e in entries:
vat_rate = effective_vat_rate(settings.vat_rate, e.fueled_at)
c = calculate_costs(e.gross, vat_rate, settings.vat_deduction_percent)
totals["gross"] += c["gross"]; totals["deductible_vat"] += c["deductible_vat"]; totals["final_cost"] += c["final_cost"]; totals["liters"] += Decimal(e.liters)
station_policy = e.station_company
entry_settings = e.vehicle.company or settings
vat_rate = effective_vat_rate(entry_settings.vat_rate, e.fueled_at)
vat_deduction_percent = entry_settings.vat_deduction_percent
normal_price = float(e.price_per_liter)
payable_price = normal_price
rule = FuelCardStationRule.query.filter_by(fuel_card_id=e.fuel_card_id, station_company_id=e.station_company_id).first() if e.used_fuel_card and e.fuel_card_id and e.station_company_id else None
uses_station_last_price = bool(rule and rule.use_orlen_last_price and e.wholesale_price is not None)
# Last price is a NET station price. Discounts and surcharges are also net.
if uses_station_last_price:
payable_price = float(e.wholesale_price) * (1 + float(vat_rate) / 100)
settlement_net = Decimal(e.wholesale_price)
else:
settlement_net = Decimal(str(normal_price)) / (Decimal("1") + Decimal(str(vat_rate)) / Decimal("100"))
if rule:
net = payable_price / (1 + float(vat_rate) / 100)
net = net * (1 - float(rule.discount_net_percent or 0) / 100) + float(rule.surcharge_net_per_liter or 0)
payable_price = net * (1 + float(vat_rate) / 100)
payable_price = max(payable_price, 0)
payable_gross = float(e.liters) * payable_price
settlements[e.id] = {"normal_price": normal_price, "payable_price": payable_price, "normal_gross": e.gross, "payable_gross": payable_gross, "uses_last_price": uses_station_last_price, "vat_rate": float(vat_rate)}
by_vehicle[e.vehicle.name]["gross"] += float(c["gross"]); by_vehicle[e.vehicle.name]["payable"] += payable_gross; by_vehicle[e.vehicle.name]["liters"] += float(e.liters)
settlement_net = settlement_net * (Decimal("1") - Decimal(rule.discount_net_percent or 0) / Decimal("100"))
settlement_net += Decimal(rule.surcharge_net_per_liter or 0)
settlement_net = max(settlement_net, Decimal("0"))
price_costs = calculate_net_price_costs(settlement_net, vat_rate, vat_deduction_percent)
liters = Decimal(e.liters)
invoice_gross = price_costs["invoice_gross"] * liters
deductible_vat = price_costs["deductible_vat"] * liters
effective_gross = price_costs["effective_cost"] * liters
totals["gross"] += invoice_gross
totals["deductible_vat"] += deductible_vat
totals["final_cost"] += effective_gross
totals["liters"] += liters
settlements[e.id] = {
"normal_price": normal_price,
"net_price": float(settlement_net),
"invoice_price": float(price_costs["invoice_gross"]),
"payable_price": float(price_costs["effective_cost"]),
"normal_gross": e.gross,
"invoice_gross": float(invoice_gross),
"payable_gross": float(effective_gross),
"uses_last_price": uses_station_last_price,
"vat_rate": float(vat_rate),
"vat_deduction_percent": float(vat_deduction_percent),
}
by_vehicle[e.vehicle.name]["gross"] += float(invoice_gross); by_vehicle[e.vehicle.name]["payable"] += float(effective_gross); by_vehicle[e.vehicle.name]["liters"] += float(liters)
if e.used_fuel_card:
key = invoice_period(e.fueled_at, settings.invoice_split_day) if split_enabled else "Cały miesiąc"
else:
key = "Poza kartą"
invoices[key]["gross"] += float(c["gross"]); invoices[key]["payable"] += payable_gross; invoices[key]["count"] += 1
invoices[key]["gross"] += float(invoice_gross); invoices[key]["payable"] += float(effective_gross); invoices[key]["count"] += 1
return render_template("dashboard.html", vehicles=vehicles, entries=entries, totals=totals, month=month, by_vehicle=dict(by_vehicle), invoices=dict(invoices), settings=settings, split_enabled=split_enabled, settlements=settlements, vat_override=global_vat_override())
@main.route("/vehicles", methods=["GET", "POST"])
+2 -2
View File
@@ -2,6 +2,6 @@
<div class="d-flex justify-content-between align-items-center mb-3"><div><h1 class="h2 mb-0">Podsumowanie kosztów</h1><small class="text-body-secondary">{{ settings.name }} · VAT do odliczenia {{ settings.vat_deduction_percent }}%</small></div><form action="{{url_for('main.dashboard')}}" class="ajax-nav-form"><input class="form-control" type="month" name="month" value="{{month}}"></form></div>
{% if vat_override.enabled %}<div class="alert alert-warning"><strong>{{ vat_override.name }}:</strong> globalny VAT {{ vat_override.rate }}%{% if vat_override.start %} od {{ vat_override.start.strftime('%d.%m.%Y') }}{% endif %}{% if vat_override.end %} do {{ vat_override.end.strftime('%d.%m.%Y') }}{% endif %}. Rozliczenia są liczone według daty tankowania.</div>{% endif %}
<div class="row g-3 mb-4"><div class="col-6 col-lg-3"><div class="card metric"><div class="card-body"><small>Brutto</small><strong>{{ '%.2f'|format(totals.gross) }} zł</strong></div></div></div><div class="col-6 col-lg-3"><div class="card metric"><div class="card-body"><small>VAT odliczony</small><strong>{{ '%.2f'|format(totals.deductible_vat) }} zł</strong></div></div></div><div class="col-6 col-lg-3"><div class="card metric"><div class="card-body"><small>Koszt po VAT</small><strong>{{ '%.2f'|format(totals.final_cost) }} zł</strong></div></div></div><div class="col-6 col-lg-3"><div class="card metric"><div class="card-body"><small>Litry</small><strong>{{ '%.2f'|format(totals.liters) }} l</strong></div></div></div></div>
<div class="row g-4"><div class="col-lg-7"><div class="card"><div class="card-body"><h2 class="h5">Koszt według pojazdu</h2><p class="small text-body-secondary">Cena na dystrybutorze a kwota do zapłaty według cennika karty.</p><div class="chart-box"><canvas id="cost-chart"></canvas><div id="cost-chart-empty" class="chart-empty d-none">Brak danych dla wybranego miesiąca.</div></div></div></div></div><div class="col-lg-5"><div class="card h-100"><div class="card-body"><h2 class="h5">Okresy faktur</h2><small class="text-body-secondary">{% if split_enabled %}Podział według dnia {{settings.invoice_split_day}}{% else %}Podział dzienny wyłączony{% endif %}</small><table class="table mt-2"><thead><tr><th>Okres</th><th>Pozycje</th><th>Detal</th><th>Do zapłaty</th></tr></thead><tbody>{% for key,row in invoices.items() %}<tr><td>{{key}}</td><td>{{row.count}}</td><td>{{'%.2f'|format(row.gross)}} zł</td><td>{{'%.2f'|format(row.payable)}} zł</td></tr>{% else %}<tr><td colspan="4">Brak danych</td></tr>{% endfor %}</tbody></table></div></div></div></div>
<div class="card mt-4"><div class="card-body"><h2 class="h5">Tankowania</h2><div class="table-responsive"><table class="table table-hover align-middle"><thead><tr><th>Data</th><th>Auto</th><th>Stacja</th><th>Paliwo</th><th>Litry</th><th>Cena detal.</th><th>Orlen last</th><th>Do zapłaty</th><th>VAT</th><th>Razem</th></tr></thead><tbody>{% for e in entries %}{% set s=settlements[e.id] %}<tr><td>{{e.fueled_at.strftime('%d.%m.%Y')}}</td><td>{{e.vehicle.name}}</td><td>{{e.station or '—'}}</td><td>{{e.fuel_type}}</td><td>{{e.liters}}</td><td>{{'%.4f'|format(s.normal_price)}} zł/l</td><td>{% if e.wholesale_price %}{{'%.4f'|format(e.wholesale_price|float)}} zł/l{% else %}—{% endif %}</td><td>{% if s.uses_last_price %}<span class="badge text-bg-info">last price</span> {% endif %}{{'%.4f'|format(s.payable_price)}} zł/l</td><td>{{'%.2f'|format(s.vat_rate)}}%</td><td><span class="text-body-secondary text-decoration-line-through">{{'%.2f'|format(s.normal_gross)}} zł</span><br><strong>{{'%.2f'|format(s.payable_gross)}} zł</strong></td></tr>{% else %}<tr><td colspan="10">Brak tankowań w tym miesiącu.</td></tr>{% endfor %}</tbody></table></div></div></div>
<div class="row g-4"><div class="col-lg-7"><div class="card"><div class="card-body"><h2 class="h5">Koszt według pojazdu</h2><p class="small text-body-secondary">Cena fakturowa a rzeczywisty koszt po odliczeniu VAT.</p><div class="chart-box"><canvas id="cost-chart"></canvas><div id="cost-chart-empty" class="chart-empty d-none">Brak danych dla wybranego miesiąca.</div></div></div></div></div><div class="col-lg-5"><div class="card h-100"><div class="card-body"><h2 class="h5">Okresy faktur</h2><small class="text-body-secondary">{% if split_enabled %}Podział według dnia {{settings.invoice_split_day}}{% else %}Podział dzienny wyłączony{% endif %}</small><table class="table mt-2"><thead><tr><th>Okres</th><th>Pozycje</th><th>Detal</th><th>Koszt po VAT</th></tr></thead><tbody>{% for key,row in invoices.items() %}<tr><td>{{key}}</td><td>{{row.count}}</td><td>{{'%.2f'|format(row.gross)}} zł</td><td>{{'%.2f'|format(row.payable)}} zł</td></tr>{% else %}<tr><td colspan="4">Brak danych</td></tr>{% endfor %}</tbody></table></div></div></div></div>
<div class="card mt-4"><div class="card-body"><h2 class="h5">Tankowania</h2><div class="table-responsive"><table class="table table-hover align-middle"><thead><tr><th>Data</th><th>Auto</th><th>Stacja</th><th>Paliwo</th><th>Litry</th><th>Cena detal.</th><th>Last price netto</th><th>Koszt po VAT</th><th>VAT</th><th>Razem</th></tr></thead><tbody>{% for e in entries %}{% set s=settlements[e.id] %}<tr><td>{{e.fueled_at.strftime('%d.%m.%Y')}}</td><td>{{e.vehicle.name}}</td><td>{{e.station or '—'}}</td><td>{{e.fuel_type}}</td><td>{{e.liters}}</td><td>{{'%.4f'|format(s.normal_price)}} zł/l</td><td>{% if s.uses_last_price %}{{'%.4f'|format(s.net_price)}} zł/l{% elif e.wholesale_price %}{{'%.4f'|format(e.wholesale_price|float)}} zł/l{% else %}—{% endif %}</td><td>{% if s.uses_last_price %}<span class="badge text-bg-info">last price</span> {% endif %}<strong>{{'%.4f'|format(s.payable_price)}} zł/l</strong><br><small class="text-body-secondary">faktura {{'%.4f'|format(s.invoice_price)}} zł/l</small></td><td>{{'%.2f'|format(s.vat_rate)}}%<br><small class="text-body-secondary">odliczenie {{'%.0f'|format(s.vat_deduction_percent)}}%</small></td><td><span class="text-body-secondary text-decoration-line-through">{{'%.2f'|format(s.normal_gross)}} zł</span><br><strong>{{'%.2f'|format(s.payable_gross)}} zł</strong><br><small class="text-body-secondary">faktura {{'%.2f'|format(s.invoice_gross)}} zł</small></td></tr>{% else %}<tr><td colspan="10">Brak tankowań w tym miesiącu.</td></tr>{% endfor %}</tbody></table></div></div></div>
{% endblock %}{% block scripts %}<script>FuelTrack.renderBarChart('cost-chart', {{by_vehicle|tojson}});</script>{% endblock %}