first commit

This commit is contained in:
2026-07-06 20:16:00 +02:00
commit 352e6d9e22
147 changed files with 12242 additions and 0 deletions
+104
View File
@@ -0,0 +1,104 @@
<?php
namespace App\Controllers;
use App\Models\CategoryModel;
use App\Models\OperationModel;
class Operations extends BaseController
{
private function model(): OperationModel
{
return model(OperationModel::class);
}
public function index()
{
$f = [
'from' => $this->request->getGet('from'),
'to' => $this->request->getGet('to'),
'category_id' => $this->request->getGet('category_id'),
'type' => $this->request->getGet('type'),
];
return view('operations/index', [
'title' => 'Operacje',
'operations' => $this->model()->filtered($f),
'totals' => $this->model()->totals($f),
'categories' => model(CategoryModel::class)->tree(),
'filter' => $f,
]);
}
public function new()
{
return view('operations/form', [
'title' => 'Nowa operacja',
'op' => null,
'categories' => model(CategoryModel::class)->tree(),
]);
}
public function create()
{
return $this->persist(null);
}
public function edit($id)
{
$op = $this->model()->find((int) $id);
if (! $op) {
return redirect()->to('/operations')->with('error', 'Nie znaleziono operacji.');
}
return view('operations/form', [
'title' => 'Edycja operacji',
'op' => $op,
'categories' => model(CategoryModel::class)->tree(),
]);
}
public function update($id)
{
return $this->persist((int) $id);
}
public function delete($id)
{
$this->model()->delete((int) $id);
return redirect()->to('/operations')->with('success', 'Operacja usunięta.');
}
private function persist(?int $id)
{
$categoryId = $this->request->getPost('category_id') ?: null;
$type = (string) $this->request->getPost('type');
// Zrodlo prawdy dla typu: wybrana kategoria narzuca typ operacji.
if ($categoryId !== null) {
$cat = model(CategoryModel::class)->find((int) $categoryId);
if ($cat) {
$type = $cat['type'];
}
}
$data = [
'date' => (string) $this->request->getPost('date'),
'amount' => (string) $this->request->getPost('amount'),
'type' => $type,
'category_id' => $categoryId,
'description' => trim((string) $this->request->getPost('description')) ?: null,
];
$model = $this->model();
$ok = $id !== null ? $model->update($id, $data) : $model->insert($data);
if ($ok === false) {
return redirect()->back()->withInput()
->with('error', implode(' ', $model->errors()));
}
return redirect()->to('/operations')->with('success', 'Zapisano operację.');
}
}