278 lines
7.8 KiB
Dart
278 lines
7.8 KiB
Dart
import 'dart:async';
|
|
import 'dart:io';
|
|
|
|
import 'package:cloud_firestore/cloud_firestore.dart';
|
|
import 'package:fcs/data/services/services.dart';
|
|
import 'package:fcs/constants.dart';
|
|
import 'package:fcs/domain/entities/carton.dart';
|
|
import 'package:fcs/domain/entities/fcs_shipment.dart';
|
|
import 'package:fcs/helpers/shared_pref.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<Carton>? getBoxes;
|
|
|
|
String? filterByStatus;
|
|
User? filterBySender;
|
|
User? filterByConsingee;
|
|
FcsShipment? shipment;
|
|
|
|
List<String> 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();
|
|
}
|
|
|
|
Future<void> _initData() async {
|
|
logout();
|
|
}
|
|
|
|
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<void> 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("update_time", descending: true);
|
|
|
|
getBoxes?.close();
|
|
getBoxes = PaginatorListener<Carton>(
|
|
col, pageQuery, (data, id) => Carton.fromMap(data, id),
|
|
rowPerLoad: 30);
|
|
}
|
|
|
|
Future<List<Carton>> 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<List<Carton>> 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<List<Carton>> 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<Carton> cartons =
|
|
querySnap.docs.map((e) => Carton.fromMap(e.data(), e.id)).toList();
|
|
return cartons;
|
|
}
|
|
|
|
Future<List<Carton>> 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<Carton?> 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<String, dynamic>, snap.id);
|
|
}
|
|
|
|
Future<Carton> createCarton(Carton carton) {
|
|
return Services.instance.cartonService.createCarton(carton);
|
|
}
|
|
|
|
Future<void> updateCarton(Carton carton) {
|
|
return Services.instance.cartonService.updateCarton(carton);
|
|
}
|
|
|
|
Future<void> deleteCarton(Carton carton) {
|
|
return Services.instance.cartonService.deleteCarton(carton);
|
|
}
|
|
|
|
Future<Carton> createMixCarton(Carton carton) {
|
|
return Services.instance.cartonService.createCarton(carton);
|
|
}
|
|
|
|
Future<void> updateMixCarton(Carton carton) {
|
|
return Services.instance.cartonService.updateMixCarton(carton);
|
|
}
|
|
|
|
Future<List<Carton>> searchCarton(String term) async {
|
|
if (term != '') {
|
|
await SharedPref.saveRecentSearch('carton_search', term);
|
|
}
|
|
return Services.instance.cartonService.searchCarton(term);
|
|
}
|
|
|
|
Future<List<Carton>> getCartonsByIds(List<String> cartonIds) async {
|
|
List<Carton> 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<void> uploadCartonImages(
|
|
Carton carton, List<File?> files, List<String?> deletedUrls) async {
|
|
if (deletedUrls.isNotEmpty) {
|
|
for (String? url in deletedUrls) {
|
|
carton.photoUrls.remove(url);
|
|
}
|
|
}
|
|
|
|
List<String> 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);
|
|
});
|
|
|
|
carton.photoUrls.removeWhere((e) => deletedUrls.contains(e));
|
|
}
|
|
try {
|
|
await Services.instance.cartonService.uploadCartonImages(carton);
|
|
} 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);
|
|
}
|
|
}
|