add invoice pdf

This commit is contained in:
Sai Naw Wun
2020-10-26 04:41:24 +06:30
parent feec3c8687
commit 2786acfd08
33 changed files with 787 additions and 1114 deletions

View File

@@ -1,33 +1,30 @@
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:fcs/config.dart';
import 'package:fcs/data/services/services.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/api_helper.dart';
import 'package:fcs/helpers/firebase_helper.dart';
import 'package:fcs/helpers/paginator.dart';
import 'package:fcs/pages/main/model/base_model.dart';
import 'package:logging/logging.dart';
import 'package:path_provider/path_provider.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> _invoices = [];
List<Invoice> get invoices =>
_selectedIndex == 1 ? _invoices : List<Invoice>.from(_paid.values);
_selectedIndex == 1 ? _invoices : List<Invoice>.from(_paginator.values);
Paginator _paid;
Paginator _paginator;
bool endOfPaidInvoices = false;
bool isLoading = false;
@@ -45,15 +42,17 @@ class InvoiceModel extends BaseModel {
super.privilegeChanged();
}
initData(bool forCustomer) {
_selectedIndex = 1;
// _loadFcsInvoices(forCustomer);
initData(bool forCustomer, bool isCanceled, bool isPaid) {
_loadInvoices(forCustomer);
if (_paid != null) _paid.close();
_paid = _getPaid(forCustomer);
if (_paginator != null) _paginator.close();
_paginator = _getPaginator(forCustomer, isCanceled, isPaid);
_paginator.load(onFinished: () {
notifyListeners();
});
}
Future<void> _loadFcsInvoices(bool forCustomer) async {
Future<void> _loadInvoices(bool forCustomer) async {
if (user == null) return;
if (!forCustomer && !user.hasInvoices()) return;
String path = "/$invoices_collection";
@@ -63,7 +62,7 @@ class InvoiceModel extends BaseModel {
try {
var q = Firestore.instance
.collection("$path")
.where("is_paid", isEqualTo: false)
.where("status", isEqualTo: invoice_issued_status)
.where("is_deleted", isEqualTo: false);
if (forCustomer) {
@@ -84,18 +83,24 @@ class InvoiceModel extends BaseModel {
}
}
Paginator _getPaid(bool isCustomer) {
Paginator _getPaginator(bool isCustomer, bool isCanceled, bool isPaid) {
if (!isCustomer) {
if (user == null || !(user.hasInvoices())) throw "No privilege";
}
var pageQuery = Firestore.instance
.collection("/$packages_collection")
.where("is_delivered", isEqualTo: true)
.collection("/$invoices_collection")
.where("is_deleted", isEqualTo: false);
if (isCustomer) {
pageQuery = pageQuery.where("user_id", isEqualTo: user.id);
}
pageQuery = pageQuery.orderBy("status_date", descending: true);
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);
var paginator = new Paginator(pageQuery, rowPerLoad: 20, toObj: (data, id) {
return Invoice.fromMap(data, id);
});
@@ -103,11 +108,11 @@ class InvoiceModel extends BaseModel {
}
Future<void> loadMore({bool isCustomer}) async {
if (_paid.ended || _selectedIndex == 1)
if (_paginator.ended || _selectedIndex == 1)
return; // when paid menu is not selected return
isLoading = true;
notifyListeners();
await _paid.load(onFinished: () {
await _paginator.load(onFinished: () {
isLoading = false;
notifyListeners();
});
@@ -115,7 +120,7 @@ class InvoiceModel extends BaseModel {
Future<void> refresh({bool isCustomer}) async {
if (_selectedIndex == 1) return; // when paid menu is not selected return
await _paid.refresh(onFinished: () {
await _paginator.refresh(onFinished: () {
notifyListeners();
});
}
@@ -125,16 +130,24 @@ class InvoiceModel extends BaseModel {
}
logout() async {
if (_paid != null) _paid.close();
if (_paginator != null) _paginator.close();
if (listener != null) await listener.cancel();
_invoices = [];
}
Future<void> createInvoice(Invoice invoice) {
// return Services.instance.invoiceService.createInvoice(invoice);
Future<void> createInvoice(Invoice invoice) async {
File file = await downloadPDF("invoice", invoice.toMap());
String url = await uploadStorage("pdfs", file);
print("uploaded url: $url");
invoice.invoiceURL = url;
return Services.instance.invoiceService.createInvoice(invoice);
}
Future<void> updateInvoice(Invoice invoice) {
// return Services.instance.invoiceService.updateInvoice(invoice);
return Services.instance.invoiceService.updateInvoice(invoice);
}
Future<void> cancelInvoice(Invoice invoice) {
return Services.instance.invoiceService.cancelInvoice(invoice);
}
}