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

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

View File

@ -102,7 +102,6 @@ class RuleController extends Controller
if ($request->old()) { if ($request->old()) {
$oldTriggers = $this->getPreviousTriggers($request); $oldTriggers = $this->getPreviousTriggers($request);
$oldActions = $this->getPreviousActions($request); $oldActions = $this->getPreviousActions($request);
} }
// has existing bill refered to in URI? // has existing bill refered to in URI?
if (null !== $bill && !$request->old()) { if (null !== $bill && !$request->old()) {
@ -110,7 +109,7 @@ class RuleController extends Controller
// create some sensible defaults: // create some sensible defaults:
$preFilled['title'] = trans('firefly.new_rule_for_bill_title', ['name' => $bill->name]); $preFilled['title'] = trans('firefly.new_rule_for_bill_title', ['name' => $bill->name]);
$preFilled['description'] = trans('firefly.new_rule_for_bill_description', ['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: // get triggers and actions for bill:
$oldTriggers = $this->getTriggersForBill($bill); $oldTriggers = $this->getTriggersForBill($bill);
@ -122,6 +121,8 @@ class RuleController extends Controller
$subTitleIcon = 'fa-clone'; $subTitleIcon = 'fa-clone';
$subTitle = trans('firefly.make_new_rule', ['title' => $ruleGroup->title]); $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"). // put previous url in session if not redirect from store (not "create another").
if (true !== session('rules.create.fromStore')) { if (true !== session('rules.create.fromStore')) {
$this->rememberPreviousUri('rules.create.uri'); $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-values' => $this->get('rule-action-value'),
'rule-action-stop' => $this->get('rule-action-stop'), 'rule-action-stop' => $this->get('rule-action-stop'),
'stop_processing' => $this->boolean('stop_processing'), '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.*' => 'required|in:' . implode(',', $validTriggers),
'rule-trigger-value.*' => 'required|min:1|ruleTriggerValue', 'rule-trigger-value.*' => 'required|min:1|ruleTriggerValue',
'rule-action.*' => 'required|in:' . implode(',', $validActions), 'rule-action.*' => 'required|in:' . implode(',', $validActions),
'strict' => 'in:0,1',
]; ];
// since Laravel does not support this stuff yet, here's a trick. // since Laravel does not support this stuff yet, here's a trick.
for ($i = 0; $i < 10; ++$i) { for ($i = 0; $i < 10; ++$i) {

View File

@ -686,35 +686,36 @@ class BunqRoutine implements RoutineInterface
} }
} }
if ($journals->count() > 0) {
// link to tag
/** @var TagRepositoryInterface $repository */
$repository = app(TagRepositoryInterface::class);
$repository->setUser($this->job->user);
$data = [
'tag' => trans('import.import_with_key', ['key' => $this->job->key]),
'date' => new Carbon,
'description' => null,
'latitude' => null,
'longitude' => null,
'zoomLevel' => null,
'tagMode' => 'nothing',
];
$tag = $repository->store($data);
$extended = $this->getExtendedStatus();
$extended['tag'] = $tag->id;
$this->setExtendedStatus($extended);
// link to tag Log::debug(sprintf('Created tag #%d ("%s")', $tag->id, $tag->tag));
/** @var TagRepositoryInterface $repository */ Log::debug('Looping journals...');
$repository = app(TagRepositoryInterface::class); $tagId = $tag->id;
$repository->setUser($this->job->user);
$data = [
'tag' => trans('import.import_with_key', ['key' => $this->job->key]),
'date' => new Carbon,
'description' => null,
'latitude' => null,
'longitude' => null,
'zoomLevel' => null,
'tagMode' => 'nothing',
];
$tag = $repository->store($data);
$extended = $this->getExtendedStatus();
$extended['tag'] = $tag->id;
$this->setExtendedStatus($extended);
Log::debug(sprintf('Created tag #%d ("%s")', $tag->id, $tag->tag)); foreach ($journals as $journal) {
Log::debug('Looping journals...'); Log::debug(sprintf('Linking journal #%d to tag #%d...', $journal->id, $tagId));
$tagId = $tag->id; DB::table('tag_transaction_journal')->insert(['transaction_journal_id' => $journal->id, 'tag_id' => $tagId]);
$this->addStep();
foreach ($journals as $journal) { }
Log::debug(sprintf('Linking journal #%d to tag #%d...', $journal->id, $tagId)); Log::info(sprintf('Linked %d journals to tag #%d ("%s")', $journals->count(), $tag->id, $tag->tag));
DB::table('tag_transaction_journal')->insert(['transaction_journal_id' => $journal->id, 'tag_id' => $tagId]);
$this->addStep();
} }
Log::info(sprintf('Linked %d journals to tag #%d ("%s")', $journals->count(), $tag->id, $tag->tag));
// set status to "finished"? // set status to "finished"?
// update job: // update job:

View File

@ -46,9 +46,10 @@ class Rule extends Model
'active' => 'boolean', 'active' => 'boolean',
'order' => 'int', 'order' => 'int',
'stop_processing' => 'boolean', 'stop_processing' => 'boolean',
'strict' => 'boolean',
]; ];
/** @var array */ /** @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 * @param string $value

View File

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

View File

@ -25,6 +25,8 @@ namespace FireflyIII\Services\Internal\Support;
use FireflyIII\Models\Bill; use FireflyIII\Models\Bill;
use FireflyIII\Models\Note; use FireflyIII\Models\Note;
use FireflyIII\Models\RuleAction;
use Illuminate\Support\Collection;
/** /**
* Trait BillServiceTrait * Trait BillServiceTrait
@ -34,6 +36,29 @@ use FireflyIII\Models\Note;
trait BillServiceTrait 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 * @param Bill $bill

View File

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

View File

@ -49,6 +49,8 @@ final class Processor
public $triggers; public $triggers;
/** @var int Found triggers */ /** @var int Found triggers */
private $foundTriggers = 0; private $foundTriggers = 0;
/** @var bool */
private $strict = true;
/** /**
* Processor constructor. * Processor constructor.
@ -72,9 +74,11 @@ final class Processor
public static function make(Rule $rule, $includeActions = true) public static function make(Rule $rule, $includeActions = true)
{ {
Log::debug(sprintf('Making new rule from Rule %d', $rule->id)); Log::debug(sprintf('Making new rule from Rule %d', $rule->id));
$self = new self; Log::debug(sprintf('Rule is strict: %s', var_export($rule->strict, true)));
$self->rule = $rule; $self = new self;
$triggerSet = $rule->ruleTriggers()->orderBy('order', 'ASC')->get(); $self->rule = $rule;
$self->strict = $rule->strict;
$triggerSet = $rule->ruleTriggers()->orderBy('order', 'ASC')->get();
/** @var RuleTrigger $trigger */ /** @var RuleTrigger $trigger */
foreach ($triggerSet as $trigger) { foreach ($triggerSet as $trigger) {
Log::debug(sprintf('Push trigger %d', $trigger->id)); Log::debug(sprintf('Push trigger %d', $trigger->id));
@ -274,6 +278,12 @@ final class Processor
if ($trigger->triggered($this->journal)) { if ($trigger->triggered($this->journal)) {
Log::debug('Is a match!'); Log::debug('Is a match!');
++$hitTriggers; ++$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) { if ($trigger->stopProcessing) {
Log::debug('Stop processing this trigger and break.'); 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'); $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 %} {% endif %}
><br/>{{ rule.description|markdown }}</small> ><br/>{{ rule.description|markdown }}</small>
{% endif %} {% 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>
<td class="hidden-xs"> <td class="hidden-xs">
{% if rule.ruleTriggers.count > 0 %} {% if rule.ruleTriggers.count > 0 %}

View File

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

View File

@ -19,6 +19,7 @@
{{ ExpandedForm.select('trigger',allJournalTriggers(), primaryTrigger) }} {{ ExpandedForm.select('trigger',allJournalTriggers(), primaryTrigger) }}
{{ ExpandedForm.checkbox('active',1,rule.active, {helpText: trans('firefly.rule_help_active')}) }} {{ 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('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> </div>
</div> </div>