$this->request->getGet('from'), 'to' => $this->request->getGet('to'), 'category_id' => $this->request->getGet('category_id'), 'type' => $this->request->getGet('type'), ]; $perPage = 10; $total = $this->model()->countFiltered($f); $pages = max(1, (int) ceil($total / $perPage)); $page = min($pages, max(1, (int) $this->request->getGet('page'))); return view('operations/index', [ 'title' => 'Operacje', 'operations' => $this->model()->filtered($f, $perPage, $page), 'totals' => $this->model()->totals($f), 'categories' => model(CategoryModel::class)->tree(), 'filter' => $f, 'page' => $page, 'pages' => $pages, 'total' => $total, ]); } 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ę.'); } }