78 lines
2.5 KiB
PHP
78 lines
2.5 KiB
PHP
<?php
|
|
namespace Incoviba\Model\Venta;
|
|
|
|
use DateTimeInterface;
|
|
use Incoviba\Common\Ideal;
|
|
use Incoviba\Common\Implement\Exception\EmptyResult;
|
|
use Incoviba\Model;
|
|
|
|
class Unidad extends Ideal\Model
|
|
{
|
|
public ?string $subtipo = '';
|
|
public int $piso;
|
|
public string $descripcion;
|
|
public ?string $orientacion = '';
|
|
public Model\Proyecto\ProyectoTipoUnidad $proyectoTipoUnidad;
|
|
public ?float $prorrateo;
|
|
|
|
public array $precios = [];
|
|
public ?Precio $currentPrecio = null;
|
|
|
|
public function precio(DateTimeInterface $dateTime): ?Precio
|
|
{
|
|
if ($this->currentPrecio !== null and $dateTime > $this->currentPrecio->current->fecha) {
|
|
return $this->currentPrecio;
|
|
}
|
|
$precio = array_reduce(array_filter($this->precios, function(Precio $precio) use ($dateTime) {
|
|
return $dateTime > $precio->current->fecha;
|
|
}), function(?Precio $max, Precio $precio) {
|
|
if ($max === null) {
|
|
return $precio;
|
|
}
|
|
return $max->current->fecha > $precio->current->fecha ? $max : $precio;
|
|
});
|
|
if ($precio === null) {
|
|
$precio = array_reduce(array_filter($this->precios, function(Precio $precio) use ($dateTime) {
|
|
return $dateTime < $precio->current->fecha;
|
|
}), function(?Precio $min, Precio $precio) {
|
|
if ($min === null) {
|
|
return $precio;
|
|
}
|
|
return $min->current->fecha < $precio->current->fecha ? $min : $precio;
|
|
});
|
|
}
|
|
|
|
return $precio;
|
|
}
|
|
|
|
protected bool $sold;
|
|
public function sold(): bool
|
|
{
|
|
if (!isset($this->sold)) {
|
|
$this->sold = false;
|
|
try {
|
|
$this->sold = $this->runFactory('sold') ?? false;
|
|
} catch (EmptyResult) {}
|
|
}
|
|
return $this->sold;
|
|
}
|
|
|
|
public function jsonSerialize(): mixed
|
|
{
|
|
$output = array_merge(parent::jsonSerialize(), [
|
|
'subtipo' => $this->subtipo,
|
|
'piso' => $this->piso,
|
|
'descripcion' => $this->descripcion,
|
|
'orientacion' => $this->orientacion,
|
|
'proyecto_tipo_unidad' => $this->proyectoTipoUnidad,
|
|
'prorrateo' => $this->prorrateo
|
|
]);
|
|
if (isset($this->precios)) {
|
|
$output['precios'] = $this->precios;
|
|
$output['current_precio'] = $this->currentPrecio;
|
|
}
|
|
$output['sold'] = $this->sold() ?? false;
|
|
return $output;
|
|
}
|
|
}
|