Why a Half-Paid Invoice Can Show Twice the Outstanding Balance
An invoice can have several lines, and a customer can settle it in several transfers. Both exports look reasonable when you open them separately. Join them on the invoice number, though, and you can get a reconciliation report that overstates the outstanding balance while showing exactly the payment status you expected.
Start with an invoice you can add by hand
Take invoice 417 from company A, issued for 100 EUR. Its two lines are 65 EUR and 35 EUR, including tax. On Monday the customer paid 30 EUR. On Friday they paid another 20 EUR. Both transfers were allocated entirely to this invoice. The customer owes 50 EUR. If this report drives a collections queue, that last figure is the amount a colleague will quote on the phone.
A join between the raw line and allocation tables produces four pairs. The 65 EUR line appears alongside each payment, as does the 35 EUR line. Sum the joined columns and the report shows 200 EUR invoiced, 100 EUR paid and 100 EUR outstanding. It still labels the invoice half paid. A test that checks only the status or percentage passes.
Give each side one row per invoice
In this export, invoice_id is unique within company_id, and allocations are already expressed in the invoice currency. The input contains only posted records through the reporting cutoff. Each allocation row carries the amount applied to its particular invoice. This matters when one bank transfer covers several invoices. The Ukrainian guide to bank statements provides background on the bank record behind that distinction.
WITH billed AS (
SELECT company_id, invoice_id, SUM(gross_amount) AS total
FROM invoice_lines
GROUP BY company_id, invoice_id
),
paid AS (
SELECT company_id, invoice_id, SUM(applied_amount) AS total
FROM payment_allocations
GROUP BY company_id, invoice_id
)
SELECT b.company_id, b.invoice_id, b.total AS invoiced,
COALESCE(p.total, 0) AS paid,
b.total - COALESCE(p.total, 0) AS outstanding
FROM billed b
LEFT JOIN paid p
ON p.company_id = b.company_id
AND p.invoice_id = b.invoice_id
The left join keeps invoices that have lines and have received no payment. For invoice 417 it returns 100, 50 and 50. Run it with a cutoff before Monday and the same invoice returns 100, 0 and 100. If your export reuses invoice numbers across years or ledgers, carry the full document key through both groupings and the join.
Make the failure visible in a test
For a discrepancy, pull the line IDs and allocation IDs for one invoice and print the pairs from the original join. Two lines and two allocations should explain all four rows. Resist the temptation to use SUM(DISTINCT applied_amount). Change Friday’s payment to 30 EUR and that expression discards a genuine transfer. The expected paid total becomes 60 EUR and the balance becomes 40 EUR.
Finally, compare the report’s billed and paid totals against independent sums of the source rows, by company and currency, over the same invoice population and cutoff. Check that the output has exactly one row per expected invoice. Separately list allocations whose invoice key has no matching lines. Otherwise an allocation can disappear from both the report and a check restricted to matched invoices. In a period-end run, keep that exception count beside the totals so a missing document stays visible.
All rights reserved