First code for #2723

This commit is contained in:
James Cole
2019-12-20 21:01:27 +01:00
parent b4af70421d
commit be2794406c
13 changed files with 567 additions and 101 deletions

View File

@@ -22,7 +22,6 @@ declare(strict_types=1);
namespace FireflyIII\TransactionRules\Triggers;
use FireflyIII\Models\Account;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use Log;
@@ -72,21 +71,18 @@ final class FromAccountContains extends AbstractTrigger implements TriggerInterf
*/
public function triggered(TransactionJournal $journal): bool
{
$fromAccountName = '';
/** @var JournalRepositoryInterface $repository */
$repository = app(JournalRepositoryInterface::class);
/** @var Account $account */
foreach ($repository->getJournalSourceAccounts($journal, false) as $account) {
$fromAccountName .= strtolower($account->name);
}
$search = strtolower($this->triggerValue);
$strpos = strpos($fromAccountName, $search);
$source = $repository->getSourceAccount($journal);
$strpos = stripos($source->name, $this->triggerValue);
if (!(false === $strpos)) {
Log::debug(sprintf('RuleTrigger FromAccountContains for journal #%d: "%s" contains "%s", return true.', $journal->id, $fromAccountName, $search));
Log::debug(
sprintf(
'RuleTrigger FromAccountContains for journal #%d: "%s" contains "%s", return true.',
$journal->id, $source->name, $this->triggerValue
)
);
return true;
}
@@ -95,8 +91,8 @@ final class FromAccountContains extends AbstractTrigger implements TriggerInterf
sprintf(
'RuleTrigger FromAccountContains for journal #%d: "%s" does not contain "%s", return false.',
$journal->id,
$fromAccountName,
$search
$source->name,
$this->triggerValue
)
);

View File

@@ -22,7 +22,6 @@ declare(strict_types=1);
namespace FireflyIII\TransactionRules\Triggers;
use FireflyIII\Models\Account;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use Log;
@@ -72,19 +71,13 @@ final class FromAccountEnds extends AbstractTrigger implements TriggerInterface
*/
public function triggered(TransactionJournal $journal): bool
{
$name = '';
/** @var JournalRepositoryInterface $repository */
$repository = app(JournalRepositoryInterface::class);
/** @var Account $account */
foreach ($repository->getJournalSourceAccounts($journal, false) as $account) {
$name .= strtolower($account->name);
}
$nameLength = strlen($name);
$search = strtolower($this->triggerValue);
$repository = app(JournalRepositoryInterface::class);
$source = $repository->getSourceAccount($journal);
$nameLength = strlen($source->name);
$search = $this->triggerValue;
$searchLength = strlen($search);
$part = substr($source->name, $searchLength * -1);
// if the string to search for is longer than the account name,
// it will never be in the account name.
@@ -94,9 +87,7 @@ final class FromAccountEnds extends AbstractTrigger implements TriggerInterface
return false;
}
$part = substr($name, $searchLength * -1);
if ($part === $search) {
if (strtolower($part) === strtolower($search)) {
Log::debug(sprintf('RuleTrigger FromAccountEnds for journal #%d: "%s" ends with "%s", return true.', $journal->id, $name, $search));
return true;

View File

@@ -22,7 +22,6 @@ declare(strict_types=1);
namespace FireflyIII\TransactionRules\Triggers;
use FireflyIII\Models\Account;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use Log;
@@ -72,19 +71,12 @@ final class FromAccountIs extends AbstractTrigger implements TriggerInterface
*/
public function triggered(TransactionJournal $journal): bool
{
$name = '';
/** @var JournalRepositoryInterface $repository */
$repository = app(JournalRepositoryInterface::class);
$source = $repository->getSourceAccount($journal);
$search = strtolower($this->triggerValue);
/** @var Account $account */
foreach ($repository->getJournalSourceAccounts($journal, false) as $account) {
$name .= strtolower($account->name);
}
$search = strtolower($this->triggerValue);
if ($name === $search) {
if (strtolower($source->name) === $search) {
Log::debug(sprintf('RuleTrigger FromAccountIs for journal #%d: "%s" is "%s", return true.', $journal->id, $name, $search));
return true;

View File

@@ -0,0 +1,103 @@
<?php
/**
* FromAccountContains.php
* Copyright (c) 2019 thegrumpydictator@gmail.com
*
* 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 FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use Log;
/**
* Class FromAccountContains.
*/
final class FromAccountNumberContains 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) {
$res = '' === (string)$value;
if (true === $res) {
Log::error(sprintf('Cannot use %s with "" as a value.', self::class));
}
return $res;
}
Log::error(sprintf('Cannot use %s with a null value.', self::class));
return true;
}
/**
* Returns true when from-account contains X
*
* @param TransactionJournal $journal
*
* @return bool
*/
public function triggered(TransactionJournal $journal): bool
{
/** @var JournalRepositoryInterface $repository */
$repository = app(JournalRepositoryInterface::class);
$source = $repository->getSourceAccount($journal);
$strpos1 = stripos($source->iban, $this->triggerValue);
$strpos2 = stripos($source->account_number, $this->triggerValue);
if (!(false === $strpos1) || !(false === $strpos2)) {
Log::debug(
sprintf(
'RuleTrigger FromAccountContains for journal #%d: "%s" or "%s" contains "%s", return true.',
$journal->id, $source->iban, $source->account_number, $this->triggerValue
)
);
return true;
}
Log::debug(
sprintf(
'RuleTrigger FromAccountContains for journal #%d: "%s" and "%s" does not contain "%s", return false.',
$journal->id,
$source->iban,
$source->account_number,
$this->triggerValue
)
);
return false;
}
}

View File

@@ -0,0 +1,104 @@
<?php
/**
* FromAccountEnds.php
* Copyright (c) 2019 thegrumpydictator@gmail.com
*
* 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 FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use Log;
/**
* Class FromAccountNumberEnds.
*/
final class FromAccountNumberEnds 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) {
$res = '' === (string)$value;
if (true === $res) {
Log::error(sprintf('Cannot use %s with "" as a value.', self::class));
}
return $res;
}
Log::error(sprintf('Cannot use %s with a null value.', self::class));
return true;
}
/**
* Returns true when from account ends with X
*
* @param TransactionJournal $journal
*
* @return bool
*/
public function triggered(TransactionJournal $journal): bool
{
/** @var JournalRepositoryInterface $repository */
$repository = app(JournalRepositoryInterface::class);
$source = $repository->getSourceAccount($journal);
$search = $this->triggerValue;
$searchLength = strlen($search);
$part1 = substr($source->iban, $searchLength * -1);
$part2 = substr($source->account_number, $searchLength * -1);
if (strtolower($part1) === strtolower($search)
|| strtolower($part2) === strtolower($search)) {
Log::debug(
sprintf(
'RuleTrigger FromAccountEnds for journal #%d: "%s" or "%s" ends with "%s", return true.',
$journal->id, $part1, $part2, $search
)
);
return true;
}
Log::debug(
sprintf(
'RuleTrigger FromAccountEnds for journal #%d: "%s" and "%s" do not end with "%s", return false.',
$journal->id, $part1, $part2, $search
)
);
return false;
}
}

View File

@@ -0,0 +1,99 @@
<?php
/**
* FromAccountNumberIs.php
* Copyright (c) 2019 thegrumpydictator@gmail.com
*
* 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 FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use Log;
/**
* Class FromAccountNumberIs.
*/
final class FromAccountNumberIs 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) {
$res = '' === (string)$value;
if (true === $res) {
Log::error(sprintf('Cannot use %s with "" as a value.', self::class));
}
return $res;
}
Log::error(sprintf('Cannot use %s with a null value.', self::class));
return true;
}
/**
* Returns true when from-account is X.
*
* @param TransactionJournal $journal
*
* @return bool
*/
public function triggered(TransactionJournal $journal): bool
{
/** @var JournalRepositoryInterface $repository */
$repository = app(JournalRepositoryInterface::class);
$source = $repository->getSourceAccount($journal);
$search = strtolower($this->triggerValue);
if (strtolower($source->iban) === $search || strtolower($source->account_number) === $search) {
Log::debug(
sprintf(
'RuleTrigger FromAccountIs for journal #%d: "%s" or "%s" is "%s", return true.', $journal->id,
$source->iban, $source->account_number, $search
)
);
return true;
}
Log::debug(
sprintf(
'RuleTrigger FromAccountIs for journal #%d: "%s" and "%s" is NOT "%s", return false.', $journal->id, $source->iban, $source->account_number,
$search
)
);
return false;
}
}

View File

@@ -0,0 +1,101 @@
<?php
/**
* FromAccountNumberStarts.php
* Copyright (c) 2019 thegrumpydictator@gmail.com
*
* 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 FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use Log;
/**
* Class FromAccountNumberStarts.
*/
final class FromAccountNumberStarts 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) {
$res = '' === (string)$value;
if (true === $res) {
Log::error(sprintf('Cannot use %s with "" as a value.', self::class));
}
return $res;
}
Log::error(sprintf('Cannot use %s with a null value.', self::class));
return true;
}
/**
* Returns true when from-account starts with X.
*
* @param TransactionJournal $journal
*
* @return bool
*/
public function triggered(TransactionJournal $journal): bool
{
/** @var JournalRepositoryInterface $repository */
$repository = app(JournalRepositoryInterface::class);
$source = $repository->getSourceAccount($journal);
$search = strtolower($this->triggerValue);
$part1 = strtolower(substr($source->iban, 0, strlen($search)));
$part2 = strtolower(substr($source->account_number, 0, strlen($search)));
if ($part1 === $search || $part2 === $search) {
Log::debug(
sprintf(
'RuleTrigger FromAccountStarts for journal #%d: "%s" or "%s" starts with "%s", return true.', $journal->id,
$part1, $part2, $search
)
);
return true;
}
Log::debug(
sprintf(
'RuleTrigger FromAccountStarts for journal #%d: "%s" and "%s" do not start with "%s", return false.',
$journal->id, $part1, $part2, $search
)
);
return false;
}
}

View File

@@ -22,7 +22,6 @@ declare(strict_types=1);
namespace FireflyIII\TransactionRules\Triggers;
use FireflyIII\Models\Account;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use Log;
@@ -72,18 +71,11 @@ final class FromAccountStarts extends AbstractTrigger implements TriggerInterfac
*/
public function triggered(TransactionJournal $journal): bool
{
$name = '';
/** @var JournalRepositoryInterface $repository */
$repository = app(JournalRepositoryInterface::class);
/** @var Account $account */
foreach ($repository->getJournalSourceAccounts($journal, false) as $account) {
$name .= strtolower($account->name);
}
$search = strtolower($this->triggerValue);
$part = substr($name, 0, strlen($search));
$source = $repository->getSourceAccount($journal);
$search = strtolower($this->triggerValue);
$part = substr($source->name, 0, strlen($search));
if ($part === $search) {
Log::debug(sprintf('RuleTrigger FromAccountStarts for journal #%d: "%s" starts with "%s", return true.', $journal->id, $name, $search));