diff --git a/app/Http/Controllers/Transaction/SplitController.php b/app/Http/Controllers/Transaction/SplitController.php index 3229d73ac9..6910c9ec72 100644 --- a/app/Http/Controllers/Transaction/SplitController.php +++ b/app/Http/Controllers/Transaction/SplitController.php @@ -189,7 +189,8 @@ class SplitController extends Controller $destinationAccounts = $this->repository->getJournalDestinationAccounts($journal); $array = [ 'journal_description' => $request->old('journal_description', $journal->description), - 'journal_amount' => $this->repository->getJournalTotal($journal), + 'journal_amount' => '0', + 'journal_foreign_amount' => '0', 'sourceAccounts' => $sourceAccounts, 'journal_source_account_id' => $request->old('journal_source_account_id', $sourceAccounts->first()->id), 'journal_source_account_name' => $request->old('journal_source_account_name', $sourceAccounts->first()->name), @@ -213,9 +214,12 @@ class SplitController extends Controller 'transactions' => $this->getTransactionDataFromJournal($journal), ]; // update transactions array with old request data. - $array['transactions'] = $this->updateWithPrevious($array['transactions'], $request->old()); + // update journal amount and foreign amount: + $array['journal_amount'] = array_sum(array_column($array['transactions'], 'amount')); + $array['journal_foreign_amount'] = array_sum(array_column($array['transactions'], 'foreign_amount')); + return $array; } diff --git a/public/js/ff/transactions/split/edit.js b/public/js/ff/transactions/split/edit.js index 97b4c1f88f..9b5aae1e99 100644 --- a/public/js/ff/transactions/split/edit.js +++ b/public/js/ff/transactions/split/edit.js @@ -19,7 +19,7 @@ */ -/** global: originalSum, accounting, what, Modernizr, currencySymbol */ +/** global: originalSum,originalForeignSum, accounting, what, Modernizr, currencySymbol, foreignCurrencySymbol */ var destAccounts = {}; var srcAccounts = {}; @@ -69,7 +69,8 @@ $(document).ready(function () { }); - $('input[name$="][amount]"]').on('input', calculateSum); + $('input[name$="][amount]"]').on('change', calculateBothSums); + $('input[name$="][foreign_amount]"]').on('change', calculateBothSums); if (!Modernizr.inputtypes.date) { $('input[type="date"]').datepicker( @@ -80,6 +81,12 @@ $(document).ready(function () { } }); +function calculateBothSums() { + console.log("Now in calculateBothSums()"); + calculateSum(); + calculateForeignSum(); +} + /** * New and cool * @param e @@ -113,8 +120,8 @@ function cloneDivRow() { source.removeClass('initial-row'); source.find('.count').text('#' + count); - source.find('input[name$="][amount]"]').val("").on('input', calculateSum); - source.find('input[name$="][foreign_amount]"]').val("").on('input', calculateSum); + source.find('input[name$="][amount]"]').val("").on('change', calculateBothSums); + source.find('input[name$="][foreign_amount]"]').val("").on('change', calculateBothSums); if (destAccounts.length > 0) { source.find('input[name$="destination_account_name]"]').typeahead({source: destAccounts, autoSelect: false}); } @@ -134,7 +141,7 @@ function cloneDivRow() { // remove original click things, add them again: $('.remove-current-split').unbind('click').click(removeDivRow); - calculateSum(); + calculateBothSums(); resetDivSplits(); return false; @@ -166,16 +173,10 @@ function resetDivSplits() { $.each($('.remove-current-split'), function (i, v) { var button = $(v); button.attr('data-split', i); - button.find('i').attr('data-split', i); + button.find('span').text(' #' + (i + 1)); }); - // loop each indicator (#) and update it: - $.each($('td.count'), function (i, v) { - var cell = $(v); - var index = i + 1; - cell.text('#' + index); - }); // loop each possible field. @@ -234,6 +235,7 @@ function resetDivSplits() { function calculateSum() { "use strict"; + console.log("Now in calculateSum()"); var left = originalSum * -1; var sum = 0; var set = $('input[name$="][amount]"]'); @@ -245,14 +247,43 @@ function calculateSum() { sum = Math.round(sum * 100) / 100; left = Math.round(left * 100) / 100; + console.log("Sum is " + sum + ", left is " + left); $('.amount-warning').remove(); if (sum !== originalSum) { - var holder = $('#journal_amount_holder'); - var par = holder.find('p.form-control-static'); - $('').text(' (' + accounting.formatMoney(sum, currencySymbol) + ')').addClass('text-danger amount-warning').appendTo(par); + console.log("Is different from original sum " + originalSum); + var paragraph = $('#journal_amount_holder').find('p.form-control-static'); + + $('').text(' (' + accounting.formatMoney(sum, currencySymbol) + ')').addClass('text-danger amount-warning').appendTo(paragraph); + // also add what's left to divide (or vice versa) - $('').text(' (' + accounting.formatMoney(left, currencySymbol) + ')').addClass('text-danger amount-warning').appendTo(par); + $('').text(' (' + accounting.formatMoney(left, currencySymbol) + ')').addClass('text-danger amount-warning').appendTo(paragraph); + } + +} + + +function calculateForeignSum() { + // "use strict"; + var left = originalForeignSum * -1; + var sum = 0; + var set = $('input[name$="][foreign_amount]"]'); + for (var i = 0; i < set.length; i++) { + var current = $(set[i]); + sum += (current.val() === "" ? 0 : parseFloat(current.val())); + left += (current.val() === "" ? 0 : parseFloat(current.val())); + } + sum = Math.round(sum * 100) / 100; + left = Math.round(left * 100) / 100; + + + $('.amount-warning-foreign').remove(); + if (sum !== originalForeignSum) { + var paragraph = $('#journal_foreign_amount_holder').find('p.form-control-static'); + $('').text(' (' + accounting.formatMoney(sum, foreignCurrencySymbol) + ')').addClass('text-danger amount-warning-foreign').appendTo(paragraph); + + // also add what's left to divide (or vice versa) + $('').text(' (' + accounting.formatMoney(left, foreignCurrencySymbol) + ')').addClass('text-danger amount-warning-foreign').appendTo(paragraph); } } \ No newline at end of file diff --git a/resources/lang/en_US/form.php b/resources/lang/en_US/form.php index e763ae382d..9904018181 100644 --- a/resources/lang/en_US/form.php +++ b/resources/lang/en_US/form.php @@ -94,6 +94,7 @@ return [ 'convert_Transfer' => 'Convert transfer', 'amount' => 'Amount', + 'foreign_amount' => 'Foreign amount', 'date' => 'Date', 'interest_date' => 'Interest date', 'book_date' => 'Book date', diff --git a/resources/views/transactions/split/edit.twig b/resources/views/transactions/split/edit.twig index dc5dbb9e60..b4dd2ad247 100644 --- a/resources/views/transactions/split/edit.twig +++ b/resources/views/transactions/split/edit.twig @@ -50,18 +50,29 @@ {{ ExpandedForm.assetAccountList('journal_destination_account_id', preFilled.journal_destination_account_id) }} {% endif %} - {# TOTAL AMOUNT IS STATIC TEXT #} - {% if preFilled.what == 'withdrawal' or preFilled.what == 'transfer' %} - {{ ExpandedForm.staticText('journal_amount', formatAmountByAccount(accountArray[preFilled.journal_source_account_id], preFilled.journal_amount, true) ) }} - - {% endif %} - - {% if preFilled.what == 'deposit' %} - {{ ExpandedForm.staticText('journal_amount', formatAmountByAccount(accountArray[preFilled.journal_destination_account_id], preFilled.journal_amount, true) ) }} - - {% endif %} - + {# show amount and some helper text when making splits: #} + {# amount #} +
+ +
+

+ {{ formatAmountBySymbol(preFilled.journal_amount, preFilled.transactions[0].currency_symbol, preFilled.transactions[0].currency_dp) }} +

+
+
+ {# foreign amount, if not zero. #} + {% if preFilled.journal_foreign_amount != 0 %} +
+ +
+

+ {{ formatAmountBySymbol(preFilled.journal_foreign_amount, preFilled.transactions[0].foreign_currency_symbol, preFilled.transactions[0].foreign_currency_dp) }} +

+
+
+ {% endif %} + {# DATE #} {{ ExpandedForm.date('date', journal.date) }} @@ -207,101 +218,83 @@ {% for index, transaction in preFilled.transactions %} -
- {# button #} - +
+ {# button #} + - {# description #} -
- -
- - {# destination for withdrawals: #} - {% if preFilled.what == 'withdrawal' %} -
- +
- {% endif %} - {# source for deposits #} - {% if preFilled.what == 'deposit' %} -
- -
- {% endif %} - - {# amount#} -
- {{ transaction.currency_symbol }}  - {% if transaction.foreign_amount != null %} - {{ transaction.foreign_currency_symbol }}  - - {% endif %} - -
- {# - {% if transaction.foreign_amount != null %} -
- {% else %} -
- {% endif %} - - - {% if transaction.foreign_amount != null %} -
-
-
{{ transaction.foreign_currency_symbol }}
- + {# destination for withdrawals: #} + {% if preFilled.what == 'withdrawal' %} +
+
- + {% endif %} + {# source for deposits #} + {% if preFilled.what == 'deposit' %} +
+ +
+ {% endif %} + + {# amount#} +
+ {{ transaction.currency_symbol }}  + {% if transaction.foreign_amount != null %} + {{ transaction.foreign_currency_symbol }}  + + {% endif %} +
- {% endif %} - #} - {# budget #} - {% if preFilled.what == 'withdrawal' %} -
- -
- {% endif %} + {# budget #} + {% if preFilled.what == 'withdrawal' %} +
+ +
+ {% endif %} - {# category #} -
- -
+ {# category #} +
+ +
+
+ {% endfor %}
- {% endfor %} +

+
+ {{ 'add_another_split'|_ }} +

-

-
- {{ 'add_another_split'|_ }} -

-
{# panel for options #} @@ -329,14 +322,10 @@ {% block scripts %}