Files
oficial/app/src/Model/Venta.php
Juan Pablo Vial 9c335fd350 FIX: Venta
2024-03-13 22:43:37 -03:00

121 lines
3.8 KiB
PHP

<?php
namespace Incoviba\Model;
use DateTimeInterface;
use Incoviba\Common\Ideal;
class Venta extends Ideal\Model
{
protected Venta\Propietario $propietario;
protected Venta\Propiedad $propiedad;
protected ?Venta\FormaPago $formaPago;
public DateTimeInterface $fecha;
public DateTimeInterface $fechaIngreso;
public float $valor;
public bool $relacionado;
protected ?Venta\Entrega $entrega;
public float $uf;
protected ?Venta\Pago $resciliacion;
public ?array $estados;
public ?Venta\EstadoVenta $currentEstado;
public function propietario(): Venta\Propietario
{
if (!isset($this->propietario)) {
$this->propietario = $this->runFactory('propietario');
}
return $this->propietario;
}
public function propiedad(): Venta\Propiedad
{
if (!isset($this->propiedad)) {
$this->propiedad = $this->runFactory('propiedad');
}
return $this->propiedad;
}
public function formaPago(): ?Venta\FormaPago
{
if (!isset($this->formaPago)) {
$this->formaPago = $this->runFactory('formaPago');
}
return $this->formaPago;
}
public function setFormaPago(Venta\FormaPago $formaPago): Venta
{
$this->formaPago = $formaPago;
return $this;
}
public function entrega(): ?Venta\Entrega
{
if (!isset($this->entrega)) {
$this->entrega = $this->runFactory('entrega');
}
return $this->entrega;
}
public function resciliacion(): ?Venta\Pago
{
if (!isset($this->resciliacion)) {
$this->resciliacion = $this->runFactory('resciliacion');
}
return $this->resciliacion;
}
public function estados(): array
{
if (!isset($this->estados)) {
$this->estados = $this->runFactory('estados');
}
return $this->estados ?? [];
}
public function currentEstado(): ?Venta\EstadoVenta
{
if (!isset($this->currentEstado)) {
$this->currentEstado = $this->runFactory('currentEstado');
}
return $this->currentEstado;
}
public function proyecto(): Proyecto
{
return $this->propiedad()->proyecto();
}
protected float $valor_util;
public function util(): float
{
if (!isset($this->valor_util)) {
$sum = $this->valor;
$sum -= array_reduce($this->propiedad()->estacionamientos(), function(float $sum, Venta\Unidad $unidad) {
return $unidad->valor ?? $unidad->precio($this->fecha)->valor;
}, 0);
$sum -= array_reduce($this->propiedad()->bodegas(), function(float $sum, Venta\Unidad $unidad) {
return $unidad->valor ?? $unidad->precio($this->fecha)->valor;
}, 0);
$this->valor_util = $sum;
}
return $this->valor_util;
}
public function saldo(string $moneda = Venta\Pago::UF): float
{
$valor = $this->valor * (($moneda === Venta\Pago::UF) ? 1 : $this->uf);
return $valor - $this->formaPago()->total($moneda);
}
public function jsonSerialize(): mixed
{
return array_merge(parent::jsonSerialize(), [
'propietario' => $this->propietario(),
'propiedad' => $this->propiedad(),
'forma_pago' => $this->formaPago()?->ids(),
'fecha' => $this->fecha->format('Y-m-d'),
'fecha_ingreso' => $this->fechaIngreso->format('Y-m-d'),
'valor' => $this->valor,
'relacionado' => $this->relacionado,
'proyecto' => $this->proyecto(),
'estados' => array_map(function(Venta\EstadoVenta $estado) {return $estado->id;}, $this->estados()),
'current_estado' => $this->currentEstado()
]);
}
}