Change namespace of rules.

This commit is contained in:
James Cole
2017-09-13 07:49:58 +02:00
parent 0583aa53a9
commit 233305ecb4
55 changed files with 108 additions and 148 deletions

View File

@@ -0,0 +1,39 @@
<?php
/**
* ActionInterface.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* This software may be modified and distributed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International License.
*
* See the LICENSE file for details.
*/
declare(strict_types=1);
namespace FireflyIII\TransactionRules\Actions;
use FireflyIII\Models\RuleAction;
use FireflyIII\Models\TransactionJournal;
/**
* Interface ActionInterface
*
* @package FireflyIII\TransactionRules\Action
*/
interface ActionInterface
{
/**
* TriggerInterface constructor.
*
* @param RuleAction $action
*/
public function __construct(RuleAction $action);
/**
* @param TransactionJournal $journal
*
* @return bool
*/
public function act(TransactionJournal $journal): bool;
}

View File

@@ -0,0 +1,66 @@
<?php
/**
* AddTag.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* This software may be modified and distributed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International License.
*
* See the LICENSE file for details.
*/
declare(strict_types=1);
namespace FireflyIII\TransactionRules\Actions;
use FireflyIII\Models\RuleAction;
use FireflyIII\Models\Tag;
use FireflyIII\Models\TransactionJournal;
use Log;
/**
* Class AddTag
*
* @package FireflyIII\TransactionRules\Actions
*/
class AddTag implements ActionInterface
{
/** @var RuleAction */
private $action;
/**
* TriggerInterface constructor.
*
* @param RuleAction $action
*/
public function __construct(RuleAction $action)
{
$this->action = $action;
}
/**
* @param TransactionJournal $journal
*
* @return bool
*/
public function act(TransactionJournal $journal): bool
{
// journal has this tag maybe?
$tag = Tag::firstOrCreateEncrypted(['tag' => $this->action->action_value, 'user_id' => $journal->user->id]);
$count = $journal->tags()->where('tag_id', $tag->id)->count();
if ($count === 0) {
$journal->tags()->save($tag);
Log::debug(sprintf('RuleAction AddTag. Added tag #%d ("%s") to journal %d.', $tag->id, $tag->tag, $journal->id));
return true;
}
Log::debug(sprintf('RuleAction AddTag fired but tag %d ("%s") was already added to journal %d.', $tag->id, $tag->tag, $journal->id));
return true;
}
}

View File

@@ -0,0 +1,54 @@
<?php
/**
* AppendDescription.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* This software may be modified and distributed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International License.
*
* See the LICENSE file for details.
*/
declare(strict_types=1);
namespace FireflyIII\TransactionRules\Actions;
use FireflyIII\Models\RuleAction;
use FireflyIII\Models\TransactionJournal;
use Log;
/**
* Class AppendDescription
*
* @package FireflyIII\TransactionRules\Actions
*/
class AppendDescription implements ActionInterface
{
private $action;
/**
* TriggerInterface constructor.
*
* @param RuleAction $action
*/
public function __construct(RuleAction $action)
{
$this->action = $action;
}
/**
* @param TransactionJournal $journal
*
* @return bool
*/
public function act(TransactionJournal $journal): bool
{
Log::debug(sprintf('RuleAction AppendDescription appended "%s" to "%s".', $this->action->action_value, $journal->description));
$journal->description = $journal->description . $this->action->action_value;
$journal->save();
return true;
}
}

View File

@@ -0,0 +1,53 @@
<?php
/**
* ClearBudget.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* This software may be modified and distributed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International License.
*
* See the LICENSE file for details.
*/
declare(strict_types=1);
namespace FireflyIII\TransactionRules\Actions;
use FireflyIII\Models\RuleAction;
use FireflyIII\Models\TransactionJournal;
use Log;
/**
* Class ClearBudget
*
* @package FireflyIII\TransactionRules\Action
*/
class ClearBudget implements ActionInterface
{
private $action;
/**
* TriggerInterface constructor.
*
* @param RuleAction $action
*/
public function __construct(RuleAction $action)
{
$this->action = $action;
}
/**
* @param TransactionJournal $journal
*
* @return bool
*/
public function act(TransactionJournal $journal): bool
{
$journal->budgets()->detach();
Log::debug(sprintf('RuleAction ClearBudget removed all budgets from journal %d.', $journal->id));
return true;
}
}

View File

@@ -0,0 +1,54 @@
<?php
/**
* ClearCategory.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* This software may be modified and distributed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International License.
*
* See the LICENSE file for details.
*/
declare(strict_types=1);
namespace FireflyIII\TransactionRules\Actions;
use FireflyIII\Models\RuleAction;
use FireflyIII\Models\TransactionJournal;
use Log;
/**
* Class ClearCategory
*
* @package FireflyIII\TransactionRules\Action
*/
class ClearCategory implements ActionInterface
{
private $action;
/**
* TriggerInterface constructor.
*
* @param RuleAction $action
*/
public function __construct(RuleAction $action)
{
$this->action = $action;
}
/**
* @param TransactionJournal $journal
*
* @return bool
*/
public function act(TransactionJournal $journal): bool
{
$journal->categories()->detach();
Log::debug(sprintf('RuleAction ClearCategory removed all categories from journal %d.', $journal->id));
return true;
}
}

View File

@@ -0,0 +1,55 @@
<?php
/**
* PrependDescription.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* This software may be modified and distributed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International License.
*
* See the LICENSE file for details.
*/
declare(strict_types=1);
namespace FireflyIII\TransactionRules\Actions;
use FireflyIII\Models\RuleAction;
use FireflyIII\Models\TransactionJournal;
use Log;
/**
* Class AppendDescription
*
* @package FireflyIII\TransactionRules\Actions
*/
class PrependDescription implements ActionInterface
{
private $action;
/**
* TriggerInterface constructor.
*
* @param RuleAction $action
*/
public function __construct(RuleAction $action)
{
$this->action = $action;
}
/**
* @param TransactionJournal $journal
*
* @return bool
*/
public function act(TransactionJournal $journal): bool
{
Log::debug(sprintf('RuleAction PrependDescription prepended "%s" to "%s".', $this->action->action_value, $journal->description));
$journal->description = $this->action->action_value . $journal->description;
$journal->save();
return true;
}
}

View File

@@ -0,0 +1,54 @@
<?php
/**
* RemoveAllTags.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* This software may be modified and distributed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International License.
*
* See the LICENSE file for details.
*/
declare(strict_types=1);
namespace FireflyIII\TransactionRules\Actions;
use FireflyIII\Models\RuleAction;
use FireflyIII\Models\TransactionJournal;
use Log;
/**
* Class RemoveAllTags
*
* @package FireflyIII\TransactionRules\Actions
*/
class RemoveAllTags implements ActionInterface
{
private $action;
/**
* TriggerInterface constructor.
*
* @param RuleAction $action
*/
public function __construct(RuleAction $action)
{
$this->action = $action;
}
/**
* @param TransactionJournal $journal
*
* @return bool
*/
public function act(TransactionJournal $journal): bool
{
Log::debug(sprintf('RuleAction ClearCategory removed all tags from journal %d.', $journal->id));
$journal->tags()->detach();
return true;
}
}

View File

@@ -0,0 +1,69 @@
<?php
/**
* RemoveTag.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* This software may be modified and distributed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International License.
*
* See the LICENSE file for details.
*/
declare(strict_types=1);
namespace FireflyIII\TransactionRules\Actions;
use FireflyIII\Models\RuleAction;
use FireflyIII\Models\Tag;
use FireflyIII\Models\TransactionJournal;
use Log;
/**
* Class RemoveTag
*
* @package FireflyIII\TransactionRules\Actions
*/
class RemoveTag implements ActionInterface
{
private $action;
/**
* TriggerInterface constructor.
*
* @param RuleAction $action
*/
public function __construct(RuleAction $action)
{
$this->action = $action;
}
/**
* @param TransactionJournal $journal
*
* @return bool
*/
public function act(TransactionJournal $journal): bool
{
// if tag does not exist, no need to continue:
$name = $this->action->action_value;
/** @var Tag $tag */
$tag = $journal->user->tags()->get()->filter(
function (Tag $tag) use ($name) {
return $tag->tag === $name;
}
)->first();
if (!is_null($tag)) {
Log::debug(sprintf('RuleAction RemoveTag removed tag #%d ("%s") from journal #%d.', $tag->id, $tag->tag, $journal->id));
$journal->tags()->detach([$tag->id]);
return true;
}
Log::debug(sprintf('RuleAction RemoveTag tried to remove tag "%s" from journal #%d but no such tag exists.', $name, $journal->id));
return true;
}
}

View File

@@ -0,0 +1,81 @@
<?php
/**
* SetBudget.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* This software may be modified and distributed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International License.
*
* See the LICENSE file for details.
*/
declare(strict_types=1);
namespace FireflyIII\TransactionRules\Actions;
use FireflyIII\Models\Budget;
use FireflyIII\Models\RuleAction;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use Log;
/**
* Class SetBudget
*
* @package FireflyIII\TransactionRules\Action
*/
class SetBudget implements ActionInterface
{
private $action;
/**
* TriggerInterface constructor.
*
* @param RuleAction $action
*/
public function __construct(RuleAction $action)
{
$this->action = $action;
}
/**
* @param TransactionJournal $journal
*
* @return bool
*/
public function act(TransactionJournal $journal): bool
{
/** @var BudgetRepositoryInterface $repository */
$repository = app(BudgetRepositoryInterface::class);
$repository->setUser($journal->user);
$search = $this->action->action_value;
$budgets = $repository->getActiveBudgets();
$budget = $budgets->filter(
function (Budget $current) use ($search) {
return $current->name === $search;
}
)->first();
if (is_null($budget)) {
Log::debug(sprintf('RuleAction SetBudget could not set budget of journal #%d to "%s" because no such budget exists.', $journal->id, $search));
return true;
}
if ($journal->transactionType->type === TransactionType::TRANSFER) {
Log::debug(sprintf('RuleAction SetBudget could not set budget of journal #%d to "%s" because journal is a transfer.', $journal->id, $search));
return true;
}
Log::debug(sprintf('RuleAction SetBudget set the budget of journal #%d to budget #%d ("%s").', $journal->id, $budget->id, $budget->name));
$journal->budgets()->sync([$budget->id]);
return true;
}
}

View File

@@ -0,0 +1,58 @@
<?php
/**
* SetCategory.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* This software may be modified and distributed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International License.
*
* See the LICENSE file for details.
*/
declare(strict_types=1);
namespace FireflyIII\TransactionRules\Actions;
use FireflyIII\Models\Category;
use FireflyIII\Models\RuleAction;
use FireflyIII\Models\TransactionJournal;
use Log;
/**
* Class SetCategory
*
* @package FireflyIII\TransactionRules\Action
*/
class SetCategory implements ActionInterface
{
private $action;
/**
* TriggerInterface constructor.
*
* @param RuleAction $action
*/
public function __construct(RuleAction $action)
{
$this->action = $action;
}
/**
* @param TransactionJournal $journal
*
* @return bool
*/
public function act(TransactionJournal $journal): bool
{
$name = $this->action->action_value;
$category = Category::firstOrCreateEncrypted(['name' => $name, 'user_id' => $journal->user->id]);
$journal->categories()->sync([$category->id]);
Log::debug(sprintf('RuleAction SetCategory set the category of journal #%d to budget #%d ("%s").', $journal->id, $category->id, $category->name));
return true;
}
}

View File

@@ -0,0 +1,63 @@
<?php
/**
* SetDescription.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* This software may be modified and distributed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International License.
*
* See the LICENSE file for details.
*/
declare(strict_types=1);
namespace FireflyIII\TransactionRules\Actions;
use FireflyIII\Models\RuleAction;
use FireflyIII\Models\TransactionJournal;
use Log;
/**
* Class SetDescription
*
* @package FireflyIII\TransactionRules\Actions
*/
class SetDescription implements ActionInterface
{
private $action;
/**
* TriggerInterface constructor.
*
* @param RuleAction $action
*/
public function __construct(RuleAction $action)
{
$this->action = $action;
}
/**
* @param TransactionJournal $journal
*
* @return bool
*/
public function act(TransactionJournal $journal): bool
{
$oldDescription = $journal->description;
$journal->description = $this->action->action_value;
$journal->save();
Log::debug(
sprintf(
'RuleAction SetDescription changed the description of journal #%d from "%s" to "%s".', $journal->id,
$oldDescription,
$this->action->action_value
)
);
return true;
}
}

View File

@@ -0,0 +1,142 @@
<?php
/**
* SetDestinationAccount.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* This software may be modified and distributed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International License.
*
* See the LICENSE file for details.
*/
declare(strict_types=1);
namespace FireflyIII\TransactionRules\Actions;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\RuleAction;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use Log;
/**
* Class SetDestinationAccount
*
* @package FireflyIII\TransactionRules\Action
*/
class SetDestinationAccount implements ActionInterface
{
private $action;
/** @var TransactionJournal */
private $journal;
/** @var Account */
private $newDestinationAccount;
/** @var AccountRepositoryInterface */
private $repository;
/**
* TriggerInterface constructor.
*
* @param RuleAction $action
*/
public function __construct(RuleAction $action)
{
$this->action = $action;
}
/**
* @param TransactionJournal $journal
*
* @return bool
*/
public function act(TransactionJournal $journal): bool
{
$this->journal = $journal;
$this->repository = app(AccountRepositoryInterface::class);
$this->repository->setUser($journal->user);
$count = $journal->transactions()->count();
if ($count > 2) {
Log::error(sprintf('Cannot change destination account of journal #%d because it is a split journal.', $journal->id));
return true;
}
// journal type:
$type = $journal->transactionType->type;
// if this is a deposit or a transfer, the destination account must be an asset account or a default account, and it MUST exist:
if (($type === TransactionType::DEPOSIT || $type === TransactionType::TRANSFER) && !$this->findAssetAccount()) {
Log::error(
sprintf(
'Cannot change destination account of journal #%d because no asset account with name "%s" exists.',
$journal->id, $this->action->action_value
)
);
return true;
}
// if this is a withdrawal, the new destination account must be a expense account and may be created:
if ($type === TransactionType::WITHDRAWAL) {
$this->findExpenseAccount();
}
Log::debug(sprintf('New destination account is #%d ("%s").', $this->newDestinationAccount->id, $this->newDestinationAccount->name));
// update destination transaction with new destination account:
// get destination transaction:
$transaction = $journal->transactions()->where('amount', '>', 0)->first();
$transaction->account_id = $this->newDestinationAccount->id;
$transaction->save();
Log::debug(sprintf('Updated transaction #%d and gave it new account ID.', $transaction->id));
return true;
}
/**
* @return bool
*/
private function findAssetAccount(): bool
{
$account = $this->repository->findByName($this->action->action_value, [AccountType::DEFAULT, AccountType::ASSET]);
if (is_null($account->id)) {
Log::debug(sprintf('There is NO asset account called "%s".', $this->action->action_value));
return false;
}
Log::debug(sprintf('There exists an asset account called "%s". ID is #%d', $this->action->action_value, $account->id));
$this->newDestinationAccount = $account;
return true;
}
/**
*
*/
private function findExpenseAccount()
{
$account = $this->repository->findByName($this->action->action_value, [AccountType::EXPENSE]);
if (is_null($account->id)) {
// create new revenue account with this name:
$data = [
'name' => $this->action->action_value,
'accountType' => 'expense',
'virtualBalance' => 0,
'active' => true,
'iban' => null,
];
$account = $this->repository->store($data);
}
Log::debug(sprintf('Found or created expense account #%d ("%s")', $account->id, $account->name));
$this->newDestinationAccount = $account;
}
}

View File

@@ -0,0 +1,141 @@
<?php
/**
* SetSourceAccount.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* This software may be modified and distributed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International License.
*
* See the LICENSE file for details.
*/
declare(strict_types=1);
namespace FireflyIII\TransactionRules\Actions;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\RuleAction;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use Log;
/**
* Class SetSourceAccount
*
* @package FireflyIII\TransactionRules\Action
*/
class SetSourceAccount implements ActionInterface
{
private $action;
/** @var TransactionJournal */
private $journal;
/** @var Account */
private $newSourceAccount;
/** @var AccountRepositoryInterface */
private $repository;
/**
* TriggerInterface constructor.
*
* @param RuleAction $action
*/
public function __construct(RuleAction $action)
{
$this->action = $action;
}
/**
* @param TransactionJournal $journal
*
* @return bool
*/
public function act(TransactionJournal $journal): bool
{
$this->journal = $journal;
$this->repository = app(AccountRepositoryInterface::class);
$this->repository->setUser($journal->user);
$count = $journal->transactions()->count();
if ($count > 2) {
Log::error(sprintf('Cannot change source account of journal #%d because it is a split journal.', $journal->id));
return true;
}
// journal type:
$type = $journal->transactionType->type;
// 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:
if (($type === TransactionType::WITHDRAWAL || $type === TransactionType::TRANSFER) && !$this->findAssetAccount()) {
Log::error(
sprintf(
'Cannot change source account of journal #%d because no asset account with name "%s" exists.',
$journal->id, $this->action->action_value
)
);
return true;
}
// if this is a deposit, the new source account must be a revenue account and may be created:
if ($type === TransactionType::DEPOSIT) {
$this->findRevenueAccount();
}
Log::debug(sprintf('New source account is #%d ("%s").', $this->newSourceAccount->id, $this->newSourceAccount->name));
// update source transaction with new source account:
// get source transaction:
$transaction = $journal->transactions()->where('amount', '<', 0)->first();
$transaction->account_id = $this->newSourceAccount->id;
$transaction->save();
Log::debug(sprintf('Updated transaction #%d and gave it new account ID.', $transaction->id));
return true;
}
/**
* @return bool
*/
private function findAssetAccount(): bool
{
$account = $this->repository->findByName($this->action->action_value, [AccountType::DEFAULT, AccountType::ASSET]);
if (is_null($account->id)) {
Log::debug(sprintf('There is NO asset account called "%s".', $this->action->action_value));
return false;
}
Log::debug(sprintf('There exists an asset account called "%s". ID is #%d', $this->action->action_value, $account->id));
$this->newSourceAccount = $account;
return true;
}
/**
*
*/
private function findRevenueAccount()
{
$account = $this->repository->findByName($this->action->action_value, [AccountType::REVENUE]);
if (is_null($account->id)) {
// create new revenue account with this name:
$data = [
'name' => $this->action->action_value,
'accountType' => 'revenue',
'virtualBalance' => 0,
'active' => true,
'iban' => null,
];
$account = $this->repository->store($data);
}
Log::debug(sprintf('Found or created revenue account #%d ("%s")', $account->id, $account->name));
$this->newSourceAccount = $account;
}
}