141 lines
3.7 KiB
Dart
141 lines
3.7 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:cloud_firestore/cloud_firestore.dart';
|
|
import 'package:fcs/domain/constants.dart';
|
|
import 'package:fcs/domain/entities/invoice.dart';
|
|
import 'package:fcs/domain/entities/receipt.dart';
|
|
import 'package:fcs/helpers/paginator.dart';
|
|
import 'package:fcs/pages/main/model/base_model.dart';
|
|
import 'package:logging/logging.dart';
|
|
|
|
class InvoiceModel extends BaseModel {
|
|
final log = Logger('InvoiceModel');
|
|
|
|
StreamSubscription<QuerySnapshot> listener;
|
|
|
|
List<Invoice> _invoices = [
|
|
Invoice(
|
|
invoiceNumber: 'A092(A)-33',
|
|
invoiceDate: DateTime(2020, 4, 6, 12, 15),
|
|
userName: 'Ko Myo Min',
|
|
phoneNumber: '+959 555555555',
|
|
amount: 300,
|
|
status: 'Pending',
|
|
)
|
|
];
|
|
|
|
List<Invoice> get invoices =>
|
|
_selectedIndex == 1 ? _invoices : List<Invoice>.from(_paid.values);
|
|
|
|
Paginator _paid;
|
|
|
|
bool endOfPaidInvoices = false;
|
|
bool isLoading = false;
|
|
int _selectedIndex = 1;
|
|
|
|
set selectedIndex(int index) {
|
|
_selectedIndex = index;
|
|
notifyListeners();
|
|
}
|
|
|
|
get selectedIndex => _selectedIndex;
|
|
|
|
@override
|
|
void privilegeChanged() {
|
|
super.privilegeChanged();
|
|
}
|
|
|
|
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";
|
|
if (listener != null) listener.cancel();
|
|
_invoices = [];
|
|
|
|
try {
|
|
var q = Firestore.instance
|
|
.collection("$path")
|
|
.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) {
|
|
var s = Invoice.fromMap(
|
|
documentSnapshot.data, documentSnapshot.documentID);
|
|
return s;
|
|
}).toList();
|
|
notifyListeners();
|
|
});
|
|
} catch (e) {
|
|
log.warning("Error!! $e");
|
|
}
|
|
}
|
|
|
|
Paginator _getPaid(bool isCustomer) {
|
|
if (!isCustomer) {
|
|
if (user == null || !(user.hasInvoices())) throw "No privilege";
|
|
}
|
|
var pageQuery = Firestore.instance
|
|
.collection("/$packages_collection")
|
|
.where("is_delivered", isEqualTo: true)
|
|
.where("is_deleted", isEqualTo: false);
|
|
if (isCustomer) {
|
|
pageQuery = pageQuery.where("user_id", isEqualTo: user.id);
|
|
}
|
|
pageQuery = pageQuery.orderBy("status_date", descending: true);
|
|
var paginator = new Paginator(pageQuery, rowPerLoad: 20, toObj: (data, id) {
|
|
return Invoice.fromMap(data, id);
|
|
});
|
|
return paginator;
|
|
}
|
|
|
|
Future<void> loadMore({bool isCustomer}) async {
|
|
if (_paid.ended || _selectedIndex == 1)
|
|
return; // when paid menu is not selected return
|
|
isLoading = true;
|
|
notifyListeners();
|
|
await _paid.load(onFinished: () {
|
|
isLoading = false;
|
|
notifyListeners();
|
|
});
|
|
}
|
|
|
|
Future<void> refresh({bool isCustomer}) async {
|
|
if (_selectedIndex == 1) return; // when paid menu is not selected return
|
|
await _paid.refresh(onFinished: () {
|
|
notifyListeners();
|
|
});
|
|
}
|
|
|
|
void initUser(user) {
|
|
super.initUser(user);
|
|
}
|
|
|
|
logout() async {
|
|
if (_paid != null) _paid.close();
|
|
if (listener != null) await listener.cancel();
|
|
_invoices = [];
|
|
}
|
|
|
|
Future<void> createInvoice(Invoice invoice) {
|
|
// return Services.instance.invoiceService.createInvoice(invoice);
|
|
}
|
|
|
|
Future<void> updateInvoice(Invoice invoice) {
|
|
// return Services.instance.invoiceService.updateInvoice(invoice);
|
|
}
|
|
}
|