1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* ToAccountNumberStarts.php |
4
|
|
|
* Copyright (c) 2019 [email protected] |
5
|
|
|
* |
6
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii). |
7
|
|
|
* |
8
|
|
|
* This program is free software: you can redistribute it and/or modify |
9
|
|
|
* it under the terms of the GNU Affero General Public License as |
10
|
|
|
* published by the Free Software Foundation, either version 3 of the |
11
|
|
|
* License, or (at your option) any later version. |
12
|
|
|
* |
13
|
|
|
* This program is distributed in the hope that it will be useful, |
14
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16
|
|
|
* GNU Affero General Public License for more details. |
17
|
|
|
* |
18
|
|
|
* You should have received a copy of the GNU Affero General Public License |
19
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
20
|
|
|
*/ |
21
|
|
|
declare(strict_types=1); |
22
|
|
|
|
23
|
|
|
namespace FireflyIII\TransactionRules\Triggers; |
24
|
|
|
|
25
|
|
|
use FireflyIII\Models\TransactionJournal; |
26
|
|
|
use FireflyIII\Repositories\Journal\JournalRepositoryInterface; |
27
|
|
|
use Log; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Class FromAccountNumberStarts. |
31
|
|
|
*/ |
32
|
|
|
final class ToAccountNumberStarts extends AbstractTrigger implements TriggerInterface |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* A trigger is said to "match anything", or match any given transaction, |
36
|
|
|
* when the trigger value is very vague or has no restrictions. Easy examples |
37
|
|
|
* are the "AmountMore"-trigger combined with an amount of 0: any given transaction |
38
|
|
|
* has an amount of more than zero! Other examples are all the "Description"-triggers |
39
|
|
|
* which have hard time handling empty trigger values such as "" or "*" (wild cards). |
40
|
|
|
* |
41
|
|
|
* If the user tries to create such a trigger, this method MUST return true so Firefly III |
42
|
|
|
* can stop the storing / updating the trigger. If the trigger is in any way restrictive |
43
|
|
|
* (even if it will still include 99.9% of the users transactions), this method MUST return |
44
|
|
|
* false. |
45
|
|
|
* |
46
|
|
|
* @param mixed $value |
47
|
|
|
* |
48
|
|
|
* @return bool |
49
|
|
|
*/ |
50
|
|
|
public static function willMatchEverything($value = null): bool |
51
|
|
|
{ |
52
|
|
|
if (null !== $value) { |
53
|
|
|
$res = '' === (string)$value; |
54
|
|
|
if (true === $res) { |
55
|
|
|
Log::error(sprintf('Cannot use %s with "" as a value.', self::class)); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $res; |
59
|
|
|
} |
60
|
|
|
Log::error(sprintf('Cannot use %s with a null value.', self::class)); |
61
|
|
|
|
62
|
|
|
return true; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Returns true when from-account starts with X. |
67
|
|
|
* |
68
|
|
|
* @param TransactionJournal $journal |
69
|
|
|
* |
70
|
|
|
* @return bool |
71
|
|
|
*/ |
72
|
|
|
public function triggered(TransactionJournal $journal): bool |
73
|
|
|
{ |
74
|
|
|
/** @var JournalRepositoryInterface $repository */ |
75
|
|
|
$repository = app(JournalRepositoryInterface::class); |
76
|
|
|
$dest = $repository->getDestinationAccount($journal); |
77
|
|
|
$search = strtolower($this->triggerValue); |
78
|
|
|
$part1 = strtolower(substr((string)$dest->iban, 0, strlen($search))); |
79
|
|
|
$part2 = strtolower(substr($dest->account_number, 0, strlen($search))); |
80
|
|
|
|
81
|
|
|
if ($part1 === $search || $part2 === $search) { |
82
|
|
|
Log::debug( |
83
|
|
|
sprintf( |
84
|
|
|
'RuleTrigger %s for journal #%d: "%s" or "%s" starts with "%s", return true.', |
85
|
|
|
get_class($this), $journal->id, $part1, $part2, $search |
86
|
|
|
) |
87
|
|
|
); |
88
|
|
|
|
89
|
|
|
return true; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
Log::debug( |
93
|
|
|
sprintf( |
94
|
|
|
'RuleTrigger %s for journal #%d: "%s" and "%s" do not start with "%s", return false.', |
95
|
|
|
get_class($this), $journal->id, $part1, $part2, $search |
96
|
|
|
) |
97
|
|
|
); |
98
|
|
|
|
99
|
|
|
return false; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|