bar chart
Ten-dollar discount over lines worth 7, 11 and 13
Illustrative inputs. Allocated cents per line after largest-remainder distribution; the three sum to 1000.
That a receipt got money off was all the discount tracker knew. It had no idea which product got it.
I use the tracker to see what promotions cost the stores and which supplier credits I can claim. The first sync read one discount total per receipt and wrote one row for it. A receipt has lines, not one product, so that row couldn't be billed to any supplier.
What the feed can tell me
The sales feed actually gives more detail than that. Some discounts sit on a line. Some sit on the receipt but point at a line or product. Some point at nothing. The new sync works through them from most certain to least:
- A discount on a line goes to that line, and the product catalog fills in missing brand and vendor.
- A receipt discount that points at a line or product goes there in full.
- A receipt discount with no pointer gets split across the lines by how much each line was worth.
- If no line has a value, it stays as one row with no product, marked unattributed.
The old receipt-total row only gets written if nothing else did. Same for returns. Otherwise the money would show up twice.
Not losing a cent
If you round each share separately, the total drifts. A dollar over three equal lines comes out to ninety-nine cents. So the split uses whole cents: each line gets its share rounded down, then the leftover cents go one at a time to the lines that lost the most to rounding. The parts always add up to the discount.
A line's value is its total, or price times quantity, or zero. Lines worth zero are skipped. Every split row is tagged as allocated so it can be told apart from a linked one.
Guesses, gaps and ties
The split is a guess. Splitting by value fits a percent-off-everything sale, but a coupon aimed at one product gets spread over lines it never touched. Nothing in the commit tests the splitter. When two lines tie for the extra cent, the one listed first wins.
Rows are only ever added or updated, never removed. So if a receipt later shows up with proper links, I'd bet its old split rows are still sitting there too.
The chart works the split through with made-up numbers: a ten-dollar discount over lines worth 7, 11 and 13.