Files
finansePRO/app/Controllers/Dashboard.php
T
2026-07-06 20:16:00 +02:00

64 lines
1.9 KiB
PHP

<?php
namespace App\Controllers;
use App\Models\InstrumentModel;
use App\Models\OperationModel;
class Dashboard extends BaseController
{
/**
* Pulpit globalny — przegląd całości: finanse bieżące (rok) + inwestycje + majątek razem.
*/
public function index()
{
$from = date('Y-01-01');
$to = date('Y-12-31');
$fin = model(OperationModel::class)->totals(['from' => $from, 'to' => $to]);
// Agregat inwestycji spójny ze źródłem prawdy w InstrumentModel::withStats.
$net = 0.0;
$val = 0.0;
foreach (model(InstrumentModel::class)->withStats() as $i) {
$net += (float) $i['net_invested'];
$val += (float) $i['value'];
}
$inv = [
'net_invested' => round($net, 2),
'value' => round($val, 2),
'profit' => round($val - $net, 2),
];
return view('dashboard/index', [
'title' => 'Pulpit',
'from' => $from,
'to' => $to,
'fin' => $fin,
'inv' => $inv,
'net_worth' => round($fin['balance'] + $val, 2),
]);
}
/**
* Pulpit finansów bieżących — wykresy przychody/wydatki/saldo.
*/
public function finances()
{
$from = $this->request->getGet('from') ?: date('Y-01-01');
$to = $this->request->getGet('to') ?: date('Y-12-31');
$model = model(OperationModel::class);
return view('dashboard/finances', [
'title' => 'Pulpit finansów',
'from' => $from,
'to' => $to,
'totals' => $model->totals(['from' => $from, 'to' => $to]),
'monthly' => $model->monthlyBalance($from, $to),
'byCat' => $model->byCategory($from, $to, 'expense'),
'running' => $model->runningBalance($from, $to),
]);
}
}