pedi3/database/seeders/CanastaSeeder.php

107 lines
3.7 KiB
PHP
Raw Normal View History

<?php
namespace Database\Seeders;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
2024-03-12 23:56:05 -03:00
use Illuminate\Support\Facades\Date;
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
{
2024-07-09 23:44:58 -03:00
$columnaTipo = 'Tipo';
$columnaProducto = 'Producto';
$columnaPrecio = 'Precio';
$tipos = ['P','PTC','B'];
$csv = Reader::createFromPath(resource_path('csv/productos.csv'), 'r');
$csv->setDelimiter('|');
$csv->setHeaderOffset(0);
$records = $csv->getRecords();
2024-07-09 23:44:58 -03:00
$productos = [];
$caracteristicasAInsertar = [];
$categoriaActual;
foreach ($records as $i => $record) {
2024-07-09 23:44:58 -03:00
$tipo = trim($record[$columnaTipo]);
if (!in_array($tipo, $tipos)) {
if (!Str::contains($tipo,'¿') && ($tipo != 'T')) {
2024-07-09 23:44:58 -03:00
$categoriaActual = Categoria::firstOrCreate(['nombre' => $tipo]);
}
} else {
2024-07-09 23:44:58 -03:00
[$solidario, $nombre, $caracteristicas] = $this->parsearNombre($record[$columnaProducto]);
2024-07-09 23:44:58 -03:00
$productos[] = [
'nombre' => $nombre,
2024-07-09 23:44:58 -03:00
'precio' => $record[$columnaPrecio],
'solidario' => $solidario,
'bono' => $tipo == 'B',
2024-07-09 23:44:58 -03:00
'categoria_id' => $categoriaActual->id,
2024-03-12 23:56:05 -03:00
'created_at' => Date::now(),
'updated_at' => Date::now(),
];
2024-07-09 23:44:58 -03:00
$caracteristicasAInsertar[] = [
'nombre' => $nombre,
'caracteristicas' => $caracteristicas
];
}
}
2024-07-09 23:44:58 -03:00
foreach (array_chunk($productos,DatabaseSeeder::CHUNK_SIZE) as $chunk)
DB::table('productos')->insert($chunk);
2024-07-09 23:44:58 -03:00
$this->insertarCaracteristicas($caracteristicasAInsertar);
}
/**
2024-07-09 23:44:58 -03:00
* Devuelve un array con datos parseados de $columnaProducto
*
2024-07-03 19:23:24 -03:00
* @return array{solidario: bool, nombre: string, caracteristicas: array(Caracteristica)}
*/
2024-07-09 23:44:58 -03:00
private function parsearNombre($columnaProducto): array {
$solidario = Str::contains($columnaProducto, '*');
$nombre = Str::replace('*','',$columnaProducto);
$caracteristicas = [];
2024-07-03 19:23:24 -03:00
if (Str::contains($nombre, 'S-G'))
$caracteristicas[] = Caracteristica::where('codigo','S-G')->first()->id;
if (Str::contains($nombre, 'S-A'))
$caracteristicas[] = Caracteristica::where('codigo','S-A')->first()->id;
if (Str::contains($nombre, 'S-S'))
$caracteristicas[] = Caracteristica::where('codigo','S-S')->first()->id;
if (Str::contains($nombre, 'S-P-A'))
$caracteristicas[] = Caracteristica::where('codigo','S-P-A')->first()->id;
if ($caracteristicas) {
2024-07-03 19:23:24 -03:00
$nombre = Str::replaceMatches('/\(S\-.*\)/', '', $nombre);
}
return [$solidario, trim($nombre), $caracteristicas];
}
2024-07-09 23:44:58 -03:00
private function insertarCaracteristicas($caracteristicasAInsertar) : void {
foreach ($caracteristicasAInsertar as $codigo => $item) {
2024-07-03 19:23:24 -03:00
$nombre = $item['nombre'];
$match = Producto::where('nombre',$nombre)->first();
if ($match) {
2024-07-03 19:23:24 -03:00
foreach ($item['caracteristicas'] as $codigo => $caracteristica) {
2024-07-09 23:44:58 -03:00
$match->caracteristicas()->attach($caracteristica);
}
}
}
}
}