Files
fcs/lib/pages/carton/model/carton_model.dart
2024-01-24 16:54:08 +06:30

190 lines
5.8 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/pages/main/model/base_model.dart';
import 'package:fcs/pagination/paginator_listener.dart';
import 'package:logging/logging.dart';
class CartonModel extends BaseModel {
final log = Logger('CartonModel');
PaginatorListener<Carton>? cartonsByFilter;
PaginatorListener<Carton>? getBoxes;
int selectedIndex = 1;
int _selectedIndexFilter = 1;
bool isLoading = false;
List<String> cartonTypes = [
carton_from_packages,
carton_from_cartons,
carton_mix_box
];
List<String> mixBoxTypes = [mix_delivery, mix_pickup];
List<String> cartonTypesInfo = [
carton_from_packages,
carton_from_cartons,
carton_mix_box,
carton_from_shipments,
carton_small_bag
];
int get selectedIndexFilter => _selectedIndexFilter;
set selectedIndexFilter(int index) {
_selectedIndexFilter = index;
_loadPaginationCartons(
_selectedIndexFilter == 1 ? "carton_weight" : "user_name");
notifyListeners();
}
@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();
_selectedIndexFilter = 1;
_loadPaginationCartons(
_selectedIndexFilter == 1 ? "carton_weight" : "user_name");
}
onChanged(int index) {
selectedIndex = index;
loadPaginationBoxes(selectedIndex);
notifyListeners();
}
Future<void> loadPaginationBoxes(int index) async {
if (user == null || !user!.hasCarton()) return;
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]);
}
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);
}
_loadPaginationCartons(String orderName) {
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(orderName, 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);
}
}