Fix billing data integrity, sale atomicity and stock safety

Bills were persisted correctly but read back wrong. The read path rebuilt a
cart from its lines alone, dropping bill-level discounts and loyalty, so every
figure derived from a stored bill was overstated: the upload payload, the day
archive and the shift report. A discounted 529 bill read back as 620.

Money and data integrity
- order_dao: restore bill_discount and points_redeemed when rebuilding a cart;
  keep the reconstruction tier-less so the membership discount is not applied
  twice. Trust the recorded total and points via SaleTransaction.storedTotal.
- checkout_sale + order_dao.commitSale: write the bill, its stock movement and
  the loyalty update in one transaction. Previously a failure part-way through
  left a persisted bill the cashier believed had failed, inviting a duplicate.
- checkout_sale: re-check every line against live stock. A parked bill resumed
  after its stock was sold passed validation and oversold.
- catalogue_dao: allocate the invoice sequence in one transaction; the previous
  read-modify-write could hand two sales the same number and fail UNIQUE.
- local_store: replay unsynced sales after a catalogue import, so a mid-shift
  re-import cannot restore stock that has already been sold.
- payment_controller: stamp the signed-in operator on the bill instead of the
  hardcoded seed session, and pass the terminal id through.
- cart: reconcile per-slab GST against the bill total so the parts sum to the
  whole on a tax invoice.

Sync and reporting
- sync_repository: drain unsynced bills in a loop rather than silently capping
  at one page; stop on rejection so rejected rows cannot loop forever.
- sync_log_dao (new): persist the sync history to the sync_log table, which the
  schema already defined but nothing used. It was in memory, so the only record
  that bills had been uploaded died at restart.
- Scope shift reports by cashier. day_archive is re-keyed to
  (business_date, cashier_name) so a till stays settleable after its bills are
  uploaded and deleted. Schema v4 with a migration that carries v3 rows across.

Input and UI
- barcode_service: consume machine-paced keystrokes so a scan cannot also land
  in the focused field, and raise the bar to 60ms/char while a text field has
  focus so typing a mobile number is not read as a scan. Clock and focus check
  injected so the behaviour is testable.
- primary_button: make the label flexible; label plus trailing total overflowed
  the Charge button by up to 131px.
- app_router: redirect instead of null-casting when the receipt route is
  entered without its transaction.
- customer_repository: reduce the search query to digits so a punctuated mobile
  number matches.

Cleanup
- Remove TransactionRepository.save, CustomerRepository.recordSale and
  OrderDao.insertOrder, all superseded by commitSale.
- dart fix across the tree; 251 analyzer issues down to 3 info-level.

Tests: 23 passing / 15 failing -> 90 passing. Fixed the two defects that broke
the existing suite (containsAll type argument, reset() needing a catalogue) and
deleted the leftover template test. Added coverage for the order round trip,
the day archive after a real sync, stock safety, checkout atomicity, the v3->v4
migration, scanner-versus-human input, and an app-level smoke test that renders
every module.

Note: bills already uploaded with a discount went up overstated. This stops it
happening again but does not correct historical server data.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Suriya
2026-07-31 18:34:10 +05:30
parent 9891a69a5f
commit af3933092f
62 changed files with 2035 additions and 561 deletions

View File

@@ -43,7 +43,7 @@ class ReceiptPreview extends StatelessWidget {
final discount = gross - cart.netAmount;
return Container(
decoration: BoxDecoration(
decoration: const BoxDecoration(
color: AppColors.surface,
borderRadius: AppRadius.brLg,
boxShadow: AppColors.shadowMd,
@@ -70,20 +70,20 @@ class ReceiptPreview extends StatelessWidget {
.copyWith(fontWeight: FontWeight.w700),
),
Text(AppConstants.storeLegalName,
style: AppTypography.mono(8.5)),
style: AppTypography.mono(8.5),),
const SizedBox(height: 2),
Text(AppConstants.storeAddress,
textAlign: TextAlign.center,
style: AppTypography.mono(8.5)),
style: AppTypography.mono(8.5),),
Text('Customer care : ${AppConstants.storePhone}',
style: AppTypography.mono(8.5)),
style: AppTypography.mono(8.5),),
Text('CIN No : ${AppConstants.storeCin}',
style: AppTypography.mono(8.5)),
style: AppTypography.mono(8.5),),
Text('GSTIN : ${AppConstants.storeGstin}',
style: AppTypography.mono(8.5)),
style: AppTypography.mono(8.5),),
Text('FSSAI Lic No : ${AppConstants.storeFssai}',
style: AppTypography.mono(8.5)),
]),
style: AppTypography.mono(8.5),),
],),
),
if (discount > 0) ...[
@@ -104,10 +104,10 @@ class ReceiptPreview extends StatelessWidget {
child: Column(children: [
Text('TAX INVOICE',
style: AppTypography.mono(11)
.copyWith(fontWeight: FontWeight.w700)),
.copyWith(fontWeight: FontWeight.w700),),
Text('xxxxxx Original for Recipient xxxxxx',
style: AppTypography.mono(8)),
]),
style: AppTypography.mono(8),),
],),
),
const SizedBox(height: AppSpacing.sm),
@@ -117,7 +117,7 @@ class ReceiptPreview extends StatelessWidget {
_plain('Customer Type: '
'${txn.customer == null ? 'URD' : 'REG'}'),
_row('Date:${Formatters.receiptStamp(txn.createdAt)}',
'Bill No:${txn.invoiceNumber.split('-').last}'),
'Bill No:${txn.invoiceNumber.split('-').last}',),
_row(
'Store:${AppConstants.storeCode} '
'Cashier:${txn.cashierName}',
@@ -135,13 +135,13 @@ class ReceiptPreview extends StatelessWidget {
Expanded(flex: 34, child: _bold('Item Description')),
Expanded(
flex: 16,
child: _bold('Net Price', align: TextAlign.right)),
child: _bold('Net Price', align: TextAlign.right),),
Expanded(
flex: 8, child: _bold('Qty', align: TextAlign.right)),
flex: 8, child: _bold('Qty', align: TextAlign.right),),
Expanded(
flex: 18,
child: _bold('Value', align: TextAlign.right)),
]),
child: _bold('Value', align: TextAlign.right),),
],),
const SizedBox(height: 3),
for (final slab in slabs) ...[
@@ -162,28 +162,28 @@ class ReceiptPreview extends StatelessWidget {
child: Row(children: [
Expanded(
flex: 12,
child: _cell(line.product.hsnCode ?? '-')),
child: _cell(line.product.hsnCode ?? '-'),),
Expanded(
flex: 34,
child:
_cell(line.product.name.toUpperCase())),
_cell(line.product.name.toUpperCase()),),
Expanded(
flex: 16,
child: _cell(
line.product.price.toStringAsFixed(2),
align: TextAlign.right),
align: TextAlign.right,),
),
Expanded(
flex: 8,
child: _cell(_qty(line.quantity),
align: TextAlign.right),
align: TextAlign.right,),
),
Expanded(
flex: 18,
child: _cell(line.payable.toStringAsFixed(2),
align: TextAlign.right),
align: TextAlign.right,),
),
]),
],),
),
],
@@ -197,7 +197,7 @@ class ReceiptPreview extends StatelessWidget {
_amount('Gross Sales Value', gross),
if (discount > 0) _amount('Total Discount', discount),
_amount(
'Net Sales Value (Inclusive of GST)', cart.netAmount),
'Net Sales Value (Inclusive of GST)', cart.netAmount,),
if (cart.roundOff != 0)
_amount('Round Off', cart.roundOff),
_amount('Total Amount Paid', txn.total, bold: true),
@@ -206,34 +206,34 @@ class ReceiptPreview extends StatelessWidget {
if (txn.changeDue > 0)
_amount('Change Returned', txn.changeDue),
Text('(AMOUNT INCLUSIVE OF APPLICABLE TAXES)',
style: AppTypography.mono(8)),
style: AppTypography.mono(8),),
const SizedBox(height: AppSpacing.md),
// ------------------------------------------- gst breakup
Center(
child: Text('------GST Breakup Details------ Amount (INR)',
style: AppTypography.mono(8.5)),
style: AppTypography.mono(8.5),),
),
const SizedBox(height: 4),
Row(children: [
Expanded(flex: 10, child: _bold('GST')),
Expanded(
flex: 20,
child: _bold('Taxable', align: TextAlign.right)),
child: _bold('Taxable', align: TextAlign.right),),
Expanded(
flex: 16,
child: _bold('CGST', align: TextAlign.right)),
child: _bold('CGST', align: TextAlign.right),),
Expanded(
flex: 16,
child: _bold('SGST', align: TextAlign.right)),
child: _bold('SGST', align: TextAlign.right),),
Expanded(
flex: 14,
child: _bold('CESS', align: TextAlign.right)),
child: _bold('CESS', align: TextAlign.right),),
Expanded(
flex: 20,
child: _bold('Total', align: TextAlign.right)),
]),
child: _bold('Total', align: TextAlign.right),),
],),
const SizedBox(height: 2),
for (final s in slabs)
Padding(
@@ -243,23 +243,23 @@ class ReceiptPreview extends StatelessWidget {
Expanded(
flex: 20,
child: _cell(s.taxable.toStringAsFixed(2),
align: TextAlign.right)),
align: TextAlign.right,),),
Expanded(
flex: 16,
child: _cell(s.cgst.toStringAsFixed(2),
align: TextAlign.right)),
align: TextAlign.right,),),
Expanded(
flex: 16,
child: _cell(s.sgst.toStringAsFixed(2),
align: TextAlign.right)),
align: TextAlign.right,),),
Expanded(
flex: 14,
child: _cell('0.00', align: TextAlign.right)),
child: _cell('0.00', align: TextAlign.right),),
Expanded(
flex: 20,
child: _cell(s.total.toStringAsFixed(2),
align: TextAlign.right)),
]),
align: TextAlign.right,),),
],),
),
const _Dashes(),
Row(children: [
@@ -270,32 +270,32 @@ class ReceiptPreview extends StatelessWidget {
slabs
.fold(0.0, (a, s) => a + s.taxable)
.toStringAsFixed(2),
align: TextAlign.right)),
align: TextAlign.right,),),
Expanded(
flex: 16,
child: _bold(
slabs
.fold(0.0, (a, s) => a + s.cgst)
.toStringAsFixed(2),
align: TextAlign.right)),
align: TextAlign.right,),),
Expanded(
flex: 16,
child: _bold(
slabs
.fold(0.0, (a, s) => a + s.sgst)
.toStringAsFixed(2),
align: TextAlign.right)),
align: TextAlign.right,),),
Expanded(
flex: 14,
child: _bold('0.00', align: TextAlign.right)),
child: _bold('0.00', align: TextAlign.right),),
Expanded(
flex: 20,
child: _bold(
slabs
.fold(0.0, (a, s) => a + s.total)
.toStringAsFixed(2),
align: TextAlign.right)),
]),
align: TextAlign.right,),),
],),
const _Dashes(),
_plain('TaxInvoice# ${txn.invoiceNumber}'),
@@ -311,10 +311,10 @@ class ReceiptPreview extends StatelessWidget {
const SizedBox(height: AppSpacing.sm),
Text('* Thank You for Shopping with us *',
style: AppTypography.mono(10)
.copyWith(fontWeight: FontWeight.w700)),
.copyWith(fontWeight: FontWeight.w700),),
Text('Powered by Nearle POS',
style: AppTypography.mono(8)),
]),
style: AppTypography.mono(8),),
],),
),
],
),
@@ -322,7 +322,7 @@ class ReceiptPreview extends StatelessWidget {
),
),
const _Perforation(top: false),
]),
],),
);
}
@@ -342,7 +342,7 @@ class ReceiptPreview extends StatelessWidget {
Flexible(
child: Text(left,
overflow: TextOverflow.ellipsis,
style: AppTypography.mono(9)),
style: AppTypography.mono(9),),
),
const SizedBox(width: AppSpacing.sm),
Text(right, style: AppTypography.mono(9)),
@@ -518,6 +518,6 @@ class _FakeBarcode extends StatelessWidget {
),
const SizedBox(height: 3),
Text(value, style: AppTypography.mono(9)),
]);
],);
}
}