fix upload business and industry with chunk
This commit is contained in:
@@ -21,36 +21,33 @@ class CustomersImport implements ToCollection, WithMultipleSheets, WithChunkRead
|
||||
public function collection(Collection $collection)
|
||||
{
|
||||
$batchData = [];
|
||||
$batchSize = 1000; // Process in smaller chunks
|
||||
$batchSize = 1000;
|
||||
|
||||
foreach ($collection as $row) {
|
||||
if (!isset($row['nomor_pelanggan']) || empty($row['nomor_pelanggan'])) {
|
||||
continue; // Skip rows without 'nomor_pelanggan'
|
||||
continue;
|
||||
}
|
||||
|
||||
// Default values
|
||||
$latitude = '0';
|
||||
$longitude = '0';
|
||||
|
||||
// Convert and normalize latitude
|
||||
if (isset($row['latkor']) && !empty(trim($row['latkor']))) {
|
||||
$latitude = str_replace(',', '.', trim($row['latkor'])); // Replace comma with dot
|
||||
$latitude = str_replace(',', '.', trim($row['latkor']));
|
||||
if (is_numeric($latitude)) {
|
||||
$latitude = bcadd($latitude, '0', 18); // Convert to decimal with 18 precision
|
||||
$latitude = bcadd($latitude, '0', 18);
|
||||
} else {
|
||||
$latitude = '0'; // Default fallback
|
||||
$latitude = '0';
|
||||
}
|
||||
} else {
|
||||
$latitude = '0';
|
||||
}
|
||||
|
||||
// Convert and normalize longitude
|
||||
if (isset($row['lonkor']) && !empty(trim($row['lonkor']))) {
|
||||
$longitude = str_replace(',', '.', trim($row['lonkor'])); // Replace comma with dot
|
||||
$longitude = str_replace(',', '.', trim($row['lonkor']));
|
||||
if (is_numeric($longitude)) {
|
||||
$longitude = bcadd($longitude, '0', 18); // Convert to decimal with 18 precision
|
||||
$longitude = bcadd($longitude, '0', 18);
|
||||
} else {
|
||||
$longitude = '0'; // Default fallback
|
||||
$longitude = '0';
|
||||
}
|
||||
} else {
|
||||
$longitude = '0';
|
||||
@@ -65,14 +62,12 @@ class CustomersImport implements ToCollection, WithMultipleSheets, WithChunkRead
|
||||
'longitude' => $longitude,
|
||||
];
|
||||
|
||||
// Batch insert every 1000 rows
|
||||
if (count($batchData) >= $batchSize) {
|
||||
Customer::upsert($batchData, ['nomor_pelanggan'], ['kota_pelayanan', 'nama', 'alamat', 'latitude', 'longitude']);
|
||||
$batchData = []; // Clear the batch
|
||||
$batchData = [];
|
||||
}
|
||||
}
|
||||
|
||||
// Insert remaining data
|
||||
if (!empty($batchData)) {
|
||||
Customer::upsert($batchData, ['nomor_pelanggan'], ['kota_pelayanan', 'nama', 'alamat', 'latitude', 'longitude']);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user