2024-03-12 22:28:22 -03:00
|
|
|
<?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;
|
2024-03-12 22:28:22 -03:00
|
|
|
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';
|
2024-03-12 22:28:22 -03:00
|
|
|
$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;
|
2024-03-12 22:28:22 -03:00
|
|
|
foreach ($records as $i => $record) {
|
2024-07-09 23:44:58 -03:00
|
|
|
$tipo = trim($record[$columnaTipo]);
|
2024-03-12 22:28:22 -03:00
|
|
|
|
|
|
|
if (!in_array($tipo, $tipos)) {
|
|
|
|
if (!Str::contains($tipo,'¿') && ($tipo != 'T')) {
|
2024-07-09 23:44:58 -03:00
|
|
|
$categoriaActual = Categoria::firstOrCreate(['nombre' => $tipo]);
|
2024-03-12 22:28:22 -03:00
|
|
|
}
|
|
|
|
} else {
|
2024-07-09 23:44:58 -03:00
|
|
|
[$solidario, $nombre, $caracteristicas] = $this->parsearNombre($record[$columnaProducto]);
|
2024-03-12 22:28:22 -03:00
|
|
|
|
2024-07-09 23:44:58 -03:00
|
|
|
$productos[] = [
|
2024-07-03 19:46:48 -03:00
|
|
|
'nombre' => $nombre,
|
2024-07-09 23:44:58 -03:00
|
|
|
'precio' => $record[$columnaPrecio],
|
2024-07-03 19:46:48 -03:00
|
|
|
'solidario' => $solidario,
|
2024-03-12 22:28:22 -03:00
|
|
|
'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-03-12 22:28:22 -03:00
|
|
|
];
|
|
|
|
|
2024-07-09 23:44:58 -03:00
|
|
|
$caracteristicasAInsertar[] = [
|
2024-07-03 19:46:48 -03:00
|
|
|
'nombre' => $nombre,
|
|
|
|
'caracteristicas' => $caracteristicas
|
2024-03-12 22:28:22 -03:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-09 23:44:58 -03:00
|
|
|
foreach (array_chunk($productos,DatabaseSeeder::CHUNK_SIZE) as $chunk)
|
2024-03-12 22:28:22 -03:00
|
|
|
DB::table('productos')->insert($chunk);
|
|
|
|
|
2024-07-09 23:44:58 -03:00
|
|
|
$this->insertarCaracteristicas($caracteristicasAInsertar);
|
2024-03-12 22:28:22 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-07-09 23:44:58 -03:00
|
|
|
* Devuelve un array con datos parseados de $columnaProducto
|
2024-03-12 22:28:22 -03:00
|
|
|
*
|
2024-07-03 19:23:24 -03:00
|
|
|
* @return array{solidario: bool, nombre: string, caracteristicas: array(Caracteristica)}
|
2024-03-12 22:28:22 -03:00
|
|
|
*/
|
2024-07-09 23:44:58 -03:00
|
|
|
private function parsearNombre($columnaProducto): array {
|
|
|
|
$solidario = Str::contains($columnaProducto, '*');
|
|
|
|
$nombre = Str::replace('*','',$columnaProducto);
|
2024-03-12 22:28:22 -03:00
|
|
|
|
|
|
|
$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;
|
2024-03-12 22:28:22 -03:00
|
|
|
|
|
|
|
if ($caracteristicas) {
|
2024-07-03 19:23:24 -03:00
|
|
|
$nombre = Str::replaceMatches('/\(S\-.*\)/', '', $nombre);
|
2024-03-12 22:28:22 -03:00
|
|
|
}
|
|
|
|
|
2024-07-03 19:46:48 -03:00
|
|
|
return [$solidario, trim($nombre), $caracteristicas];
|
2024-03-12 22:28:22 -03:00
|
|
|
}
|
|
|
|
|
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();
|
2024-03-12 22:28:22 -03:00
|
|
|
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);
|
2024-03-12 22:28:22 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|