diff --git a/app/Services/ServiceGoogleSheet.php b/app/Services/ServiceGoogleSheet.php index fa4a0ed..e14b568 100644 --- a/app/Services/ServiceGoogleSheet.php +++ b/app/Services/ServiceGoogleSheet.php @@ -592,7 +592,38 @@ class ServiceGoogleSheet $headers[$colIdx] = $this->normalizeHeader($header); } - // Truncate table and restart identity + // Log environment and header diagnostics + Log::info('sync_pbg_task_payments: diagnostics', [ + 'spreadsheet_id' => $this->spreadsheetID, + 'sheet' => $sheetName, + 'selected_indices_count' => count($selected_indices) + ]); + + // Validate that expected headers exist after normalization before truncating table + $expectedHeaders = [ + 'no','jenis_konsultasi','no_registrasi','nama_pemilik','lokasi_bg','fungsi_bg','nama_bangunan', + 'tgl_permohonan','status_verifikasi','status_permohonan','alamat_pemilik','no_hp','email', + 'tanggal_catatan','catatan_kekurangan_dokumen','gambar','krkkkpr','no_krk','lh','ska','keterangan', + 'helpdesk','pj','operator_pbg','kepemilikan','potensi_taru','validasi_dinas','kategori_retribusi', + 'no_urut_ba_tpt_20250001','tanggal_ba_tpt','no_urut_ba_tpa','tanggal_ba_tpa','no_urut_skrd_20250001', + 'tanggal_skrd','ptsp','selesai_terbit','tanggal_pembayaran_yyyymmdd','format_sts','tahun_terbit', + 'tahun_berjalan','kelurahan','kecamatan','lb','tb','jlb','unit','usulan_retribusi', + 'nilai_retribusi_keseluruhan_simbg','nilai_retribusi_keseluruhan_pad','denda','usaha__non_usaha' + ]; + + $normalizedHeaderValues = array_values($headers); + $overlap = array_intersect($expectedHeaders, $normalizedHeaderValues); + + if (count($overlap) < 10) { // too few matching headers, likely wrong sheet or headers changed + Log::error('sync_pbg_task_payments: header mismatch detected', [ + 'expected_sample' => array_slice($expectedHeaders, 0, 15), + 'found_sample' => array_slice($normalizedHeaderValues, 0, 30), + 'match_count' => count($overlap) + ]); + return ['success' => false, 'message' => 'Header mismatch - aborting to prevent null inserts']; + } + + // Truncate table and restart identity (only after header validation) Schema::disableForeignKeyConstraints(); DB::table('pbg_task_payments')->truncate(); Schema::enableForeignKeyConstraints(); @@ -694,7 +725,7 @@ class ServiceGoogleSheet // Build and insert this chunk $batch = []; - foreach ($values as $row) { + foreach ($values as $rowIndex => $row) { $record = [ 'created_at' => now(), 'updated_at' => now(), @@ -709,6 +740,17 @@ class ServiceGoogleSheet if ($rowByHeader[$h] === '') $rowByHeader[$h] = null; } + // Log first non-empty row mapping for diagnostics + if ($rowIndex === 0) { + $nonEmptySample = []; + foreach ($rowByHeader as $k => $v) { + if ($v !== null && count($nonEmptySample) < 10) { $nonEmptySample[$k] = $v; } + } + Log::info('sync_pbg_task_payments: first row sample after normalization', [ + 'sample' => $nonEmptySample + ]); + } + // Skip if this row looks like a header row $headerCheckKeys = ['no','jenis_konsultasi','no_registrasi']; $headerMatches = 0;