Files
firefly-iii/app/Api/V1/Controllers/Chart/CategoryController.php

140 lines
5.2 KiB
PHP
Raw Normal View History

<?php
2019-02-09 10:36:59 +01:00
/**
* CategoryController.php
* Copyright (c) 2019 james@firefly-iii.org
2019-02-09 10:36:59 +01:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2019-02-09 10:36:59 +01:00
*
* 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.
2019-02-09 10:36:59 +01:00
*
* This program is distributed in the hope that it will be useful,
2019-02-09 10:36:59 +01:00
* 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.
2019-02-09 10:36:59 +01:00
*
* 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/>.
2019-02-09 10:36:59 +01:00
*/
declare(strict_types=1);
namespace FireflyIII\Api\V1\Controllers\Chart;
use Carbon\Carbon;
use FireflyIII\Api\V1\Controllers\Controller;
2019-06-09 15:28:54 +02:00
use FireflyIII\Api\V1\Requests\DateRequest;
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
use FireflyIII\Repositories\Category\NoCategoryRepositoryInterface;
use FireflyIII\Repositories\Category\OperationsRepositoryInterface;
use FireflyIII\User;
use Illuminate\Http\JsonResponse;
/**
* Class CategoryController
*/
class CategoryController extends Controller
{
/** @var CategoryRepositoryInterface */
private $categoryRepository;
2020-07-31 09:42:00 +02:00
/** @var NoCategoryRepositoryInterface */
private $noCatRepository;
2020-07-31 09:42:00 +02:00
/** @var OperationsRepositoryInterface */
private $opsRepository;
2020-07-31 09:24:08 +02:00
/**
* AccountController constructor.
*
2019-06-09 15:28:54 +02:00
* @codeCoverageIgnore
*/
public function __construct()
{
parent::__construct();
$this->middleware(
function ($request, $next) {
/** @var User $user */
$user = auth()->user();
$this->categoryRepository = app(CategoryRepositoryInterface::class);
$this->opsRepository = app(OperationsRepositoryInterface::class);
$this->noCatRepository = app(NoCategoryRepositoryInterface::class);
$this->categoryRepository->setUser($user);
$this->opsRepository->setUser($user);
$this->noCatRepository->setUser($user);
return $next($request);
}
);
}
/**
2019-06-09 15:28:54 +02:00
* @param DateRequest $request
*
* @return JsonResponse
*/
2019-06-09 15:28:54 +02:00
public function overview(DateRequest $request): JsonResponse
{
// parameters for chart:
2019-06-09 15:28:54 +02:00
$dates = $request->getAll();
/** @var Carbon $start */
2019-06-09 15:28:54 +02:00
$start = $dates['start'];
/** @var Carbon $end */
2019-06-09 15:28:54 +02:00
$end = $dates['end'];
2019-08-28 12:28:23 +02:00
$tempData = [];
$spentWith = $this->opsRepository->listExpenses($start, $end);
$spentWithout = $this->noCatRepository->listExpenses($start, $end);
2019-08-28 12:28:23 +02:00
$categories = [];
2020-07-31 09:42:00 +02:00
foreach ([$spentWith, $spentWithout, ] as $set) {
2019-08-28 12:28:23 +02:00
foreach ($set as $currency) {
foreach ($currency['categories'] as $category) {
$categories[] = $category['name'];
$outKey = sprintf('%d-e', $currency['currency_id']);
$tempData[$outKey] = $tempData[$outKey] ?? [
'currency_id' => $currency['currency_id'],
'label' => (string) trans('firefly.box_spent_in_currency', ['currency' => $currency['currency_name']]),
2019-08-28 12:28:23 +02:00
'currency_code' => $currency['currency_code'],
'currency_symbol' => $currency['currency_symbol'],
'currency_decimal_places' => $currency['currency_decimal_places'],
'type' => 'bar', // line, area or bar
'yAxisID' => 0, // 0, 1, 2
'entries' => [],
2019-08-28 12:28:23 +02:00
];
foreach ($category['transaction_journals'] as $journal) {
// is it expense or income?
$currentKey = sprintf('%d-%s', $currency['currency_id'], 'e');
2019-08-28 12:28:23 +02:00
$name = $category['name'];
$tempData[$currentKey]['entries'][$name] = $tempData[$currentKey]['entries'][$name] ?? '0';
$tempData[$currentKey]['entries'][$name] = bcadd($tempData[$currentKey]['entries'][$name], $journal['amount']);
}
}
}
}
// re-sort every spent array and add 0 for missing entries.
foreach ($tempData as $index => $set) {
$oldSet = $set['entries'];
$newSet = [];
2019-08-28 12:28:23 +02:00
foreach ($categories as $category) {
2019-09-04 17:39:39 +02:00
$value = $oldSet[$category] ?? '0';
$value = -1 === bccomp($value, '0') ? bcmul($value, '-1') : $value;
2019-08-28 12:28:23 +02:00
$newSet[$category] = $value;
}
$tempData[$index]['entries'] = $newSet;
}
$chartData = array_values($tempData);
return response()->json($chartData);
}
2019-02-09 10:36:59 +01:00
}