Files
firefly-iii/app/Http/Controllers/Report/ExpenseController.php

351 lines
12 KiB
PHP
Raw Normal View History

2017-12-10 08:56:20 +01:00
<?php
2017-12-10 09:02:26 +01:00
/**
* ExpenseController.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* 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/>.
2017-12-10 09:02:26 +01:00
*/
2017-12-10 08:56:20 +01:00
declare(strict_types=1);
namespace FireflyIII\Http\Controllers\Report;
2017-12-10 12:00:08 +01:00
use Carbon\Carbon;
2019-05-31 13:35:33 +02:00
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
2017-12-10 08:56:20 +01:00
use FireflyIII\Http\Controllers\Controller;
2017-12-10 17:55:06 +01:00
use FireflyIII\Models\TransactionType;
2017-12-10 12:00:08 +01:00
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
2017-12-10 17:55:06 +01:00
use FireflyIII\Support\CacheProperties;
use FireflyIII\Support\Http\Controllers\AugumentData;
2017-12-10 12:00:08 +01:00
use Illuminate\Support\Collection;
2018-07-20 14:34:56 +02:00
use Log;
use Throwable;
2017-12-10 08:56:20 +01:00
/**
* Class ExpenseController
2018-07-20 14:34:56 +02:00
*
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
2017-12-10 08:56:20 +01:00
*/
class ExpenseController extends Controller
{
use AugumentData;
/** @var AccountRepositoryInterface The account repository */
2017-12-10 12:00:08 +01:00
protected $accountRepository;
/**
2017-12-11 14:52:30 +01:00
* Constructor for ExpenseController
* @codeCoverageIgnore
2017-12-10 12:00:08 +01:00
*/
public function __construct()
{
parent::__construct();
// translations:
$this->middleware(
function ($request, $next) {
$this->accountRepository = app(AccountRepositoryInterface::class);
return $next($request);
}
);
}
2018-07-08 12:08:53 +02:00
2018-07-20 14:34:56 +02:00
/** @noinspection MoreThanThreeArgumentsInspection */
2017-12-10 20:03:10 +01:00
/**
2017-12-11 14:52:30 +01:00
* Generates the overview per budget.
*
2017-12-10 20:03:10 +01:00
* @param Collection $accounts
* @param Collection $expense
2019-05-31 13:35:33 +02:00
* @param Carbon $start
* @param Carbon $end
2017-12-10 20:03:10 +01:00
*
* @return string
2018-07-20 14:34:56 +02:00
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
2017-12-10 20:03:10 +01:00
*/
2018-07-08 12:28:42 +02:00
public function budget(Collection $accounts, Collection $expense, Carbon $start, Carbon $end): string
2017-12-10 20:03:10 +01:00
{
// Properties for cache:
$cache = new CacheProperties;
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty('expense-budget');
$cache->addProperty($accounts->pluck('id')->toArray());
$cache->addProperty($expense->pluck('id')->toArray());
if ($cache->has()) {
return $cache->get(); // @codeCoverageIgnore
}
$combined = $this->combineAccounts($expense);
$all = new Collection;
2018-07-20 14:34:56 +02:00
foreach ($combined as $combi) {
2017-12-10 20:03:10 +01:00
$all = $all->merge($combi);
}
// now find spent / earned:
2017-12-12 18:22:29 +01:00
$spent = $this->spentByBudget($accounts, $all, $start, $end);
2017-12-11 14:52:30 +01:00
// join arrays somehow:
2017-12-10 20:03:10 +01:00
$together = [];
2017-12-11 14:52:30 +01:00
foreach ($spent as $categoryId => $spentInfo) {
if (!isset($together[$categoryId])) {
$together[$categoryId]['spent'] = $spentInfo;
2017-12-12 18:22:29 +01:00
$together[$categoryId]['budget'] = $spentInfo['name'];
2017-12-11 14:52:30 +01:00
$together[$categoryId]['grand_total'] = '0';
2017-12-10 20:03:10 +01:00
}
2017-12-11 14:52:30 +01:00
$together[$categoryId]['grand_total'] = bcadd($spentInfo['grand_total'], $together[$categoryId]['grand_total']);
2017-12-10 20:03:10 +01:00
}
2018-07-20 14:34:56 +02:00
try {
$result = view('reports.partials.exp-budgets', compact('together'))->render();
2018-09-02 20:13:25 +02:00
// @codeCoverageIgnoreStart
2018-07-20 14:34:56 +02:00
} catch (Throwable $e) {
2018-07-27 03:09:35 +02:00
Log::error(sprintf('Could not render category::budget: %s', $e->getMessage()));
2019-05-31 13:35:33 +02:00
$result = sprintf('An error prevented Firefly III from rendering: %s. Apologies.', $e->getMessage());
2018-07-20 14:34:56 +02:00
}
2018-09-02 20:13:25 +02:00
// @codeCoverageIgnoreEnd
2017-12-10 20:03:10 +01:00
$cache->store($result);
return $result;
}
2018-07-08 12:08:53 +02:00
2018-07-20 14:34:56 +02:00
/** @noinspection MoreThanThreeArgumentsInspection */
2017-12-10 20:03:10 +01:00
/**
2017-12-11 14:52:30 +01:00
* Generates the overview per category (spent and earned).
*
2017-12-10 20:03:10 +01:00
* @param Collection $accounts
* @param Collection $expense
2019-05-31 13:35:33 +02:00
* @param Carbon $start
* @param Carbon $end
2017-12-10 20:03:10 +01:00
*
* @return string
2018-07-20 14:34:56 +02:00
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
2017-12-10 20:03:10 +01:00
*/
2018-07-08 12:28:42 +02:00
public function category(Collection $accounts, Collection $expense, Carbon $start, Carbon $end): string
2017-12-10 17:55:06 +01:00
{
// Properties for cache:
$cache = new CacheProperties;
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty('expense-category');
$cache->addProperty($accounts->pluck('id')->toArray());
$cache->addProperty($expense->pluck('id')->toArray());
if ($cache->has()) {
return $cache->get(); // @codeCoverageIgnore
}
$combined = $this->combineAccounts($expense);
$all = new Collection;
2018-07-20 14:34:56 +02:00
foreach ($combined as $combi) {
2017-12-10 17:55:06 +01:00
$all = $all->merge($combi);
}
// now find spent / earned:
$spent = $this->spentByCategory($accounts, $all, $start, $end);
2017-12-10 20:10:04 +01:00
$earned = $this->earnedByCategory($accounts, $all, $start, $end);
2017-12-10 17:55:06 +01:00
// join arrays somehow:
$together = [];
2017-12-10 20:03:10 +01:00
foreach ($spent as $categoryId => $spentInfo) {
if (!isset($together[$categoryId])) {
2017-12-11 14:52:30 +01:00
$together[$categoryId]['spent'] = $spentInfo;
$together[$categoryId]['category'] = $spentInfo['name'];
$together[$categoryId]['grand_total'] = '0';
2017-12-10 17:55:06 +01:00
}
2017-12-11 14:52:30 +01:00
$together[$categoryId]['grand_total'] = bcadd($spentInfo['grand_total'], $together[$categoryId]['grand_total']);
2017-12-10 17:55:06 +01:00
}
2017-12-10 20:03:10 +01:00
foreach ($earned as $categoryId => $earnedInfo) {
if (!isset($together[$categoryId])) {
2017-12-12 18:22:29 +01:00
$together[$categoryId]['earned'] = $earnedInfo;
2017-12-11 14:52:30 +01:00
$together[$categoryId]['category'] = $earnedInfo['name'];
$together[$categoryId]['grand_total'] = '0';
2017-12-10 17:55:06 +01:00
}
2017-12-11 14:52:30 +01:00
$together[$categoryId]['grand_total'] = bcadd($earnedInfo['grand_total'], $together[$categoryId]['grand_total']);
2017-12-10 17:55:06 +01:00
}
2018-07-20 14:34:56 +02:00
try {
$result = view('reports.partials.exp-categories', compact('together'))->render();
2018-09-02 20:13:25 +02:00
// @codeCoverageIgnoreStart
2018-07-20 14:34:56 +02:00
} catch (Throwable $e) {
Log::error(sprintf('Could not render category::expenses: %s', $e->getMessage()));
2019-05-31 13:35:33 +02:00
$result = sprintf('An error prevented Firefly III from rendering: %s. Apologies.', $e->getMessage());
2018-07-20 14:34:56 +02:00
}
2018-09-02 20:13:25 +02:00
// @codeCoverageIgnoreEnd
2017-12-10 17:55:06 +01:00
$cache->store($result);
return $result;
}
2018-07-20 14:34:56 +02:00
/** @noinspection MoreThanThreeArgumentsInspection */
2017-12-10 12:00:08 +01:00
/**
2018-07-21 08:55:32 +02:00
* Overview of spending.
2017-12-11 14:52:30 +01:00
*
2017-12-10 12:00:08 +01:00
* @param Collection $accounts
* @param Collection $expense
2019-05-31 13:35:33 +02:00
* @param Carbon $start
* @param Carbon $end
2017-12-10 17:55:06 +01:00
*
* @return array|mixed|string
2017-12-10 12:00:08 +01:00
*/
2017-12-10 17:55:06 +01:00
public function spent(Collection $accounts, Collection $expense, Carbon $start, Carbon $end)
2017-12-10 12:00:08 +01:00
{
2017-12-10 17:55:06 +01:00
// chart properties for cache:
$cache = new CacheProperties;
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty('expense-spent');
$cache->addProperty($accounts->pluck('id')->toArray());
$cache->addProperty($expense->pluck('id')->toArray());
if ($cache->has()) {
return $cache->get(); // @codeCoverageIgnore
}
2017-12-10 12:00:08 +01:00
$combined = $this->combineAccounts($expense);
2017-12-10 17:55:06 +01:00
$result = [];
foreach ($combined as $name => $combi) {
/**
2017-12-22 18:32:43 +01:00
* @var string
2017-12-10 17:55:06 +01:00
* @var Collection $combi
*/
$spent = $this->spentInPeriod($accounts, $combi, $start, $end);
$earned = $this->earnedInPeriod($accounts, $combi, $start, $end);
$result[$name] = [
'spent' => $spent,
'earned' => $earned,
];
}
2018-07-20 14:34:56 +02:00
try {
$result = view('reports.partials.exp-not-grouped', compact('result'))->render();
2018-09-02 20:13:25 +02:00
// @codeCoverageIgnoreStart
2018-07-20 14:34:56 +02:00
} catch (Throwable $e) {
Log::error(sprintf('Could not render category::expenses: %s', $e->getMessage()));
2019-05-31 13:35:33 +02:00
$result = sprintf('An error prevented Firefly III from rendering: %s. Apologies.', $e->getMessage());
2018-07-20 14:34:56 +02:00
}
2018-09-02 20:13:25 +02:00
// @codeCoverageIgnoreEnd
2017-12-10 17:55:06 +01:00
$cache->store($result);
return $result;
2017-12-10 12:00:08 +01:00
// for period, get spent and earned for each account (by name)
2017-12-10 17:55:06 +01:00
}
2018-07-20 14:34:56 +02:00
/** @noinspection MoreThanThreeArgumentsInspection */
2017-12-17 14:30:53 +01:00
/**
2018-07-21 08:55:32 +02:00
* List of top expenses.
*
2017-12-17 14:30:53 +01:00
* @param Collection $accounts
* @param Collection $expense
2019-05-31 13:35:33 +02:00
* @param Carbon $start
* @param Carbon $end
2017-12-17 14:30:53 +01:00
*
* @return string
*/
2018-07-08 12:28:42 +02:00
public function topExpense(Collection $accounts, Collection $expense, Carbon $start, Carbon $end): string
2017-12-12 18:22:29 +01:00
{
// Properties for cache:
$cache = new CacheProperties;
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty('top-expense');
2017-12-12 18:22:29 +01:00
$cache->addProperty($accounts->pluck('id')->toArray());
$cache->addProperty($expense->pluck('id')->toArray());
if ($cache->has()) {
//return $cache->get(); // @codeCoverageIgnore
2017-12-12 18:22:29 +01:00
}
$combined = $this->combineAccounts($expense);
$all = new Collection;
2018-07-20 14:34:56 +02:00
foreach ($combined as $combi) {
2017-12-12 18:22:29 +01:00
$all = $all->merge($combi);
}
// get all expenses in period:
2019-05-31 13:35:33 +02:00
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
2017-12-12 18:22:29 +01:00
$collector->setRange($start, $end)->setTypes([TransactionType::WITHDRAWAL])->setAccounts($accounts);
$collector->setAccounts($all)->withAccountInformation();
$sorted = $collector->getExtractedJournals();
2019-05-31 13:35:33 +02:00
usort($sorted, function ($a, $b) {
return $a['amount'] <=> $b['amount']; // @codeCoverageIgnore
2019-05-31 13:35:33 +02:00
});
2018-07-20 14:34:56 +02:00
try {
$result = view('reports.partials.top-transactions', compact('sorted'))->render();
2018-09-02 20:13:25 +02:00
// @codeCoverageIgnoreStart
2018-07-20 14:34:56 +02:00
} catch (Throwable $e) {
2018-07-27 03:09:35 +02:00
Log::error(sprintf('Could not render category::topExpense: %s', $e->getMessage()));
2019-05-31 13:35:33 +02:00
$result = sprintf('An error prevented Firefly III from rendering: %s. Apologies.', $e->getMessage());
2018-07-20 14:34:56 +02:00
}
2018-09-02 20:13:25 +02:00
// @codeCoverageIgnoreEnd
2017-12-12 18:22:29 +01:00
$cache->store($result);
return $result;
}
2018-07-20 14:34:56 +02:00
/** @noinspection MoreThanThreeArgumentsInspection */
2017-12-17 14:30:53 +01:00
/**
2018-07-21 08:55:32 +02:00
* List of top income.
*
2017-12-17 14:30:53 +01:00
* @param Collection $accounts
* @param Collection $expense
2019-05-31 13:35:33 +02:00
* @param Carbon $start
* @param Carbon $end
2017-12-17 14:30:53 +01:00
*
* @return mixed|string
*/
2017-12-12 18:22:29 +01:00
public function topIncome(Collection $accounts, Collection $expense, Carbon $start, Carbon $end)
{
// Properties for cache:
$cache = new CacheProperties;
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty('top-income');
2017-12-12 18:22:29 +01:00
$cache->addProperty($accounts->pluck('id')->toArray());
$cache->addProperty($expense->pluck('id')->toArray());
if ($cache->has()) {
//return $cache->get(); // @codeCoverageIgnore
2017-12-12 18:22:29 +01:00
}
$combined = $this->combineAccounts($expense);
$all = new Collection;
2018-07-20 14:34:56 +02:00
foreach ($combined as $combi) {
2017-12-12 18:22:29 +01:00
$all = $all->merge($combi);
}
// get all expenses in period:
2019-05-31 13:35:33 +02:00
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
$total = $accounts->merge($all);
$collector->setRange($start, $end)->setTypes([TransactionType::DEPOSIT])->setAccounts($total)->withAccountInformation();
$sorted = $collector->getExtractedJournals();
foreach (array_keys($sorted) as $key) {
$sorted[$key]['amount'] = bcmul($sorted[$key]['amount'], '-1');
}
2019-05-31 13:35:33 +02:00
usort($sorted, function ($a, $b) {
return $a['amount'] <=> $b['amount']; // @codeCoverageIgnore
2019-05-31 13:35:33 +02:00
});
2018-07-20 14:34:56 +02:00
try {
$result = view('reports.partials.top-transactions', compact('sorted'))->render();
2018-09-02 20:13:25 +02:00
// @codeCoverageIgnoreStart
2018-07-20 14:34:56 +02:00
} catch (Throwable $e) {
2018-07-27 03:09:35 +02:00
Log::error(sprintf('Could not render category::topIncome: %s', $e->getMessage()));
2019-05-31 13:35:33 +02:00
$result = sprintf('An error prevented Firefly III from rendering: %s. Apologies.', $e->getMessage());
2018-07-20 14:34:56 +02:00
}
2018-09-02 20:13:25 +02:00
// @codeCoverageIgnoreEnd
2017-12-12 18:22:29 +01:00
$cache->store($result);
return $result;
}
2017-12-10 12:00:08 +01:00
2017-12-22 18:32:43 +01:00
}