1. Cause
Bug A: gross totals off by 0.01–0.03. line_gross rounds each line's gross to 2 decimals. invoice_total then adds up those already-rounded values. Symfonia does it differently: it sums the net amounts per VAT rate and applies VAT once. Each line can be off by up to ±0.005, and these errors add up across lines. One line can never differ, and 1–3 lines rarely do. At 8+ lines the total regularly drifts by 1–3 grosze. Using float instead of Decimal adds occasional wrong rounding at .xx5 boundaries on top of this.
Confidence: High for the mechanism. To raise it, recompute FV/2026/10/0412 as round(sum(net) * 1.23, 2) and check that you get 4 812.45.
Bug B: invoices from the 1st land in the previous month. In report_month, .astimezone(timezone.utc) converts the Warsaw time to UTC before taking the month. An invoice issued between 00:00 and 01:59 CEST (00:00–00:59 CET) on the 1st is still the previous month in UTC. "2026-10-01T00:14:52+02:00" becomes 2026-09-30T22:14:52Z, which gives "2026-09".
Confidence: High. To raise it, check that every misfiled invoice has a local time below 02:00 (CEST) or 01:00 (CET).
What neither bug explains: why they started after the move. Neither cause depends on the machine. The string carries its own offset, so the server's timezone (which you checked correctly) has no effect on report_month. Rounding is the same on both machines. Assumption: the script, the DB driver or query, or the billing schedule changed around the time of the move. To confirm this, please send the old script from the Windows PC and the code that loads issued_at and net from the database.
2. How it produces each symptom
- Off by 0.01–0.03, never more: each line's rounding error is at most 0.005, and the errors partly cancel. With 8–15 lines the net drift usually stays within ±0.03. For 0412 the 11 line errors happen to add up to +0.02.
- Only many-line invoices; 1–3 lines match: one line is mathematically identical to Symfonia's method. Two or three lines rarely add up to a full grosz, especially with typical round net prices.
- Only 23% VAT invoices mentioned: this is probably just because that is your main rate. The same error happens at any rate. Invoices mixing several rates would differ too, because Symfonia groups by rate.
- Only invoices from the 1st: only on the 1st can moving back 1–2 hours cross a month boundary.
- Only automatic subscription invoices, not the office's: billing runs just after midnight, inside the 00:00–02:00 window. Office staff create invoices during the day, so moving back 2 hours stays on the 1st.
- "Only some of them": an invoice is affected only if it was created before 02:00 CEST or 01:00 CET. Invoices created later in the billing run are fine.
3. Other possible causes
| Cause | Fits | Does not fit | How to check |
|---|
The old script was different, e.g. it used Decimal/per-rate sums and local time, and the VPS copy is a rewrite | Explains "started after the move" for both bugs | Nothing contradicts it | Diff the old Windows script against export_invoices.py |
The DB driver or query on the VPS returns a different net type or format (e.g. float instead of Decimal/numeric text) | Could add float errors at .xx5 boundaries after the move | Cannot by itself produce 2-grosz drift on 11 lines; per-line rounding is needed for that | Show the query/loader function and type(row["net"]) on both machines |
| Subscription billing started running at 00:xx in September (before, it ran later) | Explains why misfiled invoices appeared only recently | Does not explain bug A | Check the billing cron/scheduler history and the issued_at times of Sept/Aug 1st invoices |
4. Fix
import csv
from collections import defaultdict
from datetime import datetime
from decimal import Decimal, ROUND_HALF_UP
from zoneinfo import ZoneInfo
WARSAW = ZoneInfo("Europe/Warsaw")
GROSZ = Decimal("0.01")
def invoice_total(lines):
# Like Symfonia: sum net per VAT rate, compute VAT once per rate, round half-up.
net_by_rate = defaultdict(Decimal)
for l in lines:
net_by_rate[Decimal(str(l["vat"]))] += Decimal(str(l["net"]))
total = Decimal("0")
for rate, net_sum in net_by_rate.items():
vat = (net_sum * rate / 100).quantize(GROSZ, rounding=ROUND_HALF_UP)
total += net_sum + vat
return total.quantize(GROSZ)
def report_month(issued_at):
# Month of issue in Polish local time, regardless of the offset in the string.
dt = datetime.fromisoformat(issued_at).astimezone(WARSAW)
return dt.strftime("%Y-%m")
export() stays unchanged, because f"{Decimal:.2f}" formats correctly. Computing VAT once per rate in exact decimals reproduces Symfonia's numbers, and converting to Europe/Warsaw instead of UTC assigns each invoice to the month it was issued in locally.
5. Regression test
# test_export_invoices.py (run: pytest -q)
from decimal import Decimal
from export_invoices import invoice_total, report_month
def test_matches_symfonia_totals_and_local_month():
# 10 lines x 10.01 net @23%: per-line rounding gives 123.10,
# Symfonia gives 100.10 + round(23.023) = 123.12
lines = [{"net": "10.01", "vat": 23}] * 10
assert invoice_total(lines) == Decimal("123.12")
# Subscription invoice just after midnight on the 1st (CEST and CET)
assert report_month("2026-10-01T00:14:52+02:00") == "2026-10"
assert report_month("2026-12-01T00:30:00+01:00") == "2026-12"
Before the fix, invoice_total returns 123.1 (a float, not equal to Decimal("123.12")) and report_month returns "2026-09" / "2026-11". After the fix, all three assertions pass.