2016-01-13 01:14:14 -06:00
|
|
|
<?php
|
2016-02-05 05:08:25 -06:00
|
|
|
declare(strict_types = 1);
|
2016-01-13 01:14:14 -06:00
|
|
|
/**
|
|
|
|
* 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;
|
2016-01-15 16:12:52 -06:00
|
|
|
|
2016-01-13 01:14:14 -06:00
|
|
|
/**
|
|
|
|
* Class SetBudget
|
|
|
|
*
|
|
|
|
* @package FireflyIII\Rules\Action
|
|
|
|
*/
|
|
|
|
class SetBudget implements ActionInterface
|
|
|
|
{
|
|
|
|
|
|
|
|
private $action;
|
2016-02-17 12:56:05 -06:00
|
|
|
|
2016-01-13 01:14:14 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* TriggerInterface constructor.
|
|
|
|
*
|
|
|
|
* @param RuleAction $action
|
|
|
|
*/
|
2016-02-17 12:56:05 -06:00
|
|
|
public function __construct(RuleAction $action)
|
2016-01-13 01:14:14 -06:00
|
|
|
{
|
|
|
|
$this->action = $action;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-02-17 12:56:05 -06:00
|
|
|
* @param TransactionJournal $journal
|
|
|
|
*
|
2016-01-13 01:14:14 -06:00
|
|
|
* @return bool
|
|
|
|
*/
|
2016-02-17 12:56:05 -06:00
|
|
|
public function act(TransactionJournal $journal)
|
2016-01-13 01:14:14 -06:00
|
|
|
{
|
|
|
|
/** @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)) {
|
2016-02-17 12:56:05 -06:00
|
|
|
Log::debug('Will set budget "' . $search . '" (#' . $budget->id . ') on journal #' . $journal->id . '.');
|
|
|
|
$journal->budgets()->sync([$budget->id]);
|
2016-01-13 01:14:14 -06:00
|
|
|
} else {
|
2016-01-15 16:12:52 -06:00
|
|
|
Log::debug('Could not find budget "' . $search . '". Failed.');
|
2016-01-13 01:14:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2016-01-28 14:50:20 -06:00
|
|
|
}
|