1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* AccountValidator.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
|
|
|
|
22
|
|
|
declare(strict_types=1); |
23
|
|
|
|
24
|
|
|
namespace FireflyIII\Validation; |
25
|
|
|
|
26
|
|
|
use FireflyIII\Models\Account; |
27
|
|
|
use FireflyIII\Models\AccountType; |
28
|
|
|
use FireflyIII\Models\TransactionType; |
29
|
|
|
use FireflyIII\Repositories\Account\AccountRepositoryInterface; |
30
|
|
|
use FireflyIII\User; |
31
|
|
|
use FireflyIII\Validation\Account\AccountValidatorProperties; |
32
|
|
|
use FireflyIII\Validation\Account\DepositValidation; |
33
|
|
|
use FireflyIII\Validation\Account\OBValidation; |
34
|
|
|
use FireflyIII\Validation\Account\ReconciliationValidation; |
35
|
|
|
use FireflyIII\Validation\Account\TransferValidation; |
36
|
|
|
use FireflyIII\Validation\Account\WithdrawalValidation; |
37
|
|
|
use Log; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Class AccountValidator |
41
|
|
|
*/ |
42
|
|
|
class AccountValidator |
43
|
|
|
{ |
44
|
|
|
use AccountValidatorProperties, WithdrawalValidation, DepositValidation, TransferValidation, ReconciliationValidation, OBValidation; |
|
|
|
|
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* AccountValidator constructor. |
48
|
|
|
*/ |
49
|
|
|
public function __construct() |
50
|
|
|
{ |
51
|
|
|
$this->createMode = false; |
52
|
|
|
$this->destError = 'No error yet.'; |
53
|
|
|
$this->sourceError = 'No error yet.'; |
54
|
|
|
$this->combinations = config('firefly.source_dests'); |
55
|
|
|
|
56
|
|
|
/** @var AccountRepositoryInterface accountRepository */ |
57
|
|
|
$this->accountRepository = app(AccountRepositoryInterface::class); |
58
|
|
|
|
59
|
|
|
if ('testing' === config('app.env')) { |
60
|
|
|
Log::warning(sprintf('%s should not be instantiated in the TEST environment!', get_class($this))); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param string $transactionType |
66
|
|
|
*/ |
67
|
|
|
public function setTransactionType(string $transactionType): void |
68
|
|
|
{ |
69
|
|
|
Log::debug(sprintf('Transaction type for validator is now %s', ucfirst($transactionType))); |
70
|
|
|
$this->transactionType = ucfirst($transactionType); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param User $user |
75
|
|
|
*/ |
76
|
|
|
public function setUser(User $user): void |
77
|
|
|
{ |
78
|
|
|
$this->user = $user; |
79
|
|
|
$this->accountRepository->setUser($user); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param int|null $accountId |
84
|
|
|
* @param string|null $accountName |
85
|
|
|
* @param string|null $accountIban |
86
|
|
|
* |
87
|
|
|
* @return bool |
88
|
|
|
*/ |
89
|
|
|
public function validateDestination(?int $accountId, ?string $accountName, ?string $accountIban): bool |
90
|
|
|
{ |
91
|
|
|
Log::debug(sprintf('Now in AccountValidator::validateDestination(%d, "%s", "%s")', $accountId, $accountName, $accountIban)); |
92
|
|
|
if (null === $this->source) { |
93
|
|
|
Log::error('Source is NULL, always FALSE.'); |
94
|
|
|
$this->destError = 'No source account validation has taken place yet. Please do this first or overrule the object.'; |
95
|
|
|
|
96
|
|
|
return false; |
97
|
|
|
} |
98
|
|
|
switch ($this->transactionType) { |
99
|
|
|
default: |
100
|
|
|
$this->destError = sprintf('AccountValidator::validateDestination cannot handle "%s", so it will always return false.', $this->transactionType); |
101
|
|
|
Log::error(sprintf('AccountValidator::validateDestination cannot handle "%s", so it will always return false.', $this->transactionType)); |
102
|
|
|
|
103
|
|
|
$result = false; |
104
|
|
|
break; |
105
|
|
|
|
106
|
|
|
case TransactionType::WITHDRAWAL: |
107
|
|
|
$result = $this->validateWithdrawalDestination($accountId, $accountName); |
108
|
|
|
break; |
109
|
|
|
case TransactionType::DEPOSIT: |
110
|
|
|
$result = $this->validateDepositDestination($accountId, $accountName); |
111
|
|
|
break; |
112
|
|
|
case TransactionType::TRANSFER: |
113
|
|
|
$result = $this->validateTransferDestination($accountId, $accountName); |
114
|
|
|
break; |
115
|
|
|
case TransactionType::OPENING_BALANCE: |
116
|
|
|
$result = $this->validateOBDestination($accountId, $accountName); |
117
|
|
|
break; |
118
|
|
|
case TransactionType::RECONCILIATION: |
119
|
|
|
$result = $this->validateReconciliationDestination($accountId); |
120
|
|
|
break; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $result; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param int|null $accountId |
128
|
|
|
* @param string|null $accountName |
129
|
|
|
* @param string|null $accountIban |
130
|
|
|
* |
131
|
|
|
* @return bool |
132
|
|
|
*/ |
133
|
|
|
public function validateSource(?int $accountId, ?string $accountName, ?string $accountIban): bool |
134
|
|
|
{ |
135
|
|
|
Log::debug(sprintf('Now in AccountValidator::validateSource(%d, "%s", "%s")', $accountId, $accountName, $accountIban)); |
136
|
|
|
switch ($this->transactionType) { |
137
|
|
|
default: |
138
|
|
|
$result = false; |
139
|
|
|
$this->sourceError = 'Firefly III cannot validate the account information you submitted.'; |
140
|
|
|
Log::error(sprintf('AccountValidator::validateSource cannot handle "%s", so it will always return false.', $this->transactionType)); |
141
|
|
|
break; |
142
|
|
|
case TransactionType::WITHDRAWAL: |
143
|
|
|
$result = $this->validateWithdrawalSource($accountId, $accountName); |
144
|
|
|
break; |
145
|
|
|
case TransactionType::DEPOSIT: |
146
|
|
|
$result = $this->validateDepositSource($accountId, $accountName); |
147
|
|
|
break; |
148
|
|
|
case TransactionType::TRANSFER: |
149
|
|
|
$result = $this->validateTransferSource($accountId, $accountName); |
150
|
|
|
break; |
151
|
|
|
case TransactionType::OPENING_BALANCE: |
152
|
|
|
$result = $this->validateOBSource($accountId, $accountName); |
153
|
|
|
break; |
154
|
|
|
case TransactionType::RECONCILIATION: |
155
|
|
|
Log::debug('Calling validateReconciliationSource'); |
156
|
|
|
$result = $this->validateReconciliationSource($accountId); |
157
|
|
|
break; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
return $result; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @param string $accountType |
165
|
|
|
* |
166
|
|
|
* @return bool |
167
|
|
|
*/ |
168
|
|
|
protected function canCreateType(string $accountType): bool |
169
|
|
|
{ |
170
|
|
|
$canCreate = [AccountType::EXPENSE, AccountType::REVENUE, AccountType::INITIAL_BALANCE]; |
171
|
|
|
if (in_array($accountType, $canCreate, true)) { |
172
|
|
|
return true; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
return false; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @param array $accountTypes |
180
|
|
|
* |
181
|
|
|
* @return bool |
182
|
|
|
*/ |
183
|
|
|
protected function canCreateTypes(array $accountTypes): bool |
184
|
|
|
{ |
185
|
|
|
Log::debug('Can we create any of these types?', $accountTypes); |
186
|
|
|
/** @var string $accountType */ |
187
|
|
|
foreach ($accountTypes as $accountType) { |
188
|
|
|
if ($this->canCreateType($accountType)) { |
189
|
|
|
Log::debug(sprintf('YES, we can create a %s', $accountType)); |
190
|
|
|
|
191
|
|
|
return true; |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
Log::debug('NO, we cant create any of those.'); |
195
|
|
|
|
196
|
|
|
return false; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @param array $validTypes |
201
|
|
|
* @param int $accountId |
202
|
|
|
* @param string $accountName |
203
|
|
|
* |
204
|
|
|
* @return Account|null |
205
|
|
|
*/ |
206
|
|
|
protected function findExistingAccount(array $validTypes, int $accountId, string $accountName): ?Account |
207
|
|
|
{ |
208
|
|
|
// find by ID |
209
|
|
|
if ($accountId > 0) { |
210
|
|
|
$first = $this->accountRepository->findNull($accountId); |
211
|
|
|
if ((null !== $first) && in_array($first->accountType->type, $validTypes, true)) { |
212
|
|
|
return $first; |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
// find by name: |
217
|
|
|
if ('' !== $accountName) { |
218
|
|
|
return $this->accountRepository->findByName($accountName, $validTypes); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
return null; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
|
225
|
|
|
} |
226
|
|
|
|