1 | <?php |
||
2 | |||
3 | /** |
||
4 | * TransactionFactory.php |
||
5 | * Copyright (c) 2019 [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 | declare(strict_types=1); |
||
24 | |||
25 | namespace FireflyIII\Factory; |
||
26 | |||
27 | use FireflyIII\Exceptions\FireflyException; |
||
28 | use FireflyIII\Models\Account; |
||
29 | use FireflyIII\Models\Transaction; |
||
30 | use FireflyIII\Models\TransactionCurrency; |
||
31 | use FireflyIII\Models\TransactionJournal; |
||
32 | use FireflyIII\User; |
||
33 | use Illuminate\Database\QueryException; |
||
34 | use Log; |
||
35 | |||
36 | /** |
||
37 | * Class TransactionFactory |
||
38 | */ |
||
39 | class TransactionFactory |
||
40 | { |
||
41 | /** @var Account */ |
||
42 | private $account; |
||
43 | /** @var TransactionCurrency */ |
||
44 | private $currency; |
||
45 | /** @var TransactionCurrency */ |
||
46 | private $foreignCurrency; |
||
47 | /** @var TransactionJournal */ |
||
48 | private $journal; |
||
49 | /** @var bool */ |
||
50 | private $reconciled; |
||
51 | /** @var User */ |
||
52 | private $user; |
||
53 | |||
54 | /** |
||
55 | * Constructor. |
||
56 | * |
||
57 | * @codeCoverageIgnore |
||
58 | */ |
||
59 | public function __construct() |
||
60 | { |
||
61 | if ('testing' === config('app.env')) { |
||
62 | Log::warning(sprintf('%s should not be instantiated in the TEST environment!', get_class($this))); |
||
63 | } |
||
64 | $this->reconciled = false; |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * Create transaction with negative amount (for source accounts). |
||
69 | * |
||
70 | * @param string $amount |
||
71 | * @param string|null $foreignAmount |
||
72 | * |
||
73 | * @throws FireflyException |
||
74 | * @return Transaction |
||
75 | */ |
||
76 | public function createNegative(string $amount, ?string $foreignAmount): Transaction |
||
77 | { |
||
78 | if ('' === $foreignAmount) { |
||
79 | $foreignAmount = null; |
||
80 | } |
||
81 | if (null !== $foreignAmount) { |
||
82 | $foreignAmount = app('steam')->negative($foreignAmount); |
||
83 | } |
||
84 | |||
85 | return $this->create(app('steam')->negative($amount), $foreignAmount); |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * Create transaction with positive amount (for destination accounts). |
||
90 | * |
||
91 | * @param string $amount |
||
92 | * @param string|null $foreignAmount |
||
93 | * |
||
94 | * @throws FireflyException |
||
95 | * @return Transaction |
||
96 | */ |
||
97 | public function createPositive(string $amount, ?string $foreignAmount): Transaction |
||
98 | { |
||
99 | if ('' === $foreignAmount) { |
||
100 | $foreignAmount = null; |
||
101 | } |
||
102 | if (null !== $foreignAmount) { |
||
103 | $foreignAmount = app('steam')->positive($foreignAmount); |
||
104 | } |
||
105 | |||
106 | return $this->create(app('steam')->positive($amount), $foreignAmount); |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * @param Account $account |
||
111 | * |
||
112 | * @codeCoverageIgnore |
||
113 | */ |
||
114 | public function setAccount(Account $account): void |
||
115 | { |
||
116 | $this->account = $account; |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * @param TransactionCurrency $currency |
||
121 | * |
||
122 | * @codeCoverageIgnore |
||
123 | */ |
||
124 | public function setCurrency(TransactionCurrency $currency): void |
||
125 | { |
||
126 | $this->currency = $currency; |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * @param TransactionCurrency $foreignCurrency |null |
||
131 | * |
||
132 | * @codeCoverageIgnore |
||
133 | */ |
||
134 | public function setForeignCurrency(?TransactionCurrency $foreignCurrency): void |
||
135 | { |
||
136 | $this->foreignCurrency = $foreignCurrency; |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * @param TransactionJournal $journal |
||
141 | * |
||
142 | * @codeCoverageIgnore |
||
143 | */ |
||
144 | public function setJournal(TransactionJournal $journal): void |
||
145 | { |
||
146 | $this->journal = $journal; |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * @param bool $reconciled |
||
151 | * |
||
152 | * @codeCoverageIgnore |
||
153 | */ |
||
154 | public function setReconciled(bool $reconciled): void |
||
155 | { |
||
156 | $this->reconciled = $reconciled; |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * @param User $user |
||
161 | * |
||
162 | * @codeCoverageIgnore |
||
163 | */ |
||
164 | public function setUser(User $user): void |
||
165 | { |
||
166 | $this->user = $user; |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * @param string $amount |
||
171 | * @param string|null $foreignAmount |
||
172 | * |
||
173 | * @throws FireflyException |
||
174 | * @return Transaction |
||
175 | */ |
||
176 | private function create(string $amount, ?string $foreignAmount): Transaction |
||
177 | { |
||
178 | $result = null; |
||
0 ignored issues
–
show
Unused Code
introduced
by
![]() |
|||
179 | if ('' === $foreignAmount) { |
||
180 | $foreignAmount = null; |
||
181 | } |
||
182 | $data = [ |
||
183 | 'reconciled' => $this->reconciled, |
||
184 | 'account_id' => $this->account->id, |
||
185 | 'transaction_journal_id' => $this->journal->id, |
||
186 | 'description' => null, |
||
187 | 'transaction_currency_id' => $this->currency->id, |
||
188 | 'amount' => $amount, |
||
189 | 'foreign_amount' => null, |
||
190 | 'foreign_currency_id' => null, |
||
191 | 'identifier' => 0, |
||
192 | ]; |
||
193 | try { |
||
194 | $result = Transaction::create($data); |
||
195 | // @codeCoverageIgnoreStart |
||
196 | } catch (QueryException $e) { |
||
197 | Log::error(sprintf('Could not create transaction: %s', $e->getMessage()), $data); |
||
198 | Log::error($e->getMessage()); |
||
199 | Log::error($e->getTraceAsString()); |
||
200 | throw new FireflyException('Query exception when creating transaction.'); |
||
201 | } |
||
202 | if (null === $result) { |
||
203 | throw new FireflyException('Transaction is NULL.'); |
||
204 | } |
||
205 | // @codeCoverageIgnoreEnd |
||
206 | if (null !== $result) { |
||
207 | Log::debug( |
||
208 | sprintf( |
||
209 | 'Created transaction #%d (%s %s, account %s), part of journal #%d', |
||
210 | $result->id, |
||
211 | $this->currency->code, |
||
212 | $amount, |
||
213 | $this->account->name, |
||
214 | $this->journal->id |
||
215 | ) |
||
216 | ); |
||
217 | |||
218 | // do foreign currency thing: add foreign currency info to $one and $two if necessary. |
||
219 | if (null !== $this->foreignCurrency && null !== $foreignAmount && $this->foreignCurrency->id !== $this->currency->id && '' !== $foreignAmount) { |
||
220 | $result->foreign_currency_id = $this->foreignCurrency->id; |
||
221 | $result->foreign_amount = $foreignAmount; |
||
222 | } |
||
223 | $result->save(); |
||
224 | } |
||
225 | |||
226 | return $result; |
||
227 | } |
||
228 | } |
||
229 |