Files
fcs/lib/pages/fcs_shipment/model/fcs_shipment_model.dart

233 lines
7.1 KiB
Dart
Raw Normal View History

2020-10-08 03:32:52 +06:30
import 'dart:async';
import 'package:cloud_firestore/cloud_firestore.dart';
2020-10-07 14:42:07 +06:30
import 'package:fcs/data/services/services.dart';
2024-09-22 16:49:59 +06:30
import 'package:fcs/constants.dart';
2020-10-07 14:42:07 +06:30
import 'package:fcs/domain/entities/fcs_shipment.dart';
2020-10-07 02:33:06 +06:30
import 'package:fcs/pages/main/model/base_model.dart';
2020-10-08 03:32:52 +06:30
import 'package:logging/logging.dart';
2020-06-01 14:24:45 +06:30
2024-03-01 17:27:56 +06:30
import '../../../domain/entities/shipment_type.dart';
2024-01-24 16:54:08 +06:30
import '../../../pagination/paginator_listener.dart';
2020-10-07 02:33:06 +06:30
class FcsShipmentModel extends BaseModel {
2020-10-08 03:32:52 +06:30
final log = Logger('FcsShipmentModel');
2024-01-24 16:54:08 +06:30
PaginatorListener<FcsShipment>? fcsShipments;
2020-10-15 19:40:03 +06:30
bool isLoading = false;
2024-01-30 17:24:28 +06:30
int selectedIndex = 0;
2020-10-15 19:40:03 +06:30
2024-03-01 17:27:56 +06:30
List<ShipmentType> shipmentTypes = [];
StreamSubscription<QuerySnapshot>? _shipmentTypeListener;
2024-01-24 16:54:08 +06:30
onChanged(int index) {
selectedIndex = index;
loadFcsShipments(selectedIndex);
notifyListeners();
2020-06-01 14:24:45 +06:30
}
2024-01-24 16:54:08 +06:30
loadFcsShipments(int index) {
if (user == null || !user!.hasFcsShipments()) return;
2020-10-16 11:05:38 +06:30
2024-01-24 16:54:08 +06:30
String path = "/$fcs_shipment_collection";
Query col = FirebaseFirestore.instance.collection(path);
Query pageQuery = FirebaseFirestore.instance.collection(path);
2020-10-16 11:05:38 +06:30
2024-01-31 17:54:22 +06:30
// pending status
2024-01-24 16:54:08 +06:30
if (index == 1) {
2024-01-31 17:54:22 +06:30
col = col.where("status", isEqualTo: fcs_shipment_pending_status);
2024-01-24 16:54:08 +06:30
pageQuery =
2024-01-31 17:54:22 +06:30
pageQuery.where("status", isEqualTo: fcs_shipment_pending_status);
2020-10-08 03:32:52 +06:30
}
2020-06-01 14:24:45 +06:30
2024-01-31 17:54:22 +06:30
// processing status
2024-01-24 16:54:08 +06:30
if (index == 2) {
2024-09-22 16:49:59 +06:30
col = col.where("status", isEqualTo: fcs_shipment_processed_status);
2024-01-31 17:54:22 +06:30
pageQuery =
2024-09-22 16:49:59 +06:30
pageQuery.where("status", isEqualTo: fcs_shipment_processed_status);
2024-01-31 17:54:22 +06:30
}
// shipped status
if (index == 3) {
2024-01-24 16:54:08 +06:30
col = col.where("status", isEqualTo: fcs_shipment_shipped_status);
pageQuery =
pageQuery.where("status", isEqualTo: fcs_shipment_shipped_status);
}
2020-10-15 19:40:03 +06:30
2024-01-31 17:54:22 +06:30
// arrived status
if (index == 4) {
col = col.where("status", isEqualTo: fcs_shipment_arrived_status);
pageQuery =
pageQuery.where("status", isEqualTo: fcs_shipment_arrived_status);
}
// invoiced status
if (index == 5) {
col = col.where("status", isEqualTo: fcs_shipment_invoiced_status);
pageQuery =
pageQuery.where("status", isEqualTo: fcs_shipment_invoiced_status);
}
// canceled status
if (index == 6) {
col = col.where("status", isEqualTo: fcs_shipment_canceled_status);
pageQuery =
pageQuery.where("status", isEqualTo: fcs_shipment_canceled_status);
}
2024-01-24 16:54:08 +06:30
pageQuery = pageQuery.orderBy("shipment_number", descending: true);
2020-10-15 19:40:03 +06:30
2024-01-24 16:54:08 +06:30
fcsShipments?.close();
fcsShipments = PaginatorListener<FcsShipment>(
col, pageQuery, (data, id) => FcsShipment.fromMap(data, id),
rowPerLoad: 30);
2020-10-15 19:40:03 +06:30
}
2020-10-19 05:13:49 +06:30
Future<List<FcsShipment>> getActiveFcsShipments() async {
List<FcsShipment> fcsShipments = [];
try {
2021-09-10 16:48:20 +06:30
var snaps = await FirebaseFirestore.instance
2020-10-19 05:13:49 +06:30
.collection("/$fcs_shipment_collection")
// .where("status", isEqualTo: fcs_shipment_processed_status)
2021-09-10 16:48:20 +06:30
.get(const GetOptions(source: Source.server));
fcsShipments = snaps.docs.map((documentSnapshot) {
2021-09-13 09:51:55 +06:30
var fcs =
FcsShipment.fromMap(documentSnapshot.data(), documentSnapshot.id);
2020-10-19 05:13:49 +06:30
return fcs;
}).toList();
} catch (e) {
log.warning("Error!! $e");
}
return fcsShipments;
}
2021-09-10 16:48:20 +06:30
Future<FcsShipment?> getFcsShipment(String id) async {
2024-02-09 17:10:19 +06:30
if (id == "") return null;
2020-10-19 14:02:34 +06:30
try {
2021-09-10 16:48:20 +06:30
var snap = await FirebaseFirestore.instance
2020-10-19 14:02:34 +06:30
.collection("/$fcs_shipment_collection")
2021-09-10 16:48:20 +06:30
.doc(id)
.get(const GetOptions(source: Source.server));
2021-09-13 14:24:04 +06:30
var fcs = FcsShipment.fromMap(snap.data()!, snap.id);
2020-10-19 14:02:34 +06:30
return fcs;
} catch (e) {
log.warning("Error!! $e");
}
return null;
}
2020-10-22 04:14:53 +06:30
Future<List<FcsShipment>> getInvoiceFcsShipments() async {
List<FcsShipment> fcsShipments = [];
try {
2021-09-10 16:48:20 +06:30
var snaps = await FirebaseFirestore.instance
2020-10-22 04:14:53 +06:30
.collection("/$fcs_shipment_collection")
.where("pending_invoice_user_count", isGreaterThan: 0)
2021-09-10 16:48:20 +06:30
.get(const GetOptions(source: Source.server));
fcsShipments = snaps.docs.map((documentSnapshot) {
2021-10-11 17:09:47 +06:30
var fcs =
FcsShipment.fromMap(documentSnapshot.data(), documentSnapshot.id);
2020-10-22 04:14:53 +06:30
return fcs;
}).toList();
} catch (e) {
log.warning("Error!! $e");
}
return fcsShipments;
}
2020-06-01 14:24:45 +06:30
void initUser(user) {
super.initUser(user);
2024-03-01 17:27:56 +06:30
_loadShipmentTypes();
2020-06-01 14:24:45 +06:30
}
@override
logout() async {
2024-01-24 16:54:08 +06:30
fcsShipments?.close();
2024-03-01 17:27:56 +06:30
_shipmentTypeListener?.cancel();
shipmentTypes.clear();
}
Future<void> _loadShipmentTypes() async {
try {
_shipmentTypeListener = FirebaseFirestore.instance
.collection(
"/$config_collection/$setting_doc_id/$shipment_type_collection")
.snapshots()
.listen((QuerySnapshot snapshot) {
shipmentTypes.clear();
shipmentTypes = snapshot.docs.map((documentSnapshot) {
var privilege = ShipmentType.fromMap(
documentSnapshot.data() as Map<String, dynamic>,
documentSnapshot.id);
return privilege;
}).toList();
notifyListeners();
});
} catch (e) {
log.warning("Error!! $e");
}
2020-10-08 03:32:52 +06:30
}
Future<void> create(FcsShipment fcsShipment) {
return Services.instance.fcsShipmentService.createFcsShipment(fcsShipment);
}
Future<void> update(FcsShipment fcsShipment) {
return Services.instance.fcsShipmentService.updateFcsShipment(fcsShipment);
2020-06-01 14:24:45 +06:30
}
2020-10-19 05:13:49 +06:30
2024-02-21 17:05:20 +06:30
Future<void> process(String id) {
2024-09-22 16:49:59 +06:30
return Services.instance.fcsShipmentService
.updateFcsShipmentStatus(id, fcs_shipment_processed_status);
2024-02-21 17:05:20 +06:30
}
Future<void> ship(String id) {
2024-09-22 16:49:59 +06:30
return Services.instance.fcsShipmentService
.updateFcsShipmentStatus(id, fcs_shipment_shipped_status);
2024-02-21 17:05:20 +06:30
}
Future<void> arrive(String id) {
2024-09-22 16:49:59 +06:30
return Services.instance.fcsShipmentService
.updateFcsShipmentStatus(id, fcs_shipment_arrived_status);
2024-02-21 17:05:20 +06:30
}
Future<void> invoice(String id) {
2024-09-22 16:49:59 +06:30
return Services.instance.fcsShipmentService
.updateFcsShipmentStatus(id, fcs_shipment_shipped_status);
2020-10-19 05:13:49 +06:30
}
2020-10-28 05:11:06 +06:30
2024-03-01 17:27:56 +06:30
Future<void> cancel(String id) {
2024-09-22 16:49:59 +06:30
return Services.instance.fcsShipmentService
.updateFcsShipmentStatus(id, fcs_shipment_canceled_status);
2024-03-01 17:27:56 +06:30
}
2020-10-28 05:11:06 +06:30
Future<String> report(FcsShipment fcsShipment) {
return Services.instance.fcsShipmentService.report(fcsShipment);
}
2024-02-07 17:26:29 +06:30
Future<List<FcsShipment>> getShipments() async {
2024-02-07 17:26:29 +06:30
List<FcsShipment> fcsShipments = [];
try {
var snaps = await FirebaseFirestore.instance
.collection("/$fcs_shipment_collection")
.where("status", whereIn: [
2024-09-22 16:49:59 +06:30
fcs_shipment_processed_status,
fcs_shipment_shipped_status,
fcs_shipment_arrived_status,
fcs_shipment_invoiced_status
])
2024-02-07 17:26:29 +06:30
.where("is_deleted", isEqualTo: false)
.orderBy("update_time", descending: true)
.limit(shipmentCountForCartonFilter)
2024-02-07 17:26:29 +06:30
.get(const GetOptions(source: Source.server));
fcsShipments = snaps.docs.map((documentSnapshot) {
var fcs =
FcsShipment.fromMap(documentSnapshot.data(), documentSnapshot.id);
return fcs;
}).toList();
} catch (e) {
log.warning("Error!! $e");
}
return fcsShipments;
}
2020-06-01 14:24:45 +06:30
}