Lógica para calcular totales y bonos de transporte
This commit is contained in:
		
							parent
							
								
									03b9c4c065
								
							
						
					
					
						commit
						59428f04b8
					
				
					 3 changed files with 97 additions and 15 deletions
				
			
		| 
						 | 
				
			
			@ -33,6 +33,36 @@ class Barrio extends Model
 | 
			
		|||
        return $this->hasMany(Pedido::class);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    function pedidosConfirmados() {
 | 
			
		||||
        return $this->pedidos()->where('confirmed',true);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private function calcularTotalConfirmados($closure = null) : float {
 | 
			
		||||
        if (!$closure)
 | 
			
		||||
            $closure = fn($p) => $p->totalChismosa();
 | 
			
		||||
 | 
			
		||||
        return $this->pedidosConfirmados()->sum($closure);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    function totalARecaudar() : float {
 | 
			
		||||
        return $this->calcularTotalConfirmados();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    function totalNoBarriales() : float {
 | 
			
		||||
        return $this->calcularTotalConfirmados(fn($p) => $p->total($p->productosNoBarriales()));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    function totalTransporte() : float {
 | 
			
		||||
        $totalSinTransporte = $this->calcularTotalConfirmados(
 | 
			
		||||
            fn($p) => $p->total($p->productosSinTransporte())
 | 
			
		||||
        );
 | 
			
		||||
        return ($totalSinTransporte / Constants::DIVISOR_TRANSPORTE) * Constants::COSTO_TRANSPORTE;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    function totalATransferir() : float {
 | 
			
		||||
        return $this->totalNoBarriales() + $this->totalTransporte();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Los productos que pertenecen al barrio.
 | 
			
		||||
     */
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -2,6 +2,8 @@
 | 
			
		|||
 | 
			
		||||
namespace App\Models;
 | 
			
		||||
 | 
			
		||||
use App\Constants;
 | 
			
		||||
 | 
			
		||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
 | 
			
		||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
 | 
			
		||||
use Illuminate\Database\Eloquent\Model;
 | 
			
		||||
| 
						 | 
				
			
			@ -14,38 +16,84 @@ class Pedido extends Model
 | 
			
		|||
     * @var array<int, string>
 | 
			
		||||
     */
 | 
			
		||||
    protected $fillable = [
 | 
			
		||||
        'name',  
 | 
			
		||||
        'name', 'confirmed'
 | 
			
		||||
    ];
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * El barrio al que pertenece el pedido.
 | 
			
		||||
     */
 | 
			
		||||
    public function barrio(): BelongsTo
 | 
			
		||||
    {
 | 
			
		||||
    public function barrio(): BelongsTo {
 | 
			
		||||
        return $this->belongsTo(Barrio::class);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Los productos que pertenecen al pedido.
 | 
			
		||||
     */
 | 
			
		||||
    public function productos(): BelongsToMany
 | 
			
		||||
    {
 | 
			
		||||
    public function productos(): BelongsToMany {
 | 
			
		||||
        return $this->belongsToMany(Producto::class)->withPivot(['cantidad']);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function productosSinBonos() {
 | 
			
		||||
        return $this->productos()->where('bono',0)->all();
 | 
			
		||||
    /**
 | 
			
		||||
     * Los productos del pedido que no aportan para el
 | 
			
		||||
     * bono de transporte
 | 
			
		||||
     */
 | 
			
		||||
    function productosSinTransporte() {
 | 
			
		||||
        return $this->productos()->orWhere(['bono' => true, 'barrial' => true]);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Los productos del pedido que aportan para el
 | 
			
		||||
     * bono de transporte.
 | 
			
		||||
     */
 | 
			
		||||
    function productosConTransporte() {
 | 
			
		||||
        return $this->productos()->where(['bono' => false, 'barrial' => false]);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Los productos no barriales del pedido. 
 | 
			
		||||
     */
 | 
			
		||||
    function productosNoBarriales() {
 | 
			
		||||
        return $this->productos()->where('barrial',false);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Los bonos del pedido.
 | 
			
		||||
     */
 | 
			
		||||
    function bonos() {
 | 
			
		||||
        return $this->productos()->where('bono',1)->all();
 | 
			
		||||
        return $this->productos()->where('bono',true);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    function bonosDeTransporte() : int {
 | 
			
		||||
        $total = 0;
 | 
			
		||||
        foreach ($this->productosSinBonos() as $key => $producto) {
 | 
			
		||||
            $total += $producto->price * $producto->pivot->cantidad;
 | 
			
		||||
        }
 | 
			
		||||
        return 1 + ($total / 500);
 | 
			
		||||
    /**
 | 
			
		||||
     * Toma como parámetro una colección de productos
 | 
			
		||||
     * y devuelve la suma de los totales (precio * cantidad)
 | 
			
		||||
     * de cada uno.
 | 
			
		||||
     * 
 | 
			
		||||
     * Si la colección es null o no se pasa ningún parámetro
 | 
			
		||||
     * se toman todos los productos del pedido.
 | 
			
		||||
     */
 | 
			
		||||
    function total($productos = null) : float {
 | 
			
		||||
        if (!$productos)
 | 
			
		||||
            $productos = $this->productos();
 | 
			
		||||
 | 
			
		||||
        return $productos->sum(fn($p) => $p->price * $p->pivot->cantidad);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * El total de bonos de transporte del pedido
 | 
			
		||||
     */
 | 
			
		||||
    function totalTransporte() : int {
 | 
			
		||||
        if ($this->productos()->every(fn($prod) => !$prod->pagaTransporte())) 
 | 
			
		||||
            return 0;
 | 
			
		||||
        
 | 
			
		||||
        $cantidad = 1 + floor($this->total($this->productosConTransporte()) / Constants::DIVISOR_TRANSPORTE);
 | 
			
		||||
        return $cantidad * Constants::COSTO_TRANSPORTE;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * El total de los productos del pedido
 | 
			
		||||
     * sumado al total de los bonos de transporte
 | 
			
		||||
     */
 | 
			
		||||
    function totalChismosa() : float {
 | 
			
		||||
        return $this->total() + $this->totalTransporte();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -14,7 +14,7 @@ class Producto extends Model
 | 
			
		|||
     * @var array<int, string>
 | 
			
		||||
     */
 | 
			
		||||
    protected $fillable = [
 | 
			
		||||
        'name', 'price', 'solidario', 'bono', 'categoria_id', 'barrio_id'
 | 
			
		||||
        'name', 'price', 'solidario', 'bono', 'barrial', 'categoria_id', 'barrio_id'
 | 
			
		||||
    ];
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
| 
						 | 
				
			
			@ -48,4 +48,8 @@ class Producto extends Model
 | 
			
		|||
    {
 | 
			
		||||
        return $this->belongsTo(Barrio::class);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    function pagaTransporte() : bool {
 | 
			
		||||
        return !$this->bono && !$this->barrial;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		
		Reference in a new issue