From 1bea59ed828893b0ee3ac85784dfe40d8e09f3dc Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Wed, 23 Jun 2021 18:34:27 +0300 Subject: [PATCH] Payment conversion --- lib/data/models/payment_model.dart | 2 +- lib/ui/app/entity_dropdown.dart | 2 +- lib/ui/payment/edit/payment_edit.dart | 73 +++++++++++++++++---------- 3 files changed, 49 insertions(+), 28 deletions(-) diff --git a/lib/data/models/payment_model.dart b/lib/data/models/payment_model.dart index c6af62252..e596390cd 100644 --- a/lib/data/models/payment_model.dart +++ b/lib/data/models/payment_model.dart @@ -84,7 +84,7 @@ abstract class PaymentEntity extends Object clientId: client?.id ?? '', privateNotes: '', exchangeRate: 1, - exchangeCurrencyId: '', + exchangeCurrencyId: state?.company?.currencyId ?? '', refunded: 0.0, applied: 0, statusId: '', diff --git a/lib/ui/app/entity_dropdown.dart b/lib/ui/app/entity_dropdown.dart index 8fcd5741a..260f3761e 100644 --- a/lib/ui/app/entity_dropdown.dart +++ b/lib/ui/app/entity_dropdown.dart @@ -242,7 +242,7 @@ class _EntityDropdownState extends State { child: AppBorder( child: Container( color: Theme.of(context).cardColor, - width: 350, + width: 250, constraints: BoxConstraints(maxHeight: 300), child: ScrollableListViewBuilder( itemCount: options.length, diff --git a/lib/ui/payment/edit/payment_edit.dart b/lib/ui/payment/edit/payment_edit.dart index c9283fa19..c5638c1d4 100644 --- a/lib/ui/payment/edit/payment_edit.dart +++ b/lib/ui/payment/edit/payment_edit.dart @@ -21,6 +21,7 @@ import 'package:invoiceninja_flutter/utils/completers.dart'; import 'package:invoiceninja_flutter/utils/formatting.dart'; import 'package:invoiceninja_flutter/utils/localization.dart'; import 'package:invoiceninja_flutter/ui/app/entity_dropdown.dart'; +import 'package:invoiceninja_flutter/utils/money.dart'; import 'package:invoiceninja_flutter/utils/platforms.dart'; class PaymentEdit extends StatefulWidget { @@ -121,6 +122,27 @@ class _PaymentEditState extends State { } } + void convertCurrency(SelectableEntity currency) { + final viewModel = widget.viewModel; + final payment = viewModel.payment; + final state = viewModel.state; + double exchangeRate = 1; + if (currency != null) { + final client = state.clientState.get(payment.clientId); + exchangeRate = getExchangeRate(state.staticState.currencyMap, + fromCurrencyId: currency.id, toCurrencyId: client.currencyId); + } + + _exchangeRateController.removeListener(_onChanged); + _exchangeRateController.text = formatNumber(exchangeRate, context, + formatNumberType: FormatNumberType.inputMoney); + _exchangeRateController.addListener(_onChanged); + + viewModel.onChanged(payment.rebuild((b) => b + ..exchangeCurrencyId = currency?.id ?? '' + ..exchangeRate = exchangeRate)); + } + @override Widget build(BuildContext context) { final viewModel = widget.viewModel; @@ -322,13 +344,19 @@ class _PaymentEditState extends State { title: Text(localization.convertCurrency), value: _showConvertCurrency, onChanged: (value) { - _exchangeRateController.removeListener(_onChanged); - _exchangeRateController.text = ''; - _exchangeRateController.addListener(_onChanged); + if (!value) { + _exchangeRateController.removeListener(_onChanged); + _exchangeRateController.text = ''; + _exchangeRateController.addListener(_onChanged); - viewModel.onChanged(payment.rebuild((b) => b - ..exchangeCurrencyId = '' - ..exchangeRate = 1)); + viewModel.onChanged(payment.rebuild((b) => b + ..exchangeCurrencyId = '' + ..exchangeRate = 1)); + } else { + final currency = state + .staticState.currencyMap[payment.exchangeCurrencyId]; + convertCurrency(currency); + } setState(() { _showConvertCurrency = value; }); @@ -343,23 +371,8 @@ class _PaymentEditState extends State { memoizedCurrencyList(viewModel.staticState.currencyMap), labelText: localization.currency, entityId: payment.exchangeCurrencyId, - onSelected: (SelectableEntity currency) { - double exchangeRate = 1; - if (currency != null) { - exchangeRate = state - .staticState.currencyMap[currency?.id].exchangeRate; - } - - _exchangeRateController.removeListener(_onChanged); - _exchangeRateController.text = formatNumber( - exchangeRate, context, - formatNumberType: FormatNumberType.inputMoney); - _exchangeRateController.addListener(_onChanged); - - viewModel.onChanged(payment.rebuild((b) => b - ..exchangeCurrencyId = currency?.id ?? '' - ..exchangeRate = exchangeRate)); - }, + onSelected: (SelectableEntity currency) => + convertCurrency(currency), ), DecoratedFormField( key: ValueKey( @@ -370,11 +383,15 @@ class _PaymentEditState extends State { ), Focus( onFocusChange: (hasFocus) { + print('## _convertedAmount: $_convertedAmount'); if (_convertedAmount == 0) { return; } - final exchangeRate = _convertedAmount / payment.amount; + final amount = payment.isNew + ? (paymentTotal + creditTotal) + : payment.amount; + final exchangeRate = _convertedAmount / amount; _exchangeRateController.removeListener(_onChanged); _exchangeRateController.text = formatNumber( exchangeRate, context, @@ -387,11 +404,15 @@ class _PaymentEditState extends State { }, child: DecoratedFormField( key: ValueKey( - '__payment_amount_${payment.amount}_${payment.exchangeRate}__'), + '__payment_amount_${paymentTotal}_${creditTotal}_${payment.exchangeRate}__'), initialValue: payment.exchangeRate != 1 && payment.exchangeRate != 0 ? formatNumber( - payment.amount * payment.exchangeRate, context, + (payment.isNew + ? paymentTotal + creditTotal + : payment.amount) * + payment.exchangeRate, + context, formatNumberType: FormatNumberType.inputMoney) : '', label: localization.convertedAmount,