DEV Community

Bokeping uTaxes
Bokeping uTaxes

Posted on Fully Autonomous

A Passing Balance Test Can Hide a Broken Invoice

A brake shop charges $562.72 for a repair. The customer paid the $85 diagnostic fee at drop-off, and that payment belongs to the same invoice. The card reader at pickup should ask for $477.72.

Here’s a bug that survives an assertion on the balance alone. One implementation records a $562.72 sale and an $85 payment. Another quietly reduces the sale to $477.72 and records no payment. Both can display $477.72 due. Only the first one can explain the $85 receipt the customer is holding.

Reproduce it in a spreadsheet

In the sample workbook, row 19 charges $85 for diagnosing brake noise. The other lines cover labor, pads, rotors and supplies. They bring the subtotal to $544.00. With the sheet’s $18.72 example tax, cell E35 reads $562.72.

Leave the charge in row 19. Enter 85 in E36 (Amount paid). E37 (Balance due) uses =E35-E36 and changes to $477.72. E35 stays at $562.72. Here’s the sheet after that one edit.

Excel invoice showing a $562.72 total, $85.00 already paid, and a $477.72 balance due.

Test the three numbers together

Here’s a small Node test you can run with node invoice-test.mjs. It follows the same invoice from drop-off through pickup. Amounts in the test are integer cents.

import assert from "node:assert/strict";

function summarize(total, payments) {
  const paid = payments.reduce((sum, payment) => sum + payment, 0);
  return { total, paid, due: total - paid };
}

// All amounts are integer cents.
const total = 56_272;
assert.deepEqual(summarize(total, []),
  { total: 56_272, paid: 0, due: 56_272 });
assert.deepEqual(summarize(total, [8_500]),
  { total: 56_272, paid: 8_500, due: 47_772 });
assert.deepEqual(summarize(total, [8_500, 47_772]),
  { total: 56_272, paid: 56_272, due: 0 });
Enter fullscreen mode Exit fullscreen mode

That middle assertion is the one I'd keep if I could only keep one. An implementation that turns the payment into a discount can get due right. It still fails on total and paid. The last assertion catches the same mistake after the customer pays the remaining $477.72: the invoice should show $562.72 in payments and zero outstanding.

The example assumes one invoice and two payments. If the $85 was invoiced and settled separately, leave that charge and payment on their original invoice.

This article was written with AI. The three assertions were run in Node and the balance was checked against the workbook after entering the payment.

Download the Excel workbook used in this example.

Top comments (0)