Update Data Organization Inhealth

This commit is contained in:
Muhammad Fajar
2024-09-13 09:41:05 +07:00
parent 99478f2b2c
commit 578a8847a2
4 changed files with 127 additions and 37 deletions

View File

@@ -21,6 +21,10 @@ class Organization extends Model
'corporate_id_partner', 'corporate_id_partner',
'phone', 'phone',
'email', 'email',
'operational_day',
'operational_hour',
'operational_day_other',
'operational_hour_other',
]; ];
// public $with = [ // public $with = [

View File

@@ -16,7 +16,7 @@ class ApotekMandiriInhealtSeeder extends Seeder
*/ */
public function run() public function run()
{ {
$file_path = resource_path('files/Data_Provider_Pharmacy_Delivery_Mandiri_Inhealth_-_10_Juni_2024.xlsx'); $file_path = resource_path('files/data-apotik-layanan-pharmacy-delivery.xlsx');
$reader = ReaderEntityFactory::createReaderFromFile($file_path); $reader = ReaderEntityFactory::createReaderFromFile($file_path);
$reader->open($file_path); $reader->open($file_path);
@@ -35,48 +35,102 @@ class ApotekMandiriInhealtSeeder extends Seeder
]; ];
foreach ($reader->getSheetIterator() as $sheet) { foreach ($reader->getSheetIterator() as $sheet) {
$previousCells = null;
foreach ($sheet->getRowIterator() as $index => $row) { foreach ($sheet->getRowIterator() as $index => $row) {
if ($index >= 4) { if ($index >= 4) {
$cells = $row->getCells(); $cells = $row->getCells();
$data = [ $data = [];
'code' => $cells[3]->getValue(),
'name' => $cells[4]->getValue(),
'type' => 'hospital',
'corporate_id_partner' => 5, // Mandiri Inhealth
'phone' => $cells[11]->getValue(),
'email' => isset($cells[12]) ? $cells[12]->getValue() : null,
];
// Update or create organization
$organization = Organization::updateOrCreate(
['code' => $cells[3]->getValue()],
$data
);
// Update or create address // Check Merging
$geo = str_replace("'", "", $cells[9]->getValue()); // Hilangkan tanda petik tunggal if ($cells[0]->getValue() == '' && $cells[1]->getValue() == '' && $cells[2]->getValue() == '' && $cells[3]->getValue() == '' && $cells[4]->getValue() == '' && $cells[5]->getValue() == '' && $cells[6]->getValue() == '' && $cells[9]->getValue() == '' && $cells[10]->getValue() == '' && $cells[11]->getValue() == '' && $cells[12]->getValue() == '') {
$datageo = explode(",", $geo); $data = [
'code' => $previousCells[3]->getValue(),
'name' => $previousCells[4]->getValue(),
'type' => 'hospital',
'corporate_id_partner' => 5, // Mandiri Inhealth
'phone' => $previousCells[11]->getValue(),
'email' => isset($previousCells[12]) ? $previousCells[12]->getValue() : null,
'operational_day' => $this->expandDaysRange($previousCells[7]->getValue(), [$cells[7]->getValue()]),
'operational_hour' => $previousCells[8]->getValue() == "24 jam" ? "00:00-24:00" : $previousCells[8]->getValue(),
'operational_day_other' => $cells[7]->getValue(),
'operational_hour_other' => $cells[8]->getValue()
];
// Pastikan $datageo memiliki dua elemen sebelum mencoba mengaksesnya // Update or create organization
$lat = isset($datageo[0]) ? $datageo[0] : null; $organization = Organization::updateOrCreate(
$lng = isset($datageo[1]) ? $datageo[1] : null; ['code' => $previousCells[3]->getValue()],
$data
);
$address = $organization->addresses()->updateOrCreate( // Update or create address
['addressable_id' => $organization->id], $geo = str_replace("'", "", $previousCells[9]->getValue()); // Hilangkan tanda petik tunggal
[ $datageo = explode(",", $geo);
'use' => 'both',
'type' => 'physical', // Pastikan $datageo memiliki dua elemen sebelum mencoba mengaksesnya
'text' => $cells[5]->getValue(), $lat = isset($datageo[0]) ? $datageo[0] : null;
'postal_code' => $cells[6]->getValue(), $lng = isset($datageo[1]) ? $datageo[1] : null;
'lat' => $lat,
'lng' => $lng, $address = $organization->addresses()->updateOrCreate(
] ['addressable_id' => $organization->id],
); [
'use' => 'both',
// Set main address id 'type' => 'physical',
$organization->main_address_id = $address->id; 'text' => $previousCells[5]->getValue(),
$organization->save(); 'postal_code' => $previousCells[6]->getValue(),
'lat' => $lat,
'lng' => $lng,
]
);
// Set main address id
$organization->main_address_id = $address->id;
$organization->save();
} else {
$data = [
'code' => $cells[3]->getValue(),
'name' => $cells[4]->getValue(),
'type' => 'hospital',
'corporate_id_partner' => 5, // Mandiri Inhealth
'phone' => $cells[11]->getValue(),
'email' => isset($cells[12]) ? $cells[12]->getValue() : null,
'operational_day' => $this->expandDaysRange($cells[7]->getValue()),
'operational_hour' => $cells[8]->getValue() == "24 jam" ? "00:00-24:00" : $cells[8]->getValue(),
];
// Update or create organization
$organization = Organization::updateOrCreate(
['code' => $cells[3]->getValue()],
$data
);
// Update or create address
$geo = str_replace("'", "", $cells[9]->getValue()); // Hilangkan tanda petik tunggal
$datageo = explode(",", $geo);
// Pastikan $datageo memiliki dua elemen sebelum mencoba mengaksesnya
$lat = isset($datageo[0]) ? $datageo[0] : null;
$lng = isset($datageo[1]) ? $datageo[1] : null;
$address = $organization->addresses()->updateOrCreate(
['addressable_id' => $organization->id],
[
'use' => 'both',
'type' => 'physical',
'text' => $cells[5]->getValue(),
'postal_code' => $cells[6]->getValue(),
'lat' => $lat,
'lng' => $lng,
]
);
// Set main address id
$organization->main_address_id = $address->id;
$organization->save();
}
$previousCells = $cells;
} }
} }
} }
@@ -84,4 +138,36 @@ class ApotekMandiriInhealtSeeder extends Seeder
$reader->close(); $reader->close();
} }
private function expandDaysRange($input, $excludedDays = []) {
// Daftar hari dalam seminggu
$daysOfWeek = ["Senin", "Selasa", "Rabu", "Kamis", "Jumat", "Sabtu", "Minggu"];
// Pisahkan input berdasarkan tanda "-"
$range = array_map('trim', explode('-', $input));
if (count($range) == 2) {
// Cari posisi hari pertama dan hari terakhir di dalam array $daysOfWeek
$startIndex = array_search($range[0], $daysOfWeek);
$endIndex = array_search($range[1], $daysOfWeek);
// Jika ditemukan, ambil rentang hari
if ($startIndex !== false && $endIndex !== false && $startIndex <= $endIndex) {
// Ambil hari-hari dalam rentang tersebut
$daysRange = array_slice($daysOfWeek, $startIndex, ($endIndex - $startIndex + 1));
// Jika ada hari yang ingin di-exclude, hapus dari $daysRange
if (!empty($excludedDays)) {
$daysRange = array_diff($daysRange, $excludedDays);
}
// Gabungkan menjadi string dengan koma
return implode(', ', $daysRange);
} else {
return "Hari tidak valid atau rentang tidak benar.";
}
} else {
return "Format input tidak sesuai.";
}
}
} }