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

141 lines
3.7 KiB
Dart
Raw Normal View History

2020-10-16 13:46:02 +06:30
import 'dart:async';
import 'package:cloud_firestore/cloud_firestore.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';
import 'package:fcs/domain/entities/receipt.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-06-02 14:56:51 +06:30
class InvoiceModel extends BaseModel {
2020-10-16 13:46:02 +06:30
final log = Logger('InvoiceModel');
StreamSubscription<QuerySnapshot> listener;
2020-10-16 21:38:39 +06:30
List<Invoice> _invoices = [
2020-06-02 14:56:51 +06:30
Invoice(
2020-10-24 06:14:07 +06:30
invoiceNumber: 'A092(A)-33',
invoiceDate: DateTime(2020, 4, 6, 12, 15),
userName: 'Ko Myo Min',
phoneNumber: '+959 555555555',
amount: 300,
status: 'Pending',
)
2020-06-02 14:56:51 +06:30
];
2020-10-16 21:38:39 +06:30
List<Invoice> get invoices =>
_selectedIndex == 1 ? _invoices : List<Invoice>.from(_paid.values);
Paginator _paid;
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();
}
get selectedIndex => _selectedIndex;
2020-10-16 13:46:02 +06:30
@override
void privilegeChanged() {
super.privilegeChanged();
}
2020-10-16 21:38:39 +06:30
initData(bool forCustomer) {
_selectedIndex = 1;
// _loadFcsInvoices(forCustomer);
if (_paid != null) _paid.close();
_paid = _getPaid(forCustomer);
}
Future<void> _loadFcsInvoices(bool forCustomer) async {
if (user == null) return;
if (!forCustomer && !user.hasInvoices()) return;
String path = "/$invoices_collection";
2020-10-16 13:46:02 +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 {
2020-10-16 21:38:39 +06:30
var q = Firestore.instance
2020-10-16 13:46:02 +06:30
.collection("$path")
2020-10-16 21:38:39 +06:30
.where("is_paid", isEqualTo: false)
.where("is_deleted", isEqualTo: false);
if (forCustomer) {
q = q.where("user_id", isEqualTo: user.id);
}
listener = q.snapshots().listen((QuerySnapshot snapshot) {
_invoices.clear();
_invoices = snapshot.documents.map((documentSnapshot) {
2020-10-16 13:46:02 +06:30
var s = Invoice.fromMap(
documentSnapshot.data, documentSnapshot.documentID);
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-16 21:38:39 +06:30
Paginator _getPaid(bool isCustomer) {
if (!isCustomer) {
if (user == null || !(user.hasInvoices())) throw "No privilege";
2020-10-16 13:46:02 +06:30
}
var pageQuery = Firestore.instance
2020-10-16 21:38:39 +06:30
.collection("/$packages_collection")
.where("is_delivered", isEqualTo: true)
.where("is_deleted", isEqualTo: false);
if (isCustomer) {
2020-10-16 13:46:02 +06:30
pageQuery = pageQuery.where("user_id", isEqualTo: user.id);
}
2020-10-19 05:13:49 +06:30
pageQuery = pageQuery.orderBy("status_date", 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
}
2020-10-16 21:38:39 +06:30
Future<void> loadMore({bool isCustomer}) async {
2020-10-22 04:14:53 +06:30
if (_paid.ended || _selectedIndex == 1)
return; // when paid menu is not selected return
2020-10-16 21:38:39 +06:30
isLoading = true;
notifyListeners();
await _paid.load(onFinished: () {
isLoading = false;
2020-10-16 13:46:02 +06:30
notifyListeners();
2020-10-16 21:38:39 +06:30
});
}
Future<void> refresh({bool isCustomer}) async {
if (_selectedIndex == 1) return; // when paid menu is not selected return
await _paid.refresh(onFinished: () {
notifyListeners();
});
2020-06-02 14:56:51 +06:30
}
void initUser(user) {
super.initUser(user);
}
logout() async {
2020-10-16 21:38:39 +06:30
if (_paid != null) _paid.close();
2020-10-16 13:46:02 +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
2020-10-16 21:38:39 +06:30
Future<void> createInvoice(Invoice invoice) {
// 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) {
// return Services.instance.invoiceService.updateInvoice(invoice);
2020-10-16 13:46:02 +06:30
}
2020-06-02 14:56:51 +06:30
}