Files
fcs/lib/pages/shipment/model/shipment_model.dart

228 lines
6.8 KiB
Dart
Raw Normal View History

2020-05-29 15:54:26 +06:30
import 'dart:async';
import 'package:cloud_firestore/cloud_firestore.dart';
2020-10-16 10:58:31 +06:30
import 'package:fcs/data/services/services.dart';
2020-10-12 08:26:27 +06:30
import 'package:fcs/domain/constants.dart';
2020-10-15 03:06:13 +06:30
import 'package:fcs/domain/entities/shipment.dart';
2020-10-16 10:58:31 +06:30
import 'package:fcs/helpers/paginator.dart';
2020-10-07 02:33:06 +06:30
import 'package:fcs/pages/main/model/base_model.dart';
2020-05-29 15:54:26 +06:30
import 'package:logging/logging.dart';
2020-10-07 14:42:07 +06:30
class ShipmentModel extends BaseModel {
final log = Logger('ShipmentModel');
2020-05-29 15:54:26 +06:30
2021-09-10 16:48:21 +06:30
StreamSubscription<QuerySnapshot>? listener;
2020-05-29 15:54:26 +06:30
2020-10-16 10:58:31 +06:30
List<Shipment> get shipments => _menuSelectedIndex == 1
? _shipments
: List<Shipment>.from(_delivered.values);
2020-06-30 16:27:56 +06:30
2020-10-16 10:58:31 +06:30
List<Shipment> _shipments = [];
2021-09-10 16:48:21 +06:30
late Paginator _delivered;
2020-10-16 10:58:31 +06:30
bool isLoading = false;
int _menuSelectedIndex = 1;
2020-10-12 08:26:27 +06:30
2020-10-16 10:58:31 +06:30
set menuSelectedIndex(int index) {
_menuSelectedIndex = index;
notifyListeners();
2020-07-02 16:16:21 +06:30
}
2021-09-10 16:48:21 +06:30
int get menuSelectedIndex => _menuSelectedIndex;
2020-10-16 10:58:31 +06:30
2020-10-19 05:13:49 +06:30
initData(bool forCustomer, {bool myPickup = false}) {
2020-10-16 10:58:31 +06:30
logout();
2020-10-19 05:13:49 +06:30
_loadShipments(forCustomer, myPickup);
2020-10-21 05:40:58 +06:30
_delivered = _getDelivered(forCustomer);
2020-10-28 07:10:26 +06:30
_delivered.load(onFinished: () {
isLoading = false;
notifyListeners();
});
2020-07-02 16:16:21 +06:30
}
2020-10-16 10:58:31 +06:30
@override
logout() async {
if (_delivered != null) _delivered.close();
2021-09-10 16:48:21 +06:30
if (listener != null) await listener!.cancel();
2020-10-16 10:58:31 +06:30
_shipments = [];
}
2020-05-29 15:54:26 +06:30
2021-09-10 16:48:21 +06:30
Future<void> loadMore({bool? isCustomer}) async {
2020-10-16 10:58:31 +06:30
if (menuSelectedIndex == 1)
return; // when delivered menu is not selected return
if (_delivered.ended) return;
isLoading = true;
notifyListeners();
await _delivered.load(onFinished: () {
isLoading = false;
notifyListeners();
});
2020-05-31 15:00:11 +06:30
}
2021-09-10 16:48:21 +06:30
Future<void> refresh({bool? isCustomer}) async {
2020-10-16 10:58:31 +06:30
if (menuSelectedIndex == 1)
return; // when delivered menu is not selected return
await _delivered.refresh(onFinished: () {
notifyListeners();
});
}
Paginator _getDelivered(bool isCustomer) {
if (!isCustomer) {
if (user == null ||
2021-09-10 16:48:21 +06:30
!((user!.hasPackages() ||
user!.hasReceiving() ||
user!.hasProcessing()))) throw "No privilege";
2020-10-16 10:58:31 +06:30
}
2021-09-10 16:48:21 +06:30
var pageQuery = FirebaseFirestore.instance
2020-10-21 05:40:58 +06:30
.collection("/$shipments_collection")
2020-10-16 10:58:31 +06:30
.where("is_delivered", isEqualTo: true)
.where("is_deleted", isEqualTo: false);
if (isCustomer) {
2021-09-10 16:48:21 +06:30
pageQuery = pageQuery.where("user_id", isEqualTo: user!.id);
2020-10-16 10:58:31 +06:30
}
2020-10-19 05:13:49 +06:30
pageQuery = pageQuery.orderBy("status_date", descending: true);
2020-10-16 10:58:31 +06:30
var paginator = new Paginator(pageQuery, rowPerLoad: 20, toObj: (data, id) {
return Shipment.fromMap(data, id);
});
return paginator;
2020-05-31 15:00:11 +06:30
}
2020-10-19 05:13:49 +06:30
Future<void> _loadShipments(bool forCustomer, bool myPickup) async {
2020-10-16 10:58:31 +06:30
if (user == null) return;
2021-09-10 16:48:21 +06:30
if (!forCustomer && !user!.hasShipment()) return;
if (listener != null) listener!.cancel();
2020-10-16 10:58:31 +06:30
_shipments = [];
try {
2021-09-10 16:48:21 +06:30
var q = FirebaseFirestore.instance
2020-10-21 05:40:58 +06:30
.collection("$shipments_collection")
2020-10-16 10:58:31 +06:30
.where("is_delivered", isEqualTo: false)
2020-10-18 02:38:46 +06:30
.where("is_canceled", isEqualTo: false)
2020-10-16 10:58:31 +06:30
.where("is_deleted", isEqualTo: false);
if (forCustomer) {
2021-09-10 16:48:21 +06:30
q = q.where("user_id", isEqualTo: user!.id);
2020-10-16 10:58:31 +06:30
}
2020-10-19 05:13:49 +06:30
if (myPickup) {
2021-09-10 16:48:21 +06:30
q = q.where("pickup_user_id", isEqualTo: user!.id);
2020-10-19 05:13:49 +06:30
}
q = q.orderBy("created_at", descending: true);
2020-10-16 10:58:31 +06:30
listener = q.snapshots().listen((QuerySnapshot snapshot) {
_shipments.clear();
2021-09-10 16:48:21 +06:30
_shipments = snapshot.docs.map((documentSnapshot) {
2020-10-16 10:58:31 +06:30
var s = Shipment.fromMap(
2021-09-10 16:48:21 +06:30
documentSnapshot.data as Map<String, dynamic>,
documentSnapshot.id);
2020-10-16 10:58:31 +06:30
return s;
}).toList();
notifyListeners();
});
} catch (e) {
log.warning("Error!! $e");
}
}
List<String> shipmentTypes = [
shipment_local_pickup,
shipment_courier_pickup,
shipment_local_dropoff,
shipment_courier_dropoff
];
2021-09-10 16:48:21 +06:30
Shipment? getActiveShipment(String shipmentID) {
return _shipments.firstWhere((e) => e.id == shipmentID);
2020-10-23 01:27:23 +06:30
}
2021-09-10 16:48:21 +06:30
Future<Shipment?> getShipment(String shipmentID) async {
2020-10-18 02:38:46 +06:30
String path = "/$shipments_collection";
try {
2021-09-10 16:48:21 +06:30
var ref = FirebaseFirestore.instance.collection("$path").doc(shipmentID);
var snap = await ref.get(const GetOptions(source: Source.server));
2020-10-18 02:38:46 +06:30
if (snap.exists) {
2021-09-10 16:48:21 +06:30
var s = Shipment.fromMap(snap.data as Map<String, dynamic>, snap.id);
2020-10-18 02:38:46 +06:30
return s;
}
} catch (e) {
log.warning("Error!! $e");
}
return null;
}
2021-09-10 16:48:21 +06:30
Future<List<Shipment?>?> getShipmentWithHandlingFee(
2020-10-24 06:14:07 +06:30
String fcsShipmentID, String userID) async {
String path = "/$shipments_collection";
try {
2021-09-10 16:48:21 +06:30
var q = FirebaseFirestore.instance
2020-10-24 06:14:07 +06:30
.collection("$path")
.where("user_id", isEqualTo: userID)
.where("is_deleted", isEqualTo: false)
.where("handling_fee", isGreaterThan: 0)
.where("fcs_shipment_id", isEqualTo: fcsShipmentID);
2021-09-10 16:48:21 +06:30
var snaps = await q.get(const GetOptions(source: Source.server));
List<Shipment?> shipments = snaps.docs.map((snap) {
2020-10-24 06:14:07 +06:30
if (snap.exists) {
2021-09-10 16:48:21 +06:30
var s = Shipment.fromMap(snap.data as Map<String, dynamic>, snap.id);
2020-10-24 06:14:07 +06:30
return s;
}
}).toList();
return shipments;
} catch (e) {
log.warning("Error!! $e");
}
return null;
}
2020-05-29 15:54:26 +06:30
void initUser(user) {
super.initUser(user);
}
2020-10-16 10:58:31 +06:30
Future<void> createShipment(Shipment shipment) {
return Services.instance.shipmentService.createShipment(shipment);
}
Future<void> updateShipment(Shipment shipment) {
return Services.instance.shipmentService.updateShipment(shipment);
2020-05-29 15:54:26 +06:30
}
2020-10-18 02:38:46 +06:30
Future<void> cancelShipment(Shipment shipment) {
return Services.instance.shipmentService.cancelShipment(shipment);
}
2020-10-19 05:13:49 +06:30
Future<void> assignShipment(Shipment shipment) {
return Services.instance.shipmentService.assignShipment(shipment);
}
Future<void> completeAssignShipment(Shipment shipment) {
return Services.instance.shipmentService.completeAssignShipment(shipment);
}
Future<void> completePickupShipment(Shipment shipment) {
return Services.instance.shipmentService.completePickupShipment(shipment);
}
Future<void> completePackShipment(Shipment shipment) {
return Services.instance.shipmentService.completePackShipment(shipment);
}
Future<void> packShipment(Shipment shipment) {
return Services.instance.shipmentService.packShipment(shipment);
}
Future<void> confirmShipment(Shipment shipment) async {
// String path = Path.join(shipment_labels_files_path);
// String url = await uploadStorage(path, file);
// shipment.shipmentLabelUrl = url;
return Services.instance.shipmentService.confirmShipment(shipment);
}
Future<void> completeConfirmShipment(Shipment shipment) {
return Services.instance.shipmentService.completeConfirmShipment(shipment);
}
Future<void> completeReceiveShipment(Shipment shipment) {
return Services.instance.shipmentService.completeReceiveShipment(shipment);
}
2020-05-29 15:54:26 +06:30
}