Files
fcs/lib/pages/invoice/invoice_editor.dart

328 lines
11 KiB
Dart
Raw Normal View History

2020-10-15 03:06:13 +06:30
import 'package:fcs/domain/entities/cargo_type.dart';
2020-10-22 04:14:53 +06:30
import 'package:fcs/domain/entities/carton.dart';
2020-10-15 03:06:13 +06:30
import 'package:fcs/domain/entities/custom_duty.dart';
2020-10-09 19:00:39 +06:30
import 'package:fcs/domain/entities/discount.dart';
2020-10-22 04:14:53 +06:30
import 'package:fcs/domain/entities/fcs_shipment.dart';
2020-10-07 02:33:06 +06:30
import 'package:fcs/domain/entities/invoice.dart';
import 'package:fcs/domain/entities/payment_method.dart';
2020-10-09 19:00:39 +06:30
import 'package:fcs/domain/entities/user.dart';
2020-10-07 02:33:06 +06:30
import 'package:fcs/helpers/theme.dart';
2020-10-22 04:14:53 +06:30
import 'package:fcs/pages/carton/model/carton_model.dart';
2020-10-14 20:56:46 +06:30
import 'package:fcs/pages/discount/discount_list.dart';
2020-10-07 02:33:06 +06:30
import 'package:fcs/pages/discount/model/discount_model.dart';
2020-10-22 04:14:53 +06:30
import 'package:fcs/pages/invoice/invoice_cargo_table.dart';
import 'package:fcs/pages/invoice/invoice_carton_table.dart';
2020-10-07 02:33:06 +06:30
import 'package:fcs/pages/main/model/main_model.dart';
2020-10-09 19:00:39 +06:30
import 'package:fcs/pages/main/util.dart';
2020-10-13 18:38:00 +06:30
import 'package:fcs/pages/payment_methods/model/payment_method_model.dart';
2020-10-14 20:56:46 +06:30
import 'package:fcs/pages/rates/custom_list.dart';
2020-10-15 03:06:13 +06:30
import 'package:fcs/pages/rates/model/shipment_rate_model.dart';
2020-10-09 19:00:39 +06:30
import 'package:fcs/pages/widgets/display_text.dart';
2020-10-14 20:56:46 +06:30
import 'package:fcs/pages/widgets/fcs_id_icon.dart';
2020-10-13 18:38:00 +06:30
import 'package:fcs/pages/widgets/local_dropdown.dart';
2020-10-07 02:33:06 +06:30
import 'package:fcs/pages/widgets/local_text.dart';
2020-10-14 10:33:14 +06:30
import 'package:fcs/pages/widgets/local_title.dart';
2020-10-07 02:33:06 +06:30
import 'package:fcs/pages/widgets/multi_img_controller.dart';
import 'package:fcs/pages/widgets/progress.dart';
2020-10-14 13:54:42 +06:30
import 'package:flutter/cupertino.dart';
2020-06-02 14:56:51 +06:30
import 'package:flutter/material.dart';
2020-06-03 00:42:31 +06:30
import 'package:flutter_icons/flutter_icons.dart';
2020-06-02 14:56:51 +06:30
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:intl/intl.dart';
import 'package:provider/provider.dart';
2020-10-22 04:14:53 +06:30
import 'invoice_custom_table.dart';
2020-06-02 14:56:51 +06:30
class InvoiceEditor extends StatefulWidget {
final Invoice invoice;
2020-10-14 20:56:46 +06:30
final User customer;
2020-10-22 04:14:53 +06:30
final FcsShipment fcsShipment;
InvoiceEditor({this.invoice, this.customer, this.fcsShipment});
2020-06-02 14:56:51 +06:30
@override
_InvoiceEditorState createState() => _InvoiceEditorState();
}
class _InvoiceEditorState extends State<InvoiceEditor> {
2020-10-09 19:00:39 +06:30
User user;
2020-06-02 14:56:51 +06:30
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();
2020-06-29 16:03:41 +06:30
TextEditingController _handlingFeeController = new TextEditingController();
TextEditingController _customFeeController = new TextEditingController();
2020-06-02 14:56:51 +06:30
MultiImgController multiImgController = MultiImgController();
2020-06-29 16:03:41 +06:30
TextEditingController _descriptionController = new TextEditingController();
TextEditingController _balanceController = new TextEditingController();
2020-06-02 14:56:51 +06:30
Invoice _invoice;
bool _isLoading = false;
2020-10-22 04:14:53 +06:30
List<Carton> _cartons = [];
2020-06-26 16:04:40 +06:30
bool isSwitched = false;
2020-10-09 19:00:39 +06:30
int deliveryfee = 0;
2020-10-16 21:38:39 +06:30
double customFee = 10.0;
double handlingFee = 10.0; // it will get from shipment
2020-10-09 19:00:39 +06:30
double total = 0;
Discount _discount;
2020-10-22 04:14:53 +06:30
bool _isNew = false;
2020-10-13 18:38:00 +06:30
Discount selectedDiscount;
int selectedDiscountAmt;
2020-10-22 04:14:53 +06:30
PaymentMethod _paymentMethod;
2020-10-14 20:56:46 +06:30
double volumetricRatio = 0;
2020-10-09 19:00:39 +06:30
2020-10-18 02:38:46 +06:30
List<Carton> selectedBoxes = [];
2020-10-15 03:06:13 +06:30
List<CustomDuty> customs = [];
2020-10-09 19:00:39 +06:30
2020-10-22 04:14:53 +06:30
// List<CargoType> _cargoTypes = [
// CargoType(name: 'General Cargo', weight: 33, rate: 6),
// CargoType(name: 'Medicine', weight: 33, rate: 7),
// CargoType(name: 'Dangerous Cargo', weight: 33, rate: 8)
// ];
2020-06-02 14:56:51 +06:30
@override
void initState() {
super.initState();
2020-10-14 20:56:46 +06:30
2020-10-15 03:06:13 +06:30
volumetricRatio = Provider.of<ShipmentRateModel>(context, listen: false)
.rate
.volumetricRatio;
2020-10-14 20:56:46 +06:30
2020-06-02 14:56:51 +06:30
if (widget.invoice != null) {
2020-10-22 04:14:53 +06:30
_isNew = false;
2020-06-02 14:56:51 +06:30
_invoice = widget.invoice;
_invoiceNumberController.text = _invoice.invoiceNumber;
_dateController.text = dateFormatter.format(_invoice.invoiceDate);
_nameController.text = _invoice.customerName;
_phoneController.text = _invoice.customerPhoneNumber;
2020-06-29 16:03:41 +06:30
// _amountController.text = _invoice.getAmount.toString();
_amountController.text = _invoice.amount.toString();
2020-06-02 14:56:51 +06:30
_statusController.text = _invoice.status.toString();
2020-06-29 16:03:41 +06:30
_handlingFeeController.text = '0';
_customFeeController.text = '0';
2020-10-07 02:33:06 +06:30
// multiImgController.setImageUrls = _receipts;
2020-06-29 16:03:41 +06:30
_descriptionController.text = 'For Electronics goods';
_balanceController.text =
(_invoice.amount - _invoice.receipts[0].amount).toString();
2020-06-26 16:04:40 +06:30
// _boxes = _invoice.packages;
2020-06-29 16:03:41 +06:30
} else {
2020-10-22 04:14:53 +06:30
_isNew = true;
2020-06-29 16:03:41 +06:30
_dateController.text = dateFormatter.format(DateTime.now());
_amountController.text = '0';
_handlingFeeController.text = '0';
_customFeeController.text = '0';
_descriptionController.text = '';
_balanceController.text = '0';
2020-10-22 04:14:53 +06:30
_invoice = Invoice(customDuties: [], cartons: []);
2020-10-14 20:56:46 +06:30
}
2020-10-16 21:38:39 +06:30
if (widget.customer != null && widget.invoice == null) {
2020-10-14 20:56:46 +06:30
user = widget.customer;
2020-10-13 11:49:35 +06:30
setState(() {
2020-10-22 04:14:53 +06:30
_isNew = true;
2020-10-13 11:49:35 +06:30
});
2020-06-29 16:03:41 +06:30
}
2020-06-26 16:04:40 +06:30
2020-10-22 04:14:53 +06:30
_loadCartons();
}
_loadCartons() async {
CartonModel cartonModel = Provider.of<CartonModel>(context, listen: false);
List<Carton> cartons = await cartonModel.getCartonsForInvoice(
widget.fcsShipment.id, widget.customer.id);
setState(() {
_cartons = cartons;
});
2020-06-02 14:56:51 +06:30
}
@override
void dispose() {
super.dispose();
}
@override
Widget build(BuildContext context) {
2020-06-04 01:36:49 +06:30
var mainModel = Provider.of<MainModel>(context);
2020-06-26 16:04:40 +06:30
var discountModel = Provider.of<DiscountModel>(context);
2020-10-13 18:38:00 +06:30
var paymentMethodModel = Provider.of<PaymentMethodModel>(context);
2020-10-22 04:14:53 +06:30
var rateModel = Provider.of<ShipmentRateModel>(context);
var rate = rateModel.rate;
2020-10-13 18:38:00 +06:30
2020-10-14 20:56:46 +06:30
final nameBox = DisplayText(
iconData: Feather.user,
labelTextKey: 'invoice.customer_name',
2020-10-16 21:38:39 +06:30
text: user != null ? user.name : 'Ko Nyi');
2020-10-14 20:56:46 +06:30
final statusBox = DisplayText(
text: _statusController.text,
iconData: Icons.av_timer,
labelTextKey: 'invoice.status');
2020-10-16 21:38:39 +06:30
final fcsIDBox = DisplayText(
text: user != null ? user.fcsID : "FCS-KRUTUG",
labelTextKey: "box.fcs.id",
icon: FcsIDIcon(),
2020-10-14 20:56:46 +06:30
);
2020-10-22 04:14:53 +06:30
final cartonTable = InvoiceCartonTable(
cartons: _cartons,
2020-10-22 05:27:03 +06:30
rate: rate,
2020-10-22 04:14:53 +06:30
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<PaymentMethod>(
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;
});
},
);
2020-06-04 01:36:49 +06:30
2020-06-02 14:56:51 +06:30
return LocalProgress(
inAsyncCall: _isLoading,
child: Scaffold(
appBar: AppBar(
centerTitle: true,
leading: new IconButton(
2020-10-14 13:54:42 +06:30
icon: new Icon(CupertinoIcons.back, color: primaryColor),
2020-06-02 14:56:51 +06:30
onPressed: () => Navigator.of(context).pop(),
),
2020-10-09 19:00:39 +06:30
backgroundColor: Colors.white,
shadowColor: Colors.transparent,
2020-06-02 14:56:51 +06:30
title: LocalText(context, 'invoice.form.title',
2020-10-09 19:00:39 +06:30
color: primaryColor, fontSize: 20),
2020-06-02 14:56:51 +06:30
),
2020-10-14 10:33:14 +06:30
body: Padding(
padding: const EdgeInsets.all(8.0),
child: ListView(
2020-06-02 14:56:51 +06:30
children: <Widget>[
2020-10-14 20:56:46 +06:30
LocalTitle(textKey: "invoice.customer_info"),
2020-10-14 10:33:14 +06:30
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),
2020-10-14 20:56:46 +06:30
fcsIDBox,
nameBox,
2020-10-22 04:14:53 +06:30
_isNew ? Container() : statusBox,
2020-10-14 20:56:46 +06:30
SizedBox(height: 20),
2020-10-22 04:14:53 +06:30
customTableHeaderBox,
customTableBox,
2020-10-14 20:56:46 +06:30
SizedBox(height: 20),
2020-10-22 04:14:53 +06:30
cartonTable,
2020-10-14 10:33:14 +06:30
LocalTitle(textKey: "invoice.cargo_type"),
2020-10-22 04:14:53 +06:30
cargoTypeTableBox,
// Column(children: getCargoTableByBox(context)),
2020-10-14 10:33:14 +06:30
SizedBox(height: 20),
2020-10-22 04:14:53 +06:30
paymentTypesBox,
2020-10-14 20:56:46 +06:30
SizedBox(height: 20),
2020-10-22 04:14:53 +06:30
_isNew
2020-10-16 21:38:39 +06:30
? Container()
: LocalTitle(
textKey: "invoice.payment_attachment",
trailing: IconButton(
icon: Icon(Icons.add_circle, color: primaryColor),
onPressed: () async {})),
2020-06-02 14:56:51 +06:30
widget.invoice == null
2020-10-09 19:00:39 +06:30
? fcsButton(
context, getLocalString(context, 'invoice.btn_create'))
2020-06-26 16:04:40 +06:30
: mainModel.isCustomer()
? Container()
: Container(
child: Column(
children: <Widget>[
2020-10-09 19:00:39 +06:30
fcsButton(context,
getLocalString(context, 'invoice.btn_save'))
2020-06-26 16:04:40 +06:30
],
)),
2020-10-22 04:14:53 +06:30
_isNew
2020-06-02 15:30:11 +06:30
? Container()
2020-10-09 19:00:39 +06:30
: fcsButton(context,
getLocalString(context, 'invoice.btn_payment_receipt'))
2020-06-02 14:56:51 +06:30
],
),
),
),
);
}
2020-10-09 19:00:39 +06:30
getTotalBalance(total) {
double balance = 0;
double custom = customFee != 0 ? customFee.toDouble() : 0;
2020-10-14 20:56:46 +06:30
double discount = _discount != null ? _discount.amount.toDouble() : 0;
2020-10-09 19:00:39 +06:30
double deliveryFee = deliveryfee != 0 ? deliveryfee.toDouble() : 0;
2020-10-14 20:56:46 +06:30
balance = (total + custom + deliveryFee) - discount;
2020-10-09 19:00:39 +06:30
return balance;
2020-06-26 16:04:40 +06:30
}
2020-10-22 04:14:53 +06:30
_addCustom(CustomDuty customDuty) {
if (customDuty == null) return;
setState(() {
_invoice.customDuties.remove(customDuty);
_invoice.customDuties.add(customDuty);
});
2020-06-26 16:04:40 +06:30
}
2020-10-22 04:14:53 +06:30
_removeCustom(CustomDuty customDuty) {
setState(() {
_invoice.customDuties.remove(customDuty);
});
2020-06-02 14:56:51 +06:30
}
2020-10-22 04:14:53 +06:30
_save() {}
2020-06-02 14:56:51 +06:30
}