230 lines
6.6 KiB
Dart
230 lines
6.6 KiB
Dart
import 'dart:async';
|
|
|
|
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';
|
|
|
|
class CartonModel extends BaseModel {
|
|
final log = Logger('CartonModel');
|
|
|
|
PaginatorListener<Carton>? cartonsByFilter;
|
|
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();
|
|
cartonsByFilter?.close();
|
|
}
|
|
|
|
Future<void> _initData() async {
|
|
logout();
|
|
|
|
_loadPaginationCartons();
|
|
}
|
|
|
|
filterCarton(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;
|
|
}
|
|
|
|
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("created_at", descending: true);
|
|
|
|
getBoxes?.close();
|
|
getBoxes = PaginatorListener<Carton>(
|
|
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<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<List<Carton>> searchCarton(String term) async {
|
|
return Services.instance.cartonService.searchCarton(term);
|
|
}
|
|
}
|