From feec3c8687ce392825830bf9811d292fdfa0070e Mon Sep 17 00:00:00 2001 From: Sai Naw Wun Date: Sat, 24 Oct 2020 06:14:07 +0630 Subject: [PATCH] update invoice page --- assets/local/localization_en.json | 19 +- assets/local/localization_mu.json | 11 +- lib/domain/entities/cargo_type.dart | 5 +- lib/domain/entities/carton.dart | 28 +- lib/domain/entities/invoice.dart | 132 +++--- lib/domain/entities/receipt.dart | 2 - lib/domain/entities/shipment.dart | 10 + lib/pages/carton/carton_editor.dart | 41 +- lib/pages/carton/model/carton_model.dart | 5 +- lib/pages/discount/model/discount_model.dart | 25 +- lib/pages/invoice/invoice_cargo_table.dart | 350 ---------------- lib/pages/invoice/invoice_carton_table.dart | 7 +- lib/pages/invoice/invoice_discount_table.dart | 88 ++++ lib/pages/invoice/invoice_editor.dart | 342 ++++++++++------ lib/pages/invoice/invoice_info.dart | 6 +- lib/pages/invoice/invoice_shipment_table.dart | 105 +++++ lib/pages/invoice/invoice_table.dart | 377 ++++++++++++++++++ lib/pages/invoice/model/invoice_model.dart | 43 +- lib/pages/invoice/payment_page.dart | 2 +- lib/pages/shipment/model/shipment_model.dart | 24 ++ lib/pages/widgets/fcs_icons.dart | 5 + .../widgets/local_popup_menu_button.dart | 6 +- 22 files changed, 996 insertions(+), 637 deletions(-) delete mode 100644 lib/pages/invoice/invoice_cargo_table.dart create mode 100644 lib/pages/invoice/invoice_discount_table.dart create mode 100644 lib/pages/invoice/invoice_shipment_table.dart create mode 100644 lib/pages/invoice/invoice_table.dart create mode 100644 lib/pages/widgets/fcs_icons.dart diff --git a/assets/local/localization_en.json b/assets/local/localization_en.json index 0ae209d..17d9baf 100644 --- a/assets/local/localization_en.json +++ b/assets/local/localization_en.json @@ -393,13 +393,13 @@ "rate.delivery_fee":"Delivery fees", "rate.total_estimated_amount":"Total estimated amount", "rate.volumetric_ratio":"Volumetric ratio", - "rate.custom.form.title":"CUSTOM", + "rate.custom.form.title":"Custom", "rate.cutom.product_type":"Product type", "rate.custom.fee":"Fee", "rate.discount.weight":"Weight", "rate.discount.rate":"Discount rate", - "rate.custom_duty.title":"Custom Duties", - "rate.custom_duty":"Custom Duty", + "rate.custom_duty.title":"Custom Fee", + "rate.custom_duty":"Custom Fee", "rate.cargo.type":"Cargo Types", "rate.discount_by_weight":"Discounts by weight", "rate.discount_by_weight.edit.delete.confirm":"Delete this discount by weight?", @@ -428,6 +428,8 @@ "invoice.customer_name":"Customer name", "invoice.status":"Status", "invoice.amount":"Amount", + "invoice.weight":"Weight(lb)", + "invoice.rate":"Rate(USD)", "invoice.total":"Total amount", "invoice.balance":"Balance", "invoice.handling_fee":"Handling fee", @@ -435,15 +437,16 @@ "invoice.custom_fee_desc":"Custom fee description", "invoice.discount":"Discount code", "invoice.payment_method":"Payment method", - "invoice.delivery_fee":"Delivery fee : ", + "invoice.delivery_fee":"Delivery fee", "invoice.payment_attachment":"Payment attachment", - "invoice.box_info":"Carton information", + "invoice.box_info":"Cartons", "invoice.cargo_table":"Cargo table", "invoice.btn_create":"Create invoice", "invoice.btn_save":"Save invoice", "invoice.btn_payment_receipt":"Attachment payment receipt", "invoice.description": "Description", "invoice.box.cargo_type": "Cargo types", + "invoice.box.desc": "Description", "invoice.cargo_type":"Cargo types", "invoice.box.number":"Carton number", "invoice.box.length":"L", @@ -459,6 +462,12 @@ "invoice.popupmenu.paid":"Paid", "invoice.shipment.title":"Select FCS shipment", "invoice.customer.title":"Select Customer", + "invoice.add.custom.fee.menu":"Custom fee", + "invoice.add.handling.fee.menu":"Handling fee", + "invoice.add.discount.menu":"Discount", + "invoice.shipment.handling.fee.title":"Select shipment", + "invoice.shipment.number":"Shipment number", + "invoice.shipment.discount.title":"Select discount", "Invoices End ================================================================":"", "Discount Start ================================================================":"", diff --git a/assets/local/localization_mu.json b/assets/local/localization_mu.json index 2e9273c..1228908 100644 --- a/assets/local/localization_mu.json +++ b/assets/local/localization_mu.json @@ -428,6 +428,8 @@ "invoice.customer_name":"ဝယ်ယူသူနာမည်", "invoice.status":"အခြေအနေ", "invoice.amount":"ပမာဏ", + "invoice.weight":"Weight(lb)", + "invoice.rate":"Rate(USD)", "invoice.total":"စုစုပေါင်းပမာဏ", "invoice.balance":"ပေးချေရန်ကျန်ရှိငွေ", "invoice.handling_fee":"ထိန်းသိမ်းခ", @@ -437,7 +439,7 @@ "invoice.payment_method":"ငွေပေးချေစနစ်", "invoice.delivery_fee":"ပို့ဆောင်ခ", "invoice.payment_attachment":"ပေးချေပြီးဖိုင်များ", - "invoice.box_info":"Box အချက်အလက်", + "invoice.box_info":"Cartons", "invoice.cargo_table":"ကုန်ပစ္စည်းဇယား", "invoice.btn_create":"ငွေတောင်းခံလွှာ ပြုလုပ်ရန်", "invoice.btn_save":"ငွေတောင်းခံလွှာ သိမ်းရန်", @@ -445,6 +447,7 @@ "invoice.box.cargo_type": "ကုန်ပစ္စည်းအမျိုးအစားများ", "invoice.description": "ငွေတောင်းခံလွှာအကြောင်းအရာ", "invoice.cargo_type":"Cargo Types", + "invoice.box.desc": "Description", "invoice.box.number":"Box နံပါတ်", "invoice.box.length":"L", "invoice.box.width":"W", @@ -459,6 +462,12 @@ "invoice.popupmenu.paid":"Paid", "invoice.shipment.title":"Select FCS shipment", "invoice.customer.title":"Select Customer", + "invoice.add.custom.fee.menu":"Custom fee", + "invoice.add.handling.fee.menu":"Handling fee", + "invoice.add.discount.menu":"Discount", + "invoice.shipment.number":"Shipment number", + "invoice.shipment.handling.fee.title":"Select shipment", + "invoice.shipment.discount.title":"Select discount", "Invoices End ================================================================":"", "Discount Start ================================================================":"", diff --git a/lib/domain/entities/cargo_type.dart b/lib/domain/entities/cargo_type.dart index d502d62..f6a4ea0 100644 --- a/lib/domain/entities/cargo_type.dart +++ b/lib/domain/entities/cargo_type.dart @@ -4,7 +4,6 @@ class CargoType { double rate; double weight; - int shipmentWeight; double amount; double calRate; @@ -16,9 +15,10 @@ class CargoType { name: map['name'], rate: map['rate']?.toDouble() ?? 0, weight: map['weight']?.toDouble() ?? 0, + calWeight: map['cal_weight']?.toDouble() ?? 0, ); } - CargoType({this.id, this.name, this.rate, this.weight}); + CargoType({this.id, this.name, this.rate, this.weight, this.calWeight}); Map toMap() { return { @@ -26,6 +26,7 @@ class CargoType { 'name': name, 'rate': rate, 'weight': weight, + 'cal_weight': calWeight, }; } diff --git a/lib/domain/entities/carton.dart b/lib/domain/entities/carton.dart index 609ed70..a11d949 100644 --- a/lib/domain/entities/carton.dart +++ b/lib/domain/entities/carton.dart @@ -1,6 +1,7 @@ import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:fcs/domain/entities/discount_by_weight.dart'; import 'package:fcs/domain/entities/rate.dart'; +import 'package:fcs/domain/entities/shipment.dart'; import 'package:fcs/domain/vo/shipment_status.dart'; import 'package:fcs/domain/vo/delivery_address.dart'; @@ -9,6 +10,7 @@ import 'package.dart'; class Carton { String id; + String shipmentID; String shipmentNumber; String senderFCSID; String senderName; @@ -47,9 +49,9 @@ class Carton { List packageIDs; List packages; List cargoTypes; - List cartons; DeliveryAddress deliveryAddress; + Shipment shipment; int get amount => rate != null && weight != null ? rate * weight : 0; @@ -73,6 +75,23 @@ class Carton { return (length * width * height) / volumetricRatio; } + /// getCargoTypeForCalWeight returns carton with shipment weight + List getCargoTypeForCalWeight(double volumetricRatio) { + // get shipment weight + double volume = (length ?? 0) * (width ?? 0) * (height ?? 0); + double sw = volume / volumetricRatio ?? 0; + + // get actual weight + double aw = cargoTypes.fold(0.0, (p, c) => p + c.weight); + if (aw == 0 || sw == 0) return []; + + cargoTypes.forEach((e) { + double calWeight = aw > sw ? e.weight : e.weight / aw * sw; + e.calWeight = calWeight; + }); + return cargoTypes; + } + /// calAmount returns total amount double calAmount(Rate rate) { // get shipment weight @@ -101,6 +120,7 @@ class Carton { Carton( {this.id, + this.shipmentID, this.shipmentNumber, this.senderFCSID, this.senderName, @@ -133,7 +153,6 @@ class Carton { this.cartonNumber, this.fcsShipmentID, this.fcsShipmentNumber, - this.cartons, this.packageIDs, this.mixCartonID, this.mixCartonNumber, @@ -143,7 +162,6 @@ class Carton { Map toMap() { List _cargoTypes = cargoTypes.map((c) => c.toMap()).toList(); List _packages = packages?.map((c) => c.toJson())?.toList(); - List _cartons = cartons?.map((c) => c.toMap())?.toList() ?? []; return { "id": id, 'fcs_shipment_id': fcsShipmentID, @@ -155,8 +173,7 @@ class Carton { 'height': height, 'delivery_address': deliveryAddress.toMap(), 'carton_type': cartonType, - 'cartons': _cartons, - 'mix_carton_id': mixCartonID + 'mix_carton_id': mixCartonID, }; } @@ -171,6 +188,7 @@ class Carton { return Carton( id: docID, arrivedDate: _arrivedDate != null ? _arrivedDate.toDate() : null, + shipmentID: map['shipment_id'], shipmentNumber: map['shipment_number'], receiverNumber: map['receiver_number'], boxNumber: map['box_number'], diff --git a/lib/domain/entities/invoice.dart b/lib/domain/entities/invoice.dart index 8d63e51..5bc0eb9 100644 --- a/lib/domain/entities/invoice.dart +++ b/lib/domain/entities/invoice.dart @@ -3,63 +3,59 @@ import 'package:fcs/domain/entities/carton.dart'; import 'package:fcs/domain/entities/custom_duty.dart'; import 'package:fcs/domain/entities/discount.dart'; import 'package:fcs/domain/entities/discount_by_weight.dart'; +import 'package:fcs/domain/entities/payment_method.dart'; import 'package:fcs/domain/entities/rate.dart'; - -import 'package.dart'; -import 'receipt.dart'; +import 'package:fcs/domain/entities/shipment.dart'; class Invoice { String id; String invoiceNumber; DateTime invoiceDate; - String customerName; - String customerPhoneNumber; - double amount; + String fcsShipmentID; + String userID; + String userName; + String phoneNumber; String status; - String paymentAttachment; + double handlingFee; double deliveryFee; double paidAmount; + double amount; - List packages; - List receipts; - List receiptPhotos; List customDuties; List cartons; + List cargoTypes; + List shipments; Discount discount; + PaymentMethod paymentMethod; List getCargoTypes(Rate rate) { List cargoTypes = []; - double actualWeight = 0; - double shipmentWeight = 0; - cartons.forEach((c) { - c.cargoTypes.forEach((tc) { - if (cargoTypes.contains(tc)) { - CargoType existing = cargoTypes.firstWhere((wc) => wc.id == tc.id); - existing.weight += tc.weight; - } else { - cargoTypes.add(tc.clone()); - } - actualWeight += tc.weight; - }); - double volume = (c.length ?? 0) * (c.width ?? 0) * (c.height ?? 0); - double sw = volume / rate.volumetricRatio ?? 0; - shipmentWeight += sw; + double totalCalWeight = 0; + cartons.forEach((carton) { + if (carton.isChecked) { + var _cartonsTypes = + carton.getCargoTypeForCalWeight(rate.volumetricRatio); + _cartonsTypes.forEach((ct) { + if (cargoTypes.contains(ct)) { + CargoType existing = cargoTypes.firstWhere((wc) => wc.id == ct.id); + existing.calWeight += ct.calWeight; + } else { + cargoTypes.add(ct.clone()); + } + totalCalWeight += ct.calWeight; + }); + } }); - DiscountByWeight discountByWeight = rate.getDiscountByWeight( - shipmentWeight > actualWeight ? shipmentWeight : actualWeight); + DiscountByWeight discountByWeight = + rate.getDiscountByWeight(totalCalWeight); cargoTypes.forEach((e) { - print(actualWeight > shipmentWeight); - double cargoWeight = actualWeight > shipmentWeight - ? e.weight - : e.weight / actualWeight * shipmentWeight; double r = e.rate - (discountByWeight != null ? discountByWeight.discount : 0); - double amount = cargoWeight * r; + double amount = e.calWeight * r; e.calRate = r; - e.calWeight = cargoWeight; e.amount = amount; }); return cargoTypes; @@ -81,6 +77,12 @@ class Invoice { return total; } + double getHandlingFee() { + return shipments?.where((sh) => sh.isSelected)?.fold(0, (p, s) { + return p + (s?.handlingFee ?? 0) - (s?.paidHandlingFee ?? 0); + }); + } + double getTotalBalance(Rate rate) { return getNetAmount(rate) - (paidAmount ?? 0); } @@ -89,49 +91,57 @@ class Invoice { return customDuties == null ? 0 : customDuties.fold(0, (p, d) => p + d.fee); } - double getHandlingFee() { - return handlingFee == null ? 0 : handlingFee; - } - double getDeliveryFee() { return deliveryFee == null ? 0 : deliveryFee; } double getDiscount() => discount == null ? 0 : discount.amount; - Invoice( - {this.id, - this.invoiceNumber, - this.invoiceDate, - this.customerName, - this.customerPhoneNumber, - this.amount, - this.discount, - this.status, - this.paymentAttachment, - this.packages, - this.receiptPhotos, - this.customDuties, - this.cartons, - this.handlingFee, - this.receipts}); - - double get getAmount => packages.fold(0, (p, e) => (e.rate * e.weight) + p); + Invoice({ + this.id, + this.invoiceNumber, + this.invoiceDate, + this.userName, + this.phoneNumber, + this.amount, + this.discount, + this.status, + this.customDuties, + this.cartons, + this.cargoTypes, + this.handlingFee, + this.fcsShipmentID, + this.shipments, + }); 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'], + userName: map['user_name'], + phoneNumber: 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'], ); } + + Map toMap() { + List _cargoTypes = cargoTypes.map((c) => c.toMap()).toList(); + List _customDuties = customDuties?.map((c) => c.toMap())?.toList(); + List _cartons = cartons?.map((c) => c.toMap())?.toList() ?? []; + return { + "id": id, + "invoice_date": invoiceDate, + "user_id": userID, + 'fcs_shipment_id': fcsShipmentID, + 'cargo_types': _cargoTypes, + 'custom_duties': _customDuties, + 'cartons': _cartons, + 'discount': discount?.toMap(), + 'handling_fee': handlingFee, + 'delivery_fee': deliveryFee, + }; + } } diff --git a/lib/domain/entities/receipt.dart b/lib/domain/entities/receipt.dart index 854b12d..6fffbbe 100644 --- a/lib/domain/entities/receipt.dart +++ b/lib/domain/entities/receipt.dart @@ -1,5 +1,3 @@ -import 'package:flutter_local_notifications/flutter_local_notifications.dart'; - class Receipt { String id; int amount; diff --git a/lib/domain/entities/shipment.dart b/lib/domain/entities/shipment.dart index 3a71a72..e07c3ff 100644 --- a/lib/domain/entities/shipment.dart +++ b/lib/domain/entities/shipment.dart @@ -18,6 +18,7 @@ class Shipment { int numberOfPackage; int weight; double handlingFee; + double paidHandlingFee; String address; String status; bool isCourier; @@ -31,6 +32,7 @@ class Shipment { String fcsShipmentID; String fcsShipmentNumber; String shipmentLabelUrl; + bool isSelected; Shipment( {this.id, @@ -44,6 +46,7 @@ class Shipment { this.numberOfPackage, this.weight, this.handlingFee, + this.paidHandlingFee, this.address, this.status, this.pickupDate, @@ -89,6 +92,7 @@ class Shipment { pickupUserName: map['pickup_user_name'], pickupUserPhoneNumber: map['pickup_user_phone_number'], handlingFee: map['handling_fee'], + paidHandlingFee: map['paid_handling_fee'], fcsShipmentID: map['fcs_shipment_id'], fcsShipmentNumber: map['fcs_shipment_number'], shipmentLabelUrl: map['shipment_label_url'], @@ -116,6 +120,12 @@ class Shipment { }; } + @override + bool operator ==(Object other) => other is Shipment && other.id == id; + + @override + int get hashCode => id.hashCode; + @override String toString() { return 'PickUp{id:$id, userName:$userName,phoneNumber:$phoneNumber,numberOfPackage:$numberOfPackage,weight:$weight,status:$status}'; diff --git a/lib/pages/carton/carton_editor.dart b/lib/pages/carton/carton_editor.dart index 331016b..6577c30 100644 --- a/lib/pages/carton/carton_editor.dart +++ b/lib/pages/carton/carton_editor.dart @@ -89,14 +89,16 @@ class _CartonEditorState extends State { _user = User(fcsID: _carton.fcsID, name: _carton.userName); _loadPackages(); } else { - _carton = Carton(cargoTypes: [], packages: [], cartons: []); + _carton = Carton( + cargoTypes: [], + packages: [], + ); _lengthController.text = "12"; _widthController.text = "12"; _heightController.text = "12"; _isNew = true; _selectedCartonType = carton_from_packages; _loadFcsShipments(); - _loadMixCartons(); } } @@ -112,16 +114,6 @@ class _CartonEditorState extends State { }); } - _loadMixCartons() async { - if (_fcsShipment == null) return; - CartonModel cartonModel = Provider.of(context, listen: false); - var mixCartons = - await cartonModel.getMixCartonsByFcsShipment(_fcsShipment.id); - setState(() { - _mixCartons = mixCartons; - }); - } - _loadPackages() async { if (_user == null) return; PackageModel packageModel = @@ -159,19 +151,6 @@ class _CartonEditorState extends State { _populateDeliveryAddress(); } - _loadCartons() async { - if (_fcsShipment == null) return; - CartonModel cartonModel = Provider.of(context, listen: false); - List cartons = - await cartonModel.getCartonsByFcsShipment(_fcsShipment.id); - cartons.forEach((c) { - c.isChecked = true; - }); - setState(() { - _carton.cartons = cartons; - }); - } - _populateDeliveryAddress() { if (_carton.packages == null) return; var d = _carton.packages @@ -217,12 +196,6 @@ class _CartonEditorState extends State { setState(() { _fcsShipment = v; }); - if (_selectedCartonType == carton_mix_box) { - _loadCartons(); - } - if (_selectedCartonType == carton_small_bag) { - _loadMixCartons(); - } }, labelKey: "shipment.pack.fcs.shipment", iconData: Ionicons.ios_airplane, @@ -322,9 +295,6 @@ class _CartonEditorState extends State { setState(() { _selectedCartonType = v; }); - if (_selectedCartonType == carton_mix_box) { - _loadCartons(); - } }); final cargoTableTitleBox = LocalTitle( @@ -507,9 +477,6 @@ class _CartonEditorState extends State { carton.width = w; carton.height = h; carton.deliveryAddress = _deliveryAddress; - carton.cartons = _carton.cartons == null - ? [] - : _carton.cartons.where((c) => c.isChecked).toList(); setState(() { _isLoading = true; }); diff --git a/lib/pages/carton/model/carton_model.dart b/lib/pages/carton/model/carton_model.dart index c4f40fc..c503249 100644 --- a/lib/pages/carton/model/carton_model.dart +++ b/lib/pages/carton/model/carton_model.dart @@ -179,14 +179,15 @@ class CartonModel extends BaseModel { String path = "/$cartons_collection"; var querySnap = await Firestore.instance .collection(path) - // .where("fcs_shipment_id", isEqualTo: fcsShipmentID) + .where("fcs_shipment_id", isEqualTo: fcsShipmentID) .where("user_id", isEqualTo: userID) .where("is_deleted", isEqualTo: false) .where("is_invoiced", isEqualTo: false) .getDocuments(); - return querySnap.documents + List cartons = querySnap.documents .map((e) => Carton.fromMap(e.data, e.documentID)) .toList(); + return cartons; } Future> getMixCartonsByFcsShipment(String fcsShipmentID) async { diff --git a/lib/pages/discount/model/discount_model.dart b/lib/pages/discount/model/discount_model.dart index 1c425b6..5a25660 100644 --- a/lib/pages/discount/model/discount_model.dart +++ b/lib/pages/discount/model/discount_model.dart @@ -49,9 +49,9 @@ class DiscountModel extends BaseModel { .orderBy("code", descending: false) .snapshots() .listen((snaps) { - discounts.clear(); + _discounts.clear(); snaps.documents.forEach((d) { - discounts.add(Discount.fromMap(d.data, d.documentID)); + _discounts.add(Discount.fromMap(d.data, d.documentID)); }); notifyListeners(); }); @@ -73,6 +73,27 @@ class DiscountModel extends BaseModel { return paginator; } + Future> getDiscount(String userID) async { + String path = "/$discounts_collection"; + try { + var q = Firestore.instance + .collection("$path") + .where("customer_id", isEqualTo: userID) + .where("status", isEqualTo: "available"); + var snaps = await q.getDocuments(source: Source.server); + List discounts = snaps.documents.map((snap) { + if (snap.exists) { + var s = Discount.fromMap(snap.data, snap.documentID); + return s; + } + }).toList(); + return discounts; + } catch (e) { + log.warning("Error!! $e"); + } + return null; + } + Future loadMore() async { if (_used.ended || _selectedIndex == 1) return; isLoading = true; diff --git a/lib/pages/invoice/invoice_cargo_table.dart b/lib/pages/invoice/invoice_cargo_table.dart deleted file mode 100644 index 748ac2e..0000000 --- a/lib/pages/invoice/invoice_cargo_table.dart +++ /dev/null @@ -1,350 +0,0 @@ -import 'package:fcs/domain/entities/cargo_type.dart'; -import 'package:fcs/domain/entities/discount.dart'; -import 'package:fcs/domain/entities/invoice.dart'; -import 'package:fcs/domain/entities/rate.dart'; -import 'package:fcs/helpers/theme.dart'; -import 'package:fcs/pages/discount/discount_list.dart'; -import 'package:fcs/pages/main/util.dart'; -import 'package:fcs/pages/widgets/local_text.dart'; -import 'package:flutter/cupertino.dart'; -import 'package:flutter/material.dart'; -import 'package:intl/intl.dart'; - -typedef OnDiscountSelected(Discount discount); -typedef OnDeliveryFeeSelected(bool selected); -final formatter = new NumberFormat("#,###.00"); - -class InvoiceCargoTable extends StatelessWidget { - final Invoice invoice; - final Rate rate; - final OnDiscountSelected discountSelected; - final OnDeliveryFeeSelected deliveryFeeSelected; - - const InvoiceCargoTable( - {Key key, - this.invoice, - this.discountSelected, - this.deliveryFeeSelected, - this.rate}) - : super(key: key); - @override - Widget build(BuildContext context) { - return Column(children: getRows(context)); - } - - getRows(BuildContext context) { - List _cargoTypes = invoice.getCargoTypes(rate); - double total = 0; - List dataRow = _cargoTypes.map((cargo) { - var amount = cargo.calWeight * cargo.calRate; - total += amount; - return Container( - decoration: BoxDecoration( - border: Border(bottom: BorderSide(color: Colors.grey))), - padding: const EdgeInsets.only( - left: 5.0, right: 5.0, top: 15.0, bottom: 15.0), - child: Row( - children: [ - Expanded(flex: 2, child: Text('${cargo.name}')), - Expanded( - flex: 2, - child: Text( - '${cargo.calWeight.toStringAsFixed(2)} x ${cargo.calRate.toStringAsFixed(2)}', - textAlign: TextAlign.center)), - Expanded( - child: Text('\$ ${amount.toStringAsFixed(2)}', - textAlign: TextAlign.end, - style: TextStyle( - fontSize: 15, - fontWeight: FontWeight.bold, - ))) - ], - ), - ); - }).toList(); - dataRow.insert( - 0, - Container( - padding: const EdgeInsets.only( - left: 5.0, right: 5.0, top: 15.0, bottom: 15.0), - decoration: BoxDecoration( - border: Border(bottom: BorderSide(color: Colors.grey))), - child: Row( - children: [ - Expanded( - flex: 2, - child: Text(getLocalString(context, 'invoice.box.cargo_type'), - style: TextStyle( - fontSize: 12, - fontWeight: FontWeight.bold, - color: Colors.grey))), - Expanded( - flex: 2, - child: Text( - getLocalString(context, 'cargo.weight') + - ' x ' + - getLocalString(context, 'cargo.rate'), - textAlign: TextAlign.center, - style: TextStyle( - fontSize: 12, - fontWeight: FontWeight.bold, - color: Colors.grey))), - Expanded( - child: Text(getLocalString(context, 'invoice.amount'), - textAlign: TextAlign.end, - style: TextStyle( - fontSize: 12, - fontWeight: FontWeight.bold, - color: Colors.grey))) - ], - ), - )); - - dataRow.insert( - dataRow.length, - Container( - padding: const EdgeInsets.only( - left: 5.0, right: 5.0, top: 10.0, bottom: 10.0), - child: Row( - children: [ - Expanded( - flex: 1, - child: Container( - alignment: Alignment.centerRight, - child: LocalText( - context, - 'invoice.total', - color: Colors.black, - ), - ), - ), - SizedBox(width: 40), - Expanded( - child: Text( - '\$ ${total.toStringAsFixed(2)}', - style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold), - textAlign: TextAlign.end, - )) - ], - ), - )); - - dataRow.insert( - dataRow.length, - Container( - padding: const EdgeInsets.only(left: 5.0, right: 5.0, top: 0), - child: Row( - children: [ - Expanded( - flex: 1, - child: Container( - alignment: Alignment.centerRight, - child: LocalText( - context, - 'invoice.discount_value', - color: Colors.black, - ), - ), - ), - new IconButton( - icon: Icon(Icons.search, color: primaryColor), - onPressed: () async { - Discount discount = await Navigator.of(context).push( - CupertinoPageRoute( - builder: (context) => - DiscountList(selectionMode: true))); - if (discountSelected != null) { - discountSelected(discount); - } - }), - Expanded( - child: - Text('\$ ( ${invoice.getDiscount().toStringAsFixed(2)} )', - textAlign: TextAlign.end, - style: TextStyle( - fontSize: 15, - fontWeight: FontWeight.bold, - ))) - ], - ), - )); - - dataRow.insert( - dataRow.length, - Container( - padding: const EdgeInsets.only( - left: 5.0, right: 5.0, top: 10.0, bottom: 0.0), - child: Row( - children: [ - Expanded( - flex: 1, - child: Container( - alignment: Alignment.centerRight, - child: LocalText( - context, - 'invoice.custom_fee', - color: Colors.black, - ), - ), - ), - SizedBox(width: 40), - Expanded( - child: Text('\$ ${invoice.getCustomFee().toStringAsFixed(2)}', - textAlign: TextAlign.end, - style: TextStyle( - fontSize: 15, - fontWeight: FontWeight.bold, - )), - ), - ], - ), - )); - - dataRow.insert( - dataRow.length, - Container( - padding: const EdgeInsets.only(left: 5.0, right: 5.0, top: 20.0), - child: Row( - children: [ - Expanded( - flex: 1, - child: Container( - alignment: Alignment.centerRight, - child: LocalText( - context, - 'invoice.handling_fee', - color: Colors.black, - ), - ), - ), - SizedBox(width: 50), - Expanded( - child: Text( - '\$ ${invoice.getHandlingFee().toStringAsFixed(2) ?? ""}', - textAlign: TextAlign.end, - style: TextStyle( - fontSize: 15, - fontWeight: FontWeight.bold, - ))) - ], - ), - )); - - dataRow.insert( - dataRow.length, - Container( - padding: const EdgeInsets.only( - left: 5.0, right: 5.0, top: 10.0, bottom: 10.0), - child: Row( - children: [ - Expanded( - flex: 1, - child: Container( - alignment: Alignment.centerRight, - child: LocalText( - context, - 'invoice.delivery_fee', - color: Colors.black, - ), - )), - Switch( - value: (invoice.deliveryFee ?? 0) > 0, - onChanged: (value) { - if (deliveryFeeSelected != null) { - deliveryFeeSelected(value); - } - }, - activeTrackColor: primaryColor.withOpacity(0.8), - activeColor: primaryColor, - ), - Expanded( - child: - Text('\$ ${invoice.getDeliveryFee().toStringAsFixed(2)}', - textAlign: TextAlign.end, - style: TextStyle( - fontSize: 15, - fontWeight: FontWeight.bold, - ))) - ], - ), - )); - - dataRow.insert( - dataRow.length, - Container( - child: Row( - children: [ - Expanded(child: Text('')), - Expanded( - flex: 2, - child: Divider( - thickness: 3, - )), - ], - ))); - - dataRow.insert( - dataRow.length, - Container( - padding: const EdgeInsets.only( - left: 5.0, right: 5.0, top: 10.0, bottom: 10.0), - child: Row( - children: [ - Expanded( - flex: 2, - child: Center( - child: LocalText( - context, - 'invoice.net_amount', - color: Colors.black, - fontSize: 15, - fontWeight: FontWeight.bold, - ), - ), - ), - Expanded( - child: Text( - '\$ ${invoice.getNetAmount(rate).toStringAsFixed(2)}', - textAlign: TextAlign.end, - style: TextStyle( - fontSize: 18, - fontWeight: FontWeight.bold, - color: primaryColor))) - ], - ), - )); - - dataRow.insert( - dataRow.length, - Container( - padding: const EdgeInsets.only( - left: 5.0, right: 5.0, top: 10.0, bottom: 10.0), - child: Row( - children: [ - Expanded( - flex: 2, - child: Center( - child: LocalText( - context, - 'invoice.balance', - color: Colors.black, - fontSize: 15, - fontWeight: FontWeight.bold, - ), - ), - ), - Expanded( - child: Text( - '\$ ${invoice.getTotalBalance(rate).toStringAsFixed(2)}', - textAlign: TextAlign.end, - style: TextStyle( - fontSize: 18, - fontWeight: FontWeight.bold, - color: primaryColor))) - ], - ), - )); - - return dataRow; - } -} diff --git a/lib/pages/invoice/invoice_carton_table.dart b/lib/pages/invoice/invoice_carton_table.dart index 4e94119..77f05ec 100644 --- a/lib/pages/invoice/invoice_carton_table.dart +++ b/lib/pages/invoice/invoice_carton_table.dart @@ -1,5 +1,4 @@ import 'package:fcs/domain/entities/carton.dart'; -import 'package:fcs/domain/entities/package.dart'; import 'package:fcs/domain/entities/rate.dart'; import 'package:fcs/helpers/theme.dart'; import 'package:fcs/pages/widgets/local_text.dart'; @@ -20,7 +19,7 @@ class InvoiceCartonTable extends StatelessWidget { @override Widget build(BuildContext context) { final tableTitle = Container( - padding: EdgeInsets.only(right: 10.0, top: 20), + padding: EdgeInsets.only(right: 10.0, top: 5), child: Row( children: [ SizedBox( @@ -76,10 +75,6 @@ class InvoiceCartonTable extends StatelessWidget { p.value.cartonNumber, style: textStyle, ), - Text( - p.value.shipmentNumber ?? "", - style: textStyle, - ), ], )), Flexible( diff --git a/lib/pages/invoice/invoice_discount_table.dart b/lib/pages/invoice/invoice_discount_table.dart new file mode 100644 index 0000000..880eda3 --- /dev/null +++ b/lib/pages/invoice/invoice_discount_table.dart @@ -0,0 +1,88 @@ +import 'package:fcs/domain/entities/discount.dart'; +import 'package:fcs/helpers/theme.dart'; +import 'package:fcs/pages/widgets/local_text.dart'; +import 'package:fcs/pages/widgets/my_data_table.dart'; +import 'package:flutter/cupertino.dart'; +import 'package:flutter/material.dart'; + +class InvoiceDiscountTable extends StatelessWidget { + final List discounts; + + const InvoiceDiscountTable({ + Key key, + this.discounts, + }) : super(key: key); + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar( + centerTitle: true, + leading: new IconButton( + icon: new Icon(CupertinoIcons.back), + onPressed: () => Navigator.pop(context), + ), + backgroundColor: primaryColor, + title: LocalText( + context, + "invoice.shipment.discount.title", + fontSize: 20, + color: Colors.white, + ), + ), + body: Padding( + padding: const EdgeInsets.all(8.0), + child: table(context), + )); + } + + Widget table(BuildContext context) { + return MyDataTable( + headingRowHeight: 40, + columns: [ + MyDataColumn( + label: LocalText( + context, + "discount.code", + color: Colors.grey, + ), + ), + MyDataColumn( + label: LocalText( + context, + "discount.amount", + color: Colors.grey, + ), + ), + ], + rows: getRows(context), + ); + } + + List getRows(BuildContext context) { + if (discounts == null) { + return []; + } + var rows = discounts.map((c) { + return MyDataRow( + onSelectChanged: (value) => Navigator.pop(context, c), + cells: [ + MyDataCell(new Text( + c.code ?? "", + style: textStyle, + )), + MyDataCell( + Row( + mainAxisAlignment: MainAxisAlignment.end, + children: [ + Text(c.amount?.toStringAsFixed(2) ?? "0", style: textStyle), + ], + ), + ), + ], + ); + }).toList(); + + return rows; + } +} diff --git a/lib/pages/invoice/invoice_editor.dart b/lib/pages/invoice/invoice_editor.dart index 480bc15..41deb04 100644 --- a/lib/pages/invoice/invoice_editor.dart +++ b/lib/pages/invoice/invoice_editor.dart @@ -1,28 +1,33 @@ -import 'package:fcs/domain/entities/cargo_type.dart'; import 'package:fcs/domain/entities/carton.dart'; import 'package:fcs/domain/entities/custom_duty.dart'; import 'package:fcs/domain/entities/discount.dart'; import 'package:fcs/domain/entities/fcs_shipment.dart'; import 'package:fcs/domain/entities/invoice.dart'; import 'package:fcs/domain/entities/payment_method.dart'; +import 'package:fcs/domain/entities/shipment.dart'; import 'package:fcs/domain/entities/user.dart'; import 'package:fcs/helpers/theme.dart'; import 'package:fcs/pages/carton/model/carton_model.dart'; import 'package:fcs/pages/discount/discount_list.dart'; import 'package:fcs/pages/discount/model/discount_model.dart'; -import 'package:fcs/pages/invoice/invoice_cargo_table.dart'; +import 'package:fcs/pages/invoice/invoice_discount_table.dart'; +import 'package:fcs/pages/invoice/invoice_shipment_table.dart'; +import 'package:fcs/pages/invoice/invoice_table.dart'; import 'package:fcs/pages/invoice/invoice_carton_table.dart'; import 'package:fcs/pages/main/model/main_model.dart'; import 'package:fcs/pages/main/util.dart'; import 'package:fcs/pages/payment_methods/model/payment_method_model.dart'; import 'package:fcs/pages/rates/custom_list.dart'; import 'package:fcs/pages/rates/model/shipment_rate_model.dart'; +import 'package:fcs/pages/shipment/model/shipment_model.dart'; import 'package:fcs/pages/widgets/display_text.dart'; +import 'package:fcs/pages/widgets/fcs_icons.dart'; import 'package:fcs/pages/widgets/fcs_id_icon.dart'; import 'package:fcs/pages/widgets/local_dropdown.dart'; +import 'package:fcs/pages/widgets/local_popup_menu_button.dart'; +import 'package:fcs/pages/widgets/local_popupmenu.dart'; import 'package:fcs/pages/widgets/local_text.dart'; import 'package:fcs/pages/widgets/local_title.dart'; -import 'package:fcs/pages/widgets/multi_img_controller.dart'; import 'package:fcs/pages/widgets/progress.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; @@ -44,99 +49,84 @@ class InvoiceEditor extends StatefulWidget { } class _InvoiceEditorState extends State { - User user; + User _user; var dateFormatter = new DateFormat('dd MMM yyyy'); - TextEditingController _invoiceNumberController = new TextEditingController(); - TextEditingController _dateController = new TextEditingController(); - TextEditingController _nameController = new TextEditingController(); - TextEditingController _phoneController = new TextEditingController(); - TextEditingController _discountController = new TextEditingController(); - TextEditingController _amountController = new TextEditingController(); - TextEditingController _statusController = new TextEditingController(); - TextEditingController _handlingFeeController = new TextEditingController(); - TextEditingController _customFeeController = new TextEditingController(); - MultiImgController multiImgController = MultiImgController(); - TextEditingController _descriptionController = new TextEditingController(); - TextEditingController _balanceController = new TextEditingController(); Invoice _invoice; bool _isLoading = false; - List _cartons = []; - bool isSwitched = false; - int deliveryfee = 0; - double customFee = 10.0; - double handlingFee = 10.0; // it will get from shipment - double total = 0; - Discount _discount; - bool _isNew = false; - Discount selectedDiscount; - int selectedDiscountAmt; - PaymentMethod _paymentMethod; - double volumetricRatio = 0; - - List selectedBoxes = []; - List customs = []; - - // List _cargoTypes = [ - // CargoType(name: 'General Cargo', weight: 33, rate: 6), - // CargoType(name: 'Medicine', weight: 33, rate: 7), - // CargoType(name: 'Dangerous Cargo', weight: 33, rate: 8) - // ]; + bool _isNew; + List isSelected = [false]; + bool _showCartons = false; @override void initState() { super.initState(); - - volumetricRatio = Provider.of(context, listen: false) - .rate - .volumetricRatio; + _isNew = widget.invoice == null; if (widget.invoice != null) { - _isNew = false; _invoice = widget.invoice; - _invoiceNumberController.text = _invoice.invoiceNumber; - _dateController.text = dateFormatter.format(_invoice.invoiceDate); - _nameController.text = _invoice.customerName; - _phoneController.text = _invoice.customerPhoneNumber; - // _amountController.text = _invoice.getAmount.toString(); - _amountController.text = _invoice.amount.toString(); - _statusController.text = _invoice.status.toString(); - _handlingFeeController.text = '0'; - _customFeeController.text = '0'; - // multiImgController.setImageUrls = _receipts; - _descriptionController.text = 'For Electronics goods'; - _balanceController.text = - (_invoice.amount - _invoice.receipts[0].amount).toString(); - // _boxes = _invoice.packages; } else { - _isNew = true; - _dateController.text = dateFormatter.format(DateTime.now()); - _amountController.text = '0'; - _handlingFeeController.text = '0'; - _customFeeController.text = '0'; - _descriptionController.text = ''; - _balanceController.text = '0'; - _invoice = Invoice(customDuties: [], cartons: []); + _invoice = Invoice( + customDuties: [], + cartons: [], + shipments: [], + invoiceDate: DateTime.now()); } + _user = widget.customer; + _loadAll(); + } - if (widget.customer != null && widget.invoice == null) { - user = widget.customer; + _loadAll() async { + setState(() { + _isLoading = true; + }); + try { + await _loadCartons(); + await _loadShipments(); + await _loadDiscount(); + } catch (e) {} finally { setState(() { - _isNew = true; + _isLoading = false; }); } - - _loadCartons(); } _loadCartons() async { CartonModel cartonModel = Provider.of(context, listen: false); List cartons = await cartonModel.getCartonsForInvoice( widget.fcsShipment.id, widget.customer.id); - setState(() { - _cartons = cartons; + cartons.forEach((c) { + c.isChecked = true; }); + setState(() { + _invoice.cartons = cartons; + }); + } + + _loadShipments() async { + ShipmentModel shipmentModel = + Provider.of(context, listen: false); + List shipments = await shipmentModel.getShipmentWithHandlingFee( + widget.fcsShipment.id, widget.customer.id); + shipments.forEach((s) { + s.isSelected = true; + }); + setState(() { + _invoice.shipments = shipments; + }); + } + + List discounts = []; + _loadDiscount() async { + DiscountModel discountModel = + Provider.of(context, listen: false); + discounts = await discountModel.getDiscount(widget.customer.id); + if (discounts != null && discounts.length > 0) { + setState(() { + _invoice.discount = discounts.first; + }); + } } @override @@ -147,38 +137,22 @@ class _InvoiceEditorState extends State { @override Widget build(BuildContext context) { var mainModel = Provider.of(context); - var discountModel = Provider.of(context); var paymentMethodModel = Provider.of(context); var rateModel = Provider.of(context); var rate = rateModel.rate; - final nameBox = DisplayText( - iconData: Feather.user, - labelTextKey: 'invoice.customer_name', - text: user != null ? user.name : 'Ko Nyi'); - final statusBox = DisplayText( - text: _statusController.text, + text: _invoice?.status ?? "", iconData: Icons.av_timer, labelTextKey: 'invoice.status'); - final fcsIDBox = DisplayText( - text: user != null ? user.fcsID : "FCS-KRUTUG", - labelTextKey: "box.fcs.id", - icon: FcsIDIcon(), - ); final cartonTable = InvoiceCartonTable( - cartons: _cartons, + cartons: _invoice.cartons, rate: rate, onSelect: (c, checked) { setState(() { c.isChecked = checked; }); - if (checked) { - _invoice.cartons.add(c); - } else { - _invoice.cartons.remove(c); - } }, ); final customTableHeaderBox = LocalTitle( @@ -196,19 +170,19 @@ class _InvoiceEditorState extends State { onAdd: (c) => _addCustom(c), onRemove: (c) => _removeCustom(c), ); - var paymentTypesBox = LocalDropdown( + final paymentTypesBox = LocalDropdown( callback: (v) { setState(() { - _paymentMethod = v; + _invoice.paymentMethod = v; }); }, labelKey: "invoice.payment_method", iconData: FontAwesome.money, display: (u) => u.name, - selectedValue: _paymentMethod, + selectedValue: _invoice.paymentMethod, values: paymentMethodModel.paymentMethods, ); - final cargoTypeTableBox = InvoiceCargoTable( + final invoiceTableBox = InvoiceTable( invoice: _invoice, rate: rate, deliveryFeeSelected: (selected) { @@ -225,6 +199,97 @@ class _InvoiceEditorState extends State { _invoice.discount = discount; }); }, + onRemove: (i) { + if (i.invoiceDataType == InvoiceDataType.CustomFeeDataType) { + _removeCustom(i.data); + } + if (i.invoiceDataType == InvoiceDataType.DiscountDataType) { + setState(() { + _invoice.discount = null; + }); + } + if (i.invoiceDataType == InvoiceDataType.DeliveryFeeType) { + setState(() { + _invoice.deliveryFee = 0; + }); + } + if (i.invoiceDataType == InvoiceDataType.HandlingFeeType) { + setState(() { + _removeShipment(i.data); + }); + } + }, + ); + final toggleButtonsBox = ToggleButtons( + color: Colors.black45, + selectedColor: Colors.black45, + disabledColor: Colors.grey, + selectedBorderColor: primaryColor, + borderColor: Colors.transparent, + fillColor: Colors.transparent, + highlightColor: Colors.black45, + children: [ + Icon(cartonIconData), + ], + onPressed: (int index) { + setState(() { + _showCartons = !_showCartons; + }); + }, + isSelected: [_showCartons], + ); + + final popupMenu = LocalPopupMenuButton( + buttonIcon: Icons.add_circle, + selectable: false, + buttonColor: Colors.black45, + popmenus: [ + LocalPopupMenu( + id: 1, + textKey: "invoice.add.custom.fee.menu", + ), + LocalPopupMenu( + id: 2, + textKey: "invoice.add.handling.fee.menu", + ), + LocalPopupMenu( + id: 3, + textKey: "invoice.add.discount.menu", + ), + LocalPopupMenu( + id: 4, + textKey: "invoice.delivery_fee", + ) + ], + popupMenuCallback: (p) async { + if (p.id == 1) { + CustomDuty customDuty = await Navigator.of(context).push( + CupertinoPageRoute( + builder: (context) => CustomList(selected: true))); + _addCustom(customDuty); + } else if (p.id == 2) { + Shipment shipment = await Navigator.of(context).push( + CupertinoPageRoute( + builder: (context) => + InvoiceShipmentTable(shipments: _invoice.shipments))); + _addShipment(shipment); + } else if (p.id == 3) { + Discount discount = + await Navigator.of(context).push(CupertinoPageRoute( + builder: (context) => InvoiceDiscountTable( + discounts: discounts, + ))); + if (discount != null) { + setState(() { + _invoice.discount = discount; + }); + } + } else if (p.id == 4) { + setState(() { + _invoice.deliveryFee = rate.deliveryFee; + }); + } + }, ); return LocalProgress( @@ -245,31 +310,55 @@ class _InvoiceEditorState extends State { padding: const EdgeInsets.all(8.0), child: ListView( children: [ - LocalTitle(textKey: "invoice.customer_info"), - DisplayText( - labelTextKey: 'invoice.date', - iconData: Icons.date_range, - text: _dateController.text), - widget.invoice == null + Row( + crossAxisAlignment: CrossAxisAlignment.end, + children: [ + Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text(dateFormatter.format(_invoice.invoiceDate)), + SizedBox( + height: 10, + ), + Text(_user?.name ?? ""), + Text( + _user?.fcsID ?? "", + style: TextStyle(fontSize: 12), + ) + ], + ), + Spacer(), + Row( + mainAxisAlignment: MainAxisAlignment.end, + children: [ + toggleButtonsBox, + popupMenu, + ], + ), + ], + ), + _isNew ? Container() : DisplayText( labelTextKey: 'invoice.number', iconData: FontAwesomeIcons.fileInvoice, - text: _invoiceNumberController.text), - fcsIDBox, - nameBox, + text: _invoice?.invoiceNumber ?? ""), _isNew ? Container() : statusBox, - SizedBox(height: 20), - customTableHeaderBox, - customTableBox, - SizedBox(height: 20), - cartonTable, - LocalTitle(textKey: "invoice.cargo_type"), - cargoTypeTableBox, - // Column(children: getCargoTableByBox(context)), - SizedBox(height: 20), + _showCartons ? cartonTable : Container(), + _showCartons + ? Divider( + color: primaryColor, + thickness: 2, + ) + : Container(), + invoiceTableBox, + SizedBox( + height: 10, + ), paymentTypesBox, - SizedBox(height: 20), + SizedBox( + height: 10, + ), _isNew ? Container() : LocalTitle( @@ -277,7 +366,7 @@ class _InvoiceEditorState extends State { trailing: IconButton( icon: Icon(Icons.add_circle, color: primaryColor), onPressed: () async {})), - widget.invoice == null + _isNew ? fcsButton( context, getLocalString(context, 'invoice.btn_create')) : mainModel.isCustomer() @@ -300,15 +389,6 @@ class _InvoiceEditorState extends State { ); } - getTotalBalance(total) { - double balance = 0; - double custom = customFee != 0 ? customFee.toDouble() : 0; - double discount = _discount != null ? _discount.amount.toDouble() : 0; - double deliveryFee = deliveryfee != 0 ? deliveryfee.toDouble() : 0; - balance = (total + custom + deliveryFee) - discount; - return balance; - } - _addCustom(CustomDuty customDuty) { if (customDuty == null) return; setState(() { @@ -317,6 +397,24 @@ class _InvoiceEditorState extends State { }); } + _addShipment(Shipment shipment) { + if (shipment == null) return; + shipment.isSelected = true; + setState(() { + _invoice.shipments.remove(shipment); + _invoice.shipments.add(shipment); + }); + } + + _removeShipment(Shipment shipment) { + if (shipment == null) return; + shipment.isSelected = false; + setState(() { + _invoice.shipments.remove(shipment); + _invoice.shipments.add(shipment); + }); + } + _removeCustom(CustomDuty customDuty) { setState(() { _invoice.customDuties.remove(customDuty); diff --git a/lib/pages/invoice/invoice_info.dart b/lib/pages/invoice/invoice_info.dart index 7a6a9a0..b86d82d 100644 --- a/lib/pages/invoice/invoice_info.dart +++ b/lib/pages/invoice/invoice_info.dart @@ -99,16 +99,14 @@ class _InvoiceInfoPageState extends State { _invoice = widget.invoice; _invoiceNumberController.text = _invoice.invoiceNumber; _dateController.text = dateFormatter.format(_invoice.invoiceDate); - _nameController.text = _invoice.customerName; - _phoneController.text = _invoice.customerPhoneNumber; + _nameController.text = _invoice.userName; + _phoneController.text = _invoice.phoneNumber; // _amountController.text = _invoice.getAmount.toString(); _amountController.text = _invoice.amount.toString(); _statusController.text = _invoice.status.toString(); _customFeeController.text = '0'; // multiImgController.setImageUrls = _receipts; _descriptionController.text = 'For Electronics goods'; - _balanceController.text = - (_invoice.amount - _invoice.receipts[0].amount).toString(); // _boxes = _invoice.packages; } else { _dateController.text = dateFormatter.format(DateTime.now()); diff --git a/lib/pages/invoice/invoice_shipment_table.dart b/lib/pages/invoice/invoice_shipment_table.dart new file mode 100644 index 0000000..abc0318 --- /dev/null +++ b/lib/pages/invoice/invoice_shipment_table.dart @@ -0,0 +1,105 @@ +import 'package:fcs/domain/entities/shipment.dart'; +import 'package:fcs/helpers/theme.dart'; +import 'package:fcs/pages/widgets/local_text.dart'; +import 'package:fcs/pages/widgets/my_data_table.dart'; +import 'package:flutter/cupertino.dart'; +import 'package:flutter/material.dart'; + +typedef OnAdd(Shipment shipment); +typedef OnRemove(Shipment shipment); + +class InvoiceShipmentTable extends StatelessWidget { + final List shipments; + final OnAdd onAdd; + final OnRemove onRemove; + + const InvoiceShipmentTable( + {Key key, this.shipments, this.onAdd, this.onRemove}) + : super(key: key); + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar( + centerTitle: true, + leading: new IconButton( + icon: new Icon(CupertinoIcons.back), + onPressed: () => Navigator.pop(context), + ), + backgroundColor: primaryColor, + title: LocalText( + context, + "invoice.shipment.handling.fee.title", + fontSize: 20, + color: Colors.white, + ), + ), + body: Padding( + padding: const EdgeInsets.all(8.0), + child: table(context), + )); + } + + Widget table(BuildContext context) { + return MyDataTable( + headingRowHeight: 40, + columns: [ + MyDataColumn( + label: LocalText( + context, + "invoice.shipment.number", + color: Colors.grey, + ), + ), + MyDataColumn( + label: LocalText( + context, + "invoice.add.handling.fee.menu", + color: Colors.grey, + ), + ), + ], + rows: getRows(context), + ); + } + + List getRows(BuildContext context) { + if (shipments == null) { + return []; + } + var rows = shipments.map((c) { + return MyDataRow( + onSelectChanged: (value) => Navigator.pop(context, c), + cells: [ + MyDataCell(new Text( + c.shipmentNumber ?? "", + style: textStyle, + )), + MyDataCell( + Row( + mainAxisAlignment: MainAxisAlignment.end, + children: [ + Text(c.handlingFee?.toStringAsFixed(2) ?? "0", + style: textStyle), + onRemove == null + ? SizedBox( + width: 50, + ) + : IconButton( + icon: Icon( + Icons.remove_circle, + color: primaryColor, + ), + onPressed: () { + if (onRemove != null) onRemove(c); + }) + ], + ), + ), + ], + ); + }).toList(); + + return rows; + } +} diff --git a/lib/pages/invoice/invoice_table.dart b/lib/pages/invoice/invoice_table.dart new file mode 100644 index 0000000..1df1cfd --- /dev/null +++ b/lib/pages/invoice/invoice_table.dart @@ -0,0 +1,377 @@ +import 'package:fcs/domain/entities/cargo_type.dart'; +import 'package:fcs/domain/entities/discount.dart'; +import 'package:fcs/domain/entities/invoice.dart'; +import 'package:fcs/domain/entities/rate.dart'; +import 'package:fcs/helpers/theme.dart'; +import 'package:fcs/pages/discount/discount_list.dart'; +import 'package:fcs/pages/main/util.dart'; +import 'package:fcs/pages/widgets/local_text.dart'; +import 'package:flutter/cupertino.dart'; +import 'package:flutter/material.dart'; +import 'package:intl/intl.dart'; + +typedef OnDiscountSelected(Discount discount); +typedef OnDeliveryFeeSelected(bool selected); +typedef OnRemove(InvoiceTableRow row); +final formatter = new NumberFormat("#,###.00"); + +enum InvoiceDataType { + CargoDataType, + DiscountDataType, + CustomFeeDataType, + HandlingFeeType, + DeliveryFeeType, +} + +class InvoiceTableRow { + final dynamic data; + final String id; + final InvoiceDataType invoiceDataType; + final String desc; + final String rate; + final String amount; + + InvoiceTableRow( + {this.id, + this.data, + this.invoiceDataType, + this.desc, + this.rate, + this.amount}); +} + +class InvoiceTable extends StatelessWidget { + final Invoice invoice; + final Rate rate; + final OnDiscountSelected discountSelected; + final OnDeliveryFeeSelected deliveryFeeSelected; + final OnRemove onRemove; + + const InvoiceTable( + {Key key, + this.invoice, + this.discountSelected, + this.deliveryFeeSelected, + this.onRemove, + this.rate}) + : super(key: key); + @override + Widget build(BuildContext context) { + return Column(children: getRows(context)); + } + + List getTableRows() { + List tableRows = []; + // add cargo types + List _cargoTypes = invoice.getCargoTypes(rate); + _cargoTypes.forEach((c) { + tableRows.add(InvoiceTableRow( + invoiceDataType: InvoiceDataType.CargoDataType, + desc: c.name, + rate: + "${c.calWeight.toStringAsFixed(2)} x ${c.calRate.toStringAsFixed(2)}", + amount: "${c.amount.toStringAsFixed(2)}")); + }); + invoice.shipments.where((ss) => (ss.isSelected ?? false)).forEach((s) { + tableRows.add(InvoiceTableRow( + data: s, + invoiceDataType: InvoiceDataType.HandlingFeeType, + desc: "Handling fee\n${s.shipmentNumber}", + rate: "", + amount: "${s.handlingFee.toStringAsFixed(2)}")); + }); + // add custom fee + invoice.customDuties.forEach((c) { + tableRows.add(InvoiceTableRow( + data: c, + invoiceDataType: InvoiceDataType.CustomFeeDataType, + desc: "${c.productType} custom fee", + rate: "", + amount: "${c.fee.toStringAsFixed(2)}")); + }); + // add delivery fee + tableRows.add(InvoiceTableRow( + data: invoice.deliveryFee == null || invoice.deliveryFee == 0 + ? null + : invoice.deliveryFee, + invoiceDataType: InvoiceDataType.DeliveryFeeType, + desc: "Delivery fee", + rate: "", + amount: "${invoice?.deliveryFee?.toStringAsFixed(2) ?? '0'}")); + + // add discounts + if (invoice.discount != null) { + tableRows.add(InvoiceTableRow( + data: invoice.discount, + invoiceDataType: InvoiceDataType.DiscountDataType, + desc: "Discount\n${invoice?.discount?.code ?? ""}", + rate: "", + amount: "(${invoice?.discount?.amount?.toStringAsFixed(2) ?? ''})")); + } + + return tableRows; + } + + getRows(BuildContext context) { + List tableRows = getTableRows(); + + List dataRow = tableRows.map((r) { + return Container( + decoration: BoxDecoration( + border: Border(bottom: BorderSide(color: Colors.grey))), + padding: const EdgeInsets.only( + left: 5.0, right: 5.0, top: 10.0, bottom: 10.0), + child: Row( + children: [ + Expanded( + flex: 2, + child: Row( + children: [ + Flexible(child: Text('${r.desc}')), + SizedBox( + width: 5, + ), + r.data == null || onRemove == null + ? Container() + : InkWell( + onTap: () => onRemove(r), + child: Icon( + Icons.remove_circle, + color: Colors.black45, + ), + ) + ], + )), + Expanded( + flex: 1, + child: Text( + '${r.rate}', + textAlign: TextAlign.end, + style: TextStyle(fontSize: 12), + )), + Expanded( + flex: 1, + child: Text('\$ ${r.amount}', + textAlign: TextAlign.end, + style: TextStyle( + fontSize: 14, + fontWeight: FontWeight.bold, + ))) + ], + ), + ); + }).toList(); + dataRow.insert( + 0, + Container( + padding: const EdgeInsets.only( + left: 5.0, right: 5.0, top: 15.0, bottom: 15.0), + decoration: BoxDecoration( + border: Border(bottom: BorderSide(color: Colors.grey))), + child: Row( + children: [ + Expanded( + flex: 2, + child: Text(getLocalString(context, 'invoice.box.desc'), + style: TextStyle( + fontSize: 12, + fontWeight: FontWeight.bold, + color: Colors.grey))), + Expanded( + flex: 2, + child: Text( + getLocalString(context, 'invoice.weight') + + ' x ' + + getLocalString(context, 'invoice.rate'), + textAlign: TextAlign.center, + style: TextStyle( + fontSize: 12, + fontWeight: FontWeight.bold, + color: Colors.grey))), + Expanded( + child: Text(getLocalString(context, 'invoice.amount'), + textAlign: TextAlign.end, + style: TextStyle( + fontSize: 12, + fontWeight: FontWeight.bold, + color: Colors.grey))) + ], + ), + )); + + dataRow.insert( + dataRow.length, + Container( + padding: const EdgeInsets.only( + left: 5.0, right: 5.0, top: 10.0, bottom: 10.0), + child: Row( + children: [ + Expanded( + flex: 1, + child: Container( + alignment: Alignment.centerRight, + child: LocalText( + context, + 'invoice.total', + color: Colors.black, + ), + ), + ), + SizedBox(width: 20), + Text( + '\$ ${invoice.getNetAmount(rate).toStringAsFixed(2)}', + style: TextStyle(fontSize: 14, fontWeight: FontWeight.bold), + textAlign: TextAlign.end, + ) + ], + ), + )); + + // dataRow.insert( + // dataRow.length, + // Container( + // padding: const EdgeInsets.only(left: 5.0, right: 5.0, top: 20.0), + // child: Row( + // children: [ + // Expanded( + // flex: 1, + // child: Container( + // alignment: Alignment.centerRight, + // child: LocalText( + // context, + // 'invoice.handling_fee', + // color: Colors.black, + // ), + // ), + // ), + // SizedBox(width: 50), + // Expanded( + // child: Text( + // '\$ ${invoice.getHandlingFee().toStringAsFixed(2) ?? ""}', + // textAlign: TextAlign.end, + // style: TextStyle( + // fontSize: 15, + // fontWeight: FontWeight.bold, + // ))) + // ], + // ), + // )); + + // dataRow.insert( + // dataRow.length, + // Container( + // padding: const EdgeInsets.only( + // left: 5.0, right: 5.0, top: 10.0, bottom: 10.0), + // child: Row( + // children: [ + // Expanded( + // flex: 1, + // child: Container( + // alignment: Alignment.centerRight, + // child: LocalText( + // context, + // 'invoice.delivery_fee', + // color: Colors.black, + // ), + // )), + // Switch( + // value: (invoice.deliveryFee ?? 0) > 0, + // onChanged: (value) { + // if (deliveryFeeSelected != null) { + // deliveryFeeSelected(value); + // } + // }, + // activeTrackColor: primaryColor.withOpacity(0.8), + // activeColor: primaryColor, + // ), + // Expanded( + // child: + // Text('\$ ${invoice.getDeliveryFee().toStringAsFixed(2)}', + // textAlign: TextAlign.end, + // style: TextStyle( + // fontSize: 15, + // fontWeight: FontWeight.bold, + // ))) + // ], + // ), + // )); + + // dataRow.insert( + // dataRow.length, + // Container( + // child: Row( + // children: [ + // Expanded(child: Text('')), + // Expanded( + // flex: 2, + // child: Divider( + // thickness: 3, + // )), + // ], + // ))); + + // dataRow.insert( + // dataRow.length, + // Container( + // padding: const EdgeInsets.only( + // left: 5.0, right: 5.0, top: 10.0, bottom: 10.0), + // child: Row( + // children: [ + // Expanded( + // flex: 2, + // child: Center( + // child: LocalText( + // context, + // 'invoice.net_amount', + // color: Colors.black, + // fontSize: 15, + // fontWeight: FontWeight.bold, + // ), + // ), + // ), + // Expanded( + // child: Text( + // '\$ ${invoice.getNetAmount(rate).toStringAsFixed(2)}', + // textAlign: TextAlign.end, + // style: TextStyle( + // fontSize: 18, + // fontWeight: FontWeight.bold, + // color: primaryColor))) + // ], + // ), + // )); + + // dataRow.insert( + // dataRow.length, + // Container( + // padding: const EdgeInsets.only( + // left: 5.0, right: 5.0, top: 10.0, bottom: 10.0), + // child: Row( + // children: [ + // Expanded( + // flex: 2, + // child: Center( + // child: LocalText( + // context, + // 'invoice.balance', + // color: Colors.black, + // fontSize: 15, + // fontWeight: FontWeight.bold, + // ), + // ), + // ), + // Expanded( + // child: Text( + // '\$ ${invoice.getTotalBalance(rate).toStringAsFixed(2)}', + // textAlign: TextAlign.end, + // style: TextStyle( + // fontSize: 18, + // fontWeight: FontWeight.bold, + // color: primaryColor))) + // ], + // ), + // )); + + return dataRow; + } +} diff --git a/lib/pages/invoice/model/invoice_model.dart b/lib/pages/invoice/model/invoice_model.dart index fa74b18..a25678b 100644 --- a/lib/pages/invoice/model/invoice_model.dart +++ b/lib/pages/invoice/model/invoice_model.dart @@ -1,15 +1,12 @@ import 'dart:async'; import 'package:cloud_firestore/cloud_firestore.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/package.dart'; import 'package:fcs/domain/entities/receipt.dart'; -import 'package:fcs/domain/vo/message.dart'; import 'package:fcs/helpers/paginator.dart'; import 'package:fcs/pages/main/model/base_model.dart'; import 'package:logging/logging.dart'; -import 'package:fcs/domain/constants.dart'; class InvoiceModel extends BaseModel { final log = Logger('InvoiceModel'); @@ -18,37 +15,13 @@ class InvoiceModel extends BaseModel { List _invoices = [ Invoice( - invoiceNumber: 'A092(A)-33', - invoiceDate: DateTime(2020, 4, 6, 12, 15), - customerName: 'Ko Myo Min', - customerPhoneNumber: '+959 555555555', - amount: 300, - status: 'Pending', - receipts: [ - Receipt(amount: 200, date: '1 Jun 2020'), - ], - packages: [ - Package( - shipmentNumber: "A201", - receiverNumber: "1", - boxNumber: "1", - rate: 9, - packageType: "Dangerous", - weight: 25, - status: "Delivered", - arrivedDate: DateTime(2020, 5, 21), - receiverAddress: '3 Kambzwza St, Bahan Tsp, Yangon'), - Package( - shipmentNumber: "A201", - receiverNumber: "1", - boxNumber: "2", - rate: 7, - packageType: "General", - weight: 5, - status: "Delivered", - arrivedDate: DateTime(2020, 5, 21), - receiverAddress: '3 Kambzwza St, Bahan Tsp, Yangon'), - ]) + invoiceNumber: 'A092(A)-33', + invoiceDate: DateTime(2020, 4, 6, 12, 15), + userName: 'Ko Myo Min', + phoneNumber: '+959 555555555', + amount: 300, + status: 'Pending', + ) ]; List get invoices => diff --git a/lib/pages/invoice/payment_page.dart b/lib/pages/invoice/payment_page.dart index d05bd37..52ff087 100644 --- a/lib/pages/invoice/payment_page.dart +++ b/lib/pages/invoice/payment_page.dart @@ -121,7 +121,7 @@ class _PaymentPageState extends State { getCustomFeeRows(BuildContext context) { List dataRow = []; - dataRow = _invoice.receipts.asMap().entries.map((receipt) { + dataRow = [].asMap().entries.map((receipt) { var r = receipt.value; var k = receipt.key + 1; return Container( diff --git a/lib/pages/shipment/model/shipment_model.dart b/lib/pages/shipment/model/shipment_model.dart index 8e8cbe5..6c96da4 100644 --- a/lib/pages/shipment/model/shipment_model.dart +++ b/lib/pages/shipment/model/shipment_model.dart @@ -148,6 +148,30 @@ class ShipmentModel extends BaseModel { return null; } + Future> getShipmentWithHandlingFee( + String fcsShipmentID, String userID) async { + String path = "/$shipments_collection"; + try { + var q = Firestore.instance + .collection("$path") + .where("user_id", isEqualTo: userID) + .where("is_deleted", isEqualTo: false) + .where("handling_fee", isGreaterThan: 0) + .where("fcs_shipment_id", isEqualTo: fcsShipmentID); + var snaps = await q.getDocuments(source: Source.server); + List shipments = snaps.documents.map((snap) { + if (snap.exists) { + var s = Shipment.fromMap(snap.data, snap.documentID); + return s; + } + }).toList(); + return shipments; + } catch (e) { + log.warning("Error!! $e"); + } + return null; + } + void initUser(user) { super.initUser(user); } diff --git a/lib/pages/widgets/fcs_icons.dart b/lib/pages/widgets/fcs_icons.dart new file mode 100644 index 0000000..19c844e --- /dev/null +++ b/lib/pages/widgets/fcs_icons.dart @@ -0,0 +1,5 @@ +import 'package:flutter_icons/flutter_icons.dart'; + +const cartonIconData = MaterialCommunityIcons.package; +const customFeeIconData = MaterialCommunityIcons.security; +const shipmentIconData = SimpleLineIcons.direction; diff --git a/lib/pages/widgets/local_popup_menu_button.dart b/lib/pages/widgets/local_popup_menu_button.dart index 2a29681..53701e7 100644 --- a/lib/pages/widgets/local_popup_menu_button.dart +++ b/lib/pages/widgets/local_popup_menu_button.dart @@ -12,6 +12,7 @@ class LocalPopupMenuButton extends StatefulWidget { final bool multiSelect; final bool selectable; final IconData buttonIcon; + final Color buttonColor; const LocalPopupMenuButton( {Key key, @@ -19,7 +20,8 @@ class LocalPopupMenuButton extends StatefulWidget { this.popmenus, this.buttonIcon, this.selectable = true, - this.multiSelect = false}) + this.multiSelect = false, + this.buttonColor = primaryColor}) : super(key: key); @override @@ -76,7 +78,7 @@ class _LocalPopupMenuButtonState extends State { children: [ Icon( widget.buttonIcon ?? Icons.filter_list, - color: primaryColor, + color: widget.buttonColor ?? primaryColor, ), hightlight ? Positioned(