Refactor: usando CsvHelper, cambio de nombre en constantes, eliminado metodo innecesario, extraido predicado 'noTieneTipo' y cosas de formato
This commit is contained in:
parent
7c7149c5a1
commit
eb05a7de6a
1 changed files with 36 additions and 42 deletions
|
@ -9,12 +9,12 @@ use DatabaseSeeder;
|
||||||
use Illuminate\Support\Arr;
|
use Illuminate\Support\Arr;
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
use Illuminate\Support\Str;
|
use Illuminate\Support\Str;
|
||||||
use League\Csv\Reader;
|
|
||||||
|
|
||||||
class CanastaHelper
|
class CanastaHelper
|
||||||
{
|
{
|
||||||
const FILA_HEADER = "Tipo";
|
const TIPO = "Tipo";
|
||||||
const ULTIMA_FILA = "TOTAL";
|
const TOTAL = "TOTAL";
|
||||||
const ARCHIVO_SUBIDO = 'Archivo subido';
|
const ARCHIVO_SUBIDO = 'Archivo subido';
|
||||||
const CANASTA_CARGADA = 'Canasta cargada';
|
const CANASTA_CARGADA = 'Canasta cargada';
|
||||||
|
|
||||||
|
@ -31,53 +31,43 @@ class CanastaHelper
|
||||||
public static function cargarCanasta($archivo) {
|
public static function cargarCanasta($archivo) {
|
||||||
self::limpiarTablas();
|
self::limpiarTablas();
|
||||||
|
|
||||||
$csv = Reader::createFromPath(resource_path($archivo), 'r');
|
$registros = CsvHelper::getRecords($archivo);
|
||||||
$csv->setDelimiter("|");
|
|
||||||
$iHeader = self::obtenerIndiceDeHeader($csv);
|
|
||||||
$csv->setHeaderOffset($iHeader);
|
|
||||||
$registros = $csv->getRecords();
|
|
||||||
|
|
||||||
$toInsert = [];
|
$toInsert = [];
|
||||||
$categoria = '';
|
$categoria = '';
|
||||||
foreach($registros as $i => $registro){
|
|
||||||
//filas que están arriba del header
|
|
||||||
if ($i <= $iHeader){
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
//finalizar
|
foreach($registros as $i => $registro) {
|
||||||
if ($registro[self::FILA_HEADER] == self::ULTIMA_FILA) {
|
// finalizar
|
||||||
|
if ($registro[self::TIPO] == self::TOTAL)
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
|
|
||||||
//filas que no tienen tipo
|
// saltear filas que no tienen tipo
|
||||||
if (!Arr::has($registro,self::FILA_HEADER)|| trim($registro[self::FILA_HEADER]) == ''){
|
if (self::noTieneTipo($registro)) {
|
||||||
var_dump("no hay tipo en la fila " . $i);
|
var_dump("no hay tipo en la fila " . $i);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
//saltear bono de transporte
|
// saltear bono de transporte
|
||||||
if ($registro[self::FILA_HEADER] == "T"){
|
if ($registro[self::TIPO] == "T"){
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
//obtener categoria
|
// obtener categoria si no hay producto
|
||||||
if ($registro['Producto'] == '') {
|
if ($registro['Producto'] == '') {
|
||||||
//es la pregunta de la copa?
|
// no es la pregunta de la copa?
|
||||||
if (Str::contains($registro[self::FILA_HEADER],"¿")) { continue; }
|
if (!Str::contains($registro[self::TIPO],"¿"))
|
||||||
$categoria = $registro[self::FILA_HEADER];
|
$categoria = $registro[self::TIPO];
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
//completar producto
|
// completar producto
|
||||||
$toInsert[] = [
|
$toInsert[] = [
|
||||||
'fila' => $i,
|
'fila' => $i,
|
||||||
'categoria' => $categoria,
|
'categoria' => $categoria,
|
||||||
'nombre' => trim(str_replace('*', ' ',$registro['Producto'])),
|
'nombre' => trim(str_replace('*', '',$registro['Producto'])),
|
||||||
'precio' => $registro['Precio'],
|
'precio' => $registro['Precio'],
|
||||||
'proveedor_id' => self::obtenerProveedor($registro['Producto']),
|
'proveedor_id' => self::obtenerProveedor($registro['Producto']),
|
||||||
'bono' => $registro[self::FILA_HEADER] == "B",
|
'bono' => $registro[self::TIPO] == "B",
|
||||||
'requiere_notas'=> $registro[self::FILA_HEADER] =="PTC",
|
'requiere_notas'=> $registro[self::TIPO] =="PTC",
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -90,18 +80,6 @@ class CanastaHelper
|
||||||
self::log($archivo, self::CANASTA_CARGADA);
|
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) {
|
private static function obtenerProveedor($nombre) {
|
||||||
$result = null;
|
$result = null;
|
||||||
if (Str::contains($nombre,"*")){
|
if (Str::contains($nombre,"*")){
|
||||||
|
@ -137,7 +115,14 @@ class CanastaHelper
|
||||||
|
|
||||||
private static function agregarBonoBarrial()
|
private static function agregarBonoBarrial()
|
||||||
{
|
{
|
||||||
$categoria = Producto::all()->pluck('categoria')->unique()->flatten()->first(function ($c) { return Str::contains($c, 'BONO'); });
|
$categoria = Producto::all()
|
||||||
|
->pluck('categoria')
|
||||||
|
->unique()
|
||||||
|
->flatten()
|
||||||
|
->first(function ($c) {
|
||||||
|
return Str::contains($c, 'BONO');
|
||||||
|
});
|
||||||
|
|
||||||
DB::table('productos')->insert([
|
DB::table('productos')->insert([
|
||||||
'fila' => 420,
|
'fila' => 420,
|
||||||
'nombre' => "Bono barrial",
|
'nombre' => "Bono barrial",
|
||||||
|
@ -148,4 +133,13 @@ class CanastaHelper
|
||||||
'requiere_notas'=> false,
|
'requiere_notas'=> false,
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $registro
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public static function noTieneTipo($registro): bool
|
||||||
|
{
|
||||||
|
return !Arr::has($registro, self::TIPO) || trim($registro[self::TIPO]) == '';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue