75 lines
		
	
	
		
			2.0 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			75 lines
		
	
	
		
			2.0 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php
 | 
						|
 | 
						|
namespace Modules\Admin\App\Imports;
 | 
						|
 | 
						|
use Illuminate\Support\Collection;
 | 
						|
use Illuminate\Support\Facades\DB;
 | 
						|
use Maatwebsite\Excel\Concerns\ToCollection;
 | 
						|
 | 
						|
class SATPaisImport implements ToCollection
 | 
						|
{
 | 
						|
    /**
 | 
						|
     * @param Collection $collection
 | 
						|
     */
 | 
						|
    public function collection(Collection $collection)
 | 
						|
    {
 | 
						|
        $batchSize = 1000;
 | 
						|
        $batchData = [];
 | 
						|
        $processedRows = 0;
 | 
						|
 | 
						|
        foreach ($collection as $key => $row) {
 | 
						|
            if ($key < 5 || !$row[1]) {
 | 
						|
                continue;
 | 
						|
            }
 | 
						|
 | 
						|
            $requestArray = [
 | 
						|
                'c_pais' => $row[0],
 | 
						|
                'descripcion' => $row[1],
 | 
						|
                'formato_de_codigo_postal' => $row[2],
 | 
						|
                'formato_de_registro_de_identidad_tributaria' => $row[3],
 | 
						|
                'validacion_del_registro_de_identidad_tributaria' => $row[4],
 | 
						|
                'agrupaciones' => $row[5],
 | 
						|
            ];
 | 
						|
 | 
						|
            $batchData[] = $requestArray;
 | 
						|
            $processedRows++;
 | 
						|
 | 
						|
            if (count($batchData) >= $batchSize) {
 | 
						|
                $this->insertBatch($batchData);
 | 
						|
                $batchData = [];
 | 
						|
            }
 | 
						|
        }
 | 
						|
 | 
						|
        if (!empty($batchData)) {
 | 
						|
            $this->insertBatch($batchData);
 | 
						|
        }
 | 
						|
 | 
						|
        echo "\n\033[32mImport completed: Processed $processedRows rows.\033[0m\n";
 | 
						|
    }
 | 
						|
 | 
						|
    private function insertBatch(array $batchData)
 | 
						|
    {
 | 
						|
        try {
 | 
						|
            DB::table('sat_pais')->upsert(
 | 
						|
                $batchData,
 | 
						|
                [
 | 
						|
                    'c_pais',
 | 
						|
                ],
 | 
						|
                [
 | 
						|
                    'descripcion',
 | 
						|
                    'formato_de_codigo_postal',
 | 
						|
                    'formato_de_registro_de_identidad_tributaria',
 | 
						|
                    'validacion_del_registro_de_identidad_tributaria',
 | 
						|
                    'agrupaciones',
 | 
						|
                ]
 | 
						|
            );
 | 
						|
        } catch (\Exception $e) {
 | 
						|
            echo "Error in batch: " . $e->getMessage() . "\n";
 | 
						|
 | 
						|
            foreach ($batchData as $row) {
 | 
						|
                echo "Row data: " . json_encode($row) . "\n";
 | 
						|
            }
 | 
						|
        }
 | 
						|
    }
 | 
						|
}
 |