forked from nathalie/pedi2
Seeding simplificado
This commit is contained in:
parent
bb29e34b1b
commit
d91d46c589
3 changed files with 8 additions and 49 deletions
database/seeds
|
@ -4,7 +4,7 @@ use Illuminate\Database\Seeder;
|
||||||
use League\Csv\Reader;
|
use League\Csv\Reader;
|
||||||
use App\Proveedor;
|
use App\Proveedor;
|
||||||
|
|
||||||
class ImportarProductoSeeder extends Seeder
|
class CanastaSeeder extends Seeder
|
||||||
{
|
{
|
||||||
const FILA_HEADER = "Tipo";
|
const FILA_HEADER = "Tipo";
|
||||||
const ULTIMA_FILA = "TOTAL";
|
const ULTIMA_FILA = "TOTAL";
|
||||||
|
@ -21,14 +21,14 @@ class ImportarProductoSeeder extends Seeder
|
||||||
$iHeader = $this->obtenerIndiceDeHeader($csv);
|
$iHeader = $this->obtenerIndiceDeHeader($csv);
|
||||||
$csv->setHeaderOffset($iHeader);
|
$csv->setHeaderOffset($iHeader);
|
||||||
$registros = $csv->getRecords();
|
$registros = $csv->getRecords();
|
||||||
|
|
||||||
$toInsert = [];
|
$toInsert = [];
|
||||||
$categoria = '';
|
$categoria = '';
|
||||||
foreach($registros as $i => $registro){
|
foreach($registros as $i => $registro){
|
||||||
//filas que están arriba del header
|
//filas que están arriba del header
|
||||||
if ($i <= $iHeader){
|
if ($i <= $iHeader){
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
//finalizar
|
//finalizar
|
||||||
if ($registro[$this::FILA_HEADER] == $this::ULTIMA_FILA){
|
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]) == ''){
|
if (!Arr::has($registro,$this::FILA_HEADER)|| trim($registro[$this::FILA_HEADER]) == ''){
|
||||||
var_dump("no hay tipo en la fila " . $i);
|
var_dump("no hay tipo en la fila " . $i);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
//saltear bono de transporte
|
//saltear bono de transporte
|
||||||
if ($registro[$this::FILA_HEADER] == "T"){
|
if ($registro[$this::FILA_HEADER] == "T"){
|
||||||
|
@ -51,7 +51,7 @@ class ImportarProductoSeeder extends Seeder
|
||||||
//es la pregunta de la copa?
|
//es la pregunta de la copa?
|
||||||
if (Str::contains($registro[$this::FILA_HEADER],"¿")) { continue; }
|
if (Str::contains($registro[$this::FILA_HEADER],"¿")) { continue; }
|
||||||
$categoria = $registro[$this::FILA_HEADER];
|
$categoria = $registro[$this::FILA_HEADER];
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
//completar producto
|
//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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,7 +12,6 @@ class DatabaseSeeder extends Seeder
|
||||||
*/
|
*/
|
||||||
public function run()
|
public function run()
|
||||||
{
|
{
|
||||||
$this->call(GrupoDeCompraSeeder::class);
|
$this->call(CanastaSeeder::class);
|
||||||
$this->call(ImportarProductoSeeder::class);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Add table
Reference in a new issue