Files
fcs/lib/pages/invoice/model/invoice_model.dart

174 lines
5.0 KiB
Dart
Raw Normal View History

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-16 21:38:39 +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-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
class InvoiceModel extends BaseModel {
2020-10-16 13:46:02 +06:30
final log = Logger('InvoiceModel');
2021-09-10 15:22:11 +06:30
StreamSubscription<QuerySnapshot<Map<String, dynamic>>>? listener;
2020-10-16 13:46:02 +06:30
2020-10-26 04:41:24 +06:30
List<Invoice> _invoices = [];
2020-06-02 14:56:51 +06:30
2020-10-16 21:38:39 +06:30
List<Invoice> get invoices =>
2020-10-26 04:41:24 +06:30
_selectedIndex == 1 ? _invoices : List<Invoice>.from(_paginator.values);
2020-10-16 21:38:39 +06:30
2021-09-10 15:22:11 +06:30
late Paginator _paginator;
2020-10-16 13:46:02 +06:30
bool endOfPaidInvoices = false;
bool isLoading = false;
2020-10-16 21:38:39 +06:30
int _selectedIndex = 1;
set selectedIndex(int index) {
_selectedIndex = index;
notifyListeners();
}
2021-09-10 15:22:11 +06:30
int get selectedIndex => _selectedIndex;
2020-10-16 13:46:02 +06:30
@override
void privilegeChanged() {
super.privilegeChanged();
}
2020-10-26 04:41:24 +06:30
initData(bool forCustomer, bool isCanceled, bool isPaid) {
_loadInvoices(forCustomer);
2020-10-16 21:38:39 +06:30
2020-10-26 04:41:24 +06:30
if (_paginator != null) _paginator.close();
_paginator = _getPaginator(forCustomer, isCanceled, isPaid);
_paginator.load(onFinished: () {
notifyListeners();
});
2020-10-16 21:38:39 +06:30
}
2020-10-26 04:41:24 +06:30
Future<void> _loadInvoices(bool forCustomer) async {
2020-10-16 21:38:39 +06:30
if (user == null) return;
2021-09-10 15:24:31 +06:30
if (!forCustomer && !user!.hasInvoices()) return;
2020-10-16 21:38:39 +06:30
String path = "/$invoices_collection";
2021-09-10 15:22:11 +06:30
if (listener != null) listener!.cancel();
2020-10-16 21:38:39 +06:30
_invoices = [];
2020-10-16 13:46:02 +06:30
try {
2021-09-10 15:22:11 +06:30
var q = FirebaseFirestore.instance
2020-10-16 13:46:02 +06:30
.collection("$path")
2020-10-26 04:41:24 +06:30
.where("status", isEqualTo: invoice_issued_status)
2020-10-16 21:38:39 +06:30
.where("is_deleted", isEqualTo: false);
if (forCustomer) {
2021-09-10 15:24:31 +06:30
q = q.where("user_id", isEqualTo: user!.id);
2020-10-16 21:38:39 +06:30
}
listener = q.snapshots().listen((QuerySnapshot snapshot) {
_invoices.clear();
2021-09-10 15:22:11 +06:30
_invoices = snapshot.docs.map((documentSnapshot) {
var s = Invoice.fromMap(documentSnapshot.data as Map<String, dynamic>,
documentSnapshot.id);
2020-10-16 13:46:02 +06:30
return s;
}).toList();
notifyListeners();
2020-06-02 14:56:51 +06:30
});
2020-10-16 13:46:02 +06:30
} catch (e) {
log.warning("Error!! $e");
}
}
2020-10-26 04:41:24 +06:30
Paginator _getPaginator(bool isCustomer, bool isCanceled, bool isPaid) {
2020-10-16 21:38:39 +06:30
if (!isCustomer) {
2021-09-10 15:24:31 +06:30
if (user == null || !(user!.hasInvoices())) throw "No privilege";
2020-10-16 13:46:02 +06:30
}
2021-09-10 15:22:11 +06:30
var pageQuery = FirebaseFirestore.instance
2020-10-26 04:41:24 +06:30
.collection("/$invoices_collection")
2020-10-16 21:38:39 +06:30
.where("is_deleted", isEqualTo: false);
if (isCustomer) {
2021-09-10 15:24:31 +06:30
pageQuery = pageQuery.where("user_id", isEqualTo: user!.id);
2020-10-16 13:46:02 +06:30
}
2020-10-26 04:41:24 +06:30
if (isCanceled) {
pageQuery = pageQuery.where("status", isEqualTo: invoice_cancel_status);
}
if (isPaid) {
pageQuery = pageQuery.where("status", isEqualTo: invoice_paid_status);
}
pageQuery = pageQuery.orderBy("created_at", descending: true);
2020-10-16 21:38:39 +06:30
var paginator = new Paginator(pageQuery, rowPerLoad: 20, toObj: (data, id) {
return Invoice.fromMap(data, id);
2020-10-16 13:46:02 +06:30
});
2020-10-16 21:38:39 +06:30
return paginator;
2020-06-02 14:56:51 +06:30
}
2021-09-10 15:22:11 +06:30
Future<void> loadMore({bool? isCustomer}) async {
2020-10-26 04:41:24 +06:30
if (_paginator.ended || _selectedIndex == 1)
2020-10-22 04:14:53 +06:30
return; // when paid menu is not selected return
2020-10-16 21:38:39 +06:30
isLoading = true;
notifyListeners();
2020-10-26 04:41:24 +06:30
await _paginator.load(onFinished: () {
2020-10-16 21:38:39 +06:30
isLoading = false;
2020-10-16 13:46:02 +06:30
notifyListeners();
2020-10-16 21:38:39 +06:30
});
}
2021-09-10 15:22:11 +06:30
Future<void> refresh({bool? isCustomer}) async {
2020-10-16 21:38:39 +06:30
if (_selectedIndex == 1) return; // when paid menu is not selected return
2020-10-26 04:41:24 +06:30
await _paginator.refresh(onFinished: () {
2020-10-16 21:38:39 +06:30
notifyListeners();
});
2020-06-02 14:56:51 +06:30
}
void initUser(user) {
super.initUser(user);
}
logout() async {
2020-10-26 04:41:24 +06:30
if (_paginator != null) _paginator.close();
2021-09-10 15:22:11 +06:30
if (listener != null) await listener!.cancel();
2020-10-16 21:38:39 +06:30
_invoices = [];
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) {
2021-09-10 15:22:11 +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
}