67 lines
1.8 KiB
Dart
67 lines
1.8 KiB
Dart
import 'package:flutter/foundation.dart';
|
|
import 'package:sentry_flutter/sentry_flutter.dart';
|
|
|
|
import '../models/product.dart';
|
|
import '../services/product_service.dart';
|
|
|
|
class InventoryViewModel extends ChangeNotifier {
|
|
final ProductService productService;
|
|
|
|
InventoryViewModel({required this.productService});
|
|
|
|
List<Product> _products = [];
|
|
|
|
List<Product> get products => _products;
|
|
|
|
Future<void> load() async {
|
|
try {
|
|
_products = await productService.getProducts();
|
|
notifyListeners();
|
|
} catch (e, stack) {
|
|
debugPrint('InventoryViewModel: load error: $e');
|
|
Sentry.captureException(e, stackTrace: stack);
|
|
rethrow;
|
|
}
|
|
}
|
|
|
|
Future<void> decreaseStock(String productId, int amount) async {
|
|
try {
|
|
await productService.decreaseStock(productId, amount);
|
|
|
|
final index = _products.indexWhere((product) => product.id == productId);
|
|
|
|
if (index != -1) {
|
|
_products[index] = _products[index].copyWith(
|
|
stockQuantity: _products[index].stockQuantity - amount,
|
|
);
|
|
}
|
|
|
|
notifyListeners();
|
|
} catch (e, stack) {
|
|
debugPrint('InventoryViewModel: decreaseStock error: $e');
|
|
Sentry.captureException(e, stackTrace: stack);
|
|
rethrow;
|
|
}
|
|
}
|
|
|
|
Future<void> increaseStock(String productId, int amount) async {
|
|
try {
|
|
await productService.increaseStock(productId, amount);
|
|
|
|
final index = _products.indexWhere((product) => product.id == productId);
|
|
|
|
if (index != -1) {
|
|
_products[index] = _products[index].copyWith(
|
|
stockQuantity: _products[index].stockQuantity + amount,
|
|
);
|
|
}
|
|
|
|
notifyListeners();
|
|
} catch (e, stack) {
|
|
debugPrint('InventoryViewModel: increaseStock error: $e');
|
|
Sentry.captureException(e, stackTrace: stack);
|
|
rethrow;
|
|
}
|
|
}
|
|
}
|