mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-12 15:35:15 +00:00
Code cleanup.
This commit is contained in:
@@ -31,10 +31,6 @@ interface ActionInterface
|
||||
/**
|
||||
* Execute the action on an array. Returns "true" if the action was a success and the action
|
||||
* was applied. Returns false if otherwise.
|
||||
*
|
||||
* @param array $journal
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function actOnArray(array $journal): bool;
|
||||
}
|
||||
|
@@ -23,7 +23,6 @@ declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\TransactionRules\Actions;
|
||||
|
||||
use DB;
|
||||
use FireflyIII\Events\Model\Rule\RuleActionFailedOnArray;
|
||||
use FireflyIII\Events\TriggeredAuditLog;
|
||||
use FireflyIII\Factory\TagFactory;
|
||||
@@ -40,22 +39,18 @@ class AddTag implements ActionInterface
|
||||
|
||||
/**
|
||||
* TriggerInterface constructor.
|
||||
*
|
||||
* @param RuleAction $action
|
||||
*/
|
||||
public function __construct(RuleAction $action)
|
||||
{
|
||||
$this->action = $action;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function actOnArray(array $journal): bool
|
||||
{
|
||||
// journal has this tag maybe?
|
||||
/** @var TagFactory $factory */
|
||||
$factory = app(TagFactory::class);
|
||||
|
||||
/** @var User $user */
|
||||
$user = User::find($journal['user_id']);
|
||||
$factory->setUser($user);
|
||||
@@ -68,19 +63,22 @@ class AddTag implements ActionInterface
|
||||
return false;
|
||||
}
|
||||
|
||||
$count = DB::table('tag_transaction_journal')
|
||||
->where('tag_id', $tag->id)
|
||||
->where('transaction_journal_id', $journal['transaction_journal_id'])
|
||||
->count();
|
||||
$count = \DB::table('tag_transaction_journal')
|
||||
->where('tag_id', $tag->id)
|
||||
->where('transaction_journal_id', $journal['transaction_journal_id'])
|
||||
->count()
|
||||
;
|
||||
if (0 === $count) {
|
||||
// add to journal:
|
||||
DB::table('tag_transaction_journal')->insert(['tag_id' => $tag->id, 'transaction_journal_id' => $journal['transaction_journal_id']]);
|
||||
\DB::table('tag_transaction_journal')->insert(['tag_id' => $tag->id, 'transaction_journal_id' => $journal['transaction_journal_id']]);
|
||||
app('log')->debug(sprintf('RuleAction AddTag. Added tag #%d ("%s") to journal %d.', $tag->id, $tag->tag, $journal['transaction_journal_id']));
|
||||
|
||||
/** @var TransactionJournal $object */
|
||||
$object = TransactionJournal::find($journal['transaction_journal_id']);
|
||||
|
||||
// event for audit log entry
|
||||
event(new TriggeredAuditLog($this->action->rule, $object, 'add_tag', null, $tag->tag));
|
||||
|
||||
return true;
|
||||
}
|
||||
app('log')->debug(
|
||||
|
@@ -23,7 +23,6 @@ declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\TransactionRules\Actions;
|
||||
|
||||
use DB;
|
||||
use FireflyIII\Events\TriggeredAuditLog;
|
||||
use FireflyIII\Models\RuleAction;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
@@ -37,21 +36,16 @@ class AppendDescription implements ActionInterface
|
||||
|
||||
/**
|
||||
* TriggerInterface constructor.
|
||||
*
|
||||
* @param RuleAction $action
|
||||
*/
|
||||
public function __construct(RuleAction $action)
|
||||
{
|
||||
$this->action = $action;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function actOnArray(array $journal): bool
|
||||
{
|
||||
$description = sprintf('%s %s', $journal['description'], $this->action->action_value);
|
||||
DB::table('transaction_journals')->where('id', $journal['transaction_journal_id'])->limit(1)->update(['description' => $description]);
|
||||
\DB::table('transaction_journals')->where('id', $journal['transaction_journal_id'])->limit(1)->update(['description' => $description]);
|
||||
|
||||
// event for audit log entry
|
||||
/** @var TransactionJournal $object */
|
||||
|
@@ -39,24 +39,20 @@ class AppendDescriptionToNotes implements ActionInterface
|
||||
|
||||
/**
|
||||
* TriggerInterface constructor.
|
||||
*
|
||||
* @param RuleAction $action
|
||||
*/
|
||||
public function __construct(RuleAction $action)
|
||||
{
|
||||
$this->action = $action;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function actOnArray(array $journal): bool
|
||||
{
|
||||
/** @var TransactionJournal|null $object */
|
||||
/** @var null|TransactionJournal $object */
|
||||
$object = TransactionJournal::where('user_id', $journal['user_id'])->find($journal['transaction_journal_id']);
|
||||
if (null === $object) {
|
||||
app('log')->error(sprintf('No journal #%d belongs to user #%d.', $journal['transaction_journal_id'], $journal['user_id']));
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, (string)trans('rules.journal_other_user')));
|
||||
|
||||
return false;
|
||||
}
|
||||
$note = $object->notes()->first();
|
||||
@@ -78,6 +74,7 @@ class AppendDescriptionToNotes implements ActionInterface
|
||||
event(new TriggeredAuditLog($this->action->rule, $object, 'update_notes', $before, $after));
|
||||
|
||||
$note->save();
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@@ -37,22 +37,18 @@ class AppendNotes implements ActionInterface
|
||||
|
||||
/**
|
||||
* TriggerInterface constructor.
|
||||
*
|
||||
* @param RuleAction $action
|
||||
*/
|
||||
public function __construct(RuleAction $action)
|
||||
{
|
||||
$this->action = $action;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function actOnArray(array $journal): bool
|
||||
{
|
||||
$dbNote = Note::where('noteable_id', (int)$journal['transaction_journal_id'])
|
||||
->where('noteable_type', TransactionJournal::class)
|
||||
->first(['notes.*']);
|
||||
->where('noteable_type', TransactionJournal::class)
|
||||
->first(['notes.*'])
|
||||
;
|
||||
if (null === $dbNote) {
|
||||
$dbNote = new Note();
|
||||
$dbNote->noteable_id = (int)$journal['transaction_journal_id'];
|
||||
|
@@ -42,25 +42,22 @@ class AppendNotesToDescription implements ActionInterface
|
||||
|
||||
/**
|
||||
* TriggerInterface constructor.
|
||||
*
|
||||
* @param RuleAction $action
|
||||
*/
|
||||
public function __construct(RuleAction $action)
|
||||
{
|
||||
$this->action = $action;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function actOnArray(array $journal): bool
|
||||
{
|
||||
app('log')->debug('Now in AppendNotesToDescription');
|
||||
/** @var TransactionJournal|null $object */
|
||||
|
||||
/** @var null|TransactionJournal $object */
|
||||
$object = TransactionJournal::where('user_id', $journal['user_id'])->find($journal['transaction_journal_id']);
|
||||
if (null === $object) {
|
||||
app('log')->error(sprintf('No journal #%d belongs to user #%d.', $journal['transaction_journal_id'], $journal['user_id']));
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.journal_other_user')));
|
||||
|
||||
return false;
|
||||
}
|
||||
$note = $object->notes()->first();
|
||||
@@ -82,20 +79,15 @@ class AppendNotesToDescription implements ActionInterface
|
||||
return true;
|
||||
}
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.new_notes_empty')));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function get(string $key, mixed $default = null): mixed
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function has(mixed $key): mixed
|
||||
{
|
||||
return null;
|
||||
|
@@ -23,7 +23,6 @@ declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\TransactionRules\Actions;
|
||||
|
||||
use DB;
|
||||
use FireflyIII\Events\Model\Rule\RuleActionFailedOnArray;
|
||||
use FireflyIII\Events\TriggeredAuditLog;
|
||||
use FireflyIII\Models\RuleAction;
|
||||
@@ -38,17 +37,12 @@ class ClearBudget implements ActionInterface
|
||||
|
||||
/**
|
||||
* TriggerInterface constructor.
|
||||
*
|
||||
* @param RuleAction $action
|
||||
*/
|
||||
public function __construct(RuleAction $action)
|
||||
{
|
||||
$this->action = $action;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function actOnArray(array $journal): bool
|
||||
{
|
||||
/** @var TransactionJournal $object */
|
||||
@@ -57,10 +51,11 @@ class ClearBudget implements ActionInterface
|
||||
if (null === $budget) {
|
||||
app('log')->debug(sprintf('RuleAction ClearBudget, no budget in journal #%d.', $journal['transaction_journal_id']));
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.journal_already_no_budget')));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
DB::table('budget_transaction_journal')->where('transaction_journal_id', '=', $journal['transaction_journal_id'])->delete();
|
||||
\DB::table('budget_transaction_journal')->where('transaction_journal_id', '=', $journal['transaction_journal_id'])->delete();
|
||||
|
||||
event(new TriggeredAuditLog($this->action->rule, $object, 'clear_budget', $budget->name, null));
|
||||
|
||||
|
@@ -23,7 +23,6 @@ declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\TransactionRules\Actions;
|
||||
|
||||
use DB;
|
||||
use FireflyIII\Events\Model\Rule\RuleActionFailedOnArray;
|
||||
use FireflyIII\Events\TriggeredAuditLog;
|
||||
use FireflyIII\Models\RuleAction;
|
||||
@@ -38,17 +37,12 @@ class ClearCategory implements ActionInterface
|
||||
|
||||
/**
|
||||
* TriggerInterface constructor.
|
||||
*
|
||||
* @param RuleAction $action
|
||||
*/
|
||||
public function __construct(RuleAction $action)
|
||||
{
|
||||
$this->action = $action;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function actOnArray(array $journal): bool
|
||||
{
|
||||
/** @var TransactionJournal $object */
|
||||
@@ -57,10 +51,11 @@ class ClearCategory implements ActionInterface
|
||||
if (null === $category) {
|
||||
app('log')->debug(sprintf('RuleAction ClearCategory, no category in journal #%d.', $journal['transaction_journal_id']));
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.journal_already_no_category')));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
DB::table('category_transaction_journal')->where('transaction_journal_id', '=', $journal['transaction_journal_id'])->delete();
|
||||
\DB::table('category_transaction_journal')->where('transaction_journal_id', '=', $journal['transaction_journal_id'])->delete();
|
||||
|
||||
event(new TriggeredAuditLog($this->action->rule, $object, 'clear_category', $category->name, null));
|
||||
|
||||
|
@@ -23,7 +23,6 @@ declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\TransactionRules\Actions;
|
||||
|
||||
use DB;
|
||||
use FireflyIII\Events\Model\Rule\RuleActionFailedOnArray;
|
||||
use FireflyIII\Events\TriggeredAuditLog;
|
||||
use FireflyIII\Models\Note;
|
||||
@@ -39,34 +38,32 @@ class ClearNotes implements ActionInterface
|
||||
|
||||
/**
|
||||
* TriggerInterface constructor.
|
||||
*
|
||||
* @param RuleAction $action
|
||||
*/
|
||||
public function __construct(RuleAction $action)
|
||||
{
|
||||
$this->action = $action;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function actOnArray(array $journal): bool
|
||||
{
|
||||
/** @var TransactionJournal $object */
|
||||
$object = TransactionJournal::where('user_id', $journal['user_id'])->find($journal['transaction_journal_id']);
|
||||
/** @var Note|null $notes */
|
||||
|
||||
/** @var null|Note $notes */
|
||||
$notes = $object->notes()->first();
|
||||
if (null === $notes) {
|
||||
app('log')->debug(sprintf('RuleAction ClearNotes, journal #%d has no notes.', $journal['transaction_journal_id']));
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.journal_already_no_notes')));
|
||||
|
||||
return false;
|
||||
}
|
||||
$before = $notes->text;
|
||||
|
||||
DB::table('notes')
|
||||
->where('noteable_id', $journal['transaction_journal_id'])
|
||||
->where('noteable_type', TransactionJournal::class)
|
||||
->delete();
|
||||
\DB::table('notes')
|
||||
->where('noteable_id', $journal['transaction_journal_id'])
|
||||
->where('noteable_type', TransactionJournal::class)
|
||||
->delete()
|
||||
;
|
||||
app('log')->debug(sprintf('RuleAction ClearNotes removed all notes from journal #%d.', $journal['transaction_journal_id']));
|
||||
|
||||
event(new TriggeredAuditLog($this->action->rule, $object, 'clear_notes', $before, null));
|
||||
|
@@ -23,7 +23,6 @@ declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\TransactionRules\Actions;
|
||||
|
||||
use DB;
|
||||
use FireflyIII\Events\Model\Rule\RuleActionFailedOnArray;
|
||||
use FireflyIII\Events\TriggeredAuditLog;
|
||||
use FireflyIII\Exceptions\FireflyException;
|
||||
@@ -35,10 +34,8 @@ use FireflyIII\Models\Transaction;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Models\TransactionType;
|
||||
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
||||
use JsonException;
|
||||
|
||||
/**
|
||||
*
|
||||
* Class ConvertToDeposit
|
||||
*/
|
||||
class ConvertToDeposit implements ActionInterface
|
||||
@@ -47,31 +44,28 @@ class ConvertToDeposit implements ActionInterface
|
||||
|
||||
/**
|
||||
* TriggerInterface constructor.
|
||||
*
|
||||
* @param RuleAction $action
|
||||
*/
|
||||
public function __construct(RuleAction $action)
|
||||
{
|
||||
$this->action = $action;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function actOnArray(array $journal): bool
|
||||
{
|
||||
// make object from array (so the data is fresh).
|
||||
/** @var TransactionJournal|null $object */
|
||||
/** @var null|TransactionJournal $object */
|
||||
$object = TransactionJournal::where('user_id', $journal['user_id'])->find($journal['transaction_journal_id']);
|
||||
if (null === $object) {
|
||||
app('log')->error(sprintf('Cannot find journal #%d, cannot convert to deposit.', $journal['transaction_journal_id']));
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.journal_not_found')));
|
||||
|
||||
return false;
|
||||
}
|
||||
$groupCount = TransactionJournal::where('transaction_group_id', $journal['transaction_group_id'])->count();
|
||||
if ($groupCount > 1) {
|
||||
app('log')->error(sprintf('Group #%d has more than one transaction in it, cannot convert to deposit.', $journal['transaction_group_id']));
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.split_group')));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -80,6 +74,7 @@ class ConvertToDeposit implements ActionInterface
|
||||
if (TransactionType::DEPOSIT === $type) {
|
||||
app('log')->error(sprintf('Journal #%d is already a deposit (rule #%d).', $journal['transaction_journal_id'], $this->action->rule_id));
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.is_already_deposit')));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -88,10 +83,11 @@ class ConvertToDeposit implements ActionInterface
|
||||
|
||||
try {
|
||||
$res = $this->convertWithdrawalArray($object);
|
||||
} catch (JsonException | FireflyException $e) {
|
||||
} catch (FireflyException|\JsonException $e) {
|
||||
app('log')->debug('Could not convert withdrawal to deposit.');
|
||||
app('log')->error($e->getMessage());
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.complex_error')));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -104,10 +100,11 @@ class ConvertToDeposit implements ActionInterface
|
||||
|
||||
try {
|
||||
$res = $this->convertTransferArray($object);
|
||||
} catch (JsonException | FireflyException $e) {
|
||||
} catch (FireflyException|\JsonException $e) {
|
||||
app('log')->debug('Could not convert transfer to deposit.');
|
||||
app('log')->error($e->getMessage());
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.complex_error')));
|
||||
|
||||
return false;
|
||||
}
|
||||
event(new TriggeredAuditLog($this->action->rule, $object, 'update_transaction_type', TransactionType::TRANSFER, TransactionType::DEPOSIT));
|
||||
@@ -115,6 +112,7 @@ class ConvertToDeposit implements ActionInterface
|
||||
return $res;
|
||||
}
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.unsupported_transaction_type_deposit', ['type' => $type])));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -122,15 +120,13 @@ class ConvertToDeposit implements ActionInterface
|
||||
* Input is a withdrawal from A to B
|
||||
* Is converted to a deposit from C to A.
|
||||
*
|
||||
* @param TransactionJournal $journal
|
||||
*
|
||||
* @return bool
|
||||
* @throws FireflyException
|
||||
* @throws JsonException
|
||||
* @throws \JsonException
|
||||
*/
|
||||
private function convertWithdrawalArray(TransactionJournal $journal): bool
|
||||
{
|
||||
$user = $journal->user;
|
||||
|
||||
// find or create revenue account.
|
||||
/** @var AccountFactory $factory */
|
||||
$factory = app(AccountFactory::class);
|
||||
@@ -155,23 +151,26 @@ class ConvertToDeposit implements ActionInterface
|
||||
app('log')->debug(sprintf('ConvertToDeposit. Action value is "%s", new opposing name is "%s"', $this->action->action_value, $opposingAccount->name));
|
||||
|
||||
// update the source transaction and put in the new revenue ID.
|
||||
DB::table('transactions')
|
||||
->where('transaction_journal_id', '=', $journal->id)
|
||||
->where('amount', '<', 0)
|
||||
->update(['account_id' => $opposingAccount->id]);
|
||||
\DB::table('transactions')
|
||||
->where('transaction_journal_id', '=', $journal->id)
|
||||
->where('amount', '<', 0)
|
||||
->update(['account_id' => $opposingAccount->id])
|
||||
;
|
||||
|
||||
// update the destination transaction and put in the original source account ID.
|
||||
DB::table('transactions')
|
||||
->where('transaction_journal_id', '=', $journal->id)
|
||||
->where('amount', '>', 0)
|
||||
->update(['account_id' => $sourceAccount->id]);
|
||||
\DB::table('transactions')
|
||||
->where('transaction_journal_id', '=', $journal->id)
|
||||
->where('amount', '>', 0)
|
||||
->update(['account_id' => $sourceAccount->id])
|
||||
;
|
||||
|
||||
// change transaction type of journal:
|
||||
$newType = TransactionType::whereType(TransactionType::DEPOSIT)->first();
|
||||
|
||||
DB::table('transaction_journals')
|
||||
->where('id', '=', $journal->id)
|
||||
->update(['transaction_type_id' => $newType->id, 'bill_id' => null]);
|
||||
\DB::table('transaction_journals')
|
||||
->where('id', '=', $journal->id)
|
||||
->update(['transaction_type_id' => $newType->id, 'bill_id' => null])
|
||||
;
|
||||
|
||||
app('log')->debug('Converted withdrawal to deposit.');
|
||||
|
||||
@@ -179,34 +178,30 @@ class ConvertToDeposit implements ActionInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionJournal $journal
|
||||
*
|
||||
* @return Account
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function getDestinationAccount(TransactionJournal $journal): Account
|
||||
{
|
||||
/** @var Transaction|null $destAccount */
|
||||
/** @var null|Transaction $destAccount */
|
||||
$destAccount = $journal->transactions()->where('amount', '>', 0)->first();
|
||||
if (null === $destAccount) {
|
||||
throw new FireflyException(sprintf('Cannot find destination transaction for journal #%d', $journal->id));
|
||||
}
|
||||
|
||||
return $destAccount->account;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionJournal $journal
|
||||
*
|
||||
* @return Account
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function getSourceAccount(TransactionJournal $journal): Account
|
||||
{
|
||||
/** @var Transaction|null $sourceTransaction */
|
||||
/** @var null|Transaction $sourceTransaction */
|
||||
$sourceTransaction = $journal->transactions()->where('amount', '<', 0)->first();
|
||||
if (null === $sourceTransaction) {
|
||||
throw new FireflyException(sprintf('Cannot find source transaction for journal #%d', $journal->id));
|
||||
}
|
||||
|
||||
return $sourceTransaction->account;
|
||||
}
|
||||
|
||||
@@ -215,15 +210,13 @@ class ConvertToDeposit implements ActionInterface
|
||||
* Output is a deposit from C to B.
|
||||
* The source account is replaced.
|
||||
*
|
||||
* @param TransactionJournal $journal
|
||||
*
|
||||
* @return bool
|
||||
* @throws FireflyException
|
||||
* @throws JsonException
|
||||
* @throws \JsonException
|
||||
*/
|
||||
private function convertTransferArray(TransactionJournal $journal): bool
|
||||
{
|
||||
$user = $journal->user;
|
||||
|
||||
// find or create revenue account.
|
||||
/** @var AccountFactory $factory */
|
||||
$factory = app(AccountFactory::class);
|
||||
@@ -247,17 +240,19 @@ class ConvertToDeposit implements ActionInterface
|
||||
app('log')->debug(sprintf('ConvertToDeposit. Action value is "%s", revenue name is "%s"', $this->action->action_value, $opposingAccount->name));
|
||||
|
||||
// update source transaction(s) to be revenue account
|
||||
DB::table('transactions')
|
||||
->where('transaction_journal_id', '=', $journal->id)
|
||||
->where('amount', '<', 0)
|
||||
->update(['account_id' => $opposingAccount->id]);
|
||||
\DB::table('transactions')
|
||||
->where('transaction_journal_id', '=', $journal->id)
|
||||
->where('amount', '<', 0)
|
||||
->update(['account_id' => $opposingAccount->id])
|
||||
;
|
||||
|
||||
// change transaction type of journal:
|
||||
$newType = TransactionType::whereType(TransactionType::DEPOSIT)->first();
|
||||
|
||||
DB::table('transaction_journals')
|
||||
->where('id', '=', $journal->id)
|
||||
->update(['transaction_type_id' => $newType->id, 'bill_id' => null]);
|
||||
\DB::table('transaction_journals')
|
||||
->where('id', '=', $journal->id)
|
||||
->update(['transaction_type_id' => $newType->id, 'bill_id' => null])
|
||||
;
|
||||
|
||||
app('log')->debug('Converted transfer to deposit.');
|
||||
|
||||
|
@@ -23,7 +23,6 @@ declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\TransactionRules\Actions;
|
||||
|
||||
use DB;
|
||||
use FireflyIII\Events\Model\Rule\RuleActionFailedOnArray;
|
||||
use FireflyIII\Events\Model\Rule\RuleActionFailedOnObject;
|
||||
use FireflyIII\Events\TriggeredAuditLog;
|
||||
@@ -36,7 +35,6 @@ use FireflyIII\Models\TransactionType;
|
||||
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
||||
|
||||
/**
|
||||
*
|
||||
* Class ConvertToTransfer
|
||||
*/
|
||||
class ConvertToTransfer implements ActionInterface
|
||||
@@ -45,31 +43,28 @@ class ConvertToTransfer implements ActionInterface
|
||||
|
||||
/**
|
||||
* TriggerInterface constructor.
|
||||
*
|
||||
* @param RuleAction $action
|
||||
*/
|
||||
public function __construct(RuleAction $action)
|
||||
{
|
||||
$this->action = $action;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function actOnArray(array $journal): bool
|
||||
{
|
||||
// make object from array (so the data is fresh).
|
||||
/** @var TransactionJournal|null $object */
|
||||
/** @var null|TransactionJournal $object */
|
||||
$object = TransactionJournal::where('user_id', $journal['user_id'])->find($journal['transaction_journal_id']);
|
||||
if (null === $object) {
|
||||
app('log')->error(sprintf('Cannot find journal #%d, cannot convert to transfer.', $journal['transaction_journal_id']));
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.journal_not_found')));
|
||||
|
||||
return false;
|
||||
}
|
||||
$groupCount = TransactionJournal::where('transaction_group_id', $journal['transaction_group_id'])->count();
|
||||
if ($groupCount > 1) {
|
||||
app('log')->error(sprintf('Group #%d has more than one transaction in it, cannot convert to transfer.', $journal['transaction_group_id']));
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.split_group')));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -86,6 +81,7 @@ class ConvertToTransfer implements ActionInterface
|
||||
}
|
||||
if (TransactionType::DEPOSIT !== $type && TransactionType::WITHDRAWAL !== $type) {
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.unsupported_transaction_type_transfer', ['type' => $type])));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -121,64 +117,64 @@ class ConvertToTransfer implements ActionInterface
|
||||
|
||||
if (TransactionType::WITHDRAWAL === $type) {
|
||||
app('log')->debug('Going to transform a withdrawal to a transfer.');
|
||||
|
||||
try {
|
||||
$res = $this->convertWithdrawalArray($object, $opposing);
|
||||
} catch (FireflyException $e) {
|
||||
app('log')->debug('Could not convert withdrawal to transfer.');
|
||||
app('log')->error($e->getMessage());
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.complex_error')));
|
||||
|
||||
return false;
|
||||
}
|
||||
if (false !== $res) {
|
||||
event(new TriggeredAuditLog($this->action->rule, $object, 'update_transaction_type', TransactionType::WITHDRAWAL, TransactionType::TRANSFER));
|
||||
}
|
||||
|
||||
return $res;
|
||||
}
|
||||
// can only be a deposit at this point.
|
||||
app('log')->debug('Going to transform a deposit to a transfer.');
|
||||
|
||||
try {
|
||||
$res = $this->convertDepositArray($object, $opposing);
|
||||
} catch (FireflyException $e) {
|
||||
app('log')->debug('Could not convert deposit to transfer.');
|
||||
app('log')->error($e->getMessage());
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.complex_error')));
|
||||
|
||||
return false;
|
||||
}
|
||||
if (false !== $res) {
|
||||
event(new TriggeredAuditLog($this->action->rule, $object, 'update_transaction_type', TransactionType::DEPOSIT, TransactionType::TRANSFER));
|
||||
}
|
||||
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $journalId
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function getSourceType(int $journalId): string
|
||||
{
|
||||
/** @var TransactionJournal|null $journal */
|
||||
/** @var null|TransactionJournal $journal */
|
||||
$journal = TransactionJournal::find($journalId);
|
||||
if (null === $journal) {
|
||||
app('log')->error(sprintf('Journal #%d does not exist. Cannot convert to transfer.', $journalId));
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
return (string)$journal->transactions()->where('amount', '<', 0)->first()?->account?->accountType?->type;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $journalId
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function getDestinationType(int $journalId): string
|
||||
{
|
||||
/** @var TransactionJournal|null $journal */
|
||||
/** @var null|TransactionJournal $journal */
|
||||
$journal = TransactionJournal::find($journalId);
|
||||
if (null === $journal) {
|
||||
app('log')->error(sprintf('Journal #%d does not exist. Cannot convert to transfer.', $journalId));
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
return (string)$journal->transactions()->where('amount', '>', 0)->first()?->account?->accountType?->type;
|
||||
}
|
||||
|
||||
@@ -187,10 +183,6 @@ class ConvertToTransfer implements ActionInterface
|
||||
* We replace the Expense with another asset.
|
||||
* So this replaces the destination
|
||||
*
|
||||
* @param TransactionJournal $journal
|
||||
* @param Account $opposing
|
||||
*
|
||||
* @return bool
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function convertWithdrawalArray(TransactionJournal $journal, Account $opposing): bool
|
||||
@@ -209,17 +201,19 @@ class ConvertToTransfer implements ActionInterface
|
||||
}
|
||||
|
||||
// update destination transaction:
|
||||
DB::table('transactions')
|
||||
->where('transaction_journal_id', '=', $journal->id)
|
||||
->where('amount', '>', 0)
|
||||
->update(['account_id' => $opposing->id]);
|
||||
\DB::table('transactions')
|
||||
->where('transaction_journal_id', '=', $journal->id)
|
||||
->where('amount', '>', 0)
|
||||
->update(['account_id' => $opposing->id])
|
||||
;
|
||||
|
||||
// change transaction type of journal:
|
||||
$newType = TransactionType::whereType(TransactionType::TRANSFER)->first();
|
||||
|
||||
DB::table('transaction_journals')
|
||||
->where('id', '=', $journal->id)
|
||||
->update(['transaction_type_id' => $newType->id, 'bill_id' => null]);
|
||||
\DB::table('transaction_journals')
|
||||
->where('id', '=', $journal->id)
|
||||
->update(['transaction_type_id' => $newType->id, 'bill_id' => null])
|
||||
;
|
||||
|
||||
app('log')->debug('Converted withdrawal to transfer.');
|
||||
|
||||
@@ -227,18 +221,16 @@ class ConvertToTransfer implements ActionInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionJournal $journal
|
||||
*
|
||||
* @return Account
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function getSourceAccount(TransactionJournal $journal): Account
|
||||
{
|
||||
/** @var Transaction|null $sourceTransaction */
|
||||
/** @var null|Transaction $sourceTransaction */
|
||||
$sourceTransaction = $journal->transactions()->where('amount', '<', 0)->first();
|
||||
if (null === $sourceTransaction) {
|
||||
throw new FireflyException(sprintf('Cannot find source transaction for journal #%d', $journal->id));
|
||||
}
|
||||
|
||||
return $sourceTransaction->account;
|
||||
}
|
||||
|
||||
@@ -246,10 +238,6 @@ class ConvertToTransfer implements ActionInterface
|
||||
* A deposit is from Revenue to Asset.
|
||||
* We replace the Revenue with another asset.
|
||||
*
|
||||
* @param TransactionJournal $journal
|
||||
* @param Account $opposing
|
||||
*
|
||||
* @return bool
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function convertDepositArray(TransactionJournal $journal, Account $opposing): bool
|
||||
@@ -268,17 +256,19 @@ class ConvertToTransfer implements ActionInterface
|
||||
}
|
||||
|
||||
// update source transaction:
|
||||
DB::table('transactions')
|
||||
->where('transaction_journal_id', '=', $journal->id)
|
||||
->where('amount', '<', 0)
|
||||
->update(['account_id' => $opposing->id]);
|
||||
\DB::table('transactions')
|
||||
->where('transaction_journal_id', '=', $journal->id)
|
||||
->where('amount', '<', 0)
|
||||
->update(['account_id' => $opposing->id])
|
||||
;
|
||||
|
||||
// change transaction type of journal:
|
||||
$newType = TransactionType::whereType(TransactionType::TRANSFER)->first();
|
||||
|
||||
DB::table('transaction_journals')
|
||||
->where('id', '=', $journal->id)
|
||||
->update(['transaction_type_id' => $newType->id, 'bill_id' => null]);
|
||||
\DB::table('transaction_journals')
|
||||
->where('id', '=', $journal->id)
|
||||
->update(['transaction_type_id' => $newType->id, 'bill_id' => null])
|
||||
;
|
||||
|
||||
app('log')->debug('Converted deposit to transfer.');
|
||||
|
||||
@@ -286,18 +276,16 @@ class ConvertToTransfer implements ActionInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionJournal $journal
|
||||
*
|
||||
* @return Account
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function getDestinationAccount(TransactionJournal $journal): Account
|
||||
{
|
||||
/** @var Transaction|null $destAccount */
|
||||
/** @var null|Transaction $destAccount */
|
||||
$destAccount = $journal->transactions()->where('amount', '>', 0)->first();
|
||||
if (null === $destAccount) {
|
||||
throw new FireflyException(sprintf('Cannot find destination transaction for journal #%d', $journal->id));
|
||||
}
|
||||
|
||||
return $destAccount->account;
|
||||
}
|
||||
}
|
||||
|
@@ -23,7 +23,6 @@ declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\TransactionRules\Actions;
|
||||
|
||||
use DB;
|
||||
use FireflyIII\Events\Model\Rule\RuleActionFailedOnArray;
|
||||
use FireflyIII\Events\TriggeredAuditLog;
|
||||
use FireflyIII\Exceptions\FireflyException;
|
||||
@@ -35,10 +34,8 @@ use FireflyIII\Models\Transaction;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Models\TransactionType;
|
||||
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
||||
use JsonException;
|
||||
|
||||
/**
|
||||
*
|
||||
* Class ConvertToWithdrawal
|
||||
*/
|
||||
class ConvertToWithdrawal implements ActionInterface
|
||||
@@ -47,31 +44,28 @@ class ConvertToWithdrawal implements ActionInterface
|
||||
|
||||
/**
|
||||
* TriggerInterface constructor.
|
||||
*
|
||||
* @param RuleAction $action
|
||||
*/
|
||||
public function __construct(RuleAction $action)
|
||||
{
|
||||
$this->action = $action;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function actOnArray(array $journal): bool
|
||||
{
|
||||
// make object from array (so the data is fresh).
|
||||
/** @var TransactionJournal|null $object */
|
||||
/** @var null|TransactionJournal $object */
|
||||
$object = TransactionJournal::where('user_id', $journal['user_id'])->find($journal['transaction_journal_id']);
|
||||
if (null === $object) {
|
||||
app('log')->error(sprintf('Cannot find journal #%d, cannot convert to withdrawal.', $journal['transaction_journal_id']));
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.journal_not_found')));
|
||||
|
||||
return false;
|
||||
}
|
||||
$groupCount = TransactionJournal::where('transaction_group_id', $journal['transaction_group_id'])->count();
|
||||
if ($groupCount > 1) {
|
||||
app('log')->error(sprintf('Group #%d has more than one transaction in it, cannot convert to withdrawal.', $journal['transaction_group_id']));
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.split_group')));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -79,20 +73,24 @@ class ConvertToWithdrawal implements ActionInterface
|
||||
if (TransactionType::WITHDRAWAL === $type) {
|
||||
app('log')->error(sprintf('Journal #%d is already a withdrawal (rule #%d).', $journal['transaction_journal_id'], $this->action->rule_id));
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.is_already_withdrawal')));
|
||||
|
||||
return false;
|
||||
}
|
||||
if (TransactionType::DEPOSIT !== $type && TransactionType::TRANSFER !== $type) {
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.unsupported_transaction_type_withdrawal', ['type' => $type])));
|
||||
|
||||
return false;
|
||||
}
|
||||
if (TransactionType::DEPOSIT === $type) {
|
||||
app('log')->debug('Going to transform a deposit to a withdrawal.');
|
||||
|
||||
try {
|
||||
$res = $this->convertDepositArray($object);
|
||||
} catch (JsonException | FireflyException $e) {
|
||||
} catch (FireflyException|\JsonException $e) {
|
||||
app('log')->debug('Could not convert transfer to deposit.');
|
||||
app('log')->error($e->getMessage());
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.complex_error')));
|
||||
|
||||
return false;
|
||||
}
|
||||
event(new TriggeredAuditLog($this->action->rule, $object, 'update_transaction_type', TransactionType::DEPOSIT, TransactionType::WITHDRAWAL));
|
||||
@@ -104,10 +102,11 @@ class ConvertToWithdrawal implements ActionInterface
|
||||
|
||||
try {
|
||||
$res = $this->convertTransferArray($object);
|
||||
} catch (JsonException | FireflyException $e) {
|
||||
} catch (FireflyException|\JsonException $e) {
|
||||
app('log')->debug('Could not convert transfer to deposit.');
|
||||
app('log')->error($e->getMessage());
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.complex_error')));
|
||||
|
||||
return false;
|
||||
}
|
||||
event(new TriggeredAuditLog($this->action->rule, $object, 'update_transaction_type', TransactionType::TRANSFER, TransactionType::WITHDRAWAL));
|
||||
@@ -116,15 +115,13 @@ class ConvertToWithdrawal implements ActionInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionJournal $journal
|
||||
*
|
||||
* @return bool
|
||||
* @throws FireflyException
|
||||
* @throws JsonException
|
||||
* @throws \JsonException
|
||||
*/
|
||||
private function convertDepositArray(TransactionJournal $journal): bool
|
||||
{
|
||||
$user = $journal->user;
|
||||
|
||||
/** @var AccountFactory $factory */
|
||||
$factory = app(AccountFactory::class);
|
||||
$factory->setUser($user);
|
||||
@@ -148,22 +145,25 @@ class ConvertToWithdrawal implements ActionInterface
|
||||
app('log')->debug(sprintf('ConvertToWithdrawal. Action value is "%s", expense name is "%s"', $this->action->action_value, $opposingName));
|
||||
|
||||
// update source transaction(s) to be the original destination account
|
||||
DB::table('transactions')
|
||||
->where('transaction_journal_id', '=', $journal->id)
|
||||
->where('amount', '<', 0)
|
||||
->update(['account_id' => $destAccount->id]);
|
||||
\DB::table('transactions')
|
||||
->where('transaction_journal_id', '=', $journal->id)
|
||||
->where('amount', '<', 0)
|
||||
->update(['account_id' => $destAccount->id])
|
||||
;
|
||||
|
||||
// update destination transaction(s) to be new expense account.
|
||||
DB::table('transactions')
|
||||
->where('transaction_journal_id', '=', $journal->id)
|
||||
->where('amount', '>', 0)
|
||||
->update(['account_id' => $opposingAccount->id]);
|
||||
\DB::table('transactions')
|
||||
->where('transaction_journal_id', '=', $journal->id)
|
||||
->where('amount', '>', 0)
|
||||
->update(['account_id' => $opposingAccount->id])
|
||||
;
|
||||
|
||||
// change transaction type of journal:
|
||||
$newType = TransactionType::whereType(TransactionType::WITHDRAWAL)->first();
|
||||
DB::table('transaction_journals')
|
||||
->where('id', '=', $journal->id)
|
||||
->update(['transaction_type_id' => $newType->id]);
|
||||
\DB::table('transaction_journals')
|
||||
->where('id', '=', $journal->id)
|
||||
->update(['transaction_type_id' => $newType->id])
|
||||
;
|
||||
|
||||
app('log')->debug('Converted deposit to withdrawal.');
|
||||
|
||||
@@ -171,34 +171,30 @@ class ConvertToWithdrawal implements ActionInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionJournal $journal
|
||||
*
|
||||
* @return Account
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function getSourceAccount(TransactionJournal $journal): Account
|
||||
{
|
||||
/** @var Transaction|null $sourceTransaction */
|
||||
/** @var null|Transaction $sourceTransaction */
|
||||
$sourceTransaction = $journal->transactions()->where('amount', '<', 0)->first();
|
||||
if (null === $sourceTransaction) {
|
||||
throw new FireflyException(sprintf('Cannot find source transaction for journal #%d', $journal->id));
|
||||
}
|
||||
|
||||
return $sourceTransaction->account;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionJournal $journal
|
||||
*
|
||||
* @return Account
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function getDestinationAccount(TransactionJournal $journal): Account
|
||||
{
|
||||
/** @var Transaction|null $destAccount */
|
||||
/** @var null|Transaction $destAccount */
|
||||
$destAccount = $journal->transactions()->where('amount', '>', 0)->first();
|
||||
if (null === $destAccount) {
|
||||
throw new FireflyException(sprintf('Cannot find destination transaction for journal #%d', $journal->id));
|
||||
}
|
||||
|
||||
return $destAccount->account;
|
||||
}
|
||||
|
||||
@@ -206,16 +202,14 @@ class ConvertToWithdrawal implements ActionInterface
|
||||
* Input is a transfer from A to B.
|
||||
* Output is a withdrawal from A to C.
|
||||
*
|
||||
* @param TransactionJournal $journal
|
||||
*
|
||||
* @return bool
|
||||
* @throws FireflyException
|
||||
* @throws JsonException
|
||||
* @throws \JsonException
|
||||
*/
|
||||
private function convertTransferArray(TransactionJournal $journal): bool
|
||||
{
|
||||
// find or create expense account.
|
||||
$user = $journal->user;
|
||||
|
||||
/** @var AccountFactory $factory */
|
||||
$factory = app(AccountFactory::class);
|
||||
$factory->setUser($user);
|
||||
@@ -238,16 +232,18 @@ class ConvertToWithdrawal implements ActionInterface
|
||||
app('log')->debug(sprintf('ConvertToWithdrawal. Action value is "%s", destination name is "%s"', $this->action->action_value, $opposingName));
|
||||
|
||||
// update destination transaction(s) to be new expense account.
|
||||
DB::table('transactions')
|
||||
->where('transaction_journal_id', '=', $journal->id)
|
||||
->where('amount', '>', 0)
|
||||
->update(['account_id' => $opposingAccount->id]);
|
||||
\DB::table('transactions')
|
||||
->where('transaction_journal_id', '=', $journal->id)
|
||||
->where('amount', '>', 0)
|
||||
->update(['account_id' => $opposingAccount->id])
|
||||
;
|
||||
|
||||
// change transaction type of journal:
|
||||
$newType = TransactionType::whereType(TransactionType::WITHDRAWAL)->first();
|
||||
DB::table('transaction_journals')
|
||||
->where('id', '=', $journal->id)
|
||||
->update(['transaction_type_id' => $newType->id]);
|
||||
\DB::table('transaction_journals')
|
||||
->where('id', '=', $journal->id)
|
||||
->update(['transaction_type_id' => $newType->id])
|
||||
;
|
||||
|
||||
app('log')->debug('Converted transfer to withdrawal.');
|
||||
|
||||
|
@@ -39,17 +39,12 @@ class DeleteTransaction implements ActionInterface
|
||||
|
||||
/**
|
||||
* TriggerInterface constructor.
|
||||
*
|
||||
* @param RuleAction $action
|
||||
*/
|
||||
public function __construct(RuleAction $action)
|
||||
{
|
||||
$this->action = $action;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function actOnArray(array $journal): bool
|
||||
{
|
||||
$count = TransactionJournal::where('transaction_group_id', $journal['transaction_group_id'])->count();
|
||||
@@ -63,6 +58,7 @@ class DeleteTransaction implements ActionInterface
|
||||
$journal['description']
|
||||
)
|
||||
);
|
||||
|
||||
/** @var TransactionGroup $group */
|
||||
$group = TransactionGroup::find($journal['transaction_group_id']);
|
||||
$service = app(TransactionGroupDestroyService::class);
|
||||
@@ -77,7 +73,7 @@ class DeleteTransaction implements ActionInterface
|
||||
);
|
||||
|
||||
// trigger delete factory:
|
||||
/** @var TransactionJournal|null $object */
|
||||
/** @var null|TransactionJournal $object */
|
||||
$object = TransactionJournal::find($journal['transaction_journal_id']);
|
||||
if (null !== $object) {
|
||||
/** @var JournalDestroyService $service */
|
||||
|
@@ -23,7 +23,6 @@ declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\TransactionRules\Actions;
|
||||
|
||||
use DB;
|
||||
use FireflyIII\Events\Model\Rule\RuleActionFailedOnArray;
|
||||
use FireflyIII\Events\TriggeredAuditLog;
|
||||
use FireflyIII\Models\RuleAction;
|
||||
@@ -41,31 +40,27 @@ class LinkToBill implements ActionInterface
|
||||
|
||||
/**
|
||||
* TriggerInterface constructor.
|
||||
*
|
||||
*
|
||||
* @param RuleAction $action
|
||||
*/
|
||||
public function __construct(RuleAction $action)
|
||||
{
|
||||
$this->action = $action;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function actOnArray(array $journal): bool
|
||||
{
|
||||
/** @var User $user */
|
||||
$user = User::find($journal['user_id']);
|
||||
|
||||
/** @var BillRepositoryInterface $repository */
|
||||
$repository = app(BillRepositoryInterface::class);
|
||||
$repository->setUser($user);
|
||||
$billName = (string)$this->action->action_value;
|
||||
$bill = $repository->findByName($billName);
|
||||
|
||||
if (null !== $bill && $journal['transaction_type_type'] === TransactionType::WITHDRAWAL) {
|
||||
$count = DB::table('transaction_journals')->where('id', '=', $journal['transaction_journal_id'])
|
||||
->where('bill_id', $bill->id)->count();
|
||||
if (null !== $bill && TransactionType::WITHDRAWAL === $journal['transaction_type_type']) {
|
||||
$count = \DB::table('transaction_journals')->where('id', '=', $journal['transaction_journal_id'])
|
||||
->where('bill_id', $bill->id)->count()
|
||||
;
|
||||
if (0 !== $count) {
|
||||
app('log')->error(
|
||||
sprintf(
|
||||
@@ -75,13 +70,14 @@ class LinkToBill implements ActionInterface
|
||||
)
|
||||
);
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.already_linked_to_subscription', ['name' => $billName])));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
DB::table('transaction_journals')
|
||||
->where('id', '=', $journal['transaction_journal_id'])
|
||||
->update(['bill_id' => $bill->id]);
|
||||
\DB::table('transaction_journals')
|
||||
->where('id', '=', $journal['transaction_journal_id'])
|
||||
->update(['bill_id' => $bill->id])
|
||||
;
|
||||
app('log')->debug(
|
||||
sprintf('RuleAction LinkToBill set the bill of journal #%d to bill #%d ("%s").', $journal['transaction_journal_id'], $bill->id, $bill->name)
|
||||
);
|
||||
@@ -101,6 +97,7 @@ class LinkToBill implements ActionInterface
|
||||
)
|
||||
);
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.cannot_find_subscription', ['name' => $billName])));
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@@ -39,28 +39,24 @@ class MoveDescriptionToNotes implements ActionInterface
|
||||
|
||||
/**
|
||||
* TriggerInterface constructor.
|
||||
*
|
||||
*
|
||||
* @param RuleAction $action
|
||||
*/
|
||||
public function __construct(RuleAction $action)
|
||||
{
|
||||
$this->action = $action;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function actOnArray(array $journal): bool
|
||||
{
|
||||
/** @var TransactionJournal|null $object */
|
||||
/** @var null|TransactionJournal $object */
|
||||
$object = TransactionJournal::where('user_id', $journal['user_id'])->find($journal['transaction_journal_id']);
|
||||
if (null === $object) {
|
||||
app('log')->error(sprintf('No journal #%d belongs to user #%d.', $journal['transaction_journal_id'], $journal['user_id']));
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.journal_other_user')));
|
||||
|
||||
return false;
|
||||
}
|
||||
/** @var Note|null $note */
|
||||
|
||||
/** @var null|Note $note */
|
||||
$note = $object->notes()->first();
|
||||
if (null === $note) {
|
||||
$note = new Note();
|
||||
@@ -84,6 +80,7 @@ class MoveDescriptionToNotes implements ActionInterface
|
||||
|
||||
$note->save();
|
||||
$object->save();
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@@ -45,30 +45,26 @@ class MoveNotesToDescription implements ActionInterface
|
||||
|
||||
/**
|
||||
* TriggerInterface constructor.
|
||||
*
|
||||
*
|
||||
* @param RuleAction $action
|
||||
*/
|
||||
public function __construct(RuleAction $action)
|
||||
{
|
||||
$this->action = $action;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function actOnArray(array $journal): bool
|
||||
{
|
||||
/** @var TransactionJournal|null $object */
|
||||
/** @var null|TransactionJournal $object */
|
||||
$object = TransactionJournal::where('user_id', $journal['user_id'])->find($journal['transaction_journal_id']);
|
||||
if (null === $object) {
|
||||
app('log')->error(sprintf('No journal #%d belongs to user #%d.', $journal['transaction_journal_id'], $journal['user_id']));
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.journal_other_user')));
|
||||
|
||||
return false;
|
||||
}
|
||||
$note = $object->notes()->first();
|
||||
if (null === $note) {
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.no_notes_to_move')));
|
||||
|
||||
// nothing to move, return null
|
||||
return false;
|
||||
}
|
||||
@@ -76,6 +72,7 @@ class MoveNotesToDescription implements ActionInterface
|
||||
// nothing to move, return null
|
||||
$note->delete();
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.no_notes_to_move')));
|
||||
|
||||
return false;
|
||||
}
|
||||
$before = $object->description;
|
||||
@@ -90,17 +87,11 @@ class MoveNotesToDescription implements ActionInterface
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function get(string $key, mixed $default = null): mixed
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function has(mixed $key): mixed
|
||||
{
|
||||
return null;
|
||||
|
@@ -23,7 +23,6 @@ declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\TransactionRules\Actions;
|
||||
|
||||
use DB;
|
||||
use FireflyIII\Events\TriggeredAuditLog;
|
||||
use FireflyIII\Models\RuleAction;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
@@ -37,22 +36,17 @@ class PrependDescription implements ActionInterface
|
||||
|
||||
/**
|
||||
* TriggerInterface constructor.
|
||||
*
|
||||
* @param RuleAction $action
|
||||
*/
|
||||
public function __construct(RuleAction $action)
|
||||
{
|
||||
$this->action = $action;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function actOnArray(array $journal): bool
|
||||
{
|
||||
$before = $journal['description'];
|
||||
$after = sprintf('%s%s', $this->action->action_value, $journal['description']);
|
||||
DB::table('transaction_journals')->where('id', $journal['transaction_journal_id'])->limit(1)->update(['description' => $after]);
|
||||
\DB::table('transaction_journals')->where('id', $journal['transaction_journal_id'])->limit(1)->update(['description' => $after]);
|
||||
|
||||
// journal
|
||||
/** @var TransactionJournal $object */
|
||||
|
@@ -37,22 +37,18 @@ class PrependNotes implements ActionInterface
|
||||
|
||||
/**
|
||||
* TriggerInterface constructor.
|
||||
*
|
||||
* @param RuleAction $action
|
||||
*/
|
||||
public function __construct(RuleAction $action)
|
||||
{
|
||||
$this->action = $action;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function actOnArray(array $journal): bool
|
||||
{
|
||||
$dbNote = Note::where('noteable_id', (int)$journal['transaction_journal_id'])
|
||||
->where('noteable_type', TransactionJournal::class)
|
||||
->first(['notes.*']);
|
||||
->where('noteable_type', TransactionJournal::class)
|
||||
->first(['notes.*'])
|
||||
;
|
||||
if (null === $dbNote) {
|
||||
$dbNote = new Note();
|
||||
$dbNote->noteable_id = (int)$journal['transaction_journal_id'];
|
||||
|
@@ -23,7 +23,6 @@ declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\TransactionRules\Actions;
|
||||
|
||||
use DB;
|
||||
use FireflyIII\Events\Model\Rule\RuleActionFailedOnArray;
|
||||
use FireflyIII\Events\TriggeredAuditLog;
|
||||
use FireflyIII\Models\RuleAction;
|
||||
@@ -38,24 +37,20 @@ class RemoveAllTags implements ActionInterface
|
||||
|
||||
/**
|
||||
* TriggerInterface constructor.
|
||||
*
|
||||
* @param RuleAction $action
|
||||
*/
|
||||
public function __construct(RuleAction $action)
|
||||
{
|
||||
$this->action = $action;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function actOnArray(array $journal): bool
|
||||
{
|
||||
DB::table('tag_transaction_journal')->where('transaction_journal_id', $journal['transaction_journal_id'])->delete();
|
||||
$count = DB::table('tag_transaction_journal')->where('transaction_journal_id', $journal['transaction_journal_id'])->count();
|
||||
\DB::table('tag_transaction_journal')->where('transaction_journal_id', $journal['transaction_journal_id'])->delete();
|
||||
$count = \DB::table('tag_transaction_journal')->where('transaction_journal_id', $journal['transaction_journal_id'])->count();
|
||||
if (0 === $count) {
|
||||
app('log')->debug(sprintf('RuleAction RemoveAllTags, journal #%d has no tags.', $journal['transaction_journal_id']));
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.no_tags_to_remove')));
|
||||
|
||||
return false;
|
||||
}
|
||||
app('log')->debug(sprintf('RuleAction RemoveAllTags removed all tags from journal %d.', $journal['transaction_journal_id']));
|
||||
|
@@ -23,7 +23,6 @@ declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\TransactionRules\Actions;
|
||||
|
||||
use DB;
|
||||
use FireflyIII\Events\Model\Rule\RuleActionFailedOnArray;
|
||||
use FireflyIII\Events\TriggeredAuditLog;
|
||||
use FireflyIII\Models\RuleAction;
|
||||
@@ -39,21 +38,17 @@ class RemoveTag implements ActionInterface
|
||||
|
||||
/**
|
||||
* TriggerInterface constructor.
|
||||
*
|
||||
* @param RuleAction $action
|
||||
*/
|
||||
public function __construct(RuleAction $action)
|
||||
{
|
||||
$this->action = $action;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function actOnArray(array $journal): bool
|
||||
{
|
||||
// if tag does not exist, no need to continue:
|
||||
$name = $this->action->action_value;
|
||||
|
||||
/** @var User $user */
|
||||
$user = User::find($journal['user_id']);
|
||||
$tag = $user->tags()->where('tag', $name)->first();
|
||||
@@ -63,22 +58,25 @@ class RemoveTag implements ActionInterface
|
||||
sprintf('RuleAction RemoveTag tried to remove tag "%s" from journal #%d but no such tag exists.', $name, $journal['transaction_journal_id'])
|
||||
);
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.cannot_find_tag', ['tag' => $name])));
|
||||
|
||||
return false;
|
||||
}
|
||||
$count = DB::table('tag_transaction_journal')->where('transaction_journal_id', $journal['transaction_journal_id'])->where('tag_id', $tag->id)->count();
|
||||
$count = \DB::table('tag_transaction_journal')->where('transaction_journal_id', $journal['transaction_journal_id'])->where('tag_id', $tag->id)->count();
|
||||
if (0 === $count) {
|
||||
app('log')->debug(
|
||||
sprintf('RuleAction RemoveTag tried to remove tag "%s" from journal #%d but no such tag is linked.', $name, $journal['transaction_journal_id'])
|
||||
);
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.cannot_unlink_tag', ['tag' => $name])));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
app('log')->debug(sprintf('RuleAction RemoveTag removed tag #%d ("%s") from journal #%d.', $tag->id, $tag->tag, $journal['transaction_journal_id']));
|
||||
DB::table('tag_transaction_journal')
|
||||
->where('transaction_journal_id', $journal['transaction_journal_id'])
|
||||
->where('tag_id', $tag->id)
|
||||
->delete();
|
||||
\DB::table('tag_transaction_journal')
|
||||
->where('transaction_journal_id', $journal['transaction_journal_id'])
|
||||
->where('tag_id', $tag->id)
|
||||
->delete()
|
||||
;
|
||||
|
||||
/** @var TransactionJournal $object */
|
||||
$object = TransactionJournal::where('user_id', $journal['user_id'])->find($journal['transaction_journal_id']);
|
||||
|
@@ -23,7 +23,6 @@ declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\TransactionRules\Actions;
|
||||
|
||||
use DB;
|
||||
use FireflyIII\Events\Model\Rule\RuleActionFailedOnArray;
|
||||
use FireflyIII\Events\TriggeredAuditLog;
|
||||
use FireflyIII\Models\RuleAction;
|
||||
@@ -40,17 +39,12 @@ class SetBudget implements ActionInterface
|
||||
|
||||
/**
|
||||
* TriggerInterface constructor.
|
||||
*
|
||||
* @param RuleAction $action
|
||||
*/
|
||||
public function __construct(RuleAction $action)
|
||||
{
|
||||
$this->action = $action;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function actOnArray(array $journal): bool
|
||||
{
|
||||
/** @var User $user */
|
||||
@@ -67,6 +61,7 @@ class SetBudget implements ActionInterface
|
||||
)
|
||||
);
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.cannot_find_budget', ['name' => $search])));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -80,6 +75,7 @@ class SetBudget implements ActionInterface
|
||||
)
|
||||
);
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.cannot_set_budget', ['type' => $journal['transaction_type_type'], 'name' => $search])));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -90,16 +86,16 @@ class SetBudget implements ActionInterface
|
||||
$oldBudgetName = $oldBudget?->name;
|
||||
if ((int)$oldBudget?->id === $budget->id) {
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.already_linked_to_budget', ['name' => $budget->name])));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
app('log')->debug(
|
||||
sprintf('RuleAction SetBudget set the budget of journal #%d to budget #%d ("%s").', $journal['transaction_journal_id'], $budget->id, $budget->name)
|
||||
);
|
||||
|
||||
DB::table('budget_transaction_journal')->where('transaction_journal_id', '=', $journal['transaction_journal_id'])->delete();
|
||||
DB::table('budget_transaction_journal')->insert(['transaction_journal_id' => $journal['transaction_journal_id'], 'budget_id' => $budget->id]);
|
||||
\DB::table('budget_transaction_journal')->where('transaction_journal_id', '=', $journal['transaction_journal_id'])->delete();
|
||||
\DB::table('budget_transaction_journal')->insert(['transaction_journal_id' => $journal['transaction_journal_id'], 'budget_id' => $budget->id]);
|
||||
|
||||
/** @var TransactionJournal $object */
|
||||
$object = TransactionJournal::where('user_id', $journal['user_id'])->find($journal['transaction_journal_id']);
|
||||
|
@@ -23,7 +23,6 @@ declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\TransactionRules\Actions;
|
||||
|
||||
use DB;
|
||||
use FireflyIII\Events\Model\Rule\RuleActionFailedOnArray;
|
||||
use FireflyIII\Events\TriggeredAuditLog;
|
||||
use FireflyIII\Factory\CategoryFactory;
|
||||
@@ -40,25 +39,21 @@ class SetCategory implements ActionInterface
|
||||
|
||||
/**
|
||||
* TriggerInterface constructor.
|
||||
*
|
||||
* @param RuleAction $action
|
||||
*/
|
||||
public function __construct(RuleAction $action)
|
||||
{
|
||||
$this->action = $action;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function actOnArray(array $journal): bool
|
||||
{
|
||||
/** @var User|null $user */
|
||||
/** @var null|User $user */
|
||||
$user = User::find($journal['user_id']);
|
||||
$search = $this->action->action_value;
|
||||
if (null === $user) {
|
||||
app('log')->error(sprintf('Journal has no valid user ID so action SetCategory("%s") cannot be applied', $search), $journal);
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.no_such_journal')));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -75,6 +70,7 @@ class SetCategory implements ActionInterface
|
||||
)
|
||||
);
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.cannot_find_category', ['name' => $search])));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -94,11 +90,12 @@ class SetCategory implements ActionInterface
|
||||
$oldCategoryName = $oldCategory?->name;
|
||||
if ((int)$oldCategory?->id === $category->id) {
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.already_linked_to_category', ['name' => $category->name])));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
DB::table('category_transaction_journal')->where('transaction_journal_id', '=', $journal['transaction_journal_id'])->delete();
|
||||
DB::table('category_transaction_journal')->insert(['transaction_journal_id' => $journal['transaction_journal_id'], 'category_id' => $category->id]);
|
||||
\DB::table('category_transaction_journal')->where('transaction_journal_id', '=', $journal['transaction_journal_id'])->delete();
|
||||
\DB::table('category_transaction_journal')->insert(['transaction_journal_id' => $journal['transaction_journal_id'], 'category_id' => $category->id]);
|
||||
|
||||
/** @var TransactionJournal $object */
|
||||
$object = TransactionJournal::where('user_id', $journal['user_id'])->find($journal['transaction_journal_id']);
|
||||
|
@@ -23,7 +23,6 @@ declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\TransactionRules\Actions;
|
||||
|
||||
use DB;
|
||||
use FireflyIII\Events\TriggeredAuditLog;
|
||||
use FireflyIII\Models\RuleAction;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
@@ -37,26 +36,22 @@ class SetDescription implements ActionInterface
|
||||
|
||||
/**
|
||||
* TriggerInterface constructor.
|
||||
*
|
||||
* @param RuleAction $action
|
||||
*/
|
||||
public function __construct(RuleAction $action)
|
||||
{
|
||||
$this->action = $action;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function actOnArray(array $journal): bool
|
||||
{
|
||||
/** @var TransactionJournal $object */
|
||||
$object = TransactionJournal::where('user_id', $journal['user_id'])->find($journal['transaction_journal_id']);
|
||||
$before = $object->description;
|
||||
|
||||
DB::table('transaction_journals')
|
||||
->where('id', '=', $journal['transaction_journal_id'])
|
||||
->update(['description' => $this->action->action_value]);
|
||||
\DB::table('transaction_journals')
|
||||
->where('id', '=', $journal['transaction_journal_id'])
|
||||
->update(['description' => $this->action->action_value])
|
||||
;
|
||||
|
||||
app('log')->debug(
|
||||
sprintf(
|
||||
|
@@ -23,7 +23,6 @@ declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\TransactionRules\Actions;
|
||||
|
||||
use DB;
|
||||
use FireflyIII\Events\Model\Rule\RuleActionFailedOnArray;
|
||||
use FireflyIII\Events\TriggeredAuditLog;
|
||||
use FireflyIII\Models\Account;
|
||||
@@ -44,28 +43,25 @@ class SetDestinationAccount implements ActionInterface
|
||||
|
||||
/**
|
||||
* TriggerInterface constructor.
|
||||
*
|
||||
* @param RuleAction $action
|
||||
*/
|
||||
public function __construct(RuleAction $action)
|
||||
{
|
||||
$this->action = $action;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function actOnArray(array $journal): bool
|
||||
{
|
||||
/** @var User $user */
|
||||
$user = User::find($journal['user_id']);
|
||||
/** @var TransactionJournal|null $object */
|
||||
|
||||
/** @var null|TransactionJournal $object */
|
||||
$object = $user->transactionJournals()->find((int)$journal['transaction_journal_id']);
|
||||
$this->repository = app(AccountRepositoryInterface::class);
|
||||
|
||||
if (null === $object) {
|
||||
app('log')->error('Could not find journal.');
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.no_such_journal')));
|
||||
|
||||
return false;
|
||||
}
|
||||
$type = $object->transactionType->type;
|
||||
@@ -82,21 +78,24 @@ class SetDestinationAccount implements ActionInterface
|
||||
)
|
||||
);
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.cannot_find_asset', ['name' => $this->action->action_value])));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// new destination account must be different from the current source account:
|
||||
/** @var Transaction|null $source */
|
||||
/** @var null|Transaction $source */
|
||||
$source = $object->transactions()->where('amount', '<', 0)->first();
|
||||
if (null === $source) {
|
||||
app('log')->error('Could not find source transaction.');
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.cannot_find_source_transaction')));
|
||||
|
||||
return false;
|
||||
}
|
||||
// account must not be deleted (in the meantime):
|
||||
if (null === $source->account) {
|
||||
app('log')->error('Could not find source transaction account.');
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.cannot_find_source_transaction_account')));
|
||||
|
||||
return false;
|
||||
}
|
||||
if (null !== $newAccount && $newAccount->id === $source->account_id) {
|
||||
@@ -109,6 +108,7 @@ class SetDestinationAccount implements ActionInterface
|
||||
);
|
||||
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.already_has_destination', ['name' => $newAccount->name])));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -123,21 +123,17 @@ class SetDestinationAccount implements ActionInterface
|
||||
event(new TriggeredAuditLog($this->action->rule, $object, 'set_destination', null, $newAccount->name));
|
||||
|
||||
// update destination transaction with new destination account:
|
||||
DB::table('transactions')
|
||||
->where('transaction_journal_id', '=', $object->id)
|
||||
->where('amount', '>', 0)
|
||||
->update(['account_id' => $newAccount->id]);
|
||||
\DB::table('transactions')
|
||||
->where('transaction_journal_id', '=', $object->id)
|
||||
->where('amount', '>', 0)
|
||||
->update(['account_id' => $newAccount->id])
|
||||
;
|
||||
|
||||
app('log')->debug(sprintf('Updated journal #%d (group #%d) and gave it new destination account ID.', $object->id, $object->transaction_group_id));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $type
|
||||
*
|
||||
* @return Account|null
|
||||
*/
|
||||
private function findAssetAccount(string $type): ?Account
|
||||
{
|
||||
// switch on type:
|
||||
@@ -148,9 +144,6 @@ class SetDestinationAccount implements ActionInterface
|
||||
return $this->repository->findByName($this->action->action_value, $allowed);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Account
|
||||
*/
|
||||
private function findWithdrawalDestinationAccount(): Account
|
||||
{
|
||||
$allowed = config('firefly.expected_source_types.destination.Withdrawal');
|
||||
|
@@ -23,7 +23,6 @@ declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\TransactionRules\Actions;
|
||||
|
||||
use DB;
|
||||
use FireflyIII\Events\Model\Rule\RuleActionFailedOnArray;
|
||||
use FireflyIII\Events\TriggeredAuditLog;
|
||||
use FireflyIII\Models\RuleAction;
|
||||
@@ -42,34 +41,32 @@ class SetDestinationToCashAccount implements ActionInterface
|
||||
|
||||
/**
|
||||
* TriggerInterface constructor.
|
||||
*
|
||||
* @param RuleAction $action
|
||||
*/
|
||||
public function __construct(RuleAction $action)
|
||||
{
|
||||
$this->action = $action;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function actOnArray(array $journal): bool
|
||||
{
|
||||
/** @var User $user */
|
||||
$user = User::find($journal['user_id']);
|
||||
/** @var TransactionJournal|null $object */
|
||||
|
||||
/** @var null|TransactionJournal $object */
|
||||
$object = $user->transactionJournals()->find((int)$journal['transaction_journal_id']);
|
||||
$repository = app(AccountRepositoryInterface::class);
|
||||
|
||||
if (null === $object) {
|
||||
app('log')->error('Could not find journal.');
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.no_such_journal')));
|
||||
|
||||
return false;
|
||||
}
|
||||
$type = $object->transactionType->type;
|
||||
if (TransactionType::WITHDRAWAL !== $type) {
|
||||
app('log')->error('Transaction must be withdrawal.');
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.not_withdrawal')));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -78,17 +75,19 @@ class SetDestinationToCashAccount implements ActionInterface
|
||||
$cashAccount = $repository->getCashAccount();
|
||||
|
||||
// new destination account must be different from the current source account:
|
||||
/** @var Transaction|null $source */
|
||||
/** @var null|Transaction $source */
|
||||
$source = $object->transactions()->where('amount', '<', 0)->first();
|
||||
if (null === $source) {
|
||||
app('log')->error('Could not find source transaction.');
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.cannot_find_source_transaction')));
|
||||
|
||||
return false;
|
||||
}
|
||||
// account must not be deleted (in the meantime):
|
||||
if (null === $source->account) {
|
||||
app('log')->error('Could not find source transaction account.');
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.cannot_find_source_transaction_account')));
|
||||
|
||||
return false;
|
||||
}
|
||||
if ($cashAccount->id === $source->account_id) {
|
||||
@@ -101,20 +100,21 @@ class SetDestinationToCashAccount implements ActionInterface
|
||||
);
|
||||
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.already_has_destination', ['name' => $cashAccount->name])));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
event(new TriggeredAuditLog($this->action->rule, $object, 'set_destination', null, $cashAccount->name));
|
||||
|
||||
// update destination transaction with new destination account:
|
||||
DB::table('transactions')
|
||||
->where('transaction_journal_id', '=', $object->id)
|
||||
->where('amount', '>', 0)
|
||||
->update(['account_id' => $cashAccount->id]);
|
||||
\DB::table('transactions')
|
||||
->where('transaction_journal_id', '=', $object->id)
|
||||
->where('amount', '>', 0)
|
||||
->update(['account_id' => $cashAccount->id])
|
||||
;
|
||||
|
||||
app('log')->debug(sprintf('Updated journal #%d (group #%d) and gave it new destination account ID.', $object->id, $object->transaction_group_id));
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
}
|
||||
|
@@ -37,21 +37,17 @@ class SetNotes implements ActionInterface
|
||||
|
||||
/**
|
||||
* TriggerInterface constructor.
|
||||
*
|
||||
* @param RuleAction $action
|
||||
*/
|
||||
public function __construct(RuleAction $action)
|
||||
{
|
||||
$this->action = $action;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function actOnArray(array $journal): bool
|
||||
{
|
||||
$dbNote = Note::where('noteable_id', $journal['transaction_journal_id'])
|
||||
->where('noteable_type', TransactionJournal::class)->first();
|
||||
->where('noteable_type', TransactionJournal::class)->first()
|
||||
;
|
||||
if (null === $dbNote) {
|
||||
$dbNote = new Note();
|
||||
$dbNote->noteable_id = $journal['transaction_journal_id'];
|
||||
|
@@ -23,7 +23,6 @@ declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\TransactionRules\Actions;
|
||||
|
||||
use DB;
|
||||
use FireflyIII\Events\Model\Rule\RuleActionFailedOnArray;
|
||||
use FireflyIII\Events\TriggeredAuditLog;
|
||||
use FireflyIII\Models\Account;
|
||||
@@ -44,27 +43,24 @@ class SetSourceAccount implements ActionInterface
|
||||
|
||||
/**
|
||||
* TriggerInterface constructor.
|
||||
*
|
||||
* @param RuleAction $action
|
||||
*/
|
||||
public function __construct(RuleAction $action)
|
||||
{
|
||||
$this->action = $action;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function actOnArray(array $journal): bool
|
||||
{
|
||||
/** @var User $user */
|
||||
$user = User::find($journal['user_id']);
|
||||
/** @var TransactionJournal|null $object */
|
||||
|
||||
/** @var null|TransactionJournal $object */
|
||||
$object = $user->transactionJournals()->find((int)$journal['transaction_journal_id']);
|
||||
$this->repository = app(AccountRepositoryInterface::class);
|
||||
if (null === $object) {
|
||||
app('log')->error('Could not find journal.');
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.no_such_journal')));
|
||||
|
||||
return false;
|
||||
}
|
||||
$type = $object->transactionType->type;
|
||||
@@ -77,21 +73,24 @@ class SetSourceAccount implements ActionInterface
|
||||
sprintf('Cant change source account of journal #%d because no asset account with name "%s" exists.', $object->id, $this->action->action_value)
|
||||
);
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.cannot_find_asset', ['name' => $this->action->action_value])));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// new source account must be different from the current destination account:
|
||||
/** @var Transaction|null $destination */
|
||||
/** @var null|Transaction $destination */
|
||||
$destination = $object->transactions()->where('amount', '>', 0)->first();
|
||||
if (null === $destination) {
|
||||
app('log')->error('Could not find destination transaction.');
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.cannot_find_destination_transaction')));
|
||||
|
||||
return false;
|
||||
}
|
||||
// account must not be deleted (in the meantime):
|
||||
if (null === $destination->account) {
|
||||
app('log')->error('Could not find destination transaction account.');
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.cannot_find_destination_transaction_account')));
|
||||
|
||||
return false;
|
||||
}
|
||||
if (null !== $newAccount && $newAccount->id === $destination->account_id) {
|
||||
@@ -103,6 +102,7 @@ class SetSourceAccount implements ActionInterface
|
||||
)
|
||||
);
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.already_has_source', ['name' => $newAccount->name])));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -115,10 +115,11 @@ class SetSourceAccount implements ActionInterface
|
||||
app('log')->debug(sprintf('New source account is #%d ("%s").', $newAccount->id, $newAccount->name));
|
||||
|
||||
// update source transaction with new source account:
|
||||
DB::table('transactions')
|
||||
->where('transaction_journal_id', '=', $object->id)
|
||||
->where('amount', '<', 0)
|
||||
->update(['account_id' => $newAccount->id]);
|
||||
\DB::table('transactions')
|
||||
->where('transaction_journal_id', '=', $object->id)
|
||||
->where('amount', '<', 0)
|
||||
->update(['account_id' => $newAccount->id])
|
||||
;
|
||||
|
||||
event(new TriggeredAuditLog($this->action->rule, $object, 'set_source', null, $newAccount->name));
|
||||
|
||||
@@ -127,11 +128,6 @@ class SetSourceAccount implements ActionInterface
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $type
|
||||
*
|
||||
* @return Account|null
|
||||
*/
|
||||
private function findAssetAccount(string $type): ?Account
|
||||
{
|
||||
// switch on type:
|
||||
@@ -142,9 +138,6 @@ class SetSourceAccount implements ActionInterface
|
||||
return $this->repository->findByName($this->action->action_value, $allowed);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Account
|
||||
*/
|
||||
private function findDepositSourceAccount(): Account
|
||||
{
|
||||
$allowed = config('firefly.expected_source_types.source.Deposit');
|
||||
|
@@ -23,7 +23,6 @@ declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\TransactionRules\Actions;
|
||||
|
||||
use DB;
|
||||
use FireflyIII\Events\Model\Rule\RuleActionFailedOnArray;
|
||||
use FireflyIII\Events\TriggeredAuditLog;
|
||||
use FireflyIII\Models\RuleAction;
|
||||
@@ -42,34 +41,32 @@ class SetSourceToCashAccount implements ActionInterface
|
||||
|
||||
/**
|
||||
* TriggerInterface constructor.
|
||||
*
|
||||
* @param RuleAction $action
|
||||
*/
|
||||
public function __construct(RuleAction $action)
|
||||
{
|
||||
$this->action = $action;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function actOnArray(array $journal): bool
|
||||
{
|
||||
/** @var User $user */
|
||||
$user = User::find($journal['user_id']);
|
||||
/** @var TransactionJournal|null $object */
|
||||
|
||||
/** @var null|TransactionJournal $object */
|
||||
$object = $user->transactionJournals()->find((int)$journal['transaction_journal_id']);
|
||||
$repository = app(AccountRepositoryInterface::class);
|
||||
|
||||
if (null === $object) {
|
||||
app('log')->error('Could not find journal.');
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.no_such_journal')));
|
||||
|
||||
return false;
|
||||
}
|
||||
$type = $object->transactionType->type;
|
||||
if (TransactionType::DEPOSIT !== $type) {
|
||||
app('log')->error('Transaction must be deposit.');
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.not_deposit')));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -78,17 +75,19 @@ class SetSourceToCashAccount implements ActionInterface
|
||||
$cashAccount = $repository->getCashAccount();
|
||||
|
||||
// new source account must be different from the current destination account:
|
||||
/** @var Transaction|null $destination */
|
||||
/** @var null|Transaction $destination */
|
||||
$destination = $object->transactions()->where('amount', '>', 0)->first();
|
||||
if (null === $destination) {
|
||||
app('log')->error('Could not find destination transaction.');
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.cannot_find_destination_transaction')));
|
||||
|
||||
return false;
|
||||
}
|
||||
// account must not be deleted (in the meantime):
|
||||
if (null === $destination->account) {
|
||||
app('log')->error('Could not find destination transaction account.');
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.cannot_find_destination_transaction_account')));
|
||||
|
||||
return false;
|
||||
}
|
||||
if ($cashAccount->id === $destination->account_id) {
|
||||
@@ -101,16 +100,18 @@ class SetSourceToCashAccount implements ActionInterface
|
||||
);
|
||||
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.already_has_source', ['name' => $cashAccount->name])));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
event(new TriggeredAuditLog($this->action->rule, $object, 'set_source', null, $cashAccount->name));
|
||||
|
||||
// update destination transaction with new destination account:
|
||||
DB::table('transactions')
|
||||
->where('transaction_journal_id', '=', $object->id)
|
||||
->where('amount', '<', 0)
|
||||
->update(['account_id' => $cashAccount->id]);
|
||||
\DB::table('transactions')
|
||||
->where('transaction_journal_id', '=', $object->id)
|
||||
->where('amount', '<', 0)
|
||||
->update(['account_id' => $cashAccount->id])
|
||||
;
|
||||
|
||||
app('log')->debug(sprintf('Updated journal #%d (group #%d) and gave it new source account ID.', $object->id, $object->transaction_group_id));
|
||||
|
||||
|
@@ -31,7 +31,6 @@ use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Models\TransactionType;
|
||||
|
||||
/**
|
||||
*
|
||||
* Class SwitchAccounts
|
||||
*/
|
||||
class SwitchAccounts implements ActionInterface
|
||||
@@ -40,31 +39,28 @@ class SwitchAccounts implements ActionInterface
|
||||
|
||||
/**
|
||||
* TriggerInterface constructor.
|
||||
*
|
||||
* @param RuleAction $action
|
||||
*/
|
||||
public function __construct(RuleAction $action)
|
||||
{
|
||||
$this->action = $action;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function actOnArray(array $journal): bool
|
||||
{
|
||||
// make object from array (so the data is fresh).
|
||||
/** @var TransactionJournal|null $object */
|
||||
/** @var null|TransactionJournal $object */
|
||||
$object = TransactionJournal::where('user_id', $journal['user_id'])->find($journal['transaction_journal_id']);
|
||||
if (null === $object) {
|
||||
app('log')->error(sprintf('Cannot find journal #%d, cannot switch accounts.', $journal['transaction_journal_id']));
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.no_such_journal')));
|
||||
|
||||
return false;
|
||||
}
|
||||
$groupCount = TransactionJournal::where('transaction_group_id', $journal['transaction_group_id'])->count();
|
||||
if ($groupCount > 1) {
|
||||
app('log')->error(sprintf('Group #%d has more than one transaction in it, cannot switch accounts.', $journal['transaction_group_id']));
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.split_group')));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -72,16 +68,19 @@ class SwitchAccounts implements ActionInterface
|
||||
if (TransactionType::TRANSFER !== $type) {
|
||||
app('log')->error(sprintf('Journal #%d is NOT a transfer (rule #%d), cannot switch accounts.', $journal['transaction_journal_id'], $this->action->rule_id));
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.is_not_transfer')));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/** @var Transaction|null $sourceTransaction */
|
||||
/** @var null|Transaction $sourceTransaction */
|
||||
$sourceTransaction = $object->transactions()->where('amount', '<', 0)->first();
|
||||
/** @var Transaction|null $destTransaction */
|
||||
|
||||
/** @var null|Transaction $destTransaction */
|
||||
$destTransaction = $object->transactions()->where('amount', '>', 0)->first();
|
||||
if (null === $sourceTransaction || null === $destTransaction) {
|
||||
app('log')->error(sprintf('Journal #%d has no source or destination transaction (rule #%d), cannot switch accounts.', $journal['transaction_journal_id'], $this->action->rule_id));
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.cannot_find_accounts')));
|
||||
|
||||
return false;
|
||||
}
|
||||
$sourceAccountId = $sourceTransaction->account_id;
|
||||
|
@@ -42,17 +42,12 @@ class UpdatePiggybank implements ActionInterface
|
||||
|
||||
/**
|
||||
* TriggerInterface constructor.
|
||||
*
|
||||
* @param RuleAction $action
|
||||
*/
|
||||
public function __construct(RuleAction $action)
|
||||
{
|
||||
$this->action = $action;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function actOnArray(array $journal): bool
|
||||
{
|
||||
app('log')->debug(sprintf('Triggered rule action UpdatePiggybank on journal #%d', $journal['transaction_journal_id']));
|
||||
@@ -60,6 +55,7 @@ class UpdatePiggybank implements ActionInterface
|
||||
// refresh the transaction type.
|
||||
/** @var User $user */
|
||||
$user = User::find($journal['user_id']);
|
||||
|
||||
/** @var TransactionJournal $journalObj */
|
||||
$journalObj = $user->transactionJournals()->find($journal['transaction_journal_id']);
|
||||
|
||||
@@ -69,6 +65,7 @@ class UpdatePiggybank implements ActionInterface
|
||||
sprintf('No piggy bank named "%s", cant execute action #%d of rule #%d', $this->action->action_value, $this->action->id, $this->action->rule_id)
|
||||
);
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.cannot_find_piggy', ['name' => $this->action->action_value])));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -76,6 +73,7 @@ class UpdatePiggybank implements ActionInterface
|
||||
|
||||
/** @var Transaction $source */
|
||||
$source = $journalObj->transactions()->where('amount', '<', 0)->first();
|
||||
|
||||
/** @var Transaction $destination */
|
||||
$destination = $journalObj->transactions()->where('amount', '>', 0)->first();
|
||||
|
||||
@@ -129,26 +127,15 @@ class UpdatePiggybank implements ActionInterface
|
||||
)
|
||||
);
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.no_link_piggy', ['name' => $this->action->action_value])));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param User $user
|
||||
*
|
||||
* @return PiggyBank|null
|
||||
*/
|
||||
private function findPiggyBank(User $user): ?PiggyBank
|
||||
{
|
||||
return $user->piggyBanks()->where('piggy_banks.name', $this->action->action_value)->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);
|
||||
@@ -180,13 +167,6 @@ class UpdatePiggybank implements ActionInterface
|
||||
$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);
|
||||
@@ -205,7 +185,6 @@ class UpdatePiggybank implements ActionInterface
|
||||
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.');
|
||||
|
@@ -33,8 +33,6 @@ interface RuleEngineInterface
|
||||
{
|
||||
/**
|
||||
* Add operators added to each search by the rule engine.
|
||||
*
|
||||
* @param array $operator
|
||||
*/
|
||||
public function addOperator(array $operator): void;
|
||||
|
||||
@@ -50,34 +48,20 @@ interface RuleEngineInterface
|
||||
|
||||
/**
|
||||
* Return the number of changed transactions from the previous "fire" action.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getResults(): int;
|
||||
|
||||
/**
|
||||
* @param bool $refreshTriggers
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function setRefreshTriggers(bool $refreshTriggers): void;
|
||||
|
||||
/**
|
||||
* Add entire rule groups for the engine to execute.
|
||||
*
|
||||
* @param Collection $ruleGroups
|
||||
*/
|
||||
public function setRuleGroups(Collection $ruleGroups): void;
|
||||
|
||||
/**
|
||||
* Add rules for the engine to execute.
|
||||
*
|
||||
* @param Collection $rules
|
||||
*/
|
||||
public function setRules(Collection $rules): void;
|
||||
|
||||
/**
|
||||
* @param User $user
|
||||
*/
|
||||
public function setUser(User $user): void;
|
||||
}
|
||||
|
@@ -59,18 +59,12 @@ class SearchRuleEngine implements RuleEngineInterface
|
||||
$this->refreshTriggers = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function addOperator(array $operator): void
|
||||
{
|
||||
app('log')->debug('Add extra operator: ', $operator);
|
||||
$this->operators[] = $operator;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function find(): Collection
|
||||
{
|
||||
app('log')->debug('SearchRuleEngine::find()');
|
||||
@@ -89,12 +83,79 @@ class SearchRuleEngine implements RuleEngineInterface
|
||||
return $collection->unique();
|
||||
}
|
||||
|
||||
public function setUser(User $user): void
|
||||
{
|
||||
$this->user = $user;
|
||||
$this->operators = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function fire(): void
|
||||
{
|
||||
$this->resultCount = [];
|
||||
app('log')->debug('SearchRuleEngine::fire()!');
|
||||
|
||||
// if rules and no rule groups, file each rule separately.
|
||||
if (0 !== $this->rules->count()) {
|
||||
app('log')->debug(sprintf('SearchRuleEngine:: found %d rule(s) to fire.', $this->rules->count()));
|
||||
foreach ($this->rules as $rule) {
|
||||
$this->fireRule($rule);
|
||||
}
|
||||
app('log')->debug('SearchRuleEngine:: done processing all rules!');
|
||||
|
||||
return;
|
||||
}
|
||||
if (0 !== $this->groups->count()) {
|
||||
app('log')->debug(sprintf('SearchRuleEngine:: found %d rule group(s) to fire.', $this->groups->count()));
|
||||
|
||||
// fire each group:
|
||||
/** @var RuleGroup $group */
|
||||
foreach ($this->groups as $group) {
|
||||
$this->fireGroup($group);
|
||||
}
|
||||
}
|
||||
app('log')->debug('SearchRuleEngine:: done processing all rules!');
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the number of changed transactions from the previous "fire" action.
|
||||
*/
|
||||
public function getResults(): int
|
||||
{
|
||||
return count($this->resultCount);
|
||||
}
|
||||
|
||||
public function setRefreshTriggers(bool $refreshTriggers): void
|
||||
{
|
||||
$this->refreshTriggers = $refreshTriggers;
|
||||
}
|
||||
|
||||
public function setRuleGroups(Collection $ruleGroups): void
|
||||
{
|
||||
app('log')->debug(__METHOD__);
|
||||
foreach ($ruleGroups as $group) {
|
||||
if ($group instanceof RuleGroup) {
|
||||
app('log')->debug(sprintf('Adding a rule group to the SearchRuleEngine: #%d ("%s")', $group->id, $group->title));
|
||||
$this->groups->push($group);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function setRules(Collection $rules): void
|
||||
{
|
||||
app('log')->debug(__METHOD__);
|
||||
foreach ($rules as $rule) {
|
||||
if ($rule instanceof Rule) {
|
||||
app('log')->debug(sprintf('Adding a rule to the SearchRuleEngine: #%d ("%s")', $rule->id, $rule->title));
|
||||
$this->rules->push($rule);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds the transactions a strict rule will execute on.
|
||||
*
|
||||
* @param Rule $rule
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
private function findStrictRule(Rule $rule): Collection
|
||||
{
|
||||
@@ -126,7 +187,6 @@ class SearchRuleEngine implements RuleEngineInterface
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// add local operators:
|
||||
foreach ($this->operators as $operator) {
|
||||
app('log')->debug(sprintf('SearchRuleEngine:: add local added operator: %s:"%s"', $operator['type'], $operator['value']));
|
||||
@@ -161,10 +221,6 @@ class SearchRuleEngine implements RuleEngineInterface
|
||||
* Search in the triggers of this particular search and if it contains
|
||||
* one search operator for "journal_id" it means the date ranges
|
||||
* in the search may need to be updated.
|
||||
*
|
||||
* @param array $array
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function hasSpecificJournalTrigger(array $array): bool
|
||||
{
|
||||
@@ -187,18 +243,13 @@ class SearchRuleEngine implements RuleEngineInterface
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $array
|
||||
*
|
||||
* @return Carbon
|
||||
*/
|
||||
private function setDateFromJournalTrigger(array $array): Carbon
|
||||
{
|
||||
app('log')->debug('Now in setDateFromJournalTrigger()');
|
||||
$journalId = 0;
|
||||
foreach ($array as $triggerName => $values) {
|
||||
if ('journal_id' === $triggerName && is_array($values) && 1 === count($values)) {
|
||||
$journalId = (int)trim(($values[0] ?? '"0"'), '"'); // follows format "123".
|
||||
$journalId = (int)trim($values[0] ?? '"0"', '"'); // follows format "123".
|
||||
app('log')->debug(sprintf('Found journal ID #%d', $journalId));
|
||||
}
|
||||
}
|
||||
@@ -218,20 +269,6 @@ class SearchRuleEngine implements RuleEngineInterface
|
||||
return today(config('app.timezone'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function setUser(User $user): void
|
||||
{
|
||||
$this->user = $user;
|
||||
$this->operators = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Rule $rule
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
private function findNonStrictRule(Rule $rule): Collection
|
||||
{
|
||||
// start a search query for individual each trigger:
|
||||
@@ -252,6 +289,7 @@ class SearchRuleEngine implements RuleEngineInterface
|
||||
}
|
||||
if ('user_action' === $ruleTrigger->trigger_type) {
|
||||
app('log')->debug('Skip trigger type.');
|
||||
|
||||
continue;
|
||||
}
|
||||
$searchArray = [];
|
||||
@@ -286,7 +324,7 @@ class SearchRuleEngine implements RuleEngineInterface
|
||||
app('log')->debug(sprintf('Found in this run, %d transactions', $collection->count()));
|
||||
$total = $total->merge($collection);
|
||||
app('log')->debug(sprintf('Total collection is now %d transactions', $total->count()));
|
||||
$count++;
|
||||
++$count;
|
||||
}
|
||||
app('log')->debug(sprintf('Total collection is now %d transactions', $total->count()));
|
||||
app('log')->debug(sprintf('Done running %d trigger(s)', $count));
|
||||
@@ -298,10 +336,9 @@ class SearchRuleEngine implements RuleEngineInterface
|
||||
foreach ($group['transactions'] as $transaction) {
|
||||
$str = sprintf('%s%d', $str, $transaction['transaction_journal_id']);
|
||||
}
|
||||
$key = sprintf('%d%s', $group['id'], $str);
|
||||
//app('log')->debug(sprintf('Return key: %s ', $key));
|
||||
|
||||
return $key;
|
||||
return sprintf('%d%s', $group['id'], $str);
|
||||
// app('log')->debug(sprintf('Return key: %s ', $key));
|
||||
}
|
||||
);
|
||||
|
||||
@@ -310,42 +347,9 @@ class SearchRuleEngine implements RuleEngineInterface
|
||||
return $unique;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function fire(): void
|
||||
{
|
||||
$this->resultCount = [];
|
||||
app('log')->debug('SearchRuleEngine::fire()!');
|
||||
|
||||
// if rules and no rule groups, file each rule separately.
|
||||
if (0 !== $this->rules->count()) {
|
||||
app('log')->debug(sprintf('SearchRuleEngine:: found %d rule(s) to fire.', $this->rules->count()));
|
||||
foreach ($this->rules as $rule) {
|
||||
$this->fireRule($rule);
|
||||
}
|
||||
app('log')->debug('SearchRuleEngine:: done processing all rules!');
|
||||
|
||||
return;
|
||||
}
|
||||
if (0 !== $this->groups->count()) {
|
||||
app('log')->debug(sprintf('SearchRuleEngine:: found %d rule group(s) to fire.', $this->groups->count()));
|
||||
// fire each group:
|
||||
/** @var RuleGroup $group */
|
||||
foreach ($this->groups as $group) {
|
||||
$this->fireGroup($group);
|
||||
}
|
||||
}
|
||||
app('log')->debug('SearchRuleEngine:: done processing all rules!');
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the rule has been triggered.
|
||||
*
|
||||
* @param Rule $rule
|
||||
*
|
||||
* @return bool
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function fireRule(Rule $rule): bool
|
||||
@@ -369,9 +373,6 @@ class SearchRuleEngine implements RuleEngineInterface
|
||||
/**
|
||||
* Return true if the rule is fired (the collection is larger than zero).
|
||||
*
|
||||
* @param Rule $rule
|
||||
*
|
||||
* @return bool
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function fireStrictRule(Rule $rule): bool
|
||||
@@ -394,14 +395,12 @@ class SearchRuleEngine implements RuleEngineInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Rule $rule
|
||||
* @param Collection $collection
|
||||
*
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function processResults(Rule $rule, Collection $collection): void
|
||||
{
|
||||
app('log')->debug(sprintf('SearchRuleEngine:: Going to process %d results.', $collection->count()));
|
||||
|
||||
/** @var array $group */
|
||||
foreach ($collection as $group) {
|
||||
$this->processTransactionGroup($rule, $group);
|
||||
@@ -409,14 +408,12 @@ class SearchRuleEngine implements RuleEngineInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Rule $rule
|
||||
* @param array $group
|
||||
*
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function processTransactionGroup(Rule $rule, array $group): void
|
||||
{
|
||||
app('log')->debug(sprintf('SearchRuleEngine:: Will now execute actions on transaction group #%d', $group['id']));
|
||||
|
||||
/** @var array $transaction */
|
||||
foreach ($group['transactions'] as $transaction) {
|
||||
$this->processTransactionJournal($rule, $transaction);
|
||||
@@ -424,15 +421,13 @@ class SearchRuleEngine implements RuleEngineInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Rule $rule
|
||||
* @param array $transaction
|
||||
*
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function processTransactionJournal(Rule $rule, array $transaction): void
|
||||
{
|
||||
app('log')->debug(sprintf('SearchRuleEngine:: Will now execute actions on transaction journal #%d', $transaction['transaction_journal_id']));
|
||||
$actions = $rule->ruleActions()->orderBy('order', 'ASC')->get();
|
||||
|
||||
/** @var RuleAction $ruleAction */
|
||||
foreach ($actions as $ruleAction) {
|
||||
if (false === $ruleAction->active) {
|
||||
@@ -446,10 +441,6 @@ class SearchRuleEngine implements RuleEngineInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* @param RuleAction $ruleAction
|
||||
* @param array $transaction
|
||||
*
|
||||
* @return bool
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function processRuleAction(RuleAction $ruleAction, array $transaction): bool
|
||||
@@ -486,9 +477,6 @@ class SearchRuleEngine implements RuleEngineInterface
|
||||
/**
|
||||
* Return true if the rule is fired (the collection is larger than zero).
|
||||
*
|
||||
* @param Rule $rule
|
||||
*
|
||||
* @return bool
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function fireNonStrictRule(Rule $rule): bool
|
||||
@@ -503,14 +491,12 @@ class SearchRuleEngine implements RuleEngineInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* @param RuleGroup $group
|
||||
*
|
||||
* @return void
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function fireGroup(RuleGroup $group): void
|
||||
{
|
||||
app('log')->debug(sprintf('Going to fire group #%d with %d rule(s)', $group->id, $group->rules->count()));
|
||||
|
||||
/** @var Rule $rule */
|
||||
foreach ($group->rules as $rule) {
|
||||
app('log')->debug(sprintf('Going to fire rule #%d from group #%d', $rule->id, $group->id));
|
||||
@@ -522,50 +508,4 @@ class SearchRuleEngine implements RuleEngineInterface
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the number of changed transactions from the previous "fire" action.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getResults(): int
|
||||
{
|
||||
return count($this->resultCount);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $refreshTriggers
|
||||
*/
|
||||
public function setRefreshTriggers(bool $refreshTriggers): void
|
||||
{
|
||||
$this->refreshTriggers = $refreshTriggers;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function setRuleGroups(Collection $ruleGroups): void
|
||||
{
|
||||
app('log')->debug(__METHOD__);
|
||||
foreach ($ruleGroups as $group) {
|
||||
if ($group instanceof RuleGroup) {
|
||||
app('log')->debug(sprintf('Adding a rule group to the SearchRuleEngine: #%d ("%s")', $group->id, $group->title));
|
||||
$this->groups->push($group);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function setRules(Collection $rules): void
|
||||
{
|
||||
app('log')->debug(__METHOD__);
|
||||
foreach ($rules as $rule) {
|
||||
if ($rule instanceof Rule) {
|
||||
app('log')->debug(sprintf('Adding a rule to the SearchRuleEngine: #%d ("%s")', $rule->id, $rule->title));
|
||||
$this->rules->push($rule);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -30,8 +30,6 @@ use FireflyIII\TransactionRules\Actions\ActionInterface;
|
||||
|
||||
/**
|
||||
* Class ActionFactory can create actions.
|
||||
*
|
||||
|
||||
*/
|
||||
class ActionFactory
|
||||
{
|
||||
@@ -44,10 +42,6 @@ class ActionFactory
|
||||
* with value "Groceries" this method will return a corresponding SetCategory object preset
|
||||
* to "Groceries". Any transaction journal then fed to this object will have its category changed.
|
||||
*
|
||||
* @param RuleAction $action
|
||||
*
|
||||
* @return ActionInterface
|
||||
*
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public static function getAction(RuleAction $action): ActionInterface
|
||||
@@ -63,10 +57,6 @@ class ActionFactory
|
||||
* that will match the given action type (ie. "change_category") to the matching class name
|
||||
* (SetCategory) using the configuration (firefly.php).
|
||||
*
|
||||
* @param string $actionType
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public static function getActionClass(string $actionType): string
|
||||
@@ -74,12 +64,12 @@ class ActionFactory
|
||||
$actionTypes = self::getActionTypes();
|
||||
|
||||
if (!array_key_exists($actionType, $actionTypes)) {
|
||||
throw new FireflyException('No such action exists ("' . e($actionType) . '").');
|
||||
throw new FireflyException('No such action exists ("'.e($actionType).'").');
|
||||
}
|
||||
|
||||
$class = $actionTypes[$actionType];
|
||||
if (!class_exists($class)) {
|
||||
throw new FireflyException('Could not instantiate class for rule action type "' . e($actionType) . '" (' . e($class) . ').');
|
||||
throw new FireflyException('Could not instantiate class for rule action type "'.e($actionType).'" ('.e($class).').');
|
||||
}
|
||||
|
||||
return $class;
|
||||
@@ -87,8 +77,6 @@ class ActionFactory
|
||||
|
||||
/**
|
||||
* Returns a map with actiontypes, mapped to the class representing that type.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected static function getActionTypes(): array
|
||||
{
|
||||
|
Reference in New Issue
Block a user