Files
fcs/lib/pages/carton/model/carton_model.dart

223 lines
6.5 KiB
Dart
Raw Normal View History

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-16 17:57:58 +06:30
import 'package:fcs/domain/vo/message.dart';
2020-10-07 02:33:06 +06:30
import 'package:fcs/domain/vo/shipment_status.dart';
2020-10-16 17:57:58 +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-06-04 01:36:49 +06:30
import 'package:logging/logging.dart';
2020-10-18 02:38:46 +06:30
class CartonModel extends BaseModel {
List<Carton> _boxes = [];
final log = Logger('CartonModel');
List<Carton> get boxes =>
_selectedIndex == 1 ? _boxes : List<Carton>.from(_delivered.values);
2020-10-16 17:57:58 +06:30
Paginator _delivered;
int _selectedIndex = 1;
bool isLoading = false;
2020-06-04 01:36:49 +06:30
StreamSubscription<QuerySnapshot> listener;
2020-10-07 02:33:06 +06:30
static List<ShipmentStatus> statusHistory = [
ShipmentStatus(status: "Packed", date: DateTime(2020, 6, 1), done: true),
ShipmentStatus(status: "Shipped", date: DateTime(2020, 6, 5), done: false),
ShipmentStatus(
status: "Delivered", date: DateTime(2020, 6, 15), done: false)
2020-06-04 01:36:49 +06:30
];
2020-10-18 02:38:46 +06:30
List<Carton> get completed {
2020-06-04 01:36:49 +06:30
return boxes.where((e) => e.status == "Delivered").toList()
..sort((e1, e2) {
return e2.packageNumber.compareTo(e1.packageNumber);
});
}
2020-10-18 02:38:46 +06:30
List<Carton> get processed {
2020-06-26 16:04:40 +06:30
return boxes.where((e) => e.status == "Packed").toList()
..sort((e1, e2) {
return e2.packageNumber.compareTo(e1.packageNumber);
});
}
2020-10-18 02:38:46 +06:30
List<Carton> get upcoming {
2020-06-04 01:36:49 +06:30
return boxes
.where((e) =>
e.status == "Packed" ||
2020-07-02 16:16:21 +06:30
// e.status == "Received" ||
2020-06-26 16:17:40 +06:30
e.status == "Shipped" ||
e.status == "Arrived")
2020-06-04 01:36:49 +06:30
.toList()
..sort((e1, e2) {
return e2.packageNumber.compareTo(e1.packageNumber);
});
}
2020-10-21 02:59:10 +06:30
List<String> cartonTypes = [
carton_from_packages,
carton_mix_box,
carton_small_bag
];
2020-10-20 06:19:10 +06:30
List<String> cartonTypesInfo = [
carton_from_packages,
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
2020-10-16 17:57:58 +06:30
set selectedIndex(int index) {
_selectedIndex = index;
notifyListeners();
}
get selectedIndex => _selectedIndex;
initData() {
_selectedIndex = 1;
_loadBoxes();
if (_delivered != null) _delivered.close();
2020-10-21 05:40:58 +06:30
_delivered = _getDelivered();
2020-10-16 17:57:58 +06:30
_delivered.load();
}
Future<void> _loadBoxes() async {
if (user == null || !user.hasCarton()) return;
2020-10-18 02:38:46 +06:30
String path = "/$cartons_collection/";
2020-10-16 17:57:58 +06:30
if (listener != null) listener.cancel();
_boxes = [];
try {
listener = Firestore.instance
.collection("$path")
2020-10-20 06:19:10 +06:30
.where("status",
whereIn: [carton_packed_status, carton_shipped_status])
.where("is_deleted", isEqualTo: false)
.orderBy("created_at", descending: true)
2020-10-16 17:57:58 +06:30
.snapshots()
.listen((QuerySnapshot snapshot) {
2020-10-20 06:19:10 +06:30
_boxes.clear();
_boxes = snapshot.documents.map((documentSnapshot) {
var s = Carton.fromMap(
documentSnapshot.data, documentSnapshot.documentID);
return s;
}).toList();
notifyListeners();
});
2020-10-16 17:57:58 +06:30
} catch (e) {
log.warning("Error!! $e");
}
}
Paginator _getDelivered() {
if (user == null || !user.hasCarton()) return null;
var pageQuery = Firestore.instance
2020-10-18 02:38:46 +06:30
.collection("/$cartons_collection")
2020-10-16 17:57:58 +06:30
.where("is_delivered", isEqualTo: true)
.where("is_deleted", isEqualTo: false);
var paginator = new Paginator(pageQuery, rowPerLoad: 20, toObj: (data, id) {
2020-10-18 02:38:46 +06:30
return Carton.fromMap(data, id);
2020-10-16 17:57:58 +06:30
});
return paginator;
}
Future<void> loadMore() async {
2020-10-20 06:19:10 +06:30
if (_delivered.ended || selectedIndex == 1) return;
2020-10-16 17:57:58 +06:30
isLoading = true;
notifyListeners();
await _delivered.load(onFinished: () {
isLoading = false;
notifyListeners();
});
}
Future<void> refresh() async {
2020-10-20 06:19:10 +06:30
if (selectedIndex == 1) return;
2020-10-16 17:57:58 +06:30
await _delivered.refresh(onFinished: () {
notifyListeners();
});
}
2020-06-04 01:36:49 +06:30
void initUser(user) {
super.initUser(user);
}
@override
logout() async {
if (listener != null) await listener.cancel();
2020-10-16 17:57:58 +06:30
if (_delivered != null) _delivered.close();
_boxes = [];
2020-06-04 01:36:49 +06:30
}
2020-10-18 02:38:46 +06:30
Future<List<Carton>> getCartons(String shipmentID) async {
String path = "/$cartons_collection";
var querySnap = await Firestore.instance
.collection(path)
.where("shipment_id", isEqualTo: shipmentID)
.getDocuments();
return querySnap.documents
.map((e) => Carton.fromMap(e.data, e.documentID))
.toList();
}
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";
var querySnap = await Firestore.instance
.collection(path)
.where("fcs_shipment_id", isEqualTo: fcsShipmentID)
.where("is_deleted", isEqualTo: false)
.getDocuments();
return querySnap.documents
.map((e) => Carton.fromMap(e.data, e.documentID))
.toList();
}
2020-10-22 04:14:53 +06:30
Future<List<Carton>> getCartonsForInvoice(
String fcsShipmentID, String userID) async {
String path = "/$cartons_collection";
var querySnap = await Firestore.instance
.collection(path)
// .where("fcs_shipment_id", isEqualTo: fcsShipmentID)
.where("user_id", isEqualTo: userID)
.where("is_deleted", isEqualTo: false)
.where("is_invoiced", isEqualTo: false)
.getDocuments();
return querySnap.documents
.map((e) => Carton.fromMap(e.data, e.documentID))
.toList();
}
2020-10-21 02:59:10 +06:30
Future<List<Carton>> getMixCartonsByFcsShipment(String fcsShipmentID) async {
String path = "/$cartons_collection";
var querySnap = await Firestore.instance
.collection(path)
.where("fcs_shipment_id", isEqualTo: fcsShipmentID)
.where("carton_type", isEqualTo: carton_mix_box)
.where("is_deleted", isEqualTo: false)
.getDocuments();
return querySnap.documents
.map((e) => Carton.fromMap(e.data, e.documentID))
.toList();
}
2020-10-20 06:19:10 +06:30
Future<Carton> getCarton(String id) async {
String path = "/$cartons_collection";
var snap = await Firestore.instance.collection(path).document(id).get();
return Carton.fromMap(snap.data, snap.documentID);
}
2020-10-19 05:13:49 +06:30
Future<void> createCarton(Carton carton) {
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-06-04 01:36:49 +06:30
}