mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-19 10:53:31 +00:00
Merge remote-tracking branch 'upstream/main' into feat/expression-engine
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;
|
||||
}
|
||||
|
@@ -24,7 +24,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;
|
||||
@@ -32,7 +31,6 @@ use FireflyIII\Models\RuleAction;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\TransactionRules\Expressions\ActionExpressionEvaluator;
|
||||
use FireflyIII\User;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
/**
|
||||
* Class AddTag.
|
||||
@@ -44,8 +42,6 @@ class AddTag implements ActionInterface
|
||||
|
||||
/**
|
||||
* TriggerInterface constructor.
|
||||
*
|
||||
* @param RuleAction $action
|
||||
*/
|
||||
public function __construct(RuleAction $action, ActionExpressionEvaluator $evaluator)
|
||||
{
|
||||
@@ -53,17 +49,17 @@ class AddTag implements ActionInterface
|
||||
$this->evaluator = $evaluator;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function actOnArray(array $journal): bool
|
||||
{
|
||||
// journal has this tag maybe?
|
||||
/** @var TagFactory $factory */
|
||||
$factory = app(TagFactory::class);
|
||||
$factory->setUser(User::find($journal['user_id']));
|
||||
|
||||
/** @var User $user */
|
||||
$user = User::find($journal['user_id']);
|
||||
$factory->setUser($user);
|
||||
$tagName = $this->evaluator->evaluate($journal);
|
||||
$tag = $factory->findOrCreate($tagName);
|
||||
$tag = $factory->findOrCreate($tagName);
|
||||
|
||||
if (null === $tag) {
|
||||
// could not find, could not create tag.
|
||||
@@ -72,21 +68,24 @@ class AddTag implements ActionInterface
|
||||
return false;
|
||||
}
|
||||
|
||||
$count = DB::table('tag_transaction_journal')
|
||||
$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']]);
|
||||
Log::debug(sprintf('RuleAction AddTag. Added tag #%d ("%s") to journal %d.', $tag->id, $tag->tag, $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;
|
||||
}
|
||||
Log::debug(
|
||||
app('log')->debug(
|
||||
sprintf('RuleAction AddTag fired but tag %d ("%s") was already added to journal %d.', $tag->id, $tag->tag, $journal['transaction_journal_id'])
|
||||
);
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.tag_already_added', ['tag' => $tagName])));
|
||||
|
@@ -24,7 +24,6 @@ declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\TransactionRules\Actions;
|
||||
|
||||
use DB;
|
||||
use FireflyIII\Events\TriggeredAuditLog;
|
||||
use FireflyIII\Models\RuleAction;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
@@ -40,8 +39,6 @@ class AppendDescription implements ActionInterface
|
||||
|
||||
/**
|
||||
* TriggerInterface constructor.
|
||||
*
|
||||
* @param RuleAction $action
|
||||
*/
|
||||
public function __construct(RuleAction $action, ActionExpressionEvaluator $evaluator)
|
||||
{
|
||||
@@ -49,18 +46,15 @@ class AppendDescription implements ActionInterface
|
||||
$this->evaluator = $evaluator;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function actOnArray(array $journal): bool
|
||||
{
|
||||
$actionValue = $this->evaluator->evaluate($journal);
|
||||
$description = sprintf('%s%s', $journal['description'], $actionValue);
|
||||
DB::table('transaction_journals')->where('id', $journal['transaction_journal_id'])->limit(1)->update(['description' => $description]);
|
||||
$description = sprintf('%s %s', $journal['description'], $actionValue);
|
||||
\DB::table('transaction_journals')->where('id', $journal['transaction_journal_id'])->limit(1)->update(['description' => $description]);
|
||||
|
||||
// event for audit log entry
|
||||
/** @var TransactionJournal $object */
|
||||
$object = TransactionJournal::where('user_id', $journal['user_id'])->find($journal['transaction_journal_id']);
|
||||
$object = TransactionJournal::where('user_id', $journal['user_id'])->find($journal['transaction_journal_id']);
|
||||
event(new TriggeredAuditLog($this->action->rule, $object, 'update_description', $journal['description'], $description));
|
||||
|
||||
return true;
|
||||
|
@@ -29,7 +29,6 @@ use FireflyIII\Events\TriggeredAuditLog;
|
||||
use FireflyIII\Models\Note;
|
||||
use FireflyIII\Models\RuleAction;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
/**
|
||||
* Class AppendDescriptionToNotes
|
||||
@@ -40,29 +39,25 @@ 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 $journal */
|
||||
/** @var null|TransactionJournal $object */
|
||||
$object = TransactionJournal::where('user_id', $journal['user_id'])->find($journal['transaction_journal_id']);
|
||||
if (null === $object) {
|
||||
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')));
|
||||
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();
|
||||
$note = $object->notes()->first();
|
||||
if (null === $note) {
|
||||
$note = new Note();
|
||||
$note = new Note();
|
||||
$note->noteable()->associate($object);
|
||||
$note->text = '';
|
||||
}
|
||||
@@ -73,12 +68,13 @@ class AppendDescriptionToNotes implements ActionInterface
|
||||
if ('' === $note->text) {
|
||||
$note->text = (string)$object->description;
|
||||
}
|
||||
$after = $note->text;
|
||||
$after = $note->text;
|
||||
|
||||
// event for audit log entry
|
||||
event(new TriggeredAuditLog($this->action->rule, $object, 'update_notes', $before, $after));
|
||||
|
||||
$note->save();
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@@ -29,7 +29,6 @@ use FireflyIII\Models\Note;
|
||||
use FireflyIII\Models\RuleAction;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\TransactionRules\Expressions\ActionExpressionEvaluator;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
/**
|
||||
* Class AppendNotes.
|
||||
@@ -41,8 +40,6 @@ class AppendNotes implements ActionInterface
|
||||
|
||||
/**
|
||||
* TriggerInterface constructor.
|
||||
*
|
||||
* @param RuleAction $action
|
||||
*/
|
||||
public function __construct(RuleAction $action, ActionExpressionEvaluator $evaluator)
|
||||
{
|
||||
@@ -50,13 +47,10 @@ class AppendNotes implements ActionInterface
|
||||
$this->evaluator = $evaluator;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function actOnArray(array $journal): bool
|
||||
{
|
||||
$actionValue = $this->evaluator->evaluate($journal);
|
||||
$dbNote = Note::where('noteable_id', (int)$journal['transaction_journal_id'])
|
||||
$dbNote = Note::where('noteable_id', (int)$journal['transaction_journal_id'])
|
||||
->where('noteable_type', TransactionJournal::class)
|
||||
->first(['notes.*']);
|
||||
if (null === $dbNote) {
|
||||
@@ -65,14 +59,14 @@ class AppendNotes implements ActionInterface
|
||||
$dbNote->noteable_type = TransactionJournal::class;
|
||||
$dbNote->text = '';
|
||||
}
|
||||
Log::debug(sprintf('RuleAction AppendNotes appended "%s" to "%s".', $actionValue, $dbNote->text));
|
||||
app('log')->debug(sprintf('RuleAction AppendNotes appended "%s" to "%s".', $actionValue, $dbNote->text));
|
||||
$before = $dbNote->text;
|
||||
$text = sprintf('%s%s', $dbNote->text, $actionValue);
|
||||
$dbNote->text = $text;
|
||||
$dbNote->save();
|
||||
|
||||
/** @var TransactionJournal $object */
|
||||
$object = TransactionJournal::where('user_id', $journal['user_id'])->find($journal['transaction_journal_id']);
|
||||
$object = TransactionJournal::where('user_id', $journal['user_id'])->find($journal['transaction_journal_id']);
|
||||
|
||||
event(new TriggeredAuditLog($this->action->rule, $object, 'update_notes', $before, $text));
|
||||
|
||||
|
@@ -30,7 +30,6 @@ use FireflyIII\Models\Note;
|
||||
use FireflyIII\Models\RuleAction;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Support\Request\ConvertsDataTypes;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
/**
|
||||
* Class AppendNotesToDescription
|
||||
@@ -43,51 +42,49 @@ class AppendNotesToDescription implements ActionInterface
|
||||
|
||||
/**
|
||||
* TriggerInterface constructor.
|
||||
*
|
||||
* @param RuleAction $action
|
||||
*/
|
||||
public function __construct(RuleAction $action)
|
||||
{
|
||||
$this->action = $action;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function actOnArray(array $journal): bool
|
||||
{
|
||||
Log::debug('Now in AppendNotesToDescription');
|
||||
/** @var TransactionJournal $object */
|
||||
app('log')->debug('Now in AppendNotesToDescription');
|
||||
|
||||
/** @var null|TransactionJournal $object */
|
||||
$object = TransactionJournal::where('user_id', $journal['user_id'])->find($journal['transaction_journal_id']);
|
||||
if (null === $object) {
|
||||
Log::error(sprintf('No journal #%d belongs to user #%d.', $journal['transaction_journal_id'], $journal['user_id']));
|
||||
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();
|
||||
$note = $object->notes()->first();
|
||||
if (null === $note) {
|
||||
Log::debug('Journal has no notes.');
|
||||
$note = new Note();
|
||||
app('log')->debug('Journal has no notes.');
|
||||
$note = new Note();
|
||||
$note->noteable()->associate($object);
|
||||
$note->text = '';
|
||||
}
|
||||
// only append if there is something to append
|
||||
if ('' !== $note->text) {
|
||||
$before = $object->description;
|
||||
$object->description = trim(sprintf('%s %s', $object->description, (string)$this->clearString($note->text, false)));
|
||||
$object->description = trim(sprintf('%s %s', $object->description, (string)$this->clearString($note->text)));
|
||||
$object->save();
|
||||
Log::debug(sprintf('Journal description is updated to "%s".', $object->description));
|
||||
app('log')->debug(sprintf('Journal description is updated to "%s".', $object->description));
|
||||
|
||||
event(new TriggeredAuditLog($this->action->rule, $object, 'update_description', $before, $object->description));
|
||||
|
||||
return true;
|
||||
}
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.new_notes_empty')));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
||||
*/
|
||||
public function get(string $key, mixed $default = null): mixed
|
||||
{
|
||||
@@ -95,7 +92,7 @@ class AppendNotesToDescription implements ActionInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
||||
*/
|
||||
public function has(mixed $key): mixed
|
||||
{
|
||||
|
@@ -23,12 +23,10 @@ declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\TransactionRules\Actions;
|
||||
|
||||
use DB;
|
||||
use FireflyIII\Events\Model\Rule\RuleActionFailedOnArray;
|
||||
use FireflyIII\Events\TriggeredAuditLog;
|
||||
use FireflyIII\Models\RuleAction;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
/**
|
||||
* Class ClearBudget.
|
||||
@@ -39,33 +37,29 @@ 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 */
|
||||
$object = TransactionJournal::where('user_id', $journal['user_id'])->find($journal['transaction_journal_id']);
|
||||
$budget = $object->budgets()->first();
|
||||
if (null === $budget) {
|
||||
Log::debug(sprintf('RuleAction ClearBudget, no budget in journal #%d.', $journal['transaction_journal_id']));
|
||||
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));
|
||||
|
||||
Log::debug(sprintf('RuleAction ClearBudget removed all budgets from journal #%d.', $journal['transaction_journal_id']));
|
||||
app('log')->debug(sprintf('RuleAction ClearBudget removed all budgets from journal #%d.', $journal['transaction_journal_id']));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@@ -23,12 +23,10 @@ declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\TransactionRules\Actions;
|
||||
|
||||
use DB;
|
||||
use FireflyIII\Events\Model\Rule\RuleActionFailedOnArray;
|
||||
use FireflyIII\Events\TriggeredAuditLog;
|
||||
use FireflyIII\Models\RuleAction;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
/**
|
||||
* Class ClearCategory.
|
||||
@@ -39,33 +37,29 @@ 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 */
|
||||
$object = TransactionJournal::where('user_id', $journal['user_id'])->find($journal['transaction_journal_id']);
|
||||
$category = $object->categories()->first();
|
||||
if (null === $category) {
|
||||
Log::debug(sprintf('RuleAction ClearCategory, no category in journal #%d.', $journal['transaction_journal_id']));
|
||||
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));
|
||||
|
||||
Log::debug(sprintf('RuleAction ClearCategory removed all categories from journal #%d.', $journal['transaction_journal_id']));
|
||||
app('log')->debug(sprintf('RuleAction ClearCategory removed all categories from journal #%d.', $journal['transaction_journal_id']));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@@ -23,12 +23,11 @@ declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\TransactionRules\Actions;
|
||||
|
||||
use DB;
|
||||
use FireflyIII\Events\Model\Rule\RuleActionFailedOnArray;
|
||||
use FireflyIII\Events\TriggeredAuditLog;
|
||||
use FireflyIII\Models\Note;
|
||||
use FireflyIII\Models\RuleAction;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
/**
|
||||
* Class ClearNotes.
|
||||
@@ -39,33 +38,33 @@ 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 null|Note $notes */
|
||||
$notes = $object->notes()->first();
|
||||
if (null === $notes) {
|
||||
Log::debug(sprintf('RuleAction ClearNotes, journal #%d has no notes.', $journal['transaction_journal_id']));
|
||||
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();
|
||||
Log::debug(sprintf('RuleAction ClearNotes removed all notes from journal #%d.', $journal['transaction_journal_id']));
|
||||
\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));
|
||||
|
||||
|
@@ -24,7 +24,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;
|
||||
@@ -37,11 +36,8 @@ use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Models\TransactionType;
|
||||
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
||||
use FireflyIII\TransactionRules\Expressions\ActionExpressionEvaluator;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use JsonException;
|
||||
|
||||
/**
|
||||
*
|
||||
* Class ConvertToDeposit
|
||||
*/
|
||||
class ConvertToDeposit implements ActionInterface
|
||||
@@ -51,8 +47,6 @@ class ConvertToDeposit implements ActionInterface
|
||||
|
||||
/**
|
||||
* TriggerInterface constructor.
|
||||
*
|
||||
* @param RuleAction $action
|
||||
*/
|
||||
public function __construct(RuleAction $action, ActionExpressionEvaluator $evaluator)
|
||||
{
|
||||
@@ -60,45 +54,46 @@ class ConvertToDeposit implements ActionInterface
|
||||
$this->evaluator = $evaluator;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function actOnArray(array $journal): bool
|
||||
{
|
||||
$actionValue = $this->evaluator->evaluate($journal);
|
||||
|
||||
// make object from array (so the data is fresh).
|
||||
/** @var TransactionJournal|null $object */
|
||||
$object = TransactionJournal::where('user_id', $journal['user_id'])->find($journal['transaction_journal_id']);
|
||||
/** @var null|TransactionJournal $object */
|
||||
$object = TransactionJournal::where('user_id', $journal['user_id'])->find($journal['transaction_journal_id']);
|
||||
if (null === $object) {
|
||||
Log::error(sprintf('Cannot find journal #%d, cannot convert to deposit.', $journal['transaction_journal_id']));
|
||||
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) {
|
||||
Log::error(sprintf('Group #%d has more than one transaction in it, cannot convert to deposit.', $journal['transaction_group_id']));
|
||||
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;
|
||||
}
|
||||
|
||||
Log::debug(sprintf('Convert journal #%d to deposit.', $journal['transaction_journal_id']));
|
||||
$type = $object->transactionType->type;
|
||||
app('log')->debug(sprintf('Convert journal #%d to deposit.', $journal['transaction_journal_id']));
|
||||
$type = $object->transactionType->type;
|
||||
if (TransactionType::DEPOSIT === $type) {
|
||||
Log::error(sprintf('Journal #%d is already a deposit (rule #%d).', $journal['transaction_journal_id'], $this->action->rule_id));
|
||||
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;
|
||||
}
|
||||
|
||||
if (TransactionType::WITHDRAWAL === $type) {
|
||||
Log::debug('Going to transform a withdrawal to a deposit.');
|
||||
app('log')->debug('Going to transform a withdrawal to a deposit.');
|
||||
|
||||
try {
|
||||
$res = $this->convertWithdrawalArray($object, $actionValue);
|
||||
} catch (JsonException | FireflyException $e) {
|
||||
Log::debug('Could not convert withdrawal to deposit.');
|
||||
Log::error($e->getMessage());
|
||||
} catch (FireflyException $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;
|
||||
}
|
||||
|
||||
@@ -107,14 +102,15 @@ class ConvertToDeposit implements ActionInterface
|
||||
return $res;
|
||||
}
|
||||
if (TransactionType::TRANSFER === $type) {
|
||||
Log::debug('Going to transform a transfer to a deposit.');
|
||||
app('log')->debug('Going to transform a transfer to a deposit.');
|
||||
|
||||
try {
|
||||
$res = $this->convertTransferArray($object);
|
||||
} catch (JsonException | FireflyException $e) {
|
||||
Log::debug('Could not convert transfer to deposit.');
|
||||
Log::error($e->getMessage());
|
||||
$res = $this->convertTransferArray($object, $actionValue);
|
||||
} catch (FireflyException $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));
|
||||
@@ -122,6 +118,7 @@ class ConvertToDeposit implements ActionInterface
|
||||
return $res;
|
||||
}
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.unsupported_transaction_type_deposit', ['type' => $type])));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -129,29 +126,26 @@ 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
|
||||
*/
|
||||
private function convertWithdrawalArray(TransactionJournal $journal, string $actionValue): bool
|
||||
{
|
||||
$user = $journal->user;
|
||||
$user = $journal->user;
|
||||
|
||||
// find or create revenue account.
|
||||
/** @var AccountFactory $factory */
|
||||
$factory = app(AccountFactory::class);
|
||||
$factory = app(AccountFactory::class);
|
||||
$factory->setUser($user);
|
||||
|
||||
$repository = app(AccountRepositoryInterface::class);
|
||||
$repository = app(AccountRepositoryInterface::class);
|
||||
$repository->setUser($user);
|
||||
|
||||
$destAccount = $this->getDestinationAccount($journal);
|
||||
$sourceAccount = $this->getSourceAccount($journal);
|
||||
$destAccount = $this->getDestinationAccount($journal);
|
||||
$sourceAccount = $this->getSourceAccount($journal);
|
||||
|
||||
// get the action value, or use the original destination name in case the action value is empty:
|
||||
// this becomes a new or existing (revenue) account, which is the source of the new deposit.
|
||||
$opposingName = '' === $actionValue ? $destAccount->name : $actionValue;
|
||||
$opposingName = '' === $actionValue ? $destAccount->name : $actionValue;
|
||||
// we check all possible source account types if one exists:
|
||||
$validTypes = config('firefly.expected_source_types.source.Deposit');
|
||||
$opposingAccount = $repository->findByName($opposingName, $validTypes);
|
||||
@@ -159,61 +153,57 @@ class ConvertToDeposit implements ActionInterface
|
||||
$opposingAccount = $factory->findOrCreate($opposingName, AccountType::REVENUE);
|
||||
}
|
||||
|
||||
Log::debug(sprintf('ConvertToDeposit. Action value is "%s", new opposing name is "%s"', $actionValue, $opposingAccount->name));
|
||||
app('log')->debug(sprintf('ConvertToDeposit. Action value is "%s", new opposing name is "%s"', $actionValue, $opposingAccount->name));
|
||||
|
||||
// update the source transaction and put in the new revenue ID.
|
||||
DB::table('transactions')
|
||||
\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')
|
||||
\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();
|
||||
$newType = TransactionType::whereType(TransactionType::DEPOSIT)->first();
|
||||
|
||||
DB::table('transaction_journals')
|
||||
\DB::table('transaction_journals')
|
||||
->where('id', '=', $journal->id)
|
||||
->update(['transaction_type_id' => $newType->id, 'bill_id' => null]);
|
||||
|
||||
Log::debug('Converted withdrawal to deposit.');
|
||||
app('log')->debug('Converted withdrawal to deposit.');
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @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;
|
||||
}
|
||||
|
||||
@@ -222,28 +212,25 @@ 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
|
||||
*/
|
||||
private function convertTransferArray(TransactionJournal $journal): bool
|
||||
private function convertTransferArray(TransactionJournal $journal, string $actionValue): bool
|
||||
{
|
||||
$user = $journal->user;
|
||||
$user = $journal->user;
|
||||
|
||||
// find or create revenue account.
|
||||
/** @var AccountFactory $factory */
|
||||
$factory = app(AccountFactory::class);
|
||||
$factory = app(AccountFactory::class);
|
||||
$factory->setUser($user);
|
||||
|
||||
$repository = app(AccountRepositoryInterface::class);
|
||||
$repository = app(AccountRepositoryInterface::class);
|
||||
$repository->setUser($user);
|
||||
|
||||
$sourceAccount = $this->getSourceAccount($journal);
|
||||
$sourceAccount = $this->getSourceAccount($journal);
|
||||
|
||||
// get the action value, or use the original source name in case the action value is empty:
|
||||
// this becomes a new or existing (revenue) account, which is the source of the new deposit.
|
||||
$opposingName = '' === $actionValue ? $sourceAccount->name : $actionValue;
|
||||
$opposingName = '' === $actionValue ? $sourceAccount->name : $actionValue;
|
||||
// we check all possible source account types if one exists:
|
||||
$validTypes = config('firefly.expected_source_types.source.Deposit');
|
||||
$opposingAccount = $repository->findByName($opposingName, $validTypes);
|
||||
@@ -251,22 +238,22 @@ class ConvertToDeposit implements ActionInterface
|
||||
$opposingAccount = $factory->findOrCreate($opposingName, AccountType::REVENUE);
|
||||
}
|
||||
|
||||
Log::debug(sprintf('ConvertToDeposit. Action value is "%s", revenue name is "%s"', $actionValue, $opposingAccount->name));
|
||||
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')
|
||||
\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();
|
||||
$newType = TransactionType::whereType(TransactionType::DEPOSIT)->first();
|
||||
|
||||
DB::table('transaction_journals')
|
||||
\DB::table('transaction_journals')
|
||||
->where('id', '=', $journal->id)
|
||||
->update(['transaction_type_id' => $newType->id, 'bill_id' => null]);
|
||||
|
||||
Log::debug('Converted transfer to deposit.');
|
||||
app('log')->debug('Converted transfer to deposit.');
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@@ -24,7 +24,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,10 +35,8 @@ use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Models\TransactionType;
|
||||
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
||||
use FireflyIII\TransactionRules\Expressions\ActionExpressionEvaluator;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
/**
|
||||
*
|
||||
* Class ConvertToTransfer
|
||||
*/
|
||||
class ConvertToTransfer implements ActionInterface
|
||||
@@ -49,42 +46,43 @@ class ConvertToTransfer implements ActionInterface
|
||||
|
||||
/**
|
||||
* TriggerInterface constructor.
|
||||
*
|
||||
* @param RuleAction $action
|
||||
*/
|
||||
public function __construct(RuleAction $action, ActionExpressionEvaluator $evaluator)
|
||||
{
|
||||
$this->action = $action;
|
||||
$this->action = $action;
|
||||
$this->evaluator = $evaluator;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
|
||||
* @SuppressWarnings(PHPMD.NPathComplexity)
|
||||
*/
|
||||
public function actOnArray(array $journal): bool
|
||||
{
|
||||
$actionValue = $this->evaluator->evaluate($journal);
|
||||
|
||||
// make object from array (so the data is fresh).
|
||||
/** @var TransactionJournal|null $object */
|
||||
$object = TransactionJournal::where('user_id', $journal['user_id'])->find($journal['transaction_journal_id']);
|
||||
/** @var null|TransactionJournal $object */
|
||||
$object = TransactionJournal::where('user_id', $journal['user_id'])->find($journal['transaction_journal_id']);
|
||||
if (null === $object) {
|
||||
Log::error(sprintf('Cannot find journal #%d, cannot convert to transfer.', $journal['transaction_journal_id']));
|
||||
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();
|
||||
$groupCount = TransactionJournal::where('transaction_group_id', $journal['transaction_group_id'])->count();
|
||||
if ($groupCount > 1) {
|
||||
Log::error(sprintf('Group #%d has more than one transaction in it, cannot convert to transfer.', $journal['transaction_group_id']));
|
||||
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;
|
||||
}
|
||||
|
||||
$type = $object->transactionType->type;
|
||||
$user = $object->user;
|
||||
$journalId = (int)$object->id;
|
||||
$type = $object->transactionType->type;
|
||||
$user = $object->user;
|
||||
$journalId = $object->id;
|
||||
if (TransactionType::TRANSFER === $type) {
|
||||
Log::error(
|
||||
app('log')->error(
|
||||
sprintf('Journal #%d is already a transfer so cannot be converted (rule #%d).', $object->id, $this->action->rule_id)
|
||||
);
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.is_already_transfer')));
|
||||
@@ -93,12 +91,13 @@ 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;
|
||||
}
|
||||
|
||||
// find the asset account in the action value.
|
||||
/** @var AccountRepositoryInterface $repository */
|
||||
$repository = app(AccountRepositoryInterface::class);
|
||||
$repository = app(AccountRepositoryInterface::class);
|
||||
$repository->setUser($user);
|
||||
$expectedType = null;
|
||||
if (TransactionType::WITHDRAWAL === $type) {
|
||||
@@ -109,10 +108,10 @@ class ConvertToTransfer implements ActionInterface
|
||||
$expectedType = $this->getDestinationType($journalId);
|
||||
// Deposit? Replace source with account with same type as destination.
|
||||
}
|
||||
$opposing = $repository->findByName($actionValue, [$expectedType]);
|
||||
$opposing = $repository->findByName($actionValue, [$expectedType]);
|
||||
|
||||
if (null === $opposing) {
|
||||
Log::error(
|
||||
app('log')->error(
|
||||
sprintf(
|
||||
'Journal #%d cannot be converted because no valid %s account with name "%s" exists (rule #%d).',
|
||||
$expectedType,
|
||||
@@ -127,68 +126,65 @@ class ConvertToTransfer implements ActionInterface
|
||||
}
|
||||
|
||||
if (TransactionType::WITHDRAWAL === $type) {
|
||||
Log::debug('Going to transform a withdrawal to a transfer.');
|
||||
app('log')->debug('Going to transform a withdrawal to a transfer.');
|
||||
|
||||
try {
|
||||
$res = $this->convertWithdrawalArray($object, $opposing);
|
||||
} catch (FireflyException $e) {
|
||||
Log::debug('Could not convert withdrawal to transfer.');
|
||||
Log::error($e->getMessage());
|
||||
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;
|
||||
}
|
||||
if (TransactionType::DEPOSIT === $type) {
|
||||
Log::debug('Going to transform a deposit to a transfer.');
|
||||
try {
|
||||
$res = $this->convertDepositArray($object, $opposing);
|
||||
} catch (FireflyException $e) {
|
||||
Log::debug('Could not convert deposit to transfer.');
|
||||
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;
|
||||
// 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;
|
||||
}
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.unsupported_transaction_type_transfer', ['type' => $type])));
|
||||
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 $journal */
|
||||
/** @var null|TransactionJournal $journal */
|
||||
$journal = TransactionJournal::find($journalId);
|
||||
if (null === $journal) {
|
||||
Log::error(sprintf('Journal #%d does not exist. Cannot convert to transfer.', $journalId));
|
||||
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 $journal */
|
||||
/** @var null|TransactionJournal $journal */
|
||||
$journal = TransactionJournal::find($journalId);
|
||||
if (null === $journal) {
|
||||
Log::error(sprintf('Journal #%d does not exist. Cannot convert to transfer.', $journalId));
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -197,17 +193,13 @@ 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
|
||||
{
|
||||
$sourceAccount = $this->getSourceAccount($journal);
|
||||
if ((int)$sourceAccount->id === (int)$opposing->id) {
|
||||
Log::error(
|
||||
if ($sourceAccount->id === $opposing->id) {
|
||||
app('log')->error(
|
||||
vsprintf(
|
||||
'Journal #%d has already has "%s" as a source asset. ConvertToTransfer failed. (rule #%d).',
|
||||
[$journal->id, $opposing->name, $this->action->rule_id]
|
||||
@@ -219,36 +211,34 @@ class ConvertToTransfer implements ActionInterface
|
||||
}
|
||||
|
||||
// update destination transaction:
|
||||
DB::table('transactions')
|
||||
\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();
|
||||
$newType = TransactionType::whereType(TransactionType::TRANSFER)->first();
|
||||
|
||||
DB::table('transaction_journals')
|
||||
\DB::table('transaction_journals')
|
||||
->where('id', '=', $journal->id)
|
||||
->update(['transaction_type_id' => $newType->id, 'bill_id' => null]);
|
||||
|
||||
Log::debug('Converted withdrawal to transfer.');
|
||||
app('log')->debug('Converted withdrawal to transfer.');
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @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;
|
||||
}
|
||||
|
||||
@@ -256,17 +246,13 @@ 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
|
||||
{
|
||||
$destAccount = $this->getDestinationAccount($journal);
|
||||
if ((int)$destAccount->id === (int)$opposing->id) {
|
||||
Log::error(
|
||||
if ($destAccount->id === $opposing->id) {
|
||||
app('log')->error(
|
||||
vsprintf(
|
||||
'Journal #%d has already has "%s" as a destination asset. ConvertToTransfer failed. (rule #%d).',
|
||||
[$journal->id, $opposing->name, $this->action->rule_id]
|
||||
@@ -278,36 +264,34 @@ class ConvertToTransfer implements ActionInterface
|
||||
}
|
||||
|
||||
// update source transaction:
|
||||
DB::table('transactions')
|
||||
\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();
|
||||
$newType = TransactionType::whereType(TransactionType::TRANSFER)->first();
|
||||
|
||||
DB::table('transaction_journals')
|
||||
\DB::table('transaction_journals')
|
||||
->where('id', '=', $journal->id)
|
||||
->update(['transaction_type_id' => $newType->id, 'bill_id' => null]);
|
||||
|
||||
Log::debug('Converted deposit to transfer.');
|
||||
app('log')->debug('Converted deposit to transfer.');
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @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;
|
||||
}
|
||||
}
|
||||
|
@@ -24,7 +24,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;
|
||||
@@ -37,11 +36,8 @@ use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Models\TransactionType;
|
||||
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
||||
use FireflyIII\TransactionRules\Expressions\ActionExpressionEvaluator;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use JsonException;
|
||||
|
||||
/**
|
||||
*
|
||||
* Class ConvertToWithdrawal
|
||||
*/
|
||||
class ConvertToWithdrawal implements ActionInterface
|
||||
@@ -51,8 +47,6 @@ class ConvertToWithdrawal implements ActionInterface
|
||||
|
||||
/**
|
||||
* TriggerInterface constructor.
|
||||
*
|
||||
* @param RuleAction $action
|
||||
*/
|
||||
public function __construct(RuleAction $action, ActionExpressionEvaluator $evaluator)
|
||||
{
|
||||
@@ -60,90 +54,88 @@ class ConvertToWithdrawal implements ActionInterface
|
||||
$this->evaluator = $evaluator;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function actOnArray(array $journal): bool
|
||||
{
|
||||
$actionValue = $this->evaluator->evaluate($journal);
|
||||
|
||||
// make object from array (so the data is fresh).
|
||||
/** @var TransactionJournal|null $object */
|
||||
$object = TransactionJournal::where('user_id', $journal['user_id'])->find($journal['transaction_journal_id']);
|
||||
/** @var null|TransactionJournal $object */
|
||||
$object = TransactionJournal::where('user_id', $journal['user_id'])->find($journal['transaction_journal_id']);
|
||||
if (null === $object) {
|
||||
Log::error(sprintf('Cannot find journal #%d, cannot convert to withdrawal.', $journal['transaction_journal_id']));
|
||||
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) {
|
||||
Log::error(sprintf('Group #%d has more than one transaction in it, cannot convert to withdrawal.', $journal['transaction_group_id']));
|
||||
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;
|
||||
}
|
||||
|
||||
$type = $object->transactionType->type;
|
||||
$type = $object->transactionType->type;
|
||||
if (TransactionType::WITHDRAWAL === $type) {
|
||||
Log::error(sprintf('Journal #%d is already a withdrawal (rule #%d).', $journal['transaction_journal_id'], $this->action->rule_id));
|
||||
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) {
|
||||
Log::debug('Going to transform a deposit to a withdrawal.');
|
||||
app('log')->debug('Going to transform a deposit to a withdrawal.');
|
||||
|
||||
try {
|
||||
$res = $this->convertDepositArray($object, $actionValue);
|
||||
} catch (JsonException | FireflyException $e) {
|
||||
Log::debug('Could not convert transfer to deposit.');
|
||||
Log::error($e->getMessage());
|
||||
} catch (FireflyException $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));
|
||||
|
||||
return $res;
|
||||
}
|
||||
if (TransactionType::TRANSFER === $type) {
|
||||
Log::debug('Going to transform a transfer to a withdrawal.');
|
||||
// can only be transfer at this point.
|
||||
app('log')->debug('Going to transform a transfer to a withdrawal.');
|
||||
|
||||
try {
|
||||
$res = $this->convertTransferArray($object, $actionValue);
|
||||
} catch (JsonException | FireflyException $e) {
|
||||
Log::debug('Could not convert transfer to deposit.');
|
||||
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));
|
||||
try {
|
||||
$res = $this->convertTransferArray($object, $actionValue);
|
||||
} catch (FireflyException $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 $res;
|
||||
return false;
|
||||
}
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.unsupported_transaction_type_withdrawal', ['type' => $type])));
|
||||
return false;
|
||||
event(new TriggeredAuditLog($this->action->rule, $object, 'update_transaction_type', TransactionType::TRANSFER, TransactionType::WITHDRAWAL));
|
||||
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionJournal $journal
|
||||
*
|
||||
* @return bool
|
||||
* @throws FireflyException
|
||||
* @throws JsonException
|
||||
*/
|
||||
private function convertDepositArray(TransactionJournal $journal, string $actionValue): bool
|
||||
{
|
||||
$user = $journal->user;
|
||||
$user = $journal->user;
|
||||
|
||||
/** @var AccountFactory $factory */
|
||||
$factory = app(AccountFactory::class);
|
||||
$factory = app(AccountFactory::class);
|
||||
$factory->setUser($user);
|
||||
|
||||
$repository = app(AccountRepositoryInterface::class);
|
||||
$repository = app(AccountRepositoryInterface::class);
|
||||
$repository->setUser($user);
|
||||
|
||||
$sourceAccount = $this->getSourceAccount($journal);
|
||||
$destAccount = $this->getDestinationAccount($journal);
|
||||
$sourceAccount = $this->getSourceAccount($journal);
|
||||
$destAccount = $this->getDestinationAccount($journal);
|
||||
|
||||
// get the action value, or use the original source name in case the action value is empty:
|
||||
// this becomes a new or existing (expense) account, which is the destination of the new withdrawal.
|
||||
@@ -155,60 +147,56 @@ class ConvertToWithdrawal implements ActionInterface
|
||||
$opposingAccount = $factory->findOrCreate($opposingName, AccountType::EXPENSE);
|
||||
}
|
||||
|
||||
Log::debug(sprintf('ConvertToWithdrawal. Action value is "%s", expense name is "%s"', $actionValue, $opposingName));
|
||||
app('log')->debug(sprintf('ConvertToWithdrawal. Action value is "%s", expense name is "%s"', $actionValue, $opposingName));
|
||||
|
||||
// update source transaction(s) to be the original destination account
|
||||
DB::table('transactions')
|
||||
\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')
|
||||
\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')
|
||||
$newType = TransactionType::whereType(TransactionType::WITHDRAWAL)->first();
|
||||
\DB::table('transaction_journals')
|
||||
->where('id', '=', $journal->id)
|
||||
->update(['transaction_type_id' => $newType->id]);
|
||||
|
||||
Log::debug('Converted deposit to withdrawal.');
|
||||
app('log')->debug('Converted deposit to withdrawal.');
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @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;
|
||||
}
|
||||
|
||||
@@ -216,28 +204,25 @@ 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
|
||||
*/
|
||||
private function convertTransferArray(TransactionJournal $journal, string $actionValue): bool
|
||||
{
|
||||
// find or create expense account.
|
||||
$user = $journal->user;
|
||||
$user = $journal->user;
|
||||
|
||||
/** @var AccountFactory $factory */
|
||||
$factory = app(AccountFactory::class);
|
||||
$factory = app(AccountFactory::class);
|
||||
$factory->setUser($user);
|
||||
|
||||
$repository = app(AccountRepositoryInterface::class);
|
||||
$repository = app(AccountRepositoryInterface::class);
|
||||
$repository->setUser($user);
|
||||
|
||||
$destAccount = $this->getDestinationAccount($journal);
|
||||
$destAccount = $this->getDestinationAccount($journal);
|
||||
|
||||
// get the action value, or use the original source name in case the action value is empty:
|
||||
// this becomes a new or existing (expense) account, which is the destination of the new withdrawal.
|
||||
$opposingName = '' === $actionValue ? $destAccount->name : $actionValue;
|
||||
$opposingName = '' === $actionValue ? $destAccount->name : $actionValue;
|
||||
// we check all possible source account types if one exists:
|
||||
$validTypes = config('firefly.expected_source_types.destination.Withdrawal');
|
||||
$opposingAccount = $repository->findByName($opposingName, $validTypes);
|
||||
@@ -245,21 +230,21 @@ class ConvertToWithdrawal implements ActionInterface
|
||||
$opposingAccount = $factory->findOrCreate($opposingName, AccountType::EXPENSE);
|
||||
}
|
||||
|
||||
Log::debug(sprintf('ConvertToWithdrawal. Action value is "%s", destination name is "%s"', $actionValue, $opposingName));
|
||||
app('log')->debug(sprintf('ConvertToWithdrawal. Action value is "%s", destination name is "%s"', $actionValue, $opposingName));
|
||||
|
||||
// update destination transaction(s) to be new expense account.
|
||||
DB::table('transactions')
|
||||
\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')
|
||||
$newType = TransactionType::whereType(TransactionType::WITHDRAWAL)->first();
|
||||
\DB::table('transaction_journals')
|
||||
->where('id', '=', $journal->id)
|
||||
->update(['transaction_type_id' => $newType->id]);
|
||||
|
||||
Log::debug('Converted transfer to withdrawal.');
|
||||
app('log')->debug('Converted transfer to withdrawal.');
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@@ -29,7 +29,6 @@ use FireflyIII\Models\TransactionGroup;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Services\Internal\Destroy\JournalDestroyService;
|
||||
use FireflyIII\Services\Internal\Destroy\TransactionGroupDestroyService;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
/**
|
||||
* Class DeleteTransaction.
|
||||
@@ -40,30 +39,27 @@ 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();
|
||||
$count = TransactionJournal::where('transaction_group_id', $journal['transaction_group_id'])->count();
|
||||
|
||||
// destroy entire group.
|
||||
if (1 === $count) {
|
||||
Log::debug(
|
||||
app('log')->debug(
|
||||
sprintf(
|
||||
'RuleAction DeleteTransaction DELETED the entire transaction group of journal #%d ("%s").',
|
||||
$journal['transaction_journal_id'],
|
||||
$journal['description']
|
||||
)
|
||||
);
|
||||
|
||||
/** @var TransactionGroup $group */
|
||||
$group = TransactionGroup::find($journal['transaction_group_id']);
|
||||
$service = app(TransactionGroupDestroyService::class);
|
||||
$service->destroy($group);
|
||||
@@ -72,12 +68,13 @@ class DeleteTransaction implements ActionInterface
|
||||
|
||||
return true;
|
||||
}
|
||||
Log::debug(
|
||||
app('log')->debug(
|
||||
sprintf('RuleAction DeleteTransaction DELETED transaction journal #%d ("%s").', $journal['transaction_journal_id'], $journal['description'])
|
||||
);
|
||||
|
||||
// trigger delete factory:
|
||||
$object = TransactionJournal::find($journal['transaction_group_id']);
|
||||
/** @var null|TransactionJournal $object */
|
||||
$object = TransactionJournal::find($journal['transaction_journal_id']);
|
||||
if (null !== $object) {
|
||||
/** @var JournalDestroyService $service */
|
||||
$service = app(JournalDestroyService::class);
|
||||
|
@@ -24,7 +24,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;
|
||||
@@ -33,7 +32,6 @@ use FireflyIII\Models\TransactionType;
|
||||
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
|
||||
use FireflyIII\TransactionRules\Expressions\ActionExpressionEvaluator;
|
||||
use FireflyIII\User;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
/**
|
||||
* Class LinkToBill.
|
||||
@@ -45,9 +43,6 @@ class LinkToBill implements ActionInterface
|
||||
|
||||
/**
|
||||
* TriggerInterface constructor.
|
||||
*
|
||||
*
|
||||
* @param RuleAction $action
|
||||
*/
|
||||
public function __construct(RuleAction $action, ActionExpressionEvaluator $evaluator)
|
||||
{
|
||||
@@ -55,23 +50,22 @@ class LinkToBill implements ActionInterface
|
||||
$this->evaluator = $evaluator;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function actOnArray(array $journal): bool
|
||||
{
|
||||
$user = User::find($journal['user_id']);
|
||||
/** @var User $user */
|
||||
$user = User::find($journal['user_id']);
|
||||
|
||||
/** @var BillRepositoryInterface $repository */
|
||||
$repository = app(BillRepositoryInterface::class);
|
||||
$repository->setUser($user);
|
||||
$billName = $this->evaluator->evaluate($journal);
|
||||
$bill = $repository->findByName($billName);
|
||||
$billName = $this->evaluator->evaluate($journal);
|
||||
$bill = $repository->findByName($billName);
|
||||
|
||||
if (null !== $bill && $journal['transaction_type_type'] === TransactionType::WITHDRAWAL) {
|
||||
$count = DB::table('transaction_journals')->where('id', '=', $journal['transaction_journal_id'])
|
||||
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) {
|
||||
Log::error(
|
||||
app('log')->error(
|
||||
sprintf(
|
||||
'RuleAction LinkToBill could not set the bill of journal #%d to bill "%s": already set.',
|
||||
$journal['transaction_journal_id'],
|
||||
@@ -79,24 +73,25 @@ class LinkToBill implements ActionInterface
|
||||
)
|
||||
);
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.already_linked_to_subscription', ['name' => $billName])));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
DB::table('transaction_journals')
|
||||
\DB::table('transaction_journals')
|
||||
->where('id', '=', $journal['transaction_journal_id'])
|
||||
->update(['bill_id' => $bill->id]);
|
||||
Log::debug(
|
||||
app('log')->debug(
|
||||
sprintf('RuleAction LinkToBill set the bill of journal #%d to bill #%d ("%s").', $journal['transaction_journal_id'], $bill->id, $bill->name)
|
||||
);
|
||||
|
||||
/** @var TransactionJournal $object */
|
||||
$object = TransactionJournal::where('user_id', $journal['user_id'])->find($journal['transaction_journal_id']);
|
||||
event(new TriggeredAuditLog($this->action->rule, $object, 'set_bill', null, $bill->name));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
Log::error(
|
||||
app('log')->error(
|
||||
sprintf(
|
||||
'RuleAction LinkToBill could not set the bill of journal #%d to bill "%s": no such bill found or not a withdrawal.',
|
||||
$journal['transaction_journal_id'],
|
||||
@@ -104,6 +99,7 @@ class LinkToBill implements ActionInterface
|
||||
)
|
||||
);
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.cannot_find_subscription', ['name' => $billName])));
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@@ -29,7 +29,6 @@ use FireflyIII\Events\TriggeredAuditLog;
|
||||
use FireflyIII\Models\Note;
|
||||
use FireflyIII\Models\RuleAction;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
/**
|
||||
* Class MoveDescriptionToNotes
|
||||
@@ -40,30 +39,27 @@ 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 $object */
|
||||
$object = TransactionJournal::where('user_id', $journal['user_id'])->find($journal['transaction_journal_id']);
|
||||
/** @var null|TransactionJournal $object */
|
||||
$object = TransactionJournal::where('user_id', $journal['user_id'])->find($journal['transaction_journal_id']);
|
||||
if (null === $object) {
|
||||
Log::error(sprintf('No journal #%d belongs to user #%d.', $journal['transaction_journal_id'], $journal['user_id']));
|
||||
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();
|
||||
|
||||
/** @var null|Note $note */
|
||||
$note = $object->notes()->first();
|
||||
if (null === $note) {
|
||||
$note = new Note();
|
||||
$note = new Note();
|
||||
$note->noteable()->associate($object);
|
||||
$note->text = '';
|
||||
}
|
||||
@@ -77,13 +73,14 @@ class MoveDescriptionToNotes implements ActionInterface
|
||||
$note->text = (string)$object->description;
|
||||
$object->description = '(no description)';
|
||||
}
|
||||
$after = $note->text;
|
||||
$after = $note->text;
|
||||
|
||||
event(new TriggeredAuditLog($this->action->rule, $object, 'update_description', $beforeDescription, $object->description));
|
||||
event(new TriggeredAuditLog($this->action->rule, $object, 'update_notes', $before, $after));
|
||||
|
||||
$note->save();
|
||||
$object->save();
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@@ -29,7 +29,6 @@ use FireflyIII\Events\TriggeredAuditLog;
|
||||
use FireflyIII\Models\RuleAction;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Support\Request\ConvertsDataTypes;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
/**
|
||||
* Class MoveNotesToDescription
|
||||
@@ -46,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 $object */
|
||||
$object = TransactionJournal::where('user_id', $journal['user_id'])->find($journal['transaction_journal_id']);
|
||||
/** @var null|TransactionJournal $object */
|
||||
$object = TransactionJournal::where('user_id', $journal['user_id'])->find($journal['transaction_journal_id']);
|
||||
if (null === $object) {
|
||||
Log::error(sprintf('No journal #%d belongs to user #%d.', $journal['transaction_journal_id'], $journal['user_id']));
|
||||
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();
|
||||
$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;
|
||||
}
|
||||
@@ -77,11 +72,12 @@ 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;
|
||||
$beforeNote = $note->text;
|
||||
$object->description = (string)$this->clearString($note->text, false);
|
||||
$object->description = (string)$this->clearString($note->text);
|
||||
$object->save();
|
||||
$note->delete();
|
||||
|
||||
@@ -92,7 +88,7 @@ class MoveNotesToDescription implements ActionInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
||||
*/
|
||||
public function get(string $key, mixed $default = null): mixed
|
||||
{
|
||||
@@ -100,7 +96,7 @@ class MoveNotesToDescription implements ActionInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
||||
*/
|
||||
public function has(mixed $key): mixed
|
||||
{
|
||||
|
@@ -24,7 +24,6 @@ declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\TransactionRules\Actions;
|
||||
|
||||
use DB;
|
||||
use FireflyIII\Events\TriggeredAuditLog;
|
||||
use FireflyIII\Models\RuleAction;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
@@ -40,8 +39,6 @@ class PrependDescription implements ActionInterface
|
||||
|
||||
/**
|
||||
* TriggerInterface constructor.
|
||||
*
|
||||
* @param RuleAction $action
|
||||
*/
|
||||
public function __construct(RuleAction $action, ActionExpressionEvaluator $evaluator)
|
||||
{
|
||||
@@ -49,16 +46,13 @@ class PrependDescription implements ActionInterface
|
||||
$this->evaluator = $evaluator;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function actOnArray(array $journal): bool
|
||||
{
|
||||
$actionValue = $this->evaluator->evaluate($journal);
|
||||
|
||||
$before = $journal['description'];
|
||||
$after = sprintf('%s%s', $actionValue, $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 */
|
||||
|
@@ -29,7 +29,6 @@ use FireflyIII\Models\Note;
|
||||
use FireflyIII\Models\RuleAction;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\TransactionRules\Expressions\ActionExpressionEvaluator;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
/**
|
||||
* Class PrependNotes.
|
||||
@@ -41,23 +40,17 @@ class PrependNotes implements ActionInterface
|
||||
|
||||
/**
|
||||
* TriggerInterface constructor.
|
||||
*
|
||||
* @param RuleAction $action
|
||||
*/
|
||||
public function __construct(RuleAction $action, ActionExpressionEvaluator $evaluator)
|
||||
{
|
||||
$this->action = $action;
|
||||
$this->action = $action;
|
||||
$this->evaluator = $evaluator;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function actOnArray(array $journal): bool
|
||||
{
|
||||
$actionValue = $this->evaluator->evaluate($journal);
|
||||
|
||||
$dbNote = Note::where('noteable_id', (int)$journal['transaction_journal_id'])
|
||||
$actionValue = $this->evaluator->evaluate($journal);
|
||||
$dbNote = Note::where('noteable_id', (int)$journal['transaction_journal_id'])
|
||||
->where('noteable_type', TransactionJournal::class)
|
||||
->first(['notes.*']);
|
||||
if (null === $dbNote) {
|
||||
@@ -66,15 +59,15 @@ class PrependNotes implements ActionInterface
|
||||
$dbNote->noteable_type = TransactionJournal::class;
|
||||
$dbNote->text = '';
|
||||
}
|
||||
$before = $dbNote->text;
|
||||
Log::debug(sprintf('RuleAction PrependNotes prepended "%s" to "%s".', $actionValue, $dbNote->text));
|
||||
$before = $dbNote->text;
|
||||
app('log')->debug(sprintf('RuleAction PrependNotes prepended "%s" to "%s".', $actionValue, $dbNote->text));
|
||||
$text = sprintf('%s%s', $actionValue, $dbNote->text);
|
||||
$dbNote->text = $text;
|
||||
$dbNote->save();
|
||||
|
||||
// journal
|
||||
/** @var TransactionJournal $object */
|
||||
$object = TransactionJournal::where('user_id', $journal['user_id'])->find($journal['transaction_journal_id']);
|
||||
$object = TransactionJournal::where('user_id', $journal['user_id'])->find($journal['transaction_journal_id']);
|
||||
|
||||
// audit log
|
||||
event(new TriggeredAuditLog($this->action->rule, $object, 'update_notes', $before, $text));
|
||||
|
@@ -23,12 +23,10 @@ declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\TransactionRules\Actions;
|
||||
|
||||
use DB;
|
||||
use FireflyIII\Events\Model\Rule\RuleActionFailedOnArray;
|
||||
use FireflyIII\Events\TriggeredAuditLog;
|
||||
use FireflyIII\Models\RuleAction;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
/**
|
||||
* Class RemoveAllTags.
|
||||
@@ -39,27 +37,23 @@ 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) {
|
||||
Log::debug(sprintf('RuleAction RemoveAllTags, journal #%d has no tags.', $journal['transaction_journal_id']));
|
||||
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;
|
||||
}
|
||||
Log::debug(sprintf('RuleAction RemoveAllTags removed all tags from journal %d.', $journal['transaction_journal_id']));
|
||||
app('log')->debug(sprintf('RuleAction RemoveAllTags removed all tags from journal %d.', $journal['transaction_journal_id']));
|
||||
|
||||
/** @var TransactionJournal $object */
|
||||
$object = TransactionJournal::where('user_id', $journal['user_id'])->find($journal['transaction_journal_id']);
|
||||
|
@@ -24,14 +24,12 @@ declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\TransactionRules\Actions;
|
||||
|
||||
use DB;
|
||||
use FireflyIII\Events\Model\Rule\RuleActionFailedOnArray;
|
||||
use FireflyIII\Events\TriggeredAuditLog;
|
||||
use FireflyIII\Models\RuleAction;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\TransactionRules\Expressions\ActionExpressionEvaluator;
|
||||
use FireflyIII\User;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
/**
|
||||
* Class RemoveTag.
|
||||
@@ -43,8 +41,6 @@ class RemoveTag implements ActionInterface
|
||||
|
||||
/**
|
||||
* TriggerInterface constructor.
|
||||
*
|
||||
* @param RuleAction $action
|
||||
*/
|
||||
public function __construct(RuleAction $action, ActionExpressionEvaluator $evaluator)
|
||||
{
|
||||
@@ -52,34 +48,35 @@ class RemoveTag implements ActionInterface
|
||||
$this->evaluator = $evaluator;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function actOnArray(array $journal): bool
|
||||
{
|
||||
// if tag does not exist, no need to continue:
|
||||
$name = $this->evaluator->evaluate($journal);
|
||||
$user = User::find($journal['user_id']);
|
||||
$tag = $user->tags()->where('tag', $name)->first();
|
||||
$name = $this->evaluator->evaluate($journal);
|
||||
|
||||
/** @var User $user */
|
||||
$user = User::find($journal['user_id']);
|
||||
$tag = $user->tags()->where('tag', $name)->first();
|
||||
|
||||
if (null === $tag) {
|
||||
Log::debug(
|
||||
app('log')->debug(
|
||||
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) {
|
||||
Log::debug(
|
||||
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;
|
||||
}
|
||||
|
||||
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')
|
||||
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();
|
||||
|
@@ -24,7 +24,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;
|
||||
@@ -32,7 +31,6 @@ use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Models\TransactionType;
|
||||
use FireflyIII\TransactionRules\Expressions\ActionExpressionEvaluator;
|
||||
use FireflyIII\User;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
/**
|
||||
* Class SetBudget.
|
||||
@@ -44,8 +42,6 @@ class SetBudget implements ActionInterface
|
||||
|
||||
/**
|
||||
* TriggerInterface constructor.
|
||||
*
|
||||
* @param RuleAction $action
|
||||
*/
|
||||
public function __construct(RuleAction $action, ActionExpressionEvaluator $evaluator)
|
||||
{
|
||||
@@ -53,17 +49,15 @@ class SetBudget implements ActionInterface
|
||||
$this->evaluator = $evaluator;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function actOnArray(array $journal): bool
|
||||
{
|
||||
$user = User::find($journal['user_id']);
|
||||
$search = $this->evaluator->evaluate($journal);
|
||||
/** @var User $user */
|
||||
$user = User::find($journal['user_id']);
|
||||
$search = $this->evaluator->evaluate($journal);
|
||||
|
||||
$budget = $user->budgets()->where('name', $search)->first();
|
||||
$budget = $user->budgets()->where('name', $search)->first();
|
||||
if (null === $budget) {
|
||||
Log::debug(
|
||||
app('log')->debug(
|
||||
sprintf(
|
||||
'RuleAction SetBudget could not set budget of journal #%d to "%s" because no such budget exists.',
|
||||
$journal['transaction_journal_id'],
|
||||
@@ -71,11 +65,12 @@ class SetBudget implements ActionInterface
|
||||
)
|
||||
);
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.cannot_find_budget', ['name' => $search])));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (TransactionType::WITHDRAWAL !== $journal['transaction_type_type']) {
|
||||
Log::debug(
|
||||
app('log')->debug(
|
||||
sprintf(
|
||||
'RuleAction SetBudget could not set budget of journal #%d to "%s" because journal is a %s.',
|
||||
$journal['transaction_journal_id'],
|
||||
@@ -84,6 +79,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;
|
||||
}
|
||||
|
||||
@@ -92,21 +88,21 @@ class SetBudget implements ActionInterface
|
||||
$object = $user->transactionJournals()->find($journal['transaction_journal_id']);
|
||||
$oldBudget = $object->budgets()->first();
|
||||
$oldBudgetName = $oldBudget?->name;
|
||||
if ((int)$oldBudget?->id === (int)$budget->id) {
|
||||
if ((int)$oldBudget?->id === $budget->id) {
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.already_linked_to_budget', ['name' => $budget->name])));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
Log::debug(
|
||||
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']);
|
||||
$object = TransactionJournal::where('user_id', $journal['user_id'])->find($journal['transaction_journal_id']);
|
||||
event(new TriggeredAuditLog($this->action->rule, $object, 'set_budget', $oldBudgetName, $budget->name));
|
||||
|
||||
return true;
|
||||
|
@@ -24,7 +24,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;
|
||||
@@ -32,7 +31,6 @@ use FireflyIII\Models\RuleAction;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\TransactionRules\Expressions\ActionExpressionEvaluator;
|
||||
use FireflyIII\User;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
/**
|
||||
* Class SetCategory.
|
||||
@@ -44,8 +42,6 @@ class SetCategory implements ActionInterface
|
||||
|
||||
/**
|
||||
* TriggerInterface constructor.
|
||||
*
|
||||
* @param RuleAction $action
|
||||
*/
|
||||
public function __construct(RuleAction $action, ActionExpressionEvaluator $evaluator)
|
||||
{
|
||||
@@ -53,25 +49,24 @@ class SetCategory implements ActionInterface
|
||||
$this->evaluator = $evaluator;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function actOnArray(array $journal): bool
|
||||
{
|
||||
$user = User::find($journal['user_id']);
|
||||
$search = $this->evaluator->evaluate($journal);
|
||||
/** @var null|User $user */
|
||||
$user = User::find($journal['user_id']);
|
||||
$search = $this->evaluator->evaluate($journal);
|
||||
if (null === $user) {
|
||||
Log::error(sprintf('Journal has no valid user ID so action SetCategory("%s") cannot be applied', $search), $journal);
|
||||
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;
|
||||
}
|
||||
|
||||
/** @var CategoryFactory $factory */
|
||||
$factory = app(CategoryFactory::class);
|
||||
$factory = app(CategoryFactory::class);
|
||||
$factory->setUser($user);
|
||||
$category = $factory->findOrCreate(null, $search);
|
||||
$category = $factory->findOrCreate(null, $search);
|
||||
if (null === $category) {
|
||||
Log::debug(
|
||||
app('log')->debug(
|
||||
sprintf(
|
||||
'RuleAction SetCategory could not set category of journal #%d to "%s" because no such category exists.',
|
||||
$journal['transaction_journal_id'],
|
||||
@@ -79,10 +74,11 @@ class SetCategory implements ActionInterface
|
||||
)
|
||||
);
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.cannot_find_category', ['name' => $search])));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
Log::debug(
|
||||
app('log')->debug(
|
||||
sprintf(
|
||||
'RuleAction SetCategory set the category of journal #%d to category #%d ("%s").',
|
||||
$journal['transaction_journal_id'],
|
||||
@@ -96,16 +92,17 @@ class SetCategory implements ActionInterface
|
||||
$object = $user->transactionJournals()->find($journal['transaction_journal_id']);
|
||||
$oldCategory = $object->categories()->first();
|
||||
$oldCategoryName = $oldCategory?->name;
|
||||
if ((int)$oldCategory?->id === (int)$category->id) {
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.already_linked_to_category', ['name' => $category->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']);
|
||||
$object = TransactionJournal::where('user_id', $journal['user_id'])->find($journal['transaction_journal_id']);
|
||||
event(new TriggeredAuditLog($this->action->rule, $object, 'set_category', $oldCategoryName, $category->name));
|
||||
|
||||
return true;
|
||||
|
@@ -24,12 +24,10 @@ declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\TransactionRules\Actions;
|
||||
|
||||
use DB;
|
||||
use FireflyIII\Events\TriggeredAuditLog;
|
||||
use FireflyIII\Models\RuleAction;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\TransactionRules\Expressions\ActionExpressionEvaluator;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
/**
|
||||
* Class SetDescription.
|
||||
@@ -41,8 +39,6 @@ class SetDescription implements ActionInterface
|
||||
|
||||
/**
|
||||
* TriggerInterface constructor.
|
||||
*
|
||||
* @param RuleAction $action
|
||||
*/
|
||||
public function __construct(RuleAction $action, ActionExpressionEvaluator $evaluator)
|
||||
{
|
||||
@@ -50,9 +46,6 @@ class SetDescription implements ActionInterface
|
||||
$this->evaluator = $evaluator;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function actOnArray(array $journal): bool
|
||||
{
|
||||
/** @var TransactionJournal $object */
|
||||
@@ -60,11 +53,11 @@ class SetDescription implements ActionInterface
|
||||
$before = $journal['description'];
|
||||
$after = $this->evaluator->evaluate($journal);
|
||||
|
||||
DB::table('transaction_journals')
|
||||
\DB::table('transaction_journals')
|
||||
->where('id', '=', $journal['transaction_journal_id'])
|
||||
->update(['description' => $after]);
|
||||
->update(['description' => $this->action->action_value]);
|
||||
|
||||
Log::debug(
|
||||
app('log')->debug(
|
||||
sprintf(
|
||||
'RuleAction SetDescription changed the description of journal #%d from "%s" to "%s".',
|
||||
$journal['transaction_journal_id'],
|
||||
|
@@ -24,7 +24,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;
|
||||
@@ -35,7 +34,6 @@ use FireflyIII\Models\TransactionType;
|
||||
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
||||
use FireflyIII\TransactionRules\Expressions\ActionExpressionEvaluator;
|
||||
use FireflyIII\User;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
/**
|
||||
* Class SetDestinationAccount.
|
||||
@@ -48,8 +46,6 @@ class SetDestinationAccount implements ActionInterface
|
||||
|
||||
/**
|
||||
* TriggerInterface constructor.
|
||||
*
|
||||
* @param RuleAction $action
|
||||
*/
|
||||
public function __construct(RuleAction $action, ActionExpressionEvaluator $evaluator)
|
||||
{
|
||||
@@ -57,30 +53,30 @@ class SetDestinationAccount implements ActionInterface
|
||||
$this->evaluator = $evaluator;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function actOnArray(array $journal): bool
|
||||
{
|
||||
$accountName = $this->evaluator->evaluate($journal);
|
||||
$user = User::find($journal['user_id']);
|
||||
$type = $journal['transaction_type_type'];
|
||||
/** @var TransactionJournal|null $object */
|
||||
|
||||
/** @var User $user */
|
||||
$user = User::find($journal['user_id']);
|
||||
|
||||
/** @var null|TransactionJournal $object */
|
||||
$object = $user->transactionJournals()->find((int)$journal['transaction_journal_id']);
|
||||
$this->repository = app(AccountRepositoryInterface::class);
|
||||
|
||||
if (null === $object) {
|
||||
Log::error('Could not find journal.');
|
||||
app('log')->error('Could not find journal.');
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.no_such_journal')));
|
||||
|
||||
return false;
|
||||
}
|
||||
$type = $object->transactionType->type;
|
||||
$type = $object->transactionType->type;
|
||||
$this->repository->setUser($user);
|
||||
|
||||
// if this is a transfer or a deposit, the new destination account must be an asset account or a default account, and it MUST exist:
|
||||
$newAccount = $this->findAssetAccount($type, $accountName);
|
||||
$newAccount = $this->findAssetAccount($type, $accountName);
|
||||
if ((TransactionType::DEPOSIT === $type || TransactionType::TRANSFER === $type) && null === $newAccount) {
|
||||
Log::error(
|
||||
app('log')->error(
|
||||
sprintf(
|
||||
'Cant change destination account of journal #%d because no asset account with name "%s" exists.',
|
||||
$object->id,
|
||||
@@ -88,25 +84,28 @@ class SetDestinationAccount implements ActionInterface
|
||||
)
|
||||
);
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.cannot_find_asset', ['name' => $accountName])));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// new destination account must be different from the current source account:
|
||||
/** @var Transaction|null $source */
|
||||
$source = $object->transactions()->where('amount', '<', 0)->first();
|
||||
/** @var null|Transaction $source */
|
||||
$source = $object->transactions()->where('amount', '<', 0)->first();
|
||||
if (null === $source) {
|
||||
Log::error('Could not find source transaction.');
|
||||
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) {
|
||||
Log::error('Could not find source transaction 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 && (int)$newAccount->id === (int)$source->account_id) {
|
||||
Log::error(
|
||||
if (null !== $newAccount && $newAccount->id === $source->account_id) {
|
||||
app('log')->error(
|
||||
sprintf(
|
||||
'New destination account ID #%d and current source account ID #%d are the same. Do nothing.',
|
||||
$newAccount->id,
|
||||
@@ -115,6 +114,7 @@ class SetDestinationAccount implements ActionInterface
|
||||
);
|
||||
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.already_has_destination', ['name' => $newAccount->name])));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -124,39 +124,31 @@ class SetDestinationAccount implements ActionInterface
|
||||
$newAccount = $this->findWithdrawalDestinationAccount($accountName);
|
||||
}
|
||||
|
||||
Log::debug(sprintf('New destination account is #%d ("%s").', $newAccount->id, $newAccount->name));
|
||||
app('log')->debug(sprintf('New destination account is #%d ("%s").', $newAccount->id, $newAccount->name));
|
||||
|
||||
event(new TriggeredAuditLog($this->action->rule, $object, 'set_destination', null, $newAccount->name));
|
||||
|
||||
// update destination transaction with new destination account:
|
||||
DB::table('transactions')
|
||||
\DB::table('transactions')
|
||||
->where('transaction_journal_id', '=', $object->id)
|
||||
->where('amount', '>', 0)
|
||||
->update(['account_id' => $newAccount->id]);
|
||||
|
||||
Log::debug(sprintf('Updated journal #%d (group #%d) and gave it new destination account ID.', $object->id, $object->transaction_group_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, string $accountName): ?Account
|
||||
{
|
||||
// switch on type:
|
||||
$allowed = config(sprintf('firefly.expected_source_types.destination.%s', $type));
|
||||
$allowed = is_array($allowed) ? $allowed : [];
|
||||
Log::debug(sprintf('Check config for expected_source_types.destination.%s, result is', $type), $allowed);
|
||||
app('log')->debug(sprintf('Check config for expected_source_types.destination.%s, result is', $type), $allowed);
|
||||
|
||||
return $this->repository->findByName($accountName, $allowed);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Account
|
||||
*/
|
||||
private function findWithdrawalDestinationAccount(string $accountName): Account
|
||||
{
|
||||
$allowed = config('firefly.expected_source_types.destination.Withdrawal');
|
||||
@@ -172,7 +164,7 @@ class SetDestinationAccount implements ActionInterface
|
||||
];
|
||||
$account = $this->repository->store($data);
|
||||
}
|
||||
Log::debug(sprintf('Found or created expense account #%d ("%s")', $account->id, $account->name));
|
||||
app('log')->debug(sprintf('Found or created expense account #%d ("%s")', $account->id, $account->name));
|
||||
|
||||
return $account;
|
||||
}
|
||||
|
120
app/TransactionRules/Actions/SetDestinationToCashAccount.php
Normal file
120
app/TransactionRules/Actions/SetDestinationToCashAccount.php
Normal file
@@ -0,0 +1,120 @@
|
||||
<?php
|
||||
/*
|
||||
* SetDestinationToCashAccount.php
|
||||
* Copyright (c) 2023 james@firefly-iii.org
|
||||
*
|
||||
* This file is part of Firefly III (https://github.com/firefly-iii).
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\TransactionRules\Actions;
|
||||
|
||||
use FireflyIII\Events\Model\Rule\RuleActionFailedOnArray;
|
||||
use FireflyIII\Events\TriggeredAuditLog;
|
||||
use FireflyIII\Models\RuleAction;
|
||||
use FireflyIII\Models\Transaction;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Models\TransactionType;
|
||||
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
||||
use FireflyIII\User;
|
||||
|
||||
/**
|
||||
* Class SetDestinationToCashAccount
|
||||
*/
|
||||
class SetDestinationToCashAccount implements ActionInterface
|
||||
{
|
||||
private RuleAction $action;
|
||||
|
||||
/**
|
||||
* TriggerInterface constructor.
|
||||
*/
|
||||
public function __construct(RuleAction $action)
|
||||
{
|
||||
$this->action = $action;
|
||||
}
|
||||
|
||||
public function actOnArray(array $journal): bool
|
||||
{
|
||||
/** @var User $user */
|
||||
$user = User::find($journal['user_id']);
|
||||
|
||||
/** @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;
|
||||
}
|
||||
|
||||
// find cash account
|
||||
$repository->setUser($user);
|
||||
$cashAccount = $repository->getCashAccount();
|
||||
|
||||
// new destination account must be different from the current source account:
|
||||
/** @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) {
|
||||
app('log')->error(
|
||||
sprintf(
|
||||
'New destination account ID #%d and current source account ID #%d are the same. Do nothing.',
|
||||
$cashAccount->id,
|
||||
$source->account_id
|
||||
)
|
||||
);
|
||||
|
||||
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])
|
||||
;
|
||||
|
||||
app('log')->debug(sprintf('Updated journal #%d (group #%d) and gave it new destination account ID.', $object->id, $object->transaction_group_id));
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
@@ -29,20 +29,17 @@ use FireflyIII\Models\Note;
|
||||
use FireflyIII\Models\RuleAction;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\TransactionRules\Expressions\ActionExpressionEvaluator;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
/**
|
||||
* Class SetNotes.
|
||||
*/
|
||||
class SetNotes implements ActionInterface
|
||||
{
|
||||
private RuleACtion $action;
|
||||
private RuleAction $action;
|
||||
private ActionExpressionEvaluator $evaluator;
|
||||
|
||||
/**
|
||||
* TriggerInterface constructor.
|
||||
*
|
||||
* @param RuleAction $action
|
||||
*/
|
||||
public function __construct(RuleAction $action, ActionExpressionEvaluator $evaluator)
|
||||
{
|
||||
@@ -50,12 +47,9 @@ class SetNotes implements ActionInterface
|
||||
$this->evaluator = $evaluator;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function actOnArray(array $journal): bool
|
||||
{
|
||||
$dbNote = Note::where('noteable_id', $journal['transaction_journal_id'])
|
||||
$dbNote = Note::where('noteable_id', $journal['transaction_journal_id'])
|
||||
->where('noteable_type', TransactionJournal::class)->first();
|
||||
if (null === $dbNote) {
|
||||
$dbNote = new Note();
|
||||
@@ -68,7 +62,7 @@ class SetNotes implements ActionInterface
|
||||
$dbNote->text = $newNotes;
|
||||
$dbNote->save();
|
||||
|
||||
Log::debug(
|
||||
app('log')->debug(
|
||||
sprintf(
|
||||
'RuleAction SetNotes changed the notes of journal #%d from "%s" to "%s".',
|
||||
$journal['transaction_journal_id'],
|
||||
@@ -78,7 +72,7 @@ class SetNotes implements ActionInterface
|
||||
);
|
||||
|
||||
/** @var TransactionJournal $object */
|
||||
$object = TransactionJournal::where('user_id', $journal['user_id'])->find($journal['transaction_journal_id']);
|
||||
$object = TransactionJournal::where('user_id', $journal['user_id'])->find($journal['transaction_journal_id']);
|
||||
|
||||
event(new TriggeredAuditLog($this->action->rule, $object, 'update_notes', $oldNotes, $newNotes));
|
||||
|
||||
|
@@ -24,7 +24,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;
|
||||
@@ -35,7 +34,6 @@ use FireflyIII\Models\TransactionType;
|
||||
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
||||
use FireflyIII\TransactionRules\Expressions\ActionExpressionEvaluator;
|
||||
use FireflyIII\User;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
/**
|
||||
* Class SetSourceAccount.
|
||||
@@ -48,8 +46,6 @@ class SetSourceAccount implements ActionInterface
|
||||
|
||||
/**
|
||||
* TriggerInterface constructor.
|
||||
*
|
||||
* @param RuleAction $action
|
||||
*/
|
||||
public function __construct(RuleAction $action, ActionExpressionEvaluator $evaluator)
|
||||
{
|
||||
@@ -57,51 +53,54 @@ class SetSourceAccount implements ActionInterface
|
||||
$this->evaluator = $evaluator;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function actOnArray(array $journal): bool
|
||||
{
|
||||
$user = User::find($journal['user_id']);
|
||||
$type = $journal['transaction_type_type'];
|
||||
$name = $this->evaluator->evaluate($journal);
|
||||
/** @var TransactionJournal|null $object */
|
||||
$accountName = $this->evaluator->evaluate($journal);
|
||||
|
||||
/** @var User $user */
|
||||
$user = User::find($journal['user_id']);
|
||||
|
||||
/** @var null|TransactionJournal $object */
|
||||
$object = $user->transactionJournals()->find((int)$journal['transaction_journal_id']);
|
||||
$this->repository = app(AccountRepositoryInterface::class);
|
||||
if (null === $object) {
|
||||
Log::error('Could not find journal.');
|
||||
app('log')->error('Could not find journal.');
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.no_such_journal')));
|
||||
|
||||
return false;
|
||||
}
|
||||
$type = $object->transactionType->type;
|
||||
$type = $object->transactionType->type;
|
||||
$this->repository->setUser($user);
|
||||
|
||||
// if this is a transfer or a withdrawal, the new source account must be an asset account or a default account, and it MUST exist:
|
||||
$newAccount = $this->findAssetAccount($type, $name);
|
||||
$newAccount = $this->findAssetAccount($type, $accountName);
|
||||
if ((TransactionType::WITHDRAWAL === $type || TransactionType::TRANSFER === $type) && null === $newAccount) {
|
||||
Log::error(
|
||||
sprintf('Cant change source account of journal #%d because no asset account with name "%s" exists.', $object->id, $name)
|
||||
app('log')->error(
|
||||
sprintf('Cant change source account of journal #%d because no asset account with name "%s" exists.', $object->id, $accountName)
|
||||
);
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.cannot_find_asset', ['name' => $name])));
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.cannot_find_asset', ['name' => $accountName])));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// new source account must be different from the current destination account:
|
||||
/** @var Transaction|null $destination */
|
||||
$destination = $object->transactions()->where('amount', '>', 0)->first();
|
||||
/** @var null|Transaction $destination */
|
||||
$destination = $object->transactions()->where('amount', '>', 0)->first();
|
||||
if (null === $destination) {
|
||||
Log::error('Could not find destination transaction.');
|
||||
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) {
|
||||
Log::error('Could not find destination transaction 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 && (int)$newAccount->id === (int)$destination->account_id) {
|
||||
Log::error(
|
||||
if (null !== $newAccount && $newAccount->id === $destination->account_id) {
|
||||
app('log')->error(
|
||||
sprintf(
|
||||
'New source account ID #%d and current destination account ID #%d are the same. Do nothing.',
|
||||
$newAccount->id,
|
||||
@@ -109,48 +108,41 @@ class SetSourceAccount implements ActionInterface
|
||||
)
|
||||
);
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.already_has_source', ['name' => $newAccount->name])));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// if this is a deposit, the new source account must be a revenue account and may be created:
|
||||
// or it's a liability
|
||||
if (TransactionType::DEPOSIT === $type) {
|
||||
$newAccount = $this->findDepositSourceAccount($name);
|
||||
$newAccount = $this->findDepositSourceAccount($accountName);
|
||||
}
|
||||
|
||||
Log::debug(sprintf('New source account is #%d ("%s").', $newAccount->id, $newAccount->name));
|
||||
app('log')->debug(sprintf('New source account is #%d ("%s").', $newAccount->id, $newAccount->name));
|
||||
|
||||
// update source transaction with new source account:
|
||||
DB::table('transactions')
|
||||
\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));
|
||||
|
||||
Log::debug(sprintf('Updated journal #%d (group #%d) and gave it new source account ID.', $object->id, $object->transaction_group_id));
|
||||
app('log')->debug(sprintf('Updated journal #%d (group #%d) and gave it new source account ID.', $object->id, $object->transaction_group_id));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $type
|
||||
*
|
||||
* @return Account|null
|
||||
*/
|
||||
private function findAssetAccount(string $type, string $name): ?Account
|
||||
{
|
||||
// switch on type:
|
||||
$allowed = config(sprintf('firefly.expected_source_types.source.%s', $type));
|
||||
$allowed = is_array($allowed) ? $allowed : [];
|
||||
Log::debug(sprintf('Check config for expected_source_types.source.%s, result is', $type), $allowed);
|
||||
app('log')->debug(sprintf('Check config for expected_source_types.source.%s, result is', $type), $allowed);
|
||||
|
||||
return $this->repository->findByName($name, $allowed);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Account
|
||||
*/
|
||||
private function findDepositSourceAccount(string $name): Account
|
||||
{
|
||||
$allowed = config('firefly.expected_source_types.source.Deposit');
|
||||
@@ -167,7 +159,7 @@ class SetSourceAccount implements ActionInterface
|
||||
];
|
||||
$account = $this->repository->store($data);
|
||||
}
|
||||
Log::debug(sprintf('Found or created revenue account #%d ("%s")', $account->id, $account->name));
|
||||
app('log')->debug(sprintf('Found or created revenue account #%d ("%s")', $account->id, $account->name));
|
||||
|
||||
return $account;
|
||||
}
|
||||
|
120
app/TransactionRules/Actions/SetSourceToCashAccount.php
Normal file
120
app/TransactionRules/Actions/SetSourceToCashAccount.php
Normal file
@@ -0,0 +1,120 @@
|
||||
<?php
|
||||
/*
|
||||
* SetsourceToCashAccount.php
|
||||
* Copyright (c) 2023 james@firefly-iii.org
|
||||
*
|
||||
* This file is part of Firefly III (https://github.com/firefly-iii).
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\TransactionRules\Actions;
|
||||
|
||||
use FireflyIII\Events\Model\Rule\RuleActionFailedOnArray;
|
||||
use FireflyIII\Events\TriggeredAuditLog;
|
||||
use FireflyIII\Models\RuleAction;
|
||||
use FireflyIII\Models\Transaction;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Models\TransactionType;
|
||||
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
||||
use FireflyIII\User;
|
||||
|
||||
/**
|
||||
* Class SetSourceToCashAccount
|
||||
*/
|
||||
class SetSourceToCashAccount implements ActionInterface
|
||||
{
|
||||
private RuleAction $action;
|
||||
|
||||
/**
|
||||
* TriggerInterface constructor.
|
||||
*/
|
||||
public function __construct(RuleAction $action)
|
||||
{
|
||||
$this->action = $action;
|
||||
}
|
||||
|
||||
public function actOnArray(array $journal): bool
|
||||
{
|
||||
/** @var User $user */
|
||||
$user = User::find($journal['user_id']);
|
||||
|
||||
/** @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;
|
||||
}
|
||||
|
||||
// find cash account
|
||||
$repository->setUser($user);
|
||||
$cashAccount = $repository->getCashAccount();
|
||||
|
||||
// new source account must be different from the current destination account:
|
||||
/** @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) {
|
||||
app('log')->error(
|
||||
sprintf(
|
||||
'New source account ID #%d and current destination account ID #%d are the same. Do nothing.',
|
||||
$cashAccount->id,
|
||||
$destination->account_id
|
||||
)
|
||||
);
|
||||
|
||||
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])
|
||||
;
|
||||
|
||||
app('log')->debug(sprintf('Updated journal #%d (group #%d) and gave it new source account ID.', $object->id, $object->transaction_group_id));
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
@@ -23,17 +23,14 @@ declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\TransactionRules\Actions;
|
||||
|
||||
use DB;
|
||||
use FireflyIII\Events\Model\Rule\RuleActionFailedOnArray;
|
||||
use FireflyIII\Events\TriggeredAuditLog;
|
||||
use FireflyIII\Models\RuleAction;
|
||||
use FireflyIII\Models\Transaction;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Models\TransactionType;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
/**
|
||||
*
|
||||
* Class SwitchAccounts
|
||||
*/
|
||||
class SwitchAccounts implements ActionInterface
|
||||
@@ -42,51 +39,51 @@ 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 */
|
||||
$object = TransactionJournal::where('user_id', $journal['user_id'])->find($journal['transaction_journal_id']);
|
||||
/** @var null|TransactionJournal $object */
|
||||
$object = TransactionJournal::where('user_id', $journal['user_id'])->find($journal['transaction_journal_id']);
|
||||
if (null === $object) {
|
||||
Log::error(sprintf('Cannot find journal #%d, cannot switch accounts.', $journal['transaction_journal_id']));
|
||||
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();
|
||||
$groupCount = TransactionJournal::where('transaction_group_id', $journal['transaction_group_id'])->count();
|
||||
if ($groupCount > 1) {
|
||||
Log::error(sprintf('Group #%d has more than one transaction in it, cannot switch accounts.', $journal['transaction_group_id']));
|
||||
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;
|
||||
}
|
||||
|
||||
$type = $object->transactionType->type;
|
||||
$type = $object->transactionType->type;
|
||||
if (TransactionType::TRANSFER !== $type) {
|
||||
Log::error(sprintf('Journal #%d is NOT a transfer (rule #%d), cannot switch accounts.', $journal['transaction_journal_id'], $this->action->rule_id));
|
||||
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 $sourceTransaction */
|
||||
$sourceTransaction = $object->transactions()->where('amount', '<', 0)->first();
|
||||
/** @var Transaction $destTransaction */
|
||||
$destTransaction = $object->transactions()->where('amount', '>', 0)->first();
|
||||
/** @var null|Transaction $sourceTransaction */
|
||||
$sourceTransaction = $object->transactions()->where('amount', '<', 0)->first();
|
||||
|
||||
/** @var null|Transaction $destTransaction */
|
||||
$destTransaction = $object->transactions()->where('amount', '>', 0)->first();
|
||||
if (null === $sourceTransaction || null === $destTransaction) {
|
||||
Log::error(sprintf('Journal #%d has no source or destination transaction (rule #%d), cannot switch accounts.', $journal['transaction_journal_id'], $this->action->rule_id));
|
||||
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 = (int)$sourceTransaction->account_id;
|
||||
$sourceAccountId = $sourceTransaction->account_id;
|
||||
$destinationAccountId = $destTransaction->account_id;
|
||||
$sourceTransaction->account_id = $destinationAccountId;
|
||||
$destTransaction->account_id = $sourceAccountId;
|
||||
|
@@ -30,11 +30,9 @@ use FireflyIII\Models\PiggyBank;
|
||||
use FireflyIII\Models\RuleAction;
|
||||
use FireflyIII\Models\Transaction;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Models\TransactionType;
|
||||
use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface;
|
||||
use FireflyIII\TransactionRules\Expressions\ActionExpressionEvaluator;
|
||||
use FireflyIII\User;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
/**
|
||||
* Class UpdatePiggybank
|
||||
@@ -46,8 +44,6 @@ class UpdatePiggybank implements ActionInterface
|
||||
|
||||
/**
|
||||
* TriggerInterface constructor.
|
||||
*
|
||||
* @param RuleAction $action
|
||||
*/
|
||||
public function __construct(RuleAction $action, ActionExpressionEvaluator $evaluator)
|
||||
{
|
||||
@@ -55,38 +51,38 @@ class UpdatePiggybank implements ActionInterface
|
||||
$this->evaluator = $evaluator;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function actOnArray(array $journal): bool
|
||||
{
|
||||
Log::debug(sprintf('Triggered rule action UpdatePiggybank on journal #%d', $journal['transaction_journal_id']));
|
||||
app('log')->debug(sprintf('Triggered rule action UpdatePiggybank on journal #%d', $journal['transaction_journal_id']));
|
||||
|
||||
$piggyBankName = $this->evaluator->evaluate($journal);
|
||||
// refresh the transaction type.
|
||||
$user = User::find($journal['user_id']);
|
||||
/** @var TransactionJournal $journalObj */
|
||||
$journalObj = $user->transactionJournals()->find($journal['transaction_journal_id']);
|
||||
$type = TransactionType::find((int)$journalObj->transaction_type_id);
|
||||
/** @var User $user */
|
||||
$user = User::find($journal['user_id']);
|
||||
|
||||
$piggyBank = $this->findPiggyBank($user, $piggyBankName);
|
||||
/** @var TransactionJournal $journalObj */
|
||||
$journalObj = $user->transactionJournals()->find($journal['transaction_journal_id']);
|
||||
|
||||
$piggyBank = $this->findPiggyBank($user, $piggyBankName);
|
||||
if (null === $piggyBank) {
|
||||
Log::info(
|
||||
app('log')->info(
|
||||
sprintf('No piggy bank named "%s", cant execute action #%d of rule #%d', $piggyBankName, $this->action->id, $this->action->rule_id)
|
||||
);
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.cannot_find_piggy', ['name' => $piggyBankName])));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
Log::debug(sprintf('Found piggy bank #%d ("%s")', $piggyBank->id, $piggyBank->name));
|
||||
app('log')->debug(sprintf('Found piggy bank #%d ("%s")', $piggyBank->id, $piggyBank->name));
|
||||
|
||||
/** @var Transaction $source */
|
||||
$source = $journalObj->transactions()->where('amount', '<', 0)->first();
|
||||
$source = $journalObj->transactions()->where('amount', '<', 0)->first();
|
||||
|
||||
/** @var Transaction $destination */
|
||||
$destination = $journalObj->transactions()->where('amount', '>', 0)->first();
|
||||
|
||||
if ((int)$source->account_id === (int)$piggyBank->account_id) {
|
||||
Log::debug('Piggy bank account is linked to source, so remove amount from piggy bank.');
|
||||
if ($source->account_id === $piggyBank->account_id) {
|
||||
app('log')->debug('Piggy bank account is linked to source, so remove amount from piggy bank.');
|
||||
$this->removeAmount($piggyBank, $journalObj, $destination->amount);
|
||||
|
||||
event(
|
||||
@@ -106,8 +102,8 @@ class UpdatePiggybank implements ActionInterface
|
||||
|
||||
return true;
|
||||
}
|
||||
if ((int)$destination->account_id === (int)$piggyBank->account_id) {
|
||||
Log::debug('Piggy bank account is linked to source, so add amount to piggy bank.');
|
||||
if ($destination->account_id === $piggyBank->account_id) {
|
||||
app('log')->debug('Piggy bank account is linked to source, so add amount to piggy bank.');
|
||||
$this->addAmount($piggyBank, $journalObj, $destination->amount);
|
||||
|
||||
event(
|
||||
@@ -127,7 +123,7 @@ class UpdatePiggybank implements ActionInterface
|
||||
|
||||
return true;
|
||||
}
|
||||
Log::info(
|
||||
app('log')->info(
|
||||
sprintf(
|
||||
'Piggy bank is not linked to source ("#%d") or destination ("#%d"), so no action will be taken.',
|
||||
$source->account_id,
|
||||
@@ -135,38 +131,27 @@ class UpdatePiggybank implements ActionInterface
|
||||
)
|
||||
);
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.no_link_piggy', ['name' => $piggyBankName])));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param User $user
|
||||
*
|
||||
* @return PiggyBank|null
|
||||
*/
|
||||
private function findPiggyBank(User $user, string $name): ?PiggyBank
|
||||
{
|
||||
return $user->piggyBanks()->where('piggy_banks.name', $name)->first();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param PiggyBank $piggyBank
|
||||
* @param TransactionJournal $journal
|
||||
* @param string $amount
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function removeAmount(PiggyBank $piggyBank, TransactionJournal $journal, string $amount): void
|
||||
{
|
||||
$repository = app(PiggyBankRepositoryInterface::class);
|
||||
$repository->setUser($journal->user);
|
||||
|
||||
// how much can we remove from this piggy bank?
|
||||
$toRemove = $repository->getCurrentAmount($piggyBank);
|
||||
Log::debug(sprintf('Amount is %s, max to remove is %s', $amount, $toRemove));
|
||||
$toRemove = $repository->getCurrentAmount($piggyBank);
|
||||
app('log')->debug(sprintf('Amount is %s, max to remove is %s', $amount, $toRemove));
|
||||
|
||||
// if $amount is bigger than $toRemove, shrink it.
|
||||
$amount = -1 === bccomp($amount, $toRemove) ? $amount : $toRemove;
|
||||
Log::debug(sprintf('Amount is now %s', $amount));
|
||||
$amount = -1 === bccomp($amount, $toRemove) ? $amount : $toRemove;
|
||||
app('log')->debug(sprintf('Amount is now %s', $amount));
|
||||
|
||||
// if amount is zero, stop.
|
||||
if (0 === bccomp('0', $amount)) {
|
||||
@@ -181,18 +166,11 @@ class UpdatePiggybank implements ActionInterface
|
||||
|
||||
return;
|
||||
}
|
||||
Log::debug(sprintf('Will now remove %s from piggy bank.', $amount));
|
||||
app('log')->debug(sprintf('Will now remove %s from piggy bank.', $amount));
|
||||
|
||||
$repository->removeAmount($piggyBank, $amount, $journal);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param PiggyBank $piggyBank
|
||||
* @param TransactionJournal $journal
|
||||
* @param string $amount
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function addAmount(PiggyBank $piggyBank, TransactionJournal $journal, string $amount): void
|
||||
{
|
||||
$repository = app(PiggyBankRepositoryInterface::class);
|
||||
@@ -200,18 +178,17 @@ class UpdatePiggybank implements ActionInterface
|
||||
|
||||
// how much can we add to the piggy bank?
|
||||
if (0 !== bccomp($piggyBank->targetamount, '0')) {
|
||||
$toAdd = bcsub($piggyBank->targetamount, $repository->getCurrentAmount($piggyBank));
|
||||
Log::debug(sprintf('Max amount to add to piggy bank is %s, amount is %s', $toAdd, $amount));
|
||||
$toAdd = bcsub($piggyBank->targetamount, $repository->getCurrentAmount($piggyBank));
|
||||
app('log')->debug(sprintf('Max amount to add to piggy bank is %s, amount is %s', $toAdd, $amount));
|
||||
|
||||
// update amount to fit:
|
||||
$amount = -1 === bccomp($amount, $toAdd) ? $amount : $toAdd;
|
||||
Log::debug(sprintf('Amount is now %s', $amount));
|
||||
app('log')->debug(sprintf('Amount is now %s', $amount));
|
||||
}
|
||||
if (0 === bccomp($piggyBank->targetamount, '0')) {
|
||||
Log::debug('Target amount is zero, can add anything.');
|
||||
app('log')->debug('Target amount is zero, can add anything.');
|
||||
}
|
||||
|
||||
|
||||
// if amount is zero, stop.
|
||||
if (0 === bccomp('0', $amount)) {
|
||||
app('log')->warning('Amount left is zero, stop.');
|
||||
@@ -225,7 +202,7 @@ class UpdatePiggybank implements ActionInterface
|
||||
|
||||
return;
|
||||
}
|
||||
Log::debug(sprintf('Will now add %s to piggy bank.', $amount));
|
||||
app('log')->debug(sprintf('Will now add %s to piggy bank.', $amount));
|
||||
|
||||
$repository->addAmount($piggyBank, $amount, $journal);
|
||||
}
|
||||
|
Reference in New Issue
Block a user