Can now handle withdrawals in foreign currency.

This commit is contained in:
James Cole
2017-04-14 14:37:04 +02:00
parent 7e31a29b12
commit c33dd1ecee
9 changed files with 171 additions and 27 deletions

View File

@@ -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'] = '';
}

View File

@@ -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'));
}

View File

@@ -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:

View File

@@ -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();

View File

@@ -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);
}