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

595 lines
22 KiB
PHP
Raw Normal View History

2017-12-10 01:56:20 -06:00
<?php
2017-12-10 02:02:26 -06: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 07:41:58 -06:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
2017-12-10 02:02:26 -06:00
*/
2017-12-10 01:56:20 -06:00
declare(strict_types=1);
namespace FireflyIII\Http\Controllers\Report;
2017-12-10 05:00:08 -06:00
use Carbon\Carbon;
2017-12-10 10:55:06 -06:00
use FireflyIII\Helpers\Collector\JournalCollectorInterface;
2017-12-10 01:56:20 -06:00
use FireflyIII\Http\Controllers\Controller;
2017-12-10 05:00:08 -06:00
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
2017-12-12 11:22:29 -06:00
use FireflyIII\Models\Transaction;
2017-12-10 10:55:06 -06:00
use FireflyIII\Models\TransactionType;
2017-12-10 05:00:08 -06:00
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
2017-12-10 10:55:06 -06:00
use FireflyIII\Support\CacheProperties;
2017-12-10 05:00:08 -06:00
use Illuminate\Support\Collection;
2017-12-10 01:56:20 -06:00
/**
* Class ExpenseController
*/
class ExpenseController extends Controller
{
2017-12-10 05:00:08 -06:00
/** @var AccountRepositoryInterface */
protected $accountRepository;
/**
2017-12-11 07:52:30 -06:00
* Constructor for ExpenseController
2017-12-10 05:00:08 -06:00
*/
public function __construct()
{
parent::__construct();
// translations:
$this->middleware(
function ($request, $next) {
$this->accountRepository = app(AccountRepositoryInterface::class);
return $next($request);
}
);
}
2017-12-10 13:03:10 -06:00
/**
2017-12-11 07:52:30 -06:00
* Generates the overview per budget.
*
2017-12-10 13:03:10 -06:00
* @param Collection $accounts
* @param Collection $expense
* @param Carbon $start
* @param Carbon $end
*
* @return string
* @throws \Throwable
*/
public function budget(Collection $accounts, Collection $expense, Carbon $start, Carbon $end)
{
// 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;
foreach ($combined as $name => $combi) {
$all = $all->merge($combi);
}
// now find spent / earned:
2017-12-12 11:22:29 -06:00
$spent = $this->spentByBudget($accounts, $all, $start, $end);
2017-12-11 07:52:30 -06:00
// join arrays somehow:
2017-12-10 13:03:10 -06:00
$together = [];
2017-12-11 07:52:30 -06:00
foreach ($spent as $categoryId => $spentInfo) {
if (!isset($together[$categoryId])) {
$together[$categoryId]['spent'] = $spentInfo;
2017-12-12 11:22:29 -06:00
$together[$categoryId]['budget'] = $spentInfo['name'];
2017-12-11 07:52:30 -06:00
$together[$categoryId]['grand_total'] = '0';
2017-12-10 13:03:10 -06:00
}
2017-12-11 07:52:30 -06:00
$together[$categoryId]['grand_total'] = bcadd($spentInfo['grand_total'], $together[$categoryId]['grand_total']);
2017-12-10 13:03:10 -06:00
}
2017-12-11 07:52:30 -06:00
unset($spentInfo);
2017-12-10 13:03:10 -06:00
$result = view('reports.partials.exp-budgets', compact('together'))->render();
$cache->store($result);
return $result;
}
/**
2017-12-11 07:52:30 -06:00
* Generates the overview per category (spent and earned).
*
2017-12-10 13:03:10 -06:00
* @param Collection $accounts
* @param Collection $expense
* @param Carbon $start
* @param Carbon $end
*
* @return string
* @throws \Throwable
*/
2017-12-10 10:55:06 -06:00
public function category(Collection $accounts, Collection $expense, Carbon $start, Carbon $end)
{
// 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;
foreach ($combined as $name => $combi) {
$all = $all->merge($combi);
}
// now find spent / earned:
$spent = $this->spentByCategory($accounts, $all, $start, $end);
2017-12-10 13:10:04 -06:00
$earned = $this->earnedByCategory($accounts, $all, $start, $end);
2017-12-10 10:55:06 -06:00
// join arrays somehow:
$together = [];
2017-12-10 13:03:10 -06:00
foreach ($spent as $categoryId => $spentInfo) {
if (!isset($together[$categoryId])) {
2017-12-11 07:52:30 -06:00
$together[$categoryId]['spent'] = $spentInfo;
$together[$categoryId]['category'] = $spentInfo['name'];
$together[$categoryId]['grand_total'] = '0';
2017-12-10 10:55:06 -06:00
}
2017-12-11 07:52:30 -06:00
$together[$categoryId]['grand_total'] = bcadd($spentInfo['grand_total'], $together[$categoryId]['grand_total']);
2017-12-10 10:55:06 -06:00
}
2017-12-11 07:52:30 -06:00
unset($spentInfo);
2017-12-10 13:03:10 -06:00
foreach ($earned as $categoryId => $earnedInfo) {
if (!isset($together[$categoryId])) {
2017-12-12 11:22:29 -06:00
$together[$categoryId]['earned'] = $earnedInfo;
2017-12-11 07:52:30 -06:00
$together[$categoryId]['category'] = $earnedInfo['name'];
$together[$categoryId]['grand_total'] = '0';
2017-12-10 10:55:06 -06:00
}
2017-12-11 07:52:30 -06:00
$together[$categoryId]['grand_total'] = bcadd($earnedInfo['grand_total'], $together[$categoryId]['grand_total']);
2017-12-10 10:55:06 -06:00
}
$result = view('reports.partials.exp-categories', compact('together'))->render();
$cache->store($result);
return $result;
}
2017-12-10 05:00:08 -06:00
/**
2017-12-11 07:52:30 -06:00
* Overview of spending
*
2017-12-10 05:00:08 -06:00
* @param Collection $accounts
* @param Collection $expense
* @param Carbon $start
* @param Carbon $end
2017-12-10 10:55:06 -06:00
*
* @return array|mixed|string
* @throws \Throwable
2017-12-10 05:00:08 -06:00
*/
2017-12-10 10:55:06 -06:00
public function spent(Collection $accounts, Collection $expense, Carbon $start, Carbon $end)
2017-12-10 05:00:08 -06:00
{
2017-12-10 10:55:06 -06: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 05:00:08 -06:00
$combined = $this->combineAccounts($expense);
2017-12-10 10:55:06 -06:00
$result = [];
foreach ($combined as $name => $combi) {
/**
* @var string $name
* @var Collection $combi
*/
$spent = $this->spentInPeriod($accounts, $combi, $start, $end);
$earned = $this->earnedInPeriod($accounts, $combi, $start, $end);
$result[$name] = [
'spent' => $spent,
'earned' => $earned,
];
}
$result = view('reports.partials.exp-not-grouped', compact('result'))->render();
$cache->store($result);
return $result;
2017-12-10 05:00:08 -06:00
// for period, get spent and earned for each account (by name)
2017-12-10 10:55:06 -06:00
}
2017-12-17 07:30:53 -06:00
/**
* @param Collection $accounts
* @param Collection $expense
* @param Carbon $start
* @param Carbon $end
*
* @return string
* @throws \Throwable
*/
2017-12-12 11:22:29 -06:00
public function topExpense(Collection $accounts, Collection $expense, Carbon $start, Carbon $end)
{
// 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;
foreach ($combined as $name => $combi) {
$all = $all->merge($combi);
}
// get all expenses in period:
/** @var JournalCollectorInterface $collector */
$collector = app(JournalCollectorInterface::class);
$collector->setRange($start, $end)->setTypes([TransactionType::WITHDRAWAL])->setAccounts($accounts);
$collector->setOpposingAccounts($all);
$set = $collector->getJournals();
$sorted = $set->sortBy(
function (Transaction $transaction) {
return floatval($transaction->transaction_amount);
}
);
$result = view('reports.partials.top-transactions', compact('sorted'))->render();
$cache->store($result);
return $result;
}
2017-12-17 07:30:53 -06:00
/**
* @param Collection $accounts
* @param Collection $expense
* @param Carbon $start
* @param Carbon $end
*
* @return mixed|string
* @throws \Throwable
*/
2017-12-12 11:22:29 -06: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('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;
foreach ($combined as $name => $combi) {
$all = $all->merge($combi);
}
// get all expenses in period:
/** @var JournalCollectorInterface $collector */
$collector = app(JournalCollectorInterface::class);
$collector->setRange($start, $end)->setTypes([TransactionType::DEPOSIT])->setAccounts($accounts);
$collector->setOpposingAccounts($all);
$set = $collector->getJournals();
$sorted = $set->sortByDesc(
function (Transaction $transaction) {
return floatval($transaction->transaction_amount);
}
);
$result = view('reports.partials.top-transactions', compact('sorted'))->render();
$cache->store($result);
return $result;
}
2017-12-10 10:55:06 -06:00
/**
* @param Collection $accounts
*
2017-12-10 22:57:07 -06:00
* @return array
2017-12-10 10:55:06 -06:00
*/
2017-12-10 05:00:08 -06:00
protected function combineAccounts(Collection $accounts): array
{
$combined = [];
/** @var Account $expenseAccount */
foreach ($accounts as $expenseAccount) {
2017-12-10 10:55:06 -06:00
$collection = new Collection;
$collection->push($expenseAccount);
2017-12-10 05:00:08 -06:00
$revenue = $this->accountRepository->findByName($expenseAccount->name, [AccountType::REVENUE]);
if (!is_null($revenue->id)) {
2017-12-10 10:55:06 -06:00
$collection->push($revenue);
2017-12-10 05:00:08 -06:00
}
2017-12-10 10:55:06 -06:00
$combined[$expenseAccount->name] = $collection;
2017-12-10 05:00:08 -06:00
}
return $combined;
}
2017-12-10 01:56:20 -06:00
2017-12-10 13:03:10 -06:00
/**
* @param Collection $assets
* @param Collection $opposing
* @param Carbon $start
* @param Carbon $end
*
2017-12-11 07:52:30 -06:00
* @return array
2017-12-10 13:03:10 -06:00
*/
protected function earnedByCategory(Collection $assets, Collection $opposing, Carbon $start, Carbon $end): array
{
/** @var JournalCollectorInterface $collector */
$collector = app(JournalCollectorInterface::class);
$collector->setRange($start, $end)->setTypes([TransactionType::DEPOSIT])->setAccounts($assets);
$collector->setOpposingAccounts($opposing)->withCategoryInformation();
$set = $collector->getJournals();
$sum = [];
// loop to support multi currency
foreach ($set as $transaction) {
$currencyId = $transaction->transaction_currency_id;
$categoryName = $transaction->transaction_category_name;
$categoryId = intval($transaction->transaction_category_id);
// if null, grab from journal:
if ($categoryId === 0) {
$categoryName = $transaction->transaction_journal_category_name;
$categoryId = intval($transaction->transaction_journal_category_id);
}
if ($categoryId !== 0) {
$categoryName = app('steam')->tryDecrypt($categoryName);
}
// if not set, set to zero:
if (!isset($sum[$categoryId][$currencyId])) {
2017-12-11 07:52:30 -06:00
$sum[$categoryId] = [
'grand_total' => '0',
'name' => $categoryName,
'per_currency' => [
$currencyId => [
'sum' => '0',
'category' => [
'id' => $categoryId,
'name' => $categoryName,
],
'currency' => [
'symbol' => $transaction->transaction_currency_symbol,
'dp' => $transaction->transaction_currency_dp,
],
],
2017-12-10 13:03:10 -06:00
],
];
2017-12-11 07:52:30 -06:00
2017-12-10 13:03:10 -06:00
}
// add amount
2017-12-11 07:52:30 -06:00
$sum[$categoryId]['per_currency'][$currencyId]['sum'] = bcadd(
$sum[$categoryId]['per_currency'][$currencyId]['sum'], $transaction->transaction_amount
);
$sum[$categoryId]['grand_total'] = bcadd($sum[$categoryId]['grand_total'], $transaction->transaction_amount);
2017-12-10 13:03:10 -06:00
}
return $sum;
}
2017-12-17 07:30:53 -06:00
/**
* @param Collection $assets
* @param Collection $opposing
* @param Carbon $start
* @param Carbon $end
*
* @return array
*/
2017-12-10 10:55:06 -06:00
protected function earnedInPeriod(Collection $assets, Collection $opposing, Carbon $start, Carbon $end): array
{
/** @var JournalCollectorInterface $collector */
$collector = app(JournalCollectorInterface::class);
$collector->setRange($start, $end)->setTypes([TransactionType::DEPOSIT])->setAccounts($assets);
$collector->setOpposingAccounts($opposing);
$set = $collector->getJournals();
2017-12-11 07:52:30 -06:00
$sum = [
'grand_sum' => '0',
'per_currency' => [],
];
2017-12-10 10:55:06 -06:00
// loop to support multi currency
foreach ($set as $transaction) {
$currencyId = $transaction->transaction_currency_id;
// if not set, set to zero:
2017-12-11 07:52:30 -06:00
if (!isset($sum['per_currency'][$currencyId])) {
$sum['per_currency'][$currencyId] = [
2017-12-10 10:55:06 -06:00
'sum' => '0',
'currency' => [
'symbol' => $transaction->transaction_currency_symbol,
'dp' => $transaction->transaction_currency_dp,
],
];
}
// add amount
2017-12-11 07:52:30 -06:00
$sum['per_currency'][$currencyId]['sum'] = bcadd($sum['per_currency'][$currencyId]['sum'], $transaction->transaction_amount);
$sum['grand_sum'] = bcadd($sum['grand_sum'], $transaction->transaction_amount);
2017-12-10 10:55:06 -06:00
}
return $sum;
}
/**
* @param Collection $assets
* @param Collection $opposing
* @param Carbon $start
* @param Carbon $end
*
2017-12-10 13:03:10 -06:00
* @return array
2017-12-10 10:55:06 -06:00
*/
2017-12-10 13:03:10 -06:00
protected function spentByBudget(Collection $assets, Collection $opposing, Carbon $start, Carbon $end): array
2017-12-10 10:55:06 -06:00
{
/** @var JournalCollectorInterface $collector */
$collector = app(JournalCollectorInterface::class);
2017-12-10 13:03:10 -06:00
$collector->setRange($start, $end)->setTypes([TransactionType::WITHDRAWAL])->setAccounts($assets);
$collector->setOpposingAccounts($opposing)->withBudgetInformation();
2017-12-10 10:55:06 -06:00
$set = $collector->getJournals();
$sum = [];
// loop to support multi currency
foreach ($set as $transaction) {
2017-12-12 11:22:29 -06:00
$currencyId = $transaction->transaction_currency_id;
2017-12-10 13:03:10 -06:00
$budgetName = $transaction->transaction_budget_name;
$budgetId = intval($transaction->transaction_budget_id);
2017-12-10 10:55:06 -06:00
// if null, grab from journal:
2017-12-10 13:03:10 -06:00
if ($budgetId === 0) {
$budgetName = $transaction->transaction_journal_budget_name;
$budgetId = intval($transaction->transaction_journal_budget_id);
2017-12-10 10:55:06 -06:00
}
2017-12-10 13:05:56 -06:00
if ($budgetId !== 0) {
$budgetName = app('steam')->tryDecrypt($budgetName);
}
2017-12-10 10:55:06 -06:00
// if not set, set to zero:
2017-12-10 13:03:10 -06:00
if (!isset($sum[$budgetId][$currencyId])) {
2017-12-11 07:52:30 -06:00
$sum[$budgetId] = [
'grand_total' => '0',
'name' => $budgetName,
'per_currency' => [
$currencyId => [
'sum' => '0',
2017-12-12 11:22:29 -06:00
'budget' => [
2017-12-11 07:52:30 -06:00
'id' => $budgetId,
'name' => $budgetName,
],
'currency' => [
'symbol' => $transaction->transaction_currency_symbol,
'dp' => $transaction->transaction_currency_dp,
],
],
2017-12-10 10:55:06 -06:00
],
];
}
// add amount
2017-12-11 07:52:30 -06:00
$sum[$budgetId]['per_currency'][$currencyId]['sum'] = bcadd(
$sum[$budgetId]['per_currency'][$currencyId]['sum'], $transaction->transaction_amount
);
$sum[$budgetId]['grand_total'] = bcadd($sum[$budgetId]['grand_total'], $transaction->transaction_amount);
2017-12-10 10:55:06 -06:00
}
return $sum;
}
/**
* @param Collection $assets
* @param Collection $opposing
* @param Carbon $start
* @param Carbon $end
*
2017-12-11 07:52:30 -06:00
* @return array
2017-12-10 10:55:06 -06:00
*/
protected function spentByCategory(Collection $assets, Collection $opposing, Carbon $start, Carbon $end): array
{
/** @var JournalCollectorInterface $collector */
$collector = app(JournalCollectorInterface::class);
$collector->setRange($start, $end)->setTypes([TransactionType::WITHDRAWAL])->setAccounts($assets);
$collector->setOpposingAccounts($opposing)->withCategoryInformation();
$set = $collector->getJournals();
$sum = [];
// loop to support multi currency
foreach ($set as $transaction) {
$currencyId = $transaction->transaction_currency_id;
$categoryName = $transaction->transaction_category_name;
$categoryId = intval($transaction->transaction_category_id);
// if null, grab from journal:
if ($categoryId === 0) {
$categoryName = $transaction->transaction_journal_category_name;
$categoryId = intval($transaction->transaction_journal_category_id);
}
2017-12-10 13:05:56 -06:00
if ($categoryId !== 0) {
$categoryName = app('steam')->tryDecrypt($categoryName);
}
2017-12-10 10:55:06 -06:00
// if not set, set to zero:
if (!isset($sum[$categoryId][$currencyId])) {
2017-12-11 07:52:30 -06:00
$sum[$categoryId] = [
'grand_total' => '0',
'name' => $categoryName,
'per_currency' => [
$currencyId => [
'sum' => '0',
'category' => [
'id' => $categoryId,
'name' => $categoryName,
],
'currency' => [
'symbol' => $transaction->transaction_currency_symbol,
'dp' => $transaction->transaction_currency_dp,
],
],
2017-12-10 10:55:06 -06:00
],
];
}
// add amount
2017-12-11 07:52:30 -06:00
$sum[$categoryId]['per_currency'][$currencyId]['sum'] = bcadd(
$sum[$categoryId]['per_currency'][$currencyId]['sum'], $transaction->transaction_amount
);
$sum[$categoryId]['grand_total'] = bcadd($sum[$categoryId]['grand_total'], $transaction->transaction_amount);
2017-12-10 10:55:06 -06:00
}
return $sum;
}
/**
* @param Collection $assets
* @param Collection $opposing
* @param Carbon $start
* @param Carbon $end
*
2017-12-11 07:52:30 -06:00
* @return array
2017-12-10 10:55:06 -06:00
*/
protected function spentInPeriod(Collection $assets, Collection $opposing, Carbon $start, Carbon $end): array
{
/** @var JournalCollectorInterface $collector */
$collector = app(JournalCollectorInterface::class);
$collector->setRange($start, $end)->setTypes([TransactionType::WITHDRAWAL])->setAccounts($assets);
$collector->setOpposingAccounts($opposing);
$set = $collector->getJournals();
2017-12-11 07:52:30 -06:00
$sum = [
'grand_sum' => '0',
'per_currency' => [],
];
2017-12-10 10:55:06 -06:00
// loop to support multi currency
foreach ($set as $transaction) {
$currencyId = $transaction->transaction_currency_id;
// if not set, set to zero:
2017-12-11 07:52:30 -06:00
if (!isset($sum['per_currency'][$currencyId])) {
$sum['per_currency'][$currencyId] = [
2017-12-10 10:55:06 -06:00
'sum' => '0',
'currency' => [
'symbol' => $transaction->transaction_currency_symbol,
'dp' => $transaction->transaction_currency_dp,
],
];
}
// add amount
2017-12-11 07:52:30 -06:00
$sum['per_currency'][$currencyId]['sum'] = bcadd($sum['per_currency'][$currencyId]['sum'], $transaction->transaction_amount);
$sum['grand_sum'] = bcadd($sum['grand_sum'], $transaction->transaction_amount);
2017-12-10 10:55:06 -06:00
}
return $sum;
}
2017-12-10 01:56:20 -06:00
}