third commit
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:uuid/uuid.dart';
|
||||
|
||||
import '../../core/utils/formatters.dart';
|
||||
@@ -99,14 +101,37 @@ class SyncRepositoryImpl implements SyncRepository {
|
||||
required String cashierName,
|
||||
}) async {
|
||||
final today = DateTime.now();
|
||||
final orders = await _store.orders.forBusinessDate(today);
|
||||
|
||||
return ShiftReport.fromTransactions(
|
||||
transactions: orders,
|
||||
// Bills still held locally.
|
||||
final live = ShiftReport.fromTransactions(
|
||||
transactions: await _store.orders.forBusinessDate(today),
|
||||
businessDate: today,
|
||||
terminalId: terminalId,
|
||||
cashierName: cashierName,
|
||||
);
|
||||
|
||||
// Bills already uploaded and deleted survive only as archived totals.
|
||||
final row = await _store.orders.dayArchive(today);
|
||||
if (row == null) return live;
|
||||
|
||||
final payments = (jsonDecode(row['payments_json']! as String)
|
||||
as Map<String, Object?>)
|
||||
.map(
|
||||
(k, v) => MapEntry(
|
||||
PaymentMethod.values.byName(k),
|
||||
(v! as num).toDouble(),
|
||||
),
|
||||
);
|
||||
|
||||
final archived = ShiftReport.fromArchive(
|
||||
row: row,
|
||||
payments: payments,
|
||||
businessDate: today,
|
||||
terminalId: terminalId,
|
||||
cashierName: cashierName,
|
||||
);
|
||||
|
||||
return archived + live;
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -152,10 +177,13 @@ class SyncRepositoryImpl implements SyncRepository {
|
||||
pending.map(_orderToPayload).toList(),
|
||||
);
|
||||
|
||||
onProgress?.call(0.85, 'Marking bills as synced…');
|
||||
onProgress?.call(0.85, 'Clearing uploaded bills from this terminal…');
|
||||
|
||||
// Only what the server confirmed is flipped to 1.
|
||||
await _store.orders.markSynced(accepted);
|
||||
// Only what the server confirmed is archived and removed. Anything it
|
||||
// did not acknowledge stays on disk.
|
||||
final acceptedOrders =
|
||||
pending.where((o) => accepted.contains(o.id)).toList();
|
||||
await _store.orders.archiveAndDelete(acceptedOrders);
|
||||
await _store.refreshUnsyncedCount();
|
||||
|
||||
final rejected = ids.where((id) => !accepted.contains(id)).toList();
|
||||
|
||||
Reference in New Issue
Block a user