344 lines
11 KiB
Dart
344 lines
11 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:cloud_firestore/cloud_firestore.dart';
|
|
import 'package:fcs/data/services/services.dart';
|
|
import 'package:fcs/constants.dart';
|
|
import 'package:fcs/domain/entities/fcs_shipment.dart';
|
|
import 'package:fcs/domain/entities/shipment_port.dart';
|
|
import 'package:fcs/pages/main/model/base_model.dart';
|
|
import 'package:logging/logging.dart';
|
|
|
|
import '../../../domain/entities/shipment_consignee.dart';
|
|
import '../../../domain/entities/shipment_type.dart';
|
|
import '../../../pagination/paginator_listener.dart';
|
|
|
|
class FcsShipmentModel extends BaseModel {
|
|
final log = Logger('FcsShipmentModel');
|
|
PaginatorListener<FcsShipment>? fcsShipments;
|
|
bool isLoading = false;
|
|
int selectedIndex = 0;
|
|
|
|
List<ShipmentType> shipmentTypes = [];
|
|
List<ShipmentConsignee> shipmentConsingees = [];
|
|
List<ShipmentPort> shipmentPorts = [];
|
|
StreamSubscription<QuerySnapshot>? _shipmentTypeListener;
|
|
StreamSubscription<QuerySnapshot>? _shipmentConsigneeListener;
|
|
StreamSubscription<QuerySnapshot>? _shipmentPortListener;
|
|
|
|
ShipmentType? getShipmentType(id) {
|
|
var list = shipmentTypes.where((e) => e.id == id).toList();
|
|
return list.isNotEmpty ? list.first : null;
|
|
}
|
|
|
|
ShipmentConsignee? getShipmentConsignee(id) {
|
|
var list = shipmentConsingees.where((e) => e.id == id).toList();
|
|
return list.isNotEmpty ? list.first : null;
|
|
}
|
|
|
|
ShipmentPort? getShipmentPort(id) {
|
|
var list = shipmentPorts.where((e) => e.id == id).toList();
|
|
return list.isNotEmpty ? list.first : null;
|
|
}
|
|
|
|
onChanged(int index) {
|
|
selectedIndex = index;
|
|
loadFcsShipments(selectedIndex);
|
|
notifyListeners();
|
|
}
|
|
|
|
loadFcsShipments(int index) {
|
|
if (user == null || !user!.hasFcsShipments()) return;
|
|
|
|
String path = "/$fcs_shipment_collection";
|
|
Query col = FirebaseFirestore.instance.collection(path);
|
|
Query pageQuery = FirebaseFirestore.instance.collection(path);
|
|
|
|
// pending status
|
|
if (index == 1) {
|
|
col = col.where("status", isEqualTo: fcs_shipment_pending_status);
|
|
pageQuery =
|
|
pageQuery.where("status", isEqualTo: fcs_shipment_pending_status);
|
|
}
|
|
|
|
// processed status
|
|
if (index == 2) {
|
|
col = col.where("status", isEqualTo: fcs_shipment_processed_status);
|
|
pageQuery =
|
|
pageQuery.where("status", isEqualTo: fcs_shipment_processed_status);
|
|
}
|
|
|
|
// shipped status
|
|
if (index == 3) {
|
|
col = col.where("status", isEqualTo: fcs_shipment_shipped_status);
|
|
pageQuery =
|
|
pageQuery.where("status", isEqualTo: fcs_shipment_shipped_status);
|
|
}
|
|
|
|
// 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);
|
|
}
|
|
|
|
// delivered status
|
|
if (index == 7) {
|
|
col = col.where("status", isEqualTo: fcs_shipment_delivered_status);
|
|
pageQuery =
|
|
pageQuery.where("status", isEqualTo: fcs_shipment_delivered_status);
|
|
}
|
|
|
|
pageQuery = pageQuery.orderBy("update_time", descending: true);
|
|
|
|
fcsShipments?.close();
|
|
fcsShipments = PaginatorListener<FcsShipment>(
|
|
col, pageQuery, (data, id) => FcsShipment.fromMap(data, id),
|
|
rowPerLoad: 30);
|
|
}
|
|
|
|
Future<List<FcsShipment>> getActiveFcsShipments() async {
|
|
List<FcsShipment> fcsShipments = [];
|
|
try {
|
|
var snaps = await FirebaseFirestore.instance
|
|
.collection("/$fcs_shipment_collection")
|
|
.where("status", isEqualTo: fcs_shipment_processed_status)
|
|
.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;
|
|
}
|
|
|
|
Future<FcsShipment?> getFcsShipment(String id) async {
|
|
if (id == "") return null;
|
|
try {
|
|
var snap = await FirebaseFirestore.instance
|
|
.collection("/$fcs_shipment_collection")
|
|
.doc(id)
|
|
.get(const GetOptions(source: Source.server));
|
|
var fcs = FcsShipment.fromMap(snap.data()!, snap.id);
|
|
|
|
return fcs;
|
|
} catch (e) {
|
|
log.warning("Error!! $e");
|
|
}
|
|
return null;
|
|
}
|
|
|
|
Future<List<FcsShipment>> getInvoiceFcsShipments() async {
|
|
List<FcsShipment> fcsShipments = [];
|
|
try {
|
|
var snaps = await FirebaseFirestore.instance
|
|
.collection("/$fcs_shipment_collection")
|
|
.where("pending_invoice_user_count", isGreaterThan: 0)
|
|
.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;
|
|
}
|
|
|
|
void initUser(user) {
|
|
super.initUser(user);
|
|
|
|
if (user != null && user.hasFcsShipments()) {
|
|
_loadShipmentTypes();
|
|
_loadShipmentConsignees();
|
|
_loadShipmentPorts();
|
|
}
|
|
}
|
|
|
|
@override
|
|
logout() async {
|
|
fcsShipments?.close();
|
|
_shipmentTypeListener?.cancel();
|
|
_shipmentConsigneeListener?.cancel();
|
|
_shipmentPortListener?.cancel();
|
|
shipmentTypes.clear();
|
|
shipmentConsingees.clear();
|
|
shipmentPorts.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 type = ShipmentType.fromMap(
|
|
documentSnapshot.data() as Map<String, dynamic>,
|
|
documentSnapshot.id);
|
|
return type;
|
|
}).toList();
|
|
notifyListeners();
|
|
});
|
|
} catch (e) {
|
|
log.warning("Error!! $e");
|
|
}
|
|
}
|
|
|
|
Future<void> _loadShipmentConsignees() async {
|
|
try {
|
|
_shipmentConsigneeListener = FirebaseFirestore.instance
|
|
.collection(
|
|
"/$config_collection/$setting_doc_id/$shipment_consignee_collection")
|
|
.snapshots()
|
|
.listen((QuerySnapshot snapshot) {
|
|
shipmentConsingees.clear();
|
|
shipmentConsingees = snapshot.docs.map((documentSnapshot) {
|
|
var consignee = ShipmentConsignee.fromMap(
|
|
documentSnapshot.data() as Map<String, dynamic>,
|
|
documentSnapshot.id);
|
|
return consignee;
|
|
}).toList();
|
|
notifyListeners();
|
|
});
|
|
} catch (e) {
|
|
log.warning("Error!! $e");
|
|
}
|
|
}
|
|
|
|
Future<void> _loadShipmentPorts() async {
|
|
try {
|
|
_shipmentPortListener = FirebaseFirestore.instance
|
|
.collection(
|
|
"/$config_collection/$setting_doc_id/$shipment_port_collection")
|
|
.snapshots()
|
|
.listen((QuerySnapshot snapshot) {
|
|
shipmentPorts.clear();
|
|
shipmentPorts = snapshot.docs.map((documentSnapshot) {
|
|
var port = ShipmentPort.fromMap(
|
|
documentSnapshot.data() as Map<String, dynamic>,
|
|
documentSnapshot.id);
|
|
return port;
|
|
}).toList();
|
|
notifyListeners();
|
|
});
|
|
} catch (e) {
|
|
log.warning("Error!! $e");
|
|
}
|
|
}
|
|
|
|
Future<void> create(FcsShipment fcsShipment) {
|
|
return Services.instance.fcsShipmentService.createFcsShipment(fcsShipment);
|
|
}
|
|
|
|
Future<void> update(FcsShipment fcsShipment) {
|
|
return Services.instance.fcsShipmentService.updateFcsShipment(fcsShipment);
|
|
}
|
|
|
|
Future<void> process(String id) {
|
|
return Services.instance.fcsShipmentService
|
|
.updateFcsShipmentStatus(id, fcs_shipment_processed_status);
|
|
}
|
|
|
|
Future<void> ship(String id) {
|
|
return Services.instance.fcsShipmentService
|
|
.updateFcsShipmentStatus(id, fcs_shipment_shipped_status);
|
|
}
|
|
|
|
Future<void> arrive(String id) {
|
|
return Services.instance.fcsShipmentService
|
|
.updateFcsShipmentStatus(id, fcs_shipment_arrived_status);
|
|
}
|
|
|
|
Future<void> invoice(String id) {
|
|
return Services.instance.fcsShipmentService
|
|
.updateFcsShipmentStatus(id, fcs_shipment_invoiced_status);
|
|
}
|
|
|
|
Future<void> cancel(String id) {
|
|
return Services.instance.fcsShipmentService
|
|
.updateFcsShipmentStatus(id, fcs_shipment_canceled_status);
|
|
}
|
|
|
|
Future<void> deliver(String id) {
|
|
return Services.instance.fcsShipmentService
|
|
.updateFcsShipmentStatus(id, fcs_shipment_delivered_status);
|
|
}
|
|
|
|
Future<String> report(FcsShipment fcsShipment) {
|
|
return Services.instance.fcsShipmentService.report(fcsShipment);
|
|
}
|
|
|
|
Future<List<FcsShipment>> getShipments() async {
|
|
List<FcsShipment> fcsShipments = [];
|
|
try {
|
|
var snaps = await FirebaseFirestore.instance
|
|
.collection("/$fcs_shipment_collection")
|
|
.where("status", whereIn: [
|
|
fcs_shipment_processed_status,
|
|
fcs_shipment_shipped_status,
|
|
fcs_shipment_arrived_status,
|
|
fcs_shipment_invoiced_status,
|
|
fcs_shipment_delivered_status
|
|
])
|
|
.where("is_deleted", isEqualTo: false)
|
|
.orderBy("update_time", descending: true)
|
|
.limit(shipmentCountForCartonFilter)
|
|
.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;
|
|
}
|
|
|
|
Future<int?> getPackageCount({required String fcsShipmentId}) async {
|
|
String path = "/$packages_collection";
|
|
|
|
AggregateQuerySnapshot query = await FirebaseFirestore.instance
|
|
.collection(path)
|
|
.where("fcs_shipment_id", isEqualTo: fcsShipmentId)
|
|
.where("delete_time", isEqualTo: 0)
|
|
.count()
|
|
.get();
|
|
|
|
return query.count;
|
|
}
|
|
|
|
Future<int?> getCartonCount({required String fcsShipmentId}) async {
|
|
String path = "/$cartons_collection";
|
|
|
|
AggregateQuerySnapshot query = await FirebaseFirestore.instance
|
|
.collection(path)
|
|
.where("fcs_shipment_id", isEqualTo: fcsShipmentId)
|
|
.where("delete_time", isEqualTo: 0)
|
|
.count()
|
|
.get();
|
|
|
|
return query.count;
|
|
}
|
|
}
|