Agregadas nuevas funciones para calcular total de pedidos

This commit is contained in:
Alejandro Tasistro 2025-03-17 16:49:43 -03:00
parent d4bffd1df8
commit 584cebb902

View file

@ -10,7 +10,8 @@ use App\Filtros\FiltroDeSubpedido;
class Subpedido extends Model class Subpedido extends Model
{ {
public $timestamps = false; const COSTO_TRANSPORTE = 15;
public $timestamps = false;
protected $fillable = ['grupo_de_compra_id', 'aprobado', 'nombre', 'devoluciones_total', 'devoluciones_notas']; protected $fillable = ['grupo_de_compra_id', 'aprobado', 'nombre', 'devoluciones_total', 'devoluciones_notas'];
public function productos() public function productos()
@ -40,10 +41,72 @@ class Subpedido extends Model
return $filtros->aplicar($query); return $filtros->aplicar($query);
} }
public function total()
{
return ceil($this->totalBarrial() + $this->totalCentral());
}
public function totalBarrial()
{
return DB::table('producto_subpedido')
->join('productos', 'producto_subpedido.producto_id', '=', 'productos.id')
->where('producto_subpedido.subpedido_id', $this->id)
->where('productos.nombre', 'like', '%barrial%')
->selectRaw('SUM(productos.precio * producto_subpedido.cantidad) as total')
->value('total');
}
public function totalCentral()
{
return $this->totalCentralesQueNoPaganTransporte() + $this->totalCentralesQuePaganTransporte() + $this->totalTransporte();
}
public function totalCentralesQueNoPaganTransporte()
{
$total = DB::table('producto_subpedido')
->join('productos', 'producto_subpedido.producto_id', '=', 'productos.id')
->where('producto_subpedido.subpedido_id', $this->id)
->where('productos.nombre', 'not like', '%barrial%')
->where(function ($query) {
$query->where('productos.categoria', 'like', '%SUBSIDIADO%')
->orWhere('productos.bono', true);
})
->selectRaw('SUM(productos.precio * producto_subpedido.cantidad) as total')
->value('total');
return $total;
}
public function totalCentralesQuePaganTransporte()
{
return DB::table('producto_subpedido')
->join('productos', 'producto_subpedido.producto_id', '=', 'productos.id')
->where('producto_subpedido.subpedido_id', $this->id)
->where('productos.nombre', 'not like', '%barrial%')
->where('productos.bono', false)
->where('productos.categoria', 'not like', '%SUBSIDIADO%')
->selectRaw('SUM(productos.precio * producto_subpedido.cantidad) as total')
->value('total');
}
public function totalTransporte()
{
return $this->cantidadTransporte() * Subpedido::COSTO_TRANSPORTE;
}
public function cantidadTransporte()
{
return ceil($this->totalCentralesQuePaganTransporte()/500);
}
//Subtotal de dinero de productos del pedido, sin bonos ni transporte //Subtotal de dinero de productos del pedido, sin bonos ni transporte
public function totalSinBonos() public function totalSinBonos()
{ {
return $this->productosSinBonos()->sum('total'); return DB::table('producto_subpedido')
->join('productos', 'producto_subpedido.producto_id', '=', 'productos.id')
->where('producto_subpedido.subpedido_id', $this->id)
->where('productos.bono', false)
->selectRaw('CEILING(SUM(productos.precio * producto_subpedido.cantidad)) as total')
->value('total');
} }
public function totalParaTransporte() { public function totalParaTransporte() {
@ -59,7 +122,14 @@ class Subpedido extends Model
//Cantidad de bonos de transporte //Cantidad de bonos de transporte
public function cantidadBDT() public function cantidadBDT()
{ {
return ceil($this->totalParaTransporte() / 500); $total = DB::table('producto_subpedido')
->join('productos', 'producto_subpedido.producto_id', '=', 'productos.id')
->where('producto_subpedido.subpedido_id', $this->id)
->where('productos.bono', false)
->where('productos.categoria', 'not like', '%SUBSIDIADO%')
->selectRaw('CEILING(SUM(productos.precio * producto_subpedido.cantidad)) as total')
->value('total');
return ceil($total/500);
} }
//Subtotal de dinero de bonos de transporte //Subtotal de dinero de bonos de transporte