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

256 lines
8.4 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
* 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;
use FireflyIII\Exceptions\FireflyException;
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;
use Illuminate\Http\Request;
use InvalidArgumentException;
use Response;
2016-04-03 14:55:56 +02:00
use View;
2016-04-01 16:06:55 +02:00
/**
2017-11-15 12:25:49 +01:00
* Class ReportController.
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) {
2017-11-15 12:25:49 +01:00
// @var AccountRepositoryInterface $repository
2017-03-29 21:20:54 +02:00
$this->accountRepository = app(AccountRepositoryInterface::class);
2017-11-15 12:25:49 +01:00
// @var BudgetRepositoryInterface $repository
2017-03-29 21:20:54 +02:00
$this->budgetRepository = app(BudgetRepositoryInterface::class);
2017-11-15 12:25:49 +01:00
// @var CategoryRepositoryInterface categoryRepository
2017-03-29 21:20:54 +02:00
$this->categoryRepository = app(CategoryRepositoryInterface::class);
2017-11-15 12:25:49 +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
*
2016-05-15 09:00:49 +02:00
* @return \Illuminate\Http\JsonResponse
2017-11-15 12:25:49 +01:00
*
2016-04-01 16:06:55 +02:00
* @throws FireflyException
2017-12-17 14:30:53 +01:00
* @throws \Throwable
* @throws \Throwable
* @throws \Throwable
* @throws \Throwable
* @throws \Throwable
2016-04-01 16:06:55 +02:00
*/
2016-12-06 09:07:50 +01:00
public function general(Request $request)
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
View::share('start', $attributes['startDate']);
View::share('end', $attributes['endDate']);
2016-04-01 16:06:55 +02:00
switch ($attributes['location']) {
default:
throw new FireflyException('Firefly cannot handle "' . e($attributes['location']) . '" ');
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
}
return Response::json(['html' => $html]);
}
2016-04-03 13:56:06 +02:00
/**
* @param $attributes
*
* @return string
2017-11-15 12:25:49 +01:00
*
2016-04-03 13:56:06 +02:00
* @throws FireflyException
2017-12-17 14:30:53 +01:00
* @throws \Throwable
2016-04-03 13:56:06 +02:00
*/
private function balanceAmount(array $attributes): string
{
2017-03-29 21:20:54 +02:00
$role = intval($attributes['role']);
$budget = $this->budgetRepository->find(intval($attributes['budgetId']));
$account = $this->accountRepository->find(intval($attributes['accountId']));
2016-04-03 13:56:06 +02:00
switch (true) {
2017-11-15 12:25:49 +01:00
case BalanceLine::ROLE_DEFAULTROLE === $role && null !== $budget->id:
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;
2017-11-15 12:25:49 +01:00
case BalanceLine::ROLE_DEFAULTROLE === $role && null === $budget->id:
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);
2016-04-03 14:55:56 +02:00
$budget->name = strval(trans('firefly.no_budget'));
2016-04-03 13:56:06 +02:00
break;
2017-11-15 12:25:49 +01:00
case BalanceLine::ROLE_DIFFROLE === $role:
2017-03-30 18:42:02 +02:00
$journals = $this->popupHelper->balanceDifference($account, $attributes);
2016-04-03 14:55:56 +02:00
$budget->name = strval(trans('firefly.leftUnbalanced'));
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.
2016-04-03 13:56:06 +02:00
throw new FireflyException('Firefly cannot handle this type of info-button (BalanceLine::TagRole)');
}
2016-04-03 14:55:56 +02:00
$view = view('popup.report.balance-amount', compact('journals', 'budget', 'account'))->render();
2016-04-03 13:56:06 +02:00
return $view;
}
2016-04-01 16:06:55 +02:00
/**
2016-04-03 11:07:51 +02:00
* Returns all expenses inside the given budget for the given accounts.
*
2016-04-01 16:06:55 +02:00
* @param array $attributes
*
* @return string
2017-11-15 12:25:49 +01:00
*
2017-12-17 14:30:53 +01:00
* @throws \Throwable
2016-04-01 16:06:55 +02:00
*/
private function budgetSpentAmount(array $attributes): string
{
2017-03-29 21:20:54 +02:00
$budget = $this->budgetRepository->find(intval($attributes['budgetId']));
$journals = $this->popupHelper->byBudget($budget, $attributes);
$view = view('popup.report.budget-spent-amount', compact('journals', 'budget'))->render();
2016-04-01 16:06:55 +02:00
return $view;
}
2016-04-03 11:14:36 +02:00
/**
2016-04-03 11:20:55 +02:00
* Returns all expenses in category in range.
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
2017-11-15 12:25:49 +01:00
*
2017-12-17 14:30:53 +01:00
* @throws \Throwable
2016-04-03 11:14:36 +02:00
*/
2016-04-03 13:56:06 +02:00
private function categoryEntry(array $attributes): string
2016-04-03 11:14:36 +02:00
{
2017-03-29 21:20:54 +02:00
$category = $this->categoryRepository->find(intval($attributes['categoryId']));
$journals = $this->popupHelper->byCategory($category, $attributes);
$view = view('popup.report.category-entry', compact('journals', 'category'))->render();
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
2017-11-15 12:25:49 +01:00
*
2017-12-17 14:30:53 +01:00
* @throws \Throwable
2016-04-03 11:07:51 +02:00
*/
2016-04-03 13:56:06 +02:00
private function expenseEntry(array $attributes): string
2016-04-03 11:07:51 +02:00
{
2017-03-29 21:20:54 +02:00
$account = $this->accountRepository->find(intval($attributes['accountId']));
$journals = $this->popupHelper->byExpenses($account, $attributes);
$view = view('popup.report.expense-entry', compact('journals', 'account'))->render();
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
2017-11-15 12:25:49 +01:00
*
2017-12-17 14:30:53 +01:00
* @throws \Throwable
2016-04-03 11:20:55 +02:00
*/
2016-04-03 13:56:06 +02:00
private function incomeEntry(array $attributes): string
2016-04-03 11:20:55 +02:00
{
2017-03-29 21:20:54 +02:00
$account = $this->accountRepository->find(intval($attributes['accountId']));
$journals = $this->popupHelper->byIncome($account, $attributes);
$view = view('popup.report.income-entry', compact('journals', 'account'))->render();
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
2017-11-15 12:25:49 +01:00
*
2016-04-01 16:06:55 +02:00
* @throws FireflyException
*/
private function parseAttributes(array $attributes): array
{
$attributes['location'] = $attributes['location'] ?? '';
$attributes['accounts'] = AccountList::routeBinder($attributes['accounts'] ?? '', '');
try {
$attributes['startDate'] = Carbon::createFromFormat('Ymd', $attributes['startDate']);
} catch (InvalidArgumentException $e) {
throw new FireflyException('Could not parse start date "' . e($attributes['startDate']) . '".');
}
try {
$attributes['endDate'] = Carbon::createFromFormat('Ymd', $attributes['endDate']);
} catch (InvalidArgumentException $e) {
throw new FireflyException('Could not parse start date "' . e($attributes['endDate']) . '".');
}
return $attributes;
}
2016-04-08 17:54:25 +02:00
}