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/domain/entities/shipment_port.dart'; import 'package:fcs/pages/main/model/base_model.dart'; import 'package:logging/logging.dart'; import '../../../domain/entities/shipment_consignee.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 = []; List shipmentConsingees = []; List shipmentPorts = []; StreamSubscription? _shipmentTypeListener; StreamSubscription? _shipmentConsigneeListener; StreamSubscription? _shipmentPortListener; ShipmentType? getShipmentType(id) { var list = shipmentTypes.where((e) => e.id == id).toList(); return list.isNotEmpty ? list.first : null; } ShipmentConsignee? getShipmentConsignee(id) { var list = shipmentConsingees.where((e) => e.id == id).toList(); return list.isNotEmpty ? list.first : null; } ShipmentPort? getShipmentPort(id) { var list = shipmentPorts.where((e) => e.id == id).toList(); return list.isNotEmpty ? list.first : null; } 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); } // processed 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("update_time", 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(); _loadShipmentConsignees(); _loadShipmentPorts(); } @override logout() async { fcsShipments?.close(); _shipmentTypeListener?.cancel(); _shipmentConsigneeListener?.cancel(); _shipmentPortListener?.cancel(); shipmentTypes.clear(); shipmentConsingees.clear(); shipmentPorts.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 type = ShipmentType.fromMap( documentSnapshot.data() as Map, documentSnapshot.id); return type; }).toList(); notifyListeners(); }); } catch (e) { log.warning("Error!! $e"); } } Future _loadShipmentConsignees() async { try { _shipmentConsigneeListener = FirebaseFirestore.instance .collection( "/$config_collection/$setting_doc_id/$shipment_consignee_collection") .snapshots() .listen((QuerySnapshot snapshot) { shipmentConsingees.clear(); shipmentConsingees = snapshot.docs.map((documentSnapshot) { var consignee = ShipmentConsignee.fromMap( documentSnapshot.data() as Map, documentSnapshot.id); return consignee; }).toList(); notifyListeners(); }); } catch (e) { log.warning("Error!! $e"); } } Future _loadShipmentPorts() async { try { _shipmentPortListener = FirebaseFirestore.instance .collection( "/$config_collection/$setting_doc_id/$shipment_port_collection") .snapshots() .listen((QuerySnapshot snapshot) { shipmentPorts.clear(); shipmentPorts = snapshot.docs.map((documentSnapshot) { var port = ShipmentPort.fromMap( documentSnapshot.data() as Map, documentSnapshot.id); return port; }).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_invoiced_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; } }