Skip to content

Firefly III: a personal finances manager

FF3 will now correctly store exchanged / foreign amounts.

James Cole7e31a29b12b1
diff --git a/app/Http/Controllers/AccountController.php b/app/Http/Controllers/AccountController.php
index d11ca96249..82b078fc9f 100644
--- a/app/Http/Controllers/AccountController.php
+++ b/app/Http/Controllers/AccountController.php
@@ -92,7 +92,7 @@ class AccountController extends Controller
$request->session()->flash('gaEventCategory', 'accounts');
$request->session()->flash('gaEventAction', 'create-' . $what);
- return view('accounts.create', compact('subTitleIcon', 'what', 'subTitle', 'currencySelectList','allCurrencies', 'roles'));
+ return view('accounts.create', compact('subTitleIcon', 'what', 'subTitle', 'currencySelectList', 'allCurrencies', 'roles'));
}
diff --git a/app/Http/Requests/JournalFormRequest.php b/app/Http/Requests/JournalFormRequest.php
index 0a03746797..56d64b753f 100644
--- a/app/Http/Requests/JournalFormRequest.php
+++ b/app/Http/Requests/JournalFormRequest.php
@@ -67,6 +67,9 @@ class JournalFormRequest extends Request
'destination_account_name' => $this->string('destination_account_name'),
'piggy_bank_id' => $this->integer('piggy_bank_id'),
+ // amount for exchanged data:
+ 'exchanged_amount' => $this->float('exchanged_amount'),
+
];
return $data;
diff --git a/app/Repositories/Journal/JournalRepository.php b/app/Repositories/Journal/JournalRepository.php
index 2fd7811541..69a24cc128 100644
--- a/app/Repositories/Journal/JournalRepository.php
+++ b/app/Repositories/Journal/JournalRepository.php
@@ -41,7 +41,10 @@ class JournalRepository implements JournalRepositoryInterface
private $user;
/** @var array */
- private $validMetaFields = ['interest_date', 'book_date', 'process_date', 'due_date', 'payment_date', 'invoice_date', 'internal_reference', 'notes'];
+ private $validMetaFields
+ = ['interest_date', 'book_date', 'process_date', 'due_date', 'payment_date', 'invoice_date', 'internal_reference', 'notes', 'original_amount',
+ 'original_currency_id',
+ ];
/**
* @param TransactionJournal $journal
@@ -165,12 +168,34 @@ class JournalRepository implements JournalRepositoryInterface
public function store(array $data): TransactionJournal
{
// find transaction type.
+ /** @var TransactionType $transactionType */
$transactionType = TransactionType::where('type', ucfirst($data['what']))->first();
- $journal = new TransactionJournal(
+ $accounts = $this->storeAccounts($transactionType, $data);
+ $currencyId = $data['currency_id'];
+ $amount = strval($data['amount']);
+ // switch type to find what account to verify for currency stuff
+ switch ($transactionType->type) {
+ case TransactionType::WITHDRAWAL:
+ /*
+ * Overrule the currency selection and the amount:
+ */
+ $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;
+ }
+ break;
+ default:
+ throw new FireflyException(sprintf('Currency exchange routine cannot handle %s', $transactionType->type));
+ }
+
+ $journal = new TransactionJournal(
[
'user_id' => $this->user->id,
'transaction_type_id' => $transactionType->id,
- 'transaction_currency_id' => $data['currency_id'],
+ 'transaction_currency_id' => $currencyId,
'description' => $data['description'],
'completed' => 0,
'date' => $data['date'],
@@ -181,13 +206,13 @@ class JournalRepository implements JournalRepositoryInterface
// store stuff:
$this->storeCategoryWithJournal($journal, $data['category']);
$this->storeBudgetWithJournal($journal, $data['budget_id']);
- $accounts = $this->storeAccounts($transactionType, $data);
+
// store two transactions:
$one = [
'journal' => $journal,
'account' => $accounts['source'],
- 'amount' => bcmul(strval($data['amount']), '-1'),
+ 'amount' => bcmul($amount, '-1'),
'description' => null,
'category' => null,
'budget' => null,
@@ -198,7 +223,7 @@ class JournalRepository implements JournalRepositoryInterface
$two = [
'journal' => $journal,
'account' => $accounts['destination'],
- 'amount' => $data['amount'],
+ 'amount' => $amount,
'description' => null,
'category' => null,
'budget' => null,
diff --git a/app/Support/ExpandedForm.php b/app/Support/ExpandedForm.php
index 180e2b9923..bbaa657a4e 100644
--- a/app/Support/ExpandedForm.php
+++ b/app/Support/ExpandedForm.php
@@ -275,7 +275,7 @@ class ExpandedForm
$classes = $this->getHolderClasses($name);
$value = $this->fillFieldValue($name, $value);
$options['step'] = 'any';
- $options['min'] = '0.01';
+ $options['min'] = '0.01';
$selectedCurrency = isset($options['currency']) ? $options['currency'] : Amt::getDefaultCurrency();
unset($options['currency']);
unset($options['placeholder']);
@@ -312,7 +312,8 @@ class ExpandedForm
// make sure value is formatted nicely:
if (!is_null($value) && $value !== '') {
- $value = round($value, $selectedCurrency->decimal_places);
+ $decimals = $selectedCurrency->decimal_places ?? 2;
+ $value = round($value, $decimals);
}