| 
									
										
										
										
											2020-11-27 06:13:46 +01:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2021-01-29 18:50:35 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* | 
					
						
							|  |  |  |  * DateController.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/>. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-22 05:35:06 +01:00
										 |  |  | declare(strict_types=1); | 
					
						
							| 
									
										
										
										
											2020-11-27 06:13:46 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Api\V1\Controllers\Insight\Expense; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use FireflyIII\Api\V1\Controllers\Controller; | 
					
						
							| 
									
										
										
										
											2021-03-05 20:17:39 +01:00
										 |  |  | use FireflyIII\Api\V1\Requests\Insight\GenericRequest; | 
					
						
							| 
									
										
										
										
											2020-11-27 06:13:46 +01:00
										 |  |  | use FireflyIII\Repositories\Account\AccountRepositoryInterface; | 
					
						
							| 
									
										
										
										
											2021-03-05 16:28:59 +01:00
										 |  |  | use FireflyIII\Repositories\Account\OperationsRepositoryInterface; | 
					
						
							| 
									
										
										
										
											2020-11-27 06:13:46 +01:00
										 |  |  | use FireflyIII\Support\Http\Api\ApiSupport; | 
					
						
							|  |  |  | use Illuminate\Http\JsonResponse; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2021-03-03 17:06:18 +01:00
										 |  |  |  * Class AccountController | 
					
						
							| 
									
										
										
										
											2020-11-27 06:13:46 +01:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Shows expense information grouped or limited by date. | 
					
						
							| 
									
										
										
										
											2021-09-18 05:58:22 +02:00
										 |  |  |  * I.e. all expenses grouped by account + currency. | 
					
						
							| 
									
										
										
										
											2020-11-27 06:13:46 +01:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2021-03-03 17:06:18 +01:00
										 |  |  | class AccountController extends Controller | 
					
						
							| 
									
										
										
										
											2020-11-27 06:13:46 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     use ApiSupport; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-05 16:28:59 +01:00
										 |  |  |     private OperationsRepositoryInterface $opsRepository; | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |     private AccountRepositoryInterface    $repository; | 
					
						
							| 
									
										
										
										
											2020-11-27 06:13:46 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * AccountController constructor. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function __construct() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         parent::__construct(); | 
					
						
							|  |  |  |         $this->middleware( | 
					
						
							|  |  |  |             function ($request, $next) { | 
					
						
							| 
									
										
										
										
											2024-01-01 14:41:31 +01:00
										 |  |  |                 $user                = auth()->user(); | 
					
						
							|  |  |  |                 $this->repository    = app(AccountRepositoryInterface::class); | 
					
						
							| 
									
										
										
										
											2020-11-27 06:13:46 +01:00
										 |  |  |                 $this->repository->setUser($user); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-05 16:28:59 +01:00
										 |  |  |                 $this->opsRepository = app(OperationsRepositoryInterface::class); | 
					
						
							|  |  |  |                 $this->opsRepository->setUser($user); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-27 06:13:46 +01:00
										 |  |  |                 return $next($request); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2021-09-18 05:58:22 +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)#/insight/insightExpenseAsset
 | 
					
						
							| 
									
										
										
										
											2020-11-27 06:13:46 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |     public function asset(GenericRequest $request): JsonResponse | 
					
						
							| 
									
										
										
										
											2020-11-27 06:13:46 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |         $start         = $request->getStart(); | 
					
						
							|  |  |  |         $end           = $request->getEnd(); | 
					
						
							|  |  |  |         $assetAccounts = $request->getAssetAccounts(); | 
					
						
							| 
									
										
										
										
											2021-03-31 06:29:08 +02:00
										 |  |  |         $expenses      = $this->opsRepository->sumExpensesBySource($start, $end, $assetAccounts); | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |         $result        = []; | 
					
						
							| 
									
										
										
										
											2021-03-05 16:28:59 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         /** @var array $expense */ | 
					
						
							|  |  |  |         foreach ($expenses as $expense) { | 
					
						
							|  |  |  |             $result[] = [ | 
					
						
							| 
									
										
										
										
											2022-12-29 19:41:57 +01:00
										 |  |  |                 'id'               => (string)$expense['id'], | 
					
						
							| 
									
										
										
										
											2021-03-31 06:29:08 +02:00
										 |  |  |                 'name'             => $expense['name'], | 
					
						
							| 
									
										
										
										
											2021-03-05 16:28:59 +01:00
										 |  |  |                 'difference'       => $expense['sum'], | 
					
						
							| 
									
										
										
										
											2022-12-29 19:41:57 +01:00
										 |  |  |                 'difference_float' => (float)$expense['sum'], // intentional float
 | 
					
						
							|  |  |  |                 'currency_id'      => (string)$expense['currency_id'], | 
					
						
							| 
									
										
										
										
											2021-03-05 16:28:59 +01:00
										 |  |  |                 'currency_code'    => $expense['currency_code'], | 
					
						
							|  |  |  |             ]; | 
					
						
							| 
									
										
										
										
											2021-03-05 07:03:28 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-05 16:28:59 +01:00
										 |  |  |         return response()->json($result); | 
					
						
							| 
									
										
										
										
											2021-03-05 07:03:28 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2021-09-18 05:58:22 +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)#/insight/insightExpenseExpense
 | 
					
						
							| 
									
										
										
										
											2021-03-05 07:03:28 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |     public function expense(GenericRequest $request): JsonResponse | 
					
						
							| 
									
										
										
										
											2021-03-05 07:03:28 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |         $start           = $request->getStart(); | 
					
						
							|  |  |  |         $end             = $request->getEnd(); | 
					
						
							|  |  |  |         $assetAccounts   = $request->getAssetAccounts(); | 
					
						
							|  |  |  |         $expenseAccounts = $request->getExpenseAccounts(); | 
					
						
							| 
									
										
										
										
											2021-03-31 06:29:08 +02:00
										 |  |  |         $expenses        = $this->opsRepository->sumExpensesByDestination($start, $end, $assetAccounts, $expenseAccounts); | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |         $result          = []; | 
					
						
							| 
									
										
										
										
											2021-03-05 16:28:59 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         /** @var array $expense */ | 
					
						
							|  |  |  |         foreach ($expenses as $expense) { | 
					
						
							|  |  |  |             $result[] = [ | 
					
						
							| 
									
										
										
										
											2022-12-29 19:41:57 +01:00
										 |  |  |                 'id'               => (string)$expense['id'], | 
					
						
							| 
									
										
										
										
											2021-03-31 06:29:08 +02:00
										 |  |  |                 'name'             => $expense['name'], | 
					
						
							| 
									
										
										
										
											2021-03-05 16:28:59 +01:00
										 |  |  |                 'difference'       => $expense['sum'], | 
					
						
							| 
									
										
										
										
											2022-12-29 19:41:57 +01:00
										 |  |  |                 'difference_float' => (float)$expense['sum'], // intentional float
 | 
					
						
							|  |  |  |                 'currency_id'      => (string)$expense['currency_id'], | 
					
						
							| 
									
										
										
										
											2021-03-05 16:28:59 +01:00
										 |  |  |                 'currency_code'    => $expense['currency_code'], | 
					
						
							|  |  |  |             ]; | 
					
						
							| 
									
										
										
										
											2020-11-27 06:13:46 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-05 16:28:59 +01:00
										 |  |  |         return response()->json($result); | 
					
						
							| 
									
										
										
										
											2020-11-27 06:13:46 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-12-22 05:35:06 +01:00
										 |  |  | } |