Clean up piggy alerts.

This commit is contained in:
James Cole
2025-10-02 19:49:32 +02:00
parent b9c93091cd
commit a70fab1e87
3 changed files with 14 additions and 10 deletions

View File

@@ -31,7 +31,9 @@ use FireflyIII\Models\PiggyBank;
use FireflyIII\Models\RuleAction;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface;
use FireflyIII\Support\Facades\Amount;
use FireflyIII\User;
use Illuminate\Support\Facades\Log;
@@ -176,7 +178,9 @@ class UpdatePiggyBank implements ActionInterface
private function removeAmount(PiggyBank $piggyBank, array $array, TransactionJournal $journal, Account $account, string $amount): void
{
$repository = app(PiggyBankRepositoryInterface::class);
$accountRepository = app(AccountRepositoryInterface::class);
$repository->setUser($journal->user);
$accountRepository->setUser($account->user);
// how much can we remove from this piggy bank?
$toRemove = $repository->getCurrentAmount($piggyBank, $account);
@@ -196,7 +200,8 @@ class UpdatePiggyBank implements ActionInterface
if (false === $repository->canRemoveAmount($piggyBank, $account, $amount)) {
Log::warning(sprintf('Cannot remove %s from piggy bank.', $amount));
event(new RuleActionFailedOnArray($this->action, $array, trans('rules.cannot_remove_from_piggy', ['amount' => $amount, 'name' => $piggyBank->name])));
$currency = $accountRepository->getAccountCurrency($account) ?? Amount::getPrimaryCurrency();
event(new RuleActionFailedOnArray($this->action, $array, trans('rules.cannot_remove_from_piggy', ['amount' => Amount::formatAnything($amount, $currency, false), 'name' => $piggyBank->name])));
return;
}
@@ -208,7 +213,9 @@ class UpdatePiggyBank implements ActionInterface
private function addAmount(PiggyBank $piggyBank, array $array, TransactionJournal $journal, Account $account, string $amount): void
{
$repository = app(PiggyBankRepositoryInterface::class);
$accountRepository = app(AccountRepositoryInterface::class);
$repository->setUser($journal->user);
$accountRepository->setUser($account->user);
// how much can we add to the piggy bank?
if (0 !== bccomp($piggyBank->target_amount, '0')) {
@@ -233,7 +240,8 @@ class UpdatePiggyBank implements ActionInterface
if (false === $repository->canAddAmount($piggyBank, $account, $amount)) {
Log::warning(sprintf('Cannot add %s to piggy bank.', $amount));
event(new RuleActionFailedOnArray($this->action, $array, trans('rules.cannot_add_to_piggy', ['amount' => $amount, 'name' => $piggyBank->name])));
$currency = $accountRepository->getAccountCurrency($account) ?? Amount::getPrimaryCurrency();
event(new RuleActionFailedOnArray($this->action, $array, trans('rules.cannot_add_to_piggy', ['amount' => Amount::formatAnything($amount, $currency, false), 'name' => $piggyBank->name])));
return;
}