route, 'module',true); $data['breadcrumbs'] = [ ['name' => 'Dashboard'], ['name' => 'Pengaturan'], ['name' => 'GWP','active' => true], ]; $data['title'] = $this->title; $data['route'] = $this->route; $data['ar'] = AR::where('status',1)->orderBy('nomor_baris','ASC')->get(); return view($this->template.'.form',$data); } public function grid(Request $request) { $data = GHG::orderBy('nomor_baris','ASC')->get(); $ar = AR::where('status',1)->orderBy('nomor_baris','ASC')->get(); $_data = []; foreach ($data as $key => $row) { $_ardata = []; foreach ($ar as $keyAr => $rowAr) { $_ardata['ar_'.$rowAr->ArId.'_'.strtolower($rowAr->kode)] = ''; } $action = ''; $status = ''; if($row->status == 0){ $status = ' Tidak Aktif '; }else{ $status = ' Aktif '; } $action .= '
'; if((permission('is_create', $this->route.'.*','module',false)) || (permission('is_update', $this->route.'.*','module',false))){ $action .= ''; if(session('group_id') == 1){ $action .= ''; } } $action .= '
'; $_data[] = array_merge([ 'no' => $key+1, 'id' => encode_id($row->GhgId), 'kode' => ''.@$row->kode, 'nama' => @$row->deskripsi, 'status' => @$status, 'action' => @$action, ],$_ardata); } // 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) { $post = request()->all(); $ghg_list = $post['ghg'] ?? []; $ghg_count = count($ghg_list); $ar_inputs = []; try { foreach ($post as $k => $v) { if (\Str::startsWith($k, 'ar_')) { // ekstrak ArId dari nama, format di code sebelumnya: ar_{ArId}_{kode} if (preg_match('/^ar_(\d+)_/', $k, $m)) { $arId = (int) $m[1]; } else { // fallback: jika tidak ada angka, gunakan seluruh key (atau handle sesuai kebutuhan) // skip jika tidak bisa ambil ArId continue; } $ar_inputs[$arId] = array_values((array) $v); // cast ke array & reindex } } foreach ($ar_inputs as $arId => $vals) { $len = count($vals); if ($len < $ghg_count) { $ar_inputs[$arId] = array_pad($vals, $ghg_count, null); } elseif ($len > $ghg_count) { $ar_inputs[$arId] = array_slice($vals, 0, $ghg_count); } } $inserts = []; $now = now(); for ($i = 0; $i < $ghg_count; $i++) { // decode ghg id (sesuaikan fungsi decode_id) $ghgEncoded = $ghg_list[$i]; $ghgId = decode_id($ghgEncoded); foreach ($ar_inputs as $arId => $vals) { $rawValue = $vals[$i]; $value = is_string($rawValue) ? trim($rawValue) : $rawValue; if ($value === '') $value = null; $inserts[] = [ 'ghg_id' => $ghgId, 'ar_id' => $arId, 'value' => $value, 'created_at'=> $now, 'updated_at'=> $now, ]; } } \DB::transaction(function () use ($inserts) { $chunkSize = 500; foreach (array_chunk($inserts, $chunkSize) as $chunk) { foreach ($chunk as $row) { GWP::updateOrCreate( [ 'ghg_id' => $row['ghg_id'], 'ar_id' => $row['ar_id'], ],[ 'value' => @$row['value'] ? @$row['value'] : 0 ] ); } } }); return redirect()->back()->with([ 'message' => 'Berhasil update data', 'type' => 'success', ]); } catch (\Exception $e) { return redirect()->back()->with([ 'message' => 'Gagal disimpan. Silakan coba lagi. Error: ' . $e->getMessage(), 'type' => 'error', ]); } } /** * 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) { // } /** * Remove the specified resource from storage. */ public function destroy(string $id) { // } }