Files
fcs/lib/pages/invoice/invoice_editor.dart
2020-10-18 02:38:46 +06:30

891 lines
29 KiB
Dart

import 'package:fcs/domain/entities/carton.dart';
import 'package:fcs/domain/entities/cargo_type.dart';
import 'package:fcs/domain/entities/custom_duty.dart';
import 'package:fcs/domain/entities/discount.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/localization/app_translations.dart';
import 'package:fcs/pages/discount/discount_list.dart';
import 'package:fcs/pages/discount/model/discount_model.dart';
import 'package:fcs/pages/main/model/language_model.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/user_search/user_serach.dart';
import 'package:fcs/pages/widgets/bottom_up_page_route.dart';
import 'package:fcs/pages/widgets/discount_dropdown.dart';
import 'package:fcs/pages/widgets/display_text.dart';
import 'package:fcs/pages/widgets/fcs_id_icon.dart';
import 'package:fcs/pages/widgets/input_text.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/multi_img_file.dart';
import 'package:fcs/pages/widgets/my_data_table.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';
class InvoiceEditor extends StatefulWidget {
final Invoice invoice;
final User customer;
InvoiceEditor({this.invoice, this.customer});
@override
_InvoiceEditorState createState() => _InvoiceEditorState();
}
class _InvoiceEditorState extends State<InvoiceEditor> {
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<Carton> _boxes = [];
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<Carton> selectedBoxes = [];
List<CustomDuty> customs = [];
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)
];
List<String> _receipts = [
"assets/buying_online_with_first_last_name.png",
];
@override
void initState() {
super.initState();
volumetricRatio = Provider.of<ShipmentRateModel>(context, listen: false)
.rate
.volumetricRatio;
if (widget.invoice != null) {
_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 {
_dateController.text = dateFormatter.format(DateTime.now());
_amountController.text = '0';
_handlingFeeController.text = '0';
_customFeeController.text = '0';
_descriptionController.text = '';
_balanceController.text = '0';
}
if (widget.customer != null && widget.invoice == null) {
user = widget.customer;
setState(() {
isNew = true;
});
}
_boxes = [
Carton(
shipmentNumber: "A202",
receiverNumber: "3",
receiverName: "Ko Myo Min",
boxNumber: "1",
rate: 7,
packageType: "General",
weight: 75,
status: "Packed",
receiverAddress: '1 Bo Yar Nyunt St.\nDagon Tsp, Yangon',
cargoDesc: "Clothes",
arrivedDate: DateTime(2020, 6, 1),
width: 10,
height: 10,
length: 10,
// packages: packages,
// statusHistory: statusHistory,
cargoTypes: [
CargoType(name: 'General Cargo', weight: 25),
CargoType(name: 'Medicine', weight: 20),
CargoType(name: 'Dangerous Cargo', weight: 30)
]),
Carton(
shipmentNumber: "A202",
receiverNumber: "3",
receiverName: "Ko Myo Min",
boxNumber: "2",
rate: 7,
packageType: "General",
weight: 75,
status: "Packed",
cargoDesc: "Clothes",
arrivedDate: DateTime(2020, 6, 1),
width: 10,
height: 10,
length: 10,
// statusHistory: statusHistory,
// packages: packages,
receiverAddress: '1 Bo Yar Nyunt St.\nDagon Tsp, Yangon',
cargoTypes: [
CargoType(name: 'General Cargo', weight: 25),
CargoType(name: 'Medicine', weight: 20),
CargoType(name: 'Dangerous Cargo', weight: 30)
])
];
}
@override
void dispose() {
super.dispose();
}
@override
Widget build(BuildContext context) {
var mainModel = Provider.of<MainModel>(context);
var discountModel = Provider.of<DiscountModel>(context);
var paymentMethodModel = Provider.of<PaymentMethodModel>(context);
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(),
);
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: <Widget>[
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),
LocalTitle(textKey: "invoice.box_info"),
Center(child: Column(children: getCartonRows(context))),
SizedBox(height: 20),
LocalTitle(
textKey: "invoice.custom_fee",
trailing: IconButton(
icon: Icon(Icons.add_circle, color: primaryColor),
onPressed: () async {
CustomDuty custom = await Navigator.of(context).push(
CupertinoPageRoute(
builder: (context) =>
CustomList(selected: true)));
setState(() {
if (custom != null) customs.add(custom);
});
})),
Column(children: getCustomFeeRows(context)),
SizedBox(height: 20),
LocalTitle(textKey: "invoice.cargo_type"),
Column(children: getCargoTableByBox(context)),
SizedBox(height: 20),
Container(
padding: EdgeInsets.only(top: 5, left: 18),
child: Row(
children: <Widget>[
Expanded(
child: LocalText(context, 'invoice.payment_method',
fontSize: 16,
color: Colors.grey,
fontWeight: FontWeight.bold),
),
Container(
width: 150.0,
child: DropdownButtonFormField(
icon: Icon(
Icons.edit,
color: primaryColor,
),
value: paymentMethod,
items: paymentMethodModel.paymentMethods
.map((e) => DropdownMenuItem(
child: Text(e.name), value: e.name))
.toList(),
onChanged: (selected) => {},
),
),
],
),
),
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: <Widget>[
fcsButton(context,
getLocalString(context, 'invoice.btn_save'))
],
)),
isNew
? Container()
: fcsButton(context,
getLocalString(context, 'invoice.btn_payment_receipt'))
],
),
),
),
);
}
getCartonRows(BuildContext context) {
List<Widget> dataRow = [];
dataRow = _boxes.map((box) {
return Container(
height: 50,
decoration: BoxDecoration(
border: Border(bottom: BorderSide(color: Colors.grey))),
padding:
const EdgeInsets.only(left: 5.0, right: 5.0, top: 5.0, bottom: 5.0),
child: Row(
children: [
Container(
width: 35,
child: Checkbox(
value: true, onChanged: (v) {}, activeColor: primaryColor),
),
Expanded(
flex: 1,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(box.packageNumber),
Text(box.shipmentNumber),
],
)),
Expanded(
flex: 2,
child: Text(
box.length == null
? ""
: box.length.toString() +
' x ' +
box.length.toString() +
' x ' +
box.height.toString(),
textAlign: TextAlign.center)),
Expanded(
flex: 2,
child: Center(
child: Text(
box.getShipmentWeight(volumetricRatio).toString()))),
],
),
);
}).toList();
dataRow.insert(
0,
Container(
decoration: BoxDecoration(
border: Border(bottom: BorderSide(color: Colors.grey))),
padding: const EdgeInsets.only(
left: 5.0, right: 5.0, top: 5.0, bottom: 15.0),
child: Row(
children: [
Container(width: 33),
Expanded(
flex: 1,
child: Center(
child: LocalText(
context,
"invoice.box.number",
color: Colors.grey,
),
)),
Expanded(
flex: 2,
child: Center(
child: Text('L x W x H',
style: TextStyle(color: Colors.grey)))),
Expanded(
flex: 2,
child: Center(
child: LocalText(
context,
"invoice.shipment_weight",
color: Colors.grey,
),
)),
],
),
));
return dataRow;
}
getCustomFeeRows(BuildContext context) {
customFee = 0;
List<Widget> dataRow = [];
dataRow = customs.map((custom) {
customFee += custom.fee;
return Container(
decoration: BoxDecoration(
border: Border(bottom: BorderSide(color: Colors.grey))),
padding:
const EdgeInsets.only(left: 5.0, right: 5.0, top: 5.0, bottom: 5.0),
child: Row(
children: [
Expanded(flex: 2, child: Text('${custom.productType}')),
Expanded(
flex: 1,
child: Text('\$ ${custom.fee}', textAlign: TextAlign.center)),
Expanded(
child: IconButton(
icon: Icon(Icons.remove_circle, color: primaryColor),
onPressed: () {
customs.remove(custom);
}))
],
),
);
}).toList();
dataRow.insert(
0,
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('Product', style: TextStyle(color: Colors.grey))),
Expanded(
flex: 1,
child: Text('Fee',
textAlign: TextAlign.center,
style: TextStyle(color: Colors.grey))),
Expanded(flex: 1, child: Container())
],
),
));
dataRow.insert(
dataRow.length,
Container(
padding: const EdgeInsets.only(
left: 5.0, right: 5.0, top: 15.0, bottom: 15.0),
child: Row(
children: [
Expanded(
flex: 2,
child: Center(
child: LocalText(
context,
'invoice.total_custom_fee',
color: Colors.black,
fontWeight: FontWeight.bold,
),
),
),
Expanded(
flex: 1,
child: Center(
child: Text('\$ $customFee',
textAlign: TextAlign.center,
style: TextStyle(fontWeight: FontWeight.bold)))),
Expanded(
child: Container(),
)
],
),
));
return dataRow;
}
getCargoTableByBox(BuildContext context) {
var discountModel = Provider.of<DiscountModel>(context);
total = 0;
List<Widget> dataRow = _cargoTypes.map((cargo) {
var amount = cargo.weight * cargo.rate;
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.weight} x ${cargo.rate}',
textAlign: TextAlign.center)),
Expanded(
child: Text('\$ $amount',
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',
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(selected: true)));
setState(() {
if (discount != null) _discount = discount;
});
}),
Expanded(
child: Text(
'\$ ( ${_discount != null ? _discount.amount.toInt() : 0} )',
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('\$ ${customFee}',
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('\$ ${handlingFee.toString()}',
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: isSwitched,
onChanged: (value) {
setState(() {
isSwitched = value;
if (value) {
deliveryfee = 5;
} else {
deliveryfee = 0;
}
print(isSwitched);
});
},
activeTrackColor: primaryColor.withOpacity(0.8),
activeColor: primaryColor,
),
Expanded(
child: Text('\$ $deliveryfee',
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('\$ ${getTotalBalance(total)}',
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('\$ ${getTotalBalance(total)}',
textAlign: TextAlign.end,
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
color: primaryColor)))
],
),
));
return dataRow;
}
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;
}
List<MyDataRow> getBoxRow(BuildContext context) {
return _boxes.map((p) {
p.cargoTypes.map((cargo) {
_cargoTypes.asMap().map((index, _cargo) {
if (_cargo.id == cargo.id) {
setState(() {
_cargoTypes[index].weight += cargo.weight;
});
}
});
});
return MyDataRow(
onSelectChanged: (bool selected) {},
cells: [
MyDataCell(Checkbox(
value: true,
onChanged: (value) {
selectedBoxes.add(p);
},
)),
MyDataCell(new Text(
p.boxNumber == null
? ""
: '${p.shipmentNumber}-${p.receiverNumber} #${p.boxNumber}',
style: textStyle,
)),
MyDataCell(new Text(
p.length == null
? ""
: p.length.toString() +
' x ' +
p.length.toString() +
' x ' +
p.height.toString(),
style: textStyle,
)),
],
);
}).toList();
}
List<MyDataRow> getCargoDataRow(BuildContext context) {
return _cargoTypes.asMap().entries.map((c) {
var cargo = c.value;
var amt = cargo.weight * cargo.rate;
return MyDataRow(
onSelectChanged: (bool selected) {},
cells: [
MyDataCell(new Text(
cargo.name,
style: textStyle,
)),
MyDataCell(new Text(
cargo.weight.toString() + ' x ' + cargo.rate.toString(),
style: textStyle,
)),
MyDataCell(new Text(
"\$$amt",
style: textStyle,
)),
],
);
}).toList()
// .insert(_cargoTypes.length,MyDataRow(cells: [
// MyDataCell(new Text('')),
// MyDataCell(new Text('Total')),
// MyDataCell(new Text(
// "\$5000",
// style: textStyle,
// )),
// ])
// )
;
}
}