2020-06-04 01:36:49 +06:30
|
|
|
import 'dart:async';
|
|
|
|
|
|
|
|
|
|
import 'package:cloud_firestore/cloud_firestore.dart';
|
2020-10-19 05:13:49 +06:30
|
|
|
import 'package:fcs/data/services/services.dart';
|
2020-10-14 13:17:12 +06:30
|
|
|
import 'package:fcs/domain/constants.dart';
|
2020-10-18 02:38:46 +06:30
|
|
|
import 'package:fcs/domain/entities/carton.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';
|
|
|
|
|
|
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-01-24 16:54:08 +06:30
|
|
|
PaginatorListener<Carton>? cartonsByFilter;
|
|
|
|
|
PaginatorListener<Carton>? getBoxes;
|
|
|
|
|
|
|
|
|
|
int selectedIndex = 1;
|
2021-01-11 19:35:26 +06:30
|
|
|
int _selectedIndexFilter = 1;
|
2020-10-16 17:57:58 +06:30
|
|
|
bool isLoading = false;
|
2020-06-04 01:36:49 +06:30
|
|
|
|
2021-01-09 19:11:47 +06:30
|
|
|
List<String> cartonTypes = [
|
|
|
|
|
carton_from_packages,
|
2021-01-11 19:35:26 +06:30
|
|
|
carton_from_cartons,
|
2021-01-09 19:11:47 +06:30
|
|
|
carton_mix_box
|
|
|
|
|
];
|
2021-01-08 17:13:51 +06:30
|
|
|
List<String> mixBoxTypes = [mix_delivery, mix_pickup];
|
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
|
|
|
|
2024-01-24 16:54:08 +06:30
|
|
|
int get selectedIndexFilter => _selectedIndexFilter;
|
2020-10-16 17:57:58 +06:30
|
|
|
|
2021-01-11 19:35:26 +06:30
|
|
|
set selectedIndexFilter(int index) {
|
|
|
|
|
_selectedIndexFilter = index;
|
2024-01-23 17:48:34 +06:30
|
|
|
_loadPaginationCartons(
|
2021-01-11 19:35:26 +06:30
|
|
|
_selectedIndexFilter == 1 ? "carton_weight" : "user_name");
|
|
|
|
|
notifyListeners();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@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();
|
|
|
|
|
cartonsByFilter?.close();
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-11 19:35:26 +06:30
|
|
|
Future<void> _initData() async {
|
|
|
|
|
logout();
|
|
|
|
|
_selectedIndexFilter = 1;
|
2024-01-23 17:48:34 +06:30
|
|
|
_loadPaginationCartons(
|
2021-01-11 19:35:26 +06:30
|
|
|
_selectedIndexFilter == 1 ? "carton_weight" : "user_name");
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-24 16:54:08 +06:30
|
|
|
onChanged(int index) {
|
|
|
|
|
selectedIndex = index;
|
|
|
|
|
loadPaginationBoxes(selectedIndex);
|
|
|
|
|
notifyListeners();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<void> loadPaginationBoxes(int index) 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);
|
|
|
|
|
|
|
|
|
|
if (index == 1) {
|
|
|
|
|
col = col.where("status",
|
|
|
|
|
whereIn: [carton_packed_status, carton_shipped_status]);
|
|
|
|
|
pageQuery = pageQuery.where("status",
|
|
|
|
|
whereIn: [carton_packed_status, carton_shipped_status]);
|
2020-10-16 17:57:58 +06:30
|
|
|
}
|
2024-01-24 16:54:08 +06:30
|
|
|
|
|
|
|
|
if (index == 2) {
|
|
|
|
|
col = col.where("is_delivered", isEqualTo: true);
|
|
|
|
|
pageQuery = pageQuery.where("is_delivered", isEqualTo: true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pageQuery = pageQuery.orderBy("created_at", descending: true);
|
|
|
|
|
|
|
|
|
|
getBoxes?.close();
|
|
|
|
|
getBoxes = PaginatorListener<Carton>(
|
|
|
|
|
col, pageQuery, (data, id) => Carton.fromMap(data, id),
|
|
|
|
|
rowPerLoad: 30);
|
2020-10-16 17:57:58 +06:30
|
|
|
}
|
|
|
|
|
|
2024-01-23 17:48:34 +06:30
|
|
|
_loadPaginationCartons(String orderName) {
|
2021-09-10 16:33:52 +06:30
|
|
|
if (user == null || !user!.hasCarton()) return null;
|
2021-01-11 19:35:26 +06:30
|
|
|
String path = "/$cartons_collection";
|
|
|
|
|
|
2024-01-23 17:48:34 +06:30
|
|
|
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(orderName, descending: true);
|
|
|
|
|
|
|
|
|
|
cartonsByFilter?.close();
|
|
|
|
|
cartonsByFilter = PaginatorListener<Carton>(
|
|
|
|
|
col, pageQuery, (data, id) => Carton.fromMap(data, id),
|
|
|
|
|
rowPerLoad: 30);
|
2021-01-09 19:11:47 +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
|
|
|
}
|
|
|
|
|
|
2020-10-20 06:19:10 +06:30
|
|
|
Future<Carton> getCarton(String id) async {
|
|
|
|
|
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<List<Carton>> searchCarton(String term) async {
|
2021-01-09 19:11:47 +06:30
|
|
|
return Services.instance.cartonService.searchCarton(term);
|
|
|
|
|
}
|
2020-06-04 01:36:49 +06:30
|
|
|
}
|