91 lines
2.3 KiB
PHP
91 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers;
|
|
|
|
use App\Models\InstrumentModel;
|
|
use App\Models\ValuationModel;
|
|
|
|
class Valuations extends BaseController
|
|
{
|
|
private function model(): ValuationModel
|
|
{
|
|
return model(ValuationModel::class);
|
|
}
|
|
|
|
private function instruments(): array
|
|
{
|
|
return model(InstrumentModel::class)->orderBy('name', 'ASC')->findAll();
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
$f = ['instrument_id' => $this->request->getGet('instrument_id')];
|
|
|
|
return view('valuations/index', [
|
|
'title' => 'Wyceny',
|
|
'valuations' => $this->model()->filtered($f),
|
|
'instruments' => $this->instruments(),
|
|
'filter' => $f,
|
|
]);
|
|
}
|
|
|
|
public function new()
|
|
{
|
|
return view('valuations/form', [
|
|
'title' => 'Nowa wycena',
|
|
'val' => null,
|
|
'instruments' => $this->instruments(),
|
|
]);
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
return $this->persist(null);
|
|
}
|
|
|
|
public function edit($id)
|
|
{
|
|
$val = $this->model()->find((int) $id);
|
|
if (! $val) {
|
|
return redirect()->to('/investments/valuations')->with('error', 'Nie znaleziono wyceny.');
|
|
}
|
|
|
|
return view('valuations/form', [
|
|
'title' => 'Edycja wyceny',
|
|
'val' => $val,
|
|
'instruments' => $this->instruments(),
|
|
]);
|
|
}
|
|
|
|
public function update($id)
|
|
{
|
|
return $this->persist((int) $id);
|
|
}
|
|
|
|
public function delete($id)
|
|
{
|
|
$this->model()->delete((int) $id);
|
|
|
|
return redirect()->to('/investments/valuations')->with('success', 'Wycena usunięta.');
|
|
}
|
|
|
|
private function persist(?int $id)
|
|
{
|
|
$data = [
|
|
'instrument_id' => (int) $this->request->getPost('instrument_id') ?: null,
|
|
'date' => (string) $this->request->getPost('date'),
|
|
'value' => (string) $this->request->getPost('value'),
|
|
];
|
|
|
|
$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('/investments/valuations')->with('success', 'Zapisano wycenę.');
|
|
}
|
|
}
|