diff --git a/app/Subpedido.php b/app/Subpedido.php index 62eccb1..f1c9bbc 100644 --- a/app/Subpedido.php +++ b/app/Subpedido.php @@ -10,7 +10,8 @@ use App\Filtros\FiltroDeSubpedido; 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']; public function productos() @@ -40,10 +41,72 @@ class Subpedido extends Model 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 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() { @@ -59,7 +122,14 @@ class Subpedido extends Model //Cantidad de bonos de transporte 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