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

148 lines
6.0 KiB
PHP
Raw Normal View History

2016-10-26 19:45:10 +02:00
<?php
/**
* BalanceController.php
2020-01-31 07:32:04 +01:00
* Copyright (c) 2019 james@firefly-iii.org
2016-10-26 19:45:10 +02:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2016-10-26 19:45:10 +02:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
2017-10-21 08:40:00 +02:00
*
* This program is distributed in the hope that it will be useful,
2017-10-21 08:40:00 +02:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
2017-10-21 08:40:00 +02:00
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2016-10-26 19:45:10 +02:00
*/
declare(strict_types=1);
2016-10-26 19:45:10 +02:00
namespace FireflyIII\Http\Controllers\Report;
use Carbon\Carbon;
2019-09-04 16:07:44 +02:00
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
2016-10-26 19:45:10 +02:00
use FireflyIII\Http\Controllers\Controller;
2019-09-04 16:07:44 +02:00
use FireflyIII\Models\Account;
use FireflyIII\Models\Budget;
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
2016-10-26 19:45:10 +02:00
use Illuminate\Support\Collection;
2018-08-07 19:29:40 +02:00
use Log;
use Throwable;
2016-10-26 19:45:10 +02:00
/**
2017-11-15 12:25:49 +01:00
* Class BalanceController.
2016-10-26 19:45:10 +02:00
*/
class BalanceController extends Controller
{
2019-09-04 16:07:44 +02:00
/** @var BudgetRepositoryInterface */
private $repository;
/**
* BalanceController constructor.
*/
public function __construct()
{
parent::__construct();
$this->middleware(
2019-09-06 17:19:03 +02:00
function ($request, $next) {
2019-09-04 16:07:44 +02:00
$this->repository = app(BudgetRepositoryInterface::class);
return $next($request);
}
);
}
2021-03-28 11:46:23 +02:00
2016-10-26 19:45:10 +02:00
/**
2018-07-21 08:55:32 +02:00
* Show overview of budget balances.
*
2018-07-08 12:08:53 +02:00
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
2016-10-26 19:45:10 +02:00
*
2021-05-24 08:54:58 +02:00
* @return string
2016-10-26 19:45:10 +02:00
*/
2018-07-08 12:08:53 +02:00
public function general(Collection $accounts, Carbon $start, Carbon $end)
2016-10-26 19:45:10 +02:00
{
2019-09-04 16:07:44 +02:00
$report = [
'budgets' => [],
'accounts' => [],
];
/** @var Account $account */
foreach ($accounts as $account) {
$report['accounts'][$account->id] = [
'id' => $account->id,
'name' => $account->name,
'iban' => $account->iban,
'sum' => '0',
];
2016-10-26 19:45:10 +02:00
}
2019-09-04 16:07:44 +02:00
$budgets = $this->repository->getBudgets();
/** @var Budget $budget */
foreach ($budgets as $budget) {
$budgetId = $budget->id;
$report['budgets'][$budgetId] = [
'budget_id' => $budgetId,
'budget_name' => $budget->name,
'spent' => [], // per account
'sums' => [], // per currency
];
$spent = [];
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
$journals = $collector->setRange($start, $end)->setSourceAccounts($accounts)->setTypes([TransactionType::WITHDRAWAL])->setBudget($budget)
->getExtractedJournals();
/** @var array $journal */
foreach ($journals as $journal) {
$sourceAccount = $journal['source_account_id'];
$currencyId = $journal['currency_id'];
$spent[$sourceAccount] = $spent[$sourceAccount] ?? [
'source_account_id' => $sourceAccount,
'currency_id' => $journal['currency_id'],
'currency_code' => $journal['currency_code'],
'currency_name' => $journal['currency_name'],
'currency_symbol' => $journal['currency_symbol'],
'currency_decimal_places' => $journal['currency_decimal_places'],
'spent' => '0',
];
$spent[$sourceAccount]['spent'] = bcadd($spent[$sourceAccount]['spent'], $journal['amount']);
// also fix sum:
$report['sums'][$budgetId][$currencyId] = $report['sums'][$budgetId][$currencyId] ?? [
'sum' => '0',
'currency_id' => $journal['currency_id'],
'currency_code' => $journal['currency_code'],
'currency_name' => $journal['currency_name'],
'currency_symbol' => $journal['currency_symbol'],
'currency_decimal_places' => $journal['currency_decimal_places'],
];
$report['sums'][$budgetId][$currencyId]['sum'] = bcadd($report['sums'][$budgetId][$currencyId]['sum'], $journal['amount']);
$report['accounts'][$sourceAccount]['sum'] = bcadd($report['accounts'][$sourceAccount]['sum'], $journal['amount']);
// add currency info for account sum
$report['accounts'][$sourceAccount]['currency_id'] = $journal['currency_id'];
$report['accounts'][$sourceAccount]['currency_code'] = $journal['currency_code'];
$report['accounts'][$sourceAccount]['currency_name'] = $journal['currency_name'];
$report['accounts'][$sourceAccount]['currency_symbol'] = $journal['currency_symbol'];
$report['accounts'][$sourceAccount]['currency_decimal_places'] = $journal['currency_decimal_places'];
}
$report['budgets'][$budgetId]['spent'] = $spent;
// get transactions in budget
}
2019-08-17 12:08:09 +02:00
try {
$result = view('reports.partials.balance', compact('report'))->render();
2022-10-31 05:40:59 +01:00
} catch (Throwable $e) {
2019-08-17 12:08:09 +02:00
Log::debug(sprintf('Could not render reports.partials.balance: %s', $e->getMessage()));
$result = 'Could not render view.';
}
2016-10-26 19:45:10 +02:00
return $result;
}
}