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

238 lines
8.6 KiB
PHP
Raw Normal View History

2016-04-01 16:06:55 +02:00
<?php
2016-04-01 16:44:46 +02:00
declare(strict_types = 1);
2016-04-01 16:06:55 +02:00
/**
* 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 MIT license. See the LICENSE file for details.
*/
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;
2016-04-01 16:06:55 +02:00
use FireflyIII\Http\Controllers\Controller;
2016-04-03 13:56:06 +02:00
use FireflyIII\Models\TransactionJournal;
2016-04-03 11:07:51 +02:00
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
2016-04-01 16:06:55 +02:00
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
2016-04-03 11:20:55 +02:00
use FireflyIII\Repositories\Category\SingleCategoryRepositoryInterface;
2016-04-01 16:06:55 +02:00
use FireflyIII\Support\Binder\AccountList;
use Illuminate\Http\Request;
2016-05-05 22:03:35 +02:00
use Illuminate\Support\Collection;
2016-04-01 16:06:55 +02:00
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
{
/**
* @param Request $request
*
* @throws FireflyException
*/
public function info(Request $request)
{
$attributes = $request->get('attributes');
$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
{
$role = intval($attributes['role']);
/** @var BudgetRepositoryInterface $budgetRepository */
2016-05-02 20:49:19 +02:00
$budgetRepository = app(BudgetRepositoryInterface::class);
2016-04-03 13:56:06 +02:00
$budget = $budgetRepository->find(intval($attributes['budgetId']));
/** @var AccountRepositoryInterface $accountRepository */
2016-05-01 15:05:29 +02:00
$accountRepository = app(AccountRepositoryInterface::class);
2016-04-03 13:56:06 +02:00
$account = $accountRepository->find(intval($attributes['accountId']));
switch (true) {
case ($role === BalanceLine::ROLE_DEFAULTROLE && !is_null($budget->id)):
2016-05-06 06:15:46 +02:00
$journals = $budgetRepository->journalsInPeriod(
new Collection([$budget]), new Collection([$account]), $attributes['startDate'], $attributes['endDate']
);
2016-04-03 13:56:06 +02:00
break;
case ($role === BalanceLine::ROLE_DEFAULTROLE && is_null($budget->id)):
2016-04-03 14:55:56 +02:00
$budget->name = strval(trans('firefly.no_budget'));
2016-05-06 06:15:46 +02:00
$journals = $budgetRepository->journalsInPeriodWithoutBudget($attributes['accounts'], $attributes['startDate'], $attributes['endDate']);
2016-04-03 13:56:06 +02:00
break;
case ($role === BalanceLine::ROLE_DIFFROLE):
// journals no budget, not corrected by a tag.
2016-05-06 06:15:46 +02:00
$journals = $budgetRepository->journalsInPeriodWithoutBudget($attributes['accounts'], $attributes['startDate'], $attributes['endDate']);
2016-04-03 14:55:56 +02:00
$budget->name = strval(trans('firefly.leftUnbalanced'));
2016-04-25 18:43:09 +02:00
$journals = $journals->filter(
2016-04-03 13:56:06 +02:00
function (TransactionJournal $journal) {
$tags = $journal->tags()->where('tagMode', 'balancingAct')->count();
if ($tags === 0) {
return $journal;
}
}
);
break;
case ($role === BalanceLine::ROLE_TAGROLE):
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
{
// need to find the budget
// then search for expenses in the given period
// list them in some table format.
/** @var BudgetRepositoryInterface $repository */
2016-05-02 20:49:19 +02:00
$repository = app(BudgetRepositoryInterface::class);
2016-04-01 16:06:55 +02:00
$budget = $repository->find(intval($attributes['budgetId']));
if (is_null($budget->id)) {
2016-05-06 06:15:46 +02:00
$journals = $repository->journalsInPeriodWithoutBudget($attributes['accounts'], $attributes['startDate'], $attributes['endDate']);
2016-04-01 16:23:12 +02:00
} else {
// get all expenses in budget in period:
2016-05-06 06:15:46 +02:00
$journals = $repository->journalsInPeriod(new Collection([$budget]), $attributes['accounts'], $attributes['startDate'], $attributes['endDate']);
2016-04-01 16:06:55 +02:00
}
2016-04-03 14:55:56 +02:00
$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
{
2016-04-03 11:20:55 +02:00
/** @var SingleCategoryRepositoryInterface $repository */
2016-05-02 20:49:19 +02:00
$repository = app(SingleCategoryRepositoryInterface::class);
2016-04-03 11:20:55 +02:00
$category = $repository->find(intval($attributes['categoryId']));
$journals = $repository->getJournalsForAccountsInRange($category, $attributes['accounts'], $attributes['startDate'], $attributes['endDate']);
2016-04-03 14:55:56 +02:00
$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
{
/** @var AccountRepositoryInterface $repository */
2016-05-01 15:05:29 +02:00
$repository = app(AccountRepositoryInterface::class);
2016-04-03 11:07:51 +02:00
$account = $repository->find(intval($attributes['accountId']));
$journals = $repository->getExpensesByDestination($account, $attributes['accounts'], $attributes['startDate'], $attributes['endDate']);
2016-04-03 14:55:56 +02:00
$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
{
/** @var AccountRepositoryInterface $repository */
2016-05-01 15:05:29 +02:00
$repository = app(AccountRepositoryInterface::class);
2016-04-03 11:20:55 +02:00
$account = $repository->find(intval($attributes['accountId']));
$journals = $repository->getIncomeByDestination($account, $attributes['accounts'], $attributes['startDate'], $attributes['endDate']);
2016-04-03 14:55:56 +02:00
$view = view('popup.report.income-entry', compact('journals', 'account'))->render();
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
}