122 lines
		
	
	
	
		
			3.5 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			122 lines
		
	
	
	
		
			3.5 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| <?php
 | |
| 
 | |
| namespace App\Helpers;
 | |
| 
 | |
| use App\Producto;
 | |
| use App\CanastaLog;
 | |
| use DatabaseSeeder;
 | |
| use Illuminate\Support\Arr;
 | |
| use Illuminate\Support\Facades\DB;
 | |
| use Illuminate\Support\Str;
 | |
| 
 | |
| class CanastaHelper
 | |
| {
 | |
|     const TIPO = "Tipo";
 | |
|     const PRODUCTO = 'Producto';
 | |
|     const PRECIO = 'Precio';
 | |
|     const REGEX_BONO = "/^[BF]/i";
 | |
|     const ARCHIVO_SUBIDO = 'Archivo subido';
 | |
|     const CANASTA_CARGADA = 'Canasta cargada';
 | |
|     const PRODUCTO_TALLE_COLOR = "PTC";
 | |
| 
 | |
|     public static function guardarCanasta($data, $path): string {
 | |
|         $nombre = $data->getClientOriginalName();
 | |
| 
 | |
|         $data->move(resource_path($path), $nombre);
 | |
| 
 | |
|         self::log($path . $nombre, self::ARCHIVO_SUBIDO);
 | |
| 
 | |
|         return $nombre;
 | |
|     }
 | |
| 
 | |
|     public static function cargarCanasta($archivo) {
 | |
|         self::limpiarTablas();
 | |
| 
 | |
|         $registros = CsvHelper::getRecords($archivo);
 | |
|         $toInsert = [];
 | |
|         $categoria = '';
 | |
| 
 | |
|         foreach($registros as $i => $registro) {
 | |
|             // saltear bono de transporte y filas que no tienen tipo
 | |
|             if (self::noTieneTipo($registro) || $registro[self::TIPO] == "T")
 | |
|                 continue;
 | |
| 
 | |
|             // obtener categoria si no hay producto
 | |
|             if ($registro[self::PRODUCTO] == '') {
 | |
|                 // no es la pregunta de la copa?
 | |
|                 if (!Str::contains($registro[self::TIPO],"¿"))
 | |
|                     $categoria = $registro[self::TIPO];
 | |
|                 continue; // saltear si es la pregunta de la copa
 | |
|             }
 | |
| 
 | |
|             // completar producto
 | |
|             $toInsert[] = DatabaseSeeder::addTimestamps([
 | |
|                 'fila'          => $i,
 | |
|                 'categoria'     => $categoria,
 | |
|                 'nombre'        => trim(str_replace('*', '',$registro[self::PRODUCTO])),
 | |
|                 'precio'        => $registro[self::PRECIO],
 | |
|                 'es_solidario'  => Str::contains($registro[self::PRODUCTO],"*"),
 | |
|                 'bono'          => preg_match(self::REGEX_BONO, $registro[self::TIPO]),
 | |
|                 'requiere_notas'=> $registro[self::TIPO] == self::PRODUCTO_TALLE_COLOR,
 | |
|             ]);
 | |
|         }
 | |
| 
 | |
|         foreach (array_chunk($toInsert,DatabaseSeeder::CHUNK_SIZE) as $chunk)
 | |
|             Producto::insert($chunk);
 | |
| 
 | |
|         self::agregarBonoBarrial();
 | |
| 
 | |
|         self::log($archivo, self::CANASTA_CARGADA);
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * @param $path
 | |
|      * @param $descripcion
 | |
|      * @return void
 | |
|      */
 | |
|     private static function log($path, $descripcion): void
 | |
|     {
 | |
|         $log = new CanastaLog([
 | |
|             'path' => $path,
 | |
|             'descripcion' => $descripcion,
 | |
|         ]);
 | |
|         $log->save();
 | |
|     }
 | |
| 
 | |
|     private static function limpiarTablas()
 | |
|     {
 | |
|         DB::delete('delete from producto_subpedido');
 | |
|         DB::delete('delete from productos');
 | |
|         DB::delete('delete from subpedidos');
 | |
|     }
 | |
| 
 | |
|     private static function agregarBonoBarrial()
 | |
|     {
 | |
|         $categoria = Producto::all()
 | |
|             ->pluck('categoria')
 | |
|             ->unique()
 | |
|             ->flatten()
 | |
|             ->first(function ($c) {
 | |
|                 return Str::contains($c, 'BONO');
 | |
|             });
 | |
| 
 | |
|         Producto::create([
 | |
|             'fila' => 420,
 | |
|             'nombre' => "Bono barrial",
 | |
|             'precio' => 20,
 | |
|             'categoria' => $categoria,
 | |
|             'bono' => 1,
 | |
|             'es_solidario'  => 0,
 | |
|             'requiere_notas'=> false,
 | |
|         ]);
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * @param $registro
 | |
|      * @return bool
 | |
|      */
 | |
|     public static function noTieneTipo($registro): bool
 | |
|     {
 | |
|         return !Arr::has($registro, self::TIPO) || trim($registro[self::TIPO]) == '';
 | |
|     }
 | |
| }
 |