1 | <?php |
||
2 | /** |
||
3 | * TransactionJournal.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\Models; |
||
24 | |||
25 | use Carbon\Carbon; |
||
26 | use Crypt; |
||
27 | use FireflyIII\Support\CacheProperties; |
||
28 | use FireflyIII\Support\Models\TransactionJournalTrait; |
||
29 | use FireflyIII\User; |
||
30 | use Illuminate\Database\Eloquent\Builder as EloquentBuilder; |
||
31 | use Illuminate\Database\Eloquent\Model; |
||
32 | use Illuminate\Database\Eloquent\Relations\BelongsToMany; |
||
33 | use Illuminate\Database\Eloquent\Relations\HasMany; |
||
34 | use Illuminate\Database\Eloquent\SoftDeletes; |
||
35 | use Log; |
||
36 | use Preferences; |
||
37 | use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
||
38 | use Watson\Validating\ValidatingTrait; |
||
39 | |||
40 | /** |
||
41 | * Class TransactionJournal. |
||
42 | * |
||
43 | * @property User $user |
||
44 | */ |
||
45 | class TransactionJournal extends Model |
||
46 | { |
||
47 | use SoftDeletes, ValidatingTrait, TransactionJournalTrait; |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
48 | |||
49 | /** |
||
50 | * The attributes that should be casted to native types. |
||
51 | * |
||
52 | * @var array |
||
53 | */ |
||
54 | protected $casts |
||
55 | = [ |
||
56 | 'created_at' => 'datetime', |
||
57 | 'updated_at' => 'datetime', |
||
58 | 'deleted_at' => 'datetime', |
||
59 | 'date' => 'date', |
||
60 | 'interest_date' => 'date', |
||
61 | 'book_date' => 'date', |
||
62 | 'process_date' => 'date', |
||
63 | 'order' => 'int', |
||
64 | 'tag_count' => 'int', |
||
65 | 'encrypted' => 'boolean', |
||
66 | 'completed' => 'boolean', |
||
67 | ]; |
||
68 | |||
69 | /** @var array */ |
||
70 | protected $fillable |
||
71 | = ['user_id', 'transaction_type_id', 'bill_id', 'interest_date', 'book_date', 'process_date', |
||
72 | 'transaction_currency_id', 'description', 'completed', |
||
73 | 'date', 'rent_date', 'encrypted', 'tag_count',]; |
||
74 | /** @var array */ |
||
75 | protected $hidden = ['encrypted']; |
||
76 | /** @var array */ |
||
77 | protected $rules |
||
78 | = [ |
||
79 | 'user_id' => 'required|exists:users,id', |
||
80 | 'transaction_type_id' => 'required|exists:transaction_types,id', |
||
81 | 'description' => 'required|between:1,1024', |
||
82 | 'completed' => 'required|boolean', |
||
83 | 'date' => 'required|date', |
||
84 | 'encrypted' => 'required|boolean', |
||
85 | ]; |
||
86 | |||
87 | /** |
||
88 | * @param string $value |
||
89 | * |
||
90 | * @return TransactionJournal |
||
91 | * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
||
92 | */ |
||
93 | public static function routeBinder(string $value): TransactionJournal |
||
94 | { |
||
95 | if (auth()->check()) { |
||
96 | $journalId = intval($value); |
||
97 | $journal = auth()->user()->transactionJournals()->where('transaction_journals.id', $journalId) |
||
98 | ->first(['transaction_journals.*']); |
||
99 | if (!is_null($journal)) { |
||
100 | return $journal; |
||
101 | } |
||
102 | } |
||
103 | |||
104 | throw new NotFoundHttpException; |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * @codeCoverageIgnore |
||
109 | * @return \Illuminate\Database\Eloquent\Relations\MorphMany |
||
110 | */ |
||
111 | public function attachments() |
||
112 | { |
||
113 | return $this->morphMany('FireflyIII\Models\Attachment', 'attachable'); |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * @codeCoverageIgnore |
||
118 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
119 | */ |
||
120 | public function bill() |
||
121 | { |
||
122 | return $this->belongsTo('FireflyIII\Models\Bill'); |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * @codeCoverageIgnore |
||
127 | * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
||
128 | */ |
||
129 | public function budgets(): BelongsToMany |
||
130 | { |
||
131 | return $this->belongsToMany('FireflyIII\Models\Budget'); |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * @codeCoverageIgnore |
||
136 | * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
||
137 | */ |
||
138 | public function categories(): BelongsToMany |
||
139 | { |
||
140 | return $this->belongsToMany('FireflyIII\Models\Category'); |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * @codeCoverageIgnore |
||
145 | * @deprecated |
||
146 | * @param string $name |
||
147 | * |
||
148 | * @return bool |
||
149 | */ |
||
150 | public function deleteMeta(string $name): bool |
||
151 | { |
||
152 | $this->transactionJournalMeta()->where('name', $name)->delete(); |
||
153 | |||
154 | return true; |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * @codeCoverageIgnore |
||
159 | * |
||
160 | * @return HasMany |
||
161 | */ |
||
162 | public function destinationJournalLinks(): HasMany |
||
163 | { |
||
164 | return $this->hasMany(TransactionJournalLink::class, 'destination_id'); |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * @codeCoverageIgnore |
||
169 | * |
||
170 | * @param $value |
||
171 | * |
||
172 | * @return string |
||
173 | * @throws \Illuminate\Contracts\Encryption\DecryptException |
||
174 | */ |
||
175 | public function getDescriptionAttribute($value) |
||
176 | { |
||
177 | if ($this->encrypted) { |
||
178 | return Crypt::decrypt($value); |
||
179 | } |
||
180 | |||
181 | return $value; |
||
182 | } |
||
183 | |||
184 | /** |
||
185 | * |
||
186 | * @param string $name |
||
187 | * |
||
188 | * @deprecated |
||
189 | * @return string |
||
190 | */ |
||
191 | public function getMeta(string $name) |
||
192 | { |
||
193 | $value = null; |
||
194 | $cache = new CacheProperties; |
||
195 | $cache->addProperty('journal-meta'); |
||
196 | $cache->addProperty($this->id); |
||
197 | $cache->addProperty($name); |
||
198 | |||
199 | if ($cache->has()) { |
||
200 | return $cache->get(); // @codeCoverageIgnore |
||
201 | } |
||
202 | |||
203 | Log::debug(sprintf('Looking for journal #%d meta field "%s".', $this->id, $name)); |
||
204 | $entry = $this->transactionJournalMeta()->where('name', $name)->first(); |
||
205 | if (null !== $entry) { |
||
206 | $value = $entry->data; |
||
207 | // cache: |
||
208 | $cache->store($value); |
||
209 | } |
||
210 | |||
211 | // convert to Carbon if name is _date |
||
212 | if (null !== $value && '_date' === substr($name, -5)) { |
||
213 | $value = new Carbon($value); |
||
214 | // cache: |
||
215 | $cache->store($value); |
||
216 | } |
||
217 | |||
218 | return $value; |
||
219 | } |
||
220 | |||
221 | /** |
||
222 | * @codeCoverageIgnore |
||
223 | * |
||
224 | * @param string $name |
||
225 | * @deprecated |
||
226 | * @return bool |
||
227 | */ |
||
228 | public function hasMeta(string $name): bool |
||
229 | { |
||
230 | return null !== $this->getMeta($name); |
||
231 | } |
||
232 | |||
233 | /** |
||
234 | * @codeCoverageIgnore |
||
235 | * @return bool |
||
236 | */ |
||
237 | public function isDeposit(): bool |
||
238 | { |
||
239 | if (null !== $this->transaction_type_type) { |
||
240 | return TransactionType::DEPOSIT === $this->transaction_type_type; |
||
241 | } |
||
242 | |||
243 | return $this->transactionType->isDeposit(); |
||
244 | } |
||
245 | |||
246 | /** |
||
247 | * @codeCoverageIgnore |
||
248 | * @return bool |
||
249 | */ |
||
250 | public function isOpeningBalance(): bool |
||
251 | { |
||
252 | if (null !== $this->transaction_type_type) { |
||
253 | return TransactionType::OPENING_BALANCE === $this->transaction_type_type; |
||
254 | } |
||
255 | |||
256 | return $this->transactionType->isOpeningBalance(); |
||
257 | } |
||
258 | |||
259 | /** |
||
260 | * @codeCoverageIgnore |
||
261 | * @return bool |
||
262 | */ |
||
263 | public function isTransfer(): bool |
||
264 | { |
||
265 | if (null !== $this->transaction_type_type) { |
||
266 | return TransactionType::TRANSFER === $this->transaction_type_type; |
||
267 | } |
||
268 | |||
269 | return $this->transactionType->isTransfer(); |
||
270 | } |
||
271 | |||
272 | /** |
||
273 | * @codeCoverageIgnore |
||
274 | * @return bool |
||
275 | */ |
||
276 | public function isWithdrawal(): bool |
||
277 | { |
||
278 | if (null !== $this->transaction_type_type) { |
||
279 | return TransactionType::WITHDRAWAL === $this->transaction_type_type; |
||
280 | } |
||
281 | |||
282 | return $this->transactionType->isWithdrawal(); |
||
283 | } |
||
284 | |||
285 | /** |
||
286 | * @codeCoverageIgnore |
||
287 | * Get all of the notes. |
||
288 | */ |
||
289 | public function notes() |
||
290 | { |
||
291 | return $this->morphMany('FireflyIII\Models\Note', 'noteable'); |
||
292 | } |
||
293 | |||
294 | /** |
||
295 | * @codeCoverageIgnore |
||
296 | * @return \Illuminate\Database\Eloquent\Relations\HasMany |
||
297 | */ |
||
298 | public function piggyBankEvents(): HasMany |
||
299 | { |
||
300 | return $this->hasMany('FireflyIII\Models\PiggyBankEvent'); |
||
301 | } |
||
302 | |||
303 | /** |
||
304 | * @codeCoverageIgnore |
||
305 | * |
||
306 | * @param EloquentBuilder $query |
||
307 | * @param Carbon $date |
||
308 | * |
||
309 | * @return EloquentBuilder |
||
310 | */ |
||
311 | public function scopeAfter(EloquentBuilder $query, Carbon $date) |
||
312 | { |
||
313 | return $query->where('transaction_journals.date', '>=', $date->format('Y-m-d 00:00:00')); |
||
314 | } |
||
315 | |||
316 | /** |
||
317 | * @codeCoverageIgnore |
||
318 | * |
||
319 | * @param EloquentBuilder $query |
||
320 | * @param Carbon $date |
||
321 | * |
||
322 | * @return EloquentBuilder |
||
323 | */ |
||
324 | public function scopeBefore(EloquentBuilder $query, Carbon $date) |
||
325 | { |
||
326 | return $query->where('transaction_journals.date', '<=', $date->format('Y-m-d 00:00:00')); |
||
327 | } |
||
328 | |||
329 | /** |
||
330 | * @codeCoverageIgnore |
||
331 | * |
||
332 | * @param EloquentBuilder $query |
||
333 | * @param array $types |
||
334 | */ |
||
335 | public function scopeTransactionTypes(EloquentBuilder $query, array $types) |
||
336 | { |
||
337 | if (!self::isJoined($query, 'transaction_types')) { |
||
338 | $query->leftJoin('transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id'); |
||
339 | } |
||
340 | if (count($types) > 0) { |
||
341 | $query->whereIn('transaction_types.type', $types); |
||
342 | } |
||
343 | } |
||
344 | |||
345 | /** |
||
346 | * @codeCoverageIgnore |
||
347 | * |
||
348 | * @param $value |
||
349 | * |
||
350 | * @throws \Illuminate\Contracts\Encryption\EncryptException |
||
351 | */ |
||
352 | public function setDescriptionAttribute($value) |
||
353 | { |
||
354 | $encrypt = config('firefly.encryption'); |
||
355 | $this->attributes['description'] = $encrypt ? Crypt::encrypt($value) : $value; |
||
356 | $this->attributes['encrypted'] = $encrypt; |
||
357 | } |
||
358 | |||
359 | /** |
||
360 | * @deprecated |
||
361 | * |
||
362 | * @param string $name |
||
363 | * @param $value |
||
364 | * |
||
365 | * @return TransactionJournalMeta |
||
366 | */ |
||
367 | public function setMeta(string $name, $value): TransactionJournalMeta |
||
368 | { |
||
369 | if (null === $value) { |
||
370 | $this->deleteMeta($name); |
||
371 | |||
372 | return new TransactionJournalMeta(); |
||
373 | } |
||
374 | if (is_string($value) && 0 === strlen($value)) { |
||
375 | $this->deleteMeta($name); |
||
376 | |||
377 | return new TransactionJournalMeta(); |
||
378 | } |
||
379 | |||
380 | if ($value instanceof Carbon) { |
||
381 | $value = $value->toW3cString(); |
||
382 | } |
||
383 | |||
384 | Log::debug(sprintf('Going to set "%s" with value "%s"', $name, json_encode($value))); |
||
385 | $entry = $this->transactionJournalMeta()->where('name', $name)->first(); |
||
386 | if (null === $entry) { |
||
387 | $entry = new TransactionJournalMeta(); |
||
388 | $entry->transactionJournal()->associate($this); |
||
389 | $entry->name = $name; |
||
390 | } |
||
391 | $entry->data = $value; |
||
392 | $entry->save(); |
||
393 | Preferences::mark(); |
||
394 | |||
395 | return $entry; |
||
396 | } |
||
397 | |||
398 | /** |
||
399 | * @codeCoverageIgnore |
||
400 | * @return HasMany |
||
401 | */ |
||
402 | public function sourceJournalLinks(): HasMany |
||
403 | { |
||
404 | return $this->hasMany(TransactionJournalLink::class, 'source_id'); |
||
405 | } |
||
406 | |||
407 | /** |
||
408 | * @codeCoverageIgnore |
||
409 | * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
||
410 | */ |
||
411 | public function tags() |
||
412 | { |
||
413 | return $this->belongsToMany('FireflyIII\Models\Tag'); |
||
414 | } |
||
415 | |||
416 | /** |
||
417 | * @codeCoverageIgnore |
||
418 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
419 | */ |
||
420 | public function transactionCurrency() |
||
421 | { |
||
422 | return $this->belongsTo('FireflyIII\Models\TransactionCurrency'); |
||
423 | } |
||
424 | |||
425 | /** |
||
426 | * @codeCoverageIgnore |
||
427 | * @return HasMany |
||
428 | */ |
||
429 | public function transactionJournalMeta(): HasMany |
||
430 | { |
||
431 | return $this->hasMany('FireflyIII\Models\TransactionJournalMeta'); |
||
432 | } |
||
433 | |||
434 | /** |
||
435 | * @codeCoverageIgnore |
||
436 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
437 | */ |
||
438 | public function transactionType() |
||
439 | { |
||
440 | return $this->belongsTo('FireflyIII\Models\TransactionType'); |
||
441 | } |
||
442 | |||
443 | /** |
||
444 | * @codeCoverageIgnore |
||
445 | * @return HasMany |
||
446 | */ |
||
447 | public function transactions(): HasMany |
||
448 | { |
||
449 | return $this->hasMany('FireflyIII\Models\Transaction'); |
||
450 | } |
||
451 | |||
452 | /** |
||
453 | * @codeCoverageIgnore |
||
454 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
455 | */ |
||
456 | public function user() |
||
457 | { |
||
458 | return $this->belongsTo('FireflyIII\User'); |
||
459 | } |
||
460 | } |
||
461 |