mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
Pass on the journal and update rules.
This commit is contained in:
parent
44af5473a8
commit
344cbe4fb7
@ -74,8 +74,7 @@ class PiggyBankEventFactory
|
|||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// amount can be negative.
|
// amount can be negative here
|
||||||
|
$piggyRepos->addAmountToRepetition($repetition, $amount, $journal);
|
||||||
$piggyRepos->addAmountToRepetition($repetition, $amount);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -24,6 +24,8 @@ namespace FireflyIII\Handlers\Events;
|
|||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
use FireflyIII\Events\ChangedPiggyBankAmount;
|
use FireflyIII\Events\ChangedPiggyBankAmount;
|
||||||
use FireflyIII\Models\PiggyBankEvent;
|
use FireflyIII\Models\PiggyBankEvent;
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class PiggyBankEventHandler
|
* Class PiggyBankEventHandler
|
||||||
@ -42,6 +44,18 @@ class PiggyBankEventHandler
|
|||||||
$journal = $event->transactionGroup->transactionJournals()->first();
|
$journal = $event->transactionGroup->transactionJournals()->first();
|
||||||
}
|
}
|
||||||
$date = $journal?->date ?? Carbon::now();
|
$date = $journal?->date ?? Carbon::now();
|
||||||
|
|
||||||
|
// sanity check: event must not already exist for this journal and piggy bank.
|
||||||
|
if (null !== $journal) {
|
||||||
|
$exists = PiggyBankEvent::where('piggy_bank_id', $event->piggyBank->id)
|
||||||
|
->where('transaction_journal_id', $journal->id)
|
||||||
|
->exists();
|
||||||
|
if($exists) {
|
||||||
|
Log::warning('Already have event for this journal and piggy, will not create another.');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
PiggyBankEvent::create(
|
PiggyBankEvent::create(
|
||||||
[
|
[
|
||||||
'piggy_bank_id' => $event->piggyBank->id,
|
'piggy_bank_id' => $event->piggyBank->id,
|
||||||
|
@ -30,6 +30,7 @@ use FireflyIII\Exceptions\FireflyException;
|
|||||||
use FireflyIII\Models\Note;
|
use FireflyIII\Models\Note;
|
||||||
use FireflyIII\Models\PiggyBank;
|
use FireflyIII\Models\PiggyBank;
|
||||||
use FireflyIII\Models\PiggyBankRepetition;
|
use FireflyIII\Models\PiggyBankRepetition;
|
||||||
|
use FireflyIII\Models\TransactionJournal;
|
||||||
use FireflyIII\Repositories\ObjectGroup\CreatesObjectGroups;
|
use FireflyIII\Repositories\ObjectGroup\CreatesObjectGroups;
|
||||||
use Illuminate\Database\QueryException;
|
use Illuminate\Database\QueryException;
|
||||||
use Illuminate\Support\Facades\Log;
|
use Illuminate\Support\Facades\Log;
|
||||||
@ -44,10 +45,10 @@ trait ModifiesPiggyBanks
|
|||||||
/**
|
/**
|
||||||
* @param PiggyBank $piggyBank
|
* @param PiggyBank $piggyBank
|
||||||
* @param string $amount
|
* @param string $amount
|
||||||
*
|
* @param TransactionJournal|null $journal
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function addAmount(PiggyBank $piggyBank, string $amount): bool
|
public function addAmount(PiggyBank $piggyBank, string $amount, ?TransactionJournal $journal = null): bool
|
||||||
{
|
{
|
||||||
$repetition = $this->getRepetition($piggyBank);
|
$repetition = $this->getRepetition($piggyBank);
|
||||||
if (null === $repetition) {
|
if (null === $repetition) {
|
||||||
@ -58,7 +59,7 @@ trait ModifiesPiggyBanks
|
|||||||
$repetition->save();
|
$repetition->save();
|
||||||
|
|
||||||
Log::debug('addAmount: Trigger change for positive amount.');
|
Log::debug('addAmount: Trigger change for positive amount.');
|
||||||
event(new ChangedPiggyBankAmount($piggyBank, $amount, null, null));
|
event(new ChangedPiggyBankAmount($piggyBank, $amount, $journal, null));
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -69,16 +70,16 @@ trait ModifiesPiggyBanks
|
|||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function addAmountToRepetition(PiggyBankRepetition $repetition, string $amount): void
|
public function addAmountToRepetition(PiggyBankRepetition $repetition, string $amount, TransactionJournal $journal): void
|
||||||
{
|
{
|
||||||
Log::debug(sprintf('addAmountToRepetition: %s', $amount));
|
Log::debug(sprintf('addAmountToRepetition: %s', $amount));
|
||||||
if (-1 === bccomp($amount, '0')) {
|
if (-1 === bccomp($amount, '0')) {
|
||||||
Log::debug('Remove amount.');
|
Log::debug('Remove amount.');
|
||||||
$this->removeAmount($repetition->piggyBank, bcmul($amount, '-1'));
|
$this->removeAmount($repetition->piggyBank, bcmul($amount, '-1'), $journal);
|
||||||
}
|
}
|
||||||
if (1 === bccomp($amount, '0')) {
|
if (1 === bccomp($amount, '0')) {
|
||||||
Log::debug('Add amount.');
|
Log::debug('Add amount.');
|
||||||
$this->addAmount($repetition->piggyBank, $amount);
|
$this->addAmount($repetition->piggyBank, $amount, $journal);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -149,7 +150,7 @@ trait ModifiesPiggyBanks
|
|||||||
*
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function removeAmount(PiggyBank $piggyBank, string $amount): bool
|
public function removeAmount(PiggyBank $piggyBank, string $amount, ?TransactionJournal $journal = null): bool
|
||||||
{
|
{
|
||||||
$repetition = $this->getRepetition($piggyBank);
|
$repetition = $this->getRepetition($piggyBank);
|
||||||
if (null === $repetition) {
|
if (null === $repetition) {
|
||||||
@ -159,7 +160,7 @@ trait ModifiesPiggyBanks
|
|||||||
$repetition->save();
|
$repetition->save();
|
||||||
|
|
||||||
Log::debug('addAmount: Trigger change for negative amount.');
|
Log::debug('addAmount: Trigger change for negative amount.');
|
||||||
event(new ChangedPiggyBankAmount($piggyBank, bcmul($amount, '-1'), null, null));
|
event(new ChangedPiggyBankAmount($piggyBank, bcmul($amount, '-1'), $journal, null));
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -37,18 +37,18 @@ use Illuminate\Support\Collection;
|
|||||||
interface PiggyBankRepositoryInterface
|
interface PiggyBankRepositoryInterface
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @param PiggyBank $piggyBank
|
* @param PiggyBank $piggyBank
|
||||||
* @param string $amount
|
* @param string $amount
|
||||||
*
|
* @param TransactionJournal|null $journal
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function addAmount(PiggyBank $piggyBank, string $amount): bool;
|
public function addAmount(PiggyBank $piggyBank, string $amount, ?TransactionJournal $journal = null): bool;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param PiggyBankRepetition $repetition
|
* @param PiggyBankRepetition $repetition
|
||||||
* @param string $amount
|
* @param string $amount
|
||||||
*/
|
*/
|
||||||
public function addAmountToRepetition(PiggyBankRepetition $repetition, string $amount): void;
|
public function addAmountToRepetition(PiggyBankRepetition $repetition, string $amount, TransactionJournal $journal): void;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param PiggyBank $piggyBank
|
* @param PiggyBank $piggyBank
|
||||||
@ -197,12 +197,12 @@ interface PiggyBankRepositoryInterface
|
|||||||
public function leftOnAccount(PiggyBank $piggyBank, Carbon $date): string;
|
public function leftOnAccount(PiggyBank $piggyBank, Carbon $date): string;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param PiggyBank $piggyBank
|
* @param PiggyBank $piggyBank
|
||||||
* @param string $amount
|
* @param string $amount
|
||||||
*
|
* @param TransactionJournal|null $journal
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function removeAmount(PiggyBank $piggyBank, string $amount): bool;
|
public function removeAmount(PiggyBank $piggyBank, string $amount, ?TransactionJournal $journal = null): bool;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param PiggyBank $piggyBank
|
* @param PiggyBank $piggyBank
|
||||||
|
@ -27,6 +27,7 @@ namespace FireflyIII\TransactionRules\Actions;
|
|||||||
use FireflyIII\Models\PiggyBank;
|
use FireflyIII\Models\PiggyBank;
|
||||||
use FireflyIII\Models\RuleAction;
|
use FireflyIII\Models\RuleAction;
|
||||||
use FireflyIII\Models\Transaction;
|
use FireflyIII\Models\Transaction;
|
||||||
|
use FireflyIII\Models\TransactionJournal;
|
||||||
use FireflyIII\Models\TransactionType;
|
use FireflyIII\Models\TransactionType;
|
||||||
use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface;
|
use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface;
|
||||||
use FireflyIII\User;
|
use FireflyIII\User;
|
||||||
@ -37,14 +38,12 @@ use Log;
|
|||||||
*/
|
*/
|
||||||
class UpdatePiggybank implements ActionInterface
|
class UpdatePiggybank implements ActionInterface
|
||||||
{
|
{
|
||||||
|
private RuleAction $action;
|
||||||
/** @var RuleAction The rule action */
|
|
||||||
private $action;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TriggerInterface constructor.
|
* TriggerInterface constructor.
|
||||||
*
|
*
|
||||||
* @param RuleAction $action
|
* @param RuleAction $action
|
||||||
*/
|
*/
|
||||||
public function __construct(RuleAction $action)
|
public function __construct(RuleAction $action)
|
||||||
{
|
{
|
||||||
@ -59,9 +58,10 @@ class UpdatePiggybank implements ActionInterface
|
|||||||
Log::debug(sprintf('Triggered rule action UpdatePiggybank on journal #%d', $journal['transaction_journal_id']));
|
Log::debug(sprintf('Triggered rule action UpdatePiggybank on journal #%d', $journal['transaction_journal_id']));
|
||||||
|
|
||||||
// refresh the transaction type.
|
// refresh the transaction type.
|
||||||
$user = User::find($journal['user_id']);
|
$user = User::find($journal['user_id']);
|
||||||
|
/** @var TransactionJournal $journalObj */
|
||||||
$journalObj = $user->transactionJournals()->find($journal['transaction_journal_id']);
|
$journalObj = $user->transactionJournals()->find($journal['transaction_journal_id']);
|
||||||
$type = TransactionType::find((int) $journalObj->transaction_type_id);
|
$type = TransactionType::find((int)$journalObj->transaction_type_id);
|
||||||
$journal['transaction_type_type'] = $type->type;
|
$journal['transaction_type_type'] = $type->type;
|
||||||
|
|
||||||
if (TransactionType::TRANSFER !== $journal['transaction_type_type']) {
|
if (TransactionType::TRANSFER !== $journal['transaction_type_type']) {
|
||||||
@ -70,10 +70,10 @@ class UpdatePiggybank implements ActionInterface
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$piggyBank = $this->findPiggybank($user);
|
$piggyBank = $this->findPiggyBank($user);
|
||||||
if (null === $piggyBank) {
|
if (null === $piggyBank) {
|
||||||
Log::info(
|
Log::info(
|
||||||
sprintf('No piggy bank names "%s", cant execute action #%d of rule #%d', $this->action->action_value, $this->action->id, $this->action->rule_id)
|
sprintf('No piggy bank named "%s", cant execute action #%d of rule #%d', $this->action->action_value, $this->action->id, $this->action->rule_id)
|
||||||
);
|
);
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
@ -82,25 +82,27 @@ class UpdatePiggybank implements ActionInterface
|
|||||||
Log::debug(sprintf('Found piggy bank #%d ("%s")', $piggyBank->id, $piggyBank->name));
|
Log::debug(sprintf('Found piggy bank #%d ("%s")', $piggyBank->id, $piggyBank->name));
|
||||||
|
|
||||||
/** @var Transaction $source */
|
/** @var Transaction $source */
|
||||||
$source = Transaction::where('transaction_journal_id', $journal['transaction_journal_id'])->where('amount', '<', 0)->first();
|
|
||||||
/** @var Transaction $destination */
|
/** @var Transaction $destination */
|
||||||
$destination = Transaction::where('transaction_journal_id', $journal['transaction_journal_id'])->where('amount', '>', 0)->first();
|
$source = $journalObj->transactions()->where('amount', '<', 0)->first();
|
||||||
|
$destination = $journalObj->transactions()->where('amount', '>', 0)->first();
|
||||||
|
|
||||||
if ((int) $source->account_id === (int) $piggyBank->account_id) {
|
if ((int)$source->account_id === (int)$piggyBank->account_id) {
|
||||||
Log::debug('Piggy bank account is linked to source, so remove amount.');
|
Log::debug('Piggy bank account is linked to source, so remove amount from piggy bank.');
|
||||||
$this->removeAmount($journal, $piggyBank, $destination->amount);
|
$this->removeAmount($journal, $piggyBank, $destination->amount);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if ((int) $destination->account_id === (int) $piggyBank->account_id) {
|
if ((int)$destination->account_id === (int)$piggyBank->account_id) {
|
||||||
Log::debug('Piggy bank account is linked to source, so add amount.');
|
Log::debug('Piggy bank account is linked to source, so add amount to piggy bank.');
|
||||||
$this->addAmount($journal, $piggyBank, $destination->amount);
|
$this->addAmount($journal, $piggyBank, $destination->amount);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
Log::info(
|
Log::info(
|
||||||
sprintf(
|
sprintf(
|
||||||
'Piggy bank is not linked to source ("#%d") or destination ("#%d"), so no action will be taken.', $source->account_id, $destination->account_id
|
'Piggy bank is not linked to source ("#%d") or destination ("#%d"), so no action will be taken.',
|
||||||
|
$source->account_id,
|
||||||
|
$destination->account_id
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -108,30 +110,30 @@ class UpdatePiggybank implements ActionInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param User $user
|
* @param User $user
|
||||||
*
|
*
|
||||||
* @return PiggyBank|null
|
* @return PiggyBank|null
|
||||||
*/
|
*/
|
||||||
private function findPiggybank(User $user): ?PiggyBank
|
private function findPiggyBank(User $user): ?PiggyBank
|
||||||
{
|
{
|
||||||
return $user->piggyBanks()->where('piggy_banks.name', $this->action->action_value)->first();
|
return $user->piggyBanks()->where('piggy_banks.name', $this->action->action_value)->first();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $journalArray
|
* @param PiggyBank $piggyBank
|
||||||
* @param PiggyBank $piggyBank
|
* @param TransactionJournal $journal
|
||||||
* @param string $amount
|
* @param string $amount
|
||||||
|
* @return void
|
||||||
*/
|
*/
|
||||||
private function removeAmount(array $journalArray, PiggyBank $piggyBank, string $amount): void
|
private function removeAmount(PiggyBank $piggyBank, TransactionJournal $journal, string $amount): void
|
||||||
{
|
{
|
||||||
$user = User::find($journalArray['user_id']);
|
|
||||||
$journal = $user->transactionJournals()->find($journalArray['transaction_journal_id']);
|
|
||||||
$repository = app(PiggyBankRepositoryInterface::class);
|
$repository = app(PiggyBankRepositoryInterface::class);
|
||||||
$repository->setUser($journal->user);
|
$repository->setUser($journal->user);
|
||||||
|
|
||||||
// how much can we remove from piggy bank?
|
// how much can we remove from this piggy bank?
|
||||||
$toRemove = $repository->getCurrentAmount($piggyBank);
|
$toRemove = $repository->getCurrentAmount($piggyBank);
|
||||||
Log::debug(sprintf('Amount is %s, max to remove is %s', $amount, $toRemove));
|
Log::debug(sprintf('Amount is %s, max to remove is %s', $amount, $toRemove));
|
||||||
|
|
||||||
// if $amount is bigger than $toRemove, shrink it.
|
// if $amount is bigger than $toRemove, shrink it.
|
||||||
$amount = -1 === bccomp($amount, $toRemove) ? $amount : $toRemove;
|
$amount = -1 === bccomp($amount, $toRemove) ? $amount : $toRemove;
|
||||||
Log::debug(sprintf('Amount is now %s', $amount));
|
Log::debug(sprintf('Amount is now %s', $amount));
|
||||||
@ -151,18 +153,17 @@ class UpdatePiggybank implements ActionInterface
|
|||||||
}
|
}
|
||||||
Log::debug(sprintf('Will now remove %s from piggy bank.', $amount));
|
Log::debug(sprintf('Will now remove %s from piggy bank.', $amount));
|
||||||
|
|
||||||
$repository->removeAmount($piggyBank, $amount);
|
$repository->removeAmount($piggyBank, $amount, $journal);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $journalArray
|
* @param PiggyBank $piggyBank
|
||||||
* @param PiggyBank $piggyBank
|
* @param TransactionJournal $journal
|
||||||
* @param string $amount
|
* @param string $amount
|
||||||
|
* @return void
|
||||||
*/
|
*/
|
||||||
private function addAmount(array $journalArray, PiggyBank $piggyBank, string $amount): void
|
private function addAmount(PiggyBank $piggyBank, TransactionJournal $journal, string $amount): void
|
||||||
{
|
{
|
||||||
$user = User::find($journalArray['user_id']);
|
|
||||||
$journal = $user->transactionJournals()->find($journalArray['transaction_journal_id']);
|
|
||||||
$repository = app(PiggyBankRepositoryInterface::class);
|
$repository = app(PiggyBankRepositoryInterface::class);
|
||||||
$repository->setUser($journal->user);
|
$repository->setUser($journal->user);
|
||||||
|
|
||||||
@ -189,6 +190,6 @@ class UpdatePiggybank implements ActionInterface
|
|||||||
}
|
}
|
||||||
Log::debug(sprintf('Will now add %s to piggy bank.', $amount));
|
Log::debug(sprintf('Will now add %s to piggy bank.', $amount));
|
||||||
|
|
||||||
$repository->addAmount($piggyBank, $amount);
|
$repository->addAmount($piggyBank, $amount, $journal);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user