2016-10-25 18:53:54 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2016-12-06 08:59:08 +01:00
|
|
|
* OperationsController.php
|
2019-10-02 06:37:26 +02:00
|
|
|
* Copyright (c) 2019 thegrumpydictator@gmail.com
|
2016-10-25 18:53:54 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
2016-10-25 18:53:54 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +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
|
|
|
*
|
2019-10-02 06:37:26 +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
|
2019-10-02 06:37:26 +02:00
|
|
|
* GNU Affero General Public License for more details.
|
2017-10-21 08:40:00 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +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-25 18:53:54 +02:00
|
|
|
*/
|
2017-04-09 07:44:22 +02:00
|
|
|
declare(strict_types=1);
|
2016-10-25 18:53:54 +02:00
|
|
|
|
|
|
|
namespace FireflyIII\Http\Controllers\Report;
|
|
|
|
|
|
|
|
use Carbon\Carbon;
|
|
|
|
use FireflyIII\Http\Controllers\Controller;
|
2017-04-17 08:31:42 +02:00
|
|
|
use FireflyIII\Repositories\Account\AccountTaskerInterface;
|
2016-10-26 16:46:43 +02:00
|
|
|
use FireflyIII\Support\CacheProperties;
|
2016-10-25 18:53:54 +02:00
|
|
|
use Illuminate\Support\Collection;
|
2018-08-07 19:29:40 +02:00
|
|
|
use Log;
|
|
|
|
use Throwable;
|
2016-10-25 18:53:54 +02:00
|
|
|
|
|
|
|
/**
|
2017-11-15 12:25:49 +01:00
|
|
|
* Class OperationsController.
|
2016-10-25 18:53:54 +02:00
|
|
|
*/
|
2016-12-06 08:59:08 +01:00
|
|
|
class OperationsController extends Controller
|
2016-10-25 18:53:54 +02:00
|
|
|
{
|
2018-07-08 12:08:53 +02:00
|
|
|
|
2018-07-21 08:55:32 +02:00
|
|
|
/** @var AccountTaskerInterface Some specific account things. */
|
2018-07-08 12:08:53 +02:00
|
|
|
private $tasker;
|
|
|
|
|
|
|
|
/**
|
2018-08-04 17:30:06 +02:00
|
|
|
* OperationsController constructor.
|
2019-09-04 17:39:39 +02:00
|
|
|
*
|
2019-07-01 20:22:35 +02:00
|
|
|
* @codeCoverageIgnore
|
2018-07-08 12:08:53 +02:00
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
|
|
|
|
// translations:
|
|
|
|
$this->middleware(
|
|
|
|
function ($request, $next) {
|
|
|
|
$this->tasker = app(AccountTaskerInterface::class);
|
|
|
|
|
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-10-26 16:46:43 +02:00
|
|
|
/**
|
2018-07-21 08:55:32 +02:00
|
|
|
* View of income and expense.
|
|
|
|
*
|
2018-07-08 12:08:53 +02:00
|
|
|
* @param Collection $accounts
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
2016-10-26 16:46:43 +02:00
|
|
|
*
|
2016-12-06 08:59:08 +01:00
|
|
|
* @return mixed|string
|
2016-10-26 16:46:43 +02:00
|
|
|
*/
|
2018-07-08 12:08:53 +02:00
|
|
|
public function expenses(Collection $accounts, Carbon $start, Carbon $end)
|
2016-10-25 18:53:54 +02:00
|
|
|
{
|
2016-10-26 16:46:43 +02:00
|
|
|
// chart properties for cache:
|
|
|
|
$cache = new CacheProperties;
|
|
|
|
$cache->addProperty($start);
|
|
|
|
$cache->addProperty($end);
|
2016-11-02 20:45:11 +01:00
|
|
|
$cache->addProperty('expense-report');
|
2016-10-26 16:46:43 +02:00
|
|
|
$cache->addProperty($accounts->pluck('id')->toArray());
|
|
|
|
if ($cache->has()) {
|
2017-03-04 11:19:44 +01:00
|
|
|
return $cache->get(); // @codeCoverageIgnore
|
2016-11-02 20:45:11 +01:00
|
|
|
}
|
2019-08-16 06:21:10 +02:00
|
|
|
$report = $this->tasker->getExpenseReport($start, $end, $accounts);
|
|
|
|
$type = 'expense-entry';
|
2018-08-07 19:29:40 +02:00
|
|
|
try {
|
2019-08-16 06:21:10 +02:00
|
|
|
$result = view('reports.partials.income-expenses', compact('report', 'type'))->render();
|
2018-09-02 20:13:25 +02:00
|
|
|
// @codeCoverageIgnoreStart
|
2018-08-07 19:29:40 +02:00
|
|
|
} catch (Throwable $e) {
|
|
|
|
Log::debug(sprintf('Could not render reports.partials.income-expense: %s', $e->getMessage()));
|
|
|
|
$result = 'Could not render view.';
|
|
|
|
}
|
2018-09-02 20:13:25 +02:00
|
|
|
// @codeCoverageIgnoreEnd
|
2016-11-02 20:45:11 +01:00
|
|
|
$cache->store($result);
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2016-12-14 18:59:12 +01:00
|
|
|
/**
|
2018-07-21 08:55:32 +02:00
|
|
|
* View of income.
|
|
|
|
*
|
2018-07-08 12:08:53 +02:00
|
|
|
* @param Collection $accounts
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
2016-12-14 18:59:12 +01:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2018-07-08 12:28:42 +02:00
|
|
|
public function income(Collection $accounts, Carbon $start, Carbon $end): string
|
2016-12-14 18:59:12 +01:00
|
|
|
{
|
|
|
|
// chart properties for cache:
|
|
|
|
$cache = new CacheProperties;
|
|
|
|
$cache->addProperty($start);
|
|
|
|
$cache->addProperty($end);
|
|
|
|
$cache->addProperty('income-report');
|
|
|
|
$cache->addProperty($accounts->pluck('id')->toArray());
|
|
|
|
if ($cache->has()) {
|
2017-03-04 11:19:44 +01:00
|
|
|
return $cache->get(); // @codeCoverageIgnore
|
2016-12-14 18:59:12 +01:00
|
|
|
}
|
2019-08-16 06:21:10 +02:00
|
|
|
$report = $this->tasker->getIncomeReport($start, $end, $accounts);
|
|
|
|
$type = 'income-entry';
|
2018-08-07 19:29:40 +02:00
|
|
|
try {
|
2019-08-16 06:21:10 +02:00
|
|
|
$result = view('reports.partials.income-expenses', compact('report', 'type'))->render();
|
2018-09-02 20:13:25 +02:00
|
|
|
// @codeCoverageIgnoreStart
|
2018-08-07 19:29:40 +02:00
|
|
|
} catch (Throwable $e) {
|
|
|
|
Log::debug(sprintf('Could not render reports.partials.income-expenses: %s', $e->getMessage()));
|
|
|
|
$result = 'Could not render view.';
|
|
|
|
}
|
2018-09-02 20:13:25 +02:00
|
|
|
// @codeCoverageIgnoreEnd
|
2016-12-14 18:59:12 +01:00
|
|
|
$cache->store($result);
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2016-11-02 20:45:11 +01:00
|
|
|
/**
|
2018-07-21 08:55:32 +02:00
|
|
|
* Overview of income and expense.
|
|
|
|
*
|
2018-07-08 12:08:53 +02:00
|
|
|
* @param Collection $accounts
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
2016-11-02 20:45:11 +01:00
|
|
|
*
|
2016-12-06 08:59:08 +01:00
|
|
|
* @return mixed|string
|
2016-11-02 20:45:11 +01:00
|
|
|
*/
|
2018-07-08 12:08:53 +02:00
|
|
|
public function operations(Collection $accounts, Carbon $start, Carbon $end)
|
2016-11-02 20:45:11 +01:00
|
|
|
{
|
|
|
|
// chart properties for cache:
|
|
|
|
$cache = new CacheProperties;
|
|
|
|
$cache->addProperty($start);
|
|
|
|
$cache->addProperty($end);
|
|
|
|
$cache->addProperty('inc-exp-report');
|
|
|
|
$cache->addProperty($accounts->pluck('id')->toArray());
|
|
|
|
if ($cache->has()) {
|
2017-03-04 11:19:44 +01:00
|
|
|
return $cache->get(); // @codeCoverageIgnore
|
2016-10-26 16:46:43 +02:00
|
|
|
}
|
|
|
|
|
2019-08-16 06:21:10 +02:00
|
|
|
$incomes = $this->tasker->getIncomeReport($start, $end, $accounts);
|
|
|
|
$expenses = $this->tasker->getExpenseReport($start, $end, $accounts);
|
|
|
|
$sums = [];
|
|
|
|
$keys = array_unique(array_merge(array_keys($incomes['sums']), array_keys($expenses['sums'])));
|
|
|
|
|
|
|
|
/** @var int $currencyId */
|
|
|
|
foreach ($keys as $currencyId) {
|
|
|
|
$currencyInfo = $incomes['sums'][$currencyId] ?? $expenses['sums'][$currencyId];
|
|
|
|
$sums[$currencyId] = $sums[$currencyId] ?? [
|
|
|
|
'currency_id' => $currencyId,
|
|
|
|
'currency_name' => $currencyInfo['currency_name'],
|
|
|
|
'currency_code' => $currencyInfo['currency_code'],
|
|
|
|
'currency_symbol' => $currencyInfo['currency_symbol'],
|
|
|
|
'currency_decimal_places' => $currencyInfo['currency_decimal_places'],
|
|
|
|
'in' => $incomes['sums'][$currencyId]['sum'] ?? '0',
|
|
|
|
'out' => $expenses['sums'][$currencyId]['sum'] ?? '0',
|
|
|
|
'sum' => '0',
|
|
|
|
];
|
|
|
|
$sums[$currencyId]['sum'] = bcadd($sums[$currencyId]['in'], $sums[$currencyId]['out']);
|
|
|
|
}
|
2016-12-05 20:35:13 +01:00
|
|
|
|
2019-09-04 17:39:39 +02:00
|
|
|
try {
|
2019-08-16 06:21:10 +02:00
|
|
|
$result = view('reports.partials.operations', compact('sums'))->render();
|
2019-09-04 17:39:39 +02:00
|
|
|
} catch (Throwable $e) {
|
|
|
|
Log::debug(sprintf('Could not render reports.partials.operations: %s', $e->getMessage()));
|
|
|
|
$result = 'Could not render view.';
|
|
|
|
}
|
2016-11-02 20:45:11 +01:00
|
|
|
$cache->store($result);
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
2016-12-22 19:42:45 +01:00
|
|
|
}
|