Seeding simplificado

This commit is contained in:
Alejandro Tasistro 2024-09-04 15:41:35 -03:00
parent bb29e34b1b
commit d91d46c589
3 changed files with 8 additions and 49 deletions

View File

@ -4,7 +4,7 @@ use Illuminate\Database\Seeder;
use League\Csv\Reader;
use App\Proveedor;
class ImportarProductoSeeder extends Seeder
class CanastaSeeder extends Seeder
{
const FILA_HEADER = "Tipo";
const ULTIMA_FILA = "TOTAL";
@ -21,14 +21,14 @@ class ImportarProductoSeeder extends Seeder
$iHeader = $this->obtenerIndiceDeHeader($csv);
$csv->setHeaderOffset($iHeader);
$registros = $csv->getRecords();
$toInsert = [];
$categoria = '';
foreach($registros as $i => $registro){
//filas que están arriba del header
if ($i <= $iHeader){
continue;
}
}
//finalizar
if ($registro[$this::FILA_HEADER] == $this::ULTIMA_FILA){
@ -39,7 +39,7 @@ class ImportarProductoSeeder extends Seeder
if (!Arr::has($registro,$this::FILA_HEADER)|| trim($registro[$this::FILA_HEADER]) == ''){
var_dump("no hay tipo en la fila " . $i);
continue;
}
}
//saltear bono de transporte
if ($registro[$this::FILA_HEADER] == "T"){
@ -51,7 +51,7 @@ class ImportarProductoSeeder extends Seeder
//es la pregunta de la copa?
if (Str::contains($registro[$this::FILA_HEADER],"¿")) { continue; }
$categoria = $registro[$this::FILA_HEADER];
continue;
continue;
}
//completar producto
@ -65,9 +65,9 @@ class ImportarProductoSeeder extends Seeder
];
}
foreach (array_chunk($toInsert,DatabaseSeeder::CHUNK_SIZE) as $chunk)
foreach (array_chunk($toInsert,DatabaseSeeder::CHUNK_SIZE) as $chunk)
{
DB::table('productos')->insert($chunk);
DB::table('productos')->insert($chunk);
}
}

View File

@ -12,7 +12,6 @@ class DatabaseSeeder extends Seeder
*/
public function run()
{
$this->call(GrupoDeCompraSeeder::class);
$this->call(ImportarProductoSeeder::class);
$this->call(CanastaSeeder::class);
}
}

View File

@ -1,40 +0,0 @@
<?php
use Illuminate\Database\Seeder;
use League\Csv\Reader;
use App\Proveedor;
class ProductoSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$csv = Reader::createFromPath(resource_path('csv/productos.csv'), 'r');
$csv->setDelimiter("|");
$csv->setEnclosure("'");
$csv->setHeaderOffset(0);
$registros = $csv->getRecords();
$toInsert = [];
foreach($registros as $registro){
$toInsert[] = [
'categoria' => $registro['categoria'],
'nombre' => $registro['producto'],
'precio' => $registro['precio'],
'proveedor_id' => isset($registro['proveedor']) ? Proveedor::firstOrCreate([
'nombre' => $registro['proveedor']
])->id : null,
'bono' => $registro['categoria'] == 'BONOS Y FINANCIAMIENTO SORORO'
];
}
foreach (array_chunk($toInsert,DatabaseSeeder::CHUNK_SIZE) as $chunk)
{
DB::table('productos')->insert($chunk);
}
}
}