Skip to content

Commit

Permalink
DEV-41060
Browse files Browse the repository at this point in the history
  • Loading branch information
DCrow committed Apr 5, 2022
1 parent 640864a commit 62af043
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 32 deletions.
105 changes: 98 additions & 7 deletions lib/app/pages/order/order_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,91 @@ class _OrderViewState extends State<_OrderView> {
if (result != null) vm.transferOrder(result);
}

Future<void> showAcceptOrderDialog() async {
OrderViewModel vm = context.read<OrderViewModel>();
List<dynamic>? result = await showDialog<List<dynamic>>(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
Order order = vm.state.order;
String weight = order.weight != null ? Format.numberStr(order.weight! / 1000) : '';
String volume = order.volume != null ? Format.numberStr(order.volume! / 1000000) : '';
TextEditingController _weightDialogController = TextEditingController(text: weight);
TextEditingController _volumeDialogController = TextEditingController(text: volume);
bool? hasDocuments = !order.documentsReturn;
TextStyle textStyle = const TextStyle(fontSize: 14);

return StatefulBuilder(
builder: (context, setState) {
return AlertDialog(
title: const Text('Подтвердите заказ'),
content: SingleChildScrollView(
child: ListBody(
children: <Widget>[
InfoRow(
padding: EdgeInsets.zero,
title: Text('Вес, кг', style: textStyle),
trailing: TextFormField(
maxLines: 1,
autocorrect: false,
controller: _weightDialogController,
style: textStyle,
keyboardType: const TextInputType.numberWithOptions(decimal: true),
decoration: const InputDecoration(contentPadding: EdgeInsets.only())
),
),
InfoRow(
padding: EdgeInsets.zero,
title: Text('Объем, м3', style: textStyle),
trailing: TextFormField(
maxLines: 1,
autocorrect: false,
controller: _volumeDialogController,
style: textStyle,
keyboardType: const TextInputType.numberWithOptions(decimal: true),
decoration: const InputDecoration(contentPadding: EdgeInsets.only())
)
),
!order.documentsReturn ?
Container() :
InfoRow(
padding: EdgeInsets.zero,
title: Text('Документы', style: textStyle),
trailing: DropdownButton(
isExpanded: true,
menuMaxHeight: 200,
value: hasDocuments,
items: [true, false].map((e) => DropdownMenuItem<bool>(
value: e,
child: Text(e ? 'Да' : 'Нет', style: textStyle)
)).toList(),
onChanged: (bool? newVal) => setState(() => hasDocuments = newVal)
)
)
]
)
),
actions: <Widget>[
TextButton(
child: const Text(Strings.ok),
onPressed: () {
Navigator.of(context).pop(
[hasDocuments, _weightDialogController.text, _volumeDialogController.text]
);
}
),
TextButton(child: const Text(Strings.cancel), onPressed: () => Navigator.of(context).pop(null))
],
);
}
);
});

if (result != null) {
vm.acceptOrder(result[0], result[1], result[2]);
}
}

void showMessage(String message) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(message),));
}
Expand Down Expand Up @@ -202,7 +287,7 @@ class _OrderViewState extends State<_OrderView> {
),
InfoRow(
title: const Text('Вес, кг'),
trailing: !vm.state.deliverable ? Text(weight) : TextFormField(
trailing: !vm.state.scanned ? Text(weight) : TextFormField(
maxLines: 1,
autocorrect: false,
controller: _weightController,
Expand All @@ -214,7 +299,7 @@ class _OrderViewState extends State<_OrderView> {
),
InfoRow(
title: const Text('Объем, м3'),
trailing: !vm.state.deliverable ? Text(volume) : TextFormField(
trailing: !vm.state.scanned ? Text(volume) : TextFormField(
maxLines: 1,
autocorrect: false,
controller: _volumeController,
Expand Down Expand Up @@ -269,21 +354,22 @@ class _OrderViewState extends State<_OrderView> {
children: vm.state.lines.map<Widget>((e) => _buildOrderLineTile(context, e)).toList()
),
orderActions(context),
orderDeliveryActions(context)
orderPickupActions(context)
];
}

Widget orderActions(BuildContext context) {
OrderViewModel vm = context.read<OrderViewModel>();

List<Widget> actions = [
!(vm.state.transferAcceptable) ? null : TextButton(
onPressed: vm.acceptTransferOrder,
!(vm.state.storageTransferAcceptable) ? null : TextButton(
onPressed: vm.acceptStorageTransferOrder,
child: Column(children: const [Icon(Icons.how_to_reg_sharp, color: Colors.black), Text('Принять')]),
style: _buttonStyle
),
!vm.state.acceptable ? null : TextButton(
onPressed: vm.tryAcceptOrder,
onPressed: showAcceptOrderDialog,
//onPressed: vm.tryAcceptOrder,
child: Column(children: const [Icon(Icons.fact_check, color: Colors.black), Text('Приемка')]),
style: _buttonStyle
),
Expand All @@ -304,10 +390,15 @@ class _OrderViewState extends State<_OrderView> {
);
}

Widget orderDeliveryActions(BuildContext context) {
Widget orderPickupActions(BuildContext context) {
OrderViewModel vm = context.read<OrderViewModel>();

List<Widget> actions = [
!(vm.state.transferAcceptable) ? null : TextButton(
onPressed: vm.acceptTransferOrder,
child: Column(children: const [Icon(Icons.how_to_reg_sharp, color: Colors.black), Text('Принять')]),
style: _buttonStyle
),
!vm.state.deliverable ? null : TextButton(
onPressed: vm.tryConfirmOrder,
child: Column(children: const [Icon(Icons.assignment_turned_in, color: Colors.black), Text('Выдать')]),
Expand Down
3 changes: 2 additions & 1 deletion lib/app/pages/order/order_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ class OrderState {

List<OrderLine> get lines => orderWithLines.lines;
Order get order => orderWithLines.order;
bool get transferAcceptable => scanned && (storageAccess || pickupPointAccess);
bool get transferAcceptable => scanned && pickupPointAccess;
bool get storageTransferAcceptable => scanned && storageAccess;
bool get transferable => scanned && storageAccess;
bool get acceptable => scanned && storageAccess && order.firstMovementDate == null;
bool get deliverable => scanned && pickupPointAccess && order.delivered == null;
Expand Down
62 changes: 39 additions & 23 deletions lib/app/pages/order/order_view_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,34 +35,30 @@ class OrderViewModel extends PageViewModel<OrderState, OrderStateStatus> {
}

Future<void> updateWeight(String value) async {
double? parsedWeight = double.tryParse(value.replaceAll(',', '.'));
double? parsedWeight = double.tryParse(value.replaceAll(',', '.'));

if (parsedWeight == null) {
emit(state.copyWith(status: OrderStateStatus.failure, message: 'Указано не корректное число'));
return;
}

try {
int newWeight = (parsedWeight * 1000).toInt();

await _updateOrder({'weight': newWeight});
await _updateOrder({'weight': (parsedWeight * 1000).toInt()});
} on AppError catch(e) {
emit(state.copyWith(status: OrderStateStatus.failure, message: e.message));
}
}

Future<void> updateVolume(String value) async {
double? parsedVolume = double.tryParse(value.replaceAll(',', '.'));
double? parsedVolume = double.tryParse(value.replaceAll(',', '.'));

if (parsedVolume == null) {
emit(state.copyWith(status: OrderStateStatus.failure, message: 'Указано не корректное число'));
return;
}

try {
int newVolume = (parsedVolume * 1000000).toInt();

await _updateOrder({'volume': newVolume});
await _updateOrder({'volume': (parsedVolume * 1000000).toInt()});
} on AppError catch(e) {
emit(state.copyWith(status: OrderStateStatus.failure, message: e.message));
}
Expand All @@ -80,7 +76,7 @@ class OrderViewModel extends PageViewModel<OrderState, OrderStateStatus> {
}
}

Future<void> acceptOrder(bool confirmed) async {
Future<void> acceptOrder(bool confirmed, String weightStr, String volumeStr) async {
if (!confirmed) {
if (state.order.documentsReturn) {
emit(state.copyWith(
Expand All @@ -92,9 +88,18 @@ class OrderViewModel extends PageViewModel<OrderState, OrderStateStatus> {
return;
}

double? parsedWeight = double.tryParse(weightStr.replaceAll(',', '.'));
double? parsedVolume = double.tryParse(volumeStr.replaceAll(',', '.'));

if (parsedVolume == null || parsedWeight == null) {
emit(state.copyWith(status: OrderStateStatus.failure, message: 'Указано не корректное число'));
return;
}

emit(state.copyWith(status: OrderStateStatus.inProgress));

try {
await _updateOrder({'volume': (parsedVolume * 1000000).toInt(), 'weight': (parsedWeight * 1000).toInt()});
await _acceptOrder();

emit(state.copyWith(status: OrderStateStatus.success, message: 'Заказ успешно принят'));
Expand All @@ -103,6 +108,18 @@ class OrderViewModel extends PageViewModel<OrderState, OrderStateStatus> {
}
}

Future<void> acceptStorageTransferOrder() async {
emit(state.copyWith(status: OrderStateStatus.inProgress));

try {
await _acceptStorageTransferOrder();

emit(state.copyWith(status: OrderStateStatus.success, message: 'Заказ успешно принят'));
} on AppError catch(e) {
emit(state.copyWith(status: OrderStateStatus.failure, message: e.message));
}
}

Future<void> acceptTransferOrder() async {
emit(state.copyWith(status: OrderStateStatus.inProgress));

Expand Down Expand Up @@ -155,20 +172,6 @@ class OrderViewModel extends PageViewModel<OrderState, OrderStateStatus> {
));
}

void tryAcceptOrder() {
if (state.order.documentsReturn) {
emit(state.copyWith(
status: OrderStateStatus.needUserConfirmation,
confirmationCallback: acceptOrder,
message: 'Есть ли документы к заказу?',
));

return;
}

acceptOrder(true);
}

void tryConfirmOrder() {
emit(state.copyWith(
status: OrderStateStatus.needUserConfirmation,
Expand Down Expand Up @@ -275,6 +278,19 @@ class OrderViewModel extends PageViewModel<OrderState, OrderStateStatus> {
}
}

Future<void> _acceptStorageTransferOrder() async {
try {
ApiOrder newOrder = await Api(storage: app.storage).acceptStorageTransferOrder(id: state.order.id);

await _saveApiOrder(newOrder);
} on ApiException catch(e) {
throw AppError(e.errorMsg);
} catch(e, trace) {
await app.reportError(e, trace);
throw AppError(Strings.genericErrorMsg);
}
}

Future<void> _acceptTransferOrder() async {
try {
ApiOrder newOrder = await Api(storage: app.storage).acceptTransferOrder(id: state.order.id);
Expand Down
11 changes: 11 additions & 0 deletions lib/app/services/api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,17 @@ class Api {
return ApiOrder.fromJson(orderData);
}

Future<ApiOrder> acceptStorageTransferOrder({
required int id
}) async {
final orderData = await _sendRequest((dio) => dio.post(
'v1/logisto/accept_storage_transfer_order',
data: { 'id': id }
));

return ApiOrder.fromJson(orderData);
}

Future<ApiOrder> acceptTransferOrder({
required int id
}) async {
Expand Down
4 changes: 3 additions & 1 deletion lib/app/widgets/info_row.dart
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import 'package:flutter/material.dart';

class InfoRow extends StatelessWidget {
final EdgeInsets padding;
final Widget? title;
final Widget? trailing;

const InfoRow({
Key? key,
this.padding = const EdgeInsets.symmetric(horizontal: 8),
this.title,
this.trailing
}) : super(key: key);

@override
Widget build(BuildContext context) {
return Container(
padding: const EdgeInsets.symmetric(horizontal: 8),
padding: padding,
child: Row(
children: [
Flexible(
Expand Down

0 comments on commit 62af043

Please sign in to comment.