Bicycle/App/Controller/AnalyticsController.php
2026-08-15 10:39:22 +03:00

72 lines
2.6 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* @package Bicycle
* @author Egor Isaev
* @description AnalyticsController.php
* @copyright (c) 14/08/2026
*/
namespace App\Controller;
use App\Repositories\BudgetRepository;
use App\Repositories\TransactionRepository;
use App\Repositories\UserRepository;
use Services\Auth;
use System\Classes\Controller;
use System\Classes\HTTP\HTTPException;
use System\Classes\MyException;
use System\Classes\Request;
/**
* Аналитика — доходы/расходы по месяцам за год, структура расходов за текущий месяц, план vs факт,
* прогноз до конца года, сравнение с прошлым годом по кварталам (5 панелей дизайн-хендоффа
* App/design_handoff_personal_finance, без сторонней chart-библиотеки — сырой SVG/conic-gradient
* прямо в App/view/Analytics/index.html, как и в самом хендоффе).
*/
class AnalyticsController extends Controller
{
protected bool $_auth_protection = true;
/**
* @return string
* @throws HTTPException|MyException
*/
public function indexAction(): string
{
$auth_user = Auth::instance()->getUser();
$user = (new UserRepository())->getByLogin($auth_user['login']);
if (!$user) {
throw HTTPException::factory(404);
}
$year = (int)(Request::$current->query('year') ?: date('Y'));
$month = (int)date('n');
$tx_repo = new TransactionRepository();
$budget_repo = new BudgetRepository();
$monthly = $tx_repo->getMonthlyTotals($user->id, $year);
$monthly_prev = $tx_repo->getMonthlyTotals($user->id, $year - 1);
$expense_cats = $tx_repo->getMonthExpenseByCategory($user->id, $year, $month);
// План текущего месяца по тем же expense-категориям, что и факт (для панели «План vs факт»).
$plan_year = $budget_repo->getYear($user->id, $year);
$plan_month = [];
foreach ($expense_cats as $cat) {
$plan_month[(int)$cat->categorie_id] = $plan_year[(int)$cat->categorie_id][$month]['plan_summ'] ?? 0.0;
}
return $this->render('index', [
'user' => $auth_user,
'year' => $year,
'month' => $month,
'monthly' => $monthly,
'monthly_prev' => $monthly_prev,
'expense_cats' => $expense_cats,
'plan_month' => $plan_month,
]);
}
}