add carton api

This commit is contained in:
Sai Naw Wun
2020-10-20 06:19:10 +06:30
parent 453cb71fa8
commit 4d0e5b6833
15 changed files with 685 additions and 950 deletions

View File

@@ -7,6 +7,9 @@ import 'package:fcs/domain/entities/user.dart';
import 'package:fcs/domain/vo/delivery_address.dart';
import 'package:fcs/helpers/theme.dart';
import 'package:fcs/localization/app_translations.dart';
import 'package:fcs/pages/carton/carton_cargo_table.dart';
import 'package:fcs/pages/carton/carton_mix_table.dart';
import 'package:fcs/pages/carton/carton_package_table.dart';
import 'package:fcs/pages/delivery_address/delivery_address_row.dart';
import 'package:fcs/pages/delivery_address/model/delivery_address_model.dart';
import 'package:fcs/pages/fcs_shipment/model/fcs_shipment_model.dart';
@@ -35,6 +38,7 @@ import 'package:flutter_icons/flutter_icons.dart';
import 'package:provider/provider.dart';
import 'cargo_type_editor.dart';
import 'model/carton_model.dart';
import 'widgets.dart';
class CartonEditor extends StatefulWidget {
final Carton box;
@@ -49,14 +53,12 @@ class _CartonEditorState extends State<CartonEditor> {
TextEditingController _heightController = new TextEditingController();
TextEditingController _lengthController = new TextEditingController();
Carton _box;
Carton _carton;
bool _isLoading = false;
bool _isNew;
DeliveryAddress _deliveryAddress = new DeliveryAddress();
User _user;
String _selectedCartonType;
List<Package> _packages = [];
List<Carton> _mixBoxes = [];
double volumetricRatio = 0;
double shipmentWeight = 0;
FcsShipment _fcsShipment;
@@ -66,19 +68,6 @@ class _CartonEditorState extends State<CartonEditor> {
void initState() {
super.initState();
//for shipment boxes
var boxModel = Provider.of<CartonModel>(context, listen: false);
//for mix boxes
_mixBoxes = [
boxModel.boxeList[0],
boxModel.boxeList[1],
boxModel.boxeList[2]
];
_mixBoxes.forEach((b) {
b.isChecked = false;
});
//for shipment weight
volumetricRatio = Provider.of<ShipmentRateModel>(context, listen: false)
.rate
@@ -88,16 +77,16 @@ class _CartonEditorState extends State<CartonEditor> {
_heightController.addListener(_calShipmentWeight);
if (widget.box != null) {
_box = widget.box;
_deliveryAddress = _box.deliveryAddress;
_widthController.text = _box.width.toString();
_heightController.text = _box.height.toString();
_lengthController.text = _box.length.toString();
_selectedCartonType = _box.cartonType;
_carton = widget.box;
_deliveryAddress = _carton.deliveryAddress;
_widthController.text = _carton.width.toString();
_heightController.text = _carton.height.toString();
_lengthController.text = _carton.length.toString();
_selectedCartonType = _carton.cartonType;
_isNew = false;
_user = User(fcsID: _box.fcsID, name: _box.userName);
_user = User(fcsID: _carton.fcsID, name: _carton.userName);
} else {
_box = Carton(cargoTypes: []);
_carton = Carton(cargoTypes: [], packages: [], cartons: []);
_lengthController.text = "12";
_widthController.text = "12";
_heightController.text = "12";
@@ -111,8 +100,8 @@ class _CartonEditorState extends State<CartonEditor> {
FcsShipmentModel fcsShipmentModel =
Provider.of<FcsShipmentModel>(context, listen: false);
var fcsShipments = await fcsShipmentModel.getActiveFcsShipments();
var fcsShipment = fcsShipments.firstWhere((e) => e.id == _box.fcsShipmentID,
orElse: () => null);
var fcsShipment = fcsShipments
.firstWhere((e) => e.id == _carton.fcsShipmentID, orElse: () => null);
setState(() {
_fcsShipments = fcsShipments;
_fcsShipment = fcsShipment;
@@ -140,14 +129,27 @@ class _CartonEditorState extends State<CartonEditor> {
});
setState(() {
_packages = packages;
_carton.packages = packages;
});
_populateDeliveryAddress();
}
_loadCartons() async {
if (_fcsShipment == null) return;
CartonModel cartonModel = Provider.of<CartonModel>(context, listen: false);
List<Carton> cartons =
await cartonModel.getCartonsByFcsShipment(_fcsShipment.id);
cartons.forEach((c) {
c.isChecked = true;
});
setState(() {
_carton.cartons = cartons;
});
}
_populateDeliveryAddress() {
if (_packages == null) return;
var d = _packages
if (_carton.packages == null) return;
var d = _carton.packages
.firstWhere((p) => p.isChecked && p.deliveryAddress != null,
orElse: () => null)
?.deliveryAddress;
@@ -173,15 +175,24 @@ class _CartonEditorState extends State<CartonEditor> {
@override
Widget build(BuildContext context) {
var boxModel = Provider.of<CartonModel>(context);
bool isMixBox = _selectedCartonType == carton_mix_box;
final shipmentBox = DisplayText(
text: _carton.fcsShipmentNumber,
labelTextKey: "box.fcs_shipment_num",
iconData: Ionicons.ios_airplane,
);
var fcsShipmentsBox = LocalDropdown<FcsShipment>(
callback: (v) {
setState(() {
_fcsShipment = v;
});
if (_selectedCartonType == carton_mix_box) {
_loadCartons();
}
},
labelKey: "shipment.pack.fcs.shipment",
iconData: MaterialCommunityIcons.worker,
iconData: Ionicons.ios_airplane,
display: (u) => u.shipmentNumber,
selectedValue: _fcsShipment,
values: _fcsShipments,
@@ -195,14 +206,16 @@ class _CartonEditorState extends State<CartonEditor> {
labelTextKey: "box.fcs.id",
icon: FcsIDIcon(),
)),
IconButton(
icon: Icon(Icons.search, color: primaryColor),
onPressed: () => searchUser(context, callbackUserSelect: (u) {
setState(() {
this._user = u;
_loadPackages();
});
})),
_isNew
? IconButton(
icon: Icon(Icons.search, color: primaryColor),
onPressed: () => searchUser(context, callbackUserSelect: (u) {
setState(() {
this._user = u;
_loadPackages();
});
}))
: Container(),
],
);
@@ -212,151 +225,6 @@ class _CartonEditorState extends State<CartonEditor> {
iconData: Icons.person,
);
final packageTitle = Container(
padding: EdgeInsets.only(right: 10.0, top: 20),
child: Row(
children: <Widget>[
Container(
width: 30,
),
Expanded(
child: LocalText(context, 'box.tracking.id', color: Colors.grey),
),
LocalText(context, 'box.package.desc', color: Colors.grey),
],
),
);
List<Widget> getPackageRowList() {
return _packages.asMap().entries.map((p) {
return Container(
color: p.value.isChecked
? Colors.grey.withOpacity(0.2)
: Colors.grey[50].withOpacity(0.2),
child: Container(
padding:
EdgeInsets.only(left: 0.0, right: 10.0, top: 3.0, bottom: 3.0),
decoration: BoxDecoration(
border: Border(
bottom: BorderSide(
color: p.key == _packages.length - 1
? Colors.white
: Colors.grey[350],
width: 1),
),
),
child: Row(
children: <Widget>[
Checkbox(
value: p.value.isChecked,
activeColor: primaryColor,
onChanged: (bool check) {
if (check &&
_deliveryAddress != null &&
p.value.deliveryAddress?.id != _deliveryAddress.id) {
return;
}
setState(() {
p.value.isChecked = check;
});
_populateDeliveryAddress();
}),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
p.value.trackingID,
style: textStyle,
),
Text(
p.value.deliveryAddress?.fullName ?? "",
style: textStyle,
),
Text(
p.value.deliveryAddress?.phoneNumber ?? "",
style: textStyle,
),
],
)),
new Column(
children: [
new Text(
p.value?.desc ?? "",
style: textStyle,
),
new Text(
"(${p.value?.market ?? ""})",
style: textStyle,
)
],
)
],
),
),
);
}).toList();
}
final mixBoxTitle = Container(
padding: EdgeInsets.only(right: 10.0, top: 20),
child: Row(
children: <Widget>[
Container(
width: 30,
),
Expanded(
child: LocalText(context, 'box.mix.number', color: Colors.grey),
),
LocalText(context, 'box.mix.desc', color: Colors.grey),
],
),
);
List<Widget> getMixBoxRowList() {
return _mixBoxes.asMap().entries.map((b) {
return Container(
color: b.value.isChecked
? Colors.grey.withOpacity(0.2)
: Colors.grey[50].withOpacity(0.2),
child: Container(
padding:
EdgeInsets.only(left: 0.0, right: 10.0, top: 3.0, bottom: 3.0),
decoration: BoxDecoration(
border: Border(
bottom: BorderSide(
color: b.key == _mixBoxes.length - 1
? Colors.white
: Colors.grey[350],
width: 1),
),
),
child: Row(
children: <Widget>[
Checkbox(
value: b.value.isChecked,
activeColor: primaryColor,
onChanged: (bool check) {
setState(() {
b.value.isChecked = check;
});
}),
Expanded(
child: new Text(
b.value.packageNumber,
style: textStyle,
)),
new Text(
b.value.desc == null ? "" : b.value.desc,
style: textStyle,
),
],
),
),
);
}).toList();
}
final lengthBox = LengthPicker(
controller: _lengthController,
lableKey: "box.length",
@@ -394,14 +262,13 @@ class _CartonEditorState extends State<CartonEditor> {
callBack: _save,
);
final completeBtn = LocalButton(
textKey: "box.complete.btn",
final saveBtn = LocalButton(
textKey: "box.cargo.save.btn",
callBack: _save,
);
final deliveryBtn = LocalButton(
textKey: "box.deliver.btn",
);
final cartonTypeBox = LocalRadioButtons(
readOnly: !_isNew,
values: boxModel.cartonTypes,
selectedValue: _selectedCartonType,
callback: (v) {
@@ -409,8 +276,31 @@ class _CartonEditorState extends State<CartonEditor> {
setState(() {
_selectedCartonType = v;
});
if (_selectedCartonType == carton_mix_box) {
_loadCartons();
}
});
final cargoTableTitleBox = LocalTitle(
textKey: "box.cargo.type",
trailing: IconButton(
icon: Icon(
Icons.add_circle,
color: primaryColor,
),
onPressed: () async {
CargoType cargo = await Navigator.push<CargoType>(context,
CupertinoPageRoute(builder: (context) => CargoTypeEditor()));
_addCargo(cargo);
}),
);
final cargoTableBox = CargoTable(
cargoTypes: _carton.cargoTypes,
onAdd: (c) => _addCargo(c),
onRemove: (c) => _removeCargo(c),
);
return LocalProgress(
inAsyncCall: _isLoading,
child: Scaffold(
@@ -422,7 +312,7 @@ class _CartonEditorState extends State<CartonEditor> {
),
shadowColor: Colors.transparent,
backgroundColor: Colors.white,
title: widget.box == null
title: _isNew
? LocalText(
context,
"boxes.create.title",
@@ -441,96 +331,53 @@ class _CartonEditorState extends State<CartonEditor> {
child: ListView(
shrinkWrap: true,
children: [
_isNew
? Container()
: Center(child: getCartonNumberStatus(context, _carton)),
LocalTitle(textKey: "box.type.title"),
cartonTypeBox,
LocalTitle(textKey: "box.shipment_info"),
fcsShipmentsBox,
SizedBox(
height: 10,
),
fcsIDBox,
namebox,
_selectedCartonType == carton_from_packages
? Column(
children: [
LocalTitle(textKey: "box.packages"),
packageTitle,
Divider(
color: Colors.grey[400],
),
Column(
children: getPackageRowList(),
),
],
)
: _selectedCartonType == carton_mix_box
? Column(
children: [
LocalTitle(textKey: "box.shipment.boxes"),
mixBoxTitle,
Divider(
color: Colors.grey[400],
),
Column(
children: getMixBoxRowList(),
)
],
)
: Container(),
LocalTitle(
textKey: "box.cargo.type",
trailing: IconButton(
icon: Icon(
Icons.add_circle,
color: primaryColor,
),
onPressed: () async {
CargoType cargo = await Navigator.push<CargoType>(
context,
CupertinoPageRoute(
builder: (context) => CargoTypeEditor()));
_addCargo(cargo);
}),
),
MyDataTable(
headingRowHeight: 40,
columns: [
MyDataColumn(
label: LocalText(
context,
"cargo.type",
color: Colors.grey,
),
),
MyDataColumn(
label: LocalText(
context,
"cargo.weight",
color: Colors.grey,
),
),
],
rows: getCargoRows(context),
),
LocalTitle(textKey: "box.dimension"),
dimBox,
shipmentWeightBox,
LocalTitle(textKey: "box.delivery_address"),
DefaultDeliveryAddress(
deliveryAddress: _deliveryAddress,
labelKey: "box.delivery_address",
),
widget.box == null
? createBtn
: Container(
child: Column(
children: <Widget>[
completeBtn,
widget.box.status == 'Arrived'
? deliveryBtn
: Container(),
],
)),
_isNew ? fcsShipmentsBox : shipmentBox,
...(isMixBox
? [
CartonMixTable(
cartons: _carton.cartons,
onSelect: (c, check) {
setState(() {
c.isChecked = check;
});
},
)
]
: [
fcsIDBox,
namebox,
CartonPackageTable(
packages: _carton.packages,
onSelect: (p, checked) {
if (checked &&
_deliveryAddress != null &&
p.deliveryAddress?.id != _deliveryAddress.id) {
return;
}
setState(() {
p.isChecked = checked;
});
_populateDeliveryAddress();
},
),
cargoTableTitleBox,
cargoTableBox,
LocalTitle(textKey: "box.dimension"),
dimBox,
shipmentWeightBox,
LocalTitle(textKey: "box.delivery_address"),
DefaultDeliveryAddress(
deliveryAddress: _deliveryAddress,
labelKey: "box.delivery_address",
),
]),
_isNew ? createBtn : saveBtn,
SizedBox(
height: 20,
),
@@ -541,75 +388,6 @@ class _CartonEditorState extends State<CartonEditor> {
);
}
List<MyDataRow> getCargoRows(BuildContext context) {
if (_box?.cargoTypes == null) {
return [];
}
double total = 0;
var rows = _box.cargoTypes.map((c) {
total += c.weight;
return MyDataRow(
onSelectChanged: (bool selected) async {
CargoType cargo = await Navigator.push<CargoType>(
context,
CupertinoPageRoute(
builder: (context) => CargoTypeEditor(
cargo: c,
)));
_addCargo(cargo);
},
cells: [
MyDataCell(new Text(
c.name == null ? "" : c.name,
style: textStyle,
)),
MyDataCell(
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Text(c.weight == null ? "0" : c.weight.toString(),
style: textStyle),
IconButton(
icon: Icon(
Icons.remove_circle,
color: primaryColor,
),
onPressed: () => {_removeCargo(c)},
)
],
),
),
],
);
}).toList();
var totalRow = MyDataRow(
onSelectChanged: (bool selected) {},
cells: [
MyDataCell(Align(
alignment: Alignment.centerRight,
child: LocalText(
context,
"shipment.cargo.total",
color: Colors.black87,
fontWeight: FontWeight.bold,
),
)),
MyDataCell(
Padding(
padding: const EdgeInsets.only(right: 48.0),
child: Align(
alignment: Alignment.centerRight,
child: Text(total.toString(),
style: TextStyle(fontWeight: FontWeight.bold))),
),
),
],
);
rows.add(totalRow);
return rows;
}
List<Widget> getAddressList(
BuildContext context, List<DeliveryAddress> addresses) {
return addresses.asMap().entries.map((s) {
@@ -623,30 +401,54 @@ class _CartonEditorState extends State<CartonEditor> {
_addCargo(CargoType cargo) {
if (cargo == null) return;
setState(() {
_box.cargoTypes.remove(cargo);
_box.cargoTypes.add(cargo);
_carton.cargoTypes.remove(cargo);
_carton.cargoTypes.add(cargo);
});
}
_removeCargo(CargoType cargo) {
setState(() {
_box.cargoTypes.remove(cargo);
_carton.cargoTypes.remove(cargo);
});
}
_save() async {
_box.cartonType = _selectedCartonType;
_box.fcsShipmentID = _fcsShipment.id;
_box.userID = _user.id;
_box.packages = _packages.map((e) => e.isChecked ? e : null).toList();
if (_user == null) {
showMsgDialog(context, "Error", "Please select customer");
return;
}
if (_fcsShipment == null && _isNew) {
showMsgDialog(context, "Error", "Please select FCS shipment");
return;
}
if ((_carton.cargoTypes?.length ?? 0) == 0) {
showMsgDialog(context, "Error", "Expect at least one cargo type");
return;
}
double l = double.parse(_lengthController.text, (s) => 0);
double w = double.parse(_widthController.text, (s) => 0);
double h = double.parse(_heightController.text, (s) => 0);
_box.length = l;
_box.width = w;
_box.height = h;
_box.deliveryAddress = _deliveryAddress;
if (l <= 0 || w <= 0 || h <= 0) {
showMsgDialog(context, "Error", "Invalid dimension");
return;
}
if (_deliveryAddress == null) {
showMsgDialog(context, "Error", "Invalid delivery address");
return;
}
Carton carton = Carton();
carton.id = _carton.id;
carton.cartonType = _selectedCartonType;
carton.fcsShipmentID = _isNew ? _fcsShipment.id : _carton.fcsShipmentID;
carton.userID = _user.id;
carton.cargoTypes = _carton.cargoTypes;
carton.packages = _carton.packages.where((e) => e.isChecked).toList();
carton.length = l;
carton.width = w;
carton.height = h;
carton.deliveryAddress = _deliveryAddress;
setState(() {
_isLoading = true;
@@ -655,9 +457,9 @@ class _CartonEditorState extends State<CartonEditor> {
CartonModel cartonModel =
Provider.of<CartonModel>(context, listen: false);
if (_isNew) {
await cartonModel.createCarton(_box);
await cartonModel.createCarton(carton);
} else {
await cartonModel.updateCarton(_box);
await cartonModel.updateCarton(carton);
}
Navigator.pop(context, true);
} catch (e) {