146 lines
4 KiB
PHP
146 lines
4 KiB
PHP
<?php
|
|
|
|
namespace App\Helpers;
|
|
|
|
use App\Producto;
|
|
use App\Proveedor;
|
|
use App\CanastaLog;
|
|
use DatabaseSeeder;
|
|
use Illuminate\Support\Arr;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Str;
|
|
|
|
|
|
class CanastaHelper
|
|
{
|
|
const TIPO = "Tipo";
|
|
const TOTAL = "TOTAL";
|
|
const ARCHIVO_SUBIDO = 'Archivo subido';
|
|
const CANASTA_CARGADA = 'Canasta cargada';
|
|
const TIPOS_BONO = ["B", "F", "BE"];
|
|
|
|
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) {
|
|
// finalizar
|
|
if ($registro[self::TIPO] == self::TOTAL)
|
|
break;
|
|
|
|
// saltear filas que no tienen tipo
|
|
if (self::noTieneTipo($registro)) {
|
|
var_dump("no hay tipo en la fila " . $i);
|
|
continue;
|
|
}
|
|
|
|
// saltear bono de transporte
|
|
if ($registro[self::TIPO] == "T"){
|
|
continue;
|
|
}
|
|
|
|
// obtener categoria si no hay producto
|
|
if ($registro['Producto'] == '') {
|
|
// no es la pregunta de la copa?
|
|
if (!Str::contains($registro[self::TIPO],"¿"))
|
|
$categoria = $registro[self::TIPO];
|
|
continue;
|
|
}
|
|
|
|
// completar producto
|
|
$toInsert[] = [
|
|
'fila' => $i,
|
|
'categoria' => $categoria,
|
|
'nombre' => trim(str_replace('*', '',$registro['Producto'])),
|
|
'precio' => $registro['Precio'],
|
|
'proveedor_id' => self::obtenerProveedor($registro['Producto']),
|
|
'bono' => in_array($registro[self::TIPO], self::TIPOS_BONO),
|
|
'requiere_notas'=> $registro[self::TIPO] =="PTC",
|
|
];
|
|
}
|
|
|
|
foreach (array_chunk($toInsert,DatabaseSeeder::CHUNK_SIZE) as $chunk) {
|
|
DB::table('productos')->insert($chunk);
|
|
}
|
|
|
|
self::agregarBonoBarrial();
|
|
|
|
self::log($archivo, self::CANASTA_CARGADA);
|
|
}
|
|
|
|
private static function obtenerProveedor($nombre) {
|
|
$result = null;
|
|
if (Str::contains($nombre,"*")){
|
|
$result = Proveedor::firstOrCreate([
|
|
'nombre' => 'Proveedor de economía solidaria',
|
|
'economia_solidaria' => 1,
|
|
'nacional' => 1
|
|
])->id;
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* @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');
|
|
});
|
|
|
|
DB::table('productos')->insert([
|
|
'fila' => 420,
|
|
'nombre' => "Bono barrial",
|
|
'precio' => 20,
|
|
'categoria' => $categoria,
|
|
'bono' => 1,
|
|
'proveedor_id' => null,
|
|
'requiere_notas'=> false,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* @param $registro
|
|
* @return bool
|
|
*/
|
|
public static function noTieneTipo($registro): bool
|
|
{
|
|
return !Arr::has($registro, self::TIPO) || trim($registro[self::TIPO]) == '';
|
|
}
|
|
}
|