Merged in feature/BK001-add-migrations-palsu (pull request #316)

added migrations for palsu and inserting semporna data into table palsu by console commands
This commit is contained in:
Muhd hafifie
2025-07-05 23:06:59 +00:00
4 changed files with 559 additions and 0 deletions
+345
View File
@@ -0,0 +1,345 @@
<?php
namespace App\Console\Commands;
use App\Cawangan;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
class InsertSempornaData extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'palsu:insert-data {kodcaw=011237 : The kodcaw value to insert data for}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Insert sample data into palsu table for specific kodcaw';
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$kodcaw = $this->argument('kodcaw');
$this->info("Inserting data for kodcaw: {$kodcaw}");
// Validate if kodcaw exists in Cawangan table
$cawangan = DB::connection('mysql7')->table('arrahn_master_db')->where('kodcaw', $kodcaw)->first();
if (!$cawangan) {
$this->error("Error: kodcaw '{$kodcaw}' not found in Cawangan table!");
return Command::FAILURE;
}
// Convert stdClass to array if needed
$cawangan = (array) $cawangan;
if (!isset($cawangan['mysql']) || empty($cawangan['mysql'])) {
$this->error("Error: Database connection not found for kodcaw '{$kodcaw}'!");
return Command::FAILURE;
}
$this->info("Using database connection: {$cawangan['mysql']}");
$data = [
[
'kodcaw' => $kodcaw,
'tarikh' => '2024-11-28',
'norujukan' => '011237240808-0003',
'teller' => 'DM',
'nota' => 'Barang Kes Semporna 28/11/24',
'nilaimarhun' => 1748.20,
'jbg' => 4,
'untung' => 78.00,
'untungonepercent' => 52.00,
'untungcajlain' => 26.00,
'pembiayaan' => 1300.00,
],
[
'kodcaw' => $kodcaw,
'tarikh' => '2024-11-28',
'norujukan' => '011237240719-0015',
'teller' => 'DM',
'nota' => 'Barang Kes Semporna 28/11/24',
'nilaimarhun' => 8499.80,
'jbg' => 4.5,
'untung' => 519.30,
'untungonepercent' => 305.55,
'untungcajlain' => 213.75,
'pembiayaan' => 6790.00,
],
[
'kodcaw' => $kodcaw,
'tarikh' => '2024-11-28',
'norujukan' => '011237240806-0011',
'teller' => 'DM',
'nota' => 'Barang Kes Semporna 28/11/24',
'nilaimarhun' => 8740.98,
'jbg' => 4,
'untung' => 475.40,
'untungonepercent' => 279.60,
'untungcajlain' => 195.80,
'pembiayaan' => 6990.00,
],
[
'kodcaw' => $kodcaw,
'tarikh' => '2024-11-28',
'norujukan' => '011237240828-0001',
'teller' => 'DM',
'nota' => 'Barang Kes Semporna 28/11/24',
'nilaimarhun' => 8121.20,
'jbg' => 3,
'untung' => 316.65,
'untungonepercent' => 182.70,
'untungcajlain' => 133.95,
'pembiayaan' => 6090.00,
],
[
'kodcaw' => $kodcaw,
'tarikh' => '2024-11-28',
'norujukan' => '011237240904-0007',
'teller' => 'DM',
'nota' => 'Barang Kes Semporna 28/11/24',
'nilaimarhun' => 16135.20,
'jbg' => 3,
'untung' => 617.10,
'untungonepercent' => 363.00,
'untungcajlain' => 254.10,
'pembiayaan' => 12100.00,
],
[
'kodcaw' => $kodcaw,
'tarikh' => '2024-11-28',
'norujukan' => '011237240904-0008',
'teller' => 'DM',
'nota' => 'Barang Kes Semporna 28/11/24',
'nilaimarhun' => 12101.40,
'jbg' => 3,
'untung' => 462.60,
'untungonepercent' => 272.10,
'untungcajlain' => 190.50,
'pembiayaan' => 9070.00,
],
[
'kodcaw' => $kodcaw,
'tarikh' => '2024-11-28',
'norujukan' => '011237240904-0009',
'teller' => 'DM',
'nota' => 'Barang Kes Semporna 28/11/24',
'nilaimarhun' => 6050.70,
'jbg' => 3,
'untung' => 231.00,
'untungonepercent' => 135.90,
'untungcajlain' => 95.10,
'pembiayaan' => 4530.00,
],
[
'kodcaw' => $kodcaw,
'tarikh' => '2024-11-28',
'norujukan' => '011237240719-0023',
'teller' => 'DM',
'nota' => 'Barang Kes Semporna 28/11/24',
'nilaimarhun' => 19001.24,
'jbg' => 4.5,
'untung' => 149.18,
'untungonepercent' => 87.75,
'untungcajlain' => 61.43,
'pembiayaan' => 1950.00,
],
[
'kodcaw' => $kodcaw,
'tarikh' => '2024-11-28',
'norujukan' => '011237240719-0024',
'teller' => 'DM',
'nota' => 'Barang Kes Semporna 28/11/24',
'nilaimarhun' => 8499.80,
'jbg' => 4.5,
'untung' => 65.03,
'untungonepercent' => 38.25,
'untungcajlain' => 26.78,
'pembiayaan' => 850.00,
],
[
'kodcaw' => $kodcaw,
'tarikh' => '2024-11-28',
'norujukan' => '011237240719-0025',
'teller' => 'DM',
'nota' => 'Barang Kes Semporna 28/11/24',
'nilaimarhun' => 15972.57,
'jbg' => 4.5,
'untung' => 122.40,
'untungonepercent' => 72.00,
'untungcajlain' => 50.40,
'pembiayaan' => 1600.00,
],
[
'kodcaw' => $kodcaw,
'tarikh' => '2024-11-28',
'norujukan' => '011237240618-0026',
'teller' => 'DM',
'nota' => 'Barang Kes Semporna 28/11/24',
'nilaimarhun' => 8552.90,
'jbg' => 5.5,
'untung' => 187.00,
'untungonepercent' => 110.00,
'untungcajlain' => 77.00,
'pembiayaan' => 2000.00,
],
[
'kodcaw' => $kodcaw,
'tarikh' => '2024-11-28',
'norujukan' => '011237240719-0027',
'teller' => 'DM',
'nota' => 'Barang Kes Semporna 28/11/24',
'nilaimarhun' => 42923.11,
'jbg' => 4.5,
'untung' => 328.95,
'untungonepercent' => 193.50,
'untungcajlain' => 135.45,
'pembiayaan' => 4300.00,
],
[
'kodcaw' => $kodcaw,
'tarikh' => '2024-11-28',
'norujukan' => '011237240719-0028',
'teller' => 'DM',
'nota' => 'Barang Kes Semporna 28/11/24',
'nilaimarhun' => 41872.04,
'jbg' => 4.5,
'untung' => 321.30,
'untungonepercent' => 189.00,
'untungcajlain' => 132.30,
'pembiayaan' => 4200.00,
],
[
'kodcaw' => $kodcaw,
'tarikh' => '2024-11-28',
'norujukan' => '011237240618-0020',
'teller' => 'DM',
'nota' => 'Barang Kes Semporna 28/11/24',
'nilaimarhun' => 15495.86,
'jbg' => 5.5,
'untung' => 280.50,
'untungonepercent' => 165.00,
'untungcajlain' => 115.50,
'pembiayaan' => 3000.00,
],
[
'kodcaw' => $kodcaw,
'tarikh' => '2024-11-28',
'norujukan' => '011237240719-0030',
'teller' => 'DM',
'nota' => 'Barang Kes Semporna 28/11/24',
'nilaimarhun' => 17483.01,
'jbg' => 4.5,
'untung' => 133.88,
'untungonepercent' => 78.75,
'untungcajlain' => 55.13,
'pembiayaan' => 1750.00,
],
[
'kodcaw' => $kodcaw,
'tarikh' => '2024-11-28',
'norujukan' => '011237240618-0029',
'teller' => 'DM',
'nota' => 'Barang Kes Semporna 28/11/24',
'nilaimarhun' => 10062.25,
'jbg' => 5.5,
'untung' => 280.50,
'untungonepercent' => 165.00,
'untungcajlain' => 115.50,
'pembiayaan' => 3000.00,
],
[
'kodcaw' => $kodcaw,
'tarikh' => '2024-11-28',
'norujukan' => '011237240731-0026',
'teller' => 'DM',
'nota' => 'Barang Kes Semporna 28/11/24',
'nilaimarhun' => 10283.50,
'jbg' => 4.5,
'untung' => 558.45,
'untungonepercent' => 328.50,
'untungcajlain' => 229.95,
'pembiayaan' => 7300.00,
],
[
'kodcaw' => $kodcaw,
'tarikh' => '2024-11-28',
'norujukan' => '011237240715-0027',
'teller' => 'DM',
'nota' => 'Barang Kes Semporna 28/11/24',
'nilaimarhun' => 29624.77,
'jbg' => 4.5,
'untung' => 1147.50,
'untungonepercent' => 675.00,
'untungcajlain' => 472.50,
'pembiayaan' => 15000.00,
],
[
'kodcaw' => $kodcaw,
'tarikh' => '2024-11-28',
'norujukan' => '011237240912-0009',
'teller' => 'DM',
'nota' => 'Barang Kes Semporna 28/11/24',
'nilaimarhun' => 12654.35,
'jbg' => 3,
'untung' => 428.40,
'untungonepercent' => 252.00,
'untungcajlain' => 176.40,
'pembiayaan' => 8400.00,
],
];
$insertedCount = 0;
$skippedCount = 0;
// Start database transaction
DB::connection($cawangan['mysql'])->beginTransaction();
try {
foreach ($data as $record) {
// Check if record already exists
$exists = DB::connection($cawangan['mysql'])->table('palsu')
->where('kodcaw', $record['kodcaw'])
->where('norujukan', $record['norujukan'])
->exists();
if (!$exists) {
DB::connection($cawangan['mysql'])->table('palsu')->insert($record);
$insertedCount++;
$this->line("✓ Inserted: {$record['norujukan']}");
} else {
$skippedCount++;
$this->line("- Skipped: {$record['norujukan']} (already exists)");
}
}
// Commit the transaction if everything is successful
DB::connection($cawangan['mysql'])->commit();
$this->info("✓ Transaction committed successfully!");
} catch (\Exception $e) {
// Rollback the transaction if any error occurs
DB::connection($cawangan['mysql'])->rollback();
$this->error("✗ Transaction failed and rolled back: " . $e->getMessage());
return Command::FAILURE;
}
$this->info("\nSummary:");
$this->info("Inserted: {$insertedCount} records");
$this->info("Skipped: {$skippedCount} records");
return Command::SUCCESS;
}
}
+2
View File
@@ -7,6 +7,7 @@ use Illuminate\Console\Scheduling\Schedule;
use Laravel\Lumen\Console\Kernel as ConsoleKernel;
use App\Jobs\WhatsappUnsoldCommodityScanner;
use Faker\Provider\ar_SA\Color;
use Illuminate\Console\Command;
class Kernel extends ConsoleKernel
@@ -22,6 +23,7 @@ class Kernel extends ConsoleKernel
Commands\CawanganDebugConnections::class,
Commands\ClearCachesAll::class,
Commands\RestartSupervisor::class,
Commands\InsertSempornaData::class,
];
+90
View File
@@ -0,0 +1,90 @@
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use App\Audit;
use Illuminate\Support\Carbon;
class Palsu extends Model
{
protected $table = 'palsu';
protected $primaryKey = 'recno';
public $timestamps = false;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'recno',
'kodcaw',
'norujukan',
'teller',
'nota',
'nilaimarhun',
'jbg',
'untung',
'untungonepercent',
'untungcajlain',
'pembiayaan',
];
// public static function boot()
// {
// parent::boot();
// $current = Carbon::now();
// $current = new Carbon();
// static::created(function (Poskod $poskod) use($current){
// Audit::create([
// 'users' => request()->all()['user']['name'],
// 'actions' => 'Tambah poskod',
// 'norujukan' => $poskod->recno,
// 'new_data' => $poskod->toJson(),
// 'old_data' => '',
// 'kodcaw' => request()->all()['user']['cawangan'],
// 'created_at' => $current,
// 'updated_at' => $current,
// ]);
// });
// static::updated(function (Poskod $poskod) use($current) {
// Audit::create([
// 'users' => request()->all()['user']['name'],
// 'actions' => 'Kemaskini poskod',
// 'norujukan' => $poskod->recno,
// 'new_data' => $poskod->toJson(),
// 'old_data' => json_encode($poskod->getOriginal()),
// 'kodcaw' => request()->all()['user']['cawangan'],
// 'created_at' => $current,
// 'updated_at' => $current,
// ]);
// });
// }
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
// protected $hidden = [],
// public function negeri(): BelongsTo
// {
// return $this->belongsTo(Negeri::class, 'kodnegeri', 'kodnegeri');
// }
// public function daerah(): BelongsTo
// {
// return $this->belongsTo(Daerah::class, 'koddaerah', 'koddaerah');
// }
}
@@ -0,0 +1,122 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
class AddNmJbgUntungPembiayaanColumnsToPalsuTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
private function getActiveDB() : array
{
$config_db = array_keys(config("database.connections"));
$db_connection = array_filter($config_db, function($connection) {
try {
if ($connection == 'mysql7') {
return false;
}
DB::connection($connection)->select('SELECT 1');
return true;
} catch (\Exception $e) {
return false;
}
});
return $db_connection ? array_values($db_connection) : [];
}
public function up()
{
$db_connection = $this->getActiveDB();
foreach ($db_connection as $dbase) {
Schema::connection($dbase)->table('palsu', function (Blueprint $table) use ($dbase) {
if (!Schema::connection($dbase)->hasColumn('palsu', 'pembiayaan')) {
$table->decimal('pembiayaan', 11, 2)->nullable()->comment('Pembiayaan amount');
}
if (!Schema::connection($dbase)->hasColumn('palsu', 'nilaimarhun')) {
$table->decimal('nilaimarhun', 11, 2)->nullable()->comment('Nilai marhun amount');
}
if (!Schema::connection($dbase)->hasColumn('palsu', 'jbg')) {
$table->float('jbg', 4, 2)->nullable()->comment('JBG value');
}
if (!Schema::connection($dbase)->hasColumn('palsu', 'untung')) {
$table->decimal('untung', 11, 2)->nullable()->comment('Untung amount');
}
if (!Schema::connection($dbase)->hasColumn('palsu', 'untungonepercent')) {
$table->decimal('untungonepercent', 11, 2)->nullable()->comment('Satu Persen amount');
}
if (!Schema::connection($dbase)->hasColumn('palsu', 'untungcajlain')) {
$table->decimal('untungcajlain', 11, 2)->nullable()->comment('Caj Lain-Lain amount');
}
// Add index and foreign key constraint to existing norujukan column
if (Schema::connection($dbase)->hasColumn('palsu', 'norujukan')) {
// Add index to norujukan column for better performance
$table->index('norujukan');
// Add foreign key constraint named 'norujukan'
$table->foreign('norujukan', 'norujukan')->references('norujukan')->on('gadai');
}
});
}
}
public function down()
{
$db_connection = $this->getActiveDB();
foreach ($db_connection as $dbase) {
Schema::connection($dbase)->table('palsu', function (Blueprint $table) use ($dbase) {
// Drop foreign key constraint and index from norujukan column
if (Schema::connection($dbase)->hasColumn('palsu', 'norujukan')) {
try {
$table->dropForeign('norujukan');
echo "Dropped foreign key 'norujukan' on connection '{$dbase}'\n";
} catch (\Exception $e) {
echo "Warning: Could not drop foreign key 'norujukan' on connection '{$dbase}': " . $e->getMessage() . "\n";
}
try {
$table->dropIndex(['norujukan']);
echo "Dropped index on 'norujukan' column on connection '{$dbase}'\n";
} catch (\Exception $e) {
echo "Warning: Could not drop index on 'norujukan' on connection '{$dbase}': " . $e->getMessage() . "\n";
}
}
// Drop other columns if they exist
$columnsToCheck = ['nilaimarhun', 'jbg', 'untung','untungonepercent','untungcajlain', 'pembiayaan'];
foreach ($columnsToCheck as $column) {
if (Schema::connection($dbase)->hasColumn('palsu', $column)) {
try {
$table->dropColumn($column);
echo "Dropped column '{$column}' on connection '{$dbase}'\n";
} catch (\Exception $e) {
echo "Warning: Could not drop column '{$column}' on connection '{$dbase}': " . $e->getMessage() . "\n";
}
}
}
});
}
}
}