197 lines
6.5 KiB
PHP
197 lines
6.5 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);
|
|
}
|
|
|
|
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 = '<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>';
|
|
|
|
$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[] = [
|
|
'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)
|
|
{
|
|
//
|
|
}
|
|
}
|