| 
									
										
										
										
											2021-02-16 09:52:39 +01:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2024-11-25 04:18:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-16 09:52:39 +01:00
										 |  |  | /* | 
					
						
							|  |  |  |  * ShowController.php | 
					
						
							|  |  |  |  * Copyright (c) 2021 james@firefly-iii.org | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This file is part of Firefly III (https://github.com/firefly-iii). | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is free software: you can redistribute it and/or modify | 
					
						
							|  |  |  |  * it under the terms of the GNU Affero General Public License as | 
					
						
							|  |  |  |  * published by the Free Software Foundation, either version 3 of the | 
					
						
							|  |  |  |  * License, or (at your option) any later version. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program 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 Affero General Public License for more details. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * You should have received a copy of the GNU Affero General Public License | 
					
						
							|  |  |  |  * along with this program.  If not, see <https://www.gnu.org/licenses/>. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-28 11:39:26 +02:00
										 |  |  | declare(strict_types=1); | 
					
						
							| 
									
										
										
										
											2021-02-16 09:52:39 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-28 11:39:26 +02:00
										 |  |  | namespace FireflyIII\Api\V1\Controllers\Models\Account; | 
					
						
							| 
									
										
										
										
											2021-02-16 09:52:39 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | use FireflyIII\Api\V1\Controllers\Controller; | 
					
						
							| 
									
										
										
										
											2025-08-31 19:20:02 +02:00
										 |  |  | use FireflyIII\Api\V1\Requests\Models\Account\ShowRequest; | 
					
						
							| 
									
										
										
										
											2021-02-16 09:52:39 +01:00
										 |  |  | use FireflyIII\Models\Account; | 
					
						
							|  |  |  | use FireflyIII\Repositories\Account\AccountRepositoryInterface; | 
					
						
							|  |  |  | use FireflyIII\Support\Http\Api\AccountFilter; | 
					
						
							| 
									
										
										
										
											2025-02-15 16:51:13 +01:00
										 |  |  | use FireflyIII\Support\JsonApi\Enrichments\AccountEnrichment; | 
					
						
							| 
									
										
										
										
											2021-02-16 09:52:39 +01:00
										 |  |  | use FireflyIII\Transformers\AccountTransformer; | 
					
						
							| 
									
										
										
										
											2025-02-15 16:51:13 +01:00
										 |  |  | use FireflyIII\User; | 
					
						
							| 
									
										
										
										
											2021-02-16 09:52:39 +01:00
										 |  |  | use Illuminate\Http\JsonResponse; | 
					
						
							|  |  |  | use Illuminate\Pagination\LengthAwarePaginator; | 
					
						
							|  |  |  | use League\Fractal\Pagination\IlluminatePaginatorAdapter; | 
					
						
							|  |  |  | use League\Fractal\Resource\Collection as FractalCollection; | 
					
						
							|  |  |  | use League\Fractal\Resource\Item; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Class ShowController | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class ShowController extends Controller | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     use AccountFilter; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-02 12:56:48 +01:00
										 |  |  |     public const string RESOURCE_KEY = 'accounts'; | 
					
						
							| 
									
										
										
										
											2021-02-16 09:52:39 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     private AccountRepositoryInterface $repository; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * AccountController constructor. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function __construct() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         parent::__construct(); | 
					
						
							|  |  |  |         $this->middleware( | 
					
						
							|  |  |  |             function ($request, $next) { | 
					
						
							|  |  |  |                 $this->repository = app(AccountRepositoryInterface::class); | 
					
						
							|  |  |  |                 $this->repository->setUser(auth()->user()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 return $next($request); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Display a listing of the resource. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2025-08-31 19:20:02 +02:00
										 |  |  |     public function index(ShowRequest $request): JsonResponse | 
					
						
							| 
									
										
										
										
											2021-02-16 09:52:39 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2025-09-01 05:32:14 +02:00
										 |  |  |         $manager     = $this->getManager(); | 
					
						
							|  |  |  |         $params      = $request->getParameters(); | 
					
						
							| 
									
										
										
										
											2025-08-31 19:20:02 +02:00
										 |  |  |         $this->parameters->set('type', $params['type']); | 
					
						
							| 
									
										
										
										
											2021-02-16 09:52:39 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // types to get, page size:
 | 
					
						
							| 
									
										
										
										
											2025-09-01 05:32:14 +02:00
										 |  |  |         $types       = $this->mapAccountTypes($params['type']); | 
					
						
							| 
									
										
										
										
											2021-02-16 09:52:39 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // get list of accounts. Count it and split it.
 | 
					
						
							| 
									
										
										
										
											2021-03-14 06:20:23 +01:00
										 |  |  |         $this->repository->resetAccountOrder(); | 
					
						
							| 
									
										
										
										
											2025-09-03 20:34:28 +02:00
										 |  |  |         $collection  = $this->repository->getAccountsByType($types, $params['sort']); | 
					
						
							| 
									
										
										
										
											2025-09-01 05:32:14 +02:00
										 |  |  |         $count       = $collection->count(); | 
					
						
							| 
									
										
										
										
											2021-05-24 08:06:56 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // continue sort:
 | 
					
						
							| 
									
										
										
										
											2025-09-03 20:34:28 +02:00
										 |  |  |         // TODO if the user sorts on DB dependent field there must be no slice before enrichment, only after.
 | 
					
						
							|  |  |  |         // TODO still need to figure out how to do this easily.
 | 
					
						
							| 
									
										
										
										
											2025-09-01 05:32:14 +02:00
										 |  |  |         $accounts    = $collection->slice(($this->parameters->get('page') - 1) * $params['limit'], $params['limit']); | 
					
						
							| 
									
										
										
										
											2021-02-16 09:52:39 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-10-06 20:43:47 +02:00
										 |  |  |         // #11007 go to the end of the previous day.
 | 
					
						
							| 
									
										
										
										
											2025-10-07 09:49:35 +02:00
										 |  |  |         $this->parameters->set('start', $this->parameters->get('start')?->subSecond()); | 
					
						
							| 
									
										
										
										
											2025-10-07 10:06:54 +02:00
										 |  |  |         // #11018 also end of the day.
 | 
					
						
							|  |  |  |         $this->parameters->set('end', $this->parameters->get('end')?->endOfDay()); | 
					
						
							| 
									
										
										
										
											2025-10-06 20:43:47 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-15 16:51:13 +01:00
										 |  |  |         // enrich
 | 
					
						
							|  |  |  |         /** @var User $admin */ | 
					
						
							| 
									
										
										
										
											2025-09-01 05:32:14 +02:00
										 |  |  |         $admin       = auth()->user(); | 
					
						
							|  |  |  |         $enrichment  = new AccountEnrichment(); | 
					
						
							| 
									
										
										
										
											2025-09-03 20:34:28 +02:00
										 |  |  |         $enrichment->setSort($params['sort']); | 
					
						
							| 
									
										
										
										
											2025-08-03 10:22:12 +02:00
										 |  |  |         $enrichment->setDate($this->parameters->get('date')); | 
					
						
							| 
									
										
										
										
											2025-09-01 20:39:40 +02:00
										 |  |  |         $enrichment->setStart($this->parameters->get('start')); | 
					
						
							|  |  |  |         $enrichment->setEnd($this->parameters->get('end')); | 
					
						
							| 
									
										
										
										
											2025-02-15 16:51:13 +01:00
										 |  |  |         $enrichment->setUser($admin); | 
					
						
							| 
									
										
										
										
											2025-09-01 05:32:14 +02:00
										 |  |  |         $accounts    = $enrichment->enrich($accounts); | 
					
						
							| 
									
										
										
										
											2025-02-15 16:51:13 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-16 09:52:39 +01:00
										 |  |  |         // make paginator:
 | 
					
						
							| 
									
										
										
										
											2025-09-01 05:32:14 +02:00
										 |  |  |         $paginator   = new LengthAwarePaginator($accounts, $count, $params['limit'], $this->parameters->get('page')); | 
					
						
							|  |  |  |         $paginator->setPath(route('api.v1.accounts.index').$this->buildParams()); | 
					
						
							| 
									
										
										
										
											2021-02-16 09:52:39 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         /** @var AccountTransformer $transformer */ | 
					
						
							|  |  |  |         $transformer = app(AccountTransformer::class); | 
					
						
							|  |  |  |         $transformer->setParameters($this->parameters); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-09-01 05:32:14 +02:00
										 |  |  |         $resource    = new FractalCollection($accounts, $transformer, self::RESOURCE_KEY); | 
					
						
							| 
									
										
										
										
											2021-02-16 09:52:39 +01:00
										 |  |  |         $resource->setPaginator(new IlluminatePaginatorAdapter($paginator)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2021-09-18 10:07:45 +02:00
										 |  |  |      * This endpoint is documented at: | 
					
						
							| 
									
										
										
										
											2023-02-12 06:53:36 +01:00
										 |  |  |      * https://api-docs.firefly-iii.org/?urls.primaryName=2.0.0%20(v1)#/accounts/getAccount
 | 
					
						
							| 
									
										
										
										
											2021-09-18 10:07:45 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2021-02-16 09:52:39 +01:00
										 |  |  |      * Show single instance. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2025-09-01 20:39:40 +02:00
										 |  |  |     public function show(ShowRequest $request, Account $account): JsonResponse | 
					
						
							| 
									
										
										
										
											2021-02-16 09:52:39 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-03-13 14:33:48 +01:00
										 |  |  |         // get list of accounts. Count it and split it.
 | 
					
						
							| 
									
										
										
										
											2021-03-14 06:20:23 +01:00
										 |  |  |         $this->repository->resetAccountOrder(); | 
					
						
							| 
									
										
										
										
											2021-03-13 14:33:48 +01:00
										 |  |  |         $account->refresh(); | 
					
						
							| 
									
										
										
										
											2025-09-01 05:32:14 +02:00
										 |  |  |         $manager     = $this->getManager(); | 
					
						
							| 
									
										
										
										
											2021-02-16 09:52:39 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-10-06 20:43:47 +02:00
										 |  |  |         // #11007 go to the end of the previous day.
 | 
					
						
							| 
									
										
										
										
											2025-10-07 09:49:35 +02:00
										 |  |  |         $this->parameters->set('start', $this->parameters->get('start')?->subSecond()); | 
					
						
							| 
									
										
										
										
											2025-10-07 10:06:54 +02:00
										 |  |  |         // #11018 also end of the day.
 | 
					
						
							|  |  |  |         $this->parameters->set('end', $this->parameters->get('end')?->endOfDay()); | 
					
						
							| 
									
										
										
										
											2025-10-06 20:43:47 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-15 16:51:13 +01:00
										 |  |  |         // enrich
 | 
					
						
							|  |  |  |         /** @var User $admin */ | 
					
						
							| 
									
										
										
										
											2025-09-01 05:32:14 +02:00
										 |  |  |         $admin       = auth()->user(); | 
					
						
							|  |  |  |         $enrichment  = new AccountEnrichment(); | 
					
						
							| 
									
										
										
										
											2025-08-03 10:22:12 +02:00
										 |  |  |         $enrichment->setDate($this->parameters->get('date')); | 
					
						
							| 
									
										
										
										
											2025-09-01 20:39:40 +02:00
										 |  |  |         $enrichment->setStart($this->parameters->get('start')); | 
					
						
							|  |  |  |         $enrichment->setEnd($this->parameters->get('end')); | 
					
						
							| 
									
										
										
										
											2025-02-15 16:51:13 +01:00
										 |  |  |         $enrichment->setUser($admin); | 
					
						
							| 
									
										
										
										
											2025-09-01 05:32:14 +02:00
										 |  |  |         $account     = $enrichment->enrichSingle($account); | 
					
						
							| 
									
										
										
										
											2025-02-15 16:51:13 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-16 09:52:39 +01:00
										 |  |  |         /** @var AccountTransformer $transformer */ | 
					
						
							|  |  |  |         $transformer = app(AccountTransformer::class); | 
					
						
							|  |  |  |         $transformer->setParameters($this->parameters); | 
					
						
							| 
									
										
										
										
											2025-09-01 05:32:14 +02:00
										 |  |  |         $resource    = new Item($account, $transformer, self::RESOURCE_KEY); | 
					
						
							| 
									
										
										
										
											2021-02-16 09:52:39 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-03-28 11:39:26 +02:00
										 |  |  | } |