Files
fcs/lib/pages/fcs_shipment/model/fcs_shipment_model.dart
2024-01-24 16:54:08 +06:30

127 lines
3.8 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 = 1;
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);
if (index == 1) {
col = col.where("status", isEqualTo: fcs_shipment_confirmed_status);
pageQuery =
pageQuery.where("status", isEqualTo: fcs_shipment_confirmed_status);
}
if (index == 2) {
col = col.where("status", isEqualTo: fcs_shipment_shipped_status);
pageQuery =
pageQuery.where("status", isEqualTo: fcs_shipment_shipped_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 {
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> ship(FcsShipment fcsShipment) {
return Services.instance.fcsShipmentService.ship(fcsShipment);
}
Future<String> report(FcsShipment fcsShipment) {
return Services.instance.fcsShipmentService.report(fcsShipment);
}
}