update migration file, dan laporan

This commit is contained in:
majid
2025-06-11 09:55:37 +07:00
parent f69128e18e
commit 7ddcc14167
25 changed files with 2513 additions and 821 deletions

View File

@@ -9,21 +9,27 @@ use Modules\Lpj\Models\Inspeksi;
use Modules\Basicdata\Models\Branch;
use Modules\Lpj\Models\DokumenJaminan;
use Modules\Lpj\Models\Permohonan;
use DateTime;
class MigrationGambarInspeksiSeeder extends Seeder
{
/**
* Run the database seeds.
*/
protected $errorLogFile = __DIR__ . '/csv/inspeksi/mig_inspeksi_foto_error.csv';
protected $errorLogFile = __DIR__ . '/csv/inspeksi/mig_inspeksi_foto_20253_error.csv';
/**
* Migrasi data gambar inspeksi dari file csv ke tabel inspeksi_gambar
*
* @return void
*/
public function run()
{
$this->initializeErrorLog();
// Path ke file csv
$filePath = realpath(__DIR__ . '/csv/inspeksi/mig_inspeksi_foto.csv');
$filePath = realpath(__DIR__ . '/csv/inspeksi/mig_inspeksi_foto_20253.csv');
if (!$filePath) {
Log::error('File csv tidak ditemukan: ' . __DIR__ . '/csv/inspeksi/mig_inspeksi_foto.csv');
Log::error('File csv tidak ditemukan: ' . __DIR__ . '/csv/inspeksi/mig_inspeksi_foto_20253.csv');
$this->command->error('File csv tidak ditemukan.');
return;
}
@@ -82,43 +88,44 @@ class MigrationGambarInspeksiSeeder extends Seeder
}
private function processBatch(array $rows, array &$branchCache, array &$userData, int &$errorCount, array &$errorDebitureIds, int $totalData, int $batchCount, int $currentRow)
{
// Kelompokkan berdasarkan mig_nomor_jaminan
$groupedRows = $this->groupRowsByJaminan($rows);
{
// Kelompokkan berdasarkan mig_nomor_jaminan
$groupedRows = $this->groupRowsByJaminan($rows);
foreach ($groupedRows as $nomorJaminan => $groupRows) {
try {
// Ambil informasi permohonan dan dokument_id
$nomorRegis = $this->getNomorRegistrasiPermohonan($nomorJaminan, $branchCache);
if (!$nomorRegis) {
Log::warning("Nomor registrasi tidak ditemukan untuk nomor jaminan: {$nomorJaminan}");
foreach ($groupedRows as $nomorJaminan => $groupRows) {
try {
// Ambil informasi permohonan dan dokument_id
$nomorRegis = $this->getNomorRegistrasiPermohonan($nomorJaminan, $branchCache);
if (!$nomorRegis) {
Log::warning("Nomor registrasi tidak ditemukan untuk nomor jaminan: {$nomorJaminan}");
$errorCount++;
$errorDebitureIds[] = $nomorJaminan;
continue;
}
// Bangun JSON foto_form
$fotoJson = $this->checkFoto($groupRows);
Inspeksi::where('permohonan_id', $nomorRegis['id'])
->where('dokument_id', $nomorRegis['dokument_id'])
->whereNotNull('data_form')
->update([
'foto_form' => $fotoJson,
'updated_at' => now()
]);
$this->command->info("Berhasil update foto_form untuk nomor jaminan: {$nomorJaminan}");
} catch (\Exception $e) {
Log::error("Error pada nomor jaminan {$nomorJaminan}: " . $e->getMessage());
$errorCount++;
$errorDebitureIds[] = $nomorJaminan;
continue;
}
// Bangun JSON foto_form
$fotoJson = $this->checkFoto($groupRows);
Inspeksi::where('permohonan_id', $nomorRegis['id'])
->where('dokument_id', $nomorRegis['dokument_id'])
->whereNotNull('data_form')
->update([
'foto_form' => $fotoJson,
'updated_at' => now()
]);
$this->command->info("Berhasil update foto_form untuk nomor jaminan: {$nomorJaminan}");
} catch (\Exception $e) {
Log::error("Error pada nomor jaminan {$nomorJaminan}: " . $e->getMessage());
$errorCount++;
$errorDebitureIds[] = $nomorJaminan;
continue;
}
}
}
private function groupRowsByJaminan(array $rows): array {
private function groupRowsByJaminan(array $rows): array
{
$grouped = [];
foreach ($rows as $row) {
@@ -133,46 +140,101 @@ class MigrationGambarInspeksiSeeder extends Seeder
}
private function checkFoto(array $rows)
{
$fotos = [];
// Inisialisasi kelompok untuk tiap kategori
$kategoriPrioritas = [
'PETA LOKASI' => [],
'GAMBAR SITUASI / SURAT UKUR' => [],
'FOTO JAMINAN' => [],
'lainnya' => []
];
foreach ($rows as $row) {
// Pastikan kolom ada
$namaFoto = trim($row['mig_nama_gambar'] ?? '');
$pathFoto = trim($row['mig_url_gambar'] ?? '');
$kategori = trim($row['mig_kategori'] ?? 'lainnya');
$urutan = (int)($row['mig_urutan_gambar'] ?? 999); // Default urutan besar jika tidak ada
// Ambil kolom penting
$namaFoto = trim($row['mig_nama_gambar'] ?? '');
$pathFoto = trim($row['mig_url_gambar'] ?? '');
$kategori = trim($row['mig_kategori'] ?? 'lainnya');
$urutan = (int)($row['mig_urutan_gambar'] ?? 999);
$tgl = trim($row['mig_tgl'] ?? '');
$nomorLpj = trim($row['mig_nomor_laporan'] ?? '');
if (empty($pathFoto)) {
continue; // skip jika path kosong
if (empty($pathFoto) || empty($tgl)) {
continue; // Lewati jika tidak lengkap
}
$fotos[] = [
try {
$tanggal = \Carbon\Carbon::createFromFormat('Y-m-d', $tgl);
if (!$tanggal) {
throw new \Exception("Tanggal tidak valid");
}
} catch (\Exception $e) {
continue; // Lewati jika tanggal tidak valid
}
$tahun = $tanggal->format('Y');
$bulanAngka = $tanggal->format('n');
$bulanNama = [
1 => 'JANUARI', 2 => 'FEBRUARI', 3 => 'MARET',
4 => 'APRIL', 5 => 'MEI', 6 => 'JUNI',
7 => 'JULI', 8 => 'AGUSTUS', 9 => 'SEPTEMBER',
10 => 'OKTOBER', 11 => 'NOVEMBER', 12 => 'DESEMBER'
][(int)$bulanAngka] ?? 'UNKNOWN';
$tanggalFormat = $tanggal->format('dmY');
if (empty($namaFoto)) {
$namaFoto = basename($pathFoto) ?: 'image.jpg';
}
// Gunakan '/' sebagai separator path
$finalPath = "surveyor/{$tahun}/{$bulanNama}/{$tanggalFormat}/{$nomorLpj}/{$pathFoto}";
$fotoItem = [
'urutan' => $urutan,
'name' => $namaFoto,
'path' => $pathFoto,
'path' => $finalPath,
'category' => $kategori,
'sub' => null,
'description' => '',
'created_by' => null,
'created_at' => null
];
// Masukkan ke dalam kelompok kategori
if (isset($kategoriPrioritas[$kategori])) {
$kategoriPrioritas[$kategori][] = $fotoItem;
} else {
$kategoriPrioritas['lainnya'][] = $fotoItem;
}
}
// Urutkan array berdasarkan mig_urutan_gambar
usort($fotos, function ($a, $b) {
return $a['urutan'] <=> $b['urutan'];
});
// Urutkan masing-masing kategori berdasarkan urutan
foreach ($kategoriPrioritas as &$kelompok) {
usort($kelompok, function ($a, $b) {
return $a['urutan'] <=> $b['urutan'];
});
}
// Hapus indeks 'urutan' setelah diurutkan
// Gabungkan dengan urutan prioritas: PETA LOKASI -> GAMBAR SITUASI -> FOTO JAMINAN -> lainnya
$finalFotos = array_merge(
$kategoriPrioritas['PETA LOKASI'],
$kategoriPrioritas['GAMBAR SITUASI / SURAT UKUR'],
$kategoriPrioritas['FOTO JAMINAN'],
$kategoriPrioritas['lainnya']
);
// Hapus indeks 'urutan'
$finalFotos = array_map(function ($foto) {
unset($foto['urutan']);
return $foto;
}, $fotos);
}, $finalFotos);
return json_encode([
'foto_jaminan' => $finalFotos
'upload_foto' => $finalFotos
], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
}
private function getNomorRegistrasiPermohonan($nomor_jaminan_id, array &$cache)