1 | <?php |
||
2 | /** |
||
3 | * Bill.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 Crypt; |
||
26 | use Illuminate\Database\Eloquent\Model; |
||
27 | use Illuminate\Database\Eloquent\Relations\BelongsTo; |
||
28 | use Illuminate\Database\Eloquent\Relations\HasMany; |
||
29 | use Illuminate\Database\Eloquent\SoftDeletes; |
||
30 | use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
||
31 | use Watson\Validating\ValidatingTrait; |
||
32 | |||
33 | /** |
||
34 | * Class Bill. |
||
35 | */ |
||
36 | class Bill extends Model |
||
37 | { |
||
38 | use SoftDeletes, ValidatingTrait; |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
39 | /** |
||
40 | * The attributes that should be casted to native types. |
||
41 | * |
||
42 | * @var array |
||
43 | */ |
||
44 | protected $casts |
||
45 | = [ |
||
46 | 'created_at' => 'datetime', |
||
47 | 'updated_at' => 'datetime', |
||
48 | 'deleted_at' => 'datetime', |
||
49 | 'date' => 'date', |
||
50 | 'skip' => 'int', |
||
51 | 'automatch' => 'boolean', |
||
52 | 'active' => 'boolean', |
||
53 | 'name_encrypted' => 'boolean', |
||
54 | 'match_encrypted' => 'boolean', |
||
55 | ]; |
||
56 | /** |
||
57 | * @var array |
||
58 | */ |
||
59 | protected $fillable |
||
60 | = ['name', 'match', 'amount_min', 'match_encrypted', 'name_encrypted', 'user_id', 'amount_max', 'date', 'repeat_freq', 'skip', |
||
61 | 'automatch', 'active',]; |
||
62 | /** |
||
63 | * @var array |
||
64 | */ |
||
65 | protected $hidden = ['amount_min_encrypted', 'amount_max_encrypted', 'name_encrypted', 'match_encrypted']; |
||
66 | /** |
||
67 | * @var array |
||
68 | */ |
||
69 | protected $rules = ['name' => 'required|between:1,200']; |
||
70 | |||
71 | /** |
||
72 | * @param string $value |
||
73 | * |
||
74 | * @return Bill |
||
75 | * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
||
76 | */ |
||
77 | public static function routeBinder(string $value): Bill |
||
78 | { |
||
79 | if (auth()->check()) { |
||
80 | $billId = intval($value); |
||
81 | $bill = auth()->user()->bills()->find($billId); |
||
82 | if (!is_null($bill)) { |
||
83 | return $bill; |
||
84 | } |
||
85 | } |
||
86 | throw new NotFoundHttpException; |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * @codeCoverageIgnore |
||
91 | * @return \Illuminate\Database\Eloquent\Relations\MorphMany |
||
92 | */ |
||
93 | public function attachments() |
||
94 | { |
||
95 | return $this->morphMany('FireflyIII\Models\Attachment', 'attachable'); |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * @codeCoverageIgnore |
||
100 | * |
||
101 | * @param $value |
||
102 | * |
||
103 | * @return string |
||
104 | * @throws \Illuminate\Contracts\Encryption\DecryptException |
||
105 | */ |
||
106 | public function getMatchAttribute($value) |
||
107 | { |
||
108 | if (1 === intval($this->match_encrypted)) { |
||
0 ignored issues
–
show
|
|||
109 | return Crypt::decrypt($value); |
||
110 | } |
||
111 | |||
112 | return $value; |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * @codeCoverageIgnore |
||
117 | * |
||
118 | * @param $value |
||
119 | * |
||
120 | * @return string |
||
121 | * @throws \Illuminate\Contracts\Encryption\DecryptException |
||
122 | */ |
||
123 | public function getNameAttribute($value) |
||
124 | { |
||
125 | if (1 === intval($this->name_encrypted)) { |
||
0 ignored issues
–
show
|
|||
126 | return Crypt::decrypt($value); |
||
127 | } |
||
128 | |||
129 | return $value; |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * @codeCoverageIgnore |
||
134 | * Get all of the notes. |
||
135 | */ |
||
136 | public function notes() |
||
137 | { |
||
138 | return $this->morphMany(Note::class, 'noteable'); |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * @codeCoverageIgnore |
||
143 | * |
||
144 | * @param $value |
||
145 | */ |
||
146 | public function setAmountMaxAttribute($value) |
||
147 | { |
||
148 | $this->attributes['amount_max'] = strval($value); |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * @param $value |
||
153 | * |
||
154 | * @codeCoverageIgnore |
||
155 | */ |
||
156 | public function setAmountMinAttribute($value) |
||
157 | { |
||
158 | $this->attributes['amount_min'] = strval($value); |
||
159 | } |
||
160 | |||
161 | /** |
||
162 | * @param $value |
||
163 | * |
||
164 | * @codeCoverageIgnore |
||
165 | * @throws \Illuminate\Contracts\Encryption\EncryptException |
||
166 | */ |
||
167 | public function setMatchAttribute($value) |
||
168 | { |
||
169 | $encrypt = config('firefly.encryption'); |
||
170 | $this->attributes['match'] = $encrypt ? Crypt::encrypt($value) : $value; |
||
171 | $this->attributes['match_encrypted'] = $encrypt; |
||
172 | } |
||
173 | |||
174 | /** |
||
175 | * @param $value |
||
176 | * |
||
177 | * @codeCoverageIgnore |
||
178 | * @throws \Illuminate\Contracts\Encryption\EncryptException |
||
179 | */ |
||
180 | public function setNameAttribute($value) |
||
181 | { |
||
182 | $encrypt = config('firefly.encryption'); |
||
183 | $this->attributes['name'] = $encrypt ? Crypt::encrypt($value) : $value; |
||
184 | $this->attributes['name_encrypted'] = $encrypt; |
||
185 | } |
||
186 | |||
187 | /** |
||
188 | * @codeCoverageIgnore |
||
189 | * @return HasMany |
||
190 | */ |
||
191 | public function transactionJournals(): HasMany |
||
192 | { |
||
193 | return $this->hasMany('FireflyIII\Models\TransactionJournal'); |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | * @codeCoverageIgnore |
||
198 | * @return BelongsTo |
||
199 | */ |
||
200 | public function user(): BelongsTo |
||
201 | { |
||
202 | return $this->belongsTo('FireflyIII\User'); |
||
203 | } |
||
204 | } |
||
205 |