From 7629dfd54ac231c5000e92988983e8c19eb7653a Mon Sep 17 00:00:00 2001 From: James Cole Date: Mon, 2 Jul 2018 15:37:56 +0200 Subject: [PATCH] Fix issues with #1521 --- .../Transaction/SingleController.php | 4 ++ app/Http/Requests/JournalFormRequest.php | 64 +++++++++++++++++++ app/Support/ExpandedForm.php | 4 ++ public/js/ff/firefly.js | 2 + public/js/ff/transactions/single/common.js | 13 +++- public/js/ff/transactions/single/create.js | 17 ++++- resources/views/form/amount.twig | 4 +- 7 files changed, 101 insertions(+), 7 deletions(-) diff --git a/app/Http/Controllers/Transaction/SingleController.php b/app/Http/Controllers/Transaction/SingleController.php index ebbecb07c0..17da1e6fb5 100644 --- a/app/Http/Controllers/Transaction/SingleController.php +++ b/app/Http/Controllers/Transaction/SingleController.php @@ -151,6 +151,10 @@ class SingleController extends Controller $optionalFields = Preferences::get('transaction_journal_optional_fields', [])->data; $source = (int)$request->get('source'); + // grab old currency ID from old data: + $currencyID = (int)$request->old('amount_currency_id_amount'); + $preFilled['amount_currency_id_amount'] = $currencyID; + if (($what === 'withdrawal' || $what === 'transfer') && $source > 0) { $preFilled['source_id'] = $source; } diff --git a/app/Http/Requests/JournalFormRequest.php b/app/Http/Requests/JournalFormRequest.php index d98c6f43ce..05cbead4dd 100644 --- a/app/Http/Requests/JournalFormRequest.php +++ b/app/Http/Requests/JournalFormRequest.php @@ -24,6 +24,7 @@ namespace FireflyIII\Http\Requests; use FireflyIII\Exceptions\FireflyException; use FireflyIII\Models\TransactionType; +use Illuminate\Validation\Validator; /** * Class JournalFormRequest. @@ -179,6 +180,22 @@ class JournalFormRequest extends Request return $rules; } + /** + * Configure the validator instance. + * + * @param Validator $validator + * + * @return void + */ + public function withValidator(Validator $validator): void + { + $validator->after( + function (Validator $validator) { + $this->validNativeAmount($validator); + } + ); + } + /** * Inspired by https://www.youtube.com/watch?v=WwnI0RS6J5A. * @@ -212,4 +229,51 @@ class JournalFormRequest extends Request return $rules; } + + /** + * @param Validator $validator + */ + private function validNativeAmount(Validator $validator): void + { + $data = $validator->getData(); + $type = $data['what'] ?? 'invalid'; + if ($type === 'withdrawal') { + $selectedCurrency = (int)($data['amount_currency_id_amount'] ?? 0); + $accountCurrency = (int)($data['source_account_currency'] ?? 0); + $nativeAmount = (string)$data['native_amount']; + if ($selectedCurrency !== $accountCurrency && '' === $nativeAmount) { + $validator->errors()->add('native_amount', trans('validation.numeric', ['attribute' => 'native_amount'])); + + return; + } + } + + // same thing for deposits: + if ($type === 'deposit') { + $selectedCurrency = (int)($data['amount_currency_id_amount'] ?? 0); + $accountCurrency = (int)($data['destination_account_currency'] ?? 0); + $nativeAmount = (string)$data['native_amount']; + if ($selectedCurrency !== $accountCurrency && '' === $nativeAmount) { + $validator->errors()->add('native_amount', trans('validation.numeric', ['attribute' => 'native_amount'])); + + return; + } + } + + // and for transfers + if ($type === 'transfer') { + $sourceCurrency = (int)($data['source_account_currency'] ?? 0); + $destinationCurrency = (int)($data['destination_account_currency'] ?? 0); + $sourceAmount = (string)$data['source_amount']; + $destinationAmount = (string)$data['destination_amount']; + if ($sourceCurrency !== $destinationCurrency && '' === $sourceAmount) { + $validator->errors()->add('source_amount', trans('validation.numeric', ['attribute' => 'source_amount'])); + } + + if ($sourceCurrency !== $destinationCurrency && '' === $destinationAmount) { + $validator->errors()->add('destination_amount', trans('validation.numeric', ['attribute' => 'destination_amount'])); + } + return; + } + } } diff --git a/app/Support/ExpandedForm.php b/app/Support/ExpandedForm.php index b6a46453b1..24669d64a0 100644 --- a/app/Support/ExpandedForm.php +++ b/app/Support/ExpandedForm.php @@ -37,6 +37,7 @@ use FireflyIII\Repositories\RuleGroup\RuleGroupRepositoryInterface; use Form; use Illuminate\Support\Collection; use Illuminate\Support\MessageBag; +use Log; use RuntimeException; use Session; @@ -859,10 +860,13 @@ class ExpandedForm $key = 'amount_currency_id_' . $name; $sentCurrencyId = isset($preFilled[$key]) ? (int)$preFilled[$key] : $defaultCurrency->id; + Log::debug(sprintf('Sent currency ID is %d', $sentCurrencyId)); + // find this currency in set of currencies: foreach ($currencies as $currency) { if ($currency->id === $sentCurrencyId) { $defaultCurrency = $currency; + Log::debug(sprintf('default currency is now %s', $defaultCurrency->code)); break; } } diff --git a/public/js/ff/firefly.js b/public/js/ff/firefly.js index 10ecacbcc3..ba48ecd4e6 100644 --- a/public/js/ff/firefly.js +++ b/public/js/ff/firefly.js @@ -83,6 +83,7 @@ $(function () { function currencySelect(e) { "use strict"; + console.log('In currencySelect() because somebody clicked a .currency-option.'); // clicked on var target = $(e.target); // target is the tag. @@ -105,6 +106,7 @@ function currencySelect(e) { var id = target.data('id'); // update the hidden input: + console.log('Updated ' + hiddenInputName + ' to ID ' + id); $('input[name="' + hiddenInputName + '"]').val(id); // update the symbol: diff --git a/public/js/ff/transactions/single/common.js b/public/js/ff/transactions/single/common.js index 122a71d591..e42850781f 100644 --- a/public/js/ff/transactions/single/common.js +++ b/public/js/ff/transactions/single/common.js @@ -92,11 +92,14 @@ function setCommonAutocomplete() { function selectsForeignCurrency() { console.log('In selectsForeignCurrency()'); var foreignCurrencyId = parseInt($('input[name="amount_currency_id_amount"]').val()); + console.log('Foreign currency ID is ' + foreignCurrencyId); var selectedAccountId = getAccountId(); var nativeCurrencyId = parseInt(accountInfo[selectedAccountId].preferredCurrency); - if (foreignCurrencyId !== nativeCurrencyId) { + console.log('Native currency ID is ' + nativeCurrencyId); + if (foreignCurrencyId !== nativeCurrencyId) { + console.log('These are not the same.'); // the input where the native amount is entered gets the symbol for the native currency: $('.non-selectable-currency-symbol').text(currencyInfo[nativeCurrencyId].symbol); @@ -105,18 +108,24 @@ function selectsForeignCurrency() { // both holders are shown to the user: $('#exchange_rate_instruction_holder').show(); + if(what !== 'transfer') { + console.log('Show native amount holder.'); $('#native_amount_holder').show(); + } // if possible the amount is already exchanged for the foreign currency convertForeignToNative(); } if (foreignCurrencyId === nativeCurrencyId) { + console.log('These are the same.'); $('#exchange_rate_instruction_holder').hide(); + console.log('Hide native amount holder (a)'); $('#native_amount_holder').hide(); // make all other inputs empty - $('input[name="destination_amount"]').val(""); + //console.log('Make destination_amount empty!'); + //$('input[name="destination_amount"]').val(""); $('input[name="native_amount"]').val(""); } diff --git a/public/js/ff/transactions/single/create.js b/public/js/ff/transactions/single/create.js index 2f2294196c..47e045c22b 100644 --- a/public/js/ff/transactions/single/create.js +++ b/public/js/ff/transactions/single/create.js @@ -38,12 +38,12 @@ $(document).ready(function () { // when user changes source account or destination, native currency may be different. - $('select[name="source_id"]').on('change', function() { + $('select[name="source_id"]').on('change', function () { selectsDifferentSource(); // do something for transfers: validateCurrencyForTransfer(); }); - $('select[name="destination_id"]').on('change', function() { + $('select[name="destination_id"]').on('change', function () { selectsDifferentDestination(); // do something for transfers: validateCurrencyForTransfer(); @@ -57,6 +57,15 @@ $(document).ready(function () { // when user selects different currency, $('.currency-option').on('click', selectsForeignCurrency); + + + // overrule click on currency: + if(useAccountCurrency === false) { + $('.currency-option[data-id="' + overruleCurrency + '"]').click(); + $('[data-toggle="dropdown"]').parent().removeClass('open'); + } + + $('#ffInput_description').focus(); }); @@ -65,6 +74,7 @@ $(document).ready(function () { * and transfers. */ function selectsDifferentSource() { + console.log('Now in selectsDifferentSource()'); if (what === "deposit") { console.log('User is making a deposit. Don\'t bother with source.'); $('input[name="source_account_currency"]').val("0"); @@ -77,6 +87,7 @@ function selectsDifferentSource() { console.log('selectsDifferenctSource(): Set source account currency to ' + sourceCurrency); // change input thing: + console.log('Emulate click on .currency-option[data-id="' + sourceCurrency + '"]'); $('.currency-option[data-id="' + sourceCurrency + '"]').click(); $('[data-toggle="dropdown"]').parent().removeClass('open'); $('select[name="source_id"]').focus(); @@ -147,6 +158,7 @@ function updateLayout() { */ function updateForm() { "use strict"; + console.log('Now in updateForm()'); $('input[name="what"]').val(what); @@ -230,7 +242,6 @@ function updateForm() { break; } // get instructions all the time. - //updateNativeCurrency(useAccountCurrency); console.log('End of update form'); selectsDifferentSource(); selectsDifferentDestination(); diff --git a/resources/views/form/amount.twig b/resources/views/form/amount.twig index c28227d1f7..652f355148 100644 --- a/resources/views/form/amount.twig +++ b/resources/views/form/amount.twig @@ -28,6 +28,6 @@ {% include 'form/feedback' %} - - + +