1 | <?php |
||
2 | /** |
||
3 | * AccountTransformer.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 Carbon\Carbon; |
||
28 | use FireflyIII\Helpers\Collector\JournalCollectorInterface; |
||
29 | use FireflyIII\Models\Account; |
||
30 | use FireflyIII\Models\AccountType; |
||
31 | use FireflyIII\Models\TransactionCurrency; |
||
32 | use FireflyIII\Repositories\Account\AccountRepositoryInterface; |
||
33 | use Illuminate\Support\Collection; |
||
34 | use League\Fractal\Resource\Collection as FractalCollection; |
||
35 | use League\Fractal\Resource\Item; |
||
36 | use League\Fractal\TransformerAbstract; |
||
37 | use Symfony\Component\HttpFoundation\ParameterBag; |
||
38 | |||
39 | /** |
||
40 | * Class AccountTransformer |
||
41 | */ |
||
42 | class AccountTransformer extends TransformerAbstract |
||
43 | { |
||
44 | /** @noinspection ClassOverridesFieldOfSuperClassInspection */ |
||
45 | /** |
||
46 | * List of resources possible to include. |
||
47 | * |
||
48 | * @var array |
||
49 | */ |
||
50 | protected $availableIncludes = ['transactions', 'piggy_banks', 'user']; |
||
51 | /** |
||
52 | * List of resources to automatically include |
||
53 | * |
||
54 | * @var array |
||
55 | */ |
||
56 | protected $defaultIncludes = []; |
||
57 | /** @var ParameterBag */ |
||
58 | protected $parameters; |
||
59 | |||
60 | /** @var AccountRepositoryInterface */ |
||
61 | protected $repository; |
||
62 | |||
63 | /** |
||
64 | * |
||
65 | * AccountTransformer constructor. |
||
66 | * |
||
67 | * @codeCoverageIgnore |
||
68 | * |
||
69 | * @param ParameterBag $parameters |
||
70 | */ |
||
71 | public function __construct(ParameterBag $parameters) |
||
72 | { |
||
73 | $this->repository = app(AccountRepositoryInterface::class); |
||
74 | $this->parameters = $parameters; |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * Include piggy banks into end result. |
||
79 | * |
||
80 | * @codeCoverageIgnore |
||
81 | * |
||
82 | * @param Account $account |
||
83 | * |
||
84 | * @return FractalCollection |
||
85 | */ |
||
86 | public function includePiggyBanks(Account $account): FractalCollection |
||
87 | { |
||
88 | $piggies = $account->piggyBanks()->get(); |
||
89 | |||
90 | return $this->collection($piggies, new PiggyBankTransformer($this->parameters), 'piggy_banks'); |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * Include transactions into end result. |
||
95 | * |
||
96 | * @codeCoverageIgnore |
||
97 | * |
||
98 | * @param Account $account |
||
99 | * |
||
100 | * @return FractalCollection |
||
101 | */ |
||
102 | public function includeTransactions(Account $account): FractalCollection |
||
103 | { |
||
104 | $pageSize = intval(app('preferences')->getForUser($account->user, 'listPageSize', 50)->data); |
||
105 | |||
106 | // journals always use collector and limited using URL parameters. |
||
107 | $collector = app(JournalCollectorInterface::class); |
||
108 | $collector->setUser($account->user); |
||
109 | $collector->withOpposingAccount()->withCategoryInformation()->withBudgetInformation(); |
||
110 | if ($account->accountType->type === AccountType::ASSET) { |
||
111 | $collector->setAccounts(new Collection([$account])); |
||
112 | } else { |
||
113 | $collector->setOpposingAccounts(new Collection([$account])); |
||
114 | } |
||
115 | if (!is_null($this->parameters->get('start')) && !is_null($this->parameters->get('end'))) { |
||
116 | $collector->setRange($this->parameters->get('start'), $this->parameters->get('end')); |
||
117 | } |
||
118 | $collector->setLimit($pageSize)->setPage($this->parameters->get('page')); |
||
119 | $journals = $collector->getJournals(); |
||
120 | |||
121 | return $this->collection($journals, new TransactionTransformer($this->parameters), 'transactions'); |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * Include user data in end result. |
||
126 | * |
||
127 | * @codeCoverageIgnore |
||
128 | * |
||
129 | * @param Account $account |
||
130 | * |
||
131 | * @return Item |
||
132 | */ |
||
133 | public function includeUser(Account $account): Item |
||
134 | { |
||
135 | return $this->item($account->user, new UserTransformer($this->parameters), 'user'); |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * Transform the account. |
||
140 | * |
||
141 | * @param Account $account |
||
142 | * |
||
143 | * @return array |
||
144 | */ |
||
145 | public function transform(Account $account): array |
||
146 | { |
||
147 | $this->repository->setUser($account->user); |
||
148 | |||
149 | $type = $account->accountType->type; |
||
150 | $role = $this->repository->getMetaValue($account, 'accountRole'); |
||
151 | if (strlen($role) === 0 || $type !== AccountType::ASSET) { |
||
152 | $role = null; |
||
153 | } |
||
154 | $currencyId = intval($this->repository->getMetaValue($account, 'currency_id')); |
||
155 | $currencyCode = null; |
||
156 | $decimalPlaces = 2; |
||
157 | if ($currencyId > 0) { |
||
158 | $currency = TransactionCurrency::find($currencyId); |
||
159 | $currencyCode = $currency->code; |
||
160 | $decimalPlaces = $currency->decimal_places; |
||
161 | } |
||
162 | |||
163 | $date = new Carbon; |
||
164 | if (!is_null($this->parameters->get('date'))) { |
||
165 | $date = $this->parameters->get('date'); |
||
166 | } |
||
167 | |||
168 | if ($currencyId === 0) { |
||
169 | $currencyId = null; |
||
170 | } |
||
171 | |||
172 | $monthlyPaymentDate = null; |
||
173 | $creditCardType = null; |
||
174 | if ($role === 'ccAsset' && $type === AccountType::ASSET) { |
||
175 | $creditCardType = $this->repository->getMetaValue($account, 'ccType'); |
||
176 | $monthlyPaymentDate = $this->repository->getMetaValue($account, 'ccMonthlyPaymentDate'); |
||
177 | } |
||
178 | |||
179 | $openingBalance = null; |
||
180 | $openingBalanceDate = null; |
||
181 | if ($type === AccountType::ASSET) { |
||
182 | /** @var AccountRepositoryInterface $repository */ |
||
183 | $repository = app(AccountRepositoryInterface::class); |
||
184 | $repository->setUser($account->user); |
||
185 | $amount = $repository->getOpeningBalanceAmount($account); |
||
186 | $openingBalance = is_null($amount) ? null : round($amount, $decimalPlaces); |
||
187 | $openingBalanceDate = $repository->getOpeningBalanceDate($account); |
||
188 | } |
||
189 | |||
190 | $data = [ |
||
191 | 'id' => (int)$account->id, |
||
192 | 'updated_at' => $account->updated_at->toAtomString(), |
||
193 | 'created_at' => $account->created_at->toAtomString(), |
||
194 | 'name' => $account->name, |
||
195 | 'active' => intval($account->active) === 1, |
||
0 ignored issues
–
show
|
|||
196 | 'type' => $type, |
||
197 | 'currency_id' => $currencyId, |
||
198 | 'currency_code' => $currencyCode, |
||
199 | 'current_balance' => round(app('steam')->balance($account, $date), $decimalPlaces), |
||
200 | 'current_balance_date' => $date->format('Y-m-d'), |
||
201 | 'notes' => $this->repository->getNoteText($account), |
||
202 | 'monthly_payment_date' => $monthlyPaymentDate, |
||
203 | 'credit_card_type' => $creditCardType, |
||
204 | 'account_number' => $this->repository->getMetaValue($account, 'accountNumber'), |
||
205 | 'iban' => $account->iban, |
||
206 | 'bic' => $this->repository->getMetaValue($account, 'BIC'), |
||
207 | 'virtual_balance' => round($account->virtual_balance, $decimalPlaces), |
||
208 | 'opening_balance' => $openingBalance, |
||
209 | 'opening_balance_date' => $openingBalanceDate, |
||
210 | 'role' => $role, |
||
211 | 'links' => [ |
||
212 | [ |
||
213 | 'rel' => 'self', |
||
214 | 'uri' => '/accounts/' . $account->id, |
||
215 | ], |
||
216 | ], |
||
217 | ]; |
||
218 | |||
219 | return $data; |
||
220 | } |
||
221 | } |
||
222 |
Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.