Expand rules and bills.

This commit is contained in:
James Cole 2018-04-14 20:31:31 +02:00
parent 926c03986c
commit 15a22f0bfc
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
13 changed files with 90 additions and 36 deletions

View File

@ -121,7 +121,7 @@ class UpgradeDatabase extends Command
// loop bills.
$order = 1;
/** @var Collection $collection */
$collection = $user->bills()->where('active', 1)->get();
$collection = $user->bills()->get();
/** @var Bill $bill */
foreach ($collection as $bill) {
if ($bill->match !== 'MIGRATED_TO_RULES') {
@ -132,7 +132,7 @@ class UpgradeDatabase extends Command
'title' => (string)trans('firefly.rule_for_bill_title', ['name' => $bill->name], $lang->data),
'description' => (string)trans('firefly.rule_for_bill_description', ['name' => $bill->name], $lang->data),
'order' => $order,
'active' => 1,
'active' => $bill->active,
'stop_processing' => 1,
]
);

View File

@ -102,7 +102,6 @@ class RuleController extends Controller
if ($request->old()) {
$oldTriggers = $this->getPreviousTriggers($request);
$oldActions = $this->getPreviousActions($request);
}
// has existing bill refered to in URI?
if (null !== $bill && !$request->old()) {
@ -110,7 +109,7 @@ class RuleController extends Controller
// create some sensible defaults:
$preFilled['title'] = trans('firefly.new_rule_for_bill_title', ['name' => $bill->name]);
$preFilled['description'] = trans('firefly.new_rule_for_bill_description', ['name' => $bill->name]);
$request->session()->flash('preFilled', $preFilled);
// get triggers and actions for bill:
$oldTriggers = $this->getTriggersForBill($bill);
@ -122,6 +121,8 @@ class RuleController extends Controller
$subTitleIcon = 'fa-clone';
$subTitle = trans('firefly.make_new_rule', ['title' => $ruleGroup->title]);
$request->session()->flash('preFilled', $preFilled);
// put previous url in session if not redirect from store (not "create another").
if (true !== session('rules.create.fromStore')) {
$this->rememberPreviousUri('rules.create.uri');

View File

@ -56,6 +56,7 @@ class RuleFormRequest extends Request
'rule-action-values' => $this->get('rule-action-value'),
'rule-action-stop' => $this->get('rule-action-stop'),
'stop_processing' => $this->boolean('stop_processing'),
'strict' => $this->boolean('strict'),
];
}
@ -85,6 +86,7 @@ class RuleFormRequest extends Request
'rule-trigger.*' => 'required|in:' . implode(',', $validTriggers),
'rule-trigger-value.*' => 'required|min:1|ruleTriggerValue',
'rule-action.*' => 'required|in:' . implode(',', $validActions),
'strict' => 'in:0,1',
];
// since Laravel does not support this stuff yet, here's a trick.
for ($i = 0; $i < 10; ++$i) {

View File

@ -686,7 +686,7 @@ class BunqRoutine implements RoutineInterface
}
}
if ($journals->count() > 0) {
// link to tag
/** @var TagRepositoryInterface $repository */
$repository = app(TagRepositoryInterface::class);
@ -715,6 +715,7 @@ class BunqRoutine implements RoutineInterface
$this->addStep();
}
Log::info(sprintf('Linked %d journals to tag #%d ("%s")', $journals->count(), $tag->id, $tag->tag));
}
// set status to "finished"?
// update job:

View File

@ -46,9 +46,10 @@ class Rule extends Model
'active' => 'boolean',
'order' => 'int',
'stop_processing' => 'boolean',
'strict' => 'boolean',
];
/** @var array */
protected $fillable = ['rule_group_id', 'order', 'active', 'title', 'description', 'user_id'];
protected $fillable = ['rule_group_id', 'order', 'active', 'title', 'description', 'user_id','strict'];
/**
* @param string $value

View File

@ -258,6 +258,7 @@ class RuleRepository implements RuleRepositoryInterface
$rule->rule_group_id = $data['rule_group_id'];
$rule->order = ($order + 1);
$rule->active = 1;
$rule->strict = $data['strict'] ?? false;
$rule->stop_processing = 1 === (int)$data['stop_processing'];
$rule->title = $data['title'];
$rule->description = strlen($data['description']) > 0 ? $data['description'] : null;
@ -326,6 +327,7 @@ class RuleRepository implements RuleRepositoryInterface
$rule->active = $data['active'];
$rule->stop_processing = $data['stop_processing'];
$rule->title = $data['title'];
$rule->strict = $data['strict'] ?? false;
$rule->description = $data['description'];
$rule->save();

View File

@ -25,6 +25,8 @@ namespace FireflyIII\Services\Internal\Support;
use FireflyIII\Models\Bill;
use FireflyIII\Models\Note;
use FireflyIII\Models\RuleAction;
use Illuminate\Support\Collection;
/**
* Trait BillServiceTrait
@ -34,6 +36,29 @@ use FireflyIII\Models\Note;
trait BillServiceTrait
{
/**
* @param Bill $bill
* @param string $oldName
* @param string $newName
*/
public function updateBillActions(Bill $bill, string $oldName, string $newName): void
{
if ($oldName === $newName) {
return;
}
$ruleIds = $bill->user->rules()->get(['id'])->pluck('id')->toArray();
/** @var Collection $set */
$set = RuleAction::whereIn('rule_id', $ruleIds)
->where('action_type', 'link_to_bill')
->where('action_value', $oldName)->get();
/** @var RuleAction $ruleAction */
foreach ($set as $ruleAction) {
$ruleAction->action_value = $newName;
$ruleAction->save();
}
}
/**
* @param Bill $bill

View File

@ -42,6 +42,7 @@ class BillUpdateService
*/
public function update(Bill $bill, array $data): Bill
{
$oldName = $bill->name;
$bill->name = $data['name'];
$bill->amount_min = $data['amount_min'];
$bill->amount_max = $data['amount_max'];
@ -58,6 +59,9 @@ class BillUpdateService
$this->updateNote($bill, (string)$data['notes']);
}
// update rule actions.
$this->updateBillActions($bill, $oldName, $data['name']);
return $bill;
}

View File

@ -49,6 +49,8 @@ final class Processor
public $triggers;
/** @var int Found triggers */
private $foundTriggers = 0;
/** @var bool */
private $strict = true;
/**
* Processor constructor.
@ -72,8 +74,10 @@ final class Processor
public static function make(Rule $rule, $includeActions = true)
{
Log::debug(sprintf('Making new rule from Rule %d', $rule->id));
Log::debug(sprintf('Rule is strict: %s', var_export($rule->strict, true)));
$self = new self;
$self->rule = $rule;
$self->strict = $rule->strict;
$triggerSet = $rule->ruleTriggers()->orderBy('order', 'ASC')->get();
/** @var RuleTrigger $trigger */
foreach ($triggerSet as $trigger) {
@ -274,6 +278,12 @@ final class Processor
if ($trigger->triggered($this->journal)) {
Log::debug('Is a match!');
++$hitTriggers;
// is non-strict? then return true!
if (!$this->strict) {
Log::debug('Rule is set as non-strict, return true!');
return true;
}
}
if ($trigger->stopProcessing) {
Log::debug('Stop processing this trigger and break.');

View File

@ -32,5 +32,11 @@ class ChangesForV473 extends Migration
$table->foreign('transaction_currency_id')->references('id')->on('transaction_currencies')->onDelete('set null');
}
);
Schema::table(
'rules',
function (Blueprint $table) {
$table->boolean('strict')->default(true);
}
);
}
}

View File

@ -134,7 +134,7 @@
{% endif %}
><br/>{{ rule.description|markdown }}</small>
{% endif %}
<small><br />{% if rule.strict %}<span class="text-danger">{{ 'rule_is_strict'|_ }}</span>{% else %}<span class="text-success">{{ 'rule_is_not_strict'|_ }}</span>{% endif %}</small>
</td>
<td class="hidden-xs">
{% if rule.ruleTriggers.count > 0 %}

View File

@ -40,6 +40,7 @@
{{ ExpandedForm.select('trigger',allJournalTriggers()) }}
{{ ExpandedForm.select('rule_group_id',groups, ruleGroup.id) }}
{{ ExpandedForm.checkbox('stop_processing',1,null, {helpText: trans('firefly.rule_help_stop_processing')}) }}
{{ ExpandedForm.checkbox('strict',1, null,{helpText: trans('firefly.rule_help_strict')}) }}
</div>
</div>
</div>

View File

@ -19,6 +19,7 @@
{{ ExpandedForm.select('trigger',allJournalTriggers(), primaryTrigger) }}
{{ ExpandedForm.checkbox('active',1,rule.active, {helpText: trans('firefly.rule_help_active')}) }}
{{ ExpandedForm.checkbox('stop_processing',1,rule.stop_processing, {helpText: trans('firefly.rule_help_stop_processing')}) }}
{{ ExpandedForm.checkbox('strict',1,rule.strict, {helpText: trans('firefly.rule_help_strict')}) }}
</div>
</div>
</div>