Upload shopper registrations, and charge GST to the lines that earned it

Two defects that share a shape: a figure landing on the wrong record.

Bill-level discounts were apportioned across every line by a single
factor, so "20% off Beverages" pulled tax out of the atta line as well.
The bill total was right either way, which is what made it easy to ship
— only the slab split on a filed return was wrong. Targeted campaigns
now reduce the lines they name, and bill-wide reductions still spread
pro rata, so the arithmetic is unchanged wherever it was already right.

Shoppers registered at a till only ever reached the back office as three
fields riding along on a bill. Somebody who signed up and bought nothing
existed on one terminal and nowhere else, and two tills registering the
same mobile each minted their own row. Customers are now an outbox of
their own on pos/{store}/{terminal}/customer, and the id is a UUIDv5
over the normalised mobile number — so a hundred terminals agree on who
a shopper is without talking to each other.

Registrations go up before bills, and a failure there cannot strand a
day's takings. No loyalty figures are sent: they belong to the bill
stream, which is idempotent and knows about every counter.

Two things found while building it. Numbers were keyed on raw digits, so
a cashier typing +91 forked a shopper as effectively as a random id
would. And the sale path wrote the customer with ConflictAlgorithm
.replace, which is a DELETE and an INSERT — every column absent from the
row reverts to its schema default, so the new sync flag would have been
cleared by the shopper's next purchase.

Schema v8. Existing customers are queued rather than assumed sent: the
terminal cannot tell an imported row from a locally registered one, and
only one of those mistakes loses somebody.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Suriya
2026-08-03 11:34:27 +05:30
parent 467d5eee75
commit fe428931ec
24 changed files with 1274 additions and 78 deletions

View File

@@ -130,6 +130,19 @@ void main() {
'synced_bills': 12,
});
await db.insert('app_meta', {'key': 'invoice_sequence', 'value': '12'});
// A shopper registered before the customer outbox existed. Whether they
// reach the back office at all depends on what v8 does with this row.
await db.insert('customers', {
'id': 'legacy-customer-1',
'name': 'Meena',
'mobile': '9840011111',
'loyalty_points': 40,
'lifetime_spend': 2400.0,
'visit_count': 3,
'created_at': 1000,
});
await db.close();
}
@@ -139,7 +152,7 @@ void main() {
await AppDatabase.instance.open(overridePath: dbPath);
final db = AppDatabase.instance.db;
expect(await db.getVersion(), 7);
expect(await db.getVersion(), 8);
final rows = await db.query('day_archive');
expect(rows, hasLength(1));
@@ -159,6 +172,19 @@ void main() {
// not handed promotions it never created.
final promos = await db.query('promos');
expect(promos, isEmpty);
// v8 turns customers into an outbox. Every existing shopper is queued
// rather than assumed sent: the terminal cannot tell which rows came down
// in a catalogue pull and which were registered at the till, and only one
// of those two mistakes loses somebody. Re-sending is safe because the
// uplink is insert-if-absent on id.
final customer = (await db.query('customers')).single;
expect(customer['sync_status'], 0);
expect(customer['synced_at'], isNull);
// …and their loyalty standing survives the migration untouched.
expect(customer['loyalty_points'], 40);
expect(customer['lifetime_spend'], 2400.0);
expect(row['bill_count'], 12);
expect(row['gross_sales'], 8450.0);
expect(row['tax_collected'], 620.5);