mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-16 18:25:00 -06:00
Can now handle withdrawals in foreign currency.
This commit is contained in:
parent
7e31a29b12
commit
c33dd1ecee
@ -25,6 +25,7 @@ use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Models\TransactionType;
|
||||
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
||||
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
|
||||
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
|
||||
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
|
||||
use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface;
|
||||
use Log;
|
||||
@ -48,7 +49,8 @@ class SingleController extends Controller
|
||||
|
||||
/** @var BudgetRepositoryInterface */
|
||||
private $budgets;
|
||||
|
||||
/** @var CurrencyRepositoryInterface */
|
||||
private $currency;
|
||||
/** @var PiggyBankRepositoryInterface */
|
||||
private $piggyBanks;
|
||||
|
||||
@ -71,6 +73,7 @@ class SingleController extends Controller
|
||||
$this->budgets = app(BudgetRepositoryInterface::class);
|
||||
$this->piggyBanks = app(PiggyBankRepositoryInterface::class);
|
||||
$this->attachments = app(AttachmentHelperInterface::class);
|
||||
$this->currency = app(CurrencyRepositoryInterface::class);
|
||||
|
||||
View::share('title', trans('firefly.transactions'));
|
||||
View::share('mainTitleIcon', 'fa-repeat');
|
||||
@ -231,7 +234,6 @@ class SingleController extends Controller
|
||||
// view related code
|
||||
$subTitle = trans('breadcrumbs.edit_journal', ['description' => $journal->description]);
|
||||
|
||||
|
||||
// journal related code
|
||||
$sourceAccounts = $journal->sourceAccountList();
|
||||
$destinationAccounts = $journal->destinationAccountList();
|
||||
@ -249,6 +251,7 @@ class SingleController extends Controller
|
||||
'destination_account_id' => $destinationAccounts->first()->id,
|
||||
'destination_account_name' => $destinationAccounts->first()->edit_name,
|
||||
'amount' => $journal->amountPositive(),
|
||||
'currency' => $journal->transactionCurrency,
|
||||
|
||||
// new custom fields:
|
||||
'due_date' => $journal->dateAsString('due_date'),
|
||||
@ -256,8 +259,20 @@ class SingleController extends Controller
|
||||
'invoice_date' => $journal->dateAsString('invoice_date'),
|
||||
'interal_reference' => $journal->getMeta('internal_reference'),
|
||||
'notes' => $journal->getMeta('notes'),
|
||||
|
||||
// exchange rate fields
|
||||
'exchanged_amount' => $journal->amountPositive(),
|
||||
'exchanged_currency' => $journal->transactionCurrency,
|
||||
];
|
||||
|
||||
// catch possibly exchanged currencies and what-not.
|
||||
$originalCurrencyId = intval($journal->getMeta('original_currency_id'));
|
||||
if ($originalCurrencyId > 0) {
|
||||
// update some fields in pre-filled.
|
||||
$preFilled['amount'] = $journal->getMeta('original_amount');
|
||||
$preFilled['currency'] = $this->currency->find(intval($journal->getMeta('original_currency_id')));
|
||||
}
|
||||
|
||||
if ($journal->isWithdrawal() && $destinationAccounts->first()->accountType->type == AccountType::CASH) {
|
||||
$preFilled['destination_account_name'] = '';
|
||||
}
|
||||
|
@ -17,6 +17,7 @@ use Carbon\Carbon;
|
||||
use FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\Helpers\Collector\JournalCollectorInterface;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
|
||||
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
|
||||
use FireflyIII\Repositories\Journal\JournalTaskerInterface;
|
||||
use FireflyIII\Support\CacheProperties;
|
||||
@ -177,12 +178,19 @@ class TransactionController extends Controller
|
||||
return $this->redirectToAccount($journal);
|
||||
}
|
||||
|
||||
$events = $tasker->getPiggyBankEvents($journal);
|
||||
$transactions = $tasker->getTransactionsOverview($journal);
|
||||
$what = strtolower($journal->transaction_type_type ?? $journal->transactionType->type);
|
||||
$subTitle = trans('firefly.' . $what) . ' "' . e($journal->description) . '"';
|
||||
$events = $tasker->getPiggyBankEvents($journal);
|
||||
$transactions = $tasker->getTransactionsOverview($journal);
|
||||
$what = strtolower($journal->transaction_type_type ?? $journal->transactionType->type);
|
||||
$subTitle = trans('firefly.' . $what) . ' "' . e($journal->description) . '"';
|
||||
$originalCurrency = null;
|
||||
|
||||
return view('transactions.show', compact('journal', 'events', 'subTitle', 'what', 'transactions'));
|
||||
if ($journal->hasMeta('original_currency_id')) {
|
||||
/** @var CurrencyRepositoryInterface $repository */
|
||||
$repository = app(CurrencyRepositoryInterface::class);
|
||||
$originalCurrency = $repository->find(intval($journal->hasMeta('original_currency_id')));
|
||||
}
|
||||
|
||||
return view('transactions.show', compact('journal', 'events', 'subTitle', 'what', 'transactions', 'originalCurrency'));
|
||||
|
||||
|
||||
}
|
||||
|
@ -104,6 +104,9 @@ class JournalFormRequest extends Request
|
||||
'destination_account_id' => 'numeric|belongsToUser:accounts,id',
|
||||
'destination_account_name' => 'between:1,255',
|
||||
'piggy_bank_id' => 'between:1,255',
|
||||
|
||||
// exchange rate data:
|
||||
'exchanged_amount' => 'numeric|required|more:0',
|
||||
];
|
||||
|
||||
// some rules get an upgrade depending on the type of data:
|
||||
|
@ -181,10 +181,10 @@ class JournalRepository implements JournalRepositoryInterface
|
||||
*/
|
||||
$accountCurrencyId = intval($accounts['source']->getMeta('currency_id'));
|
||||
if ($accountCurrencyId !== $currencyId) {
|
||||
$currencyId = $accountCurrencyId;
|
||||
$amount = strval($data['exchanged_amount']);
|
||||
$data['original_amount'] = $data['amount'];
|
||||
$data['original_currency_id'] = $currencyId;
|
||||
$currencyId = $accountCurrencyId;
|
||||
$amount = strval($data['exchanged_amount']);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
@ -261,10 +261,22 @@ class JournalRepository implements JournalRepositoryInterface
|
||||
*/
|
||||
public function update(TransactionJournal $journal, array $data): TransactionJournal
|
||||
{
|
||||
|
||||
// update actual journal:
|
||||
$journal->transaction_currency_id = $data['currency_id'];
|
||||
$journal->description = $data['description'];
|
||||
$journal->date = $data['date'];
|
||||
$journal->description = $data['description'];
|
||||
$journal->date = $data['date'];
|
||||
$accounts = $this->storeAccounts($journal->transactionType, $data);
|
||||
$amount = strval($data['amount']);
|
||||
|
||||
if ($data['currency_id'] !== $journal->transaction_currency_id) {
|
||||
// user has entered amount in foreign currency.
|
||||
// amount in "our" currency is $data['exchanged_amount']:
|
||||
$amount = strval($data['exchanged_amount']);
|
||||
// other values must be stored as well:
|
||||
$data['original_amount'] = $data['amount'];
|
||||
$data['original_currency_id'] = $data['currency_id'];
|
||||
|
||||
}
|
||||
|
||||
// unlink all categories, recreate them:
|
||||
$journal->categories()->detach();
|
||||
@ -272,12 +284,9 @@ class JournalRepository implements JournalRepositoryInterface
|
||||
|
||||
$this->storeCategoryWithJournal($journal, $data['category']);
|
||||
$this->storeBudgetWithJournal($journal, $data['budget_id']);
|
||||
$accounts = $this->storeAccounts($journal->transactionType, $data);
|
||||
|
||||
$sourceAmount = bcmul(strval($data['amount']), '-1');
|
||||
$this->updateSourceTransaction($journal, $accounts['source'], $sourceAmount); // negative because source loses money.
|
||||
|
||||
$amount = strval($data['amount']);
|
||||
$this->updateSourceTransaction($journal, $accounts['source'], bcmul($amount, '-1')); // negative because source loses money.
|
||||
$this->updateDestinationTransaction($journal, $accounts['destination'], $amount); // positive because destination gets money.
|
||||
|
||||
$journal->save();
|
||||
|
@ -40,6 +40,8 @@ class ExpandedForm
|
||||
*/
|
||||
public function amount(string $name, $value = null, array $options = []): string
|
||||
{
|
||||
$options['min'] = '0.01';
|
||||
|
||||
return $this->currencyField($name, 'amount', $value, $options);
|
||||
}
|
||||
|
||||
@ -52,6 +54,8 @@ class ExpandedForm
|
||||
*/
|
||||
public function amountSmall(string $name, $value = null, array $options = []): string
|
||||
{
|
||||
$options['min'] = '0.01';
|
||||
|
||||
return $this->currencyField($name, 'amount-small', $value, $options);
|
||||
}
|
||||
|
||||
|
@ -43,7 +43,7 @@ $(document).ready(function () {
|
||||
});
|
||||
|
||||
function getExchangeRate() {
|
||||
var accountId = $('select[name="source_account_id"]').val();
|
||||
var accountId = getAccountId();
|
||||
var selectedCurrencyId = parseInt($('input[name="amount_currency_id_amount"]').val());
|
||||
var accountCurrencyId = parseInt(accountInfo[accountId].preferredCurrency);
|
||||
var selectedCurrencyCode = currencyInfo[selectedCurrencyId].code;
|
||||
@ -53,7 +53,6 @@ function getExchangeRate() {
|
||||
var uri = 'json/rate/' + selectedCurrencyCode + '/' + accountCurrencyCode + '/' + date + '?amount=' + amount;
|
||||
console.log('Will grab ' + uri);
|
||||
$.get(uri).done(updateExchangedAmount);
|
||||
|
||||
}
|
||||
|
||||
function updateExchangedAmount(data) {
|
||||
@ -65,7 +64,7 @@ function updateExchangedAmount(data) {
|
||||
|
||||
function triggerCurrencyChange() {
|
||||
var selectedCurrencyId = parseInt($('input[name="amount_currency_id_amount"]').val());
|
||||
var accountId = $('select[name="source_account_id"]').val();
|
||||
var accountId = getAccountId();
|
||||
var accountCurrencyId = parseInt(accountInfo[accountId].preferredCurrency);
|
||||
console.log('Selected currency is ' + selectedCurrencyId);
|
||||
console.log('Account prefers ' + accountCurrencyId);
|
||||
@ -93,7 +92,7 @@ function triggerCurrencyChange() {
|
||||
|
||||
function updateCurrency() {
|
||||
// get value:
|
||||
var accountId = $('select[name="source_account_id"]').val();
|
||||
var accountId = getAccountId();
|
||||
var currencyPreference = accountInfo[accountId].preferredCurrency;
|
||||
|
||||
$('.currency-option[data-id="' + currencyPreference + '"]').click();
|
||||
@ -253,4 +252,14 @@ function clickButton(e) {
|
||||
updateDescription();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get accountID based on some meta info.
|
||||
*/
|
||||
function getAccountId() {
|
||||
if(what === "withdrawal") {
|
||||
return $('select[name="source_account_id"]').val();
|
||||
}
|
||||
alert('Cannot handle ' + what);
|
||||
}
|
@ -59,4 +59,81 @@ $(document).ready(function () {
|
||||
$('input[name="category"]').typeahead({source: data});
|
||||
});
|
||||
|
||||
$('.currency-option').on('click', triggerCurrencyChange);
|
||||
|
||||
// always update the exchanged_amount to match the correct currency
|
||||
var journalCurrency = currencyInfo[journal.transaction_currency_id].symbol;
|
||||
$('.non-selectable-currency-symbol').text(journalCurrency);
|
||||
|
||||
// hide the exchange amount / foreign things:
|
||||
if (journal.transaction_currency_id === journalData.currency.id) {
|
||||
$('#exchange_rate_instruction_holder').hide();
|
||||
$('#exchanged_amount_holder').hide();
|
||||
}
|
||||
|
||||
// or update the related text.
|
||||
if (journal.transaction_currency_id !== journalData.currency.id) {
|
||||
// update info text:
|
||||
var accountId = getAccountId();
|
||||
var text = exchangeRateInstructions.replace('@name', accountInfo[accountId].name);
|
||||
text = text.replace(/@account_currency/g, currencyInfo[journal.transaction_currency_id].name);
|
||||
text = text.replace(/@transaction_currency/g, currencyInfo[journalData.currency.id].name);
|
||||
$('#ffInput_exchange_rate_instruction').text(text);
|
||||
}
|
||||
});
|
||||
|
||||
function triggerCurrencyChange() {
|
||||
var selectedCurrencyId = parseInt($('input[name="amount_currency_id_amount"]').val());
|
||||
var accountId = getAccountId();
|
||||
var accountCurrencyId = parseInt(accountInfo[accountId].preferredCurrency);
|
||||
console.log('Selected currency is ' + selectedCurrencyId);
|
||||
console.log('Account prefers ' + accountCurrencyId);
|
||||
if (selectedCurrencyId !== accountCurrencyId) {
|
||||
var text = exchangeRateInstructions.replace('@name', accountInfo[accountId].name);
|
||||
text = text.replace(/@account_currency/g, currencyInfo[accountCurrencyId].name);
|
||||
text = text.replace(/@transaction_currency/g, currencyInfo[selectedCurrencyId].name);
|
||||
$('.non-selectable-currency-symbol').text(currencyInfo[accountCurrencyId].symbol);
|
||||
getExchangeRate();
|
||||
|
||||
$('#ffInput_exchange_rate_instruction').text(text);
|
||||
$('#exchange_rate_instruction_holder').show();
|
||||
$('#exchanged_amount_holder').show();
|
||||
}
|
||||
if (selectedCurrencyId === accountCurrencyId) {
|
||||
$('#exchange_rate_instruction_holder').hide();
|
||||
$('#exchanged_amount_holder').hide();
|
||||
}
|
||||
|
||||
// if the value of the selected currency does not match the account's currency
|
||||
// show the exchange rate thing!
|
||||
return false;
|
||||
}
|
||||
|
||||
function getExchangeRate() {
|
||||
var accountId = getAccountId();
|
||||
var selectedCurrencyId = parseInt($('input[name="amount_currency_id_amount"]').val());
|
||||
var accountCurrencyId = parseInt(accountInfo[accountId].preferredCurrency);
|
||||
var selectedCurrencyCode = currencyInfo[selectedCurrencyId].code;
|
||||
var accountCurrencyCode = currencyInfo[accountCurrencyId].code;
|
||||
var date = $('#ffInput_date').val();
|
||||
var amount = $('#ffInput_amount').val();
|
||||
var uri = 'json/rate/' + selectedCurrencyCode + '/' + accountCurrencyCode + '/' + date + '?amount=' + amount;
|
||||
console.log('Will grab ' + uri);
|
||||
$.get(uri).done(updateExchangedAmount);
|
||||
}
|
||||
|
||||
function updateExchangedAmount(data) {
|
||||
console.log('Returned data:');
|
||||
console.log(data);
|
||||
$('#ffInput_exchanged_amount').val(data.amount);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get accountID based on some meta info.
|
||||
*/
|
||||
function getAccountId() {
|
||||
if(journal.transaction_type.type === "Withdrawal") {
|
||||
return $('select[name="source_account_id"]').val();
|
||||
}
|
||||
alert('Cannot handle ' + journal.transaction_type.type);
|
||||
}
|
||||
|
@ -36,7 +36,15 @@
|
||||
<!-- total amount -->
|
||||
<tr>
|
||||
<td>{{ 'total_amount'|_ }}</td>
|
||||
<td>{{ journal|formatJournal }}</td>
|
||||
<td>{{ journal|formatJournal }}
|
||||
{% if journal.hasMeta('original_amount') %}
|
||||
{% if journal.transactiontype.type == 'Withdrawal' %}
|
||||
({{ formatAnything(originalCurrency, journal.getMeta('original_amount')*-1) }})
|
||||
{% else %}
|
||||
({{ formatAnything(originalCurrency, journal.getMeta('original_amount')) }})
|
||||
{% endif %}
|
||||
{% endif %}
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td style="width:30%;">{{ trans('list.date') }}</td>
|
||||
|
@ -46,24 +46,28 @@
|
||||
{{ ExpandedForm.text('source_account_name',data.source_account_name, {label: trans('form.revenue_account')}) }}
|
||||
{% endif %}
|
||||
|
||||
<!-- FREE FORMAT DESTINATION ACCOUNT ONLY FOR EXPENSES -->
|
||||
{# FREE FORMAT DESTINATION ACCOUNT ONLY FOR EXPENSES #}
|
||||
{% if what == 'withdrawal' %}
|
||||
{{ ExpandedForm.text('destination_account_name',data.destination_account_name, {label: trans('form.expense_account')}) }}
|
||||
{% endif %}
|
||||
|
||||
<!-- SELECTABLE DESTINATION ACCOUNT ONLY FOR TRANSFERS AND DEPOSITS -->
|
||||
{# SELECTABLE DESTINATION ACCOUNT ONLY FOR TRANSFERS AND DEPOSITS #}
|
||||
{% if what == 'transfer' or what == 'deposit' %}
|
||||
{{ ExpandedForm.select('destination_account_id',assetAccounts, data.destination_account_id, {label: trans('form.asset_destination_account')} ) }}
|
||||
{% endif %}
|
||||
|
||||
<!-- ALWAYS SHOW AMOUNT -->
|
||||
{{ ExpandedForm.amount('amount',data.amount,{'currency' : journal.transactionCurrency}) }}
|
||||
{# ALWAYS SHOW AMOUNT #}
|
||||
{{ ExpandedForm.amount('amount',data.amount,{'currency' : data.currency}) }}
|
||||
|
||||
<!-- ALWAYS SHOW DATE -->
|
||||
{# INSTRUCTIONS FOR EXCHANGE RATES #}
|
||||
{{ ExpandedForm.staticText('exchange_rate_instruction','(here be text)') }}
|
||||
|
||||
{{ ExpandedForm.nonSelectableAmount('exchanged_amount') }}
|
||||
|
||||
{# ALWAYS SHOW DATE #}
|
||||
{{ ExpandedForm.date('date',data['date']) }}
|
||||
</div>
|
||||
</div>
|
||||
<!-- close panel -->
|
||||
|
||||
</div>
|
||||
<div class="col-lg-6 col-md-12 col-sm-12">
|
||||
@ -235,6 +239,13 @@
|
||||
<script type="text/javascript" src="js/lib/bootstrap-tagsinput.min.js"></script>
|
||||
<script type="text/javascript" src="js/lib/jquery-ui.min.js"></script>
|
||||
<script type="text/javascript" src="js/lib/modernizr-custom.js"></script>
|
||||
<script type="text/javascript" src="javascript/accounts?ext=.js"></script>
|
||||
<script type="text/javascript" src="javascript/currencies?ext=.js"></script>
|
||||
<script type="text/javascript">
|
||||
var journal = {{ journal.toArray()|json_encode|raw }};
|
||||
var journalData = {{ data|json_encode|raw }};
|
||||
var exchangeRateInstructions = "{{ 'exchange_rate_instructions'|_|escape('js') }}";
|
||||
</script>
|
||||
<script type="text/javascript" src="js/ff/transactions/single/edit.js"></script>
|
||||
|
||||
{% endblock %}
|
||||
|
Loading…
Reference in New Issue
Block a user