Files
oficial/app/src/Model/Venta.php

121 lines
3.8 KiB
PHP
Raw Normal View History

2023-07-28 16:22:20 -04:00
<?php
namespace Incoviba\Model;
use DateTimeInterface;
use Incoviba\Common\Ideal;
class Venta extends Ideal\Model
{
protected Venta\Propietario $propietario;
protected Venta\Propiedad $propiedad;
2024-03-13 14:38:44 -03:00
protected ?Venta\FormaPago $formaPago;
2023-07-28 16:22:20 -04:00
public DateTimeInterface $fecha;
public DateTimeInterface $fechaIngreso;
public float $valor;
public bool $relacionado;
protected ?Venta\Entrega $entrega;
2023-09-13 18:51:46 -03:00
public float $uf;
2024-03-13 22:43:37 -03:00
protected ?Venta\Pago $resciliacion;
2023-07-28 16:22:20 -04:00
public ?array $estados;
public ?Venta\EstadoVenta $currentEstado;
2023-07-28 16:22:20 -04:00
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;
}
2024-03-13 14:38:44 -03:00
public function formaPago(): ?Venta\FormaPago
{
if (!isset($this->formaPago)) {
$this->formaPago = $this->runFactory('formaPago');
}
return $this->formaPago;
}
2024-03-13 22:43:37 -03:00
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;
}
2023-12-21 19:15:11 -03:00
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
{
2023-11-23 23:35:19 -03:00
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) {
2023-12-04 19:00:21 -03:00
return $unidad->valor ?? $unidad->precio($this->fecha)->valor;
}, 0);
$sum -= array_reduce($this->propiedad()->bodegas(), function(float $sum, Venta\Unidad $unidad) {
2023-12-04 19:00:21 -03:00
return $unidad->valor ?? $unidad->precio($this->fecha)->valor;
}, 0);
$this->valor_util = $sum;
}
return $this->valor_util;
}
2024-03-13 22:43:37 -03:00
public function saldo(string $moneda = Venta\Pago::UF): float
{
2024-03-13 22:43:37 -03:00
$valor = $this->valor * (($moneda === Venta\Pago::UF) ? 1 : $this->uf);
2023-12-20 08:44:49 -03:00
return $valor - $this->formaPago()->total($moneda);
}
2023-07-28 16:22:20 -04:00
public function jsonSerialize(): mixed
{
return array_merge(parent::jsonSerialize(), [
'propietario' => $this->propietario(),
'propiedad' => $this->propiedad(),
2024-03-13 14:38:44 -03:00
'forma_pago' => $this->formaPago()?->ids(),
2023-07-28 16:22:20 -04:00
'fecha' => $this->fecha->format('Y-m-d'),
'fecha_ingreso' => $this->fechaIngreso->format('Y-m-d'),
'valor' => $this->valor,
'relacionado' => $this->relacionado,
2023-09-28 21:05:16 -03:00
'proyecto' => $this->proyecto(),
'estados' => array_map(function(Venta\EstadoVenta $estado) {return $estado->id;}, $this->estados()),
'current_estado' => $this->currentEstado()
2023-07-28 16:22:20 -04:00
]);
}
}