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/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_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/widgets/display_text.dart'; import 'package:fcs/pages/widgets/fcs_id_icon.dart'; import 'package:fcs/pages/widgets/local_dropdown.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'; import 'package:flutter_icons/flutter_icons.dart'; import 'package:font_awesome_flutter/font_awesome_flutter.dart'; import 'package:intl/intl.dart'; import 'package:provider/provider.dart'; import 'invoice_custom_table.dart'; class InvoiceEditor extends StatefulWidget { final Invoice invoice; final User customer; final FcsShipment fcsShipment; InvoiceEditor({this.invoice, this.customer, this.fcsShipment}); @override _InvoiceEditorState createState() => _InvoiceEditorState(); } class _InvoiceEditorState extends State { 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) // ]; @override void initState() { super.initState(); volumetricRatio = Provider.of(context, listen: false) .rate .volumetricRatio; 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: []); } if (widget.customer != null && widget.invoice == null) { user = widget.customer; setState(() { _isNew = true; }); } _loadCartons(); } _loadCartons() async { CartonModel cartonModel = Provider.of(context, listen: false); List cartons = await cartonModel.getCartonsForInvoice( widget.fcsShipment.id, widget.customer.id); setState(() { _cartons = cartons; }); } @override void dispose() { super.dispose(); } @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, 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, rate: rate, onSelect: (c, checked) { setState(() { c.isChecked = checked; }); if (checked) { _invoice.cartons.add(c); } else { _invoice.cartons.remove(c); } }, ); final customTableHeaderBox = LocalTitle( textKey: "invoice.custom_fee", trailing: IconButton( icon: Icon(Icons.add_circle, color: primaryColor), onPressed: () async { CustomDuty customDuty = await Navigator.of(context).push( CupertinoPageRoute( builder: (context) => CustomList(selected: true))); _addCustom(customDuty); })); final customTableBox = InvoiceCustomTable( customDuties: _invoice.customDuties, onAdd: (c) => _addCustom(c), onRemove: (c) => _removeCustom(c), ); var paymentTypesBox = LocalDropdown( callback: (v) { setState(() { _paymentMethod = v; }); }, labelKey: "invoice.payment_method", iconData: FontAwesome.money, display: (u) => u.name, selectedValue: _paymentMethod, values: paymentMethodModel.paymentMethods, ); final cargoTypeTableBox = InvoiceCargoTable( invoice: _invoice, rate: rate, deliveryFeeSelected: (selected) { setState(() { if (selected) { _invoice.deliveryFee = rate.deliveryFee; } else { _invoice.deliveryFee = 0; } }); }, discountSelected: (discount) { setState(() { _invoice.discount = discount; }); }, ); return LocalProgress( inAsyncCall: _isLoading, child: Scaffold( appBar: AppBar( centerTitle: true, leading: new IconButton( icon: new Icon(CupertinoIcons.back, color: primaryColor), onPressed: () => Navigator.of(context).pop(), ), backgroundColor: Colors.white, shadowColor: Colors.transparent, title: LocalText(context, 'invoice.form.title', color: primaryColor, fontSize: 20), ), body: Padding( 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 ? Container() : DisplayText( labelTextKey: 'invoice.number', iconData: FontAwesomeIcons.fileInvoice, text: _invoiceNumberController.text), fcsIDBox, nameBox, _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), paymentTypesBox, SizedBox(height: 20), _isNew ? Container() : LocalTitle( textKey: "invoice.payment_attachment", trailing: IconButton( icon: Icon(Icons.add_circle, color: primaryColor), onPressed: () async {})), widget.invoice == null ? fcsButton( context, getLocalString(context, 'invoice.btn_create')) : mainModel.isCustomer() ? Container() : Container( child: Column( children: [ fcsButton(context, getLocalString(context, 'invoice.btn_save')) ], )), _isNew ? Container() : fcsButton(context, getLocalString(context, 'invoice.btn_payment_receipt')) ], ), ), ), ); } 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(() { _invoice.customDuties.remove(customDuty); _invoice.customDuties.add(customDuty); }); } _removeCustom(CustomDuty customDuty) { setState(() { _invoice.customDuties.remove(customDuty); }); } _save() {} }