Files
fcs/lib/pages/carton/carton_package_editor.dart

358 lines
11 KiB
Dart
Raw Normal View History

2025-03-12 17:49:27 +06:30
// ignore_for_file: deprecated_member_use, use_build_context_synchronously
2024-02-02 18:00:51 +06:30
import 'package:fcs/pages/carton/model/carton_model.dart';
2024-02-01 18:07:40 +06:30
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
2024-02-05 17:49:12 +06:30
import 'package:provider/provider.dart';
2024-02-01 18:07:40 +06:30
2024-09-22 16:49:59 +06:30
import '../../constants.dart';
2024-02-02 18:00:51 +06:30
import '../../../domain/entities/carton_size.dart';
import '../../../domain/entities/fcs_shipment.dart';
2024-02-01 18:07:40 +06:30
import '../../../domain/vo/local_step.dart';
import '../../../helpers/theme.dart';
2024-02-05 17:49:12 +06:30
import '../../domain/entities/cargo_type.dart';
2024-02-09 13:49:18 +06:30
import '../../domain/entities/carton.dart';
2024-02-05 11:13:12 +06:30
import '../../domain/entities/user.dart';
import '../carton_size/model/carton_size_model.dart';
2024-02-09 17:10:19 +06:30
import '../fcs_shipment/model/fcs_shipment_model.dart';
2024-02-05 11:13:12 +06:30
import '../main/util.dart';
import '../widgets/local_text.dart';
import '../widgets/progress.dart';
import '../widgets/step_widget.dart';
2024-02-05 17:49:12 +06:30
import 'cargo_widget.dart';
2024-02-05 11:13:12 +06:30
import 'carton_size_widget.dart';
2024-02-06 17:45:36 +06:30
import 'carton_submit.dart';
2024-02-05 17:49:12 +06:30
import 'model/package_selection_model.dart';
import 'package_selection_widget.dart';
2024-02-01 18:07:40 +06:30
2024-02-09 13:49:18 +06:30
class CartonPackageEditor extends StatefulWidget {
final Carton carton;
const CartonPackageEditor({Key? key, required this.carton}) : super(key: key);
2024-02-01 18:07:40 +06:30
@override
2024-02-09 13:49:18 +06:30
State<CartonPackageEditor> createState() => _CartonPackageEditorState();
2024-02-01 18:07:40 +06:30
}
2024-02-09 13:49:18 +06:30
class _CartonPackageEditorState extends State<CartonPackageEditor> {
2024-02-01 18:07:40 +06:30
var dateFormatter = DateFormat('dd MMM yyyy');
final NumberFormat numberFormatter = NumberFormat("#,###");
List<LocalStep> steps = [
2024-02-05 11:13:12 +06:30
LocalStep(lable: 'Size', stepType: StepType.SIZE),
LocalStep(lable: 'Packages', stepType: StepType.PACKAGES),
LocalStep(lable: 'Cargos', stepType: StepType.CARGOS),
2024-02-01 18:07:40 +06:30
LocalStep(lable: 'Submit', stepType: StepType.SUBMIT)
];
2024-02-05 17:49:12 +06:30
List<CargoType> _cargoTypes = [];
2024-02-06 17:45:36 +06:30
List<CargoType> _surchareItems = [];
double _totalWeight = 0;
2024-02-01 18:07:40 +06:30
int currentStep = 0;
2024-02-02 18:00:51 +06:30
double _length = 0;
double _width = 0;
double _height = 0;
String _selectedLastMile = delivery_caton;
2024-02-09 12:16:39 +06:30
String _billToValue = billToSender;
2024-02-02 18:00:51 +06:30
FcsShipment? _shipment;
2024-02-05 11:13:12 +06:30
String _cartonSizeType = standardCarton;
2024-02-02 18:00:51 +06:30
CartonSize? _standardSize;
bool _isLoading = false;
2024-02-01 18:07:40 +06:30
2024-02-09 13:49:18 +06:30
User? _consignee;
User? _sender;
2024-02-05 17:49:12 +06:30
@override
void initState() {
2024-02-09 17:10:19 +06:30
_init();
super.initState();
}
_init() async {
2024-02-09 17:40:51 +06:30
context.read<PackageSelectionModel>().clearSelection();
2024-02-09 13:49:18 +06:30
_sender = User(
2024-02-09 17:10:19 +06:30
name: widget.carton.senderName,
fcsID: widget.carton.senderFCSID,
id: widget.carton.senderID);
2024-02-09 13:49:18 +06:30
_consignee = User(
id: widget.carton.consigneeID,
name: widget.carton.consigneeName,
fcsID: widget.carton.consigneeFCSID);
2024-02-09 17:10:19 +06:30
_billToValue = widget.carton.billTo ?? billToSender;
_selectedLastMile = widget.carton.lastMile ?? delivery_caton;
_cargoTypes =
widget.carton.cargoTypes.map((e) => e.cloneForCarton()).toList();
_surchareItems =
widget.carton.surchareItems.map((e) => e.cloneForSurchage()).toList();
_totalWeight = _cargoTypes.fold(0, (sum, value) => sum + value.weight);
2024-02-09 17:10:19 +06:30
// check carton size type
List<CartonSize> cartonSizes = context.read<CartonSizeModel>().cartonSizes;
var sameLength =
cartonSizes.any((size) => size.length == widget.carton.length);
var sameWidth =
cartonSizes.any((size) => size.width == widget.carton.width);
var sameHeight =
cartonSizes.any((size) => size.height == widget.carton.height);
bool isStandartSize = sameLength && sameWidth && sameHeight;
if (isStandartSize) {
_cartonSizeType = standardCarton;
_standardSize = cartonSizes.firstWhere((size) =>
size.length == widget.carton.length &&
size.width == widget.carton.width &&
size.height == widget.carton.height);
} else if (widget.carton.length == 0 &&
widget.carton.width == 0 &&
widget.carton.height == 0) {
_cartonSizeType = packageCarton;
} else {
_cartonSizeType = customCarton;
2024-10-01 18:15:53 +06:30
_length = widget.carton.length.toDouble();
_width = widget.carton.width.toDouble();
_height = widget.carton.height.toDouble();
}
2024-10-01 18:15:53 +06:30
var s = await context
.read<FcsShipmentModel>()
.getFcsShipment(widget.carton.fcsShipmentID ?? "");
_shipment = s;
2024-02-09 17:10:19 +06:30
if (mounted) {
setState(() {});
}
2024-02-05 17:49:12 +06:30
}
2024-02-01 18:07:40 +06:30
@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: () {
if (currentStep == 0) {
Navigator.of(context).pop();
}
if (currentStep > 0) {
setState(() {
currentStep -= 1;
});
}
return Future.value(false);
},
child: LocalProgress(
inAsyncCall: _isLoading,
child: Scaffold(
appBar: AppBar(
elevation: 0,
centerTitle: true,
leading: IconButton(
icon: const Icon(CupertinoIcons.back,
color: primaryColor, size: 25),
onPressed: () {
if (currentStep == 0) {
Navigator.of(context).pop();
}
if (currentStep > 0) {
setState(() {
currentStep -= 1;
});
}
},
),
backgroundColor: Colors.white,
title: Column(
children: [
LocalText(context, 'box.update_title',
color: primaryColor, fontSize: 20),
Text(
widget.carton.cartonNumber ?? '',
style: TextStyle(color: primaryColor, fontSize: 14),
)
],
),
2024-02-01 18:07:40 +06:30
),
body: Column(
children: [
StepperWidget(
labels: steps.map((e) => e.lable).toList(),
currentStep: currentStep,
2024-02-05 11:13:12 +06:30
eachStepWidth: MediaQuery.of(context).size.width / 4,
2024-02-01 18:07:40 +06:30
onChange: (index) {
if (index > currentStep) {
return;
}
setState(() {
currentStep = index;
});
},
),
getContent(currentStep)
],
))),
);
}
Widget getContent(int index) {
var step = steps[index];
2024-02-05 11:13:12 +06:30
if (step.stepType == StepType.SIZE) {
2024-02-02 18:00:51 +06:30
return Expanded(
2024-02-05 11:13:12 +06:30
child: CartonSizeWidget(
lastMile: _selectedLastMile,
2024-02-09 12:16:39 +06:30
billType: _billToValue,
2024-02-09 13:49:18 +06:30
sender: _sender!,
consignee: _consignee!,
2024-02-02 18:00:51 +06:30
shipment: _shipment,
2024-02-05 11:13:12 +06:30
cartonSizeType: _cartonSizeType,
2024-02-02 18:00:51 +06:30
standardSize: _standardSize,
length: _length,
width: _width,
height: _height,
2024-02-01 18:07:40 +06:30
onPrevious: () {
Navigator.pop(context);
},
2024-02-09 13:49:18 +06:30
onContinue: (deliveryType, billType, shipment, cartonSizeType,
2024-02-02 18:00:51 +06:30
{standardSize, length, width, height}) {
setState(() {
_selectedLastMile = deliveryType;
2024-02-09 12:16:39 +06:30
_billToValue = billType;
2024-02-02 18:00:51 +06:30
_shipment = shipment;
2024-02-05 11:13:12 +06:30
_cartonSizeType = cartonSizeType;
2024-02-02 18:00:51 +06:30
_standardSize = standardSize;
_length = length ?? 0;
_width = width ?? 0;
_height = height ?? 0;
currentStep += 1;
});
},
2024-02-01 18:07:40 +06:30
));
2024-02-05 11:13:12 +06:30
} else if (step.stepType == StepType.PACKAGES) {
2024-02-01 18:07:40 +06:30
return Expanded(
child: PackageSelectionWidget(
2024-02-09 13:49:18 +06:30
sender: _sender!,
consignee: _consignee!,
2024-02-05 17:49:12 +06:30
shipment: _shipment!,
onContinue: () {
2024-02-05 17:49:12 +06:30
setState(() {
currentStep += 1;
});
},
onPrevious: () {
2024-02-05 17:49:12 +06:30
setState(() {
currentStep -= 1;
});
},
),
2024-02-01 18:07:40 +06:30
);
2024-02-05 11:13:12 +06:30
} else if (step.stepType == StepType.CARGOS) {
2024-02-01 18:07:40 +06:30
return Expanded(
2024-02-05 17:49:12 +06:30
child: CargoWidget(
2024-02-09 13:49:18 +06:30
sender: _sender!,
consignee: _consignee!,
2024-02-05 17:49:12 +06:30
cargoTypes: _cargoTypes,
2024-02-06 17:45:36 +06:30
surchargeItems: _surchareItems,
totalWeight: _totalWeight,
2025-04-03 11:41:04 +06:30
onContinue: (cargoTypes, customDuties, totalWeight) {
2024-02-05 17:49:12 +06:30
setState(() {
2025-04-03 11:41:04 +06:30
_totalWeight = totalWeight;
2024-02-05 17:49:12 +06:30
_cargoTypes = List.from(cargoTypes);
2024-02-06 17:45:36 +06:30
_surchareItems = List.from(customDuties);
2024-02-05 17:49:12 +06:30
currentStep += 1;
});
},
onPrevious: (cargoTypes, customDuties, totalWeight) {
2024-02-05 17:49:12 +06:30
setState(() {
_totalWeight = totalWeight;
2024-02-05 17:49:12 +06:30
_cargoTypes = List.from(cargoTypes);
2024-02-06 17:45:36 +06:30
_surchareItems = List.from(customDuties);
2024-02-05 17:49:12 +06:30
currentStep -= 1;
});
},
),
);
} else {
return Expanded(
2024-02-06 17:45:36 +06:30
child: CartonSubmit(
2024-02-09 17:10:19 +06:30
isNew: false,
2024-02-09 13:49:18 +06:30
sender: _sender!,
consingee: _consignee!,
2024-02-09 12:16:39 +06:30
billToValue: _billToValue,
2024-02-06 17:45:36 +06:30
cartonSizeType: _cartonSizeType,
standardSize: _standardSize,
length: _length,
width: _width,
height: _height,
lastMile: _selectedLastMile,
2024-02-06 17:45:36 +06:30
shipment: _shipment!,
// packages: _packages,
2024-02-06 17:45:36 +06:30
cargoTypes: _cargoTypes,
surchareItems: _surchareItems,
onCreate: () {
_create();
},
onPrevious: () {
setState(() {
currentStep -= 1;
});
},
),
2024-02-01 18:07:40 +06:30
);
2024-02-05 11:13:12 +06:30
}
}
_create() async {
setState(() {
_isLoading = true;
});
try {
double length = 0;
double width = 0;
double height = 0;
if (_cartonSizeType == standardCarton) {
if (_standardSize != null) {
length = _standardSize!.length;
width = _standardSize!.width;
height = _standardSize!.height;
}
} else if (_cartonSizeType == customCarton) {
length = _length;
width = _width;
height = _height;
} else {
length = 0;
width = 0;
height = 0;
}
var carton = Carton(
2024-10-01 18:15:53 +06:30
id: widget.carton.id,
cartonType: carton_from_packages,
2024-10-01 18:15:53 +06:30
senderID: widget.carton.senderID,
consigneeID: widget.carton.consigneeID,
billTo: _billToValue,
lastMile: _selectedLastMile,
fcsShipmentID: _shipment?.id,
length: length,
width: width,
height: height,
// packages: _packages,
cargoTypes: _cargoTypes,
surchareItems: _surchareItems);
await context.read<CartonModel>().updateCarton(carton);
2024-02-05 11:13:12 +06:30
Navigator.pop(context, true);
} catch (e) {
showMsgDialog(context, "Error", e.toString());
} finally {
setState(() {
_isLoading = false;
});
2024-02-01 18:07:40 +06:30
}
}
}