Skip to content

Firefly III: a personal finances manager

First code for #658

James Cole19774f32c2ea
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 @@
<button type="button"
class="btn btn-default dropdown-toggle currency-dropdown" id="currency_dropdown_{{ name }}" data-toggle="dropdown"
aria-expanded="false">
- <span id="currency_select_symbol_{{ name }}">{{ defaultCurrency.symbol|raw }}</span> <span class="caret"></span>
+ <span id="currency_select_symbol_{{ name }}">{{ defaultCurrency.symbol|raw }}X</span> <span class="caret"></span>
</button>
<ul class="dropdown-menu currency-dropdown-menu" role="menu">
{% for currency in currencies %}
diff --git a/resources/views/transactions/single/create.twig b/resources/views/transactions/single/create.twig
index 0dfafe82d9..43d74ab252 100644
--- a/resources/views/transactions/single/create.twig
+++ b/resources/views/transactions/single/create.twig
@@ -231,6 +231,10 @@
button['{{ type }}'] = '{{ trans('form.store_new_' ~ type) }}';
{% endfor %}
+ // some code for the foreign amount logic:
+ var useAccountCurrency = {% if preFilled.amount_currency_id_amount > 0 %}false{% else %}true{% endif %};
+ var overruleCurrency = {{ preFilled.amount_currency_id_amount|default(0) }};
+
</script>
<script type="text/javascript" src="js/lib/bootstrap3-typeahead.min.js"></script>
<script type="text/javascript" src="js/lib/bootstrap-tagsinput.min.js"></script>