Files
modelos/src/old/Proyecto/ProyectoTipoUnidad.php
2019-12-23 18:01:23 -03:00

161 lines
4.6 KiB
PHP

<?php
namespace Incoviba\old\Proyecto;
use Incoviba\Common\Alias\OldModel as Model;
use Incoviba\old\Venta\TipoUnidad;
use Incoviba\old\Venta\Unidad;
use Incoviba\old\Venta\Venta;
/**
*
* @property int id
* @property int proyecto
* @property string nombre
* @property int tipo
* @property string abreviacion
* @property float m2
* @property float logia
* @property float terraza
* @property string descripcion
*
*/
class ProyectoTipoUnidad extends Model
{
protected $lineas;
public function proyecto()
{
return $this->belongsTo(Proyecto::class, 'proyecto')->findOne();
}
public function unidades()
{
return $this->hasMany(Unidad::class, 'pt')->orderByExpr('LPAD(subtipo, 3, "0")')->orderByExpr('LPAD(descripcion, 4, "0")')->findMany();
}
public function tipo()
{
return $this->belongsTo(TipoUnidad::class, 'tipo')->findOne();
}
public function m2($tipo = 'vendible')
{
return $this->m2 + $this->logia + $this->terraza / (($tipo == 'vendible') ? 2 : 1);
}
public function lineas()
{
if ($this->lineas == null) {
$lineas = [];
foreach ($this->unidades() as $unidad) {
if (array_search($unidad->subtipo, $lineas) === false) {
$lineas []= $unidad->subtipo;
}
}
sort($lineas);
$this->lineas = implode(', ', $lineas);
}
return $this->lineas;
}
public function precio($fecha = null)
{
$sum = 0;
$cnt = 0;
foreach ($this->unidades() as $unidad) {
if ($unidad->precio($fecha)) {
$sum += $unidad->precio($fecha)->valor;
$cnt ++;
}
}
if ($cnt == 0) {
return 0;
}
return $sum / $cnt;
}
protected $precios;
public function precioSubtipo($subtipo, $fecha = null)
{
if (!isset($this->precios[$subtipo])) {
$sum = 0;
$cnt = 0;
foreach ($this->unidades() as $unidad) {
if ($unidad->subtipo == $subtipo and $unidad->precio($fecha)) {
$sum += $unidad->precio($fecha)->valor;
$cnt ++;
}
}
if ($this->precios == null) {
$this->precios = [];
}
$prom = 0;
if ($cnt > 0) {
$prom = $sum / $cnt;
}
$this->precios[$subtipo] = $prom;
}
return $this->precios[$subtipo];
}
public function setPrecios($fecha, $valor)
{
foreach ($this->unidades() as $unidad) {
$unidad->setPrecio($fecha, $valor);
}
}
public function setPreciosSubtipo($subtipo, $fecha, $valor)
{
foreach ($this->unidades() as $unidad) {
if ($unidad->subtipo == $subtipo) {
$unidad->setPrecio($fecha, $valor);
}
}
}
protected $tipologia;
public function tipologia()
{
if ($this->tipologia == null) {
$tipologias = $this->hasMany(TipoTipologia::class, 'tipo')->findMany();
if (!$tipologias) {
$this->tipologia = false;
return false;
}
usort($tipologias, function($a, $b) {
return $a->elemento()->orden - $b->elemento()->orden;
});
$tipologia = ['tipologia' => $tipologias[0]->tipologia(), 'detalles' => $tipologias];
$resumen = [];
foreach ($tipologias as $t) {
if (strpos($t->elemento()->descripcion, 'cocina ') !== false) {
$resumen []= $t->elemento()->abreviacion;
continue;
}
$resumen []= $t->cantidad . '' . $t->elemento()->abreviacion;
}
$tipologia['descripcion'] = implode('/', $resumen);
$this->tipologia = (object) $tipologia;
}
return $this->tipologia;
}
protected $ventas;
public function ventas($order = 'departamento')
{
if ($this->ventas == null) {
$ventas = model(Venta::class)
->select('venta.*')
->join('propiedad', ['propiedad.id', '=', 'venta.propiedad'])
->join('unidad', ['unidad.id', '=', 'propiedad.unidad_principal'])
->join('proyecto_tipo_unidad', ['proyecto_tipo_unidad.id', '=', 'unidad.pt'])
->rawJoin('JOIN (SELECT e1.* FROM estado_venta e1 JOIN (SELECT venta, MAX(id) AS id FROM estado_venta GROUP BY venta) e0 ON e0.id = e1.id)', ['ev.venta', '=', 'venta.id'], 'ev')
->join('tipo_estado_venta', ['tipo_estado_venta.id', '=', 'ev.estado'])
->where('tipo_estado_venta.activa', 1)
->where('proyecto_tipo_unidad.id', $this->id);
switch (strtolower($order)) {
case 'fecha':
$ventas = $ventas->orderByAsc('venta.fecha');
case 'departamento':
default:
$ventas = $ventas->orderByExpr('LPAD(unidad.descripcion, 4, "0")');
break;
}
$ventas = $ventas->findMany();
$this->ventas = $ventas;
}
return $this->ventas;
}
}