diff --git a/app/Console/Commands/UpgradeDatabase.php b/app/Console/Commands/UpgradeDatabase.php index d842c27ce1..6d4241aa39 100644 --- a/app/Console/Commands/UpgradeDatabase.php +++ b/app/Console/Commands/UpgradeDatabase.php @@ -119,9 +119,9 @@ class UpgradeDatabase extends Command } // loop bills. - $order = 1; + $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, ] ); diff --git a/app/Http/Controllers/RuleController.php b/app/Http/Controllers/RuleController.php index 2f7ee11312..99d71a5e02 100644 --- a/app/Http/Controllers/RuleController.php +++ b/app/Http/Controllers/RuleController.php @@ -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'); diff --git a/app/Http/Requests/RuleFormRequest.php b/app/Http/Requests/RuleFormRequest.php index c466f3bc45..148c24aa43 100644 --- a/app/Http/Requests/RuleFormRequest.php +++ b/app/Http/Requests/RuleFormRequest.php @@ -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) { diff --git a/app/Import/Routine/BunqRoutine.php b/app/Import/Routine/BunqRoutine.php index 160eb4459e..18098a214c 100644 --- a/app/Import/Routine/BunqRoutine.php +++ b/app/Import/Routine/BunqRoutine.php @@ -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 - /** @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); + Log::debug(sprintf('Created tag #%d ("%s")', $tag->id, $tag->tag)); + Log::debug('Looping journals...'); + $tagId = $tag->id; - Log::debug(sprintf('Created tag #%d ("%s")', $tag->id, $tag->tag)); - Log::debug('Looping journals...'); - $tagId = $tag->id; - - foreach ($journals as $journal) { - Log::debug(sprintf('Linking journal #%d to tag #%d...', $journal->id, $tagId)); - 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)); + 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)); } - Log::info(sprintf('Linked %d journals to tag #%d ("%s")', $journals->count(), $tag->id, $tag->tag)); // set status to "finished"? // update job: diff --git a/app/Models/Rule.php b/app/Models/Rule.php index db514c8609..f9178b06b5 100644 --- a/app/Models/Rule.php +++ b/app/Models/Rule.php @@ -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 diff --git a/app/Repositories/Rule/RuleRepository.php b/app/Repositories/Rule/RuleRepository.php index d5eb41f507..50154ae2bc 100644 --- a/app/Repositories/Rule/RuleRepository.php +++ b/app/Repositories/Rule/RuleRepository.php @@ -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(); diff --git a/app/Services/Internal/Support/BillServiceTrait.php b/app/Services/Internal/Support/BillServiceTrait.php index e6ec02c8d0..d4ab545309 100644 --- a/app/Services/Internal/Support/BillServiceTrait.php +++ b/app/Services/Internal/Support/BillServiceTrait.php @@ -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 diff --git a/app/Services/Internal/Update/BillUpdateService.php b/app/Services/Internal/Update/BillUpdateService.php index 34edc0c92f..97e89b1753 100644 --- a/app/Services/Internal/Update/BillUpdateService.php +++ b/app/Services/Internal/Update/BillUpdateService.php @@ -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; } diff --git a/app/TransactionRules/Processor.php b/app/TransactionRules/Processor.php index 2cf9f31759..41d5cfed1a 100644 --- a/app/TransactionRules/Processor.php +++ b/app/TransactionRules/Processor.php @@ -49,6 +49,8 @@ final class Processor public $triggers; /** @var int Found triggers */ private $foundTriggers = 0; + /** @var bool */ + private $strict = true; /** * Processor constructor. @@ -72,9 +74,11 @@ final class Processor public static function make(Rule $rule, $includeActions = true) { Log::debug(sprintf('Making new rule from Rule %d', $rule->id)); - $self = new self; - $self->rule = $rule; - $triggerSet = $rule->ruleTriggers()->orderBy('order', 'ASC')->get(); + 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) { Log::debug(sprintf('Push trigger %d', $trigger->id)); @@ -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.'); diff --git a/database/migrations/2018_04_07_210913_changes_for_v473.php b/database/migrations/2018_04_07_210913_changes_for_v473.php index abeee6eae6..0e9492ca88 100644 --- a/database/migrations/2018_04_07_210913_changes_for_v473.php +++ b/database/migrations/2018_04_07_210913_changes_for_v473.php @@ -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); + } + ); } } diff --git a/resources/views/rules/index.twig b/resources/views/rules/index.twig index bb3bf7decb..39ed2cd4bd 100644 --- a/resources/views/rules/index.twig +++ b/resources/views/rules/index.twig @@ -134,7 +134,7 @@ {% endif %} >
{{ rule.description|markdown }} {% endif %} - +
{% if rule.strict %}{{ 'rule_is_strict'|_ }}{% else %}{{ 'rule_is_not_strict'|_ }}{% endif %}
{% if rule.ruleTriggers.count > 0 %} diff --git a/resources/views/rules/rule/create.twig b/resources/views/rules/rule/create.twig index aadaff7a4e..5c04e33608 100644 --- a/resources/views/rules/rule/create.twig +++ b/resources/views/rules/rule/create.twig @@ -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')}) }} diff --git a/resources/views/rules/rule/edit.twig b/resources/views/rules/rule/edit.twig index e45e47e92f..61226548b3 100644 --- a/resources/views/rules/rule/edit.twig +++ b/resources/views/rules/rule/edit.twig @@ -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')}) }}