From 19774f32c2ead618a4bc93ad161fc256bba7b52c Mon Sep 17 00:00:00 2001 From: James Cole Date: Fri, 7 Jul 2017 17:51:14 +0200 Subject: [PATCH] First code for #658 --- .../Transaction/SingleController.php | 65 ++++++++++--------- app/Support/ExpandedForm.php | 13 ++++ public/js/ff/transactions/single/create.js | 17 +++-- resources/views/form/amount.twig | 2 +- .../views/transactions/single/create.twig | 4 ++ 5 files changed, 64 insertions(+), 37 deletions(-) diff --git a/app/Http/Controllers/Transaction/SingleController.php b/app/Http/Controllers/Transaction/SingleController.php index 71b5bc9430..8ce2cbae87 100644 --- a/app/Http/Controllers/Transaction/SingleController.php +++ b/app/Http/Controllers/Transaction/SingleController.php @@ -21,6 +21,7 @@ use FireflyIII\Helpers\Attachments\AttachmentHelperInterface; use FireflyIII\Http\Controllers\Controller; use FireflyIII\Http\Requests\JournalFormRequest; use FireflyIII\Models\AccountType; +use FireflyIII\Models\Transaction; use FireflyIII\Models\TransactionJournal; use FireflyIII\Models\TransactionType; use FireflyIII\Repositories\Account\AccountRepositoryInterface; @@ -86,39 +87,41 @@ class SingleController extends Controller public function cloneTransaction(TransactionJournal $journal) { - $source = $journal->sourceAccountList()->first(); - $destination = $journal->destinationAccountList()->first(); - $budget = $journal->budgets()->first(); - $budgetId = is_null($budget) ? 0 : $budget->id; - $category = $journal->categories()->first(); - $categoryName = is_null($category) ? '' : $category->name; - $tags = join(',', $journal->tags()->get()->pluck('tag')->toArray()); - $transaction = $journal->transactions()->first(); - $amount = Steam::positive($transaction->amount); - $foreignAmount = is_null($transaction->foreign_amount) ? null : Steam::positive($transaction->foreign_amount); + $source = $journal->sourceAccountList()->first(); + $destination = $journal->destinationAccountList()->first(); + $budget = $journal->budgets()->first(); + $budgetId = is_null($budget) ? 0 : $budget->id; + $category = $journal->categories()->first(); + $categoryName = is_null($category) ? '' : $category->name; + $tags = join(',', $journal->tags()->get()->pluck('tag')->toArray()); + /** @var Transaction $transaction */ + $transaction = $journal->transactions()->first(); + $amount = Steam::positive($transaction->amount); + $foreignAmount = is_null($transaction->foreign_amount) ? null : Steam::positive($transaction->foreign_amount); $preFilled = [ - 'description' => $journal->description, - 'source_account_id' => $source->id, - 'source_account_name' => $source->name, - 'destination_account_id' => $destination->id, - 'destination_account_name' => $destination->name, - 'amount' => $amount, - 'source_amount' => $amount, - 'destination_amount' => $foreignAmount, - 'foreign_amount' => $foreignAmount, - 'date' => $journal->date->format('Y-m-d'), - 'budget_id' => $budgetId, - 'category' => $categoryName, - 'tags' => $tags, - 'interest_date' => $journal->getMeta('interest_date'), - 'book_date' => $journal->getMeta('book_date'), - 'process_date' => $journal->getMeta('process_date'), - 'due_date' => $journal->getMeta('due_date'), - 'payment_date' => $journal->getMeta('payment_date'), - 'invoice_date' => $journal->getMeta('invoice_date'), - 'internal_reference' => $journal->getMeta('internal_reference'), - 'notes' => $journal->getMeta('notes'), + 'description' => $journal->description, + 'source_account_id' => $source->id, + 'source_account_name' => $source->name, + 'destination_account_id' => $destination->id, + 'destination_account_name' => $destination->name, + 'amount' => $amount, + 'source_amount' => $amount, + 'destination_amount' => $foreignAmount, + 'foreign_amount' => $foreignAmount, + 'amount_currency_id_amount' => $transaction->foreign_currency_id ?? 0, + 'date' => $journal->date->format('Y-m-d'), + 'budget_id' => $budgetId, + 'category' => $categoryName, + 'tags' => $tags, + 'interest_date' => $journal->getMeta('interest_date'), + 'book_date' => $journal->getMeta('book_date'), + 'process_date' => $journal->getMeta('process_date'), + 'due_date' => $journal->getMeta('due_date'), + 'payment_date' => $journal->getMeta('payment_date'), + 'invoice_date' => $journal->getMeta('invoice_date'), + 'internal_reference' => $journal->getMeta('internal_reference'), + 'notes' => $journal->getMeta('notes'), ]; Session::flash('preFilled', $preFilled); diff --git a/app/Support/ExpandedForm.php b/app/Support/ExpandedForm.php index dab2ed286d..c1dc2fc174 100644 --- a/app/Support/ExpandedForm.php +++ b/app/Support/ExpandedForm.php @@ -565,6 +565,19 @@ class ExpandedForm unset($options['currency']); unset($options['placeholder']); + // perhaps the currency has been sent to us in the field $amount_currency_id_$name (amount_currency_id_amount) + $preFilled = session('preFilled'); + $key = 'amount_currency_id_' . $name; + $sentCurrencyId = isset($preFilled[$key]) ? intval($preFilled[$key]) : $defaultCurrency->id; + + // find this currency in set of currencies: + foreach ($currencies as $currency) { + if ($currency->id === $sentCurrencyId) { + $defaultCurrency = $currency; + break; + } + } + // make sure value is formatted nicely: if (!is_null($value) && $value !== '') { $value = round($value, $defaultCurrency->decimal_places); diff --git a/public/js/ff/transactions/single/create.js b/public/js/ff/transactions/single/create.js index c98759b9b9..9f2ac22603 100644 --- a/public/js/ff/transactions/single/create.js +++ b/public/js/ff/transactions/single/create.js @@ -23,7 +23,6 @@ $(document).ready(function () { updateForm(); updateLayout(); updateDescription(); - updateNativeCurrency(); // when user changes source account or destination, native currency may be different. @@ -60,9 +59,15 @@ function getExchangeInstructions() { * There is an input that shows the currency symbol that is native to the selected * acccount. So when the user changes the selected account, the native currency is updated: */ -function updateNativeCurrency() { - var newAccountId = getAccountId(); - var nativeCurrencyId = accountInfo[newAccountId].preferredCurrency; +function updateNativeCurrency(useAccountCurrency) { + var nativeCurrencyId; + if (useAccountCurrency) { + var newAccountId = getAccountId(); + nativeCurrencyId = accountInfo[newAccountId].preferredCurrency; + } + if (!useAccountCurrency) { + nativeCurrencyId = overruleCurrency; + } $('.currency-option[data-id="' + nativeCurrencyId + '"]').click(); $('[data-toggle="dropdown"]').parent().removeClass('open'); @@ -180,7 +185,9 @@ function updateForm() { default: break; } - updateNativeCurrency(); + // get instructions all the time. + updateNativeCurrency(useAccountCurrency); + selectsForeignCurrency(); } /** diff --git a/resources/views/form/amount.twig b/resources/views/form/amount.twig index c28227d1f7..eaf1ec2d8f 100644 --- a/resources/views/form/amount.twig +++ b/resources/views/form/amount.twig @@ -7,7 +7,7 @@