1 | <?php |
||
2 | declare(strict_types=1); |
||
3 | /** |
||
4 | * OBValidation.php |
||
5 | * Copyright (c) 2020 [email protected] |
||
6 | * |
||
7 | * This file is part of Firefly III (https://github.com/firefly-iii). |
||
8 | * |
||
9 | * This program is free software: you can redistribute it and/or modify |
||
10 | * it under the terms of the GNU Affero General Public License as |
||
11 | * published by the Free Software Foundation, either version 3 of the |
||
12 | * License, or (at your option) any later version. |
||
13 | * |
||
14 | * This program is distributed in the hope that it will be useful, |
||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
17 | * GNU Affero General Public License for more details. |
||
18 | * |
||
19 | * You should have received a copy of the GNU Affero General Public License |
||
20 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
||
21 | */ |
||
22 | |||
23 | namespace FireflyIII\Validation\Account; |
||
24 | |||
25 | use FireflyIII\Models\Account; |
||
26 | use FireflyIII\Models\AccountType; |
||
27 | use Log; |
||
28 | |||
29 | /** |
||
30 | * Trait OBValidation |
||
31 | */ |
||
32 | trait OBValidation |
||
33 | { |
||
34 | /** |
||
35 | * @param array $accountTypes |
||
36 | * |
||
37 | * @return bool |
||
38 | */ |
||
39 | abstract protected function canCreateTypes(array $accountTypes): bool; |
||
40 | |||
41 | /** |
||
42 | * @param int|null $accountId |
||
43 | * @param $accountName |
||
44 | * |
||
45 | * @return bool |
||
46 | */ |
||
47 | protected function validateOBDestination(?int $accountId, $accountName): bool |
||
48 | { |
||
49 | $result = null; |
||
50 | Log::debug(sprintf('Now in validateOBDestination(%d, "%s")', $accountId, $accountName)); |
||
51 | |||
52 | // source can be any of the following types. |
||
53 | $validTypes = $this->combinations[$this->transactionType][$this->source->accountType->type] ?? []; |
||
54 | if (null === $accountId && null === $accountName && false === $this->canCreateTypes($validTypes)) { |
||
55 | // if both values are NULL we return false, |
||
56 | // because the destination of a deposit can't be created. |
||
57 | $this->destError = (string) trans('validation.ob_dest_need_data'); |
||
58 | Log::error('Both values are NULL, cant create OB destination.'); |
||
59 | $result = false; |
||
60 | } |
||
61 | // if the account can be created anyway we don't need to search. |
||
62 | if (null === $result && true === $this->canCreateTypes($validTypes)) { |
||
63 | Log::debug('Can create some of these types, so return true.'); |
||
64 | $result = true; |
||
65 | } |
||
66 | |||
67 | if (null === $result) { |
||
68 | // otherwise try to find the account: |
||
69 | $search = $this->findExistingAccount($validTypes, (int) $accountId, (string) $accountName); |
||
70 | if (null === $search) { |
||
71 | Log::debug('findExistingAccount() returned NULL, so the result is false.', $validTypes); |
||
72 | $this->destError = (string) trans('validation.ob_dest_bad_data', ['id' => $accountId, 'name' => $accountName]); |
||
73 | $result = false; |
||
74 | } |
||
75 | if (null !== $search) { |
||
76 | Log::debug(sprintf('findExistingAccount() returned #%d ("%s"), so the result is true.', $search->id, $search->name)); |
||
77 | $this->destination = $search; |
||
78 | $result = true; |
||
79 | } |
||
80 | } |
||
81 | $result = $result ?? false; |
||
82 | Log::debug(sprintf('validateOBDestination(%d, "%s") will return %s', $accountId, $accountName, var_export($result, true))); |
||
83 | |||
84 | return $result; |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * Source of an opening balance can either be an asset account |
||
89 | * or an "initial balance account". The latter can be created. |
||
90 | * |
||
91 | * @param int|null $accountId |
||
92 | * @param string|null $accountName |
||
93 | * |
||
94 | * @return bool |
||
95 | */ |
||
96 | protected function validateOBSource(?int $accountId, ?string $accountName): bool |
||
97 | { |
||
98 | Log::debug(sprintf('Now in validateOBSource(%d, "%s")', $accountId, $accountName)); |
||
99 | Log::debug(sprintf('The account name is null: %s', var_export(null === $accountName, true))); |
||
100 | $result = null; |
||
101 | // source can be any of the following types. |
||
102 | $validTypes = array_keys($this->combinations[$this->transactionType]); |
||
103 | |||
104 | if (null === $accountId && null === $accountName && false === $this->canCreateTypes($validTypes)) { |
||
105 | // if both values are NULL return false, |
||
106 | // because the source of a deposit can't be created. |
||
107 | // (this never happens). |
||
108 | $this->sourceError = (string) trans('validation.ob_source_need_data'); |
||
109 | $result = false; |
||
110 | } |
||
111 | |||
112 | // if the user submits an ID only but that ID is not of the correct type, |
||
113 | // return false. |
||
114 | if (null !== $accountId && null === $accountName) { |
||
115 | Log::debug('Source ID is not null, but name is null.'); |
||
116 | $search = $this->accountRepository->findNull($accountId); |
||
117 | |||
118 | // the source resulted in an account, but it's not of a valid type. |
||
119 | if (null !== $search && !in_array($search->accountType->type, $validTypes, true)) { |
||
120 | $message = sprintf('User submitted only an ID (#%d), which is a "%s", so this is not a valid source.', $accountId, $search->accountType->type); |
||
121 | Log::debug($message); |
||
122 | $this->sourceError = $message; |
||
123 | $result = false; |
||
124 | } |
||
125 | // the source resulted in an account, AND it's of a valid type. |
||
126 | if (null !== $search && in_array($search->accountType->type, $validTypes, true)) { |
||
127 | Log::debug(sprintf('Found account of correct type: #%d, "%s"', $search->id, $search->name)); |
||
128 | $this->source = $search; |
||
129 | $result = true; |
||
130 | } |
||
131 | } |
||
132 | |||
133 | // if the account can be created anyway we don't need to search. |
||
134 | if (null === $result && true === $this->canCreateTypes($validTypes)) { |
||
135 | Log::debug('Result is still null.'); |
||
136 | $result = true; |
||
137 | |||
138 | // set the source to be a (dummy) initial balance account. |
||
139 | $account = new Account; |
||
140 | $accountType = AccountType::whereType(AccountType::INITIAL_BALANCE)->first(); |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
141 | $account->accountType = $accountType; |
||
142 | $this->source = $account; |
||
143 | } |
||
144 | $result = $result ?? false; |
||
145 | |||
146 | return $result; |
||
147 | } |
||
148 | } |