## TL;DR

A total mismatch means one of three things: an extraction error (misread digit, missed row, double-counted row), a real invoice feature (discounts, tax, freight on separate lines), or rounding. Check in order: recompute from rows, look for discount/tax/freight lines the extractor missed, then allow a small rounding tolerance. Only escalate to the vendor or human review after the mechanical checks pass.

## Error

```text
ValidationError: invoice total 12,480.00 != sum of line items 12,240.00 (delta 240.00)
```

## Steps

1. Recompute the sum from the raw extracted rows, watching for rows parsed twice.
   Expected: A clean recomputation, often revealing a doubled row.
2. Search the invoice for discount, tax, shipping, and fee lines outside the main table.
   Expected: The missing 240.00 is usually a freight or discount line.
3. Apply the invoice's own arithmetic: subtotal, discount, tax, shipping, total.
   Expected: Each intermediate value checked against the document.
4. Allow a rounding tolerance of a few cents per line times row count.
   Expected: Legitimate rounding no longer flags.
5. If the delta persists and is material, route to human review with the delta highlighted.
   Expected: A person decides whether to query the vendor.

## When to use

- Validation flags total vs line-item sum mismatch
- After OCR/extraction, before ERP posting
- Building the validation layer itself

## When not to use

- ERP rejects the posting (different layer)
- PO matching quantity/price mismatches
- Currency conversion differences

## Compatibility

Extractor-agnostic. Implement in the validation service (Python/Pydantic typical).

## Variant phrasings

### invoice total mismatch line items

### extracted total does not match sum

### AP validation arithmetic failed

## Root cause

Invoices are not just tables: totals are computed from subtotal minus discounts plus tax plus freight, and extractors often miss the non-table lines. The mismatch is usually a missed line or a doubled row, not a wrong total.

## Edge cases

- Cash discounts (2/10 net 30) create two valid totals; validate against the gross total
- Multi-currency invoices need conversion before comparison
- Credit memos net against invoices; validate the net, not the gross

## Provenance

Resolved from the public thread: https://vectle.com/posts/pst_UVuIslIfmYqYKiDAXWFjxw
