feat(basicdata): tambahkan fitur relasi parent-child pada cabang
- Menambahkan kolom `parent_id` pada tabel `branches` dengan migrasi baru. - Update model `Branch`: - Menambahkan relasi `parent()` untuk mendapatkan cabang induk. - Menambahkan relasi `children()` untuk mendapatkan anak cabang. - Update `BranchController`: - Menampilkan daftar cabang induk saat membuat atau mengedit cabang. - Cek validasi agar cabang tidak bisa menjadi induk dirinya sendiri. - Tambahkan larangan hapus cabang jika memiliki anak cabang, baik untuk hapus tunggal maupun multiple. - Update validation rules pada `BranchRequest` untuk memastikan validitas `parent_id`. - Update tampilan: - Formulir pembuatan/edit cabang: Menampilkan dropdown untuk memilih cabang induk. - Daftar cabang: Menampilkan kolom untuk cabang induk. - Tambahkan test unit: - Validasi relasi parent-child pada penyimpanan dan pembaruan cabang. - Melarang penghapusan cabang yang memiliki anak. - Memastikan perilaku relasi parent-child sesuai ekspektasi. Signed-off-by: Daeng Deni Mardaeni <ddeni05@gmail.com>
This commit is contained in:
@@ -14,7 +14,8 @@
|
||||
{
|
||||
protected $user;
|
||||
|
||||
public function __construct(){
|
||||
public function __construct()
|
||||
{
|
||||
$this->user = auth()->user();
|
||||
}
|
||||
|
||||
@@ -58,8 +59,8 @@
|
||||
if (is_null($this->user) || !$this->user->can('basic-data.create')) {
|
||||
abort(403, 'Sorry! You are not allowed to create branches.');
|
||||
}
|
||||
|
||||
return view('basicdata::branch.create');
|
||||
$branches = Branch::all();
|
||||
return view('basicdata::branch.create', compact('branches'));
|
||||
}
|
||||
|
||||
public function edit($id)
|
||||
@@ -69,8 +70,9 @@
|
||||
abort(403, 'Sorry! You are not allowed to update branches.');
|
||||
}
|
||||
|
||||
$branch = Branch::find($id);
|
||||
return view('basicdata::branch.create', compact('branch'));
|
||||
$branch = Branch::findOrFail($id);
|
||||
$branches = Branch::all();
|
||||
return view('basicdata::branch.create', compact('branch', 'branches'));
|
||||
}
|
||||
|
||||
public function update(BranchRequest $request, $id)
|
||||
@@ -82,6 +84,14 @@
|
||||
|
||||
$validate = $request->validated();
|
||||
|
||||
// Tambahkan validasi manual untuk memeriksa parent_id
|
||||
if (isset($validate['parent_id']) && $validate['parent_id'] == $id) {
|
||||
return redirect()
|
||||
->back()
|
||||
->withInput()
|
||||
->withErrors(['parent_id' => 'Cabang tidak dapat menjadi induk dari dirinya sendiri.']);
|
||||
}
|
||||
|
||||
if ($validate) {
|
||||
try {
|
||||
// Update in database
|
||||
@@ -102,12 +112,25 @@
|
||||
{
|
||||
// Check if the authenticated user has the required permission to delete branches
|
||||
if (is_null($this->user) || !$this->user->can('basic-data.delete')) {
|
||||
return response()->json(['success' => false, 'message' => 'Sorry! You are not allowed to delete branches.'], 403);
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'message' => 'Sorry! You are not allowed to delete branches.'
|
||||
], 403);
|
||||
}
|
||||
|
||||
try {
|
||||
// Delete from database
|
||||
// Find the branch
|
||||
$branch = Branch::find($id);
|
||||
|
||||
// Check if the branch has children
|
||||
if ($branch->children()->exists()) {
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'message' => 'Cabang dengan anak cabang tidak dapat dihapus.'
|
||||
], 422);
|
||||
}
|
||||
|
||||
// Delete from database
|
||||
$branch->delete();
|
||||
|
||||
return response()->json(['success' => true, 'message' => 'Branch deleted successfully']);
|
||||
@@ -120,10 +143,26 @@
|
||||
{
|
||||
// Check if the authenticated user has the required permission to delete branches
|
||||
if (is_null($this->user) || !$this->user->can('basic-data.delete')) {
|
||||
return response()->json(['success' => false, 'message' => 'Sorry! You are not allowed to delete branches.'], 403);
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'message' => 'Sorry! You are not allowed to delete branches.'
|
||||
], 403);
|
||||
}
|
||||
|
||||
$ids = $request->input('ids');
|
||||
|
||||
// Check if any of the branches have children
|
||||
$branchesWithChildren = Branch::whereIn('id', $ids)
|
||||
->whereHas('children')
|
||||
->get();
|
||||
|
||||
if ($branchesWithChildren->count() > 0) {
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'message' => 'Beberapa cabang memiliki anak cabang dan tidak dapat dihapus.'
|
||||
], 422);
|
||||
}
|
||||
|
||||
Branch::whereIn('id', $ids)->delete();
|
||||
return response()->json(['success' => true, 'message' => 'Branches deleted successfully']);
|
||||
}
|
||||
@@ -132,7 +171,10 @@
|
||||
{
|
||||
// Check if the authenticated user has the required permission to view branches
|
||||
if (is_null($this->user) || !$this->user->can('basic-data.read')) {
|
||||
return response()->json(['success' => false, 'message' => 'Sorry! You are not allowed to view branches.'], 403);
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'message' => 'Sorry! You are not allowed to view branches.'
|
||||
], 403);
|
||||
}
|
||||
|
||||
// Retrieve data from the database
|
||||
@@ -172,12 +214,22 @@
|
||||
// Get the data for the current page
|
||||
$data = $query->get();
|
||||
|
||||
$data = $data->map(function ($item) {
|
||||
return [
|
||||
'id' => $item->id,
|
||||
'code' => $item->code,
|
||||
'name' => $item->name,
|
||||
'parent_id' => $item->parent?->name ?? null,
|
||||
];
|
||||
});
|
||||
|
||||
// Calculate the page count
|
||||
$pageCount = ceil($totalRecords / $request->get('size'));
|
||||
|
||||
// Calculate the current page number
|
||||
$currentPage = 0 + 1;
|
||||
|
||||
|
||||
// Return the response data as a JSON object
|
||||
return response()->json([
|
||||
'draw' => $request->get('draw'),
|
||||
|
||||
@@ -15,6 +15,15 @@
|
||||
{
|
||||
$rules = [
|
||||
'name' => 'required|string|max:255',
|
||||
'parent_id' => [
|
||||
'nullable',
|
||||
'exists:branches,id',
|
||||
function ($attribute, $value, $fail) {
|
||||
if ($value == $this->route('branch')) {
|
||||
$fail('Cabang tidak dapat menjadi induk dari dirinya sendiri.');
|
||||
}
|
||||
},
|
||||
],
|
||||
'status' => 'nullable|boolean',
|
||||
'authorized_at' => 'nullable|datetime',
|
||||
'authorized_status' => 'nullable|string|max:1',
|
||||
|
||||
Reference in New Issue
Block a user