From 0e9e3da0c400530e7703904ac073ad3e542e8349 Mon Sep 17 00:00:00 2001 From: PhyoThandar Date: Fri, 16 Oct 2020 13:46:02 +0630 Subject: [PATCH] merge --- lib/domain/entities/invoice.dart | 21 +++- lib/pages/invoice/invoce_list.dart | 23 ++++- lib/pages/invoice/model/invoice_model.dart | 112 +++++++++++++++++++-- lib/pages/invoice/payment_pdf_screen.dart | 7 +- lib/pages/main/home_page.dart | 4 +- lib/pages/rates/custom_editor.dart | 4 +- lib/pages/rates/shipment_rates_edit.dart | 4 +- 7 files changed, 154 insertions(+), 21 deletions(-) diff --git a/lib/domain/entities/invoice.dart b/lib/domain/entities/invoice.dart index 7ce9636..15125c2 100644 --- a/lib/domain/entities/invoice.dart +++ b/lib/domain/entities/invoice.dart @@ -2,6 +2,7 @@ import 'package.dart'; import 'receipt.dart'; class Invoice { + String id; String invoiceNumber; DateTime invoiceDate; String customerName; @@ -16,7 +17,8 @@ class Invoice { List receiptPhotos; Invoice( - {this.invoiceNumber, + {this.id, + this.invoiceNumber, this.invoiceDate, this.customerName, this.customerPhoneNumber, @@ -29,4 +31,21 @@ class Invoice { this.receipts}); double get getAmount => packages.fold(0, (p, e) => (e.rate * e.weight) + p); + + factory Invoice.fromMap(Map map, String docID) { + return Invoice( + id: docID, + invoiceNumber: map['invoice_number'], + invoiceDate: map['invoice_date'], + customerName: map['customer_name'], + customerPhoneNumber: map['phone_number'], + amount: map['amount'], + status: map['status'], + discount: map['discount'], + paymentAttachment: map['payment_attachment'], + packages: map['packages'], + receiptPhotos: map['receiptPhotos'], + receipts: map['receipts'], + ); + } } diff --git a/lib/pages/invoice/invoce_list.dart b/lib/pages/invoice/invoce_list.dart index 2495b27..32cbf98 100644 --- a/lib/pages/invoice/invoce_list.dart +++ b/lib/pages/invoice/invoce_list.dart @@ -20,16 +20,29 @@ import 'invoice_editor.dart'; import 'invoice_list_row.dart'; class InvoiceList extends StatefulWidget { + final bool onlyFcs; + + const InvoiceList({Key key, this.onlyFcs}) : super(key: key); @override _InvoiceListState createState() => _InvoiceListState(); } class _InvoiceListState extends State { bool _isLoading = false; + bool _showPaid = false; + var _controller = ScrollController(); @override void initState() { super.initState(); + // Provider.of(context, listen: false).initPaidInvoice(true); + // _controller.addListener(() { + // if (_showPaid && + // _controller.position.pixels == _controller.position.maxScrollExtent) { + // Provider.of(context, listen: false) + // .loadMorePaidInvoices(); + // } + // }); } @override @@ -41,6 +54,9 @@ class _InvoiceListState extends State { Widget build(BuildContext context) { var owner = true; var invoiceModel = Provider.of(context); + bool onlyFcs = widget.onlyFcs; + var invoices = + _showPaid ? invoiceModel.paidInvoices : invoiceModel.invoices; final popupMenu = LocalPopupMenuButton( popmenus: [ @@ -49,7 +65,7 @@ class _InvoiceListState extends State { LocalPopupMenu(id: 2, textKey: "invoice.popupmenu.paid") ], popupMenuCallback: (p) => this.setState(() { - // _showDelivered = p.id == 2; + _showPaid = p.id == 2; }), ); @@ -95,10 +111,9 @@ class _InvoiceListState extends State { scrollDirection: Axis.vertical, padding: EdgeInsets.only(top: 15), shrinkWrap: true, - itemCount: invoiceModel.invoices.length, + itemCount: invoices.length, itemBuilder: (BuildContext context, int index) { - return InvoiceListRow( - invoice: invoiceModel.invoices[index]); + return InvoiceListRow(invoice: invoices[index]); }), ), ], diff --git a/lib/pages/invoice/model/invoice_model.dart b/lib/pages/invoice/model/invoice_model.dart index fee6e5e..5da7c8c 100644 --- a/lib/pages/invoice/model/invoice_model.dart +++ b/lib/pages/invoice/model/invoice_model.dart @@ -1,9 +1,19 @@ +import 'dart:async'; + +import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:fcs/domain/entities/invoice.dart'; import 'package:fcs/domain/entities/package.dart'; import 'package:fcs/domain/entities/receipt.dart'; +import 'package:fcs/helpers/pagination.dart'; import 'package:fcs/pages/main/model/base_model.dart'; +import 'package:logging/logging.dart'; class InvoiceModel extends BaseModel { + final log = Logger('InvoiceModel'); + + Pagination pagination; + StreamSubscription listener; + List invoices = [ Invoice( invoiceNumber: 'A092(A)-30', @@ -137,19 +147,92 @@ class InvoiceModel extends BaseModel { ]) ]; - List get pending { - List _i = invoices.where((e) => e.status == "Pending").toList() - ..sort((e1, e2) { - return e2.invoiceNumber.compareTo(e1.invoiceNumber); - }); - return _i; + List paidInvoices = []; + + bool endOfPaidInvoices = false; + bool isLoading = false; + + @override + void privilegeChanged() { + super.privilegeChanged(); + // _loadInvoices(); } - List get paided { - return invoices.where((e) => e.status == "Paid").toList() - ..sort((e1, e2) { - return e2.invoiceNumber.compareTo(e1.invoiceNumber); + Future _loadInvoices() async { + if (user == null || !user.hasInvoices()) return; + String path = "/$invoices/"; + if (listener != null) listener.cancel(); + invoices = []; + try { + listener = Firestore.instance + .collection("$path") + .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"); + } + } + + Future initPaidInvoice(bool onlyFcs) { + if (onlyFcs) { + if (user == null || + !((user.hasPackages() || + user.hasReceiving() || + user.hasProcessing()))) return null; + } + if (pagination != null) pagination.close(); + paidInvoices = []; + endOfPaidInvoices = false; + isLoading = false; + + var pageQuery = Firestore.instance + .collection("/$invoices"); + // .collection( + // "/users/8OTfsbVvsUOn1SLxy1OrKk7Y_yNKkVoGalPcIlcHnAY/messages") + // .orderBy("date", descending: true); + // .where("is_delivered", isEqualTo: true) + // .where("is_deleted", isEqualTo: false); + if (!onlyFcs) { + pageQuery = pageQuery.where("user_id", isEqualTo: user.id); + } + // pageQuery = pageQuery.orderBy("current_status_date", descending: true); + pagination = new Pagination(pageQuery, rowPerLoad: 20); + pagination.stream.listen((doc) { + if (doc == null) { + endOfPaidInvoices = true; + } else { + paidInvoices.add(Invoice.fromMap(doc.data, doc.documentID)); + // var m = Message.fromMap(doc.data, doc.documentID); + // deliveredPackages.add(Package( + // id: m.id, + // status: package_delivered_status, + // currentStatus: package_delivered_status, + // currentStatusDate: m.date, + // trackingID: (count++).toString(), + // market: m.message)); + } + }); + return null; + } + + Future loadMorePaidInvoices() { + if (pagination != null && !isLoading && !endOfPaidInvoices) { + isLoading = true; + notifyListeners(); + pagination.load().then((value) { + isLoading = false; + notifyListeners(); + }); + } + return null; } void initUser(user) { @@ -158,6 +241,15 @@ class InvoiceModel extends BaseModel { @override logout() async { + if (listener != null) await listener.cancel(); invoices = []; } + + Future create(Invoice invoice) { + // return Services.instance.fcsShipmentService.createFcsShipment(fcsShipment); + } + + Future update(Invoice invoice) { + // return Services.instance.fcsShipmentService.updateFcsShipment(fcsShipment); + } } diff --git a/lib/pages/invoice/payment_pdf_screen.dart b/lib/pages/invoice/payment_pdf_screen.dart index a341664..a30ff6a 100644 --- a/lib/pages/invoice/payment_pdf_screen.dart +++ b/lib/pages/invoice/payment_pdf_screen.dart @@ -5,6 +5,7 @@ import 'package:fcs/pages/widgets/local_text.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter_pdfview/flutter_pdfview.dart'; +import 'package:image_downloader/image_downloader.dart'; class PaymentPDFScreen extends StatefulWidget { final String path; @@ -39,7 +40,11 @@ class _PaymentPDFScreenState extends State actions: [ IconButton( icon: Icon(Icons.file_download), - onPressed: () {}, + onPressed: () async { + print('image path => ${widget.path}'); + await ImageDownloader.downloadImage(widget.path, + destination: AndroidDestinationType.directoryDownloads); + }, ), IconButton( icon: Icon(Icons.share), diff --git a/lib/pages/main/home_page.dart b/lib/pages/main/home_page.dart index 21389b6..b1f6595 100644 --- a/lib/pages/main/home_page.dart +++ b/lib/pages/main/home_page.dart @@ -297,8 +297,8 @@ class _HomePageState extends State { CupertinoPageRoute(builder: (context) => InvoiceList()))); final invoicesBtnFcs = TaskButton("invoices.btn", icon: FontAwesomeIcons.fileInvoice, - btnCallback: () => Navigator.of(context).push( - CupertinoPageRoute(builder: (context) => InvoiceList()))); + btnCallback: () => Navigator.of(context).push(CupertinoPageRoute( + builder: (context) => InvoiceList(onlyFcs: true)))); final discountBtn = TaskButton("discount.btn", icon: Entypo.price_ribbon, diff --git a/lib/pages/rates/custom_editor.dart b/lib/pages/rates/custom_editor.dart index 29e6288..2b07eb2 100644 --- a/lib/pages/rates/custom_editor.dart +++ b/lib/pages/rates/custom_editor.dart @@ -108,6 +108,7 @@ class _CustomEditorState extends State { CustomDuty _customduty = CustomDuty( productType: _productController.text, fee: double.parse(_feeController.text)); + print('_customduty => $_customduty'); if (_isNew) { await shipmentRateModel.addCustomDuty(_customduty); } else { @@ -125,8 +126,7 @@ class _CustomEditorState extends State { } _delete() { - showConfirmDialog( - context, "cargo.edit.delete.confirm", _deleteCustomDuty); + showConfirmDialog(context, "cargo.edit.delete.confirm", _deleteCustomDuty); } _deleteCustomDuty() async { diff --git a/lib/pages/rates/shipment_rates_edit.dart b/lib/pages/rates/shipment_rates_edit.dart index 7a6e138..18a6ccc 100644 --- a/lib/pages/rates/shipment_rates_edit.dart +++ b/lib/pages/rates/shipment_rates_edit.dart @@ -307,10 +307,12 @@ class _ShipmentRatesEditState extends State { try { var shipmentRateModel = Provider.of(context, listen: false); - Rate _rate = Rate( + Rate _rate = new Rate( deliveryFee: double.parse(_deliveryFee.text), freeDeliveryWeight: double.parse(_minWeight.text), volumetricRatio: double.parse(_volumetricRatio.text)); + Rate r = new Rate(); + print('_rate =>$r'); await shipmentRateModel.updateRate(_rate); Navigator.pop(context); } catch (e) {