1 | <?php |
||
2 | /** |
||
3 | * Transaction.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 Illuminate\Database\Eloquent\Builder; |
||
27 | use Illuminate\Database\Eloquent\Model; |
||
28 | use Illuminate\Database\Eloquent\SoftDeletes; |
||
29 | use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
||
30 | use Watson\Validating\ValidatingTrait; |
||
31 | |||
32 | /** |
||
33 | * Class Transaction. |
||
34 | * |
||
35 | * @property int $journal_id |
||
36 | * @property Carbon $date |
||
37 | * @property string $transaction_description |
||
38 | * @property string $transaction_amount |
||
39 | * @property string $transaction_foreign_amount |
||
40 | * @property string $transaction_type_type |
||
41 | * @property string $foreign_currency_symbol |
||
42 | * @property int $foreign_currency_dp |
||
43 | * @property int $account_id |
||
44 | * @property string $account_name |
||
45 | * @property string $account_iban |
||
46 | * @property string $account_number |
||
47 | * @property string $account_bic |
||
48 | * @property string $account_currency_code |
||
49 | * @property int $opposing_account_id |
||
50 | * @property string $opposing_account_name |
||
51 | * @property string $opposing_account_iban |
||
52 | * @property string $opposing_account_number |
||
53 | * @property string $opposing_account_bic |
||
54 | * @property string $opposing_currency_code |
||
55 | * @property int $transaction_budget_id |
||
56 | * @property string $transaction_budget_name |
||
57 | * @property int $transaction_journal_budget_id |
||
58 | * @property string $transaction_journal_budget_name |
||
59 | * @property int $transaction_category_id |
||
60 | * @property string $transaction_category_name |
||
61 | * @property int $transaction_journal_category_id |
||
62 | * @property string $transaction_journal_category_name |
||
63 | * @property int $bill_id |
||
64 | * @property string $bill_name |
||
65 | * @property string $notes |
||
66 | * @property string $tags |
||
67 | * @property string $transaction_currency_symbol |
||
68 | * @property int $transaction_currency_dp |
||
69 | * @property string $transaction_currency_code |
||
70 | * @property string $description |
||
71 | * @property bool $is_split |
||
72 | * @property int $attachmentCount |
||
73 | */ |
||
74 | class Transaction extends Model |
||
75 | { |
||
76 | /** |
||
77 | * The attributes that should be casted to native types. |
||
78 | * |
||
79 | * @var array |
||
80 | */ |
||
81 | protected $casts |
||
82 | = [ |
||
83 | 'created_at' => 'datetime', |
||
84 | 'updated_at' => 'datetime', |
||
85 | 'deleted_at' => 'datetime', |
||
86 | 'identifier' => 'int', |
||
87 | 'encrypted' => 'boolean', // model does not have these fields though |
||
88 | 'bill_name_encrypted' => 'boolean', |
||
89 | 'reconciled' => 'boolean', |
||
90 | ]; |
||
91 | /** |
||
92 | * @var array |
||
93 | */ |
||
94 | protected $fillable |
||
95 | = ['account_id', 'transaction_journal_id', 'description', 'amount', 'identifier', 'transaction_currency_id', 'foreign_currency_id', |
||
96 | 'foreign_amount', 'reconciled']; |
||
97 | /** |
||
98 | * @var array |
||
99 | */ |
||
100 | protected $hidden = ['encrypted']; |
||
101 | /** |
||
102 | * @var array |
||
103 | */ |
||
104 | protected $rules |
||
105 | = [ |
||
106 | 'account_id' => 'required|exists:accounts,id', |
||
107 | 'transaction_journal_id' => 'required|exists:transaction_journals,id', |
||
108 | 'transaction_currency_id' => 'required|exists:transaction_currencies,id', |
||
109 | 'description' => 'between:0,1024', |
||
110 | 'amount' => 'required|numeric', |
||
111 | ]; |
||
112 | |||
113 | /** |
||
114 | * @codeCoverageIgnore |
||
115 | * |
||
116 | * @param Builder $query |
||
117 | * @param string $table |
||
118 | * |
||
119 | * @return bool |
||
120 | */ |
||
121 | public static function isJoined(Builder $query, string $table): bool |
||
122 | { |
||
123 | $joins = $query->getQuery()->joins; |
||
124 | if (null === $joins) { |
||
125 | return false; |
||
126 | } |
||
127 | foreach ($joins as $join) { |
||
128 | if ($join->table === $table) { |
||
129 | return true; |
||
130 | } |
||
131 | } |
||
132 | |||
133 | return false; |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * @param string $value |
||
138 | * |
||
139 | * @return Transaction |
||
140 | * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
||
141 | */ |
||
142 | public static function routeBinder(string $value): Transaction |
||
143 | { |
||
144 | if (auth()->check()) { |
||
145 | $transactionId = intval($value); |
||
146 | $transaction = auth()->user()->transactions()->where('transactions.id', $transactionId) |
||
147 | ->first(['transactions.*']); |
||
148 | if (!is_null($transaction)) { |
||
149 | return $transaction; |
||
150 | } |
||
151 | } |
||
152 | |||
153 | throw new NotFoundHttpException; |
||
154 | } |
||
155 | |||
156 | use SoftDeletes, ValidatingTrait; |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
157 | |||
158 | /** |
||
159 | * @codeCoverageIgnore |
||
160 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
161 | */ |
||
162 | public function account() |
||
163 | { |
||
164 | return $this->belongsTo('FireflyIII\Models\Account'); |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * @codeCoverageIgnore |
||
169 | * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
||
170 | */ |
||
171 | public function budgets() |
||
172 | { |
||
173 | return $this->belongsToMany('FireflyIII\Models\Budget'); |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * @codeCoverageIgnore |
||
178 | * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
||
179 | */ |
||
180 | public function categories() |
||
181 | { |
||
182 | return $this->belongsToMany('FireflyIII\Models\Category'); |
||
183 | } |
||
184 | |||
185 | /** |
||
186 | * @codeCoverageIgnore |
||
187 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
188 | */ |
||
189 | public function foreignCurrency() |
||
190 | { |
||
191 | return $this->belongsTo('FireflyIII\Models\TransactionCurrency', 'foreign_currency_id'); |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * @codeCoverageIgnore |
||
196 | * |
||
197 | * @param $value |
||
198 | * |
||
199 | * @return float|int |
||
200 | */ |
||
201 | public function getAmountAttribute($value) |
||
202 | { |
||
203 | return $value; |
||
204 | } |
||
205 | |||
206 | /** |
||
207 | * @codeCoverageIgnore |
||
208 | * |
||
209 | * @param Builder $query |
||
210 | * @param Carbon $date |
||
211 | */ |
||
212 | public function scopeAfter(Builder $query, Carbon $date) |
||
213 | { |
||
214 | if (!self::isJoined($query, 'transaction_journals')) { |
||
215 | $query->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id'); |
||
216 | } |
||
217 | $query->where('transaction_journals.date', '>=', $date->format('Y-m-d 00:00:00')); |
||
218 | } |
||
219 | |||
220 | /** |
||
221 | * @codeCoverageIgnore |
||
222 | * |
||
223 | * @param Builder $query |
||
224 | * @param Carbon $date |
||
225 | */ |
||
226 | public function scopeBefore(Builder $query, Carbon $date) |
||
227 | { |
||
228 | if (!self::isJoined($query, 'transaction_journals')) { |
||
229 | $query->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id'); |
||
230 | } |
||
231 | $query->where('transaction_journals.date', '<=', $date->format('Y-m-d 23:59:59')); |
||
232 | } |
||
233 | |||
234 | /** |
||
235 | * @codeCoverageIgnore |
||
236 | * |
||
237 | * @param Builder $query |
||
238 | * @param array $types |
||
239 | */ |
||
240 | public function scopeTransactionTypes(Builder $query, array $types) |
||
241 | { |
||
242 | if (!self::isJoined($query, 'transaction_journals')) { |
||
243 | $query->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id'); |
||
244 | } |
||
245 | |||
246 | if (!self::isJoined($query, 'transaction_types')) { |
||
247 | $query->leftJoin('transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id'); |
||
248 | } |
||
249 | $query->whereIn('transaction_types.type', $types); |
||
250 | } |
||
251 | |||
252 | /** |
||
253 | * @codeCoverageIgnore |
||
254 | * |
||
255 | * @param $value |
||
256 | */ |
||
257 | public function setAmountAttribute($value) |
||
258 | { |
||
259 | $this->attributes['amount'] = strval($value); |
||
260 | } |
||
261 | |||
262 | /** |
||
263 | * @codeCoverageIgnore |
||
264 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
265 | */ |
||
266 | public function transactionCurrency() |
||
267 | { |
||
268 | return $this->belongsTo('FireflyIII\Models\TransactionCurrency'); |
||
269 | } |
||
270 | |||
271 | /** |
||
272 | * @codeCoverageIgnore |
||
273 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
274 | */ |
||
275 | public function transactionJournal() |
||
276 | { |
||
277 | return $this->belongsTo('FireflyIII\Models\TransactionJournal'); |
||
278 | } |
||
279 | } |
||
280 |