mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-16 01:06:46 +00:00
Test more rule actions.
This commit is contained in:
@@ -25,6 +25,7 @@ namespace FireflyIII\TransactionRules\Actions;
|
||||
use FireflyIII\Models\RuleAction;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use Log;
|
||||
use DB;
|
||||
|
||||
/**
|
||||
* Class PrependDescription.
|
||||
@@ -46,6 +47,8 @@ class PrependDescription implements ActionInterface
|
||||
|
||||
/**
|
||||
* Prepend description with X
|
||||
* @codeCoverageIgnore
|
||||
* @deprecated
|
||||
*
|
||||
* @param TransactionJournal $journal
|
||||
*
|
||||
@@ -65,6 +68,8 @@ class PrependDescription implements ActionInterface
|
||||
*/
|
||||
public function actOnArray(array $journal): bool
|
||||
{
|
||||
// TODO: Implement actOnArray() method.
|
||||
$description = sprintf('%s%s', $this->action->action_value, $journal['description']);
|
||||
DB::table('transaction_journals')->where('id', $journal['transaction_journal_id'])->limit(1)->update(['description' => $description]);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@@ -74,6 +74,21 @@ class PrependNotes implements ActionInterface
|
||||
*/
|
||||
public function actOnArray(array $journal): bool
|
||||
{
|
||||
// TODO: Implement actOnArray() method.
|
||||
$dbNote = Note
|
||||
::
|
||||
where('noteable_id', (int) $journal['transaction_journal_id'])
|
||||
->where('noteable_type', TransactionJournal::class)
|
||||
->first(['notes.*']);
|
||||
if (null === $dbNote) {
|
||||
$dbNote = new Note;
|
||||
$dbNote->noteable_id = (int) $journal['transaction_journal_id'];
|
||||
$dbNote->noteable_type = TransactionJournal::class;
|
||||
$dbNote->text = '';
|
||||
}
|
||||
Log::debug(sprintf('RuleAction PrependNotes prepended "%s" to "%s".', $this->action->action_value, $dbNote->text));
|
||||
$text = sprintf('%s%s', $this->action->action_value, $dbNote->text);
|
||||
$dbNote->text = $text;
|
||||
$dbNote->save();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@@ -25,6 +25,8 @@ namespace FireflyIII\TransactionRules\Actions;
|
||||
use FireflyIII\Models\RuleAction;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use Log;
|
||||
use DB;
|
||||
|
||||
|
||||
/**
|
||||
* Class RemoveAllTags.
|
||||
@@ -44,7 +46,8 @@ class RemoveAllTags implements ActionInterface
|
||||
* Remove all tags
|
||||
*
|
||||
* @param TransactionJournal $journal
|
||||
*
|
||||
* @deprecated
|
||||
* @codeCoverageIgnore
|
||||
* @return bool
|
||||
*/
|
||||
public function act(TransactionJournal $journal): bool
|
||||
@@ -61,6 +64,9 @@ class RemoveAllTags implements ActionInterface
|
||||
*/
|
||||
public function actOnArray(array $journal): bool
|
||||
{
|
||||
// TODO: Implement actOnArray() method.
|
||||
Log::debug(sprintf('RuleAction ClearCategory removed all tags from journal %d.', $journal['transaction_journal_id']));
|
||||
DB::table('tag_transaction_journal')->where('transaction_journal_id', $journal['transaction_journal_id'])->delete();
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@@ -24,15 +24,15 @@ namespace FireflyIII\TransactionRules\Actions;
|
||||
|
||||
use FireflyIII\Models\RuleAction;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\User;
|
||||
use Log;
|
||||
|
||||
use DB;
|
||||
/**
|
||||
* Class RemoveTag.
|
||||
*/
|
||||
class RemoveTag implements ActionInterface
|
||||
{
|
||||
/** @var RuleAction The rule action */
|
||||
private $action;
|
||||
private RuleAction $action;
|
||||
|
||||
/**
|
||||
* TriggerInterface constructor.
|
||||
@@ -46,7 +46,8 @@ class RemoveTag implements ActionInterface
|
||||
|
||||
/**
|
||||
* Remove tag X
|
||||
*
|
||||
* @deprecated
|
||||
* @codeCoverageIgnore
|
||||
* @param TransactionJournal $journal
|
||||
*
|
||||
* @return bool
|
||||
@@ -74,6 +75,22 @@ class RemoveTag implements ActionInterface
|
||||
*/
|
||||
public function actOnArray(array $journal): bool
|
||||
{
|
||||
// TODO: Implement actOnArray() method.
|
||||
// if tag does not exist, no need to continue:
|
||||
$name = $this->action->action_value;
|
||||
$user = User::find($journal['user_id']);
|
||||
$tag = $user->tags()->where('tag', $name)->first();
|
||||
|
||||
if (null !== $tag) {
|
||||
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();
|
||||
|
||||
return true;
|
||||
}
|
||||
Log::debug(sprintf('RuleAction RemoveTag tried to remove tag "%s" from journal #%d but no such tag exists.', $name, $journal['transaction_journal_id']));
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@@ -25,15 +25,16 @@ namespace FireflyIII\TransactionRules\Actions;
|
||||
use FireflyIII\Models\RuleAction;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Models\TransactionType;
|
||||
use FireflyIII\User;
|
||||
use Log;
|
||||
use DB;
|
||||
|
||||
/**
|
||||
* Class SetBudget.
|
||||
*/
|
||||
class SetBudget implements ActionInterface
|
||||
{
|
||||
/** @var RuleAction The rule action */
|
||||
private $action;
|
||||
private RuleAction $action;
|
||||
|
||||
/**
|
||||
* TriggerInterface constructor.
|
||||
@@ -49,7 +50,8 @@ class SetBudget implements ActionInterface
|
||||
* Set budget.
|
||||
*
|
||||
* @param TransactionJournal $journal
|
||||
*
|
||||
* @deprecated
|
||||
* @codeCoverageIgnore
|
||||
* @return bool
|
||||
*/
|
||||
public function act(TransactionJournal $journal): bool
|
||||
@@ -89,6 +91,34 @@ class SetBudget implements ActionInterface
|
||||
*/
|
||||
public function actOnArray(array $journal): bool
|
||||
{
|
||||
// TODO: Implement actOnArray() method.
|
||||
$user = User::find($journal['user_id']);
|
||||
$search = $this->action->action_value;
|
||||
|
||||
$budget = $user->budgets()->where('name', $search)->first();
|
||||
if (null === $budget) {
|
||||
Log::debug(sprintf('RuleAction SetBudget could not set budget of journal #%d to "%s" because no such budget exists.', $journal['transaction_journal_id'], $search));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (TransactionType::WITHDRAWAL !== $journal['transaction_type_type']) {
|
||||
Log::debug(
|
||||
sprintf(
|
||||
'RuleAction SetBudget could not set budget of journal #%d to "%s" because journal is a %s.',
|
||||
$journal['transaction_journal_id'],
|
||||
$search,
|
||||
$journal['transaction_type_type']
|
||||
)
|
||||
);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
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]);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@@ -25,15 +25,17 @@ namespace FireflyIII\TransactionRules\Actions;
|
||||
use FireflyIII\Factory\CategoryFactory;
|
||||
use FireflyIII\Models\RuleAction;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Models\TransactionType;
|
||||
use FireflyIII\User;
|
||||
use Log;
|
||||
use DB;
|
||||
|
||||
/**
|
||||
* Class SetCategory.
|
||||
*/
|
||||
class SetCategory implements ActionInterface
|
||||
{
|
||||
/** @var RuleAction The rule action */
|
||||
private $action;
|
||||
private RuleAction $action;
|
||||
|
||||
/**
|
||||
* TriggerInterface constructor.
|
||||
@@ -49,7 +51,8 @@ class SetCategory implements ActionInterface
|
||||
* Set category X
|
||||
*
|
||||
* @param TransactionJournal $journal
|
||||
*
|
||||
* @deprecated
|
||||
* @codeCoverageIgnore
|
||||
* @return bool
|
||||
*/
|
||||
public function act(TransactionJournal $journal): bool
|
||||
@@ -78,6 +81,21 @@ class SetCategory implements ActionInterface
|
||||
*/
|
||||
public function actOnArray(array $journal): bool
|
||||
{
|
||||
// TODO: Implement actOnArray() method.
|
||||
$user = User::find($journal['user_id']);
|
||||
$search = $this->action->action_value;
|
||||
|
||||
$category = $user->categories()->where('name', $search)->first();
|
||||
if (null === $category) {
|
||||
Log::debug(sprintf('RuleAction SetCategory could not set category of journal #%d to "%s" because no such category exists.', $journal['transaction_journal_id'], $search));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
Log::debug(sprintf('RuleAction SetCategory set the category of journal #%d to category #%d ("%s").', $journal['transaction_journal_id'], $category->id, $category->name));
|
||||
|
||||
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]);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user