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

181 lines
5.2 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';
2020-10-08 03:32:52 +06:30
import 'package:fcs/domain/constants.dart';
2020-10-07 14:42:07 +06:30
import 'package:fcs/domain/entities/fcs_shipment.dart';
2020-10-15 19:40:03 +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-10-08 03:32:52 +06:30
import 'package:logging/logging.dart';
2020-06-01 14:24:45 +06:30
2020-10-07 02:33:06 +06:30
class FcsShipmentModel extends BaseModel {
2020-10-08 03:32:52 +06:30
final log = Logger('FcsShipmentModel');
2020-06-01 14:24:45 +06:30
2021-09-10 16:48:20 +06:30
StreamSubscription<QuerySnapshot>? listener;
2020-10-15 19:40:03 +06:30
List<FcsShipment> _fcsShipments = [];
List<FcsShipment> get fcsShipments => _selectedIndex == 1
? _fcsShipments
2021-09-13 08:45:45 +06:30
: List<FcsShipment>.from(_shipped!.values);
2020-10-15 19:40:03 +06:30
2021-09-13 08:45:45 +06:30
Paginator? _shipped;
2020-10-15 19:40:03 +06:30
bool isLoading = false;
int _selectedIndex = 1;
set selectedIndex(int index) {
_selectedIndex = index;
notifyListeners();
}
2021-09-13 08:45:45 +06:30
int get selectedIndex => _selectedIndex;
2020-06-01 14:24:45 +06:30
2020-10-08 03:32:52 +06:30
@override
void privilegeChanged() {
super.privilegeChanged();
_loadFcsShipments();
2020-06-01 14:24:45 +06:30
}
2020-10-16 11:05:38 +06:30
initData() {
_selectedIndex = 1;
_loadFcsShipments();
2021-09-13 08:45:45 +06:30
if (_shipped != null) _shipped!.close();
2020-10-21 05:40:58 +06:30
_shipped = _getShipped();
2021-09-13 08:45:45 +06:30
_shipped!.load();
2020-10-16 11:05:38 +06:30
}
2020-10-08 03:32:52 +06:30
Future<void> _loadFcsShipments() async {
2021-10-12 10:22:57 +06:30
if (user == null || !user!.hasFcsShipments()) return;
2020-10-08 03:32:52 +06:30
String path = "/$fcs_shipment_collection/";
2021-09-10 16:48:20 +06:30
if (listener != null) listener!.cancel();
2020-10-15 19:40:03 +06:30
_fcsShipments = [];
2020-10-08 03:32:52 +06:30
try {
2021-09-10 16:48:20 +06:30
listener = FirebaseFirestore.instance
2020-10-08 03:32:52 +06:30
.collection("$path")
2020-10-21 05:40:58 +06:30
.where("status", isEqualTo: fcs_shipment_confirmed_status)
2020-10-21 06:24:26 +06:30
.where("is_deleted", isEqualTo: false)
.orderBy("shipment_number", descending: true)
2020-10-08 03:32:52 +06:30
.snapshots()
.listen((QuerySnapshot snapshot) {
2020-10-15 19:40:03 +06:30
_fcsShipments.clear();
2021-09-10 16:48:20 +06:30
_fcsShipments = snapshot.docs.map((documentSnapshot) {
2020-10-08 03:32:52 +06:30
var s = FcsShipment.fromMap(
2021-09-13 08:45:45 +06:30
documentSnapshot.data() as Map<String, dynamic>,
documentSnapshot.id);
2020-10-08 03:32:52 +06:30
return s;
}).toList();
notifyListeners();
});
} catch (e) {
log.warning("Error!! $e");
}
2020-06-01 14:24:45 +06:30
}
2020-10-21 05:40:58 +06:30
Paginator _getShipped() {
2021-09-10 16:48:20 +06:30
if (user == null || !user!.hasFcsShipments()) throw "No Privilege";
2020-10-15 19:40:03 +06:30
2021-09-10 16:48:20 +06:30
var pageQuery = FirebaseFirestore.instance
2020-10-15 19:40:03 +06:30
.collection("/$fcs_shipment_collection")
2020-10-21 05:40:58 +06:30
.where("status", isEqualTo: fcs_shipment_shipped_status)
2020-10-15 19:40:03 +06:30
.where("is_deleted", isEqualTo: false)
2020-10-21 06:24:26 +06:30
.orderBy("shipment_number", descending: true);
2020-10-15 19:40:03 +06:30
var paginator = new Paginator(pageQuery, rowPerLoad: 20, toObj: (data, id) {
return FcsShipment.fromMap(data, id);
});
return paginator;
}
Future<void> loadMore() async {
2021-09-13 08:45:45 +06:30
if (_shipped!.ended || _selectedIndex == 1) return;
2020-10-15 19:40:03 +06:30
isLoading = true;
notifyListeners();
2021-09-13 08:45:45 +06:30
await _shipped!.load(onFinished: () {
2020-10-15 19:40:03 +06:30
isLoading = false;
notifyListeners();
});
}
Future<void> refresh() async {
2020-10-21 05:40:58 +06:30
if (_selectedIndex == 1) return;
2021-09-13 08:45:45 +06:30
await _shipped!.refresh(onFinished: () {
2020-10-15 19:40:03 +06:30
notifyListeners();
});
}
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_confirmed_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 {
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);
}
@override
logout() async {
2021-09-10 16:48:20 +06:30
if (listener != null) await listener!.cancel();
2021-09-13 08:45:45 +06:30
if (_shipped != null) _shipped!.close();
2020-10-15 19:40:03 +06:30
_fcsShipments = [];
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
Future<void> ship(FcsShipment fcsShipment) {
return Services.instance.fcsShipmentService.ship(fcsShipment);
}
2020-10-28 05:11:06 +06:30
Future<String> report(FcsShipment fcsShipment) {
return Services.instance.fcsShipmentService.report(fcsShipment);
}
2020-06-01 14:24:45 +06:30
}