156 lines
4.8 KiB
Dart
156 lines
4.8 KiB
Dart
|
|
import 'package:cloud_firestore/cloud_firestore.dart';
|
||
|
|
import 'package:logging/logging.dart';
|
||
|
|
import 'package:fcs/vo/inventory_line.dart';
|
||
|
|
import 'package:fcs/vo/inventory_taking.dart';
|
||
|
|
import 'package:fcs/vo/product.dart';
|
||
|
|
import 'package:fcs/vo/storage.dart';
|
||
|
|
|
||
|
|
import 'base_model.dart';
|
||
|
|
import 'constants.dart';
|
||
|
|
import 'firebase_helper.dart';
|
||
|
|
|
||
|
|
class StorageModel extends BaseModel {
|
||
|
|
final log = Logger('StorageModel');
|
||
|
|
|
||
|
|
List<Storage> storages = [];
|
||
|
|
List<InventoryTaking> inventoryTakings = [];
|
||
|
|
int selectedIndex = 0;
|
||
|
|
DateTime selectedDate = DateTime.now();
|
||
|
|
|
||
|
|
List<Storage> getStorage(String productID) {
|
||
|
|
return storages
|
||
|
|
.where((s) => s.products.any((p) => p.id == productID))
|
||
|
|
.toList();
|
||
|
|
}
|
||
|
|
|
||
|
|
void initUser(user) async {
|
||
|
|
super.initUser(user);
|
||
|
|
if (!user.isOwnerAndAbove() && !user.hasInventory()) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
_loadStorages();
|
||
|
|
_loadInventoryTakings();
|
||
|
|
}
|
||
|
|
|
||
|
|
@override
|
||
|
|
logout() async {
|
||
|
|
storages = [];
|
||
|
|
inventoryTakings = [];
|
||
|
|
}
|
||
|
|
|
||
|
|
String getStorageName(String storageID) {
|
||
|
|
return storages.firstWhere((s) => s.id == storageID).name;
|
||
|
|
}
|
||
|
|
|
||
|
|
void _loadStorages() async {
|
||
|
|
try {
|
||
|
|
getQuerySnapshotF(
|
||
|
|
"/$biz_collection/${setting.okEnergyId}/$storage_collection",
|
||
|
|
user.accountID)
|
||
|
|
.listen((QuerySnapshot snapshot) {
|
||
|
|
storages.clear();
|
||
|
|
storages = snapshot.documents.map((documentSnapshot) {
|
||
|
|
var storage = Storage.fromMap(
|
||
|
|
documentSnapshot.data, documentSnapshot.documentID);
|
||
|
|
loadProducts(storage);
|
||
|
|
return storage;
|
||
|
|
}).toList();
|
||
|
|
notifyListeners();
|
||
|
|
}).onError((e) {
|
||
|
|
log.warning("Error! $e");
|
||
|
|
});
|
||
|
|
} catch (e) {
|
||
|
|
log.warning("Error!! $e");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void _loadInventoryTakings() async {
|
||
|
|
try {
|
||
|
|
String path = "/$biz_collection/${setting.okEnergyId}/$inventory_takings";
|
||
|
|
var startDate = new DateTime(
|
||
|
|
selectedDate.year, selectedDate.month, selectedDate.day, 0, 0, 0);
|
||
|
|
var endDate = new DateTime(
|
||
|
|
selectedDate.year, selectedDate.month, selectedDate.day, 23, 59, 59);
|
||
|
|
|
||
|
|
inventoryTakings.clear();
|
||
|
|
getFilterDateSnapshotF(path, user.accountID, 'date_time', startDate,
|
||
|
|
endDate, 'date_time')
|
||
|
|
.listen((QuerySnapshot snapshot) {
|
||
|
|
inventoryTakings = snapshot.documents.map((documentSnapshot) {
|
||
|
|
var data = InventoryTaking.fromMap(
|
||
|
|
documentSnapshot.data, documentSnapshot.documentID);
|
||
|
|
|
||
|
|
return data;
|
||
|
|
}).toList();
|
||
|
|
notifyListeners();
|
||
|
|
}).onError((e) {
|
||
|
|
log.warning("Error! $e");
|
||
|
|
});
|
||
|
|
} catch (e) {
|
||
|
|
log.warning("Error!! $e");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
loadInventoryLines(InventoryTaking inventoryTaking) async {
|
||
|
|
if (inventoryTaking.linesLoaded) return;
|
||
|
|
var snaps = await getSnapshot(
|
||
|
|
"/$biz_collection/${setting.okEnergyId}/$inventory_takings/${inventoryTaking.id}/$inventory_lines");
|
||
|
|
inventoryTaking.inventoryLines =
|
||
|
|
snaps.documents.map((s) => InventoryLine.fromMap(s.data)).toList();
|
||
|
|
inventoryTaking.linesLoaded = true;
|
||
|
|
notifyListeners();
|
||
|
|
}
|
||
|
|
|
||
|
|
loadProducts(Storage storage) async {
|
||
|
|
if (storage.productsLoaded) return;
|
||
|
|
var snaps = await getSnapshot(
|
||
|
|
"/$biz_collection/${setting.okEnergyId}/$storage_collection/${storage.id}/$product_collection");
|
||
|
|
storage.products = snaps.documents
|
||
|
|
.map((s) => Product.fromMap(s.data, s.documentID))
|
||
|
|
.toList();
|
||
|
|
storage.productsLoaded = true;
|
||
|
|
notifyListeners();
|
||
|
|
}
|
||
|
|
|
||
|
|
Future<void> createStorage(Storage storage) async {
|
||
|
|
await request("/storage", "POST",
|
||
|
|
payload: storage.toMap(), token: await getToken());
|
||
|
|
}
|
||
|
|
|
||
|
|
Future<void> updateStorage(Storage storage) async {
|
||
|
|
await request("/storage", "PUT",
|
||
|
|
payload: storage.toMap(), token: await getToken());
|
||
|
|
}
|
||
|
|
|
||
|
|
Future<void> deleteStorage(String storageID) async {
|
||
|
|
await request("/storage/" + storageID, "DELETE", token: await getToken());
|
||
|
|
}
|
||
|
|
|
||
|
|
Future<void> createInventoryTaking(InventoryTaking inventoryTaking) async {
|
||
|
|
await request("/inventory", "POST",
|
||
|
|
payload: inventoryTaking.toMap(), token: await getToken());
|
||
|
|
}
|
||
|
|
|
||
|
|
void filterDate(DateTime dateTime, int _selectedIndex) async {
|
||
|
|
this.selectedIndex = _selectedIndex;
|
||
|
|
this.selectedDate = dateTime;
|
||
|
|
|
||
|
|
String path = "/$biz_collection/${setting.okEnergyId}/$inventory_takings";
|
||
|
|
var endDate =
|
||
|
|
new DateTime(dateTime.year, dateTime.month, dateTime.day, 23, 59, 59);
|
||
|
|
inventoryTakings.clear();
|
||
|
|
getFilterDateSnapshotF(
|
||
|
|
path, user.accountID, 'date_time', dateTime, endDate, 'date_time')
|
||
|
|
.listen((snapshot) {
|
||
|
|
inventoryTakings = snapshot.documents.map((documentSnapshot) {
|
||
|
|
var data = InventoryTaking.fromMap(
|
||
|
|
documentSnapshot.data, documentSnapshot.documentID);
|
||
|
|
|
||
|
|
return data;
|
||
|
|
}).toList();
|
||
|
|
notifyListeners();
|
||
|
|
});
|
||
|
|
notifyListeners();
|
||
|
|
}
|
||
|
|
}
|