152 lines
4.4 KiB
PHP
152 lines
4.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;
|
|
use League\Csv\Reader;
|
|
|
|
class CanastaHelper
|
|
{
|
|
const FILA_HEADER = "Tipo";
|
|
const ULTIMA_FILA = "TOTAL";
|
|
const ARCHIVO_SUBIDO = 'Archivo subido';
|
|
const CANASTA_CARGADA = 'Canasta cargada';
|
|
|
|
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();
|
|
|
|
$csv = Reader::createFromPath(resource_path($archivo), 'r');
|
|
$csv->setDelimiter("|");
|
|
$iHeader = self::obtenerIndiceDeHeader($csv);
|
|
$csv->setHeaderOffset($iHeader);
|
|
$registros = $csv->getRecords();
|
|
|
|
$toInsert = [];
|
|
$categoria = '';
|
|
foreach($registros as $i => $registro){
|
|
//filas que están arriba del header
|
|
if ($i <= $iHeader){
|
|
continue;
|
|
}
|
|
|
|
//finalizar
|
|
if ($registro[self::FILA_HEADER] == self::ULTIMA_FILA) {
|
|
break;
|
|
}
|
|
|
|
//filas que no tienen tipo
|
|
if (!Arr::has($registro,self::FILA_HEADER)|| trim($registro[self::FILA_HEADER]) == ''){
|
|
var_dump("no hay tipo en la fila " . $i);
|
|
continue;
|
|
}
|
|
|
|
//saltear bono de transporte
|
|
if ($registro[self::FILA_HEADER] == "T"){
|
|
continue;
|
|
}
|
|
|
|
//obtener categoria
|
|
if ($registro['Producto'] == '') {
|
|
//es la pregunta de la copa?
|
|
if (Str::contains($registro[self::FILA_HEADER],"¿")) { continue; }
|
|
$categoria = $registro[self::FILA_HEADER];
|
|
continue;
|
|
}
|
|
|
|
//completar producto
|
|
$toInsert[] = [
|
|
'fila' => $i,
|
|
'categoria' => $categoria,
|
|
'nombre' => trim(str_replace('*', ' ',$registro['Producto'])),
|
|
'precio' => $registro['Precio'],
|
|
'proveedor_id' => self::obtenerProveedor($registro['Producto']),
|
|
'bono' => $registro[self::FILA_HEADER] == "B",
|
|
'requiere_notas'=> $registro[self::FILA_HEADER] =="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 obtenerIndiceDeHeader($csv){
|
|
$registros = $csv->getRecords();
|
|
$iheader = 0;
|
|
foreach ($registros as $i => $registro){
|
|
if (strtolower($registro[0]) == strtolower(self::FILA_HEADER)) {
|
|
$iheader = $i;
|
|
break;
|
|
}
|
|
}
|
|
return $iheader;
|
|
}
|
|
|
|
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,
|
|
]);
|
|
}
|
|
}
|