fix theme / add history
This commit is contained in:
@@ -3,6 +3,8 @@ import 'package:uuid/uuid.dart';
|
||||
|
||||
import '../database/app_database.dart';
|
||||
import '../models/bar_tab.dart';
|
||||
import '../models/closed_tab.dart';
|
||||
import '../models/closed_tab_item.dart';
|
||||
import '../models/product.dart';
|
||||
import '../models/tab_item.dart';
|
||||
|
||||
@@ -25,7 +27,11 @@ abstract class BarTabService {
|
||||
required int quantity,
|
||||
});
|
||||
|
||||
/// Archives the tab's current items into history and clears them.
|
||||
/// The tab itself stays open under the same customer name.
|
||||
Future<void> closeTab(String tabId);
|
||||
|
||||
Future<List<ClosedTab>> getClosedTabs();
|
||||
}
|
||||
|
||||
class DriftBarTabService implements BarTabService {
|
||||
@@ -61,6 +67,17 @@ class DriftBarTabService implements BarTabService {
|
||||
);
|
||||
}
|
||||
|
||||
ClosedTabItem _mapClosedItemRow(ClosedTabItemRow row) {
|
||||
return ClosedTabItem(
|
||||
id: row.id,
|
||||
closedTabId: row.closedTabId,
|
||||
productId: row.productId,
|
||||
productName: row.productName,
|
||||
quantity: row.quantity,
|
||||
unitPriceInCents: row.unitPriceInCents,
|
||||
);
|
||||
}
|
||||
|
||||
Future<List<TabItem>> _getItemsForTab(String tabId) async {
|
||||
final query = database.select(database.tabItems)
|
||||
..where((item) => item.tabId.equals(tabId))
|
||||
@@ -201,14 +218,83 @@ class DriftBarTabService implements BarTabService {
|
||||
|
||||
@override
|
||||
Future<void> closeTab(String tabId) async {
|
||||
final updateQuery = database.update(database.barTabs)
|
||||
..where((tab) => tab.id.equals(tabId));
|
||||
await database.transaction(() async {
|
||||
final tabQuery = database.select(database.barTabs)
|
||||
..where((tab) => tab.id.equals(tabId));
|
||||
|
||||
await updateQuery.write(
|
||||
BarTabsCompanion(
|
||||
status: const Value('closed'),
|
||||
closedAt: Value(DateTime.now()),
|
||||
),
|
||||
);
|
||||
final tabRow = await tabQuery.getSingleOrNull();
|
||||
|
||||
if (tabRow == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
final items = await _getItemsForTab(tabId);
|
||||
|
||||
if (items.isEmpty) {
|
||||
// Nothing to settle, leave the tab as-is.
|
||||
return;
|
||||
}
|
||||
|
||||
final closedTabId = _uuid.v4();
|
||||
|
||||
await database.into(database.closedTabs).insert(
|
||||
ClosedTabsCompanion.insert(
|
||||
id: closedTabId,
|
||||
originalTabId: tabId,
|
||||
customerName: tabRow.customerName,
|
||||
closedAt: DateTime.now(),
|
||||
),
|
||||
);
|
||||
|
||||
for (final item in items) {
|
||||
await database.into(database.closedTabItems).insert(
|
||||
ClosedTabItemsCompanion.insert(
|
||||
id: _uuid.v4(),
|
||||
closedTabId: closedTabId,
|
||||
productId: item.productId,
|
||||
productName: item.productName,
|
||||
quantity: item.quantity,
|
||||
unitPriceInCents: item.unitPriceInCents,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
final deleteQuery = database.delete(database.tabItems)
|
||||
..where((item) => item.tabId.equals(tabId));
|
||||
|
||||
await deleteQuery.go();
|
||||
|
||||
// Note: tab status/customerName untouched on purpose — the tab
|
||||
// stays open so it keeps showing in the open tabs list.
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<ClosedTab>> getClosedTabs() async {
|
||||
final query = database.select(database.closedTabs)
|
||||
..orderBy([
|
||||
(tab) => OrderingTerm.desc(tab.closedAt),
|
||||
]);
|
||||
|
||||
final closedTabRows = await query.get();
|
||||
|
||||
final closedTabs = <ClosedTab>[];
|
||||
|
||||
for (final row in closedTabRows) {
|
||||
final itemsQuery = database.select(database.closedTabItems)
|
||||
..where((item) => item.closedTabId.equals(row.id));
|
||||
|
||||
final itemRows = await itemsQuery.get();
|
||||
|
||||
closedTabs.add(ClosedTab(
|
||||
id: row.id,
|
||||
originalTabId: row.originalTabId,
|
||||
customerName: row.customerName,
|
||||
closedAt: row.closedAt,
|
||||
items: itemRows.map<ClosedTabItem>(_mapClosedItemRow).toList(),
|
||||
));
|
||||
}
|
||||
|
||||
return closedTabs;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user