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

@@ -245,7 +245,7 @@ class _Section extends ConsumerWidget {
AppSpacing.sm,
),
child: Text(section.label.toUpperCase(),
style: AppTypography.sectionLabel()),
style: AppTypography.sectionLabel(),),
)
else
const Padding(
@@ -459,7 +459,7 @@ class _LogoutTile extends ConsumerWidget {
mainAxisSize: MainAxisSize.min,
children: [
const Icon(Icons.logout_rounded,
size: 19, color: AppColors.danger),
size: 19, color: AppColors.danger,),
if (expanded) ...[
const SizedBox(width: AppSpacing.md),
const Text(

View File

@@ -1,8 +1,9 @@
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import '../../../core/constants/app_constants.dart';
import '../../../core/router/app_router.dart';
import '../../../core/theme/app_colors.dart';
import '../../../core/theme/app_dimens.dart';
@@ -66,7 +67,7 @@ class BillingPanel extends ConsumerWidget {
),
if (cart.isNotEmpty) _Summary(cart: cart),
_Actions(cart: cart),
]),
],),
);
}
}
@@ -101,7 +102,7 @@ class _Header extends ConsumerWidget {
const SizedBox(width: AppSpacing.sm),
Container(
padding: const EdgeInsets.symmetric(horizontal: 7, vertical: 2),
decoration: BoxDecoration(
decoration: const BoxDecoration(
color: AppColors.primarySurface,
borderRadius: AppRadius.brPill,
),
@@ -213,13 +214,13 @@ class _Summary extends ConsumerWidget {
horizontal: AppSpacing.md,
vertical: AppSpacing.sm + 2,
),
decoration: BoxDecoration(
decoration: const BoxDecoration(
color: AppColors.successSurface,
borderRadius: AppRadius.brSm,
),
child: Row(children: [
const Icon(Icons.stars_rounded,
size: 16, color: AppColors.success),
size: 16, color: AppColors.success,),
const SizedBox(width: AppSpacing.sm),
Text(
'This sale earns +${cart.pointsEarned} pts',
@@ -229,7 +230,7 @@ class _Summary extends ConsumerWidget {
fontSize: 13,
),
),
]),
],),
),
_Row(label: 'Subtotal', value: Formatters.money(cart.subtotal)),
@@ -324,7 +325,7 @@ class _Summary extends ConsumerWidget {
),
),
),
]),
],),
);
}
}
@@ -365,7 +366,7 @@ class _Row extends StatelessWidget {
style: const TextStyle(
fontSize: 11.5,
color: AppColors.textTertiary,
)),
),),
],
const SizedBox(width: AppSpacing.sm),
const Spacer(),
@@ -381,7 +382,7 @@ class _Row extends StatelessWidget {
const SizedBox(width: AppSpacing.xs),
Icon(trailingIcon, size: 14, color: AppColors.textTertiary),
],
]),
],),
);
}
}
@@ -412,7 +413,8 @@ class _Actions extends ConsumerWidget {
if (ref.read(cartControllerProvider).customer == null) {
await showCustomerCaptureSheet(context);
}
if (context.mounted) context.push(AppRoutes.payment);
// Navigation result is not needed here.
if (context.mounted) unawaited(context.push(AppRoutes.payment));
}
: null,
trailing: enabled

View File

@@ -36,7 +36,7 @@ class CartFab extends ConsumerWidget {
child: Container(
height: AppSizes.buttonHeightLarge,
padding: const EdgeInsets.symmetric(horizontal: AppSpacing.xl),
decoration: BoxDecoration(
decoration: const BoxDecoration(
borderRadius: AppRadius.brLg,
boxShadow: AppColors.shadowLg,
),
@@ -79,7 +79,7 @@ class CartFab extends ConsumerWidget {
),
const SizedBox(width: AppSpacing.sm),
const Icon(Icons.keyboard_arrow_up_rounded,
color: Colors.white, size: 20),
color: Colors.white, size: 20,),
],
),
),

View File

@@ -34,12 +34,12 @@ class CartLineTile extends StatelessWidget {
background: Container(
alignment: Alignment.centerRight,
padding: const EdgeInsets.only(right: AppSpacing.xl),
decoration: BoxDecoration(
decoration: const BoxDecoration(
color: AppColors.dangerSurface,
borderRadius: AppRadius.brMd,
),
child: const Icon(Icons.delete_outline_rounded,
color: AppColors.danger),
color: AppColors.danger,),
),
child: Container(
padding: const EdgeInsets.all(AppSpacing.md),
@@ -83,18 +83,18 @@ class CartLineTile extends StatelessWidget {
Formatters.money(p.price),
style: AppTypography.money(13.5,
weight: FontWeight.w600,
color: AppColors.textSecondary),
color: AppColors.textSecondary,),
),
Text(' / ${p.unit.symbol}',
style: const TextStyle(
fontSize: 11.5,
color: AppColors.textTertiary,
)),
),),
if (line.discount.isActive) ...[
const SizedBox(width: AppSpacing.sm),
Container(
padding: const EdgeInsets.symmetric(
horizontal: 5, vertical: 1),
horizontal: 5, vertical: 1,),
decoration: BoxDecoration(
color: AppColors.successSurface,
borderRadius: BorderRadius.circular(4),
@@ -109,7 +109,7 @@ class CartLineTile extends StatelessWidget {
),
),
],
]),
],),
],
),
),
@@ -121,7 +121,7 @@ class CartLineTile extends StatelessWidget {
padding: EdgeInsets.zero,
tooltip: 'Remove',
),
]),
],),
const SizedBox(height: AppSpacing.sm),
@@ -162,14 +162,14 @@ class CartLineTile extends StatelessWidget {
),
],
),
]),
],),
if (line.exceedsStock)
Padding(
padding: const EdgeInsets.only(top: AppSpacing.sm),
child: Row(children: [
const Icon(Icons.error_outline_rounded,
size: 14, color: AppColors.danger),
size: 14, color: AppColors.danger,),
const SizedBox(width: AppSpacing.xs),
Text(
'Only ${p.stock.toStringAsFixed(0)} ${p.unit.symbol} '
@@ -180,9 +180,9 @@ class CartLineTile extends StatelessWidget {
fontWeight: FontWeight.w600,
),
),
]),
],),
),
]),
],),
),
);
}
@@ -222,7 +222,7 @@ class _Stepper extends StatelessWidget {
),
),
_btn(Icons.add_rounded, onIncrement),
]),
],),
);
}

View File

@@ -108,7 +108,7 @@ class _Chip extends StatelessWidget {
),
),
],
]),
],),
),
),
),

View File

@@ -37,7 +37,7 @@ class CustomerBar extends ConsumerWidget {
: AppColors.primarySurface,
child: customer == null
? const Icon(Icons.directions_walk_rounded,
size: 20, color: AppColors.textSecondary)
size: 20, color: AppColors.textSecondary,)
: Text(
Formatters.initials(customer.name),
style: const TextStyle(
@@ -65,7 +65,7 @@ class CustomerBar extends ConsumerWidget {
const SizedBox(width: AppSpacing.sm),
StatusPill.tier(customer.tier, dense: true),
],
]),
],),
if (customer != null)
Text(
'${Formatters.mobile(customer.mobile)} · '
@@ -92,7 +92,7 @@ class CustomerBar extends ConsumerWidget {
icon: const Icon(Icons.person_off_outlined, size: 17),
label: const Text('Detach'),
style: TextButton.styleFrom(
foregroundColor: AppColors.textSecondary),
foregroundColor: AppColors.textSecondary,),
),
const SizedBox(width: AppSpacing.sm),
OutlinedButton.icon(
@@ -104,7 +104,7 @@ class CustomerBar extends ConsumerWidget {
padding: const EdgeInsets.symmetric(horizontal: AppSpacing.lg),
),
),
]),
],),
);
}
}

View File

@@ -116,7 +116,7 @@ class _DiscountSheetState extends State<_DiscountSheet> {
Container(
width: 40,
height: 4,
decoration: BoxDecoration(
decoration: const BoxDecoration(
color: AppColors.border,
borderRadius: AppRadius.brPill,
),
@@ -124,13 +124,13 @@ class _DiscountSheetState extends State<_DiscountSheet> {
const SizedBox(height: AppSpacing.xl),
Text(widget.title,
style: const TextStyle(fontSize: 18, fontWeight: FontWeight.w700)),
style: const TextStyle(fontSize: 18, fontWeight: FontWeight.w700),),
const SizedBox(height: 2),
Text(widget.subtitle,
style: const TextStyle(
fontSize: 13,
color: AppColors.textSecondary,
)),
),),
const SizedBox(height: AppSpacing.xxl),
SegmentedButton<DiscountType>(
@@ -177,10 +177,10 @@ class _DiscountSheetState extends State<_DiscountSheet> {
.map((v) => ActionChip(
label: Text(_type == DiscountType.percentage
? '$v%'
: '$v'),
: '$v',),
onPressed: () =>
setState(() => _value.text = v.toString()),
))
),)
.toList(),
),
const SizedBox(height: AppSpacing.xxl),
@@ -205,8 +205,8 @@ class _DiscountSheetState extends State<_DiscountSheet> {
onPressed: _apply,
),
),
]),
]),
],),
],),
),
);
}

View File

@@ -133,13 +133,13 @@ class _Breadcrumb extends StatelessWidget {
const Padding(
padding: EdgeInsets.symmetric(horizontal: AppSpacing.xs + 2),
child: Icon(Icons.chevron_right_rounded,
size: 13, color: AppColors.textTertiary),
size: 13, color: AppColors.textTertiary,),
),
Text(module.section.label, style: style),
const Padding(
padding: EdgeInsets.symmetric(horizontal: AppSpacing.xs + 2),
child: Icon(Icons.chevron_right_rounded,
size: 13, color: AppColors.textTertiary),
size: 13, color: AppColors.textTertiary,),
),
Text(
module.label,
@@ -285,7 +285,7 @@ class _ParkedBillsButton extends ConsumerWidget {
final bill = parked[i];
return ListTile(
leading: const Icon(Icons.receipt_long_rounded,
color: AppColors.primary),
color: AppColors.primary,),
title: Text(bill.displayLabel),
subtitle: Text(
'${bill.cart.lineCount} items · '

View File

@@ -1,6 +1,5 @@
import 'package:flutter/material.dart';
import '../../../core/constants/app_constants.dart';
import '../../../core/theme/app_colors.dart';
import '../../../core/theme/app_dimens.dart';
import '../../../core/theme/app_typography.dart';
@@ -178,7 +177,7 @@ class _ProductCardState extends State<ProductCard> {
left: AppSpacing.sm,
child: Container(
padding: const EdgeInsets.symmetric(
horizontal: 6, vertical: 2),
horizontal: 6, vertical: 2,),
decoration: BoxDecoration(
color: AppColors.success,
borderRadius: BorderRadius.circular(5),
@@ -199,7 +198,7 @@ class _ProductCardState extends State<ProductCard> {
top: AppSpacing.sm,
right: AppSpacing.sm,
child: Icon(Icons.warning_amber_rounded,
size: 15, color: AppColors.warning),
size: 15, color: AppColors.warning,),
),
// Quantity badge once the item is on the bill.
@@ -243,10 +242,10 @@ class _ProductCardState extends State<ProductCard> {
shape: BoxShape.circle,
),
child: const Icon(Icons.add_rounded,
size: 18, color: Colors.white),
size: 18, color: Colors.white,),
),
),
]),
],),
),
),
),

View File

@@ -67,7 +67,7 @@ class _ScanToastState extends ConsumerState<ScanToast> {
horizontal: AppSpacing.xl,
vertical: AppSpacing.md,
),
decoration: BoxDecoration(
decoration: const BoxDecoration(
color: AppColors.textPrimary,
borderRadius: AppRadius.brPill,
boxShadow: AppColors.shadowLg,
@@ -110,7 +110,7 @@ class _ScanToastState extends ConsumerState<ScanToast> {
fontSize: 13.5,
),
),
]),
],),
)
.animate(key: ValueKey(feedback.stamp))
.fadeIn(duration: 140.ms)

View File

@@ -44,6 +44,13 @@ class _PosSearchFieldState extends ConsumerState<PosSearchField> {
Widget build(BuildContext context) {
final query = ref.watch(searchQueryProvider);
// A scan clears the query from outside this widget. Mirror that into the
// controller so any stray character the scanner leaked into the field goes
// with it.
ref.listen<String>(searchQueryProvider, (_, next) {
if (next.isEmpty && _controller.text.isNotEmpty) _controller.clear();
});
return TextField(
controller: _controller,
focusNode: widget.focusNode,
@@ -80,23 +87,23 @@ class _PosSearchFieldState extends ConsumerState<PosSearchField> {
horizontal: AppSpacing.md,
vertical: AppSpacing.sm,
),
decoration: BoxDecoration(
decoration: const BoxDecoration(
color: AppColors.primarySurface,
borderRadius: AppRadius.brSm,
),
child: const Row(mainAxisSize: MainAxisSize.min, children: [
Icon(Icons.qr_code_scanner_rounded,
size: 18, color: AppColors.primary),
size: 18, color: AppColors.primary,),
SizedBox(width: AppSpacing.xs + 2),
Text('Scanner ready',
style: TextStyle(
color: AppColors.primary,
fontSize: 12,
fontWeight: FontWeight.w600,
)),
]),
),),
],),
),
]),
],),
),
);
}