merge conflix spatial-plannings

This commit is contained in:
arifal
2025-02-21 08:42:08 +07:00
32 changed files with 1342 additions and 127 deletions

View File

@@ -3,75 +3,61 @@
namespace App\Imports;
use App\Models\SpatialPlanning;
use Carbon\Carbon;
use DateTime;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
use Illuminate\Support\Facades\DB;
use DateTime;
use Carbon\Carbon;
class SpatialPlanningImport implements ToCollection, WithMultipleSheets
class SpatialPlanningImport implements ToCollection
{
protected static $processed = false;
/**
* @param Collection $collection
*/
public function collection(Collection $collection)
* Process each row in the file
*/
public function collection(Collection $rows)
{
$months = [
"Januari" => "January", "Februari" => "February", "Maret" => "March",
"April" => "April", "Mei" => "May", "Juni" => "June",
"Juli" => "July", "Agustus" => "August", "September" => "September",
"Oktober" => "October", "November" => "November", "Desember" => "December"
];
if (self::$processed) {
return;
}
self::$processed = true;
$collection->skip(2)->each(function ($row) use ($months) {
if (empty(array_filter($row->toArray()))) {
return;
}
if ($rows->isEmpty())
{
return;
}
if (!isset($row[6]) || empty($row[6])) {
return;
}
//cari header secara otomatis
$header = $rows->first();
$headerIndex = collect($header)->search(fn($value) => !empty($value));
if(!SpatialPlanning::where("nomor", $row[6])->exists()){
$clean_nomor = str_replace('\\','',$row[6]);
$date_string = isset($row[7]) ? trim($row[7]) : null;
$clean_sp_date = null;
if ($date_string) {
if(is_numeric($date_string)) {
$clean_sp_date = Carbon::createFromFormat('Y-m-d', '1900-01-01')->addDays($date_string - 2)->format('Y-m-d');
}else{
foreach ($months as $id => $en) {
$date_string = str_replace($id, $en, $date_string);
}
$formats = ['j F Y', 'd F Y', 'j-M-Y', 'd-M-Y'];
// Pastikan header ditemukan
if ($headerIndex === false) {
return;
}
foreach ($formats as $format) {
$date = DateTime::createFromFormat($format, $date_string);
if ($date) {
$clean_sp_date = $date->format('Y-m-d');
break;
}
}
}
}
SpatialPlanning::create([
'name' => $row[1],
'kbli' => $row[2],
'kegiatan' => $row[3],
'luas' => $row[4],
'lokasi' => $row[5],
'nomor' => $clean_nomor,
'sp_date' => $clean_sp_date,
]);
}
});
}
foreach ($rows->skip(1) as $row) {
$dateValue = trim($row[7]);
info($dateValue);
$parsedDate = Carbon::createFromFormat('Y-m-d', $dateValue)->format('Y-m-d');
info($parsedDate);
public function sheets(): array {
return [
0 => $this
];
$dataToInsert[] = [
'name'=>$row[1],
'kbli'=>$row[2],
'activities'=>$row[3],
'area'=>$row[4],
'location'=>$row[5],
'number'=>$row[6],
'date'=>$parsedDate,
];
}
if(!empty($dataToInsert)) {
SpatialPlanning::insert($dataToInsert);
} else {
return;
}
}
}