Date before and after triggers

This commit is contained in:
James Cole 2020-05-16 12:55:54 +02:00
parent c847621874
commit a4f66b3d86
5 changed files with 226 additions and 2 deletions

View File

@ -0,0 +1,107 @@
<?php
/**
* DateAfter.php
* Copyright (c) 2019 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\TransactionRules\Triggers;
use Carbon\Carbon;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Support\ParseDateString;
use Log;
/**
* Class DateAfter.
*/
final class DateAfter extends AbstractTrigger implements TriggerInterface
{
/**
* A trigger is said to "match anything", or match any given transaction,
* when the trigger value is very vague or has no restrictions. Easy examples
* are the "AmountMore"-trigger combined with an amount of 0: any given transaction
* has an amount of more than zero! Other examples are all the "Description"-triggers
* which have hard time handling empty trigger values such as "" or "*" (wild cards).
*
* If the user tries to create such a trigger, this method MUST return true so Firefly III
* can stop the storing / updating the trigger. If the trigger is in any way restrictive
* (even if it will still include 99.9% of the users transactions), this method MUST return
* false.
*
* @param mixed $value
*
* @return bool
*/
public static function willMatchEverything($value = null): bool
{
if (null !== $value) {
return false;
}
Log::error(sprintf('Cannot use %s with a null value.', self::class));
return true;
}
/**
* Returns true when category is X.
*
* @param TransactionJournal $journal
*
* @return bool
*/
public function triggered(TransactionJournal $journal): bool
{
/** @var Carbon $date */
$date = $journal->date;
Log::debug(sprintf('Found date on journal: %s', $date->format('Y-m-d')));
$dateParser = new ParseDateString();
try {
$ruleDate = $dateParser->parseDate($this->triggerValue);
} catch (FireflyException $e) {
Log::error('Cannot execute rule trigger.');
Log::error($e->getMessage());
return false;
}
if ($date->isAfter($ruleDate)) {
Log::debug(
sprintf(
'%s is after %s, so return true.',
$date->format('Y-m-d H:i:s'),
$ruleDate->format('Y-m-d H:i:s'),
)
);
return true;
}
Log::debug(
sprintf(
'%s is NOT after %s, so return true.',
$date->format('Y-m-d H:i:s'),
$ruleDate->format('Y-m-d H:i:s'),
)
);
return false;
}
}

View File

@ -0,0 +1,107 @@
<?php
/**
* DateBefore.php
* Copyright (c) 2019 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\TransactionRules\Triggers;
use Carbon\Carbon;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Support\ParseDateString;
use Log;
/**
* Class DateBefore.
*/
final class DateBefore extends AbstractTrigger implements TriggerInterface
{
/**
* A trigger is said to "match anything", or match any given transaction,
* when the trigger value is very vague or has no restrictions. Easy examples
* are the "AmountMore"-trigger combined with an amount of 0: any given transaction
* has an amount of more than zero! Other examples are all the "Description"-triggers
* which have hard time handling empty trigger values such as "" or "*" (wild cards).
*
* If the user tries to create such a trigger, this method MUST return true so Firefly III
* can stop the storing / updating the trigger. If the trigger is in any way restrictive
* (even if it will still include 99.9% of the users transactions), this method MUST return
* false.
*
* @param mixed $value
*
* @return bool
*/
public static function willMatchEverything($value = null): bool
{
if (null !== $value) {
return false;
}
Log::error(sprintf('Cannot use %s with a null value.', self::class));
return true;
}
/**
* Returns true when category is X.
*
* @param TransactionJournal $journal
*
* @return bool
*/
public function triggered(TransactionJournal $journal): bool
{
/** @var Carbon $date */
$date = $journal->date;
Log::debug(sprintf('Found date on journal: %s', $date->format('Y-m-d')));
$dateParser = new ParseDateString();
try {
$ruleDate = $dateParser->parseDate($this->triggerValue);
} catch (FireflyException $e) {
Log::error('Cannot execute rule trigger.');
Log::error($e->getMessage());
return false;
}
if ($date->isBefore($ruleDate)) {
Log::debug(
sprintf(
'%s is before %s, so return true.',
$date->format('Y-m-d H:i:s'),
$ruleDate->format('Y-m-d H:i:s'),
)
);
return true;
}
Log::debug(
sprintf(
'%s is NOT before %s, so return true.',
$date->format('Y-m-d H:i:s'),
$ruleDate->format('Y-m-d H:i:s'),
)
);
return false;
}
}

View File

@ -86,6 +86,8 @@ use FireflyIII\TransactionRules\Triggers\BudgetIs;
use FireflyIII\TransactionRules\Triggers\CategoryIs;
use FireflyIII\TransactionRules\Triggers\CurrencyIs;
use FireflyIII\TransactionRules\Triggers\DateIs;
use FireflyIII\TransactionRules\Triggers\DateBefore;
use FireflyIII\TransactionRules\Triggers\DateAfter;
use FireflyIII\TransactionRules\Triggers\DescriptionContains;
use FireflyIII\TransactionRules\Triggers\DescriptionEnds;
use FireflyIII\TransactionRules\Triggers\DescriptionIs;
@ -470,6 +472,8 @@ return [
'description_contains' => DescriptionContains::class,
'description_is' => DescriptionIs::class,
'date_is' => DateIs::class,
'date_before' => DateBefore::class,
'date_after' => DateAfter::class,
'transaction_type' => TransactionType::class,
'category_is' => CategoryIs::class,
'budget_is' => BudgetIs::class,
@ -557,6 +561,8 @@ return [
'notes_end',
'notes_are',
'date_is',
'date_before',
'date_after',
],
'test-triggers' => [

View File

@ -233,6 +233,7 @@ function updateActionInput(selectList) {
case 'clear_category':
case 'clear_budget':
case 'clear_notes':
case 'delete_transaction':
case 'remove_all_tags':
console.log('Select list value is ' + selectList.val() + ', so input needs to be disabled.');
inputResult.attr('disabled', 'disabled');
@ -278,7 +279,6 @@ function updateActionInput(selectList) {
console.log('Select list value is ' + selectList.val() + ', so input needs auto complete.');
createAutoComplete(inputResult, 'json/piggy-banks');
break;
break;
default:
console.log('Select list value is ' + selectList.val() + ', destroy auto complete, do nothing else.');
inputResult.typeahead('destroy');

View File

@ -423,7 +423,11 @@ return [
'rule_trigger_description_is' => 'Description is ":trigger_value"',
'rule_trigger_date_is_choice' => 'Transaction date is..',
'rule_trigger_date_is' => 'Transaction date is ":trigger_value"',
'rule_trigger_date_is' => 'Transaction date is ":trigger_value"',
'rule_trigger_date_before_choice' => 'Transaction date is before..',
'rule_trigger_date_before' => 'Transaction date is before ":trigger_value"',
'rule_trigger_date_after_choice' => 'Transaction date is after..',
'rule_trigger_date_after' => 'Transaction date is after ":trigger_value"',
'rule_trigger_budget_is_choice' => 'Budget is..',
'rule_trigger_budget_is' => 'Budget is ":trigger_value"',