144 lines
		
	
	
	
		
			4.2 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			144 lines
		
	
	
	
		
			4.2 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\Facades\File;
 | 
						|
use Illuminate\Support\Str;
 | 
						|
use League\Csv\Exception;
 | 
						|
 | 
						|
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 canastaActual() {
 | 
						|
        $result = [];
 | 
						|
        $log = CanastaLog::where('descripcion', self::CANASTA_CARGADA)
 | 
						|
            ->orderBy('created_at', 'desc')
 | 
						|
            ->first();
 | 
						|
        $nombre = str_replace(storage_path(), "", $log->path);
 | 
						|
        $nombre = str_replace("/csv/canastas/", "", $nombre);
 | 
						|
        $result["nombre"] = str_replace(".csv", "", $nombre);
 | 
						|
        $result["fecha"] = $log->created_at;
 | 
						|
        return $result;
 | 
						|
    }
 | 
						|
 | 
						|
    public static function guardarCanasta($data, $path): string {
 | 
						|
        if (!File::exists(storage_path('csv/canastas'))) {
 | 
						|
            File::makeDirectory(storage_path('csv/canastas'), 0755, true);
 | 
						|
        }
 | 
						|
 | 
						|
        $nombre = $data->getClientOriginalName();
 | 
						|
 | 
						|
        $storage_path = storage_path($path);
 | 
						|
        $data->move($storage_path, $nombre);
 | 
						|
 | 
						|
        self::log($storage_path . $nombre, self::ARCHIVO_SUBIDO);
 | 
						|
 | 
						|
        return $nombre;
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @throws Exception
 | 
						|
     */
 | 
						|
    public static function cargarCanasta($archivo) {
 | 
						|
        self::limpiarTablas();
 | 
						|
 | 
						|
        $registros = CsvHelper::getRecords($archivo, "No se pudo leer el 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([
 | 
						|
            '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]) == '';
 | 
						|
    }
 | 
						|
}
 |