Konrad du Plessis 25910b2861 fix: worker batch report 'Total Paid' was inflated by the work-log join (+ kill its N+1s)
_build_worker_report_context computed Sum('payroll_records__amount_paid')
in the SAME .annotate() as counts over work_logs and warnings. Django
joins ALL those relations into one query, so each payroll row was
duplicated once per work-log row — the lifetime Total Paid column on
/workers/report/ (HTML, CSV and PDF) was multiplied by the worker's
log count (3 logs + one R100 payslip displayed R300). The distinct=True
counts were immune, which is why the suite never caught it. Verified by
a controlled experiment before fixing; regression test added.

Restructure: payroll aggregates stay in one annotate (same join — safe);
days-worked, warnings and project names move to three batched GROUP BY
dicts; teams + certificates are prefetched. Also removes the ~4 queries
per worker the loop used to fire (audit findings #8).

Display-only bug — payment processing was never affected.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-12 17:56:39 +02:00
..
2026-02-22 12:14:54 +00:00
2026-04-22 00:19:15 +02:00