Files
firefly-iii/app/Api/V1/Controllers/Insight/Expense/AccountController.php

139 lines
4.7 KiB
PHP
Raw Normal View History

<?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);
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;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
2021-03-05 16:28:59 +01:00
use FireflyIII\Repositories\Account\OperationsRepositoryInterface;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use FireflyIII\Support\Http\Api\ApiSupport;
use Illuminate\Http\JsonResponse;
/**
2021-03-03 17:06:18 +01:00
*
* Class AccountController
*
* Shows expense information grouped or limited by date.
2021-09-18 05:58:22 +02:00
* I.e. all expenses grouped by account + currency.
*/
2021-03-03 17:06:18 +01:00
class AccountController extends Controller
{
use ApiSupport;
2021-03-05 16:28:59 +01:00
private CurrencyRepositoryInterface $currencyRepository;
private OperationsRepositoryInterface $opsRepository;
2021-03-21 09:15:40 +01:00
private AccountRepositoryInterface $repository;
/**
* AccountController constructor.
*
* @codeCoverageIgnore
*/
public function __construct()
{
parent::__construct();
$this->middleware(
function ($request, $next) {
$user = auth()->user();
$this->repository = app(AccountRepositoryInterface::class);
$this->repository->setUser($user);
$this->currencyRepository = app(CurrencyRepositoryInterface::class);
$this->currencyRepository->setUser($user);
2021-03-05 16:28:59 +01:00
$this->opsRepository = app(OperationsRepositoryInterface::class);
$this->opsRepository->setUser($user);
return $next($request);
}
);
}
/**
2021-09-18 05:58:22 +02:00
* This endpoint is documented at:
* https://api-docs.firefly-iii.org/#/insight/insightExpenseAsset
*
2021-03-05 20:17:39 +01:00
* @param GenericRequest $request
*
2021-03-03 17:06:18 +01:00
* @return JsonResponse
*/
2021-03-21 09:15:40 +01:00
public function asset(GenericRequest $request): JsonResponse
{
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-03-29 14:56:27 +02: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-10-16 14:44:11 +02:00
'difference_float' => (float) $expense['sum'], // float but on purpose.
2022-03-29 14:56:27 +02:00
'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:
* https://api-docs.firefly-iii.org/#/insight/insightExpenseExpense
*
2021-03-05 20:17:39 +01:00
* @param GenericRequest $request
2021-03-05 07:03:28 +01:00
*
* @return JsonResponse
*/
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-03-29 14:56:27 +02: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-10-16 14:44:11 +02:00
'difference_float' => (float) $expense['sum'], // float but on purpose.
2022-03-29 14:56:27 +02:00
'currency_id' => (string) $expense['currency_id'],
2021-03-05 16:28:59 +01:00
'currency_code' => $expense['currency_code'],
];
}
2021-03-05 16:28:59 +01:00
return response()->json($result);
}
2020-12-22 05:35:06 +01:00
}