import 'dart:async'; import 'dart:io'; 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/domain/entities/fcs_shipment.dart'; import 'package:fcs/pages/main/model/base_model.dart'; import 'package:fcs/pagination/paginator_listener.dart'; import 'package:logging/logging.dart'; import '../../../domain/entities/user.dart'; import '../../../helpers/firebase_helper.dart'; import 'package:path/path.dart' as Path; class CartonModel extends BaseModel { final log = Logger('CartonModel'); var defaultShipment =FcsShipment(shipmentNumber: "All shipments", id: all); PaginatorListener? cartonsByFilter; PaginatorListener? getBoxes; String? filterByStatus; User? filterBySender; User? filterByConsingee; FcsShipment? shipment; List cartonTypesInfo = [ carton_from_packages, carton_from_cartons, carton_mix_box, carton_from_shipments, carton_small_bag ]; @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(); _loadPaginationCartons(); } filterCarton(FcsShipment? fcsShipment, User? consignee, User? sender, String? status) async { filterByStatus = status; if (status == all_status) { filterByStatus = null; } else { filterByStatus = status; } if (consignee?.id == all) { filterByConsingee = null; } else { filterByConsingee = consignee; } if (sender?.id == all) { filterBySender = null; } else { filterBySender = sender; } if (fcsShipment?.id == all) { shipment = null; } else { shipment = fcsShipment; } loadPaginationCartons(); notifyListeners(); } filterCartonByShipment(FcsShipment? fcsShipment) { if (fcsShipment?.id == all) { shipment = null; } else { shipment = fcsShipment; } loadPaginationCartons(); notifyListeners(); } clearFilterSender() async { filterBySender = null; loadPaginationCartons(); notifyListeners(); } clearFilterConsignee() async { filterByConsingee = null; loadPaginationCartons(); notifyListeners(); } Future loadPaginationCartons() async { if (user == null || !user!.hasCarton()) return; String path = "/$cartons_collection"; Query col = FirebaseFirestore.instance.collection(path); Query pageQuery = FirebaseFirestore.instance.collection(path); if (filterByConsingee != null) { col = col.where("user_id", isEqualTo: filterByConsingee!.id); pageQuery = pageQuery.where("user_id", isEqualTo: filterByConsingee!.id); } if (filterBySender != null) { col = col.where("sender_id", isEqualTo: filterBySender!.id); pageQuery = pageQuery.where("sender_id", isEqualTo: filterBySender!.id); } if (filterByStatus != null) { col = col.where("status", isEqualTo: filterByStatus); pageQuery = pageQuery.where("status", isEqualTo: filterByStatus); } if (shipment != null) { col = col.where("fcs_shipment_id", isEqualTo: shipment!.id); pageQuery = pageQuery.where("fcs_shipment_id", isEqualTo: shipment!.id); } pageQuery = pageQuery.orderBy("created_at", descending: true); getBoxes?.close(); getBoxes = PaginatorListener( col, pageQuery, (data, id) => Carton.fromMap(data, id), rowPerLoad: 30); } _loadPaginationCartons() { 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("created_at", 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); } Future> getCartonsByIds(List cartonIds) async { List cartons = []; try { for (var e in cartonIds) { Carton? c = await getCarton(e); if (c != null) { cartons.add(c); } } } catch (e) { log.warning("Error!! $e"); } return cartons; } Future uploadCartonImages( Carton carton, List files, List deletedUrls) async { if (deletedUrls.isNotEmpty) { for (String? url in deletedUrls) { carton.photoUrls.remove(url); } } List uploadedURL = []; if (files.isNotEmpty) { var count = (carton.photoUrls.length) + files.length - (deletedUrls.length); if (count > uploadPhotoLimit) throw Exception("Exceed number of file upload"); carton.photoUrls = carton.photoUrls; String path = Path.join(carton_files_path); uploadedURL = await uploadFiles(path, files); uploadedURL.forEach((url) { carton.photoUrls.add(url); }); } try { // await Services.instance.packageService.updateReceiving(package); } catch (e) { // delete newly uploaded photos if fails try { deleteStorageFromUrls(uploadedURL); carton.photoUrls.removeWhere((i) => uploadedURL.contains(i)); } catch (e) {} throw e; } return deleteStorageFromUrls(deletedUrls); } }