| 
									
										
										
										
											2018-02-11 08:08:08 +01:00
										 |  |  | <?php | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * AccountTransformer.php | 
					
						
							|  |  |  |  * Copyright (c) 2018 thegrumpydictator@gmail.com | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This file is part of Firefly III. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Firefly III is free software: you can redistribute it and/or modify | 
					
						
							|  |  |  |  * it under the terms of the GNU General Public License as published by | 
					
						
							|  |  |  |  * the Free Software Foundation, either version 3 of the License, or | 
					
						
							|  |  |  |  * (at your option) any later version. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Firefly III is distributed in the hope that it will be useful, | 
					
						
							|  |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  |  * GNU General Public License for more details. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  |  * along with Firefly III. If not, see <http://www.gnu.org/licenses/>. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | declare(strict_types=1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Transformers; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-13 18:24:06 +01:00
										 |  |  | use Carbon\Carbon; | 
					
						
							| 
									
										
										
										
											2018-02-18 10:31:15 +01:00
										 |  |  | use FireflyIII\Helpers\Collector\JournalCollectorInterface; | 
					
						
							| 
									
										
										
										
											2018-02-11 08:08:08 +01:00
										 |  |  | use FireflyIII\Models\Account; | 
					
						
							| 
									
										
										
										
											2018-02-13 21:04:15 +01:00
										 |  |  | use FireflyIII\Models\AccountType; | 
					
						
							| 
									
										
										
										
											2018-02-11 08:08:08 +01:00
										 |  |  | use FireflyIII\Models\TransactionCurrency; | 
					
						
							| 
									
										
										
										
											2018-02-16 22:14:34 +01:00
										 |  |  | use FireflyIII\Repositories\Account\AccountRepositoryInterface; | 
					
						
							| 
									
										
										
										
											2018-02-13 21:04:15 +01:00
										 |  |  | use Illuminate\Support\Collection; | 
					
						
							| 
									
										
										
										
											2018-02-11 20:45:33 +01:00
										 |  |  | use League\Fractal\Resource\Collection as FractalCollection; | 
					
						
							|  |  |  | use League\Fractal\Resource\Item; | 
					
						
							| 
									
										
										
										
											2018-02-11 08:08:08 +01:00
										 |  |  | use League\Fractal\TransformerAbstract; | 
					
						
							| 
									
										
										
										
											2018-02-11 20:45:33 +01:00
										 |  |  | use Symfony\Component\HttpFoundation\ParameterBag; | 
					
						
							| 
									
										
										
										
											2018-02-11 08:08:08 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Class AccountTransformer | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class AccountTransformer extends TransformerAbstract | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-03-29 19:01:47 +02:00
										 |  |  |     /** @noinspection ClassOverridesFieldOfSuperClassInspection */ | 
					
						
							| 
									
										
										
										
											2018-02-11 20:45:33 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * List of resources possible to include. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @var array | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2018-02-13 21:04:15 +01:00
										 |  |  |     protected $availableIncludes = ['transactions', 'piggy_banks', 'user']; | 
					
						
							| 
									
										
										
										
											2018-02-11 20:45:33 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * List of resources to automatically include | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @var array | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2018-02-13 18:24:06 +01:00
										 |  |  |     protected $defaultIncludes = []; | 
					
						
							| 
									
										
										
										
											2018-02-11 20:45:33 +01:00
										 |  |  |     /** @var ParameterBag */ | 
					
						
							|  |  |  |     protected $parameters; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-19 19:39:02 +01:00
										 |  |  |     /** @var AccountRepositoryInterface */ | 
					
						
							|  |  |  |     protected $repository; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-11 20:45:33 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2018-02-16 22:14:34 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      * AccountTransformer constructor. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @codeCoverageIgnore | 
					
						
							| 
									
										
										
										
											2018-02-11 20:45:33 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @param ParameterBag $parameters | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function __construct(ParameterBag $parameters) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-03-19 19:39:02 +01:00
										 |  |  |         $this->repository = app(AccountRepositoryInterface::class); | 
					
						
							| 
									
										
										
										
											2018-02-11 20:45:33 +01:00
										 |  |  |         $this->parameters = $parameters; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2018-02-16 22:14:34 +01:00
										 |  |  |      * Include piggy banks into end result. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @codeCoverageIgnore | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param Account $account | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return FractalCollection | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function includePiggyBanks(Account $account): FractalCollection | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $piggies = $account->piggyBanks()->get(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $this->collection($piggies, new PiggyBankTransformer($this->parameters), 'piggy_banks'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Include transactions into end result. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @codeCoverageIgnore | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2018-02-11 20:45:33 +01:00
										 |  |  |      * @param Account $account | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return FractalCollection | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2018-02-13 21:04:15 +01:00
										 |  |  |     public function includeTransactions(Account $account): FractalCollection | 
					
						
							| 
									
										
										
										
											2018-02-11 20:45:33 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-04-02 14:50:17 +02:00
										 |  |  |         $pageSize = (int)app('preferences')->getForUser($account->user, 'listPageSize', 50)->data; | 
					
						
							| 
									
										
										
										
											2018-02-13 21:04:15 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // journals always use collector and limited using URL parameters.
 | 
					
						
							| 
									
										
										
										
											2018-02-18 10:31:15 +01:00
										 |  |  |         $collector = app(JournalCollectorInterface::class); | 
					
						
							| 
									
										
										
										
											2018-02-13 21:04:15 +01:00
										 |  |  |         $collector->setUser($account->user); | 
					
						
							|  |  |  |         $collector->withOpposingAccount()->withCategoryInformation()->withBudgetInformation(); | 
					
						
							|  |  |  |         if ($account->accountType->type === AccountType::ASSET) { | 
					
						
							|  |  |  |             $collector->setAccounts(new Collection([$account])); | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             $collector->setOpposingAccounts(new Collection([$account])); | 
					
						
							| 
									
										
										
										
											2018-02-11 20:45:33 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-04-02 14:50:17 +02:00
										 |  |  |         if (null !== $this->parameters->get('start') && null !== $this->parameters->get('end')) { | 
					
						
							| 
									
										
										
										
											2018-02-13 21:04:15 +01:00
										 |  |  |             $collector->setRange($this->parameters->get('start'), $this->parameters->get('end')); | 
					
						
							| 
									
										
										
										
											2018-02-11 20:45:33 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-02-13 21:04:15 +01:00
										 |  |  |         $collector->setLimit($pageSize)->setPage($this->parameters->get('page')); | 
					
						
							|  |  |  |         $journals = $collector->getJournals(); | 
					
						
							| 
									
										
										
										
											2018-02-11 20:45:33 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-13 21:04:15 +01:00
										 |  |  |         return $this->collection($journals, new TransactionTransformer($this->parameters), 'transactions'); | 
					
						
							| 
									
										
										
										
											2018-02-11 20:45:33 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2018-02-16 22:14:34 +01:00
										 |  |  |      * Include user data in end result. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @codeCoverageIgnore | 
					
						
							| 
									
										
										
										
											2018-02-11 20:45:33 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @param Account $account | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return Item | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function includeUser(Account $account): Item | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->item($account->user, new UserTransformer($this->parameters), 'user'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-11 08:08:08 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2018-02-17 10:47:06 +01:00
										 |  |  |      * Transform the account. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2018-02-11 08:08:08 +01:00
										 |  |  |      * @param Account $account | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return array | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function transform(Account $account): array | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-03-19 19:39:02 +01:00
										 |  |  |         $this->repository->setUser($account->user); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-16 22:14:34 +01:00
										 |  |  |         $type = $account->accountType->type; | 
					
						
							| 
									
										
										
										
											2018-03-19 19:39:02 +01:00
										 |  |  |         $role = $this->repository->getMetaValue($account, 'accountRole'); | 
					
						
							| 
									
										
										
										
											2018-04-14 09:38:58 +02:00
										 |  |  |         if ($type !== AccountType::ASSET || (string)$role === '') { | 
					
						
							| 
									
										
										
										
											2018-02-11 08:08:08 +01:00
										 |  |  |             $role = null; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-04-21 23:48:54 +02:00
										 |  |  |         $currencyId     = (int)$this->repository->getMetaValue($account, 'currency_id'); | 
					
						
							|  |  |  |         $currencyCode   = null; | 
					
						
							| 
									
										
										
										
											2018-06-24 06:51:22 +02:00
										 |  |  |         $currencySymbol = null; | 
					
						
							| 
									
										
										
										
											2018-04-21 23:48:54 +02:00
										 |  |  |         $decimalPlaces  = 2; | 
					
						
							| 
									
										
										
										
											2018-02-11 08:08:08 +01:00
										 |  |  |         if ($currencyId > 0) { | 
					
						
							| 
									
										
										
										
											2018-04-21 23:48:54 +02:00
										 |  |  |             $currency       = TransactionCurrency::find($currencyId); | 
					
						
							|  |  |  |             $currencyCode   = $currency->code; | 
					
						
							|  |  |  |             $decimalPlaces  = $currency->decimal_places; | 
					
						
							|  |  |  |             $currencySymbol = $currency->symbol; | 
					
						
							| 
									
										
										
										
											2018-02-13 18:24:06 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $date = new Carbon; | 
					
						
							| 
									
										
										
										
											2018-04-02 14:50:17 +02:00
										 |  |  |         if (null !== $this->parameters->get('date')) { | 
					
						
							| 
									
										
										
										
											2018-02-13 18:24:06 +01:00
										 |  |  |             $date = $this->parameters->get('date'); | 
					
						
							| 
									
										
										
										
											2018-02-11 08:08:08 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ($currencyId === 0) { | 
					
						
							|  |  |  |             $currencyId = null; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-16 22:14:34 +01:00
										 |  |  |         $monthlyPaymentDate = null; | 
					
						
							|  |  |  |         $creditCardType     = null; | 
					
						
							|  |  |  |         if ($role === 'ccAsset' && $type === AccountType::ASSET) { | 
					
						
							| 
									
										
										
										
											2018-03-19 19:39:02 +01:00
										 |  |  |             $creditCardType     = $this->repository->getMetaValue($account, 'ccType'); | 
					
						
							|  |  |  |             $monthlyPaymentDate = $this->repository->getMetaValue($account, 'ccMonthlyPaymentDate'); | 
					
						
							| 
									
										
										
										
											2018-02-16 22:14:34 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $openingBalance     = null; | 
					
						
							|  |  |  |         $openingBalanceDate = null; | 
					
						
							|  |  |  |         if ($type === AccountType::ASSET) { | 
					
						
							|  |  |  |             /** @var AccountRepositoryInterface $repository */ | 
					
						
							|  |  |  |             $repository = app(AccountRepositoryInterface::class); | 
					
						
							| 
									
										
										
										
											2018-03-29 19:01:47 +02:00
										 |  |  |             $repository->setUser($account->user); | 
					
						
							| 
									
										
										
										
											2018-02-16 22:14:34 +01:00
										 |  |  |             $amount             = $repository->getOpeningBalanceAmount($account); | 
					
						
							| 
									
										
										
										
											2018-04-02 14:50:17 +02:00
										 |  |  |             $openingBalance     = null === $amount ? null : round($amount, $decimalPlaces); | 
					
						
							| 
									
										
										
										
											2018-02-16 22:14:34 +01:00
										 |  |  |             $openingBalanceDate = $repository->getOpeningBalanceDate($account); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-11 08:08:08 +01:00
										 |  |  |         $data = [ | 
					
						
							| 
									
										
										
										
											2018-02-11 20:45:33 +01:00
										 |  |  |             'id'                   => (int)$account->id, | 
					
						
							|  |  |  |             'updated_at'           => $account->updated_at->toAtomString(), | 
					
						
							|  |  |  |             'created_at'           => $account->created_at->toAtomString(), | 
					
						
							|  |  |  |             'name'                 => $account->name, | 
					
						
							| 
									
										
										
										
											2018-04-02 14:50:17 +02:00
										 |  |  |             'active'               => (int)$account->active === 1, | 
					
						
							| 
									
										
										
										
											2018-02-16 22:14:34 +01:00
										 |  |  |             'type'                 => $type, | 
					
						
							| 
									
										
										
										
											2018-02-11 20:45:33 +01:00
										 |  |  |             'currency_id'          => $currencyId, | 
					
						
							|  |  |  |             'currency_code'        => $currencyCode, | 
					
						
							| 
									
										
										
										
											2018-04-21 23:48:54 +02:00
										 |  |  |             'currency_symbol'      => $currencySymbol, | 
					
						
							|  |  |  |             'currency_dp'          => $decimalPlaces, | 
					
						
							| 
									
										
										
										
											2018-02-13 18:24:06 +01:00
										 |  |  |             'current_balance'      => round(app('steam')->balance($account, $date), $decimalPlaces), | 
					
						
							|  |  |  |             'current_balance_date' => $date->format('Y-m-d'), | 
					
						
							| 
									
										
										
										
											2018-03-23 05:31:45 +01:00
										 |  |  |             'notes'                => $this->repository->getNoteText($account), | 
					
						
							| 
									
										
										
										
											2018-02-16 22:14:34 +01:00
										 |  |  |             'monthly_payment_date' => $monthlyPaymentDate, | 
					
						
							|  |  |  |             'credit_card_type'     => $creditCardType, | 
					
						
							| 
									
										
										
										
											2018-03-19 19:39:02 +01:00
										 |  |  |             'account_number'       => $this->repository->getMetaValue($account, 'accountNumber'), | 
					
						
							| 
									
										
										
										
											2018-02-11 20:45:33 +01:00
										 |  |  |             'iban'                 => $account->iban, | 
					
						
							| 
									
										
										
										
											2018-03-19 19:39:02 +01:00
										 |  |  |             'bic'                  => $this->repository->getMetaValue($account, 'BIC'), | 
					
						
							| 
									
										
										
										
											2018-02-13 18:24:06 +01:00
										 |  |  |             'virtual_balance'      => round($account->virtual_balance, $decimalPlaces), | 
					
						
							| 
									
										
										
										
											2018-02-16 22:14:34 +01:00
										 |  |  |             'opening_balance'      => $openingBalance, | 
					
						
							|  |  |  |             'opening_balance_date' => $openingBalanceDate, | 
					
						
							| 
									
										
										
										
											2018-02-11 20:45:33 +01:00
										 |  |  |             'role'                 => $role, | 
					
						
							|  |  |  |             'links'                => [ | 
					
						
							| 
									
										
										
										
											2018-02-11 08:08:08 +01:00
										 |  |  |                 [ | 
					
						
							|  |  |  |                     'rel' => 'self', | 
					
						
							|  |  |  |                     'uri' => '/accounts/' . $account->id, | 
					
						
							|  |  |  |                 ], | 
					
						
							|  |  |  |             ], | 
					
						
							|  |  |  |         ]; | 
					
						
							| 
									
										
										
										
											2018-02-13 18:24:06 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-11 08:08:08 +01:00
										 |  |  |         return $data; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-03-05 19:35:58 +01:00
										 |  |  | } |