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

@@ -195,9 +195,9 @@ class _CustomersViewState extends ConsumerState<CustomersView> {
StatusPill.tier(c.tier, dense: true),
Cell('${c.loyaltyPoints}', mono: true),
Cell(Formatters.moneyCompact(c.lifetimeSpend),
mono: true, bold: true),
mono: true, bold: true,),
Cell('${c.visitCount}', mono: true),
])
],)
.toList(),
),
],

View File

@@ -146,7 +146,7 @@ class EventsView extends ConsumerWidget {
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Icon(Icons.shield_outlined,
size: 15, color: AppColors.textTertiary),
size: 15, color: AppColors.textTertiary,),
SizedBox(width: AppSpacing.sm),
Expanded(
child: Text(
@@ -181,14 +181,14 @@ class EventsView extends ConsumerWidget {
.map((o) => [
Cell(o.invoiceNumber, bold: true, mono: true),
Cell(Formatters.time(o.createdAt),
color: AppColors.textTertiary),
color: AppColors.textTertiary,),
Cell(Formatters.money(o.total), mono: true, bold: true),
TagChip(
o.isSynced ? 'Synced' : 'Pending',
color:
o.isSynced ? AppColors.success : AppColors.warning,
),
])
],)
.toList(),
),
),
@@ -227,8 +227,8 @@ class EventsView extends ConsumerWidget {
: AppColors.textSecondary,
),
Cell(Formatters.time(e.createdAt),
color: AppColors.textTertiary),
])
color: AppColors.textTertiary,),
],)
.toList(),
),
),

View File

@@ -102,14 +102,14 @@ class ProductImportView extends ConsumerWidget {
mainAxisSize: MainAxisSize.min,
children: [
Text(p.emoji,
style: const TextStyle(fontSize: 17)),
style: const TextStyle(fontSize: 17),),
const SizedBox(width: AppSpacing.sm),
Flexible(child: Cell(p.name, bold: true)),
],
),
Cell(p.sku, color: AppColors.textTertiary),
TagChip(p.category.label,
color: AppColors.textSecondary),
color: AppColors.textSecondary,),
Cell(Formatters.money(p.price), mono: true, bold: true),
TagChip(
p.isOutOfStock
@@ -121,7 +121,7 @@ class ProductImportView extends ConsumerWidget {
? AppColors.warning
: AppColors.success),
),
])
],)
.toList(),
),
),
@@ -146,13 +146,13 @@ class _NotImportedBanner extends StatelessWidget {
borderRadius: AppRadius.brLg,
border: Border.all(color: AppColors.warning.withValues(alpha: 0.35)),
),
child: Row(
child: const Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Icon(Icons.cloud_download_outlined,
color: AppColors.warning, size: 22),
const SizedBox(width: AppSpacing.md),
const Expanded(
Icon(Icons.cloud_download_outlined,
color: AppColors.warning, size: 22,),
SizedBox(width: AppSpacing.md),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
@@ -225,7 +225,7 @@ class _ImportPanel extends ConsumerWidget {
Container(
padding: const EdgeInsets.all(AppSpacing.md),
margin: const EdgeInsets.only(bottom: AppSpacing.lg),
decoration: BoxDecoration(
decoration: const BoxDecoration(
color: AppColors.dangerSurface,
borderRadius: AppRadius.brSm,
),
@@ -233,7 +233,7 @@ class _ImportPanel extends ConsumerWidget {
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Icon(Icons.wifi_off_rounded,
color: AppColors.danger, size: 18),
color: AppColors.danger, size: 18,),
const SizedBox(width: AppSpacing.sm),
Expanded(
child: Text(
@@ -254,14 +254,14 @@ class _ImportPanel extends ConsumerWidget {
Container(
padding: const EdgeInsets.all(AppSpacing.md),
margin: const EdgeInsets.only(bottom: AppSpacing.lg),
decoration: BoxDecoration(
decoration: const BoxDecoration(
color: AppColors.successSurface,
borderRadius: AppRadius.brSm,
),
child: Row(
children: [
const Icon(Icons.check_circle_outline_rounded,
color: AppColors.success, size: 18),
color: AppColors.success, size: 18,),
const SizedBox(width: AppSpacing.sm),
Expanded(
child: Text(
@@ -312,13 +312,13 @@ class _ImportPanel extends ConsumerWidget {
),
const SizedBox(height: AppSpacing.lg),
Row(
const Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Icon(Icons.info_outline_rounded,
size: 15, color: AppColors.textTertiary),
const SizedBox(width: AppSpacing.sm),
const Expanded(
Icon(Icons.info_outline_rounded,
size: 15, color: AppColors.textTertiary,),
SizedBox(width: AppSpacing.sm),
Expanded(
child: Text(
'After this import the terminal works entirely offline. '
'Sales, customers and parked bills are held locally and are '

View File

@@ -64,21 +64,21 @@ class _PromosViewState extends State<PromosView> {
icon: Icons.campaign_rounded,
caption: 'of ${_campaigns.length} configured',
),
StatTile(
const StatTile(
label: 'Redemptions',
value: '970',
icon: Icons.confirmation_number_rounded,
color: AppColors.info,
caption: 'this month',
),
StatTile(
const StatTile(
label: 'Discount Given',
value: '₹48,240',
icon: Icons.local_offer_rounded,
color: AppColors.warning,
caption: '2.6% of sales',
),
StatTile(
const StatTile(
label: 'Incremental Sales',
value: '₹2.14L',
icon: Icons.trending_up_rounded,
@@ -133,7 +133,7 @@ class _PromosViewState extends State<PromosView> {
borderRadius: AppRadius.brSm,
),
child: Icon(Icons.sell_rounded,
size: 18, color: c.$6),
size: 18, color: c.$6,),
),
const SizedBox(width: AppSpacing.md),
Expanded(

View File

@@ -167,7 +167,7 @@ class _SettingsViewState extends ConsumerState<SettingsView> {
if (list.isEmpty) {
return Container(
padding: const EdgeInsets.all(AppSpacing.md),
decoration: BoxDecoration(
decoration: const BoxDecoration(
color: AppColors.warningSurface,
borderRadius: AppRadius.brSm,
),
@@ -175,7 +175,7 @@ class _SettingsViewState extends ConsumerState<SettingsView> {
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Icon(Icons.print_disabled_rounded,
size: 18, color: AppColors.warning),
size: 18, color: AppColors.warning,),
SizedBox(width: AppSpacing.sm),
Expanded(
child: Text(
@@ -235,7 +235,7 @@ class _SettingsViewState extends ConsumerState<SettingsView> {
final ok = await ref
.read(receiptServiceProvider)
.printTestPage(
printerUrl: settings.printerUrl);
printerUrl: settings.printerUrl,);
if (!context.mounted) return;
ScaffoldMessenger.of(context)
..hideCurrentSnackBar()
@@ -245,8 +245,8 @@ class _SettingsViewState extends ConsumerState<SettingsView> {
: AppColors.danger,
content: Text(ok
? 'Test slip sent to the printer.'
: 'Could not reach the printer.'),
));
: 'Could not reach the printer.',),
),);
},
icon: const Icon(Icons.receipt_long_rounded, size: 17),
label: const Text('Print test slip'),
@@ -307,9 +307,9 @@ class _SettingsViewState extends ConsumerState<SettingsView> {
Cell(s.role.label, color: AppColors.textSecondary),
s.id == current?.id
? const TagChip('Signed in',
color: AppColors.success)
color: AppColors.success,)
: const SizedBox.shrink(),
])
],)
.toList(),
),
);