102 lines
4.1 KiB
PHP
102 lines
4.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\ActivityLog;
|
|
use App\Models\RumahIbadah;
|
|
use App\Models\RumahMiskin;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class ImportController extends Controller
|
|
{
|
|
public function ibadah(Request $request)
|
|
{
|
|
$request->validate(['file' => 'required|file|mimes:csv,txt|max:5120']);
|
|
|
|
$rows = $this->parseCsv($request->file('file')->getRealPath());
|
|
$imported = 0; $failed = 0;
|
|
|
|
DB::beginTransaction();
|
|
try {
|
|
foreach ($rows as $r) {
|
|
// [0]=ID(diabaikan) [1]=Nama [2]=Jenis [3]=Alamat [4]=Radius [5]=Jemaah [6]=Pengurus [7]=Telepon [8]=Lat [9]=Lon
|
|
if (count($r) < 10 || trim($r[1]) === '' || !is_numeric($r[8]) || !is_numeric($r[9])) { $failed++; continue; }
|
|
RumahIbadah::create([
|
|
'user_id' => auth()->id(),
|
|
'nama' => $r[1],
|
|
'jenis' => $r[2] ?: null,
|
|
'alamat' => $r[3] ?: null,
|
|
'radius' => is_numeric($r[4]) ? (int) $r[4] : 500,
|
|
'jumlah_jemaah' => is_numeric($r[5]) ? (int) $r[5] : null,
|
|
'pengurus' => $r[6] ?: null,
|
|
'telepon' => $r[7] ?: null,
|
|
'latitude' => (float) $r[8],
|
|
'longitude' => (float) $r[9],
|
|
]);
|
|
$imported++;
|
|
}
|
|
DB::commit();
|
|
} catch (\Throwable $e) {
|
|
DB::rollBack();
|
|
return response()->json(['message' => 'Import gagal: ' . $e->getMessage()], 422);
|
|
}
|
|
|
|
ActivityLog::record('import', "Import CSV rumah ibadah: {$imported} berhasil, {$failed} gagal", 'ibadah');
|
|
return response()->json(['success' => true, 'imported' => $imported, 'failed' => $failed]);
|
|
}
|
|
|
|
public function miskin(Request $request)
|
|
{
|
|
$request->validate(['file' => 'required|file|mimes:csv,txt|max:5120']);
|
|
|
|
$rows = $this->parseCsv($request->file('file')->getRealPath());
|
|
$imported = 0; $failed = 0;
|
|
|
|
DB::beginTransaction();
|
|
try {
|
|
foreach ($rows as $r) {
|
|
// [0]=ID Rumah [1]=Nama KK [2]=Alamat [3]=KK [4]=Jiwa [5]=Pekerjaan [6]=Pendapatan [7]=Kondisi [8]=Lat [9]=Lon
|
|
if (count($r) < 10 || trim($r[0]) === '' || !is_numeric($r[8]) || !is_numeric($r[9])) { $failed++; continue; }
|
|
RumahMiskin::updateOrCreate(
|
|
['id_rumah' => $r[0]],
|
|
[
|
|
'nama_kk' => $r[1] ?: null,
|
|
'alamat' => $r[2] ?: null,
|
|
'jumlah_kk' => is_numeric($r[3]) ? (int) $r[3] : 1,
|
|
'jumlah_orang' => is_numeric($r[4]) ? (int) $r[4] : 1,
|
|
'pekerjaan' => $r[5] ?: null,
|
|
'pendapatan' => is_numeric($r[6]) ? (int) $r[6] : null,
|
|
'kondisi_rumah' => $r[7] ?: null,
|
|
'latitude' => (float) $r[8],
|
|
'longitude' => (float) $r[9],
|
|
]
|
|
);
|
|
$imported++;
|
|
}
|
|
DB::commit();
|
|
} catch (\Throwable $e) {
|
|
DB::rollBack();
|
|
return response()->json(['message' => 'Import gagal: ' . $e->getMessage()], 422);
|
|
}
|
|
|
|
ActivityLog::record('import', "Import CSV keluarga miskin: {$imported} berhasil, {$failed} gagal", 'miskin');
|
|
return response()->json(['success' => true, 'imported' => $imported, 'failed' => $failed]);
|
|
}
|
|
|
|
/** Baca CSV, lewati baris header, kembalikan array baris data. */
|
|
private function parseCsv(string $path): array
|
|
{
|
|
$rows = [];
|
|
$handle = fopen($path, 'r');
|
|
$first = true;
|
|
while (($data = fgetcsv($handle)) !== false) {
|
|
if ($first) { $first = false; continue; } // skip header
|
|
if (count($data) === 1 && trim((string) $data[0]) === '') continue; // skip blank
|
|
$rows[] = $data;
|
|
}
|
|
fclose($handle);
|
|
return $rows;
|
|
}
|
|
}
|