forked from nathalie/pedi2
Usando CsvHelper en vez de Reader + metodo para fila de transporte movideo a TransporteHelper
This commit is contained in:
parent
ead7483e19
commit
b1b424897c
2 changed files with 25 additions and 43 deletions
|
@ -4,9 +4,9 @@ namespace App;
|
|||
|
||||
use App\Helpers\TransporteHelper;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use League\Csv\CannotInsertRecord;
|
||||
use League\Csv\Reader;
|
||||
use League\Csv\Writer;
|
||||
use Mpdf\Mpdf;
|
||||
|
||||
|
@ -17,9 +17,9 @@ class GrupoDeCompra extends Model
|
|||
protected $table = 'grupos_de_compra';
|
||||
protected $hidden = ['password'];
|
||||
|
||||
public function subpedidos()
|
||||
public function subpedidos(): HasMany
|
||||
{
|
||||
return $this->hasMany('App\Subpedido');
|
||||
return $this->hasMany(Subpedido::class);
|
||||
}
|
||||
|
||||
public function toggleDevoluciones()
|
||||
|
@ -139,48 +139,10 @@ class GrupoDeCompra extends Model
|
|||
$template[$fila] = GrupoDeCompra::filaVacia($productosIDNombre[$id], $columns);
|
||||
$num_fila = $fila + 1;
|
||||
}
|
||||
$template[GrupoDeCompra::obtenerFilaDeBonoTransporte()] = GrupoDeCompra::filaVacia("Bonos de transporte", $columns);
|
||||
$template[TransporteHelper::filaTransporte()] = GrupoDeCompra::filaVacia("Bonos de transporte", $columns);
|
||||
return $template;
|
||||
}
|
||||
|
||||
private static function obtenerFilaDeBonoTransporte()
|
||||
{
|
||||
$csv = Reader::createFromPath(resource_path('csv/productos.csv'), 'r');
|
||||
$csv->setDelimiter("|");
|
||||
$csv->setEnclosure("'");
|
||||
$registros = $csv->getRecords();
|
||||
|
||||
foreach ($registros as $key => $registro)
|
||||
if ($registro[0] == 'T') return $key;
|
||||
|
||||
throw new Exception('No hay bono de transporte');
|
||||
}
|
||||
|
||||
private function totalPedidosSinBonos()
|
||||
{
|
||||
$total = 0;
|
||||
foreach ($this->pedidosAprobados() as $pedido) {
|
||||
$total += ceil($pedido->totalSinBonos());
|
||||
}
|
||||
return $total;
|
||||
}
|
||||
|
||||
public function totalBonosBarriales()
|
||||
{
|
||||
$total = 0;
|
||||
$bonoBarrial = Producto::where('nombre', 'LIKE', '%barrial%')->first();
|
||||
if ($bonoBarrial) {
|
||||
$pedidos = $this->pedidosAprobados();
|
||||
foreach ($pedidos as $pedido) {
|
||||
$bonoPedido = $pedido->productos()->find($bonoBarrial["id"]);
|
||||
if ($bonoPedido) {
|
||||
$total += $bonoPedido["pivot"]["total"];
|
||||
}
|
||||
}
|
||||
}
|
||||
return $total;
|
||||
}
|
||||
|
||||
public function exportarPedidoEnCSV()
|
||||
{
|
||||
$records = $this->generarColumnaCantidades();
|
||||
|
@ -212,7 +174,7 @@ class GrupoDeCompra extends Model
|
|||
$records[$fila][1] = $producto_pedido->cantidad_pedida;
|
||||
}
|
||||
|
||||
$records[$this->obtenerFilaDeBonoTransporte()][1] = $this->cantidadTransporte();
|
||||
$records[TransporteHelper::filaTransporte()][1] = $this->cantidadTransporte();
|
||||
|
||||
return $records;
|
||||
}
|
||||
|
|
|
@ -2,6 +2,10 @@
|
|||
|
||||
namespace App\Helpers;
|
||||
|
||||
use App\CanastaLog;
|
||||
use Exception;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class TransporteHelper
|
||||
{
|
||||
const COSTO_TRANSPORTE = 15;
|
||||
|
@ -16,4 +20,20 @@ class TransporteHelper
|
|||
{
|
||||
return self::cantidadTransporte($monto) * self::COSTO_TRANSPORTE;
|
||||
}
|
||||
|
||||
public static function filaTransporte()
|
||||
{
|
||||
$ultimaCanasta = CanastaLog::where('descripcion', CanastaHelper::CANASTA_CARGADA)
|
||||
->orderBy('created_at', 'desc')
|
||||
->pluck('path')
|
||||
->first();
|
||||
|
||||
$registros = CsvHelper::getRecords($ultimaCanasta);
|
||||
|
||||
foreach ($registros as $key => $registro)
|
||||
if ($registro[CanastaHelper::TIPO] == 'T') return $key;
|
||||
|
||||
Log::error('No hay fila de tipo T en la planilla: ' . $ultimaCanasta);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue