import 'dart:async'; import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:fcs/data/services/services.dart'; import 'package:fcs/domain/constants.dart'; import 'package:fcs/domain/entities/carton.dart'; import 'package:fcs/pages/main/model/base_model.dart'; import 'package:fcs/pagination/paginator_listener.dart'; import 'package:logging/logging.dart'; class CartonModel extends BaseModel { final log = Logger('CartonModel'); PaginatorListener? cartonsByFilter; PaginatorListener? getBoxes; int selectedIndex = 1; int _selectedIndexFilter = 1; bool isLoading = false; List cartonTypes = [ carton_from_packages, carton_from_cartons, carton_mix_box ]; List mixBoxTypes = [mix_delivery, mix_pickup]; List cartonTypesInfo = [ carton_from_packages, carton_from_cartons, carton_mix_box, carton_from_shipments, carton_small_bag ]; int get selectedIndexFilter => _selectedIndexFilter; set selectedIndexFilter(int index) { _selectedIndexFilter = index; _loadPaginationCartons( _selectedIndexFilter == 1 ? "carton_weight" : "user_name"); notifyListeners(); } @override void privilegeChanged() { if (user != null || !user!.hasCarton()) { _initData(); } } void initUser(user) { super.initUser(user); } @override logout() async { getBoxes?.close(); cartonsByFilter?.close(); } Future _initData() async { logout(); _selectedIndexFilter = 1; _loadPaginationCartons( _selectedIndexFilter == 1 ? "carton_weight" : "user_name"); } onChanged(int index) { selectedIndex = index; loadPaginationBoxes(selectedIndex); notifyListeners(); } Future loadPaginationBoxes(int index) async { if (user == null || !user!.hasCarton()) return; String path = "/$cartons_collection"; Query col = FirebaseFirestore.instance.collection(path); Query pageQuery = FirebaseFirestore.instance.collection(path); if (index == 1) { col = col.where("status", whereIn: [carton_packed_status, carton_shipped_status]); pageQuery = pageQuery.where("status", whereIn: [carton_packed_status, carton_shipped_status]); } if (index == 2) { col = col.where("is_delivered", isEqualTo: true); pageQuery = pageQuery.where("is_delivered", isEqualTo: true); } pageQuery = pageQuery.orderBy("created_at", descending: true); getBoxes?.close(); getBoxes = PaginatorListener( col, pageQuery, (data, id) => Carton.fromMap(data, id), rowPerLoad: 30); } _loadPaginationCartons(String orderName) { if (user == null || !user!.hasCarton()) return null; String path = "/$cartons_collection"; Query col = FirebaseFirestore.instance.collection(path).where("carton_type", whereIn: [ carton_from_packages, carton_from_cartons ]).where("status", isEqualTo: carton_packed_status); Query pageQuery = FirebaseFirestore.instance .collection(path) .where("carton_type", whereIn: [carton_from_packages, carton_from_cartons]) .where("status", isEqualTo: carton_packed_status) .orderBy(orderName, descending: true); cartonsByFilter?.close(); cartonsByFilter = PaginatorListener( col, pageQuery, (data, id) => Carton.fromMap(data, id), rowPerLoad: 30); } Future> getCartons(String shipmentID) async { String path = "/$cartons_collection"; var querySnap = await FirebaseFirestore.instance .collection(path) .where("shipment_id", isEqualTo: shipmentID) .get(); return querySnap.docs.map((e) => Carton.fromMap(e.data(), e.id)).toList(); } Future> getCartonsByFcsShipment(String fcsShipmentID) async { String path = "/$cartons_collection"; var querySnap = await FirebaseFirestore.instance .collection(path) .where("fcs_shipment_id", isEqualTo: fcsShipmentID) .where("is_deleted", isEqualTo: false) .get(); return querySnap.docs.map((e) => Carton.fromMap(e.data(), e.id)).toList(); } Future> getCartonsForInvoice( String fcsShipmentID, String userID) async { String path = "/$cartons_collection"; var querySnap = await FirebaseFirestore.instance .collection(path) .where("fcs_shipment_id", isEqualTo: fcsShipmentID) .where("user_id", isEqualTo: userID) .where("is_deleted", isEqualTo: false) .where("is_invoiced", isEqualTo: false) .get(); List cartons = querySnap.docs.map((e) => Carton.fromMap(e.data(), e.id)).toList(); return cartons; } Future> getMixCartonsByFcsShipment(String fcsShipmentID) async { String path = "/$cartons_collection"; var querySnap = await FirebaseFirestore.instance .collection(path) .where("fcs_shipment_id", isEqualTo: fcsShipmentID) .where("carton_type", isEqualTo: carton_mix_box) .where("is_deleted", isEqualTo: false) .get(); return querySnap.docs.map((e) => Carton.fromMap(e.data(), e.id)).toList(); } Future getCarton(String id) async { String path = "/$cartons_collection"; var snap = await FirebaseFirestore.instance.collection(path).doc(id).get(); return Carton.fromMap(snap.data() as Map, snap.id); } Future createCarton(Carton carton) { return Services.instance.cartonService.createCarton(carton); } Future updateCarton(Carton carton) { return Services.instance.cartonService.updateCarton(carton); } Future deleteCarton(Carton carton) { return Services.instance.cartonService.deleteCarton(carton); } Future> searchCarton(String term) async { return Services.instance.cartonService.searchCarton(term); } }