Compare commits
8 Commits
08582a2aa3
...
shola
| Author | SHA1 | Date | |
|---|---|---|---|
| 8fd8afd8df | |||
|
|
429a3a99fd | ||
| 2234ab2147 | |||
|
|
8d7a8d0fc7 | ||
| e24eaa963d | |||
|
|
254fe33028 | ||
| 0c9ba37486 | |||
|
|
b8cf5e550f |
@@ -1,43 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Basicdata\Models;
|
||||
namespace Modules\Basicdata\Models;
|
||||
|
||||
class Branch extends Base
|
||||
class Branch extends Base
|
||||
{
|
||||
protected $table = 'branches';
|
||||
protected $fillable = [
|
||||
'code',
|
||||
'name',
|
||||
'is_dalam_kota',
|
||||
'address',
|
||||
'mnemonic',
|
||||
'customer_company',
|
||||
'customer_mnemonic',
|
||||
'company_group',
|
||||
'curr_no',
|
||||
'co_code',
|
||||
'l_vendor_atm',
|
||||
'l_vendor_cpc',
|
||||
'status',
|
||||
'authorized_at',
|
||||
'authorized_status',
|
||||
'authorized_by',
|
||||
'parent_id'
|
||||
];
|
||||
|
||||
/**
|
||||
* Get the parent branch of this branch
|
||||
*/
|
||||
public function parent()
|
||||
{
|
||||
protected $table = 'branches';
|
||||
protected $fillable = [
|
||||
'code',
|
||||
'name',
|
||||
'is_dalam_kota',
|
||||
'address',
|
||||
'mnemonic',
|
||||
'customer_company',
|
||||
'customer_mnemonic',
|
||||
'company_group',
|
||||
'curr_no',
|
||||
'co_code',
|
||||
'l_vendor_atm',
|
||||
'l_vendor_cpc',
|
||||
'status',
|
||||
'authorized_at',
|
||||
'authorized_status',
|
||||
'authorized_by',
|
||||
'parent_id'
|
||||
];
|
||||
|
||||
/**
|
||||
* Get the parent branch of this branch
|
||||
*/
|
||||
public function parent()
|
||||
{
|
||||
return $this->belongsTo(Branch::class, 'parent_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the child branches of this branch
|
||||
*/
|
||||
public function children()
|
||||
{
|
||||
return $this->hasMany(Branch::class, 'parent_id');
|
||||
}
|
||||
return $this->belongsTo(Branch::class, 'parent_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the child branches of this branch
|
||||
*/
|
||||
public function children()
|
||||
{
|
||||
return $this->hasMany(Branch::class, 'parent_id');
|
||||
}
|
||||
|
||||
|
||||
public function users()
|
||||
{
|
||||
return $this->belongsToMany(Modules\Usermanagement\Models\User::class, 'user_branches', 'branch_id', 'user_id');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,39 +1,39 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration {
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up()
|
||||
: void
|
||||
{
|
||||
Schema::create('branches', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->string('code', 9)->unique();
|
||||
$table->string('name');
|
||||
$table->boolean('status')->default(true)->nullable();
|
||||
$table->timestamps();
|
||||
$table->timestamp('authorized_at')->nullable();
|
||||
$table->char('authorized_status', 1)->nullable();
|
||||
$table->softDeletes();
|
||||
return new class extends Migration {
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('branches', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->string('code', 9)->unique();
|
||||
$table->string('name');
|
||||
$table->boolean('status')->default(true)->nullable();
|
||||
$table->timestamps();
|
||||
$table->timestamp('authorized_at')->nullable();
|
||||
$table->char('authorized_status', 1)->nullable();
|
||||
$table->softDeletes();
|
||||
|
||||
$table->unsignedBigInteger('created_by')->nullable();
|
||||
$table->unsignedBigInteger('updated_by')->nullable();
|
||||
$table->unsignedBigInteger('deleted_by')->nullable();
|
||||
$table->unsignedBigInteger('authorized_by')->nullable();
|
||||
});
|
||||
}
|
||||
$table->unsignedBigInteger('created_by')->nullable();
|
||||
$table->unsignedBigInteger('updated_by')->nullable();
|
||||
$table->unsignedBigInteger('deleted_by')->nullable();
|
||||
$table->unsignedBigInteger('authorized_by')->nullable();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down()
|
||||
: void
|
||||
{
|
||||
Schema::dropIfExists('branches');
|
||||
}
|
||||
};
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::disableForeignKeyConstraints();
|
||||
Schema::dropIfExists('branches');
|
||||
Schema::enableForeignKeyConstraints();
|
||||
}
|
||||
};
|
||||
|
||||
@@ -3,18 +3,30 @@
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
return new class extends Migration {
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::table('branches', function (Blueprint $table) {
|
||||
$table->unsignedBigInteger('parent_id')->nullable()->after('name');
|
||||
$table->foreign('parent_id')->references('id')->on('branches')->onDelete('set null');
|
||||
// Tambah kolom parent_id jika belum ada
|
||||
if (!Schema::hasColumn('branches', 'parent_id')) {
|
||||
$table->unsignedBigInteger('parent_id')->nullable()->after('name');
|
||||
}
|
||||
});
|
||||
|
||||
// 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
|
||||
{
|
||||
// Drop foreign key jika ada
|
||||
if ($this->foreignKeyExists('branches', 'branches_parent_id_foreign')) {
|
||||
Schema::table('branches', function (Blueprint $table) {
|
||||
$table->dropForeign('branches_parent_id_foreign');
|
||||
});
|
||||
}
|
||||
|
||||
// Drop kolom jika ada
|
||||
Schema::table('branches', function (Blueprint $table) {
|
||||
$table->dropForeign(['parent_id']);
|
||||
$table->dropColumn('parent_id');
|
||||
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;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -18,7 +18,7 @@ class BranchesSeeder extends Seeder
|
||||
$branches = [
|
||||
[
|
||||
'code' => 'ID0010001',
|
||||
'name' => 'PT. Bank Artha Graha',
|
||||
'name' => 'KPNO',
|
||||
'status' => true,
|
||||
'created_at' => $now,
|
||||
'updated_at' => $now
|
||||
@@ -186,7 +186,7 @@ class BranchesSeeder extends Seeder
|
||||
],
|
||||
[
|
||||
'code' => 'ID0010031',
|
||||
'name' => 'BKR Bandung - KCP',
|
||||
'name' => 'BKR BANDUNG - KCP',
|
||||
'status' => true,
|
||||
'created_at' => $now,
|
||||
'updated_at' => $now
|
||||
@@ -235,14 +235,14 @@ class BranchesSeeder extends Seeder
|
||||
],
|
||||
[
|
||||
'code' => 'ID0010039',
|
||||
'name' => 'Cimahi - KCP',
|
||||
'name' => 'CIMAHI - KCP',
|
||||
'status' => true,
|
||||
'created_at' => $now,
|
||||
'updated_at' => $now
|
||||
],
|
||||
[
|
||||
'code' => 'ID0010050',
|
||||
'name' => 'Ir.Soekarno(MERR)-KCP',
|
||||
'name' => 'IR.SOEKARNO(MERR)-KCP',
|
||||
'status' => true,
|
||||
'created_at' => $now,
|
||||
'updated_at' => $now
|
||||
@@ -319,7 +319,7 @@ class BranchesSeeder extends Seeder
|
||||
],
|
||||
[
|
||||
'code' => 'ID0010063',
|
||||
'name' => 'Diponegoro Denpasar KK',
|
||||
'name' => 'DIPONEGORO DENPASAR KK',
|
||||
'status' => true,
|
||||
'created_at' => $now,
|
||||
'updated_at' => $now
|
||||
@@ -494,7 +494,7 @@ class BranchesSeeder extends Seeder
|
||||
],
|
||||
[
|
||||
'code' => 'ID0010108',
|
||||
'name' => 'Tzu Chi - KK',
|
||||
'name' => 'TZU CHI - KK',
|
||||
'status' => true,
|
||||
'created_at' => $now,
|
||||
'updated_at' => $now
|
||||
@@ -557,7 +557,7 @@ class BranchesSeeder extends Seeder
|
||||
],
|
||||
[
|
||||
'code' => 'ID0010131',
|
||||
'name' => 'Veteran Makassar - KCP',
|
||||
'name' => 'VETERAN MAKASSAR - KCP',
|
||||
'status' => true,
|
||||
'created_at' => $now,
|
||||
'updated_at' => $now
|
||||
@@ -704,7 +704,7 @@ class BranchesSeeder extends Seeder
|
||||
],
|
||||
[
|
||||
'code' => 'ID0010272',
|
||||
'name' => 'Setiabudi Kuningan KK',
|
||||
'name' => 'SETIABUDI KUNINGAN KK',
|
||||
'status' => true,
|
||||
'created_at' => $now,
|
||||
'updated_at' => $now
|
||||
|
||||
Reference in New Issue
Block a user