134 lines
4.2 KiB
Dart
134 lines
4.2 KiB
Dart
import 'dart:async';
|
|
import 'dart:io';
|
|
|
|
import 'package:cloud_firestore/cloud_firestore.dart';
|
|
import 'package:fcs/data/services/services.dart';
|
|
import 'package:fcs/constants.dart';
|
|
import 'package:fcs/domain/entities/invoice.dart';
|
|
import 'package:fcs/domain/entities/payment.dart';
|
|
import 'package:fcs/helpers/firebase_helper.dart';
|
|
import 'package:fcs/pages/main/model/base_model.dart';
|
|
import 'package:logging/logging.dart';
|
|
import 'package:path/path.dart' as Path;
|
|
|
|
import '../../../domain/entities/shipment.dart';
|
|
import '../../../pagination/paginator_listener.dart';
|
|
|
|
class InvoiceModel extends BaseModel {
|
|
final log = Logger('InvoiceModel');
|
|
PaginatorListener<Invoice>? getInvoices;
|
|
int selectedIndex = 1;
|
|
|
|
void initUser(user) {
|
|
super.initUser(user);
|
|
}
|
|
|
|
logout() async {
|
|
getInvoices?.close();
|
|
}
|
|
|
|
onChanged(int index, bool isCustomer) {
|
|
selectedIndex = index;
|
|
loadPaginationInvoices(index, isCustomer);
|
|
notifyListeners();
|
|
}
|
|
|
|
Future<void> loadPaginationInvoices(int index, bool isCustomer) async {
|
|
if (user == null) return;
|
|
if (!isCustomer && !user!.hasInvoices()) return;
|
|
|
|
String path = "/$invoices_collection";
|
|
Query col = FirebaseFirestore.instance.collection(path);
|
|
Query pageQuery = FirebaseFirestore.instance.collection(path);
|
|
|
|
if (isCustomer) {
|
|
col = col.where("user_id", isEqualTo: user!.id);
|
|
pageQuery = pageQuery.where("user_id", isEqualTo: user!.id);
|
|
}
|
|
|
|
if (index == 1) {
|
|
col = col.where("status", isEqualTo: invoice_issued_status);
|
|
pageQuery = pageQuery.where("status", isEqualTo: invoice_issued_status);
|
|
}
|
|
|
|
if (index == 2) {
|
|
col = col.where("status", isEqualTo: invoice_paid_status);
|
|
pageQuery = pageQuery.where("status", isEqualTo: invoice_paid_status);
|
|
}
|
|
|
|
if (index == 3) {
|
|
col = col.where("status", isEqualTo: invoice_cancel_status);
|
|
pageQuery = pageQuery.where("status", isEqualTo: invoice_cancel_status);
|
|
}
|
|
|
|
pageQuery = pageQuery.orderBy("created_at", descending: true);
|
|
|
|
getInvoices?.close();
|
|
getInvoices = PaginatorListener<Invoice>(
|
|
col, pageQuery, (data, id) => Invoice.fromMap(data, id),
|
|
rowPerLoad: 30);
|
|
}
|
|
|
|
Future<Invoice?> getInvoice(String id) async {
|
|
String path = "/$invoices_collection";
|
|
try {
|
|
var ref = FirebaseFirestore.instance.collection("$path").doc(id);
|
|
var snap = await ref.get(const GetOptions(source: Source.server));
|
|
if (snap.exists) {
|
|
var s = Invoice.fromMap(snap.data() as Map<String, dynamic>, snap.id);
|
|
return s;
|
|
}
|
|
} catch (e) {
|
|
log.warning("Error!! $e");
|
|
}
|
|
return null;
|
|
}
|
|
|
|
Future<void> pay(Payment payment, File file) async {
|
|
String path = Path.join(receipt_labels_files_path, user!.id);
|
|
String url = await uploadStorage(path, file);
|
|
payment.paymentReceiptURL = url;
|
|
return Services.instance.invoiceService.pay(payment);
|
|
}
|
|
|
|
Future<void> updatePaymentStatus(Payment payment) async {
|
|
return Services.instance.invoiceService.updatPaymentStatus(payment);
|
|
}
|
|
|
|
Future<void> createInvoice(Invoice invoice) async {
|
|
return Services.instance.invoiceService.createInvoice(invoice);
|
|
}
|
|
|
|
Future<void> updateInvoice(Invoice invoice) {
|
|
return Services.instance.invoiceService.updateInvoice(invoice);
|
|
}
|
|
|
|
Future<void> cancelInvoice(Invoice invoice) {
|
|
return Services.instance.invoiceService.cancelInvoice(invoice);
|
|
}
|
|
|
|
Future<List<Shipment?>?> getShipmentWithHandlingFee(
|
|
String fcsShipmentID, String userID) async {
|
|
String path = "/$shipments_collection";
|
|
try {
|
|
var q = FirebaseFirestore.instance
|
|
.collection(path)
|
|
.where("user_id", isEqualTo: userID)
|
|
.where("is_deleted", isEqualTo: false)
|
|
.where("handling_fee", isGreaterThan: 0)
|
|
.where("fcs_shipment_id", isEqualTo: fcsShipmentID);
|
|
var snaps = await q.get(const GetOptions(source: Source.server));
|
|
List<Shipment?> shipments = snaps.docs.map((snap) {
|
|
if (snap.exists) {
|
|
var s = Shipment.fromMap(snap.data as Map<String, dynamic>, snap.id);
|
|
return s;
|
|
}
|
|
}).toList();
|
|
return shipments;
|
|
} catch (e) {
|
|
log.warning("Error!! $e");
|
|
}
|
|
return null;
|
|
}
|
|
}
|