1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* SupportJournalsTrait.php |
4
|
|
|
* Copyright (c) 2017 [email protected] |
5
|
|
|
* |
6
|
|
|
* This file is part of Firefly III. |
7
|
|
|
* |
8
|
|
|
* Firefly III is free software: you can redistribute it and/or modify |
9
|
|
|
* it under the terms of the GNU General Public License as published by |
10
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
11
|
|
|
* (at your option) any later version. |
12
|
|
|
* |
13
|
|
|
* Firefly III 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 General Public License for more details. |
17
|
|
|
* |
18
|
|
|
* You should have received a copy of the GNU General Public License |
19
|
|
|
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>. |
20
|
|
|
*/ |
21
|
|
|
declare(strict_types=1); |
22
|
|
|
|
23
|
|
|
namespace FireflyIII\Repositories\Journal; |
24
|
|
|
|
25
|
|
|
use FireflyIII\Exceptions\FireflyException; |
26
|
|
|
use FireflyIII\Models\Account; |
27
|
|
|
use FireflyIII\Models\AccountType; |
28
|
|
|
use FireflyIII\Models\Budget; |
29
|
|
|
use FireflyIII\Models\Category; |
30
|
|
|
use FireflyIII\Models\TransactionJournal; |
31
|
|
|
use FireflyIII\Models\TransactionType; |
32
|
|
|
use FireflyIII\User; |
33
|
|
|
use Log; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Trait SupportJournalsTrait. |
37
|
|
|
*/ |
38
|
|
|
trait SupportJournalsTrait |
39
|
|
|
{ |
40
|
|
|
/** |
41
|
|
|
* @param User $user |
42
|
|
|
* @param TransactionType $type |
43
|
|
|
* @param array $data |
44
|
|
|
* |
45
|
|
|
* @return array |
46
|
|
|
* |
47
|
|
|
* @throws FireflyException |
48
|
|
|
*/ |
49
|
|
|
protected function storeAccounts(User $user, TransactionType $type, array $data): array |
50
|
|
|
{ |
51
|
|
|
$accounts = [ |
52
|
|
|
'source' => null, |
53
|
|
|
'destination' => null, |
54
|
|
|
]; |
55
|
|
|
|
56
|
|
|
Log::debug(sprintf('Going to store accounts for type %s', $type->type)); |
57
|
|
|
switch ($type->type) { |
58
|
|
|
case TransactionType::WITHDRAWAL: |
59
|
|
|
$accounts = $this->storeWithdrawalAccounts($user, $data); |
60
|
|
|
break; |
61
|
|
|
|
62
|
|
|
case TransactionType::DEPOSIT: |
63
|
|
|
$accounts = $this->storeDepositAccounts($user, $data); |
64
|
|
|
|
65
|
|
|
break; |
66
|
|
|
case TransactionType::TRANSFER: |
67
|
|
|
$accounts['source'] = Account::where('user_id', $user->id)->where('id', $data['source_account_id'])->first(); |
68
|
|
|
$accounts['destination'] = Account::where('user_id', $user->id)->where('id', $data['destination_account_id'])->first(); |
69
|
|
|
break; |
70
|
|
|
case TransactionType::RECONCILIATION: |
71
|
|
|
$accounts['source'] = $data['source']; |
72
|
|
|
$accounts['destination'] = $data['destination']; |
73
|
|
|
unset($data['source'], $data['destination']); |
74
|
|
|
break; |
75
|
|
|
default: |
76
|
|
|
throw new FireflyException(sprintf('Did not recognise transaction type "%s".', $type->type)); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
if (null === $accounts['source']) { |
80
|
|
|
Log::error('"source"-account is null, so we cannot continue!', ['data' => $data]); |
81
|
|
|
throw new FireflyException('"source"-account is null, so we cannot continue!'); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
if (null === $accounts['destination']) { |
85
|
|
|
Log::error('"destination"-account is null, so we cannot continue!', ['data' => $data]); |
86
|
|
|
throw new FireflyException('"destination"-account is null, so we cannot continue!'); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $accounts; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param TransactionJournal $journal |
94
|
|
|
* @param int $budgetId |
95
|
|
|
*/ |
96
|
|
|
protected function storeBudgetWithJournal(TransactionJournal $journal, int $budgetId) |
97
|
|
|
{ |
98
|
|
|
if (intval($budgetId) > 0 && TransactionType::WITHDRAWAL === $journal->transactionType->type) { |
99
|
|
|
/** @var \FireflyIII\Models\Budget $budget */ |
100
|
|
|
$budget = Budget::find($budgetId); |
101
|
|
|
$journal->budgets()->save($budget); |
102
|
|
|
} |
103
|
|
|
$journal->touch(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param TransactionJournal $journal |
108
|
|
|
* @param string $category |
109
|
|
|
*/ |
110
|
|
|
protected function storeCategoryWithJournal(TransactionJournal $journal, string $category) |
111
|
|
|
{ |
112
|
|
|
if (strlen($category) > 0) { |
113
|
|
|
$category = Category::firstOrCreateEncrypted(['name' => $category, 'user_id' => $journal->user_id]); |
114
|
|
|
$journal->categories()->save($category); |
115
|
|
|
} |
116
|
|
|
$journal->touch(); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param User $user |
121
|
|
|
* @param array $data |
122
|
|
|
* |
123
|
|
|
* @return array |
124
|
|
|
* |
125
|
|
|
* @throws FireflyException |
126
|
|
|
* @throws FireflyException |
127
|
|
|
*/ |
128
|
|
|
protected function storeDepositAccounts(User $user, array $data): array |
129
|
|
|
{ |
130
|
|
|
Log::debug('Now in storeDepositAccounts().'); |
131
|
|
|
$destinationAccount = Account::where('user_id', $user->id)->where('id', $data['destination_account_id'])->first(['accounts.*']); |
132
|
|
|
|
133
|
|
|
Log::debug(sprintf('Destination account is #%d ("%s")', $destinationAccount->id, $destinationAccount->name)); |
134
|
|
|
|
135
|
|
|
if (strlen($data['source_account_name']) > 0) { |
136
|
|
|
$sourceType = AccountType::where('type', 'Revenue account')->first(); |
137
|
|
|
$sourceAccount = Account::firstOrCreateEncrypted( |
138
|
|
|
['user_id' => $user->id, 'account_type_id' => $sourceType->id, 'name' => $data['source_account_name']] |
139
|
|
|
); |
140
|
|
|
// always make account active |
141
|
|
|
$sourceAccount->active = true; |
142
|
|
|
$sourceAccount->save(); |
143
|
|
|
|
144
|
|
|
Log::debug(sprintf('source account name is "%s", account is %d', $data['source_account_name'], $sourceAccount->id)); |
145
|
|
|
|
146
|
|
|
return [ |
147
|
|
|
'source' => $sourceAccount, |
148
|
|
|
'destination' => $destinationAccount, |
149
|
|
|
]; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
Log::debug('source_account_name is empty, so default to cash account!'); |
153
|
|
|
|
154
|
|
|
$sourceType = AccountType::where('type', AccountType::CASH)->first(); |
155
|
|
|
$sourceAccount = Account::firstOrCreateEncrypted( |
156
|
|
|
['user_id' => $user->id, 'account_type_id' => $sourceType->id, 'name' => 'Cash account'] |
157
|
|
|
); |
158
|
|
|
// always make account active |
159
|
|
|
$sourceAccount->active = true; |
160
|
|
|
$sourceAccount->save(); |
161
|
|
|
|
162
|
|
|
return [ |
163
|
|
|
'source' => $sourceAccount, |
164
|
|
|
'destination' => $destinationAccount, |
165
|
|
|
]; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @param User $user |
170
|
|
|
* @param array $data |
171
|
|
|
* |
172
|
|
|
* @return array |
173
|
|
|
* |
174
|
|
|
* @throws FireflyException |
175
|
|
|
* @throws FireflyException |
176
|
|
|
*/ |
177
|
|
|
protected function storeWithdrawalAccounts(User $user, array $data): array |
178
|
|
|
{ |
179
|
|
|
Log::debug('Now in storeWithdrawalAccounts().'); |
180
|
|
|
$sourceAccount = Account::where('user_id', $user->id)->where('id', $data['source_account_id'])->first(['accounts.*']); |
181
|
|
|
|
182
|
|
|
Log::debug(sprintf('Source account is #%d ("%s")', $sourceAccount->id, $sourceAccount->name)); |
183
|
|
|
|
184
|
|
|
if (strlen($data['destination_account_name']) > 0) { |
185
|
|
|
$destinationType = AccountType::where('type', AccountType::EXPENSE)->first(); |
186
|
|
|
$destinationAccount = Account::firstOrCreateEncrypted( |
187
|
|
|
[ |
188
|
|
|
'user_id' => $user->id, |
189
|
|
|
'account_type_id' => $destinationType->id, |
190
|
|
|
'name' => $data['destination_account_name'], |
191
|
|
|
] |
192
|
|
|
); |
193
|
|
|
|
194
|
|
|
// always make account active |
195
|
|
|
$destinationAccount->active = true; |
196
|
|
|
$destinationAccount->save(); |
197
|
|
|
|
198
|
|
|
Log::debug(sprintf('destination account name is "%s", account is %d', $data['destination_account_name'], $destinationAccount->id)); |
199
|
|
|
|
200
|
|
|
return [ |
201
|
|
|
'source' => $sourceAccount, |
202
|
|
|
'destination' => $destinationAccount, |
203
|
|
|
]; |
204
|
|
|
} |
205
|
|
|
Log::debug('destination_account_name is empty, so default to cash account!'); |
206
|
|
|
$destinationType = AccountType::where('type', AccountType::CASH)->first(); |
207
|
|
|
$destinationAccount = Account::firstOrCreateEncrypted( |
208
|
|
|
['user_id' => $user->id, 'account_type_id' => $destinationType->id, 'name' => 'Cash account'] |
209
|
|
|
); |
210
|
|
|
// always make account active |
211
|
|
|
$destinationAccount->active = true; |
212
|
|
|
$destinationAccount->save(); |
213
|
|
|
|
214
|
|
|
return [ |
215
|
|
|
'source' => $sourceAccount, |
216
|
|
|
'destination' => $destinationAccount, |
217
|
|
|
]; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* This method checks the data array and the given accounts to verify that the native amount, currency |
222
|
|
|
* and possible the foreign currency and amount are properly saved. |
223
|
|
|
* |
224
|
|
|
* @param array $data |
225
|
|
|
* @param array $accounts |
226
|
|
|
* |
227
|
|
|
* @return array |
228
|
|
|
* |
229
|
|
|
* @throws FireflyException |
230
|
|
|
*/ |
231
|
|
|
protected function verifyNativeAmount(array $data, array $accounts): array |
232
|
|
|
{ |
233
|
|
|
/** @var TransactionType $transactionType */ |
234
|
|
|
$transactionType = TransactionType::where('type', ucfirst($data['what']))->first(); |
235
|
|
|
$submittedCurrencyId = $data['currency_id']; |
236
|
|
|
$data['foreign_amount'] = null; |
237
|
|
|
$data['foreign_currency_id'] = null; |
238
|
|
|
|
239
|
|
|
// which account to check for what the native currency is? |
240
|
|
|
$check = 'source'; |
241
|
|
|
if (TransactionType::DEPOSIT === $transactionType->type) { |
242
|
|
|
$check = 'destination'; |
243
|
|
|
} |
244
|
|
|
switch ($transactionType->type) { |
245
|
|
|
case TransactionType::RECONCILIATION: |
246
|
|
|
// do nothing. |
247
|
|
|
break; |
248
|
|
|
case TransactionType::DEPOSIT: |
249
|
|
|
case TransactionType::WITHDRAWAL: |
250
|
|
|
// continue: |
251
|
|
|
$nativeCurrencyId = intval($accounts[$check]->getMeta('currency_id')); |
252
|
|
|
if ($nativeCurrencyId === 0) { |
253
|
|
|
// fall back to given ID (not everybody upgrades nicely). |
254
|
|
|
$nativeCurrencyId = $submittedCurrencyId; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
// does not match? Then user has submitted amount in a foreign currency: |
258
|
|
|
if ($nativeCurrencyId !== $submittedCurrencyId) { |
259
|
|
|
// store amount and submitted currency in "foreign currency" fields: |
260
|
|
|
$data['foreign_amount'] = $data['amount']; |
261
|
|
|
$data['foreign_currency_id'] = $submittedCurrencyId; |
262
|
|
|
|
263
|
|
|
// overrule the amount and currency ID fields to be the original again: |
264
|
|
|
$data['amount'] = strval($data['native_amount']); |
265
|
|
|
$data['currency_id'] = $nativeCurrencyId; |
266
|
|
|
} |
267
|
|
|
break; |
268
|
|
|
case TransactionType::TRANSFER: |
269
|
|
|
$sourceCurrencyId = intval($accounts['source']->getMeta('currency_id')); |
270
|
|
|
$destinationCurrencyId = intval($accounts['destination']->getMeta('currency_id')); |
271
|
|
|
$data['amount'] = strval($data['source_amount']); |
272
|
|
|
$data['currency_id'] = intval($accounts['source']->getMeta('currency_id')); |
273
|
|
|
|
274
|
|
|
if ($sourceCurrencyId !== $destinationCurrencyId) { |
275
|
|
|
// accounts have different id's, save this info: |
276
|
|
|
$data['foreign_amount'] = strval($data['destination_amount']); |
277
|
|
|
$data['foreign_currency_id'] = $destinationCurrencyId; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
break; |
281
|
|
|
default: |
282
|
|
|
throw new FireflyException(sprintf('Cannot handle %s in verifyNativeAmount()', $transactionType->type)); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
return $data; |
286
|
|
|
} |
287
|
|
|
} |
288
|
|
|
|