service = $service; } public static function middleware(): array { return [ //new Middleware('permission:/agriculture/kotoran_ternak'), ]; } public function grid() { $_data = []; $data = $this->service->getAll(); foreach ($data as $key => $row) { $btn = 'Edit'; $btn .= '
'; if ($row->active_status == 0) { $btn .= ' '; } if ($row->active_status == 1) { $status = 'Aktif'; } else { $status = 'Tidak Aktif'; } $_data[] = [ 'no' => $key+1, 'no_baris' => @$row->row_num, 'ef_direct_n2o_n' => $row->ef_direct_n2o_n === 0 ? 0 : getFormattedValue($row->ef_direct_n2o_n, 2), 'ef_evaporation_n' => $row->ef_evaporation_n === 0 ? 0 : getFormattedValue($row->ef_evaporation_n), 'name' => $row->name, 'description' => $row->description, 'status' => $status, 'action' => @$btn, ]; } return response()->json($_data); } public function index(Request $request) { $data['route'] = $this->route; $data['title'] = $this->title; return view($this->template.'.index',$data); } public function create() { $data['route'] = $this->route; $data['title'] = $this->title; return view($this->template.'.create',$data); } public function store(Request $request) { $request->validate([ 'name' => 'required|string|max:255', 'ef_direct_n2o_n' => 'nullable', 'ef_evaporation_n' => 'nullable' ]); try { $nextRowNum = LivestockManure::max('row_num') + 1; $formattedEf1 = getOriginalValue($request->input('ef_direct_n2o_n')); $formattedEf2 = getOriginalValue($request->input('ef_evaporation_n')); $data = array_merge($request->all(), [ 'row_num' => $nextRowNum, 'active_status' => 0, 'ef_direct_n2o_n' => $formattedEf1, 'ef_evaporation_n' => $formattedEf2, ]); $this->service->create($data); return redirect()->route($this->route.'.index')->with('success', 'Jenis Pengelolaan Kotoran Ternak berhasil ditambahkan.'); } catch (\Exception $e) { return back()->withErrors(['error' => 'Jenis Pengelolaan Kotoran Ternak gagal disimpan. Silakan coba lagi. Error: ' . $e->getMessage()]); } } public function edit($id) { $data['lm'] = $this->service->find($id); $data['route'] = $this->route; $data['title'] = $this->title; return view($this->template.'.edit', $data); } public function update(Request $request, LivestockManure $kotoran_ternak) { $request->validate([ // 'code' => [ // 'required', // 'string', // 'max:255', // new UniqueInSchema('activity', 'livestock_manure', 'code', $row->id), // ], 'name' => 'required|string|max:255', 'ef_direct_n2o_n' => 'nullable', 'ef_evaporation_n' => 'nullable', 'row_num' => 'required|numeric', ]); try { $formattedEf1 = getOriginalValue($request->input('ef_direct_n2o_n')); $formattedEf2 = getOriginalValue($request->input('ef_evaporation_n')); $data = array_merge($request->all(), [ 'ef_direct_n2o_n' => $formattedEf1, 'ef_evaporation_n' => $formattedEf2, ]); $this->service->update($kotoran_ternak, $data); return redirect()->route($this->route.'.index')->with('success', 'Jenis Pengelolaan Kotoran Ternak berhasil diperbarui.'); } catch (\Exception $e) { return back()->withErrors(['error' => 'Jenis Pengelolaan Kotoran Ternak gagal diperbarui. Silakan coba lagi. Error: ' . $e->getMessage()]); } } public function destroy(LivestockManure $kotoran_ternak) { try { $this->service->delete($kotoran_ternak); $records = LivestockManure::orderBy('row_num')->get(); foreach ($records as $index => $record) { $record->row_num = $index + 1; $record->save(); } return redirect()->route($this->route.'.index')->with('success', 'Jenis Pengelolaan Kotoran Ternak berhasil dihapus.'); } catch (\Exception $e) { return back()->withErrors(['error' => 'Jenis Pengelolaan Kotoran Ternak gagal dihapus. Silakan coba lagi. Error: ' . $e->getMessage()]); } } public function setAktif($id) { try { $this->service->setAktif($id); return redirect()->route($this->route.'.index')->with('success', 'Jenis Pengelolaan Kotoran Ternak berhasil diaktifkan.'); } catch (\Exception $e) { return back()->withErrors(['error' => 'Gagal mengaktifkan Jenis Pengelolaan Kotoran Ternak. Silakan coba lagi. Error: ' . $e->getMessage()]); } } }