Compare commits

...

10 Commits

Author SHA1 Message Date
8fd8afd8df Merge pull request 'feat(migration): add parent_id column and foreign key to branches table with checks' (#7) from shola into master
Reviewed-on: #7
2025-12-23 16:59:48 +07:00
Sholahuddin Al Ayubi
429a3a99fd feat(migration): add parent_id column and foreign key to branches table with checks 2025-12-23 16:56:09 +07:00
2234ab2147 Merge pull request 'fix(seeder): standardize branch names to uppercase in BranchesSeeder' (#5) from shola into master
Reviewed-on: #5
2025-12-23 15:40:48 +07:00
Sholahuddin Al Ayubi
8d7a8d0fc7 fix(seeder): standardize branch names to uppercase in BranchesSeeder 2025-12-23 15:38:49 +07:00
e24eaa963d Merge pull request 'feat(branches): update branch names for consistency and formatting' (#4) from shola into master
Reviewed-on: #4
2025-12-23 10:16:08 +07:00
Sholahuddin Al Ayubi
254fe33028 feat(branches): update branch names for consistency and formatting 2025-12-23 10:14:08 +07:00
0c9ba37486 Merge pull request 'refactor(branch): clean up formatting and structure in Branch model' (#3) from shola into master
Reviewed-on: #3
2025-12-11 17:24:33 +07:00
Sholahuddin Al Ayubi
b8cf5e550f refactor(branch): clean up formatting and structure in Branch model 2025-12-11 17:22:45 +07:00
08582a2aa3 Merge pull request 'feat(branch): add 'is_dalam_kota' field to Branch form and validation rules' (#2) from shola into master
Reviewed-on: #2
2025-12-11 15:32:06 +07:00
Sholahuddin Al Ayubi
0526a1bb05 feat(branch): add 'is_dalam_kota' field to Branch form and validation rules 2025-12-11 15:30:20 +07:00
6 changed files with 249 additions and 161 deletions

View File

@@ -28,6 +28,7 @@
'authorized_at' => 'nullable|datetime', 'authorized_at' => 'nullable|datetime',
'authorized_status' => 'nullable|string|max:1', 'authorized_status' => 'nullable|string|max:1',
'authorized_by' => 'nullable|exists:users,id', 'authorized_by' => 'nullable|exists:users,id',
'is_dalam_kota' => 'nullable|in:0,1',
]; ];
if ($this->method() == 'PUT') { if ($this->method() == 'PUT') {

View File

@@ -1,9 +1,9 @@
<?php <?php
namespace Modules\Basicdata\Models; namespace Modules\Basicdata\Models;
class Branch extends Base class Branch extends Base
{ {
protected $table = 'branches'; protected $table = 'branches';
protected $fillable = [ protected $fillable = [
'code', 'code',
@@ -40,4 +40,10 @@
{ {
return $this->hasMany(Branch::class, 'parent_id'); return $this->hasMany(Branch::class, 'parent_id');
} }
public function users()
{
return $this->belongsToMany(Modules\Usermanagement\Models\User::class, 'user_branches', 'branch_id', 'user_id');
} }
}

View File

@@ -1,15 +1,14 @@
<?php <?php
use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema; use Illuminate\Support\Facades\Schema;
return new class extends Migration { return new class extends Migration {
/** /**
* Run the migrations. * Run the migrations.
*/ */
public function up() public function up(): void
: void
{ {
Schema::create('branches', function (Blueprint $table) { Schema::create('branches', function (Blueprint $table) {
$table->id(); $table->id();
@@ -31,9 +30,10 @@
/** /**
* Reverse the migrations. * Reverse the migrations.
*/ */
public function down() public function down(): void
: void
{ {
Schema::disableForeignKeyConstraints();
Schema::dropIfExists('branches'); Schema::dropIfExists('branches');
Schema::enableForeignKeyConstraints();
} }
}; };

View File

@@ -3,18 +3,30 @@
use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema; use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\DB;
return new class extends Migration return new class extends Migration {
{
/** /**
* Run the migrations. * Run the migrations.
*/ */
public function up(): void public function up(): void
{ {
Schema::table('branches', function (Blueprint $table) { Schema::table('branches', function (Blueprint $table) {
// Tambah kolom parent_id jika belum ada
if (!Schema::hasColumn('branches', 'parent_id')) {
$table->unsignedBigInteger('parent_id')->nullable()->after('name'); $table->unsignedBigInteger('parent_id')->nullable()->after('name');
$table->foreign('parent_id')->references('id')->on('branches')->onDelete('set null'); }
}); });
// Tambah foreign key jika belum ada
if (!$this->foreignKeyExists('branches', 'branches_parent_id_foreign')) {
Schema::table('branches', function (Blueprint $table) {
$table->foreign('parent_id', 'branches_parent_id_foreign')
->references('id')
->on('branches')
->onDelete('set null');
});
}
} }
/** /**
@@ -22,9 +34,39 @@ return new class extends Migration
*/ */
public function down(): void public function down(): void
{ {
// Drop foreign key jika ada
if ($this->foreignKeyExists('branches', 'branches_parent_id_foreign')) {
Schema::table('branches', function (Blueprint $table) { Schema::table('branches', function (Blueprint $table) {
$table->dropForeign(['parent_id']); $table->dropForeign('branches_parent_id_foreign');
$table->dropColumn('parent_id');
}); });
} }
// Drop kolom jika ada
Schema::table('branches', function (Blueprint $table) {
if (Schema::hasColumn('branches', 'parent_id')) {
$table->dropColumn('parent_id');
}
});
}
/**
* Cek apakah foreign key exists
*/
private function foreignKeyExists(string $table, string $name): bool
{
$conn = Schema::getConnection();
$dbName = $conn->getDatabaseName();
$result = DB::select(
"SELECT CONSTRAINT_NAME
FROM information_schema.TABLE_CONSTRAINTS
WHERE TABLE_SCHEMA = ?
AND TABLE_NAME = ?
AND CONSTRAINT_NAME = ?
AND CONSTRAINT_TYPE = 'FOREIGN KEY'",
[$dbName, $table, $name]
);
return count($result) > 0;
}
}; };

View File

@@ -18,7 +18,7 @@ class BranchesSeeder extends Seeder
$branches = [ $branches = [
[ [
'code' => 'ID0010001', 'code' => 'ID0010001',
'name' => 'PT. Bank Artha Graha', 'name' => 'KPNO',
'status' => true, 'status' => true,
'created_at' => $now, 'created_at' => $now,
'updated_at' => $now 'updated_at' => $now
@@ -186,7 +186,7 @@ class BranchesSeeder extends Seeder
], ],
[ [
'code' => 'ID0010031', 'code' => 'ID0010031',
'name' => 'BKR Bandung - KCP', 'name' => 'BKR BANDUNG - KCP',
'status' => true, 'status' => true,
'created_at' => $now, 'created_at' => $now,
'updated_at' => $now 'updated_at' => $now
@@ -235,14 +235,14 @@ class BranchesSeeder extends Seeder
], ],
[ [
'code' => 'ID0010039', 'code' => 'ID0010039',
'name' => 'Cimahi - KCP', 'name' => 'CIMAHI - KCP',
'status' => true, 'status' => true,
'created_at' => $now, 'created_at' => $now,
'updated_at' => $now 'updated_at' => $now
], ],
[ [
'code' => 'ID0010050', 'code' => 'ID0010050',
'name' => 'Ir.Soekarno(MERR)-KCP', 'name' => 'IR.SOEKARNO(MERR)-KCP',
'status' => true, 'status' => true,
'created_at' => $now, 'created_at' => $now,
'updated_at' => $now 'updated_at' => $now
@@ -319,7 +319,7 @@ class BranchesSeeder extends Seeder
], ],
[ [
'code' => 'ID0010063', 'code' => 'ID0010063',
'name' => 'Diponegoro Denpasar KK', 'name' => 'DIPONEGORO DENPASAR KK',
'status' => true, 'status' => true,
'created_at' => $now, 'created_at' => $now,
'updated_at' => $now 'updated_at' => $now
@@ -494,7 +494,7 @@ class BranchesSeeder extends Seeder
], ],
[ [
'code' => 'ID0010108', 'code' => 'ID0010108',
'name' => 'Tzu Chi - KK', 'name' => 'TZU CHI - KK',
'status' => true, 'status' => true,
'created_at' => $now, 'created_at' => $now,
'updated_at' => $now 'updated_at' => $now
@@ -557,7 +557,7 @@ class BranchesSeeder extends Seeder
], ],
[ [
'code' => 'ID0010131', 'code' => 'ID0010131',
'name' => 'Veteran Makassar - KCP', 'name' => 'VETERAN MAKASSAR - KCP',
'status' => true, 'status' => true,
'created_at' => $now, 'created_at' => $now,
'updated_at' => $now 'updated_at' => $now
@@ -704,7 +704,7 @@ class BranchesSeeder extends Seeder
], ],
[ [
'code' => 'ID0010272', 'code' => 'ID0010272',
'name' => 'Setiabudi Kuningan KK', 'name' => 'SETIABUDI KUNINGAN KK',
'status' => true, 'status' => true,
'created_at' => $now, 'created_at' => $now,
'updated_at' => $now 'updated_at' => $now

View File

@@ -6,7 +6,7 @@
@section('content') @section('content')
<div class="w-full grid gap-5 lg:gap-7.5 mx-auto"> <div class="w-full grid gap-5 lg:gap-7.5 mx-auto">
@if(isset($branch->id)) @if (isset($branch->id))
<form action="{{ route('basicdata.branch.update', $branch->id) }}" method="POST"> <form action="{{ route('basicdata.branch.update', $branch->id) }}" method="POST">
<input type="hidden" name="id" value="{{ $branch->id }}"> <input type="hidden" name="id" value="{{ $branch->id }}">
@method('PUT') @method('PUT')
@@ -20,7 +20,8 @@
{{ isset($branch->id) ? 'Edit' : 'Tambah' }} Branch {{ isset($branch->id) ? 'Edit' : 'Tambah' }} Branch
</h3> </h3>
<div class="flex items-center gap-2"> <div class="flex items-center gap-2">
<a href="{{ route('basicdata.branch.index') }}" class="btn btn-xs btn-info"><i class="ki-filled ki-exit-left"></i> Back</a> <a href="{{ route('basicdata.branch.index') }}" class="btn btn-xs btn-info"><i
class="ki-filled ki-exit-left"></i> Back</a>
</div> </div>
</div> </div>
<div class="card-body grid gap-5"> <div class="card-body grid gap-5">
@@ -29,7 +30,8 @@
Code Code
</label> </label>
<div class="flex flex-wrap items-baseline w-full"> <div class="flex flex-wrap items-baseline w-full">
<input class="input @error('code') border-danger bg-danger-light @enderror" type="text" name="code" value="{{ $branch->code ?? '' }}"> <input class="input @error('code') border-danger bg-danger-light @enderror" type="text"
name="code" value="{{ $branch->code ?? '' }}">
@error('code') @error('code')
<em class="alert text-danger text-sm">{{ $message }}</em> <em class="alert text-danger text-sm">{{ $message }}</em>
@enderror @enderror
@@ -40,12 +42,48 @@
Name Name
</label> </label>
<div class="flex flex-wrap items-baseline w-full"> <div class="flex flex-wrap items-baseline w-full">
<input class="input @error('name') border-danger bg-danger-light @enderror" type="text" name="name" value="{{ $branch->name ?? '' }}"> <input class="input @error('name') border-danger bg-danger-light @enderror" type="text"
name="name" value="{{ $branch->name ?? '' }}">
@error('name') @error('name')
<em class="alert text-danger text-sm">{{ $message }}</em> <em class="alert text-danger text-sm">{{ $message }}</em>
@enderror @enderror
</div> </div>
</div> </div>
<div class="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
<label class="form-label max-w-56">
Branch Type
</label>
<div class="flex flex-wrap items-baseline w-full">
@php
$selectedDalamKota = old(
'is_dalam_kota',
isset($branch->is_dalam_kota) ? $branch->is_dalam_kota : '',
);
@endphp
<select class="input @error('is_dalam_kota') border-danger bg-danger-light @enderror"
name="is_dalam_kota" id="is_dalam_kota">
<!-- DEFAULT PILIHAN SAAT CREATE -->
<option value="">-- Select Branch Location --</option>
<option value="1" {{ $selectedDalamKota == 1 ? 'selected' : '' }}>
Dalam Kota
</option>
<option value="0" {{ $selectedDalamKota == 0 ? 'selected' : '' }}>
Luar Kota
</option>
</select>
@error('is_dalam_kota')
<em class="alert text-danger text-sm">{{ $message }}</em>
@enderror
</div>
</div>
<div class="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5"> <div class="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
<label class="form-label max-w-56"> <label class="form-label max-w-56">
Parent Branch Parent Branch
@@ -53,9 +91,10 @@
<div class="flex flex-wrap items-baseline w-full"> <div class="flex flex-wrap items-baseline w-full">
<select class="input @error('parent_id') border-danger bg-danger-light @enderror" name="parent_id"> <select class="input @error('parent_id') border-danger bg-danger-light @enderror" name="parent_id">
<option value="">-- Select Parent Branch --</option> <option value="">-- Select Parent Branch --</option>
@foreach($branches as $parentBranch) @foreach ($branches as $parentBranch)
@if(!isset($branch->id) || $parentBranch->id != $branch->id) @if (!isset($branch->id) || $parentBranch->id != $branch->id)
<option value="{{ $parentBranch->id }}" {{ (isset($branch->parent_id) && $branch->parent_id == $parentBranch->id) ? 'selected' : '' }}> <option value="{{ $parentBranch->id }}"
{{ isset($branch->parent_id) && $branch->parent_id == $parentBranch->id ? 'selected' : '' }}>
{{ $parentBranch->code }} - {{ $parentBranch->name }} {{ $parentBranch->code }} - {{ $parentBranch->name }}
</option> </option>
@endif @endif
@@ -67,7 +106,7 @@
</div> </div>
</div> </div>
<div class="flex justify-end"> <div class="flex justify-end">
@if(isset($branch->id)) @if (isset($branch->id))
@can('basic-data.update') @can('basic-data.update')
<button type="submit" class="btn btn-primary"> <button type="submit" class="btn btn-primary">
Save Save