Fix issues with #1521

This commit is contained in:
James Cole 2018-07-02 15:37:56 +02:00
parent f62fd18b72
commit 7629dfd54a
7 changed files with 101 additions and 7 deletions

View File

@ -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;
}

View File

@ -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;
}
}
}

View File

@ -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;
}
}

View File

@ -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 <A> 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:

View File

@ -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("");
}

View File

@ -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();

View File

@ -28,6 +28,6 @@
{% include 'form/feedback' %}
</div>
<input type="hidden" name="amount_currency_id_{{ name }}" value="{{ defaultCurrency.id }}"/>
<!-- Going to put in this value: {{ defaultCurrency.id }} -->
<input type="hidden" name="amount_currency_id_{{ name }}" value="{{ defaultCurrency.id }}" data-backup="{{ defaultCurrency.id }}"/>
</div>