Files
aso/database/seeders/ApotekMandiriInhealtSeeder.php
Server D3 Linksehat 697c41eb70 update
2024-12-05 14:41:08 +07:00

174 lines
7.6 KiB
PHP

<?php
namespace Database\Seeders;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use App\Models\Organization;
use Box\Spout\Reader\Common\Creator\ReaderEntityFactory;
class ApotekMandiriInhealtSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$file_path = resource_path('files/data-apotik-layanan-pharmacy-delivery-update.xlsx');
$reader = ReaderEntityFactory::createReaderFromFile($file_path);
$reader->open($file_path);
$cell_map = [ // Urutan kolom di excel
'No',
'Nama KOP',
'Nama KLY',
'Kode Provider',
'Nama Provider',
'Jalan',
'KODEPOS',
'OPERASIONAL DAY',
'OPERASIONAL HOUR',
'AVAILABLE Brozo',
'Geo Tag'
];
foreach ($reader->getSheetIterator() as $sheet) {
$previousCells = null;
foreach ($sheet->getRowIterator() as $index => $row) {
if ($index >= 4) {
$cells = $row->getCells();
$data = [];
// Check Merging
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() == '') {
$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()
];
// Update or create organization
$organization = Organization::updateOrCreate(
['code' => $previousCells[3]->getValue()],
$data
);
// Update or create address
$geo = str_replace("'", "", $previousCells[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' => $previousCells[5]->getValue(),
'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;
}
}
}
$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.";
}
}
}