mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-12 15:35:15 +00:00
Multi account piggy banks.
This commit is contained in:
@@ -26,6 +26,7 @@ namespace FireflyIII\Repositories\PiggyBank;
|
||||
use Carbon\Carbon;
|
||||
use FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\Factory\PiggyBankFactory;
|
||||
use FireflyIII\Models\Account;
|
||||
use FireflyIII\Models\Attachment;
|
||||
use FireflyIII\Models\Note;
|
||||
use FireflyIII\Models\PiggyBank;
|
||||
@@ -95,7 +96,7 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
|
||||
|
||||
public function getAttachments(PiggyBank $piggyBank): Collection
|
||||
{
|
||||
$set = $piggyBank->attachments()->get();
|
||||
$set = $piggyBank->attachments()->get();
|
||||
|
||||
/** @var \Storage $disk */
|
||||
$disk = \Storage::disk('upload');
|
||||
@@ -114,22 +115,28 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
|
||||
/**
|
||||
* Get current amount saved in piggy bank.
|
||||
*/
|
||||
public function getCurrentAmount(PiggyBank $piggyBank): string
|
||||
public function getCurrentAmount(PiggyBank $piggyBank, ?Account $account = null): string
|
||||
{
|
||||
$sum = '0';
|
||||
foreach ($piggyBank->accounts as $account) {
|
||||
$amount = (string) $account->pivot->current_amount;
|
||||
foreach ($piggyBank->accounts as $current) {
|
||||
if(null !== $account && $account->id !== $current->id) {
|
||||
continue;
|
||||
}
|
||||
$amount = (string) $current->pivot->current_amount;
|
||||
$amount = '' === $amount ? '0' : $amount;
|
||||
$sum = bcadd($sum, $amount);
|
||||
}
|
||||
Log::debug(sprintf('Current amount in piggy bank #%d ("%s") is %s', $piggyBank->id, $piggyBank->name, $sum));
|
||||
|
||||
return $sum;
|
||||
}
|
||||
|
||||
public function getRepetition(PiggyBank $piggyBank): ?PiggyBankRepetition
|
||||
public function getRepetition(PiggyBank $piggyBank, bool $overrule = false): ?PiggyBankRepetition
|
||||
{
|
||||
throw new FireflyException('[b] Piggy bank repetitions are EOL.');
|
||||
|
||||
if (false === $overrule) {
|
||||
throw new FireflyException('[b] Piggy bank repetitions are EOL.');
|
||||
}
|
||||
Log::warning('Piggy bank repetitions are EOL.');
|
||||
return $piggyBank->piggyBankRepetitions()->first();
|
||||
}
|
||||
|
||||
@@ -148,15 +155,15 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
|
||||
throw new FireflyException('[c] Piggy bank repetitions are EOL.');
|
||||
app('log')->debug(sprintf('Now in getExactAmount(%d, %d, %d)', $piggyBank->id, $repetition->id, $journal->id));
|
||||
|
||||
$operator = null;
|
||||
$currency = null;
|
||||
$operator = null;
|
||||
$currency = null;
|
||||
|
||||
/** @var JournalRepositoryInterface $journalRepost */
|
||||
$journalRepost = app(JournalRepositoryInterface::class);
|
||||
$journalRepost = app(JournalRepositoryInterface::class);
|
||||
$journalRepost->setUser($this->user);
|
||||
|
||||
/** @var AccountRepositoryInterface $accountRepos */
|
||||
$accountRepos = app(AccountRepositoryInterface::class);
|
||||
$accountRepos = app(AccountRepositoryInterface::class);
|
||||
$accountRepos->setUser($this->user);
|
||||
|
||||
$defaultCurrency = app('amount')->getDefaultCurrencyByUserGroup($this->user->userGroup);
|
||||
@@ -165,10 +172,10 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
|
||||
app('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) {
|
||||
@@ -190,7 +197,7 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
|
||||
}
|
||||
// currency of the account + the piggy bank currency are almost the same.
|
||||
// which amount from the transaction matches?
|
||||
$amount = null;
|
||||
$amount = null;
|
||||
if ((int) $source->transaction_currency_id === $currency->id) {
|
||||
app('log')->debug('Use normal amount');
|
||||
$amount = app('steam')->{$operator}($source->amount); // @phpstan-ignore-line
|
||||
@@ -206,8 +213,8 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
|
||||
}
|
||||
|
||||
app('log')->debug(sprintf('The currency is %s and the amount is %s', $currency->code, $amount));
|
||||
$room = bcsub($piggyBank->target_amount, $repetition->current_amount);
|
||||
$compare = bcmul($repetition->current_amount, '-1');
|
||||
$room = bcsub($piggyBank->target_amount, $repetition->current_amount);
|
||||
$compare = bcmul($repetition->current_amount, '-1');
|
||||
|
||||
if (0 === bccomp($piggyBank->target_amount, '0')) {
|
||||
// amount is zero? then the "room" is positive amount of we wish to add or remove.
|
||||
@@ -239,7 +246,7 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
|
||||
return (string) $amount;
|
||||
}
|
||||
|
||||
public function setUser(null|Authenticatable|User $user): void
|
||||
public function setUser(null | Authenticatable | User $user): void
|
||||
{
|
||||
if ($user instanceof User) {
|
||||
$this->user = $user;
|
||||
@@ -264,12 +271,12 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
|
||||
{
|
||||
$currency = app('amount')->getDefaultCurrency();
|
||||
|
||||
$set = $this->getPiggyBanks();
|
||||
$set = $this->getPiggyBanks();
|
||||
|
||||
/** @var PiggyBank $piggy */
|
||||
foreach ($set as $piggy) {
|
||||
$currentAmount = $this->getRepetition($piggy)->current_amount ?? '0';
|
||||
$piggy->name = $piggy->name.' ('.app('amount')->formatAnything($currency, $currentAmount, false).')';
|
||||
$piggy->name = $piggy->name . ' (' . app('amount')->formatAnything($currency, $currentAmount, false) . ')';
|
||||
}
|
||||
|
||||
return $set;
|
||||
@@ -278,16 +285,15 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
|
||||
public function getPiggyBanks(): Collection
|
||||
{
|
||||
return PiggyBank::leftJoin('account_piggy_bank', 'account_piggy_bank.piggy_bank_id', '=', 'piggy_banks.id')
|
||||
->leftJoin('accounts', 'accounts.id', '=', 'account_piggy_bank.account_id')
|
||||
->where('accounts.user_id', auth()->user()->id)
|
||||
->with(
|
||||
[
|
||||
'account',
|
||||
'objectGroups',
|
||||
]
|
||||
)
|
||||
->orderBy('piggy_banks.order', 'ASC')->get(['piggy_banks.*'])
|
||||
;
|
||||
->leftJoin('accounts', 'accounts.id', '=', 'account_piggy_bank.account_id')
|
||||
->where('accounts.user_id', auth()->user()->id)
|
||||
->with(
|
||||
[
|
||||
'account',
|
||||
'objectGroups',
|
||||
]
|
||||
)
|
||||
->orderBy('piggy_banks.order', 'ASC')->distinct()->get(['piggy_banks.*']);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -320,21 +326,22 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
|
||||
/**
|
||||
* Get for piggy account what is left to put in piggies.
|
||||
*/
|
||||
public function leftOnAccount(PiggyBank $piggyBank, Carbon $date): string
|
||||
public function leftOnAccount(PiggyBank $piggyBank, Account $account, Carbon $date): string
|
||||
{
|
||||
$balance = app('steam')->balanceIgnoreVirtual($piggyBank->account, $date);
|
||||
Log::debug(sprintf('leftOnAccount("%s","%s","%s")', $piggyBank->name, $account->name, $date->format('Y-m-d H:i:s')));
|
||||
$balance = app('steam')->balanceConvertedIgnoreVirtual($account, $date, $piggyBank->transactionCurrency);
|
||||
Log::debug(sprintf('Balance is: %s', $balance));
|
||||
|
||||
/** @var Collection $piggies */
|
||||
$piggies = $piggyBank->account->piggyBanks;
|
||||
$piggies = $account->piggyBanks;
|
||||
|
||||
/** @var PiggyBank $current */
|
||||
foreach ($piggies as $current) {
|
||||
$repetition = $this->getRepetition($current);
|
||||
if (null !== $repetition) {
|
||||
$balance = bcsub($balance, $repetition->current_amount);
|
||||
}
|
||||
$amount = $this->getCurrentAmount($current, $account);
|
||||
$balance = bcsub($balance, $amount);
|
||||
Log::debug(sprintf('Piggy bank: #%d with amount %s, balance is now %s', $current->id, $amount, $balance));
|
||||
}
|
||||
|
||||
Log::debug(sprintf('Final balance is: %s', $balance));
|
||||
return $balance;
|
||||
}
|
||||
|
||||
@@ -345,8 +352,7 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
|
||||
$search->whereLike('piggy_banks.name', sprintf('%%%s%%', $query));
|
||||
}
|
||||
$search->orderBy('piggy_banks.order', 'ASC')
|
||||
->orderBy('piggy_banks.name', 'ASC')
|
||||
;
|
||||
->orderBy('piggy_banks.name', 'ASC');
|
||||
|
||||
return $search->take($limit)->get();
|
||||
}
|
||||
|
Reference in New Issue
Block a user