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>
223 lines
7.4 KiB
Dart
223 lines
7.4 KiB
Dart
import 'package:equatable/equatable.dart';
|
|
|
|
import '../../core/utils/extensions.dart';
|
|
import 'transaction.dart';
|
|
|
|
/// Everything the back office needs from a day at this terminal.
|
|
///
|
|
/// Computed from the locally stored transactions, so it can be produced with
|
|
/// no connection and pushed whenever one is available.
|
|
class ShiftReport extends Equatable {
|
|
const ShiftReport({
|
|
required this.businessDate,
|
|
required this.terminalId,
|
|
required this.cashierName,
|
|
required this.billCount,
|
|
required this.itemCount,
|
|
required this.grossSales,
|
|
required this.taxCollected,
|
|
required this.discountGiven,
|
|
required this.roundOff,
|
|
required this.paymentBreakdown,
|
|
required this.loyaltyPointsIssued,
|
|
required this.loyaltyPointsRedeemed,
|
|
this.firstBillAt,
|
|
this.lastBillAt,
|
|
});
|
|
|
|
final DateTime businessDate;
|
|
final String terminalId;
|
|
final String cashierName;
|
|
|
|
final int billCount;
|
|
|
|
/// Total units sold across every bill.
|
|
final double itemCount;
|
|
|
|
final double grossSales;
|
|
final double taxCollected;
|
|
final double discountGiven;
|
|
final double roundOff;
|
|
|
|
final Map<PaymentMethod, double> paymentBreakdown;
|
|
|
|
final int loyaltyPointsIssued;
|
|
final int loyaltyPointsRedeemed;
|
|
|
|
final DateTime? firstBillAt;
|
|
final DateTime? lastBillAt;
|
|
|
|
/// A zeroed report for a day with no trading. Not a const — [DateTime]
|
|
/// cannot appear in a constant expression.
|
|
factory ShiftReport.blank({
|
|
required DateTime businessDate,
|
|
required String terminalId,
|
|
required String cashierName,
|
|
}) =>
|
|
ShiftReport(
|
|
businessDate: businessDate,
|
|
terminalId: terminalId,
|
|
cashierName: cashierName,
|
|
billCount: 0,
|
|
itemCount: 0,
|
|
grossSales: 0,
|
|
taxCollected: 0,
|
|
discountGiven: 0,
|
|
roundOff: 0,
|
|
paymentBreakdown: const {},
|
|
loyaltyPointsIssued: 0,
|
|
loyaltyPointsRedeemed: 0,
|
|
);
|
|
|
|
bool get isEmpty => billCount == 0;
|
|
|
|
double get averageBasket =>
|
|
billCount == 0 ? 0 : (grossSales / billCount).asMoney;
|
|
|
|
double get netOfTax => (grossSales - taxCollected).asMoney;
|
|
|
|
/// Builds the report from the day's transactions.
|
|
factory ShiftReport.fromTransactions({
|
|
required List<SaleTransaction> transactions,
|
|
required DateTime businessDate,
|
|
required String terminalId,
|
|
required String cashierName,
|
|
}) {
|
|
final completed = transactions
|
|
.where((t) =>
|
|
t.status == TransactionStatus.completed &&
|
|
t.createdAt.year == businessDate.year &&
|
|
t.createdAt.month == businessDate.month &&
|
|
t.createdAt.day == businessDate.day,)
|
|
.toList()
|
|
..sort((a, b) => a.createdAt.compareTo(b.createdAt));
|
|
|
|
final byMethod = <PaymentMethod, double>{};
|
|
for (final t in completed) {
|
|
for (final p in t.payments) {
|
|
byMethod[p.method] = ((byMethod[p.method] ?? 0) + p.amount).asMoney;
|
|
}
|
|
}
|
|
|
|
return ShiftReport(
|
|
businessDate: businessDate,
|
|
terminalId: terminalId,
|
|
cashierName: cashierName,
|
|
billCount: completed.length,
|
|
itemCount:
|
|
completed.fold(0.0, (s, t) => s + t.cart.totalQuantity),
|
|
grossSales: completed.fold(0.0, (s, t) => s + t.total).asMoney,
|
|
taxCollected:
|
|
completed.fold(0.0, (s, t) => s + t.cart.taxAmount).asMoney,
|
|
discountGiven: completed
|
|
.fold(0.0,
|
|
(s, t) => s + t.cart.billDiscountTotal + t.cart.lineDiscountTotal,)
|
|
.asMoney,
|
|
roundOff: completed.fold(0.0, (s, t) => s + t.cart.roundOff).asMoney,
|
|
paymentBreakdown: byMethod,
|
|
loyaltyPointsIssued:
|
|
completed.fold(0, (s, t) => s + t.pointsEarned),
|
|
loyaltyPointsRedeemed:
|
|
completed.fold(0, (s, t) => s + t.pointsRedeemed),
|
|
firstBillAt: completed.isEmpty ? null : completed.first.createdAt,
|
|
lastBillAt: completed.isEmpty ? null : completed.last.createdAt,
|
|
);
|
|
}
|
|
|
|
/// Rebuilds a report from an archived day row.
|
|
factory ShiftReport.fromArchive({
|
|
required Map<String, Object?> row,
|
|
required Map<PaymentMethod, double> payments,
|
|
required DateTime businessDate,
|
|
required String terminalId,
|
|
required String cashierName,
|
|
}) {
|
|
DateTime? at(Object? v) =>
|
|
v == null ? null : DateTime.fromMillisecondsSinceEpoch(v as int);
|
|
|
|
return ShiftReport(
|
|
businessDate: businessDate,
|
|
terminalId: terminalId,
|
|
cashierName: cashierName,
|
|
billCount: (row['bill_count'] as int?) ?? 0,
|
|
itemCount: (row['item_count'] as num?)?.toDouble() ?? 0,
|
|
grossSales: (row['gross_sales'] as num?)?.toDouble() ?? 0,
|
|
taxCollected: (row['tax_collected'] as num?)?.toDouble() ?? 0,
|
|
discountGiven: (row['discount_given'] as num?)?.toDouble() ?? 0,
|
|
roundOff: (row['round_off'] as num?)?.toDouble() ?? 0,
|
|
paymentBreakdown: payments,
|
|
loyaltyPointsIssued: (row['points_issued'] as int?) ?? 0,
|
|
loyaltyPointsRedeemed: (row['points_redeemed'] as int?) ?? 0,
|
|
firstBillAt: at(row['first_bill_at']),
|
|
lastBillAt: at(row['last_bill_at']),
|
|
);
|
|
}
|
|
|
|
/// Adds two reports for the same day.
|
|
///
|
|
/// Needed because synced bills are deleted from the terminal: the day's true
|
|
/// figures are the archived totals plus whatever is still held locally.
|
|
ShiftReport operator +(ShiftReport other) {
|
|
final payments = <PaymentMethod, double>{...paymentBreakdown};
|
|
other.paymentBreakdown.forEach((k, v) {
|
|
payments[k] = ((payments[k] ?? 0) + v).asMoney;
|
|
});
|
|
|
|
DateTime? earliest(DateTime? a, DateTime? b) {
|
|
if (a == null) return b;
|
|
if (b == null) return a;
|
|
return a.isBefore(b) ? a : b;
|
|
}
|
|
|
|
DateTime? latest(DateTime? a, DateTime? b) {
|
|
if (a == null) return b;
|
|
if (b == null) return a;
|
|
return a.isAfter(b) ? a : b;
|
|
}
|
|
|
|
return ShiftReport(
|
|
businessDate: businessDate,
|
|
terminalId: terminalId,
|
|
cashierName: cashierName,
|
|
billCount: billCount + other.billCount,
|
|
itemCount: itemCount + other.itemCount,
|
|
grossSales: (grossSales + other.grossSales).asMoney,
|
|
taxCollected: (taxCollected + other.taxCollected).asMoney,
|
|
discountGiven: (discountGiven + other.discountGiven).asMoney,
|
|
roundOff: (roundOff + other.roundOff).asMoney,
|
|
paymentBreakdown: payments,
|
|
loyaltyPointsIssued: loyaltyPointsIssued + other.loyaltyPointsIssued,
|
|
loyaltyPointsRedeemed:
|
|
loyaltyPointsRedeemed + other.loyaltyPointsRedeemed,
|
|
firstBillAt: earliest(firstBillAt, other.firstBillAt),
|
|
lastBillAt: latest(lastBillAt, other.lastBillAt),
|
|
);
|
|
}
|
|
|
|
/// The JSON body that would be sent to the back office.
|
|
Map<String, Object?> toPayload() => {
|
|
'business_date': businessDate.toIso8601String().substring(0, 10),
|
|
'terminal_id': terminalId,
|
|
'cashier': cashierName,
|
|
'bill_count': billCount,
|
|
'item_count': itemCount,
|
|
'gross_sales': grossSales,
|
|
'net_of_tax': netOfTax,
|
|
'tax_collected': taxCollected,
|
|
'discount_given': discountGiven,
|
|
'round_off': roundOff,
|
|
'average_basket': averageBasket,
|
|
'loyalty_points_issued': loyaltyPointsIssued,
|
|
'loyalty_points_redeemed': loyaltyPointsRedeemed,
|
|
'first_bill_at': firstBillAt?.toIso8601String(),
|
|
'last_bill_at': lastBillAt?.toIso8601String(),
|
|
'payments': {
|
|
for (final e in paymentBreakdown.entries) e.key.name: e.value,
|
|
},
|
|
};
|
|
|
|
@override
|
|
List<Object?> get props =>
|
|
[businessDate, terminalId, billCount, grossSales];
|
|
}
|