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
|
|
|
|
{
|
|
|
|
$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]);
|
2024-03-12 22:28:22 -03:00
|
|
|
}
|
|
|
|
} else {
|
2024-03-14 22:49:24 -03:00
|
|
|
[$solidario, $name, $caracteristicas] = $this->parseAndFormatName($record[$productoColumn]);
|
2024-03-12 22:28:22 -03:00
|
|
|
|
|
|
|
$productosToInsert[] = [
|
2024-07-03 19:23:24 -03:00
|
|
|
'nombre' => $parsed['nombre'],
|
|
|
|
'precio' => $record[$precioColumn],
|
2024-03-12 22:28:22 -03:00
|
|
|
'solidario' => $parsed['solidario'],
|
|
|
|
'bono' => $tipo == 'B',
|
2024-03-12 23:56:05 -03:00
|
|
|
'categoria_id' => $currentCategoria->id,
|
|
|
|
'created_at' => Date::now(),
|
|
|
|
'updated_at' => Date::now(),
|
2024-03-12 22:28:22 -03:00
|
|
|
];
|
|
|
|
|
|
|
|
$caracteristicasToInsert[] = [
|
2024-07-03 19:23:24 -03:00
|
|
|
'nombre' => $parsed['nombre'],
|
2024-03-12 22:28:22 -03:00
|
|
|
'caracteristicas' => $parsed['caracteristicas']
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-14 22:49:24 -03:00
|
|
|
foreach (array_chunk($productosToInsert,DatabaseSeeder::CHUNK_SIZE) as $chunk)
|
2024-03-12 22:28:22 -03:00
|
|
|
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-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
|
|
|
*/
|
|
|
|
private function parseAndFormatName($productoColumn): array {
|
|
|
|
$solidario = Str::contains($productoColumn, '*');
|
2024-07-03 19:23:24 -03:00
|
|
|
$nombre = Str::replace('*','',$productoColumn);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
'solidario' => $solidario,
|
2024-07-03 19:23:24 -03:00
|
|
|
'nombre' => trim($nombre),
|
2024-03-12 22:28:22 -03:00
|
|
|
'caracteristicas' => $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();
|
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-03-12 22:28:22 -03:00
|
|
|
DB::table('productos_caracteristicas')->insert([
|
|
|
|
'producto_id' => $match->id,
|
|
|
|
'caracteristica_id' => $caracteristica,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|