mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-12-12 01:42:32 +00:00
Merge remote-tracking branch 'upstream/main' into feat/expression-engine
This commit is contained in:
@@ -30,11 +30,9 @@ use FireflyIII\Models\PiggyBank;
|
||||
use FireflyIII\Models\RuleAction;
|
||||
use FireflyIII\Models\Transaction;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Models\TransactionType;
|
||||
use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface;
|
||||
use FireflyIII\TransactionRules\Expressions\ActionExpressionEvaluator;
|
||||
use FireflyIII\User;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
/**
|
||||
* Class UpdatePiggybank
|
||||
@@ -46,8 +44,6 @@ class UpdatePiggybank implements ActionInterface
|
||||
|
||||
/**
|
||||
* TriggerInterface constructor.
|
||||
*
|
||||
* @param RuleAction $action
|
||||
*/
|
||||
public function __construct(RuleAction $action, ActionExpressionEvaluator $evaluator)
|
||||
{
|
||||
@@ -55,38 +51,38 @@ class UpdatePiggybank implements ActionInterface
|
||||
$this->evaluator = $evaluator;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function actOnArray(array $journal): bool
|
||||
{
|
||||
Log::debug(sprintf('Triggered rule action UpdatePiggybank on journal #%d', $journal['transaction_journal_id']));
|
||||
app('log')->debug(sprintf('Triggered rule action UpdatePiggybank on journal #%d', $journal['transaction_journal_id']));
|
||||
|
||||
$piggyBankName = $this->evaluator->evaluate($journal);
|
||||
// refresh the transaction type.
|
||||
$user = User::find($journal['user_id']);
|
||||
/** @var TransactionJournal $journalObj */
|
||||
$journalObj = $user->transactionJournals()->find($journal['transaction_journal_id']);
|
||||
$type = TransactionType::find((int)$journalObj->transaction_type_id);
|
||||
/** @var User $user */
|
||||
$user = User::find($journal['user_id']);
|
||||
|
||||
$piggyBank = $this->findPiggyBank($user, $piggyBankName);
|
||||
/** @var TransactionJournal $journalObj */
|
||||
$journalObj = $user->transactionJournals()->find($journal['transaction_journal_id']);
|
||||
|
||||
$piggyBank = $this->findPiggyBank($user, $piggyBankName);
|
||||
if (null === $piggyBank) {
|
||||
Log::info(
|
||||
app('log')->info(
|
||||
sprintf('No piggy bank named "%s", cant execute action #%d of rule #%d', $piggyBankName, $this->action->id, $this->action->rule_id)
|
||||
);
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.cannot_find_piggy', ['name' => $piggyBankName])));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
Log::debug(sprintf('Found piggy bank #%d ("%s")', $piggyBank->id, $piggyBank->name));
|
||||
app('log')->debug(sprintf('Found piggy bank #%d ("%s")', $piggyBank->id, $piggyBank->name));
|
||||
|
||||
/** @var Transaction $source */
|
||||
$source = $journalObj->transactions()->where('amount', '<', 0)->first();
|
||||
$source = $journalObj->transactions()->where('amount', '<', 0)->first();
|
||||
|
||||
/** @var Transaction $destination */
|
||||
$destination = $journalObj->transactions()->where('amount', '>', 0)->first();
|
||||
|
||||
if ((int)$source->account_id === (int)$piggyBank->account_id) {
|
||||
Log::debug('Piggy bank account is linked to source, so remove amount from piggy bank.');
|
||||
if ($source->account_id === $piggyBank->account_id) {
|
||||
app('log')->debug('Piggy bank account is linked to source, so remove amount from piggy bank.');
|
||||
$this->removeAmount($piggyBank, $journalObj, $destination->amount);
|
||||
|
||||
event(
|
||||
@@ -106,8 +102,8 @@ class UpdatePiggybank implements ActionInterface
|
||||
|
||||
return true;
|
||||
}
|
||||
if ((int)$destination->account_id === (int)$piggyBank->account_id) {
|
||||
Log::debug('Piggy bank account is linked to source, so add amount to piggy bank.');
|
||||
if ($destination->account_id === $piggyBank->account_id) {
|
||||
app('log')->debug('Piggy bank account is linked to source, so add amount to piggy bank.');
|
||||
$this->addAmount($piggyBank, $journalObj, $destination->amount);
|
||||
|
||||
event(
|
||||
@@ -127,7 +123,7 @@ class UpdatePiggybank implements ActionInterface
|
||||
|
||||
return true;
|
||||
}
|
||||
Log::info(
|
||||
app('log')->info(
|
||||
sprintf(
|
||||
'Piggy bank is not linked to source ("#%d") or destination ("#%d"), so no action will be taken.',
|
||||
$source->account_id,
|
||||
@@ -135,38 +131,27 @@ class UpdatePiggybank implements ActionInterface
|
||||
)
|
||||
);
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.no_link_piggy', ['name' => $piggyBankName])));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param User $user
|
||||
*
|
||||
* @return PiggyBank|null
|
||||
*/
|
||||
private function findPiggyBank(User $user, string $name): ?PiggyBank
|
||||
{
|
||||
return $user->piggyBanks()->where('piggy_banks.name', $name)->first();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param PiggyBank $piggyBank
|
||||
* @param TransactionJournal $journal
|
||||
* @param string $amount
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function removeAmount(PiggyBank $piggyBank, TransactionJournal $journal, string $amount): void
|
||||
{
|
||||
$repository = app(PiggyBankRepositoryInterface::class);
|
||||
$repository->setUser($journal->user);
|
||||
|
||||
// how much can we remove from this piggy bank?
|
||||
$toRemove = $repository->getCurrentAmount($piggyBank);
|
||||
Log::debug(sprintf('Amount is %s, max to remove is %s', $amount, $toRemove));
|
||||
$toRemove = $repository->getCurrentAmount($piggyBank);
|
||||
app('log')->debug(sprintf('Amount is %s, max to remove is %s', $amount, $toRemove));
|
||||
|
||||
// if $amount is bigger than $toRemove, shrink it.
|
||||
$amount = -1 === bccomp($amount, $toRemove) ? $amount : $toRemove;
|
||||
Log::debug(sprintf('Amount is now %s', $amount));
|
||||
$amount = -1 === bccomp($amount, $toRemove) ? $amount : $toRemove;
|
||||
app('log')->debug(sprintf('Amount is now %s', $amount));
|
||||
|
||||
// if amount is zero, stop.
|
||||
if (0 === bccomp('0', $amount)) {
|
||||
@@ -181,18 +166,11 @@ class UpdatePiggybank implements ActionInterface
|
||||
|
||||
return;
|
||||
}
|
||||
Log::debug(sprintf('Will now remove %s from piggy bank.', $amount));
|
||||
app('log')->debug(sprintf('Will now remove %s from piggy bank.', $amount));
|
||||
|
||||
$repository->removeAmount($piggyBank, $amount, $journal);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param PiggyBank $piggyBank
|
||||
* @param TransactionJournal $journal
|
||||
* @param string $amount
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function addAmount(PiggyBank $piggyBank, TransactionJournal $journal, string $amount): void
|
||||
{
|
||||
$repository = app(PiggyBankRepositoryInterface::class);
|
||||
@@ -200,18 +178,17 @@ class UpdatePiggybank implements ActionInterface
|
||||
|
||||
// how much can we add to the piggy bank?
|
||||
if (0 !== bccomp($piggyBank->targetamount, '0')) {
|
||||
$toAdd = bcsub($piggyBank->targetamount, $repository->getCurrentAmount($piggyBank));
|
||||
Log::debug(sprintf('Max amount to add to piggy bank is %s, amount is %s', $toAdd, $amount));
|
||||
$toAdd = bcsub($piggyBank->targetamount, $repository->getCurrentAmount($piggyBank));
|
||||
app('log')->debug(sprintf('Max amount to add to piggy bank is %s, amount is %s', $toAdd, $amount));
|
||||
|
||||
// update amount to fit:
|
||||
$amount = -1 === bccomp($amount, $toAdd) ? $amount : $toAdd;
|
||||
Log::debug(sprintf('Amount is now %s', $amount));
|
||||
app('log')->debug(sprintf('Amount is now %s', $amount));
|
||||
}
|
||||
if (0 === bccomp($piggyBank->targetamount, '0')) {
|
||||
Log::debug('Target amount is zero, can add anything.');
|
||||
app('log')->debug('Target amount is zero, can add anything.');
|
||||
}
|
||||
|
||||
|
||||
// if amount is zero, stop.
|
||||
if (0 === bccomp('0', $amount)) {
|
||||
app('log')->warning('Amount left is zero, stop.');
|
||||
@@ -225,7 +202,7 @@ class UpdatePiggybank implements ActionInterface
|
||||
|
||||
return;
|
||||
}
|
||||
Log::debug(sprintf('Will now add %s to piggy bank.', $amount));
|
||||
app('log')->debug(sprintf('Will now add %s to piggy bank.', $amount));
|
||||
|
||||
$repository->addAmount($piggyBank, $amount, $journal);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user