sigd/app/Http/Controllers/Pengaturan/GWPController.php

212 lines
6.9 KiB
PHP

<?php
namespace App\Http\Controllers\Pengaturan;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\GHG;
use App\Models\AR;
use App\Models\GWP;
class GWPController extends Controller
{
protected $title = 'GWP';
protected $template = 'modules.pengaturan.gwp';
protected $route = 'modules.pengaturan.gwp';
/**
* Display a listing of the resource.
*/
public function index()
{
permission('is_read', $this->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)] = '<input name="ar_'.$rowAr->ArId.'_'.strtolower($rowAr->kode).'[]" type="text" value="'.getFormattedValue( getMatriks($row->kode, $rowAr->kode)).'" class="form-control">';
}
$action = '';
$status = '';
if($row->status == 0){
$status = '<span class="btn btn-sm btn-block btn-danger"> Tidak Aktif </span>';
}else{
$status = '<span class="btn btn-sm btn-block btn-success"> Aktif </span>';
}
$action .= '<div class="d-flex gap-1">';
if((permission('is_create', $this->route.'.*','module',false)) || (permission('is_update', $this->route.'.*','module',false))){
$action .= '<a data-id="'.encode_id($row->GhgId).'" href="'.url('pengaturan/ghg/update/'.encode_id($row->GhgId)).'" data-toggle="tooltip" title="Edit Data" class="btn btn-sm btn-block btn-primary"><i class="mdi mdi-pencil text-white"></i></a>';
if(session('group_id') == 1){
$action .= '<a href="#" data-href="'.url('pengaturan/ghg/delete/'.encode_id($row->GhgId)).'" data-toggle="tooltip" title="Edit Data" class="remove_data btn btn-sm btn-block btn-danger"><i class="mdi mdi-delete text-white"></i></a>';
}
}
$action .= '</div>';
$_data[] = array_merge([
'no' => $key+1,
'id' => encode_id($row->GhgId),
'kode' => '<input name="ghg[]" type="hidden" value="'.encode_id($row->GhgId).'" class="form-control">'.@$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)
{
//
}
}