2020-10-16 13:46:02 +06:30
|
|
|
import 'dart:async';
|
2020-10-26 04:41:24 +06:30
|
|
|
import 'dart:io';
|
2020-10-16 13:46:02 +06:30
|
|
|
|
|
|
|
|
import 'package:cloud_firestore/cloud_firestore.dart';
|
2020-10-26 04:41:24 +06:30
|
|
|
import 'package:fcs/data/services/services.dart';
|
2020-10-24 06:14:07 +06:30
|
|
|
import 'package:fcs/domain/constants.dart';
|
2020-10-07 02:33:06 +06:30
|
|
|
import 'package:fcs/domain/entities/invoice.dart';
|
2020-10-28 05:11:06 +06:30
|
|
|
import 'package:fcs/domain/entities/payment.dart';
|
2020-10-26 04:41:24 +06:30
|
|
|
import 'package:fcs/helpers/firebase_helper.dart';
|
2020-10-07 02:33:06 +06:30
|
|
|
import 'package:fcs/pages/main/model/base_model.dart';
|
2020-10-16 13:46:02 +06:30
|
|
|
import 'package:logging/logging.dart';
|
2020-10-28 05:11:06 +06:30
|
|
|
import 'package:path/path.dart' as Path;
|
2020-06-02 14:56:51 +06:30
|
|
|
|
2024-01-26 16:56:20 +06:30
|
|
|
import '../../../pagination/paginator_listener.dart';
|
|
|
|
|
|
2020-06-02 14:56:51 +06:30
|
|
|
class InvoiceModel extends BaseModel {
|
2020-10-16 13:46:02 +06:30
|
|
|
final log = Logger('InvoiceModel');
|
2024-01-26 16:56:20 +06:30
|
|
|
PaginatorListener<Invoice>? getInvoices;
|
|
|
|
|
int selectedIndex = 1;
|
2020-10-16 13:46:02 +06:30
|
|
|
|
2024-01-26 16:56:20 +06:30
|
|
|
void initUser(user) {
|
|
|
|
|
super.initUser(user);
|
2020-10-16 21:38:39 +06:30
|
|
|
}
|
|
|
|
|
|
2024-01-26 16:56:20 +06:30
|
|
|
logout() async {
|
|
|
|
|
getInvoices?.close();
|
2020-10-16 13:46:02 +06:30
|
|
|
}
|
|
|
|
|
|
2024-01-26 16:56:20 +06:30
|
|
|
onChanged(int index, bool isCustomer) {
|
|
|
|
|
selectedIndex = index;
|
|
|
|
|
loadPaginationInvoices(index, isCustomer);
|
|
|
|
|
notifyListeners();
|
2020-10-16 21:38:39 +06:30
|
|
|
}
|
|
|
|
|
|
2024-01-26 16:56:20 +06:30
|
|
|
Future<void> loadPaginationInvoices(int index, bool isCustomer) async {
|
2020-10-16 21:38:39 +06:30
|
|
|
if (user == null) return;
|
2024-01-26 16:56:20 +06:30
|
|
|
if (!isCustomer && !user!.hasInvoices()) return;
|
2020-10-16 21:38:39 +06:30
|
|
|
|
2024-01-26 16:56:20 +06:30
|
|
|
String path = "/$invoices_collection";
|
|
|
|
|
Query col = FirebaseFirestore.instance.collection(path);
|
|
|
|
|
Query pageQuery = FirebaseFirestore.instance.collection(path);
|
2020-10-16 13:46:02 +06:30
|
|
|
|
2020-10-16 21:38:39 +06:30
|
|
|
if (isCustomer) {
|
2024-01-26 16:56:20 +06:30
|
|
|
col = col.where("user_id", isEqualTo: user!.id);
|
2021-09-10 15:24:31 +06:30
|
|
|
pageQuery = pageQuery.where("user_id", isEqualTo: user!.id);
|
2020-10-16 13:46:02 +06:30
|
|
|
}
|
2024-01-26 16:56:20 +06:30
|
|
|
|
|
|
|
|
if (index == 1) {
|
|
|
|
|
col = col.where("status", isEqualTo: invoice_issued_status);
|
|
|
|
|
pageQuery = pageQuery.where("status", isEqualTo: invoice_issued_status);
|
2020-10-26 04:41:24 +06:30
|
|
|
}
|
2024-01-26 16:56:20 +06:30
|
|
|
|
|
|
|
|
if (index == 2) {
|
|
|
|
|
col = col.where("status", isEqualTo: invoice_paid_status);
|
2020-10-26 04:41:24 +06:30
|
|
|
pageQuery = pageQuery.where("status", isEqualTo: invoice_paid_status);
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-26 16:56:20 +06:30
|
|
|
if (index == 3) {
|
|
|
|
|
col = col.where("status", isEqualTo: invoice_cancel_status);
|
|
|
|
|
pageQuery = pageQuery.where("status", isEqualTo: invoice_cancel_status);
|
|
|
|
|
}
|
2020-06-02 14:56:51 +06:30
|
|
|
|
2024-01-26 16:56:20 +06:30
|
|
|
pageQuery = pageQuery.orderBy("created_at", descending: true);
|
2020-06-02 14:56:51 +06:30
|
|
|
|
2024-01-26 16:56:20 +06:30
|
|
|
getInvoices?.close();
|
|
|
|
|
getInvoices = PaginatorListener<Invoice>(
|
|
|
|
|
col, pageQuery, (data, id) => Invoice.fromMap(data, id),
|
|
|
|
|
rowPerLoad: 30);
|
2020-06-02 14:56:51 +06:30
|
|
|
}
|
2020-10-16 13:46:02 +06:30
|
|
|
|
2021-09-10 15:22:11 +06:30
|
|
|
Future<Invoice?> getInvoice(String id) async {
|
2020-10-28 05:11:06 +06:30
|
|
|
String path = "/$invoices_collection";
|
|
|
|
|
try {
|
2021-09-10 15:22:11 +06:30
|
|
|
var ref = FirebaseFirestore.instance.collection("$path").doc(id);
|
|
|
|
|
var snap = await ref.get(const GetOptions(source: Source.server));
|
2020-10-28 05:11:06 +06:30
|
|
|
if (snap.exists) {
|
2024-01-26 16:56:20 +06:30
|
|
|
var s = Invoice.fromMap(snap.data() as Map<String, dynamic>, snap.id);
|
2020-10-28 05:11:06 +06:30
|
|
|
return s;
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
log.warning("Error!! $e");
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<void> pay(Payment payment, File file) async {
|
2021-09-10 15:24:31 +06:30
|
|
|
String path = Path.join(receipt_labels_files_path, user!.id);
|
2020-10-28 05:11:06 +06:30
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-26 04:41:24 +06:30
|
|
|
Future<void> createInvoice(Invoice invoice) async {
|
|
|
|
|
return Services.instance.invoiceService.createInvoice(invoice);
|
2020-10-16 13:46:02 +06:30
|
|
|
}
|
|
|
|
|
|
2020-10-16 21:38:39 +06:30
|
|
|
Future<void> updateInvoice(Invoice invoice) {
|
2020-10-26 04:41:24 +06:30
|
|
|
return Services.instance.invoiceService.updateInvoice(invoice);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<void> cancelInvoice(Invoice invoice) {
|
|
|
|
|
return Services.instance.invoiceService.cancelInvoice(invoice);
|
2020-10-16 13:46:02 +06:30
|
|
|
}
|
2020-06-02 14:56:51 +06:30
|
|
|
}
|