pedi3/app/Models/Barrio.php

78 lines
1.9 KiB
PHP
Raw Normal View History

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Model;
class Barrio extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'name',
];
/**
* La región a la que pertenece el barrio.
*/
public function region(): BelongsTo
{
return $this->belongsTo(Region::class);
}
2024-03-11 19:41:52 -03:00
/**
* Los pedidos que pertenecen al barrio.
*/
public function pedidos(): HasMany
{
return $this->hasMany(Pedido::class);
}
2024-03-19 22:34:13 -03:00
function crearPedido(string $nombre) : Pedido {
return $this->pedidos()->create(['name' => $name]);
}
function pedidosConfirmados() {
return $this->pedidos()->where('confirmed',true);
}
2024-03-19 22:34:13 -03:00
private function calcularTotalConfirmados(Closure $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.
*/
public function productos(): HasMany
{
return $this->hasMany(Producto::class);
}
}