import 'dart:async'; import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:fcs/data/services/services.dart'; import 'package:fcs/constants.dart'; import 'package:fcs/domain/entities/fcs_shipment.dart'; import 'package:fcs/pages/main/model/base_model.dart'; import 'package:logging/logging.dart'; import '../../../domain/entities/shipment_type.dart'; import '../../../pagination/paginator_listener.dart'; class FcsShipmentModel extends BaseModel { final log = Logger('FcsShipmentModel'); PaginatorListener? fcsShipments; bool isLoading = false; int selectedIndex = 0; List shipmentTypes = []; StreamSubscription? _shipmentTypeListener; onChanged(int index) { selectedIndex = index; loadFcsShipments(selectedIndex); notifyListeners(); } loadFcsShipments(int index) { if (user == null || !user!.hasFcsShipments()) return; String path = "/$fcs_shipment_collection"; Query col = FirebaseFirestore.instance.collection(path); Query pageQuery = FirebaseFirestore.instance.collection(path); // pending status if (index == 1) { col = col.where("status", isEqualTo: fcs_shipment_pending_status); pageQuery = pageQuery.where("status", isEqualTo: fcs_shipment_pending_status); } // processing status if (index == 2) { col = col.where("status", isEqualTo: fcs_shipment_processed_status); pageQuery = pageQuery.where("status", isEqualTo: fcs_shipment_processed_status); } // shipped status if (index == 3) { col = col.where("status", isEqualTo: fcs_shipment_shipped_status); pageQuery = pageQuery.where("status", isEqualTo: fcs_shipment_shipped_status); } // arrived status if (index == 4) { col = col.where("status", isEqualTo: fcs_shipment_arrived_status); pageQuery = pageQuery.where("status", isEqualTo: fcs_shipment_arrived_status); } // invoiced status if (index == 5) { col = col.where("status", isEqualTo: fcs_shipment_invoiced_status); pageQuery = pageQuery.where("status", isEqualTo: fcs_shipment_invoiced_status); } // canceled status if (index == 6) { col = col.where("status", isEqualTo: fcs_shipment_canceled_status); pageQuery = pageQuery.where("status", isEqualTo: fcs_shipment_canceled_status); } pageQuery = pageQuery.orderBy("shipment_number", descending: true); fcsShipments?.close(); fcsShipments = PaginatorListener( col, pageQuery, (data, id) => FcsShipment.fromMap(data, id), rowPerLoad: 30); } Future> getActiveFcsShipments() async { List fcsShipments = []; try { var snaps = await FirebaseFirestore.instance .collection("/$fcs_shipment_collection") .where("status", isEqualTo: fcs_shipment_processed_status) .get(const GetOptions(source: Source.server)); fcsShipments = snaps.docs.map((documentSnapshot) { var fcs = FcsShipment.fromMap(documentSnapshot.data(), documentSnapshot.id); return fcs; }).toList(); } catch (e) { log.warning("Error!! $e"); } return fcsShipments; } Future getFcsShipment(String id) async { if (id == "") return null; try { var snap = await FirebaseFirestore.instance .collection("/$fcs_shipment_collection") .doc(id) .get(const GetOptions(source: Source.server)); var fcs = FcsShipment.fromMap(snap.data()!, snap.id); return fcs; } catch (e) { log.warning("Error!! $e"); } return null; } Future> getInvoiceFcsShipments() async { List fcsShipments = []; try { var snaps = await FirebaseFirestore.instance .collection("/$fcs_shipment_collection") .where("pending_invoice_user_count", isGreaterThan: 0) .get(const GetOptions(source: Source.server)); fcsShipments = snaps.docs.map((documentSnapshot) { var fcs = FcsShipment.fromMap(documentSnapshot.data(), documentSnapshot.id); return fcs; }).toList(); } catch (e) { log.warning("Error!! $e"); } return fcsShipments; } void initUser(user) { super.initUser(user); _loadShipmentTypes(); } @override logout() async { fcsShipments?.close(); _shipmentTypeListener?.cancel(); shipmentTypes.clear(); } Future _loadShipmentTypes() async { try { _shipmentTypeListener = FirebaseFirestore.instance .collection( "/$config_collection/$setting_doc_id/$shipment_type_collection") .snapshots() .listen((QuerySnapshot snapshot) { shipmentTypes.clear(); shipmentTypes = snapshot.docs.map((documentSnapshot) { var privilege = ShipmentType.fromMap( documentSnapshot.data() as Map, documentSnapshot.id); return privilege; }).toList(); notifyListeners(); }); } catch (e) { log.warning("Error!! $e"); } } Future create(FcsShipment fcsShipment) { return Services.instance.fcsShipmentService.createFcsShipment(fcsShipment); } Future update(FcsShipment fcsShipment) { return Services.instance.fcsShipmentService.updateFcsShipment(fcsShipment); } Future process(String id) { return Services.instance.fcsShipmentService .updateFcsShipmentStatus(id, fcs_shipment_processed_status); } Future ship(String id) { return Services.instance.fcsShipmentService .updateFcsShipmentStatus(id, fcs_shipment_shipped_status); } Future arrive(String id) { return Services.instance.fcsShipmentService .updateFcsShipmentStatus(id, fcs_shipment_arrived_status); } Future invoice(String id) { return Services.instance.fcsShipmentService .updateFcsShipmentStatus(id, fcs_shipment_shipped_status); } Future cancel(String id) { return Services.instance.fcsShipmentService .updateFcsShipmentStatus(id, fcs_shipment_canceled_status); } Future report(FcsShipment fcsShipment) { return Services.instance.fcsShipmentService.report(fcsShipment); } Future> getShipments() async { List fcsShipments = []; try { var snaps = await FirebaseFirestore.instance .collection("/$fcs_shipment_collection") .where("status", whereIn: [ fcs_shipment_processed_status, fcs_shipment_shipped_status, fcs_shipment_arrived_status, fcs_shipment_invoiced_status ]) .where("is_deleted", isEqualTo: false) .orderBy("update_time", descending: true) .limit(shipmentCountForCartonFilter) .get(const GetOptions(source: Source.server)); fcsShipments = snaps.docs.map((documentSnapshot) { var fcs = FcsShipment.fromMap(documentSnapshot.data(), documentSnapshot.id); return fcs; }).toList(); } catch (e) { log.warning("Error!! $e"); } return fcsShipments; } }