route, 'module',true); $data['breadcrumbs'] = [ ['name' => 'Dashboard'], ['name' => 'Pengaturan'], ['name' => 'Unit Konversi','active' => true], ]; $data['title'] = $this->title; $data['route'] = $this->route; $data['kategori'] = Kategori::all(); $data['unit'] = Unit::where('kategori_id',decode_id(@request()->kategori_id))->get(); return view($this->template.'.index',$data); } function valueKonversion($kategori,$from,$to) { $value = valueUnitKonversion(decode_id($kategori),decode_id($from),decode_id($to)); // return ; return response()->json(['value' => $value]); } public function grid(Request $request) { if(@$request->kategori_id){ $data = Unit::where('kategori_id',decode_id($request->kategori_id))->orderBy('UnitId','ASC')->get(); }else{ $data = []; } $_data = []; foreach ($data as $key => $row) { $action = ''; $status = ''; if($row->status == 0){ $status = ' Tidak Aktif '; }else{ $status = ' Aktif '; } $unit = Unit::where('kode',$row->kode)->first(); $input = '
'; $action .= '
'; if((permission('is_create', $this->route.'.*','module',false)) || (permission('is_update', $this->route.'.*','module',false))){ $action .= ''; // $action .= ''; if(session('group_id') == 1){ // $action .= ''; } } $action .= '
'; $_data[] = [ 'no' => $key+1, 'id' => encode_id($row->UnitId), 'kategori' => @$row->kategori->nama, 'nama' => @$row->nama, 'kategoriID' => encode_id(@$row->kategori->KategoriId), 'UnitId' => encode_id($unit->UnitId), 'status' => @$status, 'input' => @$input, 'action' => @$action, ]; } // return response()->json($_data); // Return the data as a JSON response return response()->json($_data); } /** * Show the form for creating a new resource. */ public function create() { // } /** * Store a newly created resource in storage. */ public function store(Request $request) { $kategoriId = decode_id($request->kategori_id); $toRaw = array_values($request->input('to')); $codes = array_values(array_filter($toRaw, fn($v) => !is_numeric($v))); $codeToId = !empty($codes) ? Unit::whereIn('kode', $codes)->pluck('UnitId','kode')->toArray() : []; $toIds = array_map(fn($v) => is_numeric($v) ? (int)$v : ($codeToId[$v] ?? null), $toRaw); DB::transaction(function() use ($request, $kategoriId, $toIds) { foreach ($request->all() as $key => $vals) { if (!str_starts_with($key, 'val_')) continue; $fromId = (int) substr($key, 4); if (!$fromId || !is_array($vals)) continue; foreach ($toIds as $col => $toId) { if (!$toId) continue; $raw = $vals[$col] ?? null; // skip empty if you want: if ($raw === null || $raw === '') continue; $value = ($raw === null || $raw === '') ? 0 : (float)$raw; UnitKonversi::updateOrCreate( ['kategori_id' => $kategoriId, 'from_id' => $fromId, 'to_id' => $toId], ['value' => $value, 'updated_at' => now()] ); } } }); return redirect()->back()->with([ 'message' => 'Berhasil update data', 'type' => 'success', ]); } /** * Display the specified resource. */ public function show(string $id) { // } /** * Show the form for editing the specified resource. */ public function edit(string $id) { // } /** * Update the specified resource in storage. */ public function update($id = null) { $data['breadcrumbs'] = [ ['name' => 'Dashboard'], ['name' => 'Pengaturan'], ['name' => 'Unit Konversi','active' => true], ]; $keyId = decode_id($id); $data['title'] = $this->title; $data['route'] = $this->route; $data['keyId'] = $id; $data['item'] = UnitKonversi::where('UnitKonversiId',$keyId)->first(); $data['kategori'] = Kategori::all(); return view($this->template.'.form',$data); } public function delete($id) { $keyId = decode_id($id); $data = UnitKonversi::where('UnitKonversiId',$keyId)->delete(); return response()->json(['success' => true,'message' => 'Berhasil update data','type' => 'success']); } /** * Remove the specified resource from storage. */ public function destroy(string $id) { // } }