188 lines
5.6 KiB
Dart
188 lines
5.6 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:cloud_firestore/cloud_firestore.dart';
|
|
import 'package:fcs/data/services/services.dart';
|
|
import 'package:fcs/domain/constants.dart';
|
|
import 'package:fcs/domain/entities/fcs_shipment.dart';
|
|
import 'package:fcs/pages/main/model/base_model.dart';
|
|
import 'package:logging/logging.dart';
|
|
|
|
import '../../../pagination/paginator_listener.dart';
|
|
|
|
class FcsShipmentModel extends BaseModel {
|
|
final log = Logger('FcsShipmentModel');
|
|
PaginatorListener<FcsShipment>? fcsShipments;
|
|
bool isLoading = false;
|
|
int selectedIndex = 0;
|
|
|
|
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);
|
|
}
|
|
|
|
// processing status
|
|
if (index == 2) {
|
|
col = col.where("status", isEqualTo: fcs_shipment_processing_status);
|
|
pageQuery =
|
|
pageQuery.where("status", isEqualTo: fcs_shipment_processing_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);
|
|
}
|
|
|
|
pageQuery = pageQuery.orderBy("shipment_number", 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_confirmed_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);
|
|
}
|
|
|
|
@override
|
|
logout() async {
|
|
fcsShipments?.close();
|
|
}
|
|
|
|
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.processFcsShipment(id);
|
|
}
|
|
|
|
Future<void> ship(String id) {
|
|
return Services.instance.fcsShipmentService.shipFcsShipment(id);
|
|
}
|
|
|
|
Future<void> arrive(String id) {
|
|
return Services.instance.fcsShipmentService.arriveFcsShipment(id);
|
|
}
|
|
|
|
Future<void> invoice(String id) {
|
|
return Services.instance.fcsShipmentService.invoiceFcsShipment(id);
|
|
}
|
|
|
|
Future<String> report(FcsShipment fcsShipment) {
|
|
return Services.instance.fcsShipmentService.report(fcsShipment);
|
|
}
|
|
|
|
Future<List<FcsShipment>> getAllShipments() async {
|
|
List<FcsShipment> fcsShipments = [];
|
|
try {
|
|
var snaps = await FirebaseFirestore.instance
|
|
.collection("/$fcs_shipment_collection")
|
|
.where("is_deleted", isEqualTo: false)
|
|
.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;
|
|
}
|
|
}
|