update data pemanding dan otorisator, pembuatan seeder kjpp mengunakan exel
This commit is contained in:
@@ -4,14 +4,271 @@ namespace Modules\Lpj\Database\Seeders;
|
||||
|
||||
use Illuminate\Database\Seeder;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Maatwebsite\Excel\Facades\Excel;
|
||||
use Illuminate\Support\Str;
|
||||
use Modules\Lpj\Models\KJPP;
|
||||
use Modules\Lpj\Models\JenisJaminan;
|
||||
use Modules\Lpj\Models\IjinUsaha;
|
||||
use Modules\Location\Models\City;
|
||||
use Modules\Location\Models\District;
|
||||
use Modules\Location\Models\Village;
|
||||
|
||||
class KJPPSeeder extends Seeder
|
||||
{
|
||||
/**
|
||||
* Run the database seeds.
|
||||
*/
|
||||
|
||||
public function run(): void
|
||||
{
|
||||
DB::unprepared(file_get_contents(__DIR__ . '/kjpp.sql'));
|
||||
$filePath = __DIR__ . '/LISTsatsa.xlsx';
|
||||
// DB::unprepared(file_get_contents(__DIR__ . '/kjpp.sql'));
|
||||
|
||||
if (!file_exists($filePath)) {
|
||||
$this->command->error('File excel tidak ditemukan di: ' . $filePath);
|
||||
return;
|
||||
}
|
||||
|
||||
$this->command->info('Membaca file Excel...');
|
||||
|
||||
try {
|
||||
// Baca excel dan tampilkan raw data
|
||||
$collection = Excel::toCollection(null, $filePath, null, function($reader) {
|
||||
$reader->setActiveSheetIndex(1);
|
||||
})->first();
|
||||
|
||||
// Debug: Tampilkan jumlah baris
|
||||
$this->command->info('Total baris: ' . $collection->count());
|
||||
|
||||
// Debug: Tampilkan 3 baris pertama (header)
|
||||
$this->command->info('Header data:');
|
||||
foreach($collection->take(3) as $index => $row) {
|
||||
$this->command->info("Baris $index: " . print_r($row->toArray(), true));
|
||||
}
|
||||
|
||||
// Skip 3 baris header
|
||||
$data = $collection->skip(3);
|
||||
|
||||
// Debug: Tampilkan jumlah data setelah skip header
|
||||
$this->command->info('Total data setelah skip header: ' . $data->count());
|
||||
|
||||
// Proses setiap baris
|
||||
foreach ($data as $index => $row) {
|
||||
$this->command->info("Memproses baris ke-$index:");
|
||||
|
||||
// Debug: Tampilkan data mentah
|
||||
$this->command->info('Raw data: ' . print_r($row->toArray(), true));
|
||||
|
||||
// Skip jika baris kosong
|
||||
if (empty($row[2])) {
|
||||
$this->command->warn("Baris $index dilewati karena nama KJPP kosong");
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
$locationData = $this->checkKota($row[5]);
|
||||
|
||||
$jenisUsaha = $this->checkJenisUsaha($row[17]);
|
||||
$jenisAsset = $this->checkJenisAsset($row[18]);
|
||||
|
||||
try {
|
||||
$dataToInsert = [
|
||||
'code' => $this->generateKJPPCode(),
|
||||
'name' => $row[2],
|
||||
'jenis_kantor' => "Kantor " . $row[3],
|
||||
'nomor_ijin_usaha' => $row[4],
|
||||
'province_code' => $locationData['province'],
|
||||
'city_code' => $locationData['city'],
|
||||
'district_code' => $locationData['district'],
|
||||
'village_code' => $locationData['village'],
|
||||
'address' => $row[6],
|
||||
'postal_code' => $locationData['postal_code'],
|
||||
'nomor_telepon_kantor' => $row[7],
|
||||
'email_kantor' => $row[8],
|
||||
'detail_email_kantor' => json_encode($row[8]),
|
||||
'nama_pimpinan' => $row[9],
|
||||
'nomor_hp_pimpinan' => $row[10],
|
||||
'nama_pic_reviewer' => json_encode($row[11]),
|
||||
'detail_nama_pic_reviewer' => json_encode($row[11]),
|
||||
'nomor_hp_pic_reviewer' => json_encode($row[12]),
|
||||
'detail_nomor_hp_pic_reviewer' => json_encode($row[12]),
|
||||
'nama_pic_admin' => $row[13],
|
||||
'detail_nama_pic_admin' => json_encode($row[13]),
|
||||
'nomor_hp_pic_admin' => json_encode($row[14]),
|
||||
'detail_nomor_hp_pic_admin' => json_encode($row[14]),
|
||||
'nama_pic_marketing' => json_encode($row[15]),
|
||||
'detail_nama_pic_marketing' => json_encode($row[15]),
|
||||
'nomor_hp_pic_marketing' => json_encode($row[16]),
|
||||
'detail_nomor_hp_pic_marketing' => json_encode($row[16]),
|
||||
'ijin_usaha_id' => json_encode($jenisUsaha),
|
||||
'jenis_aset_id' => json_encode($jenisAsset),
|
||||
'attachment' => null,
|
||||
];
|
||||
|
||||
|
||||
// Debug: Tampilkan data yang akan diinsert
|
||||
$this->command->info('Data yang akan disimpan: ' . print_r($dataToInsert, true));
|
||||
|
||||
// Simpan ke database
|
||||
KJPP::create($dataToInsert);
|
||||
|
||||
$this->command->info("Berhasil menyimpan data baris $index");
|
||||
|
||||
} catch (\Exception $e) {
|
||||
$this->command->error("Error pada baris $index: " . $e->getMessage());
|
||||
// Lanjut ke baris berikutnya
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
$this->command->info('Selesai import data KJPP!');
|
||||
|
||||
} catch (\Exception $e) {
|
||||
$this->command->error('Error utama: ' . $e->getMessage());
|
||||
$this->command->error('Stack trace: ' . $e->getTraceAsString());
|
||||
}
|
||||
}
|
||||
|
||||
private function generateKJPPCode()
|
||||
{
|
||||
do {
|
||||
$code = 'K' . str_pad(rand(1, 99999), 5, '0', STR_PAD_LEFT);
|
||||
} while (KJPP::where('code', $code)->exists());
|
||||
|
||||
return $code;
|
||||
}
|
||||
|
||||
public function checkKota($data)
|
||||
{
|
||||
try {
|
||||
$city = null;
|
||||
if ($data) {
|
||||
// Bersihkan data dan ubah ke uppercase
|
||||
$cleanData = trim(strtoupper($data));
|
||||
|
||||
$possibleNames = [
|
||||
'KAB. ' . $cleanData,
|
||||
'KOTA ' . $cleanData,
|
||||
'KAB. ADM. ' . $cleanData,
|
||||
'KOTA ADM. ' . $cleanData,
|
||||
$cleanData
|
||||
];
|
||||
|
||||
$city = City::whereIn(DB::raw('UPPER(name)'), $possibleNames)->first();
|
||||
|
||||
if ($city) {
|
||||
// Cari district berdasarkan city_code
|
||||
$district = District::where('city_code', $city->code)->first();
|
||||
if (!$district) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$village = Village::where('district_code', $district->code)->first();
|
||||
if (!$village) {
|
||||
|
||||
return null;
|
||||
}
|
||||
return [
|
||||
'province' => $city->province_code,
|
||||
'city' => $city->code,
|
||||
'district' => $district->code,
|
||||
'village' => $village->code,
|
||||
'postal_code' => $village->postal_code
|
||||
];
|
||||
} else{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
} catch (\Exception $e) {
|
||||
$this->command->error(" ❌ Error: " . $e->getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public function checkJenisAsset($data)
|
||||
{
|
||||
try {
|
||||
if (!$data) return [];
|
||||
|
||||
// Bersihkan dan ubah ke lowercase
|
||||
$cleanData = trim(strtolower($data));
|
||||
|
||||
// Pisahkan berdasarkan koma
|
||||
$assets = array_map('trim', explode(',', $cleanData));
|
||||
|
||||
// Daftar asset yang valid di database
|
||||
$validAssets = JenisJaminan::pluck('code')->toArray();
|
||||
$assetIds = [];
|
||||
|
||||
foreach ($assets as $asset) {
|
||||
// Cari di database dengan lowercase
|
||||
$jenisAsset = JenisJaminan::whereRaw('LOWER(name) = ?', [$asset])->first();
|
||||
|
||||
if ($jenisAsset && in_array($jenisAsset->code, $validAssets)) {
|
||||
$assetIds[] = $jenisAsset->code;
|
||||
$this->command->info(" ✅ Jenis Asset ditemukan: " . $jenisAsset->name);
|
||||
}
|
||||
// Hilangkan warning untuk yang tidak ditemukan
|
||||
}
|
||||
|
||||
// Menghilangkan duplikat jika ada
|
||||
$assetIds = array_unique($assetIds);
|
||||
|
||||
// Sort array untuk konsistensi
|
||||
sort($assetIds);
|
||||
|
||||
return $assetIds;
|
||||
|
||||
} catch (\Exception $e) {
|
||||
$this->command->error(" ❌ Error pada checkJenisAsset: " . $e->getMessage());
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function checkJenisUsaha($data)
|
||||
{
|
||||
try {
|
||||
if (!$data) return [];
|
||||
|
||||
// Bersihkan dan ubah ke lowercase
|
||||
$cleanData = trim(strtolower($data));
|
||||
|
||||
// Pisahkan berdasarkan koma jika ada
|
||||
$usahas = array_map('trim', explode(',', $cleanData));
|
||||
|
||||
// Daftar usaha yang valid di database
|
||||
$validUsaha = IjinUsaha::pluck('code')->toArray();
|
||||
$usahaIds = [];
|
||||
|
||||
foreach ($usahas as $usaha) {
|
||||
// Cari di database dengan lowercase
|
||||
$jenisUsaha = IjinUsaha::whereRaw('LOWER(name) = ?', [$usaha])->first();
|
||||
|
||||
if ($jenisUsaha && in_array($jenisUsaha->code, $validUsaha)) {
|
||||
$usahaIds[] = $jenisUsaha->code;
|
||||
$this->command->info(" ✅ Jenis Usaha ditemukan: " . $jenisUsaha->name);
|
||||
}
|
||||
// Hilangkan warning untuk yang tidak ditemukan
|
||||
}
|
||||
|
||||
// Menghilangkan duplikat jika ada
|
||||
$usahaIds = array_unique($usahaIds);
|
||||
|
||||
// Sort array untuk konsistensi
|
||||
sort($usahaIds);
|
||||
|
||||
return $usahaIds;
|
||||
|
||||
} catch (\Exception $e) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user