Can now automatically handle some rules. No user interface, yet.

This commit is contained in:
James Cole
2016-01-13 08:14:14 +01:00
parent ce250c85fc
commit 46ee2a0568
8 changed files with 200 additions and 12 deletions

View File

@@ -0,0 +1,33 @@
<?php
/**
* ActionInterface.php
* Copyright (C) 2016 Sander Dorigo
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace FireflyIII\Rules\Actions;
use FireflyIII\Models\RuleAction;
use FireflyIII\Models\TransactionJournal;
/**
* Interface ActionInterface
*
* @package FireflyIII\Rules\Action
*/
interface ActionInterface
{
/**
* TriggerInterface constructor.
*
* @param RuleAction $action
* @param TransactionJournal $journal
*/
public function __construct(RuleAction $action, TransactionJournal $journal);
/**
* @return bool
*/
public function act();
}

View File

@@ -0,0 +1,64 @@
<?php
/**
* SetBudget.php
* Copyright (C) 2016 Sander Dorigo
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace FireflyIII\Rules\Actions;
use FireflyIII\Models\Budget;
use FireflyIII\Models\RuleAction;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use Log;
/**
* Class SetBudget
*
* @package FireflyIII\Rules\Action
*/
class SetBudget implements ActionInterface
{
private $action;
private $journal;
/**
* TriggerInterface constructor.
*
* @param RuleAction $action
* @param TransactionJournal $journal
*/
public function __construct(RuleAction $action, TransactionJournal $journal)
{
$this->action = $action;
$this->journal = $journal;
}
/**
* @return bool
*/
public function act()
{
/** @var BudgetRepositoryInterface $repository */
$repository = app('FireflyIII\Repositories\Budget\BudgetRepositoryInterface');
$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('Will set budget "' . $search . '" (#' . $budget->id . ') on journal #' . $this->journal->id . '.');
$this->journal->budgets()->save($budget);
} else {
Log::debug('Could not find budget "'.$search.'". Failed.');
}
return true;
}
}

View File

@@ -0,0 +1,54 @@
<?php
/**
* SetCategory.php
* Copyright (C) 2016 Sander Dorigo
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace FireflyIII\Rules\Actions;
use Auth;
use FireflyIII\Models\Category;
use FireflyIII\Models\RuleAction;
use FireflyIII\Models\TransactionJournal;
use Log;
/**
* Class SetCategory
*
* @package FireflyIII\Rules\Action
*/
class SetCategory implements ActionInterface
{
private $action;
private $journal;
/**
* TriggerInterface constructor.
*
* @param RuleAction $action
* @param TransactionJournal $journal
*/
public function __construct(RuleAction $action, TransactionJournal $journal)
{
$this->action = $action;
$this->journal = $journal;
}
/**
* @return bool
*/
public function act()
{
$name = $this->action->action_value;
$category = Category::firstOrCreateEncrypted(['name' => $name, 'user_id' => Auth::user()->id]);
Log::debug('Will set category "' . $name . '" (#' . $category->id . ') on journal #' . $this->journal->id . '.');
$this->journal->categories()->save($category);
return true;
}
}