199 lines
6.4 KiB
PHP
199 lines
6.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Pengaturan;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Http\Request;
|
|
use DB;
|
|
use App\Models\Unit;
|
|
use App\Models\UnitKonversi;
|
|
use App\Models\Kategori;
|
|
|
|
class UnitKonversiController extends Controller
|
|
{
|
|
protected $title = 'Unit Konversi';
|
|
protected $template = 'modules.pengaturan.unit-conversion';
|
|
protected $route = 'modules.pengaturan.unit-conversion';
|
|
|
|
/**
|
|
* Display a listing of the resource.
|
|
*/
|
|
public function index()
|
|
{
|
|
permission('is_read', $this->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);
|
|
}
|
|
|
|
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) {
|
|
|
|
//unit to
|
|
foreach ($data as $keyAr => $rowAr) {
|
|
$_ardata['input_'.$rowAr->UnitId.'_'.strtolower($rowAr->kode)] = '<input name="val_'.$row->UnitId.'_'.$rowAr->UnitId.'" type="text" value="'.@valueUnitKonversion($row->kategori->KategoriId,$row->UnitId,$rowAr->UnitId).'" 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>';
|
|
}
|
|
$unit = Unit::where('kode',$row->kode)->first();
|
|
|
|
// $input = '<div class="append"></div>';
|
|
// $input = '';
|
|
// $input .= '<input type="text" value="0" class="form-control" name="val_'.@$unit->UnitId.'[]">';
|
|
|
|
$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->UnitId).'" href="'.url('pengaturan/unit/update/'.encode_id($row->UnitId)).'" data-toggle="tooltip" title="Simpan Perubahan Data" class="btn btn-sm btn-block btn-primary"><i class="mdi mdi-content-save-outline text-white"></i></a>';
|
|
// $action .= '<a data-id="'.encode_id($row->UnitId).'" href="'.url('pengaturan/unit/update/'.encode_id($row->UnitId)).'" 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/unit/delete/'.encode_id($row->UnitId)).'" 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->UnitId),
|
|
'kategori' => @$row->kategori->nama,
|
|
'nama' => @$row->nama,
|
|
'kategoriID' => @$row->kategori->KategoriId,
|
|
'UnitId' => $unit->UnitId,
|
|
'status' => @$status,
|
|
'input' => @$input,
|
|
'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)
|
|
{
|
|
$kategoriId = decode_id($request->kategori_id);
|
|
|
|
foreach ($request->all() as $key => $value) {
|
|
// pastikan key diawali "val_"
|
|
if (!str_starts_with($key, 'val_')) {
|
|
continue;
|
|
}
|
|
|
|
// pecah: "val_6_17" -> ["val", "6", "17"]
|
|
$parts = explode('_', $key);
|
|
|
|
// jaga-jaga kalau format gak sesuai
|
|
if (count($parts) !== 3) {
|
|
continue;
|
|
}
|
|
|
|
$fromId = (int) $parts[1];
|
|
$toId = (int) $parts[2];
|
|
|
|
// skip kalau value kosong (opsional)
|
|
if ($value === null || $value === '') {
|
|
continue;
|
|
}
|
|
|
|
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)
|
|
{
|
|
//
|
|
}
|
|
}
|