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

246 lines
7.8 KiB
PHP
Raw Normal View History

2016-04-01 16:06:55 +02:00
<?php
/**
* ReportController.php
2016-04-01 16:44:46 +02:00
* Copyright (C) 2016 thegrumpydictator@gmail.com
2016-04-01 16:06:55 +02:00
*
* This software may be modified and distributed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International License.
*
* See the LICENSE file for details.
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
/**
* Class ReportController
*
* @package FireflyIII\Http\Controllers\Popup
*/
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) {
/** @var AccountRepositoryInterface $repository */
$this->accountRepository = app(AccountRepositoryInterface::class);
/** @var BudgetRepositoryInterface $repository */
$this->budgetRepository = app(BudgetRepositoryInterface::class);
/** @var CategoryRepositoryInterface categoryRepository */
$this->categoryRepository = app(CategoryRepositoryInterface::class);
/** @var PopupReportInterface popupHelper */
$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
2016-04-01 16:06:55 +02:00
* @throws FireflyException
*/
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
* @throws FireflyException
*/
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) {
case ($role === BalanceLine::ROLE_DEFAULTROLE && !is_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;
case ($role === BalanceLine::ROLE_DEFAULTROLE && is_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;
case ($role === BalanceLine::ROLE_DIFFROLE):
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;
case ($role === BalanceLine::ROLE_TAGROLE):
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
* @throws FireflyException
*/
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
*
* @param $attributes
*
* @return string
* @throws FireflyException
*/
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.
*
* @param $attributes
*
* @return string
* @throws FireflyException
*/
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.
*
* @param $attributes
*
* @return string
* @throws FireflyException
*/
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
* @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
}