mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-16 17:33:45 +00:00
Fix #3745
This commit is contained in:
@@ -43,6 +43,7 @@ use Storage;
|
|||||||
class PiggyBankRepository implements PiggyBankRepositoryInterface
|
class PiggyBankRepository implements PiggyBankRepositoryInterface
|
||||||
{
|
{
|
||||||
use ModifiesPiggyBanks;
|
use ModifiesPiggyBanks;
|
||||||
|
|
||||||
/** @var User */
|
/** @var User */
|
||||||
private $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));
|
Log::debug(sprintf('Now in getExactAmount(%d, %d, %d)', $piggyBank->id, $repetition->id, $journal->id));
|
||||||
|
|
||||||
$operator = null;
|
$operator = null;
|
||||||
|
$currency = null;
|
||||||
/** @var JournalRepositoryInterface $journalRepost */
|
/** @var JournalRepositoryInterface $journalRepost */
|
||||||
$journalRepost = app(JournalRepositoryInterface::class);
|
$journalRepost = app(JournalRepositoryInterface::class);
|
||||||
$journalRepost->setUser($this->user);
|
$journalRepost->setUser($this->user);
|
||||||
@@ -165,15 +167,15 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
|
|||||||
$accountRepos = app(AccountRepositoryInterface::class);
|
$accountRepos = app(AccountRepositoryInterface::class);
|
||||||
$accountRepos->setUser($this->user);
|
$accountRepos->setUser($this->user);
|
||||||
|
|
||||||
$defaultCurrency = app('amount')->getDefaultCurrencyByUser($this->user);
|
$defaultCurrency = app('amount')->getDefaultCurrencyByUser($this->user);
|
||||||
$piggyBankCurrency = $accountRepos->getAccountCurrency($piggyBank->account) ?? $defaultCurrency;
|
$piggyBankCurrency = $accountRepos->getAccountCurrency($piggyBank->account) ?? $defaultCurrency;
|
||||||
|
|
||||||
Log::debug(sprintf('Piggy bank #%d currency is %s', $piggyBank->id, $piggyBankCurrency->code));
|
Log::debug(sprintf('Piggy bank #%d currency is %s', $piggyBank->id, $piggyBankCurrency->code));
|
||||||
|
|
||||||
/** @var Transaction $source */
|
/** @var Transaction $source */
|
||||||
$source = $journal->transactions()->with(['Account'])->where('amount', '<', 0)->first();
|
$source = $journal->transactions()->with(['account'])->where('amount', '<', 0)->first();
|
||||||
/** @var Transaction $destination */
|
/** @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:
|
// matches source, which means amount will be removed from piggy:
|
||||||
if ($source->account_id === $piggyBank->account_id) {
|
if ($source->account_id === $piggyBank->account_id) {
|
||||||
@@ -189,28 +191,30 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
|
|||||||
Log::debug(sprintf('Currency will add money to piggy bank. Destination currency is %s', $currency->code));
|
Log::debug(sprintf('Currency will add money to piggy bank. Destination currency is %s', $currency->code));
|
||||||
}
|
}
|
||||||
if (null === $operator || null === $currency) {
|
if (null === $operator || null === $currency) {
|
||||||
|
Log::debug('Currency is NULL and operator is NULL, return "0".');
|
||||||
return '0';
|
return '0';
|
||||||
}
|
}
|
||||||
// currency of the account + the piggy bank currency are almost the same.
|
// currency of the account + the piggy bank currency are almost the same.
|
||||||
// which amount from the transaction matches?
|
// which amount from the transaction matches?
|
||||||
// $currency->id === $piggyBankCurrency->id
|
// $currency->id === $piggyBankCurrency->id
|
||||||
$amount = null;
|
$amount = null;
|
||||||
if ($source->transaction_currency_id === $currency->id) {
|
if ((int)$source->transaction_currency_id === (int)$currency->id) {
|
||||||
Log::debug('Use normal amount');
|
Log::debug('Use normal amount');
|
||||||
$amount = app('steam')->$operator($source->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');
|
Log::debug('Use foreign amount');
|
||||||
$amount = app('steam')->$operator($source->foreign_amount);
|
$amount = app('steam')->$operator($source->foreign_amount);
|
||||||
}
|
}
|
||||||
if (null === $amount) {
|
if (null === $amount) {
|
||||||
|
Log::debug('No match on currency, so amount remains null, return "0".');
|
||||||
return '0';
|
return '0';
|
||||||
}
|
}
|
||||||
|
|
||||||
Log::debug(sprintf('The currency is %s and the amount is %s', $currency->code, $amount));
|
Log::debug(sprintf('The currency is %s and the amount is %s', $currency->code, $amount));
|
||||||
|
|
||||||
|
|
||||||
$room = bcsub((string)$piggyBank->targetamount, (string)$repetition->currentamount);
|
$room = bcsub((string)$piggyBank->targetamount, (string)$repetition->currentamount);
|
||||||
$compare = bcmul($repetition->currentamount, '-1');
|
$compare = bcmul($repetition->currentamount, '-1');
|
||||||
Log::debug(sprintf('Will add/remove %f to piggy bank #%d ("%s")', $amount, $piggyBank->id, $piggyBank->name));
|
Log::debug(sprintf('Will add/remove %f to piggy bank #%d ("%s")', $amount, $piggyBank->id, $piggyBank->name));
|
||||||
|
|
||||||
@@ -269,7 +273,6 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Also add amount in name.
|
* Also add amount in name.
|
||||||
*
|
*
|
||||||
@@ -285,7 +288,7 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
|
|||||||
/** @var PiggyBank $piggy */
|
/** @var PiggyBank $piggy */
|
||||||
foreach ($set as $piggy) {
|
foreach ($set as $piggy) {
|
||||||
$currentAmount = $this->getRepetition($piggy)->currentamount ?? '0';
|
$currentAmount = $this->getRepetition($piggy)->currentamount ?? '0';
|
||||||
$piggy->name = $piggy->name . ' (' . app('amount')->formatAnything($currency, $currentAmount, false) . ')';
|
$piggy->name = $piggy->name . ' (' . app('amount')->formatAnything($currency, $currentAmount, false) . ')';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -313,13 +316,13 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
|
|||||||
public function getSuggestedMonthlyAmount(PiggyBank $piggyBank): string
|
public function getSuggestedMonthlyAmount(PiggyBank $piggyBank): string
|
||||||
{
|
{
|
||||||
$savePerMonth = '0';
|
$savePerMonth = '0';
|
||||||
$repetition = $this->getRepetition($piggyBank);
|
$repetition = $this->getRepetition($piggyBank);
|
||||||
if (null === $repetition) {
|
if (null === $repetition) {
|
||||||
return $savePerMonth;
|
return $savePerMonth;
|
||||||
}
|
}
|
||||||
if (null !== $piggyBank->targetdate && $repetition->currentamount < $piggyBank->targetamount) {
|
if (null !== $piggyBank->targetdate && $repetition->currentamount < $piggyBank->targetamount) {
|
||||||
$now = Carbon::now();
|
$now = Carbon::now();
|
||||||
$diffInMonths = $now->diffInMonths($piggyBank->targetdate, false);
|
$diffInMonths = $now->diffInMonths($piggyBank->targetdate, false);
|
||||||
$remainingAmount = bcsub($piggyBank->targetamount, $repetition->currentamount);
|
$remainingAmount = bcsub($piggyBank->targetamount, $repetition->currentamount);
|
||||||
|
|
||||||
// more than 1 month to go and still need money to save:
|
// more than 1 month to go and still need money to save:
|
||||||
@@ -384,9 +387,9 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
|
|||||||
|
|
||||||
$set = $set->each(
|
$set = $set->each(
|
||||||
static function (Attachment $attachment) use ($disk) {
|
static function (Attachment $attachment) use ($disk) {
|
||||||
$notes = $attachment->notes()->first();
|
$notes = $attachment->notes()->first();
|
||||||
$attachment->file_exists = $disk->exists($attachment->fileName());
|
$attachment->file_exists = $disk->exists($attachment->fileName());
|
||||||
$attachment->notes = $notes ? $notes->text : '';
|
$attachment->notes = $notes ? $notes->text : '';
|
||||||
|
|
||||||
return $attachment;
|
return $attachment;
|
||||||
}
|
}
|
||||||
@@ -414,7 +417,7 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
|
|||||||
$search->where('piggy_banks.name', 'LIKE', sprintf('%%%s%%', $query));
|
$search->where('piggy_banks.name', 'LIKE', sprintf('%%%s%%', $query));
|
||||||
}
|
}
|
||||||
$search->orderBy('piggy_banks.order', 'ASC')
|
$search->orderBy('piggy_banks.order', 'ASC')
|
||||||
->orderBy('piggy_banks.name', 'ASC');
|
->orderBy('piggy_banks.name', 'ASC');
|
||||||
|
|
||||||
return $search->take($limit)->get();
|
return $search->take($limit)->get();
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user