Files
fcs/lib/pages/carton/model/carton_model.dart

280 lines
7.9 KiB
Dart
Raw Normal View History

2020-06-04 01:36:49 +06:30
import 'dart:async';
import 'dart:io';
2020-06-04 01:36:49 +06:30
import 'package:cloud_firestore/cloud_firestore.dart';
2020-10-19 05:13:49 +06:30
import 'package:fcs/data/services/services.dart';
2024-09-22 16:49:59 +06:30
import 'package:fcs/constants.dart';
2020-10-18 02:38:46 +06:30
import 'package:fcs/domain/entities/carton.dart';
2024-02-07 17:26:29 +06:30
import 'package:fcs/domain/entities/fcs_shipment.dart';
import 'package:fcs/helpers/shared_pref.dart';
2020-10-07 02:33:06 +06:30
import 'package:fcs/pages/main/model/base_model.dart';
2021-01-11 19:35:26 +06:30
import 'package:fcs/pagination/paginator_listener.dart';
2020-06-04 01:36:49 +06:30
import 'package:logging/logging.dart';
2024-02-07 17:26:29 +06:30
import '../../../domain/entities/user.dart';
import '../../../helpers/firebase_helper.dart';
import 'package:path/path.dart' as Path;
2024-02-07 17:26:29 +06:30
2020-10-18 02:38:46 +06:30
class CartonModel extends BaseModel {
final log = Logger('CartonModel');
2020-10-16 17:57:58 +06:30
2024-09-22 16:49:59 +06:30
var defaultShipment = FcsShipment(shipmentNumber: "All shipments", id: all);
2024-01-24 16:54:08 +06:30
PaginatorListener<Carton>? getBoxes;
2024-02-07 17:26:29 +06:30
String? filterByStatus;
User? filterBySender;
User? filterByConsingee;
FcsShipment? shipment;
2020-06-04 01:36:49 +06:30
2020-10-20 06:19:10 +06:30
List<String> cartonTypesInfo = [
carton_from_packages,
2021-01-11 19:35:26 +06:30
carton_from_cartons,
2020-10-20 06:19:10 +06:30
carton_mix_box,
2020-10-21 02:59:10 +06:30
carton_from_shipments,
carton_small_bag
2020-10-20 06:19:10 +06:30
];
2020-10-14 13:17:12 +06:30
2021-01-11 19:35:26 +06:30
@override
void privilegeChanged() {
2021-09-10 16:33:52 +06:30
if (user != null || !user!.hasCarton()) {
2021-01-11 19:35:26 +06:30
_initData();
}
}
2024-01-24 16:54:08 +06:30
void initUser(user) {
super.initUser(user);
}
@override
logout() async {
getBoxes?.close();
}
2021-01-11 19:35:26 +06:30
Future<void> _initData() async {
logout();
2024-02-07 17:26:29 +06:30
}
filterCarton(FcsShipment? fcsShipment, User? consignee, User? sender,
String? status) async {
2024-02-07 17:26:29 +06:30
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;
}
2024-02-07 17:26:29 +06:30
loadPaginationCartons();
notifyListeners();
}
filterCartonByShipment(FcsShipment? fcsShipment) {
if (fcsShipment?.id == all) {
shipment = null;
} else {
shipment = fcsShipment;
}
loadPaginationCartons();
notifyListeners();
2021-01-11 19:35:26 +06:30
}
2024-02-07 17:26:29 +06:30
clearFilterSender() async {
filterBySender = null;
loadPaginationCartons();
2024-01-24 16:54:08 +06:30
notifyListeners();
}
2024-02-07 17:26:29 +06:30
clearFilterConsignee() async {
filterByConsingee = null;
loadPaginationCartons();
notifyListeners();
}
Future<void> loadPaginationCartons() async {
2021-09-10 16:33:52 +06:30
if (user == null || !user!.hasCarton()) return;
2024-01-24 16:54:08 +06:30
String path = "/$cartons_collection";
Query col = FirebaseFirestore.instance.collection(path);
Query pageQuery = FirebaseFirestore.instance.collection(path);
2024-02-07 17:26:29 +06:30
if (filterByConsingee != null) {
2024-10-01 18:15:53 +06:30
col = col.where("consignee_user_id", isEqualTo: filterByConsingee!.id);
pageQuery = pageQuery.where("consignee_user_id",
isEqualTo: filterByConsingee!.id);
2024-02-07 17:26:29 +06:30
}
if (filterBySender != null) {
2024-10-01 18:15:53 +06:30
col = col.where("sender_user_id", isEqualTo: filterBySender!.id);
pageQuery =
pageQuery.where("sender_user_id", isEqualTo: filterBySender!.id);
2024-02-07 17:26:29 +06:30
}
if (filterByStatus != null) {
col = col.where("status", isEqualTo: filterByStatus);
pageQuery = pageQuery.where("status", isEqualTo: filterByStatus);
2020-10-16 17:57:58 +06:30
}
2024-01-24 16:54:08 +06:30
2024-02-07 17:26:29 +06:30
if (shipment != null) {
col = col.where("fcs_shipment_id", isEqualTo: shipment!.id);
pageQuery = pageQuery.where("fcs_shipment_id", isEqualTo: shipment!.id);
2024-01-24 16:54:08 +06:30
}
pageQuery = pageQuery.orderBy("update_time", descending: true);
2024-01-24 16:54:08 +06:30
getBoxes?.close();
getBoxes = PaginatorListener<Carton>(
col, pageQuery, (data, id) => Carton.fromMap(data, id),
rowPerLoad: 30);
2020-10-16 17:57:58 +06:30
}
2020-10-18 02:38:46 +06:30
Future<List<Carton>> getCartons(String shipmentID) async {
String path = "/$cartons_collection";
2021-09-10 16:33:52 +06:30
var querySnap = await FirebaseFirestore.instance
2020-10-18 02:38:46 +06:30
.collection(path)
.where("shipment_id", isEqualTo: shipmentID)
2021-09-10 16:33:52 +06:30
.get();
return querySnap.docs.map((e) => Carton.fromMap(e.data(), e.id)).toList();
2020-10-18 02:38:46 +06:30
}
2020-10-19 05:13:49 +06:30
2020-10-20 06:19:10 +06:30
Future<List<Carton>> getCartonsByFcsShipment(String fcsShipmentID) async {
String path = "/$cartons_collection";
2021-09-10 16:33:52 +06:30
var querySnap = await FirebaseFirestore.instance
2020-10-20 06:19:10 +06:30
.collection(path)
.where("fcs_shipment_id", isEqualTo: fcsShipmentID)
.where("is_deleted", isEqualTo: false)
2021-09-10 16:33:52 +06:30
.get();
return querySnap.docs.map((e) => Carton.fromMap(e.data(), e.id)).toList();
2020-10-20 06:19:10 +06:30
}
2020-10-22 04:14:53 +06:30
Future<List<Carton>> getCartonsForInvoice(
String fcsShipmentID, String userID) async {
String path = "/$cartons_collection";
2021-09-10 16:33:52 +06:30
var querySnap = await FirebaseFirestore.instance
2020-10-22 04:14:53 +06:30
.collection(path)
2020-10-24 06:14:07 +06:30
.where("fcs_shipment_id", isEqualTo: fcsShipmentID)
2020-10-22 04:14:53 +06:30
.where("user_id", isEqualTo: userID)
.where("is_deleted", isEqualTo: false)
.where("is_invoiced", isEqualTo: false)
2021-09-10 16:33:52 +06:30
.get();
List<Carton> cartons =
querySnap.docs.map((e) => Carton.fromMap(e.data(), e.id)).toList();
2020-10-24 06:14:07 +06:30
return cartons;
2020-10-22 04:14:53 +06:30
}
2020-10-21 02:59:10 +06:30
Future<List<Carton>> getMixCartonsByFcsShipment(String fcsShipmentID) async {
String path = "/$cartons_collection";
2021-09-10 16:33:52 +06:30
var querySnap = await FirebaseFirestore.instance
2020-10-21 02:59:10 +06:30
.collection(path)
.where("fcs_shipment_id", isEqualTo: fcsShipmentID)
.where("carton_type", isEqualTo: carton_mix_box)
.where("is_deleted", isEqualTo: false)
2021-09-10 16:33:52 +06:30
.get();
return querySnap.docs.map((e) => Carton.fromMap(e.data(), e.id)).toList();
2020-10-21 02:59:10 +06:30
}
2024-02-09 13:35:32 +06:30
Future<Carton?> getCarton(String id) async {
2020-10-20 06:19:10 +06:30
String path = "/$cartons_collection";
2021-09-10 16:33:52 +06:30
var snap = await FirebaseFirestore.instance.collection(path).doc(id).get();
return Carton.fromMap(snap.data() as Map<String, dynamic>, snap.id);
2020-10-20 06:19:10 +06:30
}
2021-01-07 18:15:39 +06:30
Future<Carton> createCarton(Carton carton) {
2020-10-19 05:13:49 +06:30
return Services.instance.cartonService.createCarton(carton);
}
Future<void> updateCarton(Carton carton) {
return Services.instance.cartonService.updateCarton(carton);
}
2020-10-20 06:19:10 +06:30
Future<void> deleteCarton(Carton carton) {
return Services.instance.cartonService.deleteCarton(carton);
}
2020-12-10 20:06:15 +06:30
Future<Carton> createMixCarton(Carton carton) {
2024-10-01 18:15:53 +06:30
return Services.instance.cartonService.createMixCarton(carton);
}
Future<void> updateMixCarton(Carton carton) {
return Services.instance.cartonService.updateMixCarton(carton);
}
2020-12-10 20:06:15 +06:30
Future<List<Carton>> searchCarton(String term) async {
if (term != '') {
await SharedPref.saveRecentSearch('carton_search', term);
}
2021-01-09 19:11:47 +06:30
return Services.instance.cartonService.searchCarton(term);
}
2024-02-09 13:35:32 +06:30
Future<List<Carton>> getCartonsByIds(List<String> cartonIds) async {
2024-02-09 13:35:32 +06:30
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);
}
2020-06-04 01:36:49 +06:30
}