Update Jenis Komoditas
This commit is contained in:
@@ -0,0 +1,216 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers\Web\Master;
|
||||||
|
|
||||||
|
use App\Http\Controllers\Controller;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use App\Models\Master\JenisKomoditas;
|
||||||
|
use App\Models\Pertanian\Komoditas;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
use Illuminate\Support\Facades\Validator;
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
|
use Illuminate\Support\Str;
|
||||||
|
use Illuminate\Validation\Rule;
|
||||||
|
|
||||||
|
class JenisKomoditasController extends Controller
|
||||||
|
{
|
||||||
|
public function index(Request $request)
|
||||||
|
{
|
||||||
|
$perPage = $request->get('per_page', 10);
|
||||||
|
$search = $request->get('search');
|
||||||
|
|
||||||
|
$jenisKomoditas = JenisKomoditas::with('komoditas')
|
||||||
|
->when($search, function ($query) use ($search) {
|
||||||
|
$query->where(function ($q) use ($search) {
|
||||||
|
$q->where('nama', 'ILIKE', "%{$search}%")
|
||||||
|
->orWhere('path', 'ILIKE', "%{$search}%")
|
||||||
|
->orWhereHas('komoditas', function ($komoditasQuery) use ($search) {
|
||||||
|
$komoditasQuery->where('nama', 'ILIKE', "%{$search}%");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
})
|
||||||
|
->latest()
|
||||||
|
->paginate($perPage);
|
||||||
|
|
||||||
|
$komoditas = Komoditas::orderBy('nama', 'asc')->get();
|
||||||
|
|
||||||
|
return view('master.jeniskomoditas.index', compact(
|
||||||
|
'jenisKomoditas',
|
||||||
|
'komoditas'
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Store a newly created resource in storage.
|
||||||
|
*/
|
||||||
|
public function store(Request $request)
|
||||||
|
{
|
||||||
|
$validated = $request->validate([
|
||||||
|
'komoditas_id' => [
|
||||||
|
'required',
|
||||||
|
'integer',
|
||||||
|
'exists:master_komoditas,id',
|
||||||
|
],
|
||||||
|
'nama' => [
|
||||||
|
'required',
|
||||||
|
'string',
|
||||||
|
'max:255',
|
||||||
|
Rule::unique('jenis_komoditas', 'nama')
|
||||||
|
->where('komoditas_id', $request->komoditas_id),
|
||||||
|
],
|
||||||
|
], [
|
||||||
|
'komoditas_id.required' => 'Komoditas wajib dipilih.',
|
||||||
|
'komoditas_id.exists' => 'Komoditas yang dipilih tidak valid.',
|
||||||
|
'nama.required' => 'Nama jenis komoditas wajib diisi.',
|
||||||
|
'nama.unique' => 'Nama jenis komoditas sudah tersedia pada komoditas ini.',
|
||||||
|
]);
|
||||||
|
|
||||||
|
try {
|
||||||
|
DB::beginTransaction();
|
||||||
|
|
||||||
|
$jenisKomoditas = JenisKomoditas::create([
|
||||||
|
'komoditas_id' => $validated['komoditas_id'],
|
||||||
|
'nama' => $validated['nama'],
|
||||||
|
'created_by' => auth()->id(),
|
||||||
|
'updated_by' => auth()->id(),
|
||||||
|
]);
|
||||||
|
|
||||||
|
DB::commit();
|
||||||
|
|
||||||
|
return response()->json([
|
||||||
|
'status' => 'success',
|
||||||
|
'message' => 'Data jenis komoditas berhasil disimpan.',
|
||||||
|
'data' => $jenisKomoditas,
|
||||||
|
]);
|
||||||
|
} catch (\Throwable $e) {
|
||||||
|
DB::rollBack();
|
||||||
|
|
||||||
|
Log::error('Gagal menyimpan jenis komoditas', [
|
||||||
|
'error' => $e->getMessage(),
|
||||||
|
'request' => $request->all(),
|
||||||
|
]);
|
||||||
|
|
||||||
|
return response()->json([
|
||||||
|
'status' => 'error',
|
||||||
|
'message' => 'Gagal menyimpan data: ' . $e->getMessage(),
|
||||||
|
], 500);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Display the specified resource.
|
||||||
|
*/
|
||||||
|
public function show(string $id)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$jenisKomoditas = JenisKomoditas::with('komoditas')->findOrFail($id);
|
||||||
|
|
||||||
|
return response()->json([
|
||||||
|
'status' => 'success',
|
||||||
|
'data' => $jenisKomoditas,
|
||||||
|
]);
|
||||||
|
} catch (\Throwable $e) {
|
||||||
|
return response()->json([
|
||||||
|
'status' => 'error',
|
||||||
|
'message' => 'Data jenis komoditas tidak ditemukan.',
|
||||||
|
], 404);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update the specified resource in storage.
|
||||||
|
*/
|
||||||
|
public function update(Request $request, string $id)
|
||||||
|
{
|
||||||
|
$jenisKomoditas = JenisKomoditas::findOrFail($id);
|
||||||
|
|
||||||
|
$validated = $request->validate([
|
||||||
|
'komoditas_id' => [
|
||||||
|
'required',
|
||||||
|
'integer',
|
||||||
|
'exists:master_komoditas,id',
|
||||||
|
],
|
||||||
|
'nama' => [
|
||||||
|
'required',
|
||||||
|
'string',
|
||||||
|
'max:255',
|
||||||
|
Rule::unique('jenis_komoditas', 'nama')
|
||||||
|
->where('komoditas_id', $request->komoditas_id)
|
||||||
|
->ignore($jenisKomoditas->id),
|
||||||
|
],
|
||||||
|
'path' => [
|
||||||
|
'nullable',
|
||||||
|
'string',
|
||||||
|
],
|
||||||
|
], [
|
||||||
|
'komoditas_id.required' => 'Komoditas wajib dipilih.',
|
||||||
|
'komoditas_id.exists' => 'Komoditas yang dipilih tidak valid.',
|
||||||
|
'nama.required' => 'Nama jenis komoditas wajib diisi.',
|
||||||
|
'nama.unique' => 'Nama jenis komoditas sudah tersedia pada komoditas ini.',
|
||||||
|
]);
|
||||||
|
|
||||||
|
try {
|
||||||
|
DB::beginTransaction();
|
||||||
|
|
||||||
|
$jenisKomoditas->update([
|
||||||
|
'komoditas_id' => $validated['komoditas_id'],
|
||||||
|
'nama' => $validated['nama'],
|
||||||
|
'updated_by' => auth()->id(),
|
||||||
|
]);
|
||||||
|
|
||||||
|
DB::commit();
|
||||||
|
|
||||||
|
return response()->json([
|
||||||
|
'status' => 'success',
|
||||||
|
'message' => 'Data jenis komoditas berhasil diperbarui.',
|
||||||
|
'data' => $jenisKomoditas->fresh('komoditas'),
|
||||||
|
]);
|
||||||
|
} catch (\Throwable $e) {
|
||||||
|
DB::rollBack();
|
||||||
|
|
||||||
|
Log::error('Gagal update jenis komoditas', [
|
||||||
|
'error' => $e->getMessage(),
|
||||||
|
'id' => $id,
|
||||||
|
'request' => $request->all(),
|
||||||
|
]);
|
||||||
|
|
||||||
|
return response()->json([
|
||||||
|
'status' => 'error',
|
||||||
|
'message' => 'Gagal mengupdate data: ' . $e->getMessage(),
|
||||||
|
], 500);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove the specified resource from storage.
|
||||||
|
*/
|
||||||
|
public function destroy(string $id)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
DB::beginTransaction();
|
||||||
|
|
||||||
|
$jenisKomoditas = JenisKomoditas::findOrFail($id);
|
||||||
|
$jenisKomoditas->update(['deleted_by' => auth()->id()]);
|
||||||
|
$jenisKomoditas->delete();
|
||||||
|
|
||||||
|
DB::commit();
|
||||||
|
|
||||||
|
return response()->json([
|
||||||
|
'status' => 'success',
|
||||||
|
'message' => 'Data jenis komoditas berhasil dihapus.',
|
||||||
|
]);
|
||||||
|
} catch (\Throwable $e) {
|
||||||
|
DB::rollBack();
|
||||||
|
|
||||||
|
Log::error('Gagal hapus jenis komoditas', [
|
||||||
|
'error' => $e->getMessage(),
|
||||||
|
'id' => $id,
|
||||||
|
]);
|
||||||
|
|
||||||
|
return response()->json([
|
||||||
|
'status' => 'error',
|
||||||
|
'message' => 'Gagal menghapus data: ' . $e->getMessage(),
|
||||||
|
], 500);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models\Master;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||||
|
use App\Models\Pertanian\Komoditas;
|
||||||
|
|
||||||
|
class JenisKomoditas extends Model
|
||||||
|
{
|
||||||
|
use HasFactory, SoftDeletes;
|
||||||
|
|
||||||
|
protected $table = 'jenis_komoditas';
|
||||||
|
|
||||||
|
protected $fillable = [
|
||||||
|
'nama',
|
||||||
|
'komoditas_id',
|
||||||
|
'created_by',
|
||||||
|
'updated_by',
|
||||||
|
'deleted_by',
|
||||||
|
];
|
||||||
|
|
||||||
|
public function komoditas()
|
||||||
|
{
|
||||||
|
return $this->belongsTo(Komoditas::class, 'komoditas_id');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
<div class="offcanvas offcanvas-end" tabindex="-1" id="offcanvasAddJenisKomoditas" aria-labelledby="offcanvasAddJenisKomoditasLabel">
|
||||||
|
<div class="offcanvas-header border-bottom">
|
||||||
|
<h5 id="offcanvasAddJenisKomoditasLabel" class="offcanvas-title">Tambah Jenis Komoditas</h5>
|
||||||
|
<button type="button" class="btn-close text-reset" data-bs-dismiss="offcanvas" aria-label="Close"></button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="offcanvas-body mx-0 flex-grow-0 p-6 h-100">
|
||||||
|
<form class="add-new-jeniskomoditas pt-0" id="addNewJenisKomoditasForm" action="{{ route('jenis-komoditas.store') }}" method="POST">
|
||||||
|
@csrf
|
||||||
|
|
||||||
|
<div class="mb-6 form-control-validation">
|
||||||
|
<label class="form-label" for="add-jenis-komoditas-komoditas">
|
||||||
|
Komoditas <span class="text-danger">*</span>
|
||||||
|
</label>
|
||||||
|
<select id="add-jenis-komoditas-komoditas" name="komoditas_id" class="select2 form-select" data-placeholder="Pilih Komoditas">
|
||||||
|
<option value=""></option>
|
||||||
|
@foreach($komoditas as $item)
|
||||||
|
<option value="{{ $item->id }}">{{ $item->nama }}</option>
|
||||||
|
@endforeach
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="mb-6 form-control-validation">
|
||||||
|
<label class="form-label" for="add-jenis-komoditas-name">
|
||||||
|
Nama Jenis Komoditas <span class="text-danger">*</span>
|
||||||
|
</label>
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
class="form-control"
|
||||||
|
id="add-jenis-komoditas-name"
|
||||||
|
placeholder="Contoh: Kopi Arabika"
|
||||||
|
name="nama"
|
||||||
|
autocomplete="off" />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="mt-8">
|
||||||
|
<button type="submit" class="btn btn-primary me-3 data-submit">Submit</button>
|
||||||
|
<button type="reset" class="btn btn-label-danger" data-bs-dismiss="offcanvas">Cancel</button>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
<div class="offcanvas offcanvas-end" tabindex="-1" id="offcanvasEditJenisKomoditas" aria-labelledby="offcanvasEditJenisKomoditasLabel">
|
||||||
|
<div class="offcanvas-header border-bottom">
|
||||||
|
<h5 id="offcanvasEditJenisKomoditasLabel" class="offcanvas-title">Edit Jenis Komoditas</h5>
|
||||||
|
<button type="button" class="btn-close text-reset" data-bs-dismiss="offcanvas" aria-label="Close"></button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="offcanvas-body mx-0 flex-grow-0 p-6 h-100">
|
||||||
|
<form class="edit-jeniskomoditas pt-0" id="editJenisKomoditasForm" method="POST">
|
||||||
|
@csrf
|
||||||
|
@method('PUT')
|
||||||
|
|
||||||
|
<input type="hidden" id="edit-jenis-komoditas-id" name="id">
|
||||||
|
|
||||||
|
<div class="mb-6 form-control-validation">
|
||||||
|
<label class="form-label" for="edit-jenis-komoditas-komoditas">
|
||||||
|
Komoditas <span class="text-danger">*</span>
|
||||||
|
</label>
|
||||||
|
<select id="edit-jenis-komoditas-komoditas" name="komoditas_id" class="select2 form-select" data-placeholder="Pilih Komoditas">
|
||||||
|
<option value=""></option>
|
||||||
|
@foreach($komoditas as $item)
|
||||||
|
<option value="{{ $item->id }}">{{ $item->nama }}</option>
|
||||||
|
@endforeach
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="mb-6 form-control-validation">
|
||||||
|
<label class="form-label" for="edit-jenis-komoditas-name">
|
||||||
|
Nama Jenis Komoditas <span class="text-danger">*</span>
|
||||||
|
</label>
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
class="form-control"
|
||||||
|
id="edit-jenis-komoditas-name"
|
||||||
|
placeholder="Contoh: Kopi Arabika"
|
||||||
|
name="nama"
|
||||||
|
autocomplete="off" />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="mt-8">
|
||||||
|
<button type="submit" class="btn btn-primary me-3 data-submit">Update</button>
|
||||||
|
<button type="reset" class="btn btn-label-danger" data-bs-dismiss="offcanvas">Cancel</button>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
@@ -0,0 +1,485 @@
|
|||||||
|
@extends('layout.main')
|
||||||
|
|
||||||
|
@section('content')
|
||||||
|
<div class="container-xxl flex-grow-1 container-p-y">
|
||||||
|
|
||||||
|
<div class="card">
|
||||||
|
<div class="card-header border-bottom">
|
||||||
|
<div class="d-flex flex-column flex-md-row justify-content-between align-items-md-center gap-3">
|
||||||
|
<div>
|
||||||
|
<h5 class="card-title mb-0">Jenis Komoditas</h5>
|
||||||
|
<small class="text-muted">Kelola data jenis komoditas</small>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
class="btn btn-primary"
|
||||||
|
data-bs-toggle="offcanvas"
|
||||||
|
data-bs-target="#offcanvasAddJenisKomoditas">
|
||||||
|
<i class="ti tabler-plus me-1"></i>
|
||||||
|
Tambah Jenis Komoditas
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="card-body border-bottom">
|
||||||
|
<form
|
||||||
|
method="GET"
|
||||||
|
action="{{ route('jenis-komoditas.index') }}"
|
||||||
|
class="row g-3 align-items-center justify-content-between">
|
||||||
|
|
||||||
|
<div class="col-md-auto">
|
||||||
|
<div class="d-flex align-items-center">
|
||||||
|
<label class="me-2 text-nowrap">Show</label>
|
||||||
|
<select name="per_page" class="form-select form-select-sm w-auto" onchange="this.form.submit()">
|
||||||
|
@foreach([10, 25, 50, 100] as $limit)
|
||||||
|
<option value="{{ $limit }}" {{ (int) request('per_page', 10) === $limit ? 'selected' : '' }}>
|
||||||
|
{{ $limit }}
|
||||||
|
</option>
|
||||||
|
@endforeach
|
||||||
|
</select>
|
||||||
|
<label class="ms-2 text-nowrap">entries</label>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="col-md-auto">
|
||||||
|
<div class="input-group input-group-sm">
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
name="search"
|
||||||
|
class="form-control"
|
||||||
|
placeholder="Cari jenis komoditas..."
|
||||||
|
value="{{ request('search') }}">
|
||||||
|
|
||||||
|
@if(request('search'))
|
||||||
|
<a href="{{ route('jenis-komoditas.index', ['per_page' => request('per_page', 10)]) }}"
|
||||||
|
class="btn btn-outline-secondary">
|
||||||
|
<i class="ti tabler-x"></i>
|
||||||
|
</a>
|
||||||
|
@endif
|
||||||
|
|
||||||
|
<button type="submit" class="btn btn-outline-primary">
|
||||||
|
<i class="ti tabler-search"></i>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="table-responsive text-nowrap">
|
||||||
|
<table class="table table-hover">
|
||||||
|
<thead class="border-top">
|
||||||
|
<tr>
|
||||||
|
<th style="width: 70px;">No</th>
|
||||||
|
<th>Nama Jenis Komoditas</th>
|
||||||
|
<th>Komoditas</th>
|
||||||
|
<th class="text-center" style="width: 160px;">Actions</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
|
||||||
|
<tbody>
|
||||||
|
@forelse($jenisKomoditas as $index => $jenis)
|
||||||
|
@php
|
||||||
|
$komoditasNama = $jenis->komoditas->nama ?? '-';
|
||||||
|
$words = preg_split('/\s+/', trim($komoditasNama));
|
||||||
|
$initials = collect($words)
|
||||||
|
->filter()
|
||||||
|
->map(fn($word) => strtoupper(substr($word, 0, 1)))
|
||||||
|
->take(2)
|
||||||
|
->implode('');
|
||||||
|
|
||||||
|
$states = ['success', 'danger', 'warning', 'info', 'primary', 'secondary'];
|
||||||
|
$state = $states[$jenis->id % count($states)];
|
||||||
|
@endphp
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td>{{ $jenisKomoditas->firstItem() + $index }}</td>
|
||||||
|
|
||||||
|
<td>
|
||||||
|
<div class="d-flex justify-content-start align-items-center">
|
||||||
|
<div class="avatar-wrapper me-3">
|
||||||
|
<div class="avatar avatar-sm">
|
||||||
|
<span class="avatar-initial rounded-circle bg-label-{{ $state }}">
|
||||||
|
{{ $initials ?: 'JK' }}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="d-flex flex-column">
|
||||||
|
<span class="fw-medium text-heading">{{ $jenis->nama }}</span>
|
||||||
|
<small class="text-muted">ID: {{ $jenis->id }}</small>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</td>
|
||||||
|
|
||||||
|
<td>
|
||||||
|
<span class="badge bg-label-secondary">
|
||||||
|
{{ $komoditasNama }}
|
||||||
|
</span>
|
||||||
|
</td>
|
||||||
|
|
||||||
|
<td>
|
||||||
|
<div class="d-flex justify-content-center align-items-center">
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
class="btn btn-icon btn-text-secondary rounded-pill view-record"
|
||||||
|
data-id="{{ $jenis->id }}"
|
||||||
|
title="Lihat">
|
||||||
|
<i class="ti tabler-eye ti-md"></i>
|
||||||
|
</button>
|
||||||
|
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
class="btn btn-icon btn-text-secondary rounded-pill edit-record"
|
||||||
|
data-id="{{ $jenis->id }}"
|
||||||
|
title="Edit">
|
||||||
|
<i class="ti tabler-edit ti-md"></i>
|
||||||
|
</button>
|
||||||
|
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
class="btn btn-icon btn-text-secondary rounded-pill delete-record"
|
||||||
|
data-id="{{ $jenis->id }}"
|
||||||
|
data-nama="{{ $jenis->nama }}"
|
||||||
|
title="Hapus">
|
||||||
|
<i class="ti tabler-trash ti-md"></i>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
@empty
|
||||||
|
<tr>
|
||||||
|
<td colspan="5" class="text-center py-5">
|
||||||
|
<div class="d-flex flex-column align-items-center">
|
||||||
|
<i class="ti tabler-database-off ti-lg mb-2 text-muted"></i>
|
||||||
|
<span class="text-muted">Tidak ada data ditemukan.</span>
|
||||||
|
</div>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
@endforelse
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
@if($jenisKomoditas->hasPages())
|
||||||
|
<div class="card-footer d-flex justify-content-center justify-content-md-end border-top">
|
||||||
|
{{ $jenisKomoditas->appends(request()->query())->links('pagination::bootstrap-5') }}
|
||||||
|
</div>
|
||||||
|
@endif
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
@include('master.jeniskomoditas.add')
|
||||||
|
@include('master.jeniskomoditas.edit')
|
||||||
|
@include('master.jeniskomoditas.view')
|
||||||
|
@endsection
|
||||||
|
|
||||||
|
|
||||||
|
@push('scripts')
|
||||||
|
<script>
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
$(function () {
|
||||||
|
const csrfToken = "{{ csrf_token() }}";
|
||||||
|
const baseUrl = "{{ url('jenis-komoditas') }}";
|
||||||
|
|
||||||
|
const offcanvasAdd = new bootstrap.Offcanvas(document.getElementById('offcanvasAddJenisKomoditas'));
|
||||||
|
const offcanvasEdit = new bootstrap.Offcanvas(document.getElementById('offcanvasEditJenisKomoditas'));
|
||||||
|
const offcanvasView = new bootstrap.Offcanvas(document.getElementById('offcanvasViewJenisKomoditas'));
|
||||||
|
|
||||||
|
$.ajaxSetup({
|
||||||
|
headers: {
|
||||||
|
'X-CSRF-TOKEN': csrfToken
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
$('.select2').each(function () {
|
||||||
|
const $this = $(this);
|
||||||
|
|
||||||
|
$this.wrap('<div class="position-relative"></div>').select2({
|
||||||
|
dropdownParent: $this.parent(),
|
||||||
|
placeholder: $this.data('placeholder') || 'Pilih data',
|
||||||
|
allowClear: true,
|
||||||
|
width: '100%'
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
$.validator.setDefaults({
|
||||||
|
errorElement: 'div',
|
||||||
|
errorClass: 'invalid-feedback',
|
||||||
|
highlight: function (element) {
|
||||||
|
$(element).addClass('is-invalid');
|
||||||
|
},
|
||||||
|
unhighlight: function (element) {
|
||||||
|
$(element).removeClass('is-invalid');
|
||||||
|
},
|
||||||
|
errorPlacement: function (error, element) {
|
||||||
|
if (element.hasClass('select2-hidden-accessible')) {
|
||||||
|
error.insertAfter(element.next('.select2-container'));
|
||||||
|
} else {
|
||||||
|
error.appendTo(element.closest('.form-control-validation'));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
$('.select2').on('change', function () {
|
||||||
|
const $form = $(this).closest('form');
|
||||||
|
|
||||||
|
if ($form.data('validator')) {
|
||||||
|
$(this).valid();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
function showSuccess(title = 'Berhasil!', text = 'Data berhasil diproses') {
|
||||||
|
return Swal.fire({
|
||||||
|
icon: 'success',
|
||||||
|
title,
|
||||||
|
text,
|
||||||
|
showConfirmButton: false,
|
||||||
|
timer: 1500,
|
||||||
|
customClass: {
|
||||||
|
confirmButton: 'btn btn-primary'
|
||||||
|
},
|
||||||
|
buttonsStyling: false
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function showError(xhr, fallback = 'Terjadi kesalahan sistem.') {
|
||||||
|
let message = fallback;
|
||||||
|
|
||||||
|
if (xhr.responseJSON?.message) {
|
||||||
|
message = xhr.responseJSON.message;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (xhr.responseJSON?.errors) {
|
||||||
|
message = Object.values(xhr.responseJSON.errors).flat().join('<br>');
|
||||||
|
}
|
||||||
|
|
||||||
|
return Swal.fire({
|
||||||
|
icon: 'error',
|
||||||
|
title: 'Gagal!',
|
||||||
|
html: message,
|
||||||
|
customClass: {
|
||||||
|
confirmButton: 'btn btn-primary'
|
||||||
|
},
|
||||||
|
buttonsStyling: false
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function resetForm($form) {
|
||||||
|
if (!$form.length) return;
|
||||||
|
|
||||||
|
$form[0].reset();
|
||||||
|
|
||||||
|
if ($form.data('validator')) {
|
||||||
|
$form.validate().resetForm();
|
||||||
|
}
|
||||||
|
|
||||||
|
$form.find('.is-invalid').removeClass('is-invalid');
|
||||||
|
|
||||||
|
$form.find('.select2').each(function () {
|
||||||
|
$(this).val('').trigger('change.select2');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function setButtonLoading($button, isLoading, text = 'Menyimpan...') {
|
||||||
|
if (isLoading) {
|
||||||
|
$button
|
||||||
|
.data('original-text', $button.html())
|
||||||
|
.prop('disabled', true)
|
||||||
|
.html(`<span class="spinner-border spinner-border-sm me-1"></span>${text}`);
|
||||||
|
} else {
|
||||||
|
$button
|
||||||
|
.prop('disabled', false)
|
||||||
|
.html($button.data('original-text'));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$('#addNewJenisKomoditasForm').validate({
|
||||||
|
rules: {
|
||||||
|
komoditas_id: {
|
||||||
|
required: true
|
||||||
|
},
|
||||||
|
nama: {
|
||||||
|
required: true,
|
||||||
|
maxlength: 255
|
||||||
|
}
|
||||||
|
},
|
||||||
|
messages: {
|
||||||
|
komoditas_id: {
|
||||||
|
required: 'Komoditas wajib dipilih'
|
||||||
|
},
|
||||||
|
nama: {
|
||||||
|
required: 'Nama jenis komoditas wajib diisi',
|
||||||
|
maxlength: 'Nama maksimal 255 karakter'
|
||||||
|
}
|
||||||
|
},
|
||||||
|
submitHandler: function (form, event) {
|
||||||
|
event.preventDefault();
|
||||||
|
|
||||||
|
const $form = $(form);
|
||||||
|
const $button = $form.find('button[type="submit"]');
|
||||||
|
|
||||||
|
setButtonLoading($button, true);
|
||||||
|
|
||||||
|
$.ajax({
|
||||||
|
url: $form.attr('action'),
|
||||||
|
type: 'POST',
|
||||||
|
data: $form.serialize(),
|
||||||
|
success: function (res) {
|
||||||
|
offcanvasAdd.hide();
|
||||||
|
|
||||||
|
showSuccess(
|
||||||
|
'Berhasil!',
|
||||||
|
res.message || 'Data jenis komoditas berhasil disimpan'
|
||||||
|
).then(() => window.location.reload());
|
||||||
|
},
|
||||||
|
error: function (xhr) {
|
||||||
|
showError(xhr, 'Gagal menyimpan data jenis komoditas');
|
||||||
|
},
|
||||||
|
complete: function () {
|
||||||
|
setButtonLoading($button, false);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
$('#offcanvasAddJenisKomoditas').on('hidden.bs.offcanvas', function () {
|
||||||
|
resetForm($('#addNewJenisKomoditasForm'));
|
||||||
|
});
|
||||||
|
|
||||||
|
$(document).on('click', '.edit-record', function () {
|
||||||
|
const id = $(this).data('id');
|
||||||
|
const $form = $('#editJenisKomoditasForm');
|
||||||
|
|
||||||
|
resetForm($form);
|
||||||
|
|
||||||
|
$.ajax({
|
||||||
|
url: `${baseUrl}/${id}`,
|
||||||
|
type: 'GET',
|
||||||
|
success: function (res) {
|
||||||
|
const data = res.data || res;
|
||||||
|
|
||||||
|
$('#edit-jenis-komoditas-id').val(data.id);
|
||||||
|
$('#edit-jenis-komoditas-komoditas').val(data.komoditas_id).trigger('change');
|
||||||
|
$('#edit-jenis-komoditas-name').val(data.nama);
|
||||||
|
|
||||||
|
offcanvasEdit.show();
|
||||||
|
},
|
||||||
|
error: function (xhr) {
|
||||||
|
showError(xhr, 'Terjadi kesalahan saat mengambil data edit');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
$('#editJenisKomoditasForm').validate({
|
||||||
|
rules: {
|
||||||
|
komoditas_id: {
|
||||||
|
required: true
|
||||||
|
},
|
||||||
|
nama: {
|
||||||
|
required: true,
|
||||||
|
maxlength: 255
|
||||||
|
}
|
||||||
|
},
|
||||||
|
messages: {
|
||||||
|
komoditas_id: {
|
||||||
|
required: 'Komoditas wajib dipilih'
|
||||||
|
},
|
||||||
|
nama: {
|
||||||
|
required: 'Nama jenis komoditas wajib diisi',
|
||||||
|
maxlength: 'Nama maksimal 255 karakter'
|
||||||
|
}
|
||||||
|
},
|
||||||
|
submitHandler: function (form, event) {
|
||||||
|
event.preventDefault();
|
||||||
|
|
||||||
|
const $form = $(form);
|
||||||
|
const id = $('#edit-jenis-komoditas-id').val();
|
||||||
|
const $button = $form.find('button[type="submit"]');
|
||||||
|
|
||||||
|
setButtonLoading($button, true, 'Mengupdate...');
|
||||||
|
|
||||||
|
$.ajax({
|
||||||
|
url: `${baseUrl}/${id}`,
|
||||||
|
type: 'POST',
|
||||||
|
data: $form.serialize(),
|
||||||
|
success: function (res) {
|
||||||
|
offcanvasEdit.hide();
|
||||||
|
|
||||||
|
showSuccess(
|
||||||
|
'Updated!',
|
||||||
|
res.message || 'Data jenis komoditas berhasil diperbarui'
|
||||||
|
).then(() => window.location.reload());
|
||||||
|
},
|
||||||
|
error: function (xhr) {
|
||||||
|
showError(xhr, 'Gagal mengupdate data jenis komoditas');
|
||||||
|
},
|
||||||
|
complete: function () {
|
||||||
|
setButtonLoading($button, false);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
$('#offcanvasEditJenisKomoditas').on('hidden.bs.offcanvas', function () {
|
||||||
|
resetForm($('#editJenisKomoditasForm'));
|
||||||
|
});
|
||||||
|
|
||||||
|
$(document).on('click', '.view-record', function () {
|
||||||
|
const id = $(this).data('id');
|
||||||
|
|
||||||
|
$.ajax({
|
||||||
|
url: `${baseUrl}/${id}`,
|
||||||
|
type: 'GET',
|
||||||
|
success: function (res) {
|
||||||
|
const data = res.data || res;
|
||||||
|
|
||||||
|
$('#view-jenis-komoditas-komoditas').val(data.komoditas?.nama || data.nama_komoditas || '-');
|
||||||
|
$('#view-jenis-komoditas-name').val(data.nama || '-');
|
||||||
|
|
||||||
|
offcanvasView.show();
|
||||||
|
},
|
||||||
|
error: function (xhr) {
|
||||||
|
showError(xhr, 'Terjadi kesalahan saat mengambil detail data');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
$(document).on('click', '.delete-record', function () {
|
||||||
|
const id = $(this).data('id');
|
||||||
|
const nama = $(this).data('nama') || 'data ini';
|
||||||
|
|
||||||
|
Swal.fire({
|
||||||
|
title: 'Apakah Anda yakin?',
|
||||||
|
text: `Jenis Komoditas "${nama}" akan dihapus secara permanen.`,
|
||||||
|
icon: 'warning',
|
||||||
|
showCancelButton: true,
|
||||||
|
confirmButtonText: 'Ya, Hapus!',
|
||||||
|
cancelButtonText: 'Batal',
|
||||||
|
customClass: {
|
||||||
|
confirmButton: 'btn btn-primary me-3',
|
||||||
|
cancelButton: 'btn btn-label-secondary'
|
||||||
|
},
|
||||||
|
buttonsStyling: false
|
||||||
|
}).then(function (result) {
|
||||||
|
if (!result.isConfirmed) return;
|
||||||
|
|
||||||
|
$.ajax({
|
||||||
|
url: `${baseUrl}/${id}`,
|
||||||
|
type: 'DELETE',
|
||||||
|
success: function (res) {
|
||||||
|
showSuccess(
|
||||||
|
'Terhapus!',
|
||||||
|
res.message || 'Data jenis komoditas berhasil dihapus'
|
||||||
|
).then(() => window.location.reload());
|
||||||
|
},
|
||||||
|
error: function (xhr) {
|
||||||
|
showError(xhr, 'Gagal menghapus data jenis komoditas');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
@endpush
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
<div class="offcanvas offcanvas-end" tabindex="-1" id="offcanvasViewJenisKomoditas" aria-labelledby="offcanvasViewJenisKomoditasLabel">
|
||||||
|
<div class="offcanvas-header border-bottom">
|
||||||
|
<h5 id="offcanvasViewJenisKomoditasLabel" class="offcanvas-title">Detail Jenis Komoditas</h5>
|
||||||
|
<button type="button" class="btn-close text-reset" data-bs-dismiss="offcanvas" aria-label="Close"></button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="offcanvas-body mx-0 flex-grow-0 p-6 h-100">
|
||||||
|
<div class="mb-6">
|
||||||
|
<label class="form-label">Komoditas</label>
|
||||||
|
<input type="text" class="form-control" id="view-jenis-komoditas-komoditas" readonly>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="mb-6">
|
||||||
|
<label class="form-label">Nama Jenis Komoditas</label>
|
||||||
|
<input type="text" class="form-control" id="view-jenis-komoditas-name" readonly>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="mt-8">
|
||||||
|
<button type="button" class="btn btn-label-secondary" data-bs-dismiss="offcanvas">
|
||||||
|
Tutup
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
@@ -85,6 +85,8 @@ Route::middleware('auth')
|
|||||||
Route::put('/komoditas/{id}', [App\Http\Controllers\Web\Master\KomoditasController::class, 'update'])->name('komoditas.update');
|
Route::put('/komoditas/{id}', [App\Http\Controllers\Web\Master\KomoditasController::class, 'update'])->name('komoditas.update');
|
||||||
Route::delete('/komoditas/{id}', [App\Http\Controllers\Web\Master\KomoditasController::class, 'destroy'])->name('komoditas.destroy');
|
Route::delete('/komoditas/{id}', [App\Http\Controllers\Web\Master\KomoditasController::class, 'destroy'])->name('komoditas.destroy');
|
||||||
|
|
||||||
|
Route::resource('jenis-komoditas', App\Http\Controllers\Web\Master\JenisKomoditasController::class);
|
||||||
|
|
||||||
Route::get('/keluarga', [App\Http\Controllers\Web\Master\KeluargaController::class, 'index'])->name('keluarga.index');
|
Route::get('/keluarga', [App\Http\Controllers\Web\Master\KeluargaController::class, 'index'])->name('keluarga.index');
|
||||||
Route::get('/keluarga/add', [App\Http\Controllers\Web\Master\KeluargaController::class, 'create'])->name('keluarga.create');
|
Route::get('/keluarga/add', [App\Http\Controllers\Web\Master\KeluargaController::class, 'create'])->name('keluarga.create');
|
||||||
Route::post('/keluarga', [App\Http\Controllers\Web\Master\KeluargaController::class, 'store'])->name('keluarga.store');
|
Route::post('/keluarga', [App\Http\Controllers\Web\Master\KeluargaController::class, 'store'])->name('keluarga.store');
|
||||||
|
|||||||
Reference in New Issue
Block a user