1 | <?php |
||
2 | /** |
||
3 | * TransactionTransformer.php |
||
4 | * Copyright (c) 2018 [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 | |||
22 | declare(strict_types=1); |
||
23 | |||
24 | namespace FireflyIII\Transformers; |
||
25 | |||
26 | |||
27 | use FireflyIII\Exceptions\FireflyException; |
||
28 | use FireflyIII\Models\Note; |
||
29 | use FireflyIII\Models\Transaction; |
||
30 | use FireflyIII\Models\TransactionType; |
||
31 | use League\Fractal\Resource\Collection as FractalCollection; |
||
32 | use League\Fractal\Resource\Item; |
||
33 | use League\Fractal\TransformerAbstract; |
||
34 | use Symfony\Component\HttpFoundation\ParameterBag; |
||
35 | |||
36 | /** |
||
37 | * Class TransactionTransformer |
||
38 | */ |
||
39 | class TransactionTransformer extends TransformerAbstract |
||
40 | { |
||
41 | /** |
||
42 | * List of resources possible to include |
||
43 | * |
||
44 | * @var array |
||
45 | */ |
||
46 | protected $availableIncludes = ['attachments', 'user', 'tags', 'journal_meta', 'piggy_bank_events']; |
||
47 | /** |
||
48 | * List of resources to automatically include |
||
49 | * |
||
50 | * @var array |
||
51 | */ |
||
52 | protected $defaultIncludes = []; |
||
53 | |||
54 | /** @var ParameterBag */ |
||
55 | protected $parameters; |
||
56 | |||
57 | /** |
||
58 | * TransactionTransformer constructor. |
||
59 | * |
||
60 | * @codeCoverageIgnore |
||
61 | * |
||
62 | * @param ParameterBag $parameters |
||
63 | */ |
||
64 | public function __construct(ParameterBag $parameters) |
||
65 | { |
||
66 | $this->parameters = $parameters; |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * Include attachments. |
||
71 | * |
||
72 | * @codeCoverageIgnore |
||
73 | * |
||
74 | * @param Transaction $transaction |
||
75 | * |
||
76 | * @return FractalCollection |
||
77 | */ |
||
78 | public function includeAttachments(Transaction $transaction): FractalCollection |
||
79 | { |
||
80 | return $this->collection($transaction->transactionJournal->attachments, new AttachmentTransformer($this->parameters), 'attachments'); |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * Include meta data |
||
85 | * |
||
86 | * @codeCoverageIgnore |
||
87 | * |
||
88 | * @param Transaction $transaction |
||
89 | * |
||
90 | * @return FractalCollection |
||
91 | */ |
||
92 | public function includeJournalMeta(Transaction $transaction): FractalCollection |
||
93 | { |
||
94 | $meta = $transaction->transactionJournal->transactionJournalMeta()->get(); |
||
95 | |||
96 | return $this->collection($meta, new JournalMetaTransformer($this->parameters), 'journal_meta'); |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * Include piggy bank events |
||
101 | * |
||
102 | * @codeCoverageIgnore |
||
103 | * |
||
104 | * @param Transaction $transaction |
||
105 | * |
||
106 | * @return FractalCollection |
||
107 | */ |
||
108 | public function includePiggyBankEvents(Transaction $transaction): FractalCollection |
||
109 | { |
||
110 | $events = $transaction->transactionJournal->piggyBankEvents()->get(); |
||
111 | |||
112 | return $this->collection($events, new PiggyBankEventTransformer($this->parameters), 'piggy_bank_events'); |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * Include tags |
||
117 | * |
||
118 | * @codeCoverageIgnore |
||
119 | * |
||
120 | * @param Transaction $transaction |
||
121 | * |
||
122 | * @return FractalCollection |
||
123 | */ |
||
124 | public function includeTags(Transaction $transaction): FractalCollection |
||
125 | { |
||
126 | $set = $transaction->transactionJournal->tags; |
||
127 | |||
128 | return $this->collection($set, new TagTransformer($this->parameters), 'tags'); |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * Include the user. |
||
133 | * |
||
134 | * @codeCoverageIgnore |
||
135 | * |
||
136 | * @param Transaction $transaction |
||
137 | * |
||
138 | * @return \League\Fractal\Resource\Item |
||
139 | */ |
||
140 | public function includeUser(Transaction $transaction): Item |
||
141 | { |
||
142 | return $this->item($transaction->transactionJournal->user, new UserTransformer($this->parameters), 'users'); |
||
143 | } |
||
144 | |||
145 | |||
146 | /** |
||
147 | * Transform the journal. |
||
148 | * |
||
149 | * @param Transaction $transaction |
||
150 | * |
||
151 | * @return array |
||
152 | * @throws FireflyException |
||
153 | */ |
||
154 | public function transform(Transaction $transaction): array |
||
155 | { |
||
156 | $categoryId = null; |
||
157 | $categoryName = null; |
||
158 | $budgetId = null; |
||
159 | $budgetName = null; |
||
160 | $categoryId = is_null($transaction->transaction_category_id) ? $transaction->transaction_journal_category_id |
||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
161 | : $transaction->transaction_category_id; |
||
162 | $categoryName = is_null($transaction->transaction_category_name) ? $transaction->transaction_journal_category_name |
||
0 ignored issues
–
show
|
|||
163 | : $transaction->transaction_category_name; |
||
164 | |||
165 | if ($transaction->transaction_type_type === TransactionType::WITHDRAWAL) { |
||
166 | $budgetId = is_null($transaction->transaction_budget_id) ? $transaction->transaction_journal_budget_id |
||
0 ignored issues
–
show
|
|||
167 | : $transaction->transaction_budget_id; |
||
168 | $budgetName = is_null($transaction->transaction_budget_name) ? $transaction->transaction_journal_budget_name |
||
0 ignored issues
–
show
|
|||
169 | : $transaction->transaction_budget_name; |
||
170 | } |
||
171 | /** @var Note $dbNote */ |
||
172 | $dbNote = $transaction->transactionJournal->notes()->first(); |
||
173 | $notes = null; |
||
174 | if (!is_null($dbNote)) { |
||
175 | $notes = $dbNote->text; |
||
176 | } |
||
177 | |||
178 | $data = [ |
||
179 | 'id' => (int)$transaction->id, |
||
180 | 'updated_at' => $transaction->updated_at->toAtomString(), |
||
181 | 'created_at' => $transaction->created_at->toAtomString(), |
||
182 | 'description' => $transaction->description, |
||
183 | 'transaction_description' => $transaction->transaction_description, |
||
184 | 'date' => $transaction->date->format('Y-m-d'), |
||
185 | 'type' => $transaction->transaction_type_type, |
||
186 | 'identifier' => $transaction->identifier, |
||
187 | 'journal_id' => (int)$transaction->journal_id, |
||
188 | 'reconciled' => (bool)$transaction->reconciled, |
||
189 | 'amount' => round($transaction->transaction_amount, intval($transaction->transaction_currency_dp)), |
||
190 | 'currency_id' => $transaction->transaction_currency_id, |
||
191 | 'currency_code' => $transaction->transaction_currency_code, |
||
192 | 'currency_symbol' => $transaction->transaction_currency_symbol, |
||
193 | 'currency_dp' => $transaction->transaction_currency_dp, |
||
194 | 'foreign_amount' => null, |
||
195 | 'foreign_currency_id' => $transaction->foreign_currency_id, |
||
196 | 'foreign_currency_code' => $transaction->foreign_currency_code, |
||
197 | 'foreign_currency_symbol' => $transaction->foreign_currency_symbol, |
||
198 | 'foreign_currency_dp' => $transaction->foreign_currency_dp, |
||
199 | 'bill_id' => $transaction->bill_id, |
||
200 | 'bill_name' => $transaction->bill_name, |
||
201 | 'category_id' => $categoryId, |
||
202 | 'category_name' => $categoryName, |
||
203 | 'budget_id' => $budgetId, |
||
204 | 'budget_name' => $budgetName, |
||
205 | 'notes' => $notes, |
||
206 | 'links' => [ |
||
207 | [ |
||
208 | 'rel' => 'self', |
||
209 | 'uri' => '/transactions/' . $transaction->id, |
||
210 | ], |
||
211 | ], |
||
212 | ]; |
||
213 | |||
214 | // expand foreign amount: |
||
215 | if (!is_null($transaction->transaction_foreign_amount)) { |
||
0 ignored issues
–
show
|
|||
216 | $data['foreign_amount'] = round($transaction->transaction_foreign_amount, intval($transaction->foreign_currency_dp)); |
||
217 | } |
||
218 | |||
219 | // switch on type for consistency |
||
220 | switch ($transaction->transaction_type_type) { |
||
221 | case TransactionType::WITHDRAWAL: |
||
222 | $data['source_id'] = $transaction->account_id; |
||
223 | $data['source_name'] = $transaction->account_name; |
||
224 | $data['source_iban'] = $transaction->account_iban; |
||
225 | $data['source_type'] = $transaction->account_type; |
||
226 | $data['destination_id'] = $transaction->opposing_account_id; |
||
227 | $data['destination_name'] = $transaction->opposing_account_name; |
||
228 | $data['destination_iban'] = $transaction->opposing_account_iban; |
||
229 | $data['destination_type'] = $transaction->opposing_account_type; |
||
230 | break; |
||
231 | case TransactionType::DEPOSIT: |
||
232 | case TransactionType::TRANSFER: |
||
233 | case TransactionType::OPENING_BALANCE: |
||
234 | case TransactionType::RECONCILIATION: |
||
235 | $data['source_id'] = $transaction->opposing_account_id; |
||
236 | $data['source_name'] = $transaction->opposing_account_name; |
||
237 | $data['source_iban'] = $transaction->opposing_account_iban; |
||
238 | $data['source_type'] = $transaction->opposing_account_type; |
||
239 | $data['destination_id'] = $transaction->account_id; |
||
240 | $data['destination_name'] = $transaction->account_name; |
||
241 | $data['destination_iban'] = $transaction->account_iban; |
||
242 | $data['destination_type'] = $transaction->account_type; |
||
243 | break; |
||
244 | default: |
||
245 | // @codeCoverageIgnoreStart |
||
246 | throw new FireflyException( |
||
247 | sprintf('Transaction transformer cannot handle transactions of type "%s"!', $transaction->transaction_type_type) |
||
248 | ); |
||
249 | // @codeCoverageIgnoreEnd |
||
250 | |||
251 | } |
||
252 | |||
253 | // expand description. |
||
254 | if (strlen(strval($transaction->transaction_description)) > 0) { |
||
255 | $data['description'] = $transaction->transaction_description . ' (' . $transaction->description . ')'; |
||
256 | } |
||
257 | |||
258 | |||
259 | return $data; |
||
260 | } |
||
261 | } |
||
262 |