Files
firefly-iii/app/Http/Controllers/Popup/ReportController.php

285 lines
9.6 KiB
PHP
Raw Normal View History

2016-04-01 16:06:55 +02:00
<?php
/**
* ReportController.php
2017-10-21 08:40:00 +02:00
* Copyright (c) 2017 thegrumpydictator@gmail.com
2016-04-01 16:06:55 +02:00
*
2017-10-21 08:40:00 +02:00
* This file is part of Firefly III.
*
2017-10-21 08:40:00 +02:00
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2017-12-17 14:41:58 +01:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
2016-04-01 16:06:55 +02:00
*/
2017-03-29 21:20:54 +02:00
declare(strict_types=1);
2016-04-01 16:06:55 +02:00
namespace FireflyIII\Http\Controllers\Popup;
use Carbon\Carbon;
2016-04-03 13:56:06 +02:00
use FireflyIII\Helpers\Collection\BalanceLine;
2017-03-29 21:20:54 +02:00
use FireflyIII\Helpers\Report\PopupReportInterface;
2016-04-01 16:06:55 +02:00
use FireflyIII\Http\Controllers\Controller;
2016-10-10 07:12:39 +02:00
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
2016-04-01 16:06:55 +02:00
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
2016-04-01 16:06:55 +02:00
use FireflyIII\Support\Binder\AccountList;
2018-07-08 12:28:42 +02:00
use Illuminate\Http\JsonResponse;
2016-04-01 16:06:55 +02:00
use Illuminate\Http\Request;
2017-12-25 10:34:32 +01:00
use Illuminate\Routing\Route;
2016-04-01 16:06:55 +02:00
use InvalidArgumentException;
2018-07-20 14:34:56 +02:00
use Log;
use Throwable;
2016-04-01 16:06:55 +02:00
/**
2017-11-15 12:25:49 +01:00
* Class ReportController.
2018-07-20 14:34:56 +02:00
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2016-04-01 16:06:55 +02:00
*/
class ReportController extends Controller
{
2017-03-29 21:20:54 +02:00
/** @var AccountRepositoryInterface */
private $accountRepository;
/** @var BudgetRepositoryInterface */
private $budgetRepository;
/** @var CategoryRepositoryInterface */
private $categoryRepository;
/** @var PopupReportInterface */
private $popupHelper;
/**
*
*/
public function __construct()
{
parent::__construct();
$this->middleware(
function ($request, $next) {
2018-01-17 09:22:45 +01:00
/** @var AccountRepositoryInterface accountRepository */
2017-03-29 21:20:54 +02:00
$this->accountRepository = app(AccountRepositoryInterface::class);
2018-01-17 09:22:45 +01:00
/** @var BudgetRepositoryInterface budgetRepository */
2017-03-29 21:20:54 +02:00
$this->budgetRepository = app(BudgetRepositoryInterface::class);
2018-01-17 09:22:45 +01:00
/** @var CategoryRepositoryInterface categoryRepository */
2017-03-29 21:20:54 +02:00
$this->categoryRepository = app(CategoryRepositoryInterface::class);
2018-01-17 09:22:45 +01:00
/** @var PopupReportInterface popupHelper */
2017-03-29 21:20:54 +02:00
$this->popupHelper = app(PopupReportInterface::class);
return $next($request);
}
);
}
2016-04-01 16:06:55 +02:00
/**
* @param Request $request
*
2018-07-08 12:28:42 +02:00
* @return JsonResponse
2018-07-20 14:34:56 +02:00
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
2016-04-01 16:06:55 +02:00
*/
2018-07-08 12:28:42 +02:00
public function general(Request $request): JsonResponse
2016-04-01 16:06:55 +02:00
{
2016-05-22 15:48:34 +02:00
$attributes = $request->get('attributes') ?? [];
2016-04-01 16:06:55 +02:00
$attributes = $this->parseAttributes($attributes);
2016-04-03 14:55:56 +02:00
app('view')->share('start', $attributes['startDate']);
app('view')->share('end', $attributes['endDate']);
2016-04-03 14:55:56 +02:00
2016-04-01 16:06:55 +02:00
switch ($attributes['location']) {
default:
2018-07-20 14:34:56 +02:00
$html = sprintf('Firefly III cannot handle "%s"-popups.', $attributes['location']);
break;
2016-04-01 16:06:55 +02:00
case 'budget-spent-amount':
$html = $this->budgetSpentAmount($attributes);
2016-04-03 11:07:51 +02:00
break;
case 'expense-entry':
$html = $this->expenseEntry($attributes);
2016-04-01 16:06:55 +02:00
break;
2016-04-03 11:14:36 +02:00
case 'income-entry':
$html = $this->incomeEntry($attributes);
break;
2016-04-03 11:20:55 +02:00
case 'category-entry':
$html = $this->categoryEntry($attributes);
break;
2016-04-03 13:56:06 +02:00
case 'balance-amount':
$html = $this->balanceAmount($attributes);
break;
2016-04-01 16:06:55 +02:00
}
2018-03-10 20:30:09 +01:00
return response()->json(['html' => $html]);
2016-04-01 16:06:55 +02:00
}
2016-04-03 13:56:06 +02:00
/**
2018-04-28 06:23:13 +02:00
* @param array $attributes
2016-04-03 13:56:06 +02:00
*
* @return string
2017-11-15 12:25:49 +01:00
*
2018-07-20 14:34:56 +02:00
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
2016-04-03 13:56:06 +02:00
*/
private function balanceAmount(array $attributes): string
{
2018-04-02 15:10:40 +02:00
$role = (int)$attributes['role'];
$budget = $this->budgetRepository->findNull((int)$attributes['budgetId']);
$account = $this->accountRepository->findNull((int)$attributes['accountId']);
2016-04-03 13:56:06 +02:00
2018-06-23 17:40:41 +02:00
2016-04-03 13:56:06 +02:00
switch (true) {
2018-07-09 19:24:08 +02:00
case BalanceLine::ROLE_DEFAULTROLE === $role && null !== $budget && null !== $account:
2017-03-29 21:20:54 +02:00
// normal row with a budget:
$journals = $this->popupHelper->balanceForBudget($budget, $account, $attributes);
2016-04-03 13:56:06 +02:00
break;
2018-07-09 19:24:08 +02:00
case BalanceLine::ROLE_DEFAULTROLE === $role && null === $budget && null !== $account:
2017-03-29 21:20:54 +02:00
// normal row without a budget:
2017-03-30 18:42:02 +02:00
$journals = $this->popupHelper->balanceForNoBudget($account, $attributes);
2018-04-02 15:10:40 +02:00
$budget->name = (string)trans('firefly.no_budget');
2016-04-03 13:56:06 +02:00
break;
2017-11-15 12:25:49 +01:00
case BalanceLine::ROLE_TAGROLE === $role:
2017-03-29 21:20:54 +02:00
// row with tag info.
2018-07-20 14:34:56 +02:00
return 'Firefly cannot handle this type of info-button (BalanceLine::TagRole)';
}
try {
$view = view('popup.report.balance-amount', compact('journals', 'budget', 'account'))->render();
} catch (Throwable $e) {
Log::error(sprintf('Could not render: %s', $e->getMessage()));
$view = 'Firefly III could not render the view. Please see the log files.';
2016-04-03 13:56:06 +02:00
}
return $view;
}
2016-04-01 16:06:55 +02:00
/**
* @param array $attributes
*
* @return string
*/
private function budgetSpentAmount(array $attributes): string
{
2018-07-13 15:50:42 +02:00
$budget = $this->budgetRepository->findNull((int)$attributes['budgetId']);
if (null === $budget) {
2018-07-20 14:34:56 +02:00
return 'This is an unknown budget. Apologies.';
2018-07-09 19:24:08 +02:00
}
2017-03-29 21:20:54 +02:00
$journals = $this->popupHelper->byBudget($budget, $attributes);
2018-07-20 14:34:56 +02:00
try {
$view = view('popup.report.budget-spent-amount', compact('journals', 'budget'))->render();
} catch (Throwable $e) {
Log::error(sprintf('Could not render: %s', $e->getMessage()));
$view = 'Firefly III could not render the view. Please see the log files.';
}
2016-04-01 16:06:55 +02:00
return $view;
}
2016-04-03 11:14:36 +02:00
/**
2017-12-17 14:30:53 +01:00
* @param array $attributes
2016-04-03 11:14:36 +02:00
*
* @return string
*/
2016-04-03 13:56:06 +02:00
private function categoryEntry(array $attributes): string
2016-04-03 11:14:36 +02:00
{
2018-04-02 15:10:40 +02:00
$category = $this->categoryRepository->findNull((int)$attributes['categoryId']);
2018-07-09 19:24:08 +02:00
2018-07-13 15:50:42 +02:00
if (null === $category) {
2018-07-20 14:34:56 +02:00
return 'This is an unknown category. Apologies.';
2018-07-09 19:24:08 +02:00
}
2017-03-29 21:20:54 +02:00
$journals = $this->popupHelper->byCategory($category, $attributes);
2018-07-20 14:34:56 +02:00
try {
$view = view('popup.report.category-entry', compact('journals', 'category'))->render();
} catch (Throwable $e) {
Log::error(sprintf('Could not render: %s', $e->getMessage()));
$view = 'Firefly III could not render the view. Please see the log files.';
}
2016-04-03 11:14:36 +02:00
return $view;
}
2016-04-03 11:07:51 +02:00
/**
* Returns all the expenses that went to the given expense account.
*
2017-12-17 14:30:53 +01:00
* @param array $attributes
2016-04-03 11:07:51 +02:00
*
* @return string
*/
2016-04-03 13:56:06 +02:00
private function expenseEntry(array $attributes): string
2016-04-03 11:07:51 +02:00
{
2018-07-13 15:50:42 +02:00
$account = $this->accountRepository->findNull((int)$attributes['accountId']);
2018-07-09 19:24:08 +02:00
2018-07-13 15:50:42 +02:00
if (null === $account) {
2018-07-20 14:34:56 +02:00
return 'This is an unknown account. Apologies.';
2018-07-09 19:24:08 +02:00
}
2017-03-29 21:20:54 +02:00
$journals = $this->popupHelper->byExpenses($account, $attributes);
2018-07-20 14:34:56 +02:00
try {
$view = view('popup.report.expense-entry', compact('journals', 'account'))->render();
} catch (Throwable $e) {
Log::error(sprintf('Could not render: %s', $e->getMessage()));
$view = 'Firefly III could not render the view. Please see the log files.';
}
2016-04-03 11:07:51 +02:00
return $view;
}
2016-04-03 11:20:55 +02:00
/**
* Returns all the incomes that went to the given asset account.
*
2017-12-17 14:30:53 +01:00
* @param array $attributes
2016-04-03 11:20:55 +02:00
*
* @return string
*/
2016-04-03 13:56:06 +02:00
private function incomeEntry(array $attributes): string
2016-04-03 11:20:55 +02:00
{
2018-07-13 15:50:42 +02:00
$account = $this->accountRepository->findNull((int)$attributes['accountId']);
2018-07-09 19:24:08 +02:00
2018-07-13 15:50:42 +02:00
if (null === $account) {
2018-07-20 14:34:56 +02:00
return 'This is an unknown category. Apologies.';
2018-07-09 19:24:08 +02:00
}
2017-03-29 21:20:54 +02:00
$journals = $this->popupHelper->byIncome($account, $attributes);
2018-07-20 14:34:56 +02:00
try {
$view = view('popup.report.income-entry', compact('journals', 'account'))->render();
} catch (Throwable $e) {
Log::error(sprintf('Could not render: %s', $e->getMessage()));
$view = 'Firefly III could not render the view. Please see the log files.';
}
2016-04-03 14:55:56 +02:00
2016-04-03 11:20:55 +02:00
return $view;
}
2016-04-01 16:06:55 +02:00
/**
* @param array $attributes
*
* @return array
*/
private function parseAttributes(array $attributes): array
{
$attributes['location'] = $attributes['location'] ?? '';
2017-12-29 09:05:35 +01:00
$attributes['accounts'] = AccountList::routeBinder($attributes['accounts'] ?? '', new Route('get', '', []));
2016-04-01 16:06:55 +02:00
try {
$attributes['startDate'] = Carbon::createFromFormat('Ymd', $attributes['startDate']);
} catch (InvalidArgumentException $e) {
2018-07-20 16:28:54 +02:00
Log::debug(sprintf('Not important error message: %s', $e->getMessage()));
2018-07-20 14:34:56 +02:00
$date = new Carbon;
$date->startOfMonth();
$attributes['startDate'] = $date;
2016-04-01 16:06:55 +02:00
}
try {
$attributes['endDate'] = Carbon::createFromFormat('Ymd', $attributes['endDate']);
} catch (InvalidArgumentException $e) {
2018-07-20 14:34:56 +02:00
Log::debug('Not important error message: %s', $e->getMessage());
$date = new Carbon;
$date->startOfMonth();
$attributes['endDate'] = $date;
2016-04-01 16:06:55 +02:00
}
return $attributes;
}
2016-04-08 17:54:25 +02:00
}