This commit is contained in:
2026-07-27 21:32:03 +02:00
parent 160b4e4cda
commit 77c1747b40
23 changed files with 1392 additions and 87 deletions
+21 -9
View File
@@ -8,9 +8,11 @@ import '../services/product_service.dart';
class BarScreenViewModel extends ChangeNotifier {
final BarTabService barTabService;
final ProductService productService;
BarScreenViewModel({
required this.barTabService,
required this.productService,
});
List<BarTab> _tabs = [];
@@ -74,9 +76,7 @@ class BarScreenViewModel extends ChangeNotifier {
}
Future<void> createTab(String customerName) async {
final tab = await barTabService.createTab(
customerName: customerName,
);
final tab = await barTabService.createTab(customerName: customerName);
_selectedTabId = tab.id;
await _reloadTabs();
@@ -87,23 +87,34 @@ class BarScreenViewModel extends ChangeNotifier {
if (tab == null) return;
if (product.stockQuantity <= 0) {
throw Exception('Product is out of stock.');
}
await barTabService.addProductToTab(
tabId: tab.id,
product: product,
);
await productService.decreaseStock(product.id, 1);
await _reloadTabs();
}
Future<void> changeItemQuantity(
TabItem item,
int quantity,
) async {
Future<void> changeItemQuantity(TabItem item, int quantity) async {
final difference = quantity - item.quantity;
await barTabService.updateTabItemQuantity(
tabItemId: item.id,
quantity: quantity,
);
if (difference > 0) {
await productService.decreaseStock(item.productId, difference);
} else if (difference < 0) {
await productService.increaseStock(item.productId, -difference);
}
await _reloadTabs();
}
@@ -126,10 +137,11 @@ class BarScreenViewModel extends ChangeNotifier {
Future<void> _reloadTabs() async {
_tabs = await barTabService.getOpenTabs();
if (_selectedTabId == null || !_tabs.any((tab) => tab.id == _selectedTabId)) {
if (_selectedTabId == null ||
!_tabs.any((tab) => tab.id == _selectedTabId)) {
_selectedTabId = _tabs.isEmpty ? null : _tabs.first.id;
}
notifyListeners();
}
}
}