| 
									
										
										
										
											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\Transaction; | 
					
						
							| 
									
										
										
										
											2021-02-16 09:52:39 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | use FireflyIII\Api\V1\Controllers\Controller; | 
					
						
							| 
									
										
										
										
											2021-09-18 10:20:19 +02:00
										 |  |  | use FireflyIII\Exceptions\FireflyException; | 
					
						
							| 
									
										
										
										
											2021-02-16 09:52:39 +01:00
										 |  |  | use FireflyIII\Helpers\Collector\GroupCollectorInterface; | 
					
						
							|  |  |  | use FireflyIII\Models\TransactionGroup; | 
					
						
							|  |  |  | use FireflyIII\Models\TransactionJournal; | 
					
						
							|  |  |  | use FireflyIII\Support\Http\Api\TransactionFilter; | 
					
						
							| 
									
										
										
										
											2025-02-15 14:10:54 +01:00
										 |  |  | use FireflyIII\Support\JsonApi\Enrichments\TransactionGroupEnrichment; | 
					
						
							| 
									
										
										
										
											2021-02-16 09:52:39 +01:00
										 |  |  | use FireflyIII\Transformers\TransactionGroupTransformer; | 
					
						
							|  |  |  | use FireflyIII\User; | 
					
						
							|  |  |  | use Illuminate\Http\JsonResponse; | 
					
						
							|  |  |  | use Illuminate\Http\Request; | 
					
						
							|  |  |  | use League\Fractal\Pagination\IlluminatePaginatorAdapter; | 
					
						
							|  |  |  | use League\Fractal\Resource\Collection as FractalCollection; | 
					
						
							|  |  |  | use League\Fractal\Resource\Item; | 
					
						
							|  |  |  | use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Class ShowController | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class ShowController extends Controller | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     use TransactionFilter; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2021-09-19 10:20:28 +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)#/transactions/listTransaction
 | 
					
						
							| 
									
										
										
										
											2021-09-19 10:20:28 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2021-02-16 09:52:39 +01:00
										 |  |  |      * Show all transactions. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2021-09-18 10:20:19 +02:00
										 |  |  |      * @throws FireflyException | 
					
						
							| 
									
										
										
										
											2021-02-16 09:52:39 +01:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function index(Request $request): JsonResponse | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-01-01 14:41:31 +01:00
										 |  |  |         $pageSize     = $this->parameters->get('limit'); | 
					
						
							|  |  |  |         $type         = $request->get('type') ?? 'default'; | 
					
						
							| 
									
										
										
										
											2021-02-16 09:52:39 +01:00
										 |  |  |         $this->parameters->set('type', $type); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-01 14:41:31 +01:00
										 |  |  |         $types        = $this->mapTransactionTypes($this->parameters->get('type')); | 
					
						
							|  |  |  |         $manager      = $this->getManager(); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-16 09:52:39 +01:00
										 |  |  |         /** @var User $admin */ | 
					
						
							| 
									
										
										
										
											2024-01-01 14:41:31 +01:00
										 |  |  |         $admin        = auth()->user(); | 
					
						
							| 
									
										
										
										
											2021-02-16 09:52:39 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // use new group collector:
 | 
					
						
							|  |  |  |         /** @var GroupCollectorInterface $collector */ | 
					
						
							| 
									
										
										
										
											2024-01-01 14:41:31 +01:00
										 |  |  |         $collector    = app(GroupCollectorInterface::class); | 
					
						
							| 
									
										
										
										
											2021-02-16 09:52:39 +01:00
										 |  |  |         $collector | 
					
						
							|  |  |  |             ->setUser($admin) | 
					
						
							|  |  |  |             // all info needed for the API:
 | 
					
						
							|  |  |  |             ->withAPIInformation() | 
					
						
							|  |  |  |             // set page size:
 | 
					
						
							|  |  |  |             ->setLimit($pageSize) | 
					
						
							|  |  |  |             // set page to retrieve
 | 
					
						
							|  |  |  |             ->setPage($this->parameters->get('page')) | 
					
						
							|  |  |  |             // set types of transactions to return.
 | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |             ->setTypes($types) | 
					
						
							|  |  |  |         ; | 
					
						
							| 
									
										
										
										
											2023-09-19 07:31:12 +02:00
										 |  |  |         if (null !== $this->parameters->get('start') || null !== $this->parameters->get('end')) { | 
					
						
							| 
									
										
										
										
											2021-02-16 09:52:39 +01:00
										 |  |  |             $collector->setRange($this->parameters->get('start'), $this->parameters->get('end')); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-01-01 14:41:31 +01:00
										 |  |  |         $paginator    = $collector->getPaginatedGroups(); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |         $paginator->setPath(route('api.v1.transactions.index').$this->buildParams()); | 
					
						
							| 
									
										
										
										
											2025-02-15 14:10:54 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // enrich
 | 
					
						
							| 
									
										
										
										
											2025-02-17 04:11:50 +01:00
										 |  |  |         $enrichment   = new TransactionGroupEnrichment(); | 
					
						
							| 
									
										
										
										
											2025-02-15 14:10:54 +01:00
										 |  |  |         $enrichment->setUser($admin); | 
					
						
							|  |  |  |         $transactions = $enrichment->enrich($paginator->getCollection()); | 
					
						
							| 
									
										
										
										
											2021-02-16 09:52:39 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         /** @var TransactionGroupTransformer $transformer */ | 
					
						
							| 
									
										
										
										
											2024-01-01 14:41:31 +01:00
										 |  |  |         $transformer  = app(TransactionGroupTransformer::class); | 
					
						
							| 
									
										
										
										
											2021-02-16 09:52:39 +01:00
										 |  |  |         $transformer->setParameters($this->parameters); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-01 14:41:31 +01:00
										 |  |  |         $resource     = new FractalCollection($transactions, $transformer, 'transactions'); | 
					
						
							| 
									
										
										
										
											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); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * This endpoint is documented at: | 
					
						
							|  |  |  |      * https://api-docs.firefly-iii.org/?urls.primaryName=2.0.0%20(v1)#/transactions/getTransactionByJournal
 | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * Show a single transaction, by transaction journal. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function showJournal(TransactionJournal $transactionJournal): JsonResponse | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->show($transactionJournal->transactionGroup); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-16 09:52:39 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2021-09-19 10:20:28 +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)#/transactions/getTransaction
 | 
					
						
							| 
									
										
										
										
											2021-09-19 10:20:28 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2021-02-16 09:52:39 +01:00
										 |  |  |      * Show a single transaction. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function show(TransactionGroup $transactionGroup): JsonResponse | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-01-01 14:41:31 +01:00
										 |  |  |         $manager       = $this->getManager(); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-16 09:52:39 +01:00
										 |  |  |         /** @var User $admin */ | 
					
						
							| 
									
										
										
										
											2024-01-01 14:41:31 +01:00
										 |  |  |         $admin         = auth()->user(); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-16 09:52:39 +01:00
										 |  |  |         // use new group collector:
 | 
					
						
							|  |  |  |         /** @var GroupCollectorInterface $collector */ | 
					
						
							| 
									
										
										
										
											2024-01-01 14:41:31 +01:00
										 |  |  |         $collector     = app(GroupCollectorInterface::class); | 
					
						
							| 
									
										
										
										
											2021-02-16 09:52:39 +01:00
										 |  |  |         $collector | 
					
						
							|  |  |  |             ->setUser($admin) | 
					
						
							|  |  |  |             // filter on transaction group.
 | 
					
						
							|  |  |  |             ->setTransactionGroup($transactionGroup) | 
					
						
							|  |  |  |             // all info needed for the API:
 | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |             ->withAPIInformation() | 
					
						
							|  |  |  |         ; | 
					
						
							| 
									
										
										
										
											2021-02-16 09:52:39 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $selectedGroup = $collector->getGroups()->first(); | 
					
						
							|  |  |  |         if (null === $selectedGroup) { | 
					
						
							|  |  |  |             throw new NotFoundHttpException(); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-15 14:10:54 +01:00
										 |  |  |         // enrich
 | 
					
						
							| 
									
										
										
										
											2025-02-17 04:11:50 +01:00
										 |  |  |         $enrichment    = new TransactionGroupEnrichment(); | 
					
						
							| 
									
										
										
										
											2025-02-15 14:10:54 +01:00
										 |  |  |         $enrichment->setUser($admin); | 
					
						
							|  |  |  |         $selectedGroup = $enrichment->enrichSingle($selectedGroup); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-27 20:45:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-16 09:52:39 +01:00
										 |  |  |         /** @var TransactionGroupTransformer $transformer */ | 
					
						
							| 
									
										
										
										
											2024-01-01 14:41:31 +01:00
										 |  |  |         $transformer   = app(TransactionGroupTransformer::class); | 
					
						
							| 
									
										
										
										
											2021-02-16 09:52:39 +01:00
										 |  |  |         $transformer->setParameters($this->parameters); | 
					
						
							| 
									
										
										
										
											2024-01-01 14:41:31 +01:00
										 |  |  |         $resource      = new Item($selectedGroup, $transformer, 'transactions'); | 
					
						
							| 
									
										
										
										
											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
										 |  |  | } |