This commit is contained in:
James Cole
2020-09-07 13:03:10 +02:00
parent a7af8d2195
commit ff6c0c26e0

View File

@@ -43,6 +43,7 @@ use Storage;
class PiggyBankRepository implements PiggyBankRepositoryInterface
{
use ModifiesPiggyBanks;
/** @var User */
private $user;
@@ -157,6 +158,7 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
Log::debug(sprintf('Now in getExactAmount(%d, %d, %d)', $piggyBank->id, $repetition->id, $journal->id));
$operator = null;
$currency = null;
/** @var JournalRepositoryInterface $journalRepost */
$journalRepost = app(JournalRepositoryInterface::class);
$journalRepost->setUser($this->user);
@@ -171,9 +173,9 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
Log::debug(sprintf('Piggy bank #%d currency is %s', $piggyBank->id, $piggyBankCurrency->code));
/** @var Transaction $source */
$source = $journal->transactions()->with(['Account'])->where('amount', '<', 0)->first();
$source = $journal->transactions()->with(['account'])->where('amount', '<', 0)->first();
/** @var Transaction $destination */
$destination = $journal->transactions()->with(['Account'])->where('amount', '>', 0)->first();
$destination = $journal->transactions()->with(['account'])->where('amount', '>', 0)->first();
// matches source, which means amount will be removed from piggy:
if ($source->account_id === $piggyBank->account_id) {
@@ -189,21 +191,23 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
Log::debug(sprintf('Currency will add money to piggy bank. Destination currency is %s', $currency->code));
}
if (null === $operator || null === $currency) {
Log::debug('Currency is NULL and operator is NULL, return "0".');
return '0';
}
// currency of the account + the piggy bank currency are almost the same.
// which amount from the transaction matches?
// $currency->id === $piggyBankCurrency->id
$amount = null;
if ($source->transaction_currency_id === $currency->id) {
if ((int)$source->transaction_currency_id === (int)$currency->id) {
Log::debug('Use normal amount');
$amount = app('steam')->$operator($source->amount);
}
if ($source->foreign_currency_id === $currency->id) {
if ((int)$source->foreign_currency_id === (int)$currency->id) {
Log::debug('Use foreign amount');
$amount = app('steam')->$operator($source->foreign_amount);
}
if (null === $amount) {
Log::debug('No match on currency, so amount remains null, return "0".');
return '0';
}
@@ -269,7 +273,6 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
}
/**
* Also add amount in name.
*