logica para parsear productos y categorías de csv

This commit is contained in:
Alejandro Tasistro 2024-03-12 22:28:22 -03:00
parent 3e44e8ee1c
commit 3a36da6077
2 changed files with 114 additions and 0 deletions

View File

@ -0,0 +1,111 @@
<?php
namespace Database\Seeders;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use League\Csv\Reader;
use App\Models\Categoria;
use App\Models\Caracteristica;
use App\Models\Producto;
class CanastaSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
$tipoColumn = 'Tipo';
$productoColumn = 'Producto';
$precioColumn = 'Precio';
$tipos = ['P','PTC','B'];
$csv = Reader::createFromPath(resource_path('csv/productos.csv'), 'r');
$csv->setDelimiter('|');
$csv->setHeaderOffset(0);
$records = $csv->getRecords();
$productosToInsert = [];
$caracteristicasToInsert = [];
$currentCategoria;
foreach ($records as $i => $record) {
$tipo = trim($record[$tipoColumn]);
if (!in_array($tipo, $tipos)) {
if (!Str::contains($tipo,'¿') && ($tipo != 'T')) {
$currentCategoria = Categoria::firstOrCreate(['name' => $tipo]);
}
} else {
$parsed = $this->parseAndFormatName($record[$productoColumn]);
$productosToInsert[] = [
'name' => $parsed['name'],
'price' => $record[$precioColumn],
'solidario' => $parsed['solidario'],
'bono' => $tipo == 'B',
'categoria_id' => $currentCategoria->id
];
$caracteristicasToInsert[] = [
'name' => $parsed['name'],
'caracteristicas' => $parsed['caracteristicas']
];
}
}
foreach (array_chunk($productosToInsert,DatabaseSeeder::CHUNK_SIZE) as $chunk) {
DB::table('productos')->insert($chunk);
}
$this->insertCaracteristicas($caracteristicasToInsert);
}
/**
* Returns an array data parsed from productoColumn.
*
* @return array{solidario: bool, name: string, caracteristicas: array(Caracteristica)}
*/
private function parseAndFormatName($productoColumn): array {
$solidario = Str::contains($productoColumn, '*');
$name = Str::replace('*','',$productoColumn);
$caracteristicas = [];
if (Str::contains($name, 'S-G'))
$caracteristicas[] = Caracteristica::where('key','S-G')->first()->id;
if (Str::contains($name, 'S-A'))
$caracteristicas[] = Caracteristica::where('key','S-A')->first()->id;
if (Str::contains($name, 'S-S'))
$caracteristicas[] = Caracteristica::where('key','S-S')->first()->id;
if (Str::contains($name, 'S-P-A'))
$caracteristicas[] = Caracteristica::where('key','S-P-A')->first()->id;
if ($caracteristicas) {
$name = Str::replaceMatches('/\(S\-.*\)/', '', $name);
}
return [
'solidario' => $solidario,
'name' => trim($name),
'caracteristicas' => $caracteristicas
];
}
private function insertCaracteristicas($caracteristicasToInsert) : void {
foreach ($caracteristicasToInsert as $key => $item) {
$name = $item['name'];
$match = Producto::where('name',$name)->first();
if ($match) {
foreach ($item['caracteristicas'] as $key => $caracteristica) {
DB::table('productos_caracteristicas')->insert([
'producto_id' => $match->id,
'caracteristica_id' => $caracteristica,
]);
}
}
}
}
}

View File

@ -7,6 +7,8 @@
class DatabaseSeeder extends Seeder
{
const CHUNK_SIZE = 10;
/**
* Seed the application's database.
*/
@ -15,6 +17,7 @@ public function run(): void
$this->call([
BarrioSeeder::class,
CaracteristicaSeeder::class,
CanastaSeeder::class,
]);
}
}