pedi3/database/seeders/CanastaSeeder.php

110 lines
3.8 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
{
$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')) {
2024-07-03 19:23:24 -03:00
$currentCategoria = Categoria::firstOrCreate(['nombre' => $tipo]);
}
} else {
[$solidario, $nombre, $caracteristicas] = $this->parseAndFormatName($record[$productoColumn]);
$productosToInsert[] = [
'nombre' => $nombre,
2024-07-03 19:23:24 -03:00
'precio' => $record[$precioColumn],
'solidario' => $solidario,
'bono' => $tipo == 'B',
2024-03-12 23:56:05 -03:00
'categoria_id' => $currentCategoria->id,
'created_at' => Date::now(),
'updated_at' => Date::now(),
];
$caracteristicasToInsert[] = [
'nombre' => $nombre,
'caracteristicas' => $caracteristicas
];
}
}
2024-03-14 22:49:24 -03:00
foreach (array_chunk($productosToInsert,DatabaseSeeder::CHUNK_SIZE) as $chunk)
DB::table('productos')->insert($chunk);
$this->insertCaracteristicas($caracteristicasToInsert);
}
/**
2024-03-14 22:49:24 -03:00
* Returns an array with data parsed from productoColumn.
*
2024-07-03 19:23:24 -03:00
* @return array{solidario: bool, nombre: string, caracteristicas: array(Caracteristica)}
*/
private function parseAndFormatName($productoColumn): array {
$solidario = Str::contains($productoColumn, '*');
2024-07-03 19:23:24 -03:00
$nombre = Str::replace('*','',$productoColumn);
$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];
}
private function insertCaracteristicas($caracteristicasToInsert) : void {
2024-07-03 19:23:24 -03:00
foreach ($caracteristicasToInsert as $codigo => $item) {
$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) {
DB::table('producto_caracteristica')->insert([
'producto_id' => $match->id,
'caracteristica_id' => $caracteristica,
]);
}
}
}
}
}