99 lines
2.9 KiB
PHP
99 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers;
|
|
|
|
use App\Models\InstrumentModel;
|
|
use App\Models\InvOperationModel;
|
|
|
|
class InvOperations extends BaseController
|
|
{
|
|
private function model(): InvOperationModel
|
|
{
|
|
return model(InvOperationModel::class);
|
|
}
|
|
|
|
private function instruments(): array
|
|
{
|
|
return model(InstrumentModel::class)->orderBy('name', 'ASC')->findAll();
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
$f = [
|
|
'from' => $this->request->getGet('from'),
|
|
'to' => $this->request->getGet('to'),
|
|
'instrument_id' => $this->request->getGet('instrument_id'),
|
|
'type' => $this->request->getGet('type'),
|
|
];
|
|
|
|
return view('inv_operations/index', [
|
|
'title' => 'Operacje inwestycyjne',
|
|
'operations' => $this->model()->filtered($f),
|
|
'instruments' => $this->instruments(),
|
|
'filter' => $f,
|
|
]);
|
|
}
|
|
|
|
public function new()
|
|
{
|
|
return view('inv_operations/form', [
|
|
'title' => 'Nowa operacja inwestycyjna',
|
|
'op' => null,
|
|
'instruments' => $this->instruments(),
|
|
]);
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
return $this->persist(null);
|
|
}
|
|
|
|
public function edit($id)
|
|
{
|
|
$op = $this->model()->find((int) $id);
|
|
if (! $op) {
|
|
return redirect()->to('/investments/operations')->with('error', 'Nie znaleziono operacji.');
|
|
}
|
|
|
|
return view('inv_operations/form', [
|
|
'title' => 'Edycja operacji inwestycyjnej',
|
|
'op' => $op,
|
|
'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/operations')->with('success', 'Operacja usunięta.');
|
|
}
|
|
|
|
private function persist(?int $id)
|
|
{
|
|
$type = (string) $this->request->getPost('type');
|
|
$data = [
|
|
'instrument_id' => (int) $this->request->getPost('instrument_id') ?: null,
|
|
'date' => (string) $this->request->getPost('date'),
|
|
'amount' => (string) $this->request->getPost('amount'),
|
|
'type' => in_array($type, ['deposit', 'withdraw'], true) ? $type : 'deposit',
|
|
'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('/investments/operations')->with('success', 'Zapisano operację.');
|
|
}
|
|
}
|