2021-03-25 21:25:17 -03:00
< ? php
namespace Incoviba\old\Venta ;
use Incoviba\Common\Alias\OldModel as Model ;
use Incoviba\old\Proyecto\Proyecto ;
use Incoviba\old\Proyecto\ProyectoTipoUnidad ;
/**
*
* @ property int id
* @ property Proyecto proyecto
* @ property TipoUnidad tipo
* @ property string subtipo
* @ property int piso
* @ property string descripcion
* @ property string abreviacion
* @ property float m2
* @ property float terraza
* @ property float cubierta
* @ property float logia
* @ property char orientacion
* @ property float costo_inmobiliaria
* @ property ProyectoTipoUnidad pt
* @ property float valor
*
*/
class Unidad extends Model
{
public function proyecto ()
{
return $this -> belongs_to ( Proyecto :: class , 'proyecto' ) -> findOne ();
}
protected $propiedad ;
public function propiedad ()
{
if ( $this -> propiedad == null ) {
$this -> propiedad = $this -> hasMany ( PropiedadUnidad :: class , 'unidad' ) -> findOne ();
}
return $this -> propiedad ;
if ( $this -> tipo () -> descripcion == 'departamento' ) {
$propiedad = $this -> has_one ( Propiedad :: class , 'unidad_principal' );
if ( $propiedad ) {
return $propiedad -> findOne ();
}
return null ;
}
if ( $this -> tipo () -> descripcion == 'estacionamiento' ) {
// id | id; | ;id | ;id;
$propiedad = model ( Propiedad :: class ) -> whereLike ( 'estacionamientos' , $this -> id ) -> findOne ();
if ( $propiedad ) {
return $propiedad ;
}
$propiedad = model ( Propiedad :: class ) -> whereLike ( 'estacionamientos' , '%;' . $this -> id ) -> findOne ();
if ( $propiedad ) {
return $propiedad ;
}
$propiedad = model ( Propiedad :: class ) -> whereLike ( 'estacionamientos' , $this -> id . ';%' ) -> findOne ();
if ( $propiedad ) {
return $propiedad ;
}
$propiedad = model ( Propiedad :: class ) -> whereLike ( 'estacionamientos' , '%;' . $this -> id . ';%' ) -> findOne ();
if ( $propiedad ) {
return $propiedad ;
}
return null ;
}
if ( $this -> tipo () -> descripcion == 'bodega' ) {
// id | id; | ;id | ;id;
$propiedad = model ( Propiedad :: class ) -> whereLike ( 'bodegas' , $this -> id ) -> findOne ();
if ( $propiedad ) {
return $propiedad ;
}
$propiedad = model ( Propiedad :: class ) -> whereLike ( 'bodegas' , '%;' . $this -> id ) -> findOne ();
if ( $propiedad ) {
return $propiedad ;
}
$propiedad = model ( Propiedad :: class ) -> whereLike ( 'bodegas' , $this -> id . ';%' ) -> findOne ();
if ( $propiedad ) {
return $propiedad ;
}
$propiedad = model ( Propiedad :: class ) -> whereLike ( 'bodegas' , '%;' . $this -> id . ';%' ) -> findOne ();
if ( $propiedad ) {
return $propiedad ;
}
return null ;
}
}
public function tipo ()
{
return $this -> belongs_to ( TipoUnidad :: class , 'tipo' ) -> findOne ();
}
private $venta = null ;
public function venta ()
{
if ( $this -> venta == null ) {
$propiedad = $this -> propiedad ();
if ( $propiedad ) {
$venta = $propiedad -> propiedad () -> venta ();
if ( $venta ) {
$this -> venta = $venta ;
}
}
}
return $this -> venta ;
}
private $m2t = null ;
public function m2 ( $tipo = 'vendible' )
{
if ( $this -> m2t == null or ! isset ( $this -> m2t -> { $tipo })) {
if ( $this -> m2t == null ) {
$this -> m2t = [];
} else {
$this -> m2t = ( Array ) $this -> m2t ;
}
$this -> m2t [ $tipo ] = $this -> tipologia () -> m2 ( $tipo );
$this -> m2t = ( object ) $this -> m2t ;
}
return $this -> m2t -> { $tipo };
}
public function precios ()
{
return $this -> hasMany ( Precio :: class , 'unidad' ) -> findMany ();
}
public function precio ( $fecha = null )
{
if ( $fecha == null ) {
return \model ( Precio :: class )
-> select ( 'precio.*' )
-> rawJoin ( 'JOIN (SELECT e1.* FROM (SELECT precio, MAX(id) AS id FROM estado_precio GROUP BY precio) e0 JOIN estado_precio e1 ON e1.id = e0.id)' , [ 'ep.precio' , '=' , 'precio.id' ], 'ep' )
-> where ( 'unidad' , $this -> id )
-> where ( 'ep.estado' , 1 )
-> findOne ();
}
return \model ( Precio :: class )
-> select ( 'precio.*' )
-> join ( 'estado_precio' , [ 'ep.precio' , '=' , 'precio.id' ], 'ep' )
-> where ( 'precio.unidad' , $this -> id )
-> where ( 'ep.estado' , 1 )
-> whereLte ( 'ep.fecha' , $fecha -> format ( 'Y-m-d' ))
-> orderByDesc ( 'ep.fecha' )
-> orderByDesc ( 'ep.id' )
-> findOne ();
}
public function setPrecio ( $fecha , $valor )
{
$exists = false ;
// Dejar valores antiguos reemplazados (estado = 'reemplazado')
foreach ( $this -> precios () as $precio ) {
if ( ! $precio -> vigente ()) {
continue ;
}
// Ya existe precio a este valor
if ( $precio -> valor == $valor ) {
// La fecha es anterior
if ( $precio -> estado () -> fecha != $fecha -> format ( 'Y-m-d' )) {
$precio -> actualizar ( $fecha );
}
$exists = true ;
continue ;
}
$precio -> reemplazar ( $fecha );
}
if ( $exists ) {
return ;
}
// Agregar precio
$data = [
'unidad' => $this -> id ,
'valor' => $valor
];
$precio = model ( Precio :: class ) -> create ( $data );
$precio -> new ( $fecha );
}
public function tipologia ()
{
return $this -> belongsTo ( ProyectoTipoUnidad :: class , 'pt' ) -> findOne ();
}
protected $is_vendida ;
public function isVendida () {
if ( $this -> is_vendidad == null ) {
$this -> is_vendida = false ;
2021-03-25 23:33:37 -03:00
try {
$p = $this -> propiedad ();
if ( $p ) {
$v = $p -> venta ();
if ( $v ) {
$this -> is_vendida = true ;
}
2021-03-25 21:25:17 -03:00
}
2021-03-25 23:33:37 -03:00
} catch ( \Exception $e ) {}
2021-03-25 21:25:17 -03:00
}
return $this -> is_vendida ;
}
protected $cierres ;
public function cierres () {
if ( $this -> cierres == null ) {
$ucs = $this -> hasMany ( UnidadCierre :: class , 'unidad' ) -> findMany ();
if ( ! $ucs ) {
$this -> cierres = false ;
return $this -> cierres ;
}
$cierres = [];
foreach ( $ucs as $uc ) {
$c = $uc -> cierre ();
if ( $c ) {
$cierres [] = $c ;
}
}
usort ( $cierres , function ( $a , $b ) {
return $a -> fecha () -> diffInDays ( $b -> fecha ());
});
$this -> cierres = $cierres ;
}
return $this -> cierres ;
}
public function cierre () {
if ( $this -> cierres ()) {
return $this -> cierres [ count ( $this -> cierres ) - 1 ];
}
return false ;
}
protected $is_reservada ;
public function isReservada () {
if ( $this -> is_reservada == null ) {
$this -> is_reservada = false ;
if ( ! $this -> isVendida () and $this -> cierres () !== false ) {
$this -> is_reservada = true ;
}
}
return $this -> is_reservada ;
}
protected $linea ;
public function linea () {
if ( $this -> linea == null ) {
if ( $this -> tipo == 1 ) {
$this -> linea = ( int ) \substr ( $this -> descripcion , - 2 );
}
}
return $this -> linea ;
}
}
?>