Payment conversion

This commit is contained in:
Hillel Coren 2021-06-23 18:34:27 +03:00
parent f6d0ee380e
commit 1bea59ed82
3 changed files with 49 additions and 28 deletions

View File

@ -84,7 +84,7 @@ abstract class PaymentEntity extends Object
clientId: client?.id ?? '', clientId: client?.id ?? '',
privateNotes: '', privateNotes: '',
exchangeRate: 1, exchangeRate: 1,
exchangeCurrencyId: '', exchangeCurrencyId: state?.company?.currencyId ?? '',
refunded: 0.0, refunded: 0.0,
applied: 0, applied: 0,
statusId: '', statusId: '',

View File

@ -242,7 +242,7 @@ class _EntityDropdownState extends State<EntityDropdown> {
child: AppBorder( child: AppBorder(
child: Container( child: Container(
color: Theme.of(context).cardColor, color: Theme.of(context).cardColor,
width: 350, width: 250,
constraints: BoxConstraints(maxHeight: 300), constraints: BoxConstraints(maxHeight: 300),
child: ScrollableListViewBuilder( child: ScrollableListViewBuilder(
itemCount: options.length, itemCount: options.length,

View File

@ -21,6 +21,7 @@ import 'package:invoiceninja_flutter/utils/completers.dart';
import 'package:invoiceninja_flutter/utils/formatting.dart'; import 'package:invoiceninja_flutter/utils/formatting.dart';
import 'package:invoiceninja_flutter/utils/localization.dart'; import 'package:invoiceninja_flutter/utils/localization.dart';
import 'package:invoiceninja_flutter/ui/app/entity_dropdown.dart'; import 'package:invoiceninja_flutter/ui/app/entity_dropdown.dart';
import 'package:invoiceninja_flutter/utils/money.dart';
import 'package:invoiceninja_flutter/utils/platforms.dart'; import 'package:invoiceninja_flutter/utils/platforms.dart';
class PaymentEdit extends StatefulWidget { class PaymentEdit extends StatefulWidget {
@ -121,6 +122,27 @@ class _PaymentEditState extends State<PaymentEdit> {
} }
} }
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 @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final viewModel = widget.viewModel; final viewModel = widget.viewModel;
@ -322,13 +344,19 @@ class _PaymentEditState extends State<PaymentEdit> {
title: Text(localization.convertCurrency), title: Text(localization.convertCurrency),
value: _showConvertCurrency, value: _showConvertCurrency,
onChanged: (value) { onChanged: (value) {
_exchangeRateController.removeListener(_onChanged); if (!value) {
_exchangeRateController.text = ''; _exchangeRateController.removeListener(_onChanged);
_exchangeRateController.addListener(_onChanged); _exchangeRateController.text = '';
_exchangeRateController.addListener(_onChanged);
viewModel.onChanged(payment.rebuild((b) => b viewModel.onChanged(payment.rebuild((b) => b
..exchangeCurrencyId = '' ..exchangeCurrencyId = ''
..exchangeRate = 1)); ..exchangeRate = 1));
} else {
final currency = state
.staticState.currencyMap[payment.exchangeCurrencyId];
convertCurrency(currency);
}
setState(() { setState(() {
_showConvertCurrency = value; _showConvertCurrency = value;
}); });
@ -343,23 +371,8 @@ class _PaymentEditState extends State<PaymentEdit> {
memoizedCurrencyList(viewModel.staticState.currencyMap), memoizedCurrencyList(viewModel.staticState.currencyMap),
labelText: localization.currency, labelText: localization.currency,
entityId: payment.exchangeCurrencyId, entityId: payment.exchangeCurrencyId,
onSelected: (SelectableEntity currency) { onSelected: (SelectableEntity currency) =>
double exchangeRate = 1; convertCurrency(currency),
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));
},
), ),
DecoratedFormField( DecoratedFormField(
key: ValueKey( key: ValueKey(
@ -370,11 +383,15 @@ class _PaymentEditState extends State<PaymentEdit> {
), ),
Focus( Focus(
onFocusChange: (hasFocus) { onFocusChange: (hasFocus) {
print('## _convertedAmount: $_convertedAmount');
if (_convertedAmount == 0) { if (_convertedAmount == 0) {
return; return;
} }
final exchangeRate = _convertedAmount / payment.amount; final amount = payment.isNew
? (paymentTotal + creditTotal)
: payment.amount;
final exchangeRate = _convertedAmount / amount;
_exchangeRateController.removeListener(_onChanged); _exchangeRateController.removeListener(_onChanged);
_exchangeRateController.text = formatNumber( _exchangeRateController.text = formatNumber(
exchangeRate, context, exchangeRate, context,
@ -387,11 +404,15 @@ class _PaymentEditState extends State<PaymentEdit> {
}, },
child: DecoratedFormField( child: DecoratedFormField(
key: ValueKey( key: ValueKey(
'__payment_amount_${payment.amount}_${payment.exchangeRate}__'), '__payment_amount_${paymentTotal}_${creditTotal}_${payment.exchangeRate}__'),
initialValue: initialValue:
payment.exchangeRate != 1 && payment.exchangeRate != 0 payment.exchangeRate != 1 && payment.exchangeRate != 0
? formatNumber( ? formatNumber(
payment.amount * payment.exchangeRate, context, (payment.isNew
? paymentTotal + creditTotal
: payment.amount) *
payment.exchangeRate,
context,
formatNumberType: FormatNumberType.inputMoney) formatNumberType: FormatNumberType.inputMoney)
: '', : '',
label: localization.convertedAmount, label: localization.convertedAmount,