2024-03-11 19:08:31 -03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2024-03-19 15:40:27 -03:00
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
2024-03-11 19:08:31 -03:00
|
|
|
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-14 23:04:54 -03:00
|
|
|
|
2024-03-19 22:34:13 -03:00
|
|
|
function crearPedido(string $nombre) : Pedido {
|
|
|
|
return $this->pedidos()->create(['name' => $name]);
|
|
|
|
}
|
|
|
|
|
2024-03-19 22:13:49 -03:00
|
|
|
function pedidosConfirmados() {
|
|
|
|
return $this->pedidos()->where('confirmed',true);
|
|
|
|
}
|
|
|
|
|
2024-03-19 22:34:13 -03:00
|
|
|
private function calcularTotalConfirmados(Closure $closure = null) : float {
|
2024-03-19 22:13:49 -03:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2024-03-14 23:04:54 -03:00
|
|
|
/**
|
|
|
|
* Los productos que pertenecen al barrio.
|
|
|
|
*/
|
|
|
|
public function productos(): HasMany
|
|
|
|
{
|
|
|
|
return $this->hasMany(Producto::class);
|
|
|
|
}
|
2024-03-11 19:08:31 -03:00
|
|
|
}
|