old models
This commit is contained in:
22
src/old/Venta/BonoPie.php
Normal file
22
src/old/Venta/BonoPie.php
Normal file
@ -0,0 +1,22 @@
|
||||
<?php
|
||||
namespace Incoviba\old\Venta;
|
||||
|
||||
use Incoviba\Common\Alias\OldModel as Model;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Aldarien
|
||||
*
|
||||
* @property int id
|
||||
* @property float valor
|
||||
* @property Pago pago
|
||||
*
|
||||
*/
|
||||
class BonoPie extends Model
|
||||
{
|
||||
public function pago()
|
||||
{
|
||||
return $this->belongs_to(Pago::class, 'pago')->findOne();
|
||||
}
|
||||
}
|
||||
?>
|
335
src/old/Venta/Cierre.php
Normal file
335
src/old/Venta/Cierre.php
Normal file
@ -0,0 +1,335 @@
|
||||
<?php
|
||||
namespace Incoviba\old\Venta;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Incoviba\Common\Service\Factory;
|
||||
use Incoviba\Common\Alias\OldModel as Model;
|
||||
use Incoviba\old\Proyecto\Proyecto;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property Proyecto $proyecto
|
||||
* @property float $precio
|
||||
* @property \DateTime $fecha
|
||||
* @property int $relacionado
|
||||
* @property Propietario $propietario
|
||||
*/
|
||||
class Cierre extends Model
|
||||
{
|
||||
const VIGENTES = 1;
|
||||
const NO_VIGENTES = -1;
|
||||
public static function find(Proyecto $proyecto, Unidad $unidad, float $precio)
|
||||
{
|
||||
return model(Cierre::class)
|
||||
->select('cierre.*')
|
||||
->join('unidad_cierre', ['unidad_cierre.cierre', '=', 'cierre.id'])
|
||||
->where('cierre.proyecto', $proyecto->id)
|
||||
->where('unidad_cierre.unidad', $unidad->id)
|
||||
->where('cierre.precio', $precio);
|
||||
}
|
||||
public static function vigentes()
|
||||
{
|
||||
return model(Cierre::class)
|
||||
->select('cierre.*')
|
||||
->join('estado_cierre', ['estado_cierre.cierre', '=', 'cierre.id'])
|
||||
->join('tipo_estado_cierre', ['tipo_estado_cierre.id', '=', 'estado_cierre.tipo'])
|
||||
->join('proyecto', ['proyecto.id', '=', 'cierre.proyecto'])
|
||||
->where('tipo_estado_cierre.vigente', 1)
|
||||
->orderByAsc('proyecto.descripcion')
|
||||
->orderByAsc('cierre.fecha')
|
||||
->groupBy('cierre.id')
|
||||
->findMany();
|
||||
}
|
||||
public static function proyectos()
|
||||
{
|
||||
return model(Proyecto::class)
|
||||
->select('proyecto.*')
|
||||
->join('cierre', ['proyecto.id', '=', 'cierre.proyecto'])
|
||||
->join('estado_cierre', ['estado_cierre.cierre', '=', 'cierre.id'])
|
||||
->join('tipo_estado_cierre', ['tipo_estado_cierre.id', '=', 'estado_cierre.tipo'])
|
||||
->where('tipo_estado_cierre.vigente', 1)
|
||||
->orderByAsc('proyecto.descripcion')
|
||||
->orderByAsc('cierre.fecha')
|
||||
->groupBy('proyecto.id')
|
||||
->findMany();
|
||||
}
|
||||
|
||||
public function proyecto()
|
||||
{
|
||||
return $this->belongsTo(Proyecto::class, 'proyecto')->findOne();
|
||||
}
|
||||
public function unidades()
|
||||
{
|
||||
return $this->hasMany(UnidadCierre::class, 'cierre')->where('principal', 0)->findMany();
|
||||
}
|
||||
public function unidadPrincipal()
|
||||
{
|
||||
return $this->hasMany(UnidadCierre::class, 'cierre')->where('principal', 1)->findOne();
|
||||
}
|
||||
public function fecha(\DateTime $fecha = null)
|
||||
{
|
||||
if ($fecha == null) {
|
||||
return Carbon::parse($this->fecha, config('app.timezone'));
|
||||
}
|
||||
$this->fecha = $fecha->format('Y-m-d');
|
||||
}
|
||||
public function propietario()
|
||||
{
|
||||
$propietario = $this->belongsTo(Propietario::class, 'propietario');
|
||||
if ($propietario) {
|
||||
return $propietario->findOne();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
public function uf_m2()
|
||||
{
|
||||
return $this->neto() / $this->unidadPrincipal()->unidad()->m2();
|
||||
}
|
||||
public function neto()
|
||||
{
|
||||
$valor = $this->precio;
|
||||
foreach ($this->unidades() as $unidad) {
|
||||
$valor -= $unidad->unidad()->precio($this->fecha())->valor;
|
||||
}
|
||||
foreach ($this->valores() as $v) {
|
||||
if ($v->tipo()->descripcion == 'pie') {
|
||||
continue;
|
||||
}
|
||||
$valor -= $v->valor;
|
||||
}
|
||||
return $valor;
|
||||
}
|
||||
public function valores()
|
||||
{
|
||||
return $this->hasMany(ValorCierre::class, 'cierre')->findMany();
|
||||
}
|
||||
public function valor($tipo = 'pie')
|
||||
{
|
||||
return $this->hasMany(ValorCierre::class, 'cierre')
|
||||
->select('valor_cierre.*')
|
||||
->join('tipo_valor_cierre', ['tipo_valor_cierre.id', '=', 'valor_cierre.tipo'])
|
||||
->where('tipo_valor_cierre.descripcion', $tipo)
|
||||
->findOne();
|
||||
}
|
||||
public function estados()
|
||||
{
|
||||
return $this->hasMany(EstadoCierre::class, 'cierre')->findMany();
|
||||
}
|
||||
public function estado(\DateTime $fecha = null)
|
||||
{
|
||||
if ($fecha == null) {
|
||||
$estado = $this->hasMany(EstadoCierre::class, 'cierre')->orderByDesc('id')->findOne();
|
||||
if ($estado->tipo()->vigente == 1 and $this->oldest()) {
|
||||
if ($this->promesa() and $estado->tipo()->descripcion != 'promesado') {
|
||||
$tipo = model(TipoEstadoCierre::class)->where('descripcion', 'promesado')->findOne();
|
||||
$data = [
|
||||
'cierre' => $this->id,
|
||||
'tipo' => $tipo->id,
|
||||
'fecha' => $this->promesa()->fecha
|
||||
];
|
||||
$estado = model(EstadoCierre::class)->create($data);
|
||||
$estado->save();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$estado = $this->hasMany(EstadoCierre::class, 'cierre')->whereLte('fecha', $fecha->format('Y-m-d'))->orderByDesc('id')->findOne();
|
||||
}
|
||||
return $estado;
|
||||
}
|
||||
public function new(\DateTime $fecha)
|
||||
{
|
||||
$this->save();
|
||||
$tipo = model(TipoEstadoCierre::class)->where('descripcion', 'revisado')->findOne();
|
||||
$data = [
|
||||
'cierre' => $this->id,
|
||||
'tipo' => $tipo->id,
|
||||
'fecha' => $fecha->format('Y-m-d')
|
||||
];
|
||||
$estado = model(EstadoCierre::class)->create($data);
|
||||
$estado->save();
|
||||
|
||||
if ($this->other()) {
|
||||
$this->replace($fecha);
|
||||
}
|
||||
}
|
||||
protected function cierresUnidad() {
|
||||
$up = $this->unidadPrincipal();
|
||||
if (!$up) {
|
||||
return false;
|
||||
}
|
||||
$up = $up->id;
|
||||
$cierres = model(Cierre::class)
|
||||
->select('cierre.*')
|
||||
->join('unidad_cierre', ['unidad_cierre.cierre', '=', 'cierre.id'])
|
||||
->where('unidad_cierre.unidad', $up)
|
||||
->findMany();
|
||||
$id = $this->id;
|
||||
$cierres = array_filter($cierres, function($item) use ($id) {
|
||||
return ($id != $item->id);
|
||||
});
|
||||
return $cierres;
|
||||
}
|
||||
protected function other(): bool {
|
||||
$cierres = $this->cierresUnidad();
|
||||
if ($cierres and count($cierres) > 0) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
protected function oldest(): bool {
|
||||
if ($this->other()) {
|
||||
$last = $this->cierresUnidad()[count($this->cierresUnidad()) - 1];
|
||||
if ($last->fecha < $this->fecha) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
protected function replace(\DateTime $fecha) {
|
||||
$cierres = $this->cierresUnidad();
|
||||
$tipo = model(TipoEstadoCierre::class)->where('descripcion', 'abandonado')->findOne();
|
||||
array_walk($cierres, function($item) use ($tipo, $fecha) {
|
||||
$data = [
|
||||
'cierre' => $item->id,
|
||||
'tipo' => $tipo->id,
|
||||
'fecha' => $fecha->format('Y-m-d')
|
||||
];
|
||||
$estado = model(EstadoCierre::class)->create($data);
|
||||
$estado->save();
|
||||
});
|
||||
}
|
||||
public function addUnidad(array $data)
|
||||
{
|
||||
$data['cierre'] = $this->id;
|
||||
$unidad = model(UnidadCierre::class)->create($data);
|
||||
$unidad->save();
|
||||
}
|
||||
public function addValor(array $data)
|
||||
{
|
||||
$data['cierre'] = $this->id;
|
||||
$tipo = model(TipoValorCierre::class)->where('descripcion', $data['tipo'])->findOne();
|
||||
$data['tipo'] = $tipo->id;
|
||||
$valor = model(ValorCierre::class)->create($data);
|
||||
$valor->save();
|
||||
}
|
||||
public static function evaluar($precio_neto, $unidad, $fecha, $relacionado = false)
|
||||
{
|
||||
$precio_oferta = round($precio_neto, 2);
|
||||
$precio_lista = round($unidad->precio($fecha)->valor * (($relacionado) ? (1 - 0.06) : 1), 2);
|
||||
if ($precio_oferta >= $precio_lista) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
public function guardar(object $input)
|
||||
{
|
||||
$this->proyecto = $input->proyecto->id;
|
||||
$this->precio = $input->precio;
|
||||
$this->fecha = $input->fecha->format('Y-m-d');
|
||||
$this->relacionado = 0;
|
||||
if (isset($input->relacionado) and $input->relacionado) {
|
||||
$this->relacionado = 1;
|
||||
}
|
||||
if (isset($input->subrelacionado) and $input->subrelacionado) {
|
||||
$this->relacionado = 2;
|
||||
}
|
||||
$fecha = Carbon::today(config('app.timezone'));
|
||||
$this->new($fecha);
|
||||
|
||||
$data = [
|
||||
'unidad' => $input->departamento->id,
|
||||
'principal' => 1
|
||||
];
|
||||
$this->addUnidad($data);
|
||||
|
||||
foreach ($input->unidades as $unidad) {
|
||||
$data = ['unidad' => $unidad->id];
|
||||
$this->addUnidad($data);
|
||||
}
|
||||
|
||||
if (isset($input->pie)) {
|
||||
$data = [
|
||||
'tipo' => 'pie',
|
||||
'valor' => $input->pie
|
||||
];
|
||||
$this->addValor($data);
|
||||
}
|
||||
if (isset($input->bono)) {
|
||||
$data = [
|
||||
'tipo' => 'bono pie',
|
||||
'valor' => $input->bono
|
||||
];
|
||||
$this->addValor($data);
|
||||
}
|
||||
if (isset($input->promocion)) {
|
||||
$data = [
|
||||
'tipo' => 'premio',
|
||||
'valor' => $input->promocion
|
||||
];
|
||||
$this->addValor($data);
|
||||
}
|
||||
if (isset($input->operador)) {
|
||||
$data = [
|
||||
'tipo' => 'operador',
|
||||
'valor' => $input->operador * $this->precio / 100
|
||||
];
|
||||
$this->addValor($data);
|
||||
}
|
||||
}
|
||||
public function aprobar(\DateTime $fecha)
|
||||
{
|
||||
$tipo = model(TipoEstadoCierre::class)->where('descripcion', 'aprobado')->findOne();
|
||||
$data = [
|
||||
'cierre' => $this->id,
|
||||
'tipo' => $tipo->id
|
||||
];
|
||||
$estado = (new Factory(EstadoCierre::class))->where($data)->find();
|
||||
if (!$estado) {
|
||||
$data['fecha'] = $fecha->format('Y-m-d');
|
||||
$estado = model(EstadoCierre::class)->create($data);
|
||||
$estado->save();
|
||||
}
|
||||
}
|
||||
public function rechazar(\DateTime $fecha)
|
||||
{
|
||||
$tipo = model(TipoEstadoCierre::class)->where('descripcion', 'rechazado')->findOne();
|
||||
$data = [
|
||||
'cierre' => $this->id,
|
||||
'tipo' => $tipo->id
|
||||
];
|
||||
$estado = (new Factory(EstadoCierre::class))->where($data)->find();
|
||||
if (!$estado) {
|
||||
$data['fecha'] = $fecha->format('Y-m-d');
|
||||
$estado = model(EstadoCierre::class)->create($data);
|
||||
$estado->save();
|
||||
}
|
||||
}
|
||||
protected $promesa;
|
||||
public function promesa()
|
||||
{
|
||||
if ($this->promesa == null) {
|
||||
$propiedad = model(Propiedad::class)->where('unidad_principal', $this->unidadPrincipal()->unidad)->findOne();
|
||||
if (!$propiedad) {
|
||||
return false;
|
||||
}
|
||||
$this->promesa = model(Venta::class)->where('propiedad', $propiedad->id)->where('estado', 1)->findOne();
|
||||
if ($this->promesa != null and $this->propietario == 0) {
|
||||
$this->propietario = $this->promesa->propietario;
|
||||
$this->save();
|
||||
}
|
||||
}
|
||||
return $this->promesa;
|
||||
}
|
||||
public function isRelacionado() {
|
||||
return ($this->relacionado == 1) ? true : false;
|
||||
}
|
||||
public function isSubrelacionado() {
|
||||
return ($this->relacionado == 2) ? true : false;
|
||||
}
|
||||
public function periodo() {
|
||||
$today = Carbon::today(config('app.timezone'));
|
||||
$dif = $today->diffInDays($this->fecha());
|
||||
return $dif;
|
||||
}
|
||||
}
|
22
src/old/Venta/Comentario.php
Normal file
22
src/old/Venta/Comentario.php
Normal file
@ -0,0 +1,22 @@
|
||||
<?php
|
||||
namespace Incoviba\old\Venta;
|
||||
|
||||
use Incoviba\Common\Alias\OldModel as Model;
|
||||
|
||||
/**
|
||||
*
|
||||
* @property int id
|
||||
* @property int venta
|
||||
* @property datetime fecha
|
||||
* @property string texto
|
||||
* @property int estado
|
||||
*
|
||||
*/
|
||||
class Comentario extends Model
|
||||
{
|
||||
public function venta()
|
||||
{
|
||||
return $this->belongsTo(Venta::class, 'venta')->findOne();
|
||||
}
|
||||
}
|
||||
?>
|
29
src/old/Venta/Credito.php
Normal file
29
src/old/Venta/Credito.php
Normal file
@ -0,0 +1,29 @@
|
||||
<?php
|
||||
namespace Incoviba\old\Venta;
|
||||
|
||||
use Incoviba\Common\Alias\OldModel as Model;
|
||||
|
||||
/**
|
||||
*
|
||||
* @property int id
|
||||
* @property string banco
|
||||
* @property int valor
|
||||
* @property date fecha
|
||||
* @property float uf
|
||||
* @property int abonado
|
||||
* @property date fecha_abono
|
||||
* @property Pago pago
|
||||
*
|
||||
*/
|
||||
class Credito extends Model
|
||||
{
|
||||
public function pago()
|
||||
{
|
||||
return $this->belongs_to(Pago::class, 'pago')->findOne();
|
||||
}
|
||||
public function venta()
|
||||
{
|
||||
return $this->hasOne(Venta::class, 'credito')->findOne();
|
||||
}
|
||||
}
|
||||
?>
|
72
src/old/Venta/Cuota.php
Normal file
72
src/old/Venta/Cuota.php
Normal file
@ -0,0 +1,72 @@
|
||||
<?php
|
||||
namespace Incoviba\old\Venta;
|
||||
|
||||
use Incoviba\Common\Alias\OldModel as Model;
|
||||
|
||||
/**
|
||||
*
|
||||
* @property int id
|
||||
* @property int pie
|
||||
* @property date fecha
|
||||
* @property int valor_$
|
||||
* @property int estado
|
||||
* @property string banco
|
||||
* @property date fecha_pago
|
||||
* @property int abonado
|
||||
* @property date fecha_abono
|
||||
* @property float uf
|
||||
* @property Pago pago
|
||||
* @property int numero
|
||||
*
|
||||
*/
|
||||
class Cuota extends Model
|
||||
{
|
||||
public function pago()
|
||||
{
|
||||
return $this->belongs_to(Pago::class, 'pago')->findOne();
|
||||
}
|
||||
public function pie()
|
||||
{
|
||||
return $this->belongs_to(Pie::class, 'pie')->findOne();
|
||||
}
|
||||
public function uf()
|
||||
{
|
||||
if ($this->uf == 0) {
|
||||
$uf = $this->pago()->uf();
|
||||
if ($uf == 1) {
|
||||
$uf = $this->pie()->uf();
|
||||
}
|
||||
$this->uf = $uf;
|
||||
}
|
||||
return $this->uf;
|
||||
}
|
||||
public function valor($tipo = 'pesos')
|
||||
{
|
||||
$valor = $this->pago()->valor;
|
||||
if ($tipo == 'pesos') {
|
||||
return $valor;
|
||||
}
|
||||
$uf = $this->uf();
|
||||
if ($uf == 0) {
|
||||
return 0;
|
||||
}
|
||||
return $valor / $uf;
|
||||
}
|
||||
public function numero()
|
||||
{
|
||||
if ($this->numero == '') {
|
||||
$cuotas = $this->pie()->cuotas('fecha');
|
||||
$n = 0;
|
||||
foreach ($cuotas as $cuota) {
|
||||
$n ++;
|
||||
if ($cuota->id == $this->id) {
|
||||
$this->numero = $n;
|
||||
$this->save();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $this->numero;
|
||||
}
|
||||
}
|
||||
?>
|
21
src/old/Venta/Entrega.php
Normal file
21
src/old/Venta/Entrega.php
Normal file
@ -0,0 +1,21 @@
|
||||
<?php
|
||||
namespace Incoviba\old\Venta;
|
||||
|
||||
use Incoviba\Common\Alias\OldModel as Model;
|
||||
|
||||
/**
|
||||
*
|
||||
* @property int id
|
||||
* @property date fecha
|
||||
* @property int fondo_operacion
|
||||
* @property int fondo_reserva
|
||||
* @property date fecha_fondo_operacion
|
||||
* @property date fecha_fondo_reserva
|
||||
* @property int pago_operacion
|
||||
* @property int pago_reserva
|
||||
*
|
||||
*/
|
||||
class Entrega extends Model
|
||||
{
|
||||
}
|
||||
?>
|
24
src/old/Venta/Escritura.php
Normal file
24
src/old/Venta/Escritura.php
Normal file
@ -0,0 +1,24 @@
|
||||
<?php
|
||||
namespace Incoviba\old\Venta;
|
||||
|
||||
use Incoviba\Common\Alias\OldModel as Model;
|
||||
|
||||
/**
|
||||
*
|
||||
* @property int id
|
||||
* @property int valor
|
||||
* @property date fecha
|
||||
* @property float uf
|
||||
* @property int abonado
|
||||
* @property date fecha_abono
|
||||
* @property Pago pago
|
||||
*
|
||||
*/
|
||||
class Escritura extends Model
|
||||
{
|
||||
public function pago()
|
||||
{
|
||||
return $this->belongs_to(Pago::class, 'pago')->findOne();
|
||||
}
|
||||
}
|
||||
?>
|
30
src/old/Venta/EstadoCierre.php
Normal file
30
src/old/Venta/EstadoCierre.php
Normal file
@ -0,0 +1,30 @@
|
||||
<?php
|
||||
namespace Incoviba\old\Venta;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Incoviba\Common\Alias\OldModel as Model;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property Cierre $cierre
|
||||
* @property TipoEstadoCierre $tipo
|
||||
* @property \DateTime $fecha
|
||||
*/
|
||||
class EstadoCierre extends Model
|
||||
{
|
||||
public function cierre()
|
||||
{
|
||||
return $this->belongsTo(Cierre::class, 'cierre')->findOne();
|
||||
}
|
||||
public function tipo()
|
||||
{
|
||||
return $this->belongsTo(TipoEstadoCierre::class, 'tipo')->findOne();
|
||||
}
|
||||
public function fecha(\DateTime $fecha = null)
|
||||
{
|
||||
if ($fecha == null) {
|
||||
return Carbon::parse($this->fecha, config('app.timezone'));
|
||||
}
|
||||
$this->fecha = $fecha->format('Y-m-d');
|
||||
}
|
||||
}
|
27
src/old/Venta/EstadoPago.php
Normal file
27
src/old/Venta/EstadoPago.php
Normal file
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
namespace Incoviba\old\Venta;
|
||||
|
||||
use Incoviba\Common\Alias\OldModel as Model;
|
||||
|
||||
use Carbon\Carbon;
|
||||
|
||||
/**
|
||||
*
|
||||
* @property int id
|
||||
* @property int pago
|
||||
* @property date fecha
|
||||
* @property int estado
|
||||
*
|
||||
*/
|
||||
class EstadoPago extends Model
|
||||
{
|
||||
public function tipo()
|
||||
{
|
||||
return $this->belongs_to(TipoEstadoPago::class, 'estado')->findOne();
|
||||
}
|
||||
public function fecha()
|
||||
{
|
||||
return Carbon::parse($this->fecha, config('app.timezone'));
|
||||
}
|
||||
}
|
||||
?>
|
33
src/old/Venta/EstadoPrecio.php
Normal file
33
src/old/Venta/EstadoPrecio.php
Normal file
@ -0,0 +1,33 @@
|
||||
<?php
|
||||
namespace Incoviba\old\Venta;
|
||||
|
||||
use Incoviba\Common\Alias\OldModel as Model;
|
||||
use Carbon\Carbon;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property Precio $precio
|
||||
* @property \DateTime $fecha
|
||||
* @property TipoEstadoPrecio $estado
|
||||
*/
|
||||
class EstadoPrecio extends Model
|
||||
{
|
||||
public function precio()
|
||||
{
|
||||
return $this->belongsTo(Precio::class, 'precio')->findOne();
|
||||
}
|
||||
public function fecha($fecha = null)
|
||||
{
|
||||
if ($fecha == null) {
|
||||
return Carbon::parse($this->fecha, config('app.timezone'));
|
||||
}
|
||||
if (is_a($fecha, \DateTime::class)) {
|
||||
$fecha = $fecha->format('Y-m-d');
|
||||
}
|
||||
$this->fecha = $fecha;
|
||||
}
|
||||
public function estado()
|
||||
{
|
||||
return $this->belongsTo(TipoEstadoPrecio::class, 'estado')->findOne();
|
||||
}
|
||||
}
|
17
src/old/Venta/EstadoProblema.php
Normal file
17
src/old/Venta/EstadoProblema.php
Normal file
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
namespace Incoviba\old\Venta;
|
||||
|
||||
use Incoviba\Common\Alias\OldModel as Model;
|
||||
|
||||
/**
|
||||
*
|
||||
* @property int id
|
||||
* @property int problema
|
||||
* @property date fecha
|
||||
* @property int estado
|
||||
*
|
||||
*/
|
||||
class EstadoProblema extends Model
|
||||
{
|
||||
}
|
||||
?>
|
30
src/old/Venta/EstadoUnidadBloqueada.php
Normal file
30
src/old/Venta/EstadoUnidadBloqueada.php
Normal file
@ -0,0 +1,30 @@
|
||||
<?php
|
||||
namespace Incoviba\old\Venta;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Incoviba\Common\Alias\OldModel as Model;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property UnidadBloqueada $unidad
|
||||
* @property \DateTime $fecha
|
||||
* @property TipoEstadoUnidadBloqueada $tipo
|
||||
*/
|
||||
class EstadoUnidadBloqueada extends Model
|
||||
{
|
||||
public function unidad()
|
||||
{
|
||||
return $this->belongsTo(UnidadBloqueada::class, 'unidad')->findOne();
|
||||
}
|
||||
public function fecha(\DateTime $fecha = null)
|
||||
{
|
||||
if ($fecha == null) {
|
||||
return Carbon::parse($this->fecha, config('app.timezone'));
|
||||
}
|
||||
$this->fecha = $fecha->format('Y-m-d');
|
||||
}
|
||||
public function tipo()
|
||||
{
|
||||
return $this->belongsTo(TipoEstadoUnidadBloqueada::class, 'tipo')->findOne();
|
||||
}
|
||||
}
|
27
src/old/Venta/EstadoVenta.php
Normal file
27
src/old/Venta/EstadoVenta.php
Normal file
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
namespace Incoviba\old\Venta;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Incoviba\Common\Alias\OldModel as Model;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property Venta $venta
|
||||
* @property TipoEstadoVenta $estado
|
||||
* @property Carbon $fecha
|
||||
*/
|
||||
class EstadoVenta extends Model
|
||||
{
|
||||
public function venta()
|
||||
{
|
||||
return $this->belongsTo(Venta::class, 'venta')->findOne();
|
||||
}
|
||||
public function tipo()
|
||||
{
|
||||
return $this->belongsTo(TipoEstadoVenta::class, 'estado')->findOne();
|
||||
}
|
||||
public function fecha()
|
||||
{
|
||||
return Carbon::parse($this->fecha, config('app.timezone'));
|
||||
}
|
||||
}
|
156
src/old/Venta/Pago.php
Normal file
156
src/old/Venta/Pago.php
Normal file
@ -0,0 +1,156 @@
|
||||
<?php
|
||||
namespace Incoviba\old\Venta;
|
||||
|
||||
use Incoviba\Common\Alias\OldModel as Model;
|
||||
use Incoviba\old\Common\Banco;
|
||||
use Carbon\Carbon;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Aldarien
|
||||
*
|
||||
* @property int id
|
||||
* @property double valor
|
||||
* @property Banco banco
|
||||
* @property TipoPago tipo
|
||||
* @property string identificador
|
||||
* @property date fecha
|
||||
* @property float uf
|
||||
* @property string pagador
|
||||
*
|
||||
*/
|
||||
class Pago extends Model
|
||||
{
|
||||
public function banco()
|
||||
{
|
||||
$banco = $this->belongs_to(Banco::class, 'banco');
|
||||
if ($banco) {
|
||||
return $banco->findOne();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public function tipo()
|
||||
{
|
||||
return $this->belongs_to(TipoPago::class, 'tipo')->findOne();
|
||||
}
|
||||
public function estados($order = 'fecha', $direction = 'desc')
|
||||
{
|
||||
$estados = $this->has_many(EstadoPago::class, 'pago');
|
||||
if ($direction == 'desc') {
|
||||
$estados->orderByDesc($order);
|
||||
if ($order != 'id') {
|
||||
$estados->orderByDesc('id');
|
||||
}
|
||||
} else {
|
||||
$estados->orderByAsc($order);
|
||||
if ($order != 'id') {
|
||||
$estados->orderByAsc('id');
|
||||
}
|
||||
}
|
||||
return $estados->findMany();
|
||||
}
|
||||
public function estado()
|
||||
{
|
||||
$estado = $this->has_many(EstadoPago::class, 'pago')->order_by_desc('id')->findOne();
|
||||
return $estado;
|
||||
}
|
||||
public function fecha()
|
||||
{
|
||||
return Carbon::parse($this->fecha, config('app.timezone'));
|
||||
}
|
||||
public function uf()
|
||||
{
|
||||
if ($this->uf == 0) {
|
||||
$uf = uf($this->fecha);
|
||||
if (!$uf or $uf->total == 0) {
|
||||
return 1;
|
||||
}
|
||||
if ($uf->total > 0) {
|
||||
$this->uf = $uf->uf->value;
|
||||
$this->save();
|
||||
}
|
||||
}
|
||||
return $this->uf;
|
||||
}
|
||||
public function valor($tipo = 'pesos')
|
||||
{
|
||||
$valor = $this->valor;
|
||||
if ($tipo == 'ufs') {
|
||||
$uf = $this->uf();
|
||||
if ($uf == 1) {
|
||||
$uf = $this->fuente()[0]->obj->uf();
|
||||
}
|
||||
$valor /= $uf;
|
||||
if ($this->asociado() and $this->asociado == 0) {
|
||||
$valor += $this->asociado()->valor('ufs');
|
||||
}
|
||||
return $valor;
|
||||
}
|
||||
if ($this->asociado() and $this->asociado == 0) {
|
||||
$valor += $this->asociado()->valor();
|
||||
}
|
||||
return $valor;
|
||||
}
|
||||
public function new()
|
||||
{
|
||||
$this->save();
|
||||
|
||||
$estado = \Model::factory(EstadoPago::class)->create();
|
||||
$estado->pago = $this->id;
|
||||
$estado->fecha = $this->fecha;
|
||||
$estado->estado = 0;
|
||||
$estado->save();
|
||||
}
|
||||
public function newPagado()
|
||||
{
|
||||
$this->new();
|
||||
|
||||
$estado = \Model::factory(EstadoPago::class)->create();
|
||||
$estado->pago = $this->id;
|
||||
$estado->fecha = $this->fecha;
|
||||
$estado->estado = 1;
|
||||
$estado->save();
|
||||
}
|
||||
public function fuente()
|
||||
{
|
||||
$results = [];
|
||||
$cuota = model(Cuota::class)->where('pago', $this->id)->findOne();
|
||||
if ($cuota) {
|
||||
$results []= (object) ['tipo' => 'cuota', 'obj' => $cuota];
|
||||
}
|
||||
$credito = model(Credito::class)->where('pago', $this->id)->findOne();
|
||||
if ($credito) {
|
||||
$results []= (object) ['tipo' => 'credito', 'obj' => $credito];
|
||||
}
|
||||
$escritura = model(Escritura::class)->where('pago', $this->id)->findOne();
|
||||
if ($escritura) {
|
||||
$results []= (object) ['tipo' => 'escritura', 'obj' => $escritura];
|
||||
}
|
||||
$subsidio = model(Subsidio::class)->where('pago', $this->id)->findOne();
|
||||
if ($subsidio) {
|
||||
$results []= (object) ['tipo' => 'subsidio', 'obj' => $subsidio];
|
||||
}
|
||||
$bono = model(BonoPie::class)->where('pago', $this->id)->findOne();
|
||||
if ($bono) {
|
||||
$results []= (object) ['tipo' => 'bono_pie', 'obj' => $bono];
|
||||
}
|
||||
return $results;
|
||||
}
|
||||
public function asociado()
|
||||
{
|
||||
if ($this->asociado == 0) {
|
||||
return $this->hasOne(Pago::class, 'asociado')->findOne();
|
||||
}
|
||||
return $this->belongsTo(Pago::class, 'asociado')->findOne();
|
||||
}
|
||||
|
||||
public static function filterEstado(\ORM $orm)
|
||||
{
|
||||
return $orm->rawJoin(
|
||||
'JOIN (SELECT ep.* FROM (SELECT pago, MAX(id) AS id FROM estado_pago GROUP BY pago) e0 JOIN estado_pago ep ON ep.id = e0.id)',
|
||||
['estado_pago.pago', '=', 'pago.id'],
|
||||
'estado_pago'
|
||||
);
|
||||
}
|
||||
}
|
||||
?>
|
225
src/old/Venta/Pie.php
Normal file
225
src/old/Venta/Pie.php
Normal file
@ -0,0 +1,225 @@
|
||||
<?php
|
||||
namespace Incoviba\old\Venta;
|
||||
|
||||
use Incoviba\Common\Alias\OldModel as Model;
|
||||
use Carbon\Carbon;
|
||||
|
||||
/**
|
||||
*
|
||||
* @property int id
|
||||
* @property date fecha
|
||||
* @property float valor
|
||||
* @property float uf
|
||||
* @property int cuotas
|
||||
* @property int asociado
|
||||
* @property int reajuste
|
||||
*
|
||||
*/
|
||||
class Pie extends Model
|
||||
{
|
||||
public function cuotas($order = 'numero')
|
||||
{
|
||||
if ($this->asociado != 0) {
|
||||
return $this->asociado()->cuotas();
|
||||
}
|
||||
$cuotas = $this->hasMany(Cuota::class, 'pie')
|
||||
->select('cuota.*')
|
||||
->join('pago', ['pago.id', '=', 'cuota.pago'])
|
||||
->rawJoin('JOIN (SELECT e1.* FROM (SELECT pago, MAX(id) AS id FROM estado_pago GROUP BY pago) e0 JOIN estado_pago e1 ON e1.id = e0.id)', ['ep.pago', '=', 'pago.id'], 'ep')
|
||||
->join('tipo_estado_pago', ['tipo_estado_pago.id', '=', 'ep.estado'])
|
||||
->whereNotEqual('tipo_estado_pago.descripcion', 'reemplazado')
|
||||
->whereNotEqual('tipo_estado_pago.descripcion', 'anulado');
|
||||
switch ($order) {
|
||||
case 'numero':
|
||||
case 'number':
|
||||
default:
|
||||
$cuotas = $cuotas->orderByAsc('cuota.numero');
|
||||
case 'fecha':
|
||||
case 'date':
|
||||
$cuotas = $cuotas->orderByAsc('pago.fecha')
|
||||
->orderByDesc('pago.valor');
|
||||
break;
|
||||
}
|
||||
$cuotas = $cuotas->findMany();
|
||||
return $cuotas;
|
||||
}
|
||||
public function pagadas()
|
||||
{
|
||||
$estado = model(TipoEstadoPago::class)->where('descripcion', 'depositado')->findOne();
|
||||
$cuotas = $this->cuotas();
|
||||
foreach ($cuotas as $i => &$cuota) {
|
||||
if ($cuota->pago()->estado()->estado < $estado->id) {
|
||||
unset($cuotas[$i]);
|
||||
}
|
||||
}
|
||||
array_values($cuotas);
|
||||
return $cuotas;
|
||||
}
|
||||
public function abonadas()
|
||||
{
|
||||
$estado = model(TipoEstadoPago::class)->where('descripcion', 'abonado')->findOne();
|
||||
$cuotas = $this->pagadas();
|
||||
foreach ($cuotas as $i => &$cuota) {
|
||||
if ($cuota->pago()->estado()->estado != $estado->id) {
|
||||
unset($cuotas[$i]);
|
||||
}
|
||||
}
|
||||
array_values($cuotas);
|
||||
return $cuotas;
|
||||
}
|
||||
public function rebotadas()
|
||||
{
|
||||
$estado = model(TipoEstadoPago::class)->where('descripcion', 'devuelto')->findOne();
|
||||
$cuotas = $this->cuotas();
|
||||
foreach ($cuotas as $i => &$cuota) {
|
||||
if ($cuota->pago()->estado()->estado != $estado->id) {
|
||||
unset($cuotas[$i]);
|
||||
}
|
||||
}
|
||||
array_values($cuotas);
|
||||
return $cuotas;
|
||||
}
|
||||
public function valorPesos()
|
||||
{
|
||||
return $this->valor * $this->uf();
|
||||
}
|
||||
public function valorPagado($tipo = 'uf')
|
||||
{
|
||||
$cuotas = $this->pagadas();
|
||||
$sum = 0;
|
||||
foreach ($cuotas as $cuota) {
|
||||
$pago = $cuota->pago();
|
||||
if ($tipo == 'uf' or $tipo == 'ufs') {
|
||||
if ($pago->uf() == 0) {
|
||||
$sum += $pago->valor / $this->uf();
|
||||
continue;
|
||||
}
|
||||
$sum += $pago->valor / $pago->uf();
|
||||
} else {
|
||||
$sum += $pago->valor;
|
||||
}
|
||||
}
|
||||
|
||||
return $sum * $this->proporcion();
|
||||
}
|
||||
public function valorAbonado($tipo = 'uf')
|
||||
{
|
||||
$cuotas = $this->abonadas();
|
||||
$sum = 0;
|
||||
foreach ($cuotas as $cuota) {
|
||||
$pago = $cuota->pago();
|
||||
if ($tipo == 'uf' or $tipo == 'ufs') {
|
||||
if ($pago->uf() == 0) {
|
||||
$sum += $pago->valor / $this->uf();
|
||||
continue;
|
||||
}
|
||||
$sum += $pago->valor / $pago->uf;
|
||||
} else {
|
||||
$sum += $pago->valor;
|
||||
}
|
||||
}
|
||||
|
||||
return $sum * $this->proporcion();
|
||||
}
|
||||
public function reajuste()
|
||||
{
|
||||
$reajuste = $this->belongsTo(Pago::class, 'reajuste');
|
||||
if ($reajuste) {
|
||||
$reajuste = $reajuste->findOne();
|
||||
return $reajuste;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public function venta()
|
||||
{
|
||||
return $this->has_one(Venta::class, 'pie')->findOne();
|
||||
}
|
||||
public function asociado()
|
||||
{
|
||||
$pie = $this->belongs_to(Pie::class, 'asociado')->findOne();
|
||||
if ($pie) {
|
||||
return $pie;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
private $asociados = null;
|
||||
public function asociados()
|
||||
{
|
||||
if ($this->asociados == null) {
|
||||
$pie = $this->has_many(Pie::class, 'asociado');
|
||||
if (!$pie) {
|
||||
return null;
|
||||
}
|
||||
$asociados = $pie->findMany();
|
||||
usort($asociados, function($a, $b) {
|
||||
return strcmp($a->venta()->unidad()->descripcion, $b->venta()->unidad()->descripcion);
|
||||
});
|
||||
$this->asociados = $asociados;
|
||||
}
|
||||
return $this->asociados;
|
||||
}
|
||||
public function proporcion()
|
||||
{
|
||||
$pie = $this;
|
||||
if ($this->asociado != 0) {
|
||||
$pie = $this->asociado();
|
||||
}
|
||||
if ($pie->asociados() != null) {
|
||||
$pies = $pie->asociados();
|
||||
$base = $pie->valor;
|
||||
foreach ($pies as $p) {
|
||||
$base += $p->valor;
|
||||
}
|
||||
|
||||
return $this->valor / $base;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
public function pendientes()
|
||||
{
|
||||
return count($this->cuotas()) - count($this->pagadas());
|
||||
}
|
||||
public function pagos($estado = 0)
|
||||
{
|
||||
$pagos = model(Pago::class)
|
||||
->select('pago.*')
|
||||
->join('cuota', ['cuota.pago', '=', 'pago.id'])
|
||||
->filter('filterEstado')
|
||||
->where('estado_pago.estado', $estado)
|
||||
->where('cuota.pie', $this->id)
|
||||
->findMany();
|
||||
if ($this->reajuste != 0 and $this->reajuste()->estado()->estado == $estado) {
|
||||
$pagos []= $this->reajuste();
|
||||
}
|
||||
return $pagos;
|
||||
}
|
||||
public function uf()
|
||||
{
|
||||
if ($this->uf == 0) {
|
||||
$uf = uf($this->fecha);
|
||||
if (!$uf) {
|
||||
return 1;
|
||||
}
|
||||
if ($uf->total > 0) {
|
||||
$this->uf = $uf->uf->value;
|
||||
$this->save();
|
||||
}
|
||||
}
|
||||
return $this->uf;
|
||||
}
|
||||
public function valor($tipo = 'pesos')
|
||||
{
|
||||
switch ($tipo) {
|
||||
case 'uf':
|
||||
case 'ufs':
|
||||
return $this->valor;
|
||||
break;
|
||||
case 'peso':
|
||||
case 'pesos':
|
||||
default:
|
||||
return $this->valorPesos();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
?>
|
81
src/old/Venta/Precio.php
Normal file
81
src/old/Venta/Precio.php
Normal file
@ -0,0 +1,81 @@
|
||||
<?php
|
||||
namespace Incoviba\old\Venta;
|
||||
|
||||
use Incoviba\Common\Alias\OldModel as Model;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property Unidad $unidad
|
||||
* @property double $valor
|
||||
*/
|
||||
class Precio extends Model
|
||||
{
|
||||
public function unidad()
|
||||
{
|
||||
return $this->belongsTo(Unidad::class, 'unidad')->findOne();
|
||||
}
|
||||
public function estados()
|
||||
{
|
||||
return $this->hasMany(EstadoPrecio::class, 'precio')->findMany();
|
||||
}
|
||||
public function estado()
|
||||
{
|
||||
return \model(EstadoPrecio::class)
|
||||
->select('estado_precio.*')
|
||||
->rawJoin('JOIN (SELECT precio, MAX(id) AS id FROM estado_precio GROUP BY precio)', ['e0.id', '=', 'estado_precio.id'], 'e0')
|
||||
->where('estado_precio.precio', $this->id)
|
||||
->findOne();
|
||||
}
|
||||
public function inicio()
|
||||
{
|
||||
return \model(EstadoPrecio::class)
|
||||
->where('estado_precio.precio', $this->id)
|
||||
->orderByAsc('id')
|
||||
->findOne();
|
||||
}
|
||||
public function vigente()
|
||||
{
|
||||
if ($this->estado()->estado()->descripcion == 'vigente') {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
public function reemplazar($fecha)
|
||||
{
|
||||
if ($this->estado()->estado()->descripcion == 'reemplazado') {
|
||||
return;
|
||||
}
|
||||
$tipo = model(TipoEstadoPrecio::class)->where('descripcion', 'reemplazado')->findOne();
|
||||
$data = [
|
||||
'precio' => $this->id,
|
||||
'fecha' => $fecha->format('Y-m-d'),
|
||||
'estado' => $tipo->id
|
||||
];
|
||||
$estado = model(EstadoPrecio::class)->create($data);
|
||||
$estado->save();
|
||||
}
|
||||
public function actualizar($fecha)
|
||||
{
|
||||
$tipo = model(TipoEstadoPrecio::class)->where('descripcion', 'vigente')->findOne();
|
||||
$data = [
|
||||
'precio' => $this->id,
|
||||
'fecha' => $fecha->format('Y-m-d'),
|
||||
'estado' => $tipo->id
|
||||
];
|
||||
$estado = model(EstadoPrecio::class)->create($data);
|
||||
$estado->save();
|
||||
}
|
||||
public function new($fecha)
|
||||
{
|
||||
$this->save();
|
||||
|
||||
$tipo = model(TipoEstadoPrecio::class)->where('descripcion', 'vigente')->findOne();
|
||||
$data = [
|
||||
'precio' => $this->id,
|
||||
'fecha' => $fecha->format('Y-m-d'),
|
||||
'estado' => $tipo->id
|
||||
];
|
||||
$estado = model(EstadoPrecio::class)->create($data);
|
||||
$estado->save();
|
||||
}
|
||||
}
|
16
src/old/Venta/Problema.php
Normal file
16
src/old/Venta/Problema.php
Normal file
@ -0,0 +1,16 @@
|
||||
<?php
|
||||
namespace Incoviba\old\Venta;
|
||||
|
||||
use Incoviba\Common\Alias\OldModel as Model;
|
||||
|
||||
/**
|
||||
*
|
||||
* @property int id
|
||||
* @property int venta
|
||||
* @property string descripcion
|
||||
*
|
||||
*/
|
||||
class Problema extends Model
|
||||
{
|
||||
}
|
||||
?>
|
23
src/old/Venta/Promocion.php
Normal file
23
src/old/Venta/Promocion.php
Normal file
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
namespace Incoviba\old\Venta;
|
||||
|
||||
use Incoviba\Common\Alias\OldModel as Model;
|
||||
use Incoviba\old\Proyecto\Proyecto;
|
||||
|
||||
/**
|
||||
*
|
||||
* @property int id
|
||||
* @property Proyecto proyecto
|
||||
* @property string descripcion
|
||||
* @property string titulo
|
||||
* @property date fecha_inicio
|
||||
*
|
||||
*/
|
||||
class Promocion extends Model
|
||||
{
|
||||
public function proyecto()
|
||||
{
|
||||
return $this->belongsTo(Proyecto::class, 'proyecto')->findOne();
|
||||
}
|
||||
}
|
||||
?>
|
24
src/old/Venta/PromocionVenta.php
Normal file
24
src/old/Venta/PromocionVenta.php
Normal file
@ -0,0 +1,24 @@
|
||||
<?php
|
||||
namespace Incoviba\old\Venta;
|
||||
|
||||
use Incoviba\Common\Alias\OldModel as Model;
|
||||
|
||||
/**
|
||||
*
|
||||
* @property Promocion promocion
|
||||
* @property Venta venta
|
||||
* @property double valor
|
||||
*
|
||||
*/
|
||||
class PromocionVenta extends Model
|
||||
{
|
||||
public function promocion()
|
||||
{
|
||||
return $this->belongsTo(Promocion::class, 'promocion')->findOne();
|
||||
}
|
||||
public function venta()
|
||||
{
|
||||
return $this->belongsTo(Venta::class, 'venta')->findOne();
|
||||
}
|
||||
}
|
||||
?>
|
117
src/old/Venta/Propiedad.php
Normal file
117
src/old/Venta/Propiedad.php
Normal file
@ -0,0 +1,117 @@
|
||||
<?php
|
||||
namespace Incoviba\old\Venta;
|
||||
|
||||
use Incoviba\Common\Alias\OldModel as Model;
|
||||
use Incoviba\nuevo\Venta\Propiedad as P;
|
||||
use Incoviba\nuevo\Venta\UnidadPropiedad;
|
||||
|
||||
/**
|
||||
*
|
||||
* @property int id
|
||||
* @property Unidad unidad_principal
|
||||
* @property string estacionamientos
|
||||
* @property string bodegas
|
||||
* @property int estado
|
||||
*
|
||||
*/
|
||||
class Propiedad extends Model
|
||||
{
|
||||
private $estacionamientos_arr = null;
|
||||
private $bodegas_arr = null;
|
||||
|
||||
public function unidad()
|
||||
{
|
||||
return $this->belongs_to(Unidad::class, 'unidad_principal')->findOne();
|
||||
}
|
||||
protected $unidades;
|
||||
public function unidades() {
|
||||
if ($this->unidades == null) {
|
||||
$this->unidades = $this->hasMany(PropiedadUnidad::class, 'propiedad')->findMany();
|
||||
}
|
||||
return $this->unidades;
|
||||
}
|
||||
protected $departamentos;
|
||||
public function departamentos() {
|
||||
if ($this->departamentos == null) {
|
||||
$this->departamentos = array_map(function($item) {
|
||||
return $item->unidad();
|
||||
}, array_filter($this->unidades(), function($item) {
|
||||
return ($item->unidad()->tipo()->descripcion == 'departamento');
|
||||
}));
|
||||
}
|
||||
return $this->departamentos;
|
||||
}
|
||||
public function estacionamientos($mod = null)
|
||||
{
|
||||
if ($this->estacionamientos_arr == null) {
|
||||
if (($unidades = $this->unidades()) !== false) {
|
||||
$estacionamientos = array_filter($unidades, function($item) {
|
||||
return ($item->unidad()->tipo()->descripcion == 'estacionamiento');
|
||||
});
|
||||
$this->estacionamientos_arr = array_map(function($item) {
|
||||
return $item->unidad();
|
||||
}, $estacionamientos);
|
||||
} else {
|
||||
$ests = explode(';', $this->estacionamientos);
|
||||
$estacionamientos = [];
|
||||
foreach ($ests as $e) {
|
||||
$estacionamiento = \Model::factory(Unidad::class)->findOne($e);
|
||||
if ($estacionamiento) {
|
||||
$estacionamientos []= $estacionamiento;
|
||||
}
|
||||
}
|
||||
$this->estacionamientos_arr = $estacionamientos;
|
||||
}
|
||||
}
|
||||
if ($mod != null) {
|
||||
switch ($mod) {
|
||||
case 'array':
|
||||
$result = [];
|
||||
foreach ($this->estacionamientos_arr as $est) {
|
||||
$result []= $est->descripcion;
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
return $this->estacionamientos_arr;
|
||||
}
|
||||
public function bodegas($mod = null)
|
||||
{
|
||||
if ($this->bodegas_arr == null) {
|
||||
if (($unidades = $this->unidades()) !== false) {
|
||||
$bodegas = array_filter($unidades, function($item) {
|
||||
return ($item->unidad()->tipo()->descripcion == 'bodega');
|
||||
});
|
||||
$this->bodegas_arr = array_map(function($item) {
|
||||
return $item->unidad();
|
||||
}, $bodegas);
|
||||
} else {
|
||||
$bods = explode(';', $this->bodegas);
|
||||
$bodegas = [];
|
||||
foreach ($bods as $b) {
|
||||
$bodega = \Model::factory(Unidad::class)->findOne($b);
|
||||
if ($bodega) {
|
||||
$bodegas []= $bodega;
|
||||
}
|
||||
}
|
||||
$this->bodegas_arr = $bodegas;
|
||||
}
|
||||
}
|
||||
if ($mod != null) {
|
||||
switch ($mod) {
|
||||
case 'array':
|
||||
$result = [];
|
||||
foreach ($this->bodegas_arr as $bod) {
|
||||
$result []= $bod->descripcion;
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
return $this->bodegas_arr;
|
||||
}
|
||||
public function venta()
|
||||
{
|
||||
return $this->has_one(Venta::class, 'propiedad')->findOne();
|
||||
}
|
||||
}
|
||||
?>
|
25
src/old/Venta/PropiedadUnidad.php
Normal file
25
src/old/Venta/PropiedadUnidad.php
Normal file
@ -0,0 +1,25 @@
|
||||
<?php
|
||||
namespace Incoviba\old\Venta;
|
||||
|
||||
use Incoviba\Common\Alias\OldModel as Model;
|
||||
|
||||
class PropiedadUnidad extends Model {
|
||||
protected $propiedad_model;
|
||||
public function propiedad() {
|
||||
if ($this->propiedad_model == null) {
|
||||
$this->propiedad_model = $this->belongsTo(Propiedad::class, 'propiedad')->findOne();
|
||||
}
|
||||
return $this->propiedad_model;
|
||||
}
|
||||
protected $unidad_model;
|
||||
public function unidad() {
|
||||
if ($this->unidad_model == null) {
|
||||
$this->unidad_model = $this->belongsTo(Unidad::class, 'unidad')->findOne();
|
||||
}
|
||||
return $this->unidad_model;
|
||||
}
|
||||
protected $is_principal;
|
||||
public function isPrincipal() {
|
||||
return ($this->principal == 0) ? false : true;
|
||||
}
|
||||
}
|
66
src/old/Venta/Propietario.php
Normal file
66
src/old/Venta/Propietario.php
Normal file
@ -0,0 +1,66 @@
|
||||
<?php
|
||||
namespace Incoviba\old\Venta;
|
||||
|
||||
use Incoviba\Common\Alias\OldModel as Model;
|
||||
use Incoviba\old\Common\Direccion;
|
||||
use Incoviba\nuevo\Venta\Propietario as P;
|
||||
|
||||
/**
|
||||
*
|
||||
* @property int $rut
|
||||
* @property char $dv
|
||||
* @property string $nombres
|
||||
* @property string $apellido_paterno
|
||||
* @property string $apellido_materno
|
||||
* @property string $sexo
|
||||
* @property string $estado_civil
|
||||
* @property string $profesion
|
||||
* @property Direccion $direccion
|
||||
* @property int $telefono
|
||||
* @property string $email
|
||||
* @property int $representante
|
||||
* @property int $otro
|
||||
*
|
||||
*/
|
||||
class Propietario extends Model
|
||||
{
|
||||
public static $_id_column = 'rut';
|
||||
|
||||
public function direccion()
|
||||
{
|
||||
return $this->belongs_to(Direccion::class, 'direccion')->findOne();
|
||||
}
|
||||
public function nombreCompleto($ap = false)
|
||||
{
|
||||
$str = '';
|
||||
if ($ap) {
|
||||
$str = $this->apellido_paterno . ' ' . $this->apellido_materno . ', ' . $this->nombres;
|
||||
} else {
|
||||
$str = $this->nombres . ' ' . $this->apellido_paterno . ' ' . $this->apellido_materno;
|
||||
}
|
||||
return $str;
|
||||
}
|
||||
public function representante()
|
||||
{
|
||||
$r = $this->belongsTo(Propietario::class, 'representante', 'rut');
|
||||
if ($r) {
|
||||
return $r->findOne();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public function ventas() {
|
||||
return $this->hasMany(Venta::class, 'propietario', 'rut')
|
||||
->select('venta.*')
|
||||
->join('propiedad', ['propiedad.id', '=', 'venta.propiedad'])
|
||||
->join('unidad', ['unidad.id', '=', 'propiedad.unidad_principal'])
|
||||
->join('proyecto', ['proyecto.id', '=', 'unidad.proyecto'])
|
||||
->orderByAsc('proyecto.descripcion')
|
||||
->orderByExpr("LPAD(unidad.descripcion, 4, '0')")
|
||||
->findMany();
|
||||
}
|
||||
public function rut()
|
||||
{
|
||||
return format('rut', $this->rut) . '-' . $this->dv;
|
||||
}
|
||||
}
|
||||
?>
|
44
src/old/Venta/Subsidio.php
Normal file
44
src/old/Venta/Subsidio.php
Normal file
@ -0,0 +1,44 @@
|
||||
<?php
|
||||
namespace Incoviba\old\Venta;
|
||||
|
||||
use Incoviba\Common\Alias\OldModel as Model;
|
||||
|
||||
/**
|
||||
*
|
||||
* @property int id
|
||||
* @property Pago pago
|
||||
* @property null|Pago subsidio
|
||||
*
|
||||
*/
|
||||
class Subsidio extends Model
|
||||
{
|
||||
public function pago()
|
||||
{
|
||||
return $this->belongs_to(Pago::class, 'pago')->findOne();
|
||||
}
|
||||
public function subsidio()
|
||||
{
|
||||
$pago = $this->belongs_to(Pago::class, 'subsidio');
|
||||
if ($pago) {
|
||||
return $pago->findOne();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public function total($tipo = 'pesos')
|
||||
{
|
||||
$sum = 0;
|
||||
if ($tipo == 'pesos') {
|
||||
$sum = $this->pago()->valor;
|
||||
if ($this->subsidio != 0) {
|
||||
$sum += $this->subsidio()->valor;
|
||||
}
|
||||
} else {
|
||||
$sum = $this->pago()->valor('ufs');
|
||||
if ($this->subsidio != 0) {
|
||||
$sum += $this->subsidio()->valor('ufs');
|
||||
}
|
||||
}
|
||||
return $sum;
|
||||
}
|
||||
}
|
||||
?>
|
13
src/old/Venta/TipoEstadoCierre.php
Normal file
13
src/old/Venta/TipoEstadoCierre.php
Normal file
@ -0,0 +1,13 @@
|
||||
<?php
|
||||
namespace Incoviba\old\Venta;
|
||||
|
||||
use Incoviba\Common\Alias\OldModel as Model;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property string $descripcion
|
||||
* @property int $vigente
|
||||
*/
|
||||
class TipoEstadoCierre extends Model
|
||||
{
|
||||
}
|
17
src/old/Venta/TipoEstadoPago.php
Normal file
17
src/old/Venta/TipoEstadoPago.php
Normal file
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
namespace Incoviba\old\Venta;
|
||||
|
||||
use Incoviba\Common\Alias\OldModel as Model;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Aldarien
|
||||
*
|
||||
* @property int id
|
||||
* @property string descripcion
|
||||
*
|
||||
*/
|
||||
class TipoEstadoPago extends Model
|
||||
{
|
||||
}
|
||||
?>
|
12
src/old/Venta/TipoEstadoPrecio.php
Normal file
12
src/old/Venta/TipoEstadoPrecio.php
Normal file
@ -0,0 +1,12 @@
|
||||
<?php
|
||||
namespace Incoviba\old\Venta;
|
||||
|
||||
use Incoviba\Common\Alias\OldModel as Model;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property string $descripcion
|
||||
*/
|
||||
class TipoEstadoPrecio extends Model
|
||||
{
|
||||
}
|
19
src/old/Venta/TipoEstadoUnidadBloqueada.php
Normal file
19
src/old/Venta/TipoEstadoUnidadBloqueada.php
Normal file
@ -0,0 +1,19 @@
|
||||
<?php
|
||||
namespace Incoviba\old\Venta;
|
||||
|
||||
use Incoviba\Common\Alias\OldModel as Model;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property string $descripcion
|
||||
* @property int $activo
|
||||
*/
|
||||
class TipoEstadoUnidadBloqueada extends Model
|
||||
{
|
||||
public function activo() {
|
||||
if ($this->activo == 1) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
21
src/old/Venta/TipoEstadoVenta.php
Normal file
21
src/old/Venta/TipoEstadoVenta.php
Normal file
@ -0,0 +1,21 @@
|
||||
<?php
|
||||
namespace Incoviba\old\Venta;
|
||||
|
||||
use Incoviba\Common\Alias\OldModel as Model;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property string $descripcion
|
||||
* @property int $activa
|
||||
*/
|
||||
class TipoEstadoVenta extends Model
|
||||
{
|
||||
public function estados()
|
||||
{
|
||||
return $this->hasMany(EstadoVenta::class, 'estado')->findMany();
|
||||
}
|
||||
public function activa()
|
||||
{
|
||||
return ($this->activa == 1) ? true : false;
|
||||
}
|
||||
}
|
17
src/old/Venta/TipoPago.php
Normal file
17
src/old/Venta/TipoPago.php
Normal file
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
namespace Incoviba\old\Venta;
|
||||
|
||||
use Incoviba\Common\Alias\OldModel as Model;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Aldarien
|
||||
*
|
||||
* @property int id
|
||||
* @property string descripcion
|
||||
*
|
||||
*/
|
||||
class TipoPago extends Model
|
||||
{
|
||||
}
|
||||
?>
|
17
src/old/Venta/TipoUnidad.php
Normal file
17
src/old/Venta/TipoUnidad.php
Normal file
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
namespace Incoviba\old\Venta;
|
||||
|
||||
use Incoviba\Common\Alias\OldModel as Model;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Aldarien
|
||||
*
|
||||
* @property int id
|
||||
* @property string descripcion
|
||||
*
|
||||
*/
|
||||
class TipoUnidad extends Model
|
||||
{
|
||||
}
|
||||
?>
|
12
src/old/Venta/TipoValorCierre.php
Normal file
12
src/old/Venta/TipoValorCierre.php
Normal file
@ -0,0 +1,12 @@
|
||||
<?php
|
||||
namespace Incoviba\old\Venta;
|
||||
|
||||
use Incoviba\Common\Alias\OldModel as Model;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property string $descripcion
|
||||
*/
|
||||
class TipoValorCierre extends Model
|
||||
{
|
||||
}
|
242
src/old/Venta/Unidad.php
Normal file
242
src/old/Venta/Unidad.php
Normal file
@ -0,0 +1,242 @@
|
||||
<?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;
|
||||
$p = $this->propiedad();
|
||||
if ($p) {
|
||||
$v = $p->venta();
|
||||
if ($v) {
|
||||
$this->is_vendida = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
?>
|
42
src/old/Venta/UnidadBloqueada.php
Normal file
42
src/old/Venta/UnidadBloqueada.php
Normal file
@ -0,0 +1,42 @@
|
||||
<?php
|
||||
namespace Incoviba\old\Venta;
|
||||
|
||||
use Incoviba\Common\Alias\OldModel as Model;
|
||||
use Incoviba\old\Proyecto\ProyectoAgente;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property Agente $agente
|
||||
* @property Unidad $unidad
|
||||
*/
|
||||
class UnidadBloqueada extends Model
|
||||
{
|
||||
public function agente()
|
||||
{
|
||||
return $this->belongsTo(ProyectoAgente::class, 'agente')->findOne();
|
||||
}
|
||||
public function unidad()
|
||||
{
|
||||
return $this->belongsTo(Unidad::class, 'unidad')->findOne();
|
||||
}
|
||||
public function estados()
|
||||
{
|
||||
return $this->hasMany(EstadoUnidadBloqueada::class, 'unidad')->orderByAsc('fecha')->findMany();
|
||||
}
|
||||
public function estado()
|
||||
{
|
||||
return $this->hasMany(EstadoUnidadBloqueada::class, 'unidad')->orderByDesc('fecha')->findOne();
|
||||
}
|
||||
public function new(\DateTime $fecha)
|
||||
{
|
||||
parent::save();
|
||||
$tipo = model(TipoEstadoUnidadBloqueada::class)->where('descripcion', 'bloqueada')->findOne();
|
||||
$data = [
|
||||
'unidad' => $this->id,
|
||||
'fecha' => $fecha->format('Y-m-d'),
|
||||
'tipo' => $tipo->id
|
||||
];
|
||||
$estado = model(EstadoUnidadBloqueada::class)->create($data);
|
||||
$estado->save();
|
||||
}
|
||||
}
|
22
src/old/Venta/UnidadCierre.php
Normal file
22
src/old/Venta/UnidadCierre.php
Normal file
@ -0,0 +1,22 @@
|
||||
<?php
|
||||
namespace Incoviba\old\Venta;
|
||||
|
||||
use Incoviba\Common\Alias\OldModel as Model;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property Cierre $cierre
|
||||
* @property Unidad $unidad
|
||||
* @property int $principal
|
||||
*/
|
||||
class UnidadCierre extends Model
|
||||
{
|
||||
public function cierre()
|
||||
{
|
||||
return $this->belongsTo(Cierre::class, 'cierre')->findOne();
|
||||
}
|
||||
public function unidad()
|
||||
{
|
||||
return $this->belongsTo(Unidad::class, 'unidad')->findOne();
|
||||
}
|
||||
}
|
22
src/old/Venta/ValorCierre.php
Normal file
22
src/old/Venta/ValorCierre.php
Normal file
@ -0,0 +1,22 @@
|
||||
<?php
|
||||
namespace Incoviba\old\Venta;
|
||||
|
||||
use Incoviba\Common\Alias\OldModel as Model;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property Cierre $cierre
|
||||
* @property TipoValorCierre $tipo
|
||||
* @property float $valor
|
||||
*/
|
||||
class ValorCierre extends Model
|
||||
{
|
||||
public function cierre()
|
||||
{
|
||||
return $this->belongsTo(Cierre::class, 'cierre')->findOne();
|
||||
}
|
||||
public function tipo()
|
||||
{
|
||||
return $this->belongsTo(TipoValorCierre::class, 'tipo')->findOne();
|
||||
}
|
||||
}
|
515
src/old/Venta/Venta.php
Normal file
515
src/old/Venta/Venta.php
Normal file
@ -0,0 +1,515 @@
|
||||
<?php
|
||||
namespace Incoviba\old\Venta;
|
||||
|
||||
use Incoviba\Common\Alias\OldModel as Model;
|
||||
use Carbon\Carbon;
|
||||
//use Incoviba\old\Proyecto\Agente;
|
||||
use Incoviba\old\Proyecto\ProyectoAgente;
|
||||
|
||||
/**
|
||||
*
|
||||
* @property int id
|
||||
* @property Propietario propietario
|
||||
* @property Propiedad propiedad
|
||||
* @property Pie pie
|
||||
* @property BonoPie bono_pie
|
||||
* @property Credito credito
|
||||
* @property Escritura escritura
|
||||
* @property Subsidio subsidio
|
||||
* @property date escriturado
|
||||
* @property Entrega entrega
|
||||
* @property date entregado
|
||||
* @property date fecha
|
||||
* @property float valor_uf
|
||||
* @property int estado
|
||||
* @property date fecha_ingreso
|
||||
* @property int avalchile
|
||||
* @property ProyectoAgente agente
|
||||
* @property float uf
|
||||
* @property boolean relacionado
|
||||
* @property Promocion promocion
|
||||
* @property Pago resciliacion
|
||||
*
|
||||
*/
|
||||
class Venta extends Model
|
||||
{
|
||||
protected $saldo;
|
||||
protected $uf;
|
||||
protected $anticipo;
|
||||
protected $pagado;
|
||||
protected $comision;
|
||||
protected $valor_neto;
|
||||
protected $promociones;
|
||||
protected $valor_corredora;
|
||||
|
||||
public function propietario()
|
||||
{
|
||||
return $this->belongs_to(Propietario::class, 'propietario', 'rut')->findOne();
|
||||
}
|
||||
public function propiedad()
|
||||
{
|
||||
return $this->belongs_to(Propiedad::class, 'propiedad')->findOne();
|
||||
}
|
||||
public function bonoPie()
|
||||
{
|
||||
$bono = $this->belongs_to(BonoPie::class, 'bono_pie');
|
||||
if ($bono) {
|
||||
return $bono->findOne();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public function pie()
|
||||
{
|
||||
$pie = $this->belongs_to(Pie::class, 'pie');
|
||||
if ($pie) {
|
||||
return $pie->findOne();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public function entrega()
|
||||
{
|
||||
if ($this->entrega != '0') {
|
||||
return $this->belongs_to(Entrega::class, 'entrega')->findOne();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public function fecha()
|
||||
{
|
||||
return Carbon::parse($this->fecha, new \DateTimeZone(config('app.timezone')));
|
||||
}
|
||||
public function proyecto()
|
||||
{
|
||||
return $this->propiedad()->unidad()->proyecto();
|
||||
}
|
||||
public function unidad()
|
||||
{
|
||||
return $this->propiedad()->unidad();
|
||||
}
|
||||
public function agente()
|
||||
{
|
||||
$agente = $this->belongs_to(ProyectoAgente::class, 'agente');
|
||||
if ($agente) {
|
||||
return $agente->findOne();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public function comision()
|
||||
{
|
||||
if (!isset($this->comision)) {
|
||||
$pa = $this->agente();
|
||||
if ($pa and $pa->agente()->tipo == 19) {
|
||||
$this->comision = $pa->comision / 100;
|
||||
} else {
|
||||
$this->comision = 0;
|
||||
}
|
||||
}
|
||||
return $this->comision;
|
||||
}
|
||||
public function valorComision()
|
||||
{
|
||||
$bono_pie = $this->bonoPie();
|
||||
if ($this->bono_pie != 0) {
|
||||
$bono_pie = $bono_pie->pago()->valor('ufs');
|
||||
} else {
|
||||
$bono_pie = 0;
|
||||
}
|
||||
return ($this->valor_uf - $bono_pie) * $this->comision();
|
||||
}
|
||||
protected $superficie_departamentos;
|
||||
public function superficie() {
|
||||
if ($this->superficie_departamentos == null) {
|
||||
$this->superficie_departamentos = array_reduce($this->propiedad()->departamentos(), function($sum, $item) {
|
||||
return $sum + $item->m2();
|
||||
});
|
||||
}
|
||||
return $this->superficie_departamentos;
|
||||
}
|
||||
protected $valor_departamentos;
|
||||
public function valorDepartamentos() {
|
||||
if ($this->valor_departamentos == null) {
|
||||
$fecha = $this->fecha();
|
||||
$this->valor_departamentos = array_reduce($this->propiedad()->departamentos(), function($sum, $item) use ($fecha) {
|
||||
return $sum + $item->precio($fecha)->valor;
|
||||
});
|
||||
}
|
||||
return $this->valor_departamentos;
|
||||
}
|
||||
protected $valor_unidades;
|
||||
public function valorUnidades() {
|
||||
if ($this->valor_unidades == null) {
|
||||
$fecha = $this->fecha();
|
||||
$this->valor_unidades = array_reduce($this->propiedad()->unidades(), function($sum, $item) use ($fecha) {
|
||||
return $sum + $item->unidad()->precio($fecha)->valor;
|
||||
});
|
||||
}
|
||||
return $this->valor_unidades;
|
||||
}
|
||||
public function valorEstacionamientos()
|
||||
{
|
||||
$estacionamientos = $this->propiedad()->estacionamientos();
|
||||
$sum = 0;
|
||||
foreach ($estacionamientos as $estacionamiento) {
|
||||
$sum += $estacionamiento->precio($this->fecha())->valor;
|
||||
}
|
||||
return $sum;
|
||||
}
|
||||
public function valorBodegas()
|
||||
{
|
||||
$bodegas = $this->propiedad()->bodegas();
|
||||
$sum = 0;
|
||||
foreach ($bodegas as $bodega) {
|
||||
$sum += $bodega->precio($this->fecha())->valor;
|
||||
}
|
||||
return $sum;
|
||||
}
|
||||
public function valorEstacionamientosYBodegas()
|
||||
{
|
||||
return $this->valorEstacionamientos() + $this->valorBodegas();
|
||||
}
|
||||
public function valorCorredora()
|
||||
{
|
||||
if ($this->valor_corredora == null) {
|
||||
$bono_pie = $this->bonoPie();
|
||||
if ($this->bono_pie != 0) {
|
||||
$bono_pie = $bono_pie->pago()->valor('ufs');
|
||||
} else {
|
||||
$bono_pie = 0;
|
||||
}
|
||||
$promos = 0;
|
||||
$ps = $this->promociones();
|
||||
if (count($ps) > 0) {
|
||||
foreach ($ps as $promo) {
|
||||
$promos += $promo->valor;
|
||||
}
|
||||
}
|
||||
$this->valor_corredora = $this->valor_uf - $bono_pie - $promos;
|
||||
}
|
||||
return $this->valor_corredora;
|
||||
}
|
||||
public function valorFinal()
|
||||
{
|
||||
if (!isset($this->valor_neto)) {
|
||||
$comision = $this->valorComision();
|
||||
$bono_pie = $this->bonoPie();
|
||||
if ($this->bono_pie != 0) {
|
||||
$bono_pie = $bono_pie->pago()->valor('ufs');
|
||||
} else {
|
||||
$bono_pie = 0;
|
||||
}
|
||||
$ests = $this->valorEstacionamientos();
|
||||
$bods = $this->valorBodegas();
|
||||
$promos = 0;
|
||||
$ps = $this->promociones();
|
||||
if (count($ps) > 0) {
|
||||
foreach ($ps as $promo) {
|
||||
$promos += $promo->valor;
|
||||
}
|
||||
}
|
||||
$this->valor_neto = $this->valor_uf - $bono_pie - $comision - $ests - $bods - $promos;
|
||||
}
|
||||
return $this->valor_neto;
|
||||
}
|
||||
public function uf_m2()
|
||||
{
|
||||
$m2 = array_reduce($this->propiedad()->departamentos(), function($sum, $item) {
|
||||
return $sum + $item->m2();
|
||||
});
|
||||
return $this->valorFinal() / $m2;
|
||||
}
|
||||
public function escritura()
|
||||
{
|
||||
$escritura = $this->belongs_to(Escritura::class, 'escritura');
|
||||
if ($escritura) {
|
||||
return $escritura->findOne();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public function credito()
|
||||
{
|
||||
$credito = $this->belongs_to(Credito::class, 'credito');
|
||||
if ($credito) {
|
||||
return $credito->findOne();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public function comentarios()
|
||||
{
|
||||
return $this->has_many(Comentario::class, 'venta')->findMany();
|
||||
}
|
||||
public function subsidio()
|
||||
{
|
||||
$subsidio = $this->belongs_to(Subsidio::class, 'subsidio');
|
||||
if ($subsidio) {
|
||||
return $subsidio->findOne();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public function resciliacion()
|
||||
{
|
||||
$res = $this->belongs_to(Pago::class, 'resciliacion');
|
||||
if ($res) {
|
||||
return $res->findOne();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public function postventas()
|
||||
{
|
||||
$postventas = $this->has_many(\Incoviba\nuevo\Venta\Postventa::class, 'venta_id');
|
||||
if ($postventas) {
|
||||
return $postventas->findMany();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public function promociones()
|
||||
{
|
||||
if ($this->promociones == null) {
|
||||
$pvs = model(PromocionVenta::class)->where('venta', $this->id)->findMany();
|
||||
$this->promociones = $pvs;
|
||||
}
|
||||
return $this->promociones;
|
||||
}
|
||||
public function devolucion()
|
||||
{
|
||||
$devolucion = $this->belongsTo(Pago::class, 'devolucion');
|
||||
if ($devolucion) {
|
||||
return $devolucion->findOne();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public function anticipo($tipo = 'ufs')
|
||||
{
|
||||
if (!isset($this->anticipo[$tipo])) {
|
||||
$anticipo = 0;
|
||||
if ($this->pie != 0) {
|
||||
if ($tipo == 'ufs') {
|
||||
$anticipo += $this->pieReajustado($tipo);
|
||||
} else {
|
||||
$anticipo += $this->pie()->valorPagado($tipo);
|
||||
}
|
||||
if ($this->pie()->reajuste != 0) {
|
||||
$anticipo += $this->pie()->reajuste()->valor($tipo);
|
||||
}
|
||||
}
|
||||
if ($this->escritura != 0) {
|
||||
$anticipo += $this->escritura()->pago()->valor($tipo);
|
||||
}
|
||||
if ($this->saldo('ufs') > 0) {
|
||||
$anticipo -= $this->saldo($tipo);
|
||||
}
|
||||
$this->anticipo[$tipo] = $anticipo;
|
||||
}
|
||||
|
||||
return $this->anticipo[$tipo];
|
||||
}
|
||||
public function saldo($tipo = 'ufs')
|
||||
{
|
||||
if (!isset($this->saldo[$tipo])) {
|
||||
if ($tipo == 'pesos') {
|
||||
$this->saldo[$tipo] = $this->saldo() * $this->uf();
|
||||
return $this->saldo[$tipo];
|
||||
}
|
||||
$saldo = $this->valor($tipo);
|
||||
if ($this->bono_pie != 0) {
|
||||
$saldo -= $this->bonoPie()->pago()->valor($tipo);
|
||||
}
|
||||
if ($this->pie != 0) {
|
||||
$saldo -= $this->pie()->valorPagado($tipo);
|
||||
if ($this->pie()->reajuste != 0) {
|
||||
$saldo -= $this->pie()->reajuste()->valor($tipo);
|
||||
}
|
||||
}
|
||||
if ($this->escritura != 0) {
|
||||
$saldo -= $this->escritura()->pago()->valor($tipo);
|
||||
}
|
||||
if ($this->subsidio != 0) {
|
||||
$saldo -= $this->subsidio()->total($tipo);
|
||||
}
|
||||
if ($this->credito != 0) {
|
||||
$saldo -= $this->credito()->pago()->valor($tipo);
|
||||
}
|
||||
if ($this->devolucion) {
|
||||
$saldo += $this->devolucion()->valor($tipo);
|
||||
}
|
||||
if ($this->resciliacion) {
|
||||
$saldo += $this->resciliacion()->valor($tipo);
|
||||
}
|
||||
$this->saldo[$tipo] = -$saldo;
|
||||
}
|
||||
|
||||
return $this->saldo[$tipo];
|
||||
}
|
||||
public function valor($tipo = 'ufs')
|
||||
{
|
||||
if ($tipo == 'ufs') {
|
||||
return $this->valor_uf;
|
||||
}
|
||||
return $this->valor_uf * $this->uf();
|
||||
}
|
||||
protected $valores;
|
||||
public function valorPagado($tipo = 'ufs')
|
||||
{
|
||||
if ($this->valores == null or !isset($this->valores->pagado->ufs)) {
|
||||
$valores = [];
|
||||
if (isset($this->valores)) {
|
||||
$valores = (array) $valores;
|
||||
}
|
||||
$valores['pagado'] = (object) ['ufs' => 0, 'pesos' => 0];
|
||||
if ($this->pie()) {
|
||||
$valores['pagado']->ufs = $this->pie()->valorPagado();
|
||||
$valores['pagado']->pesos = $this->pie()->valorPagado('pesos');
|
||||
}
|
||||
$this->valores = (object) $valores;
|
||||
}
|
||||
return $this->valores->pagado->{$tipo};
|
||||
}
|
||||
public function valorAbonado($tipo = 'ufs')
|
||||
{
|
||||
if ($this->valores == null or !isset($this->valores->abonado->{$tipo})) {
|
||||
$valores = [];
|
||||
if (isset($this->valores)) {
|
||||
$valores = (array) $valores;
|
||||
}
|
||||
$valores['abonado'] = (object) ['ufs' => 0, 'pesos' => 0];
|
||||
if ($this->pie()) {
|
||||
$valores['abonado']->ufs = $this->pie()->valorAbonado();
|
||||
$valores['abonado']->pesos = $this->pie()->valorAbonado('pesos');
|
||||
}
|
||||
$this->valores = (object) $valores;
|
||||
}
|
||||
return $this->valores->abonado->{$tipo};
|
||||
}
|
||||
public function pagado($tipo = 'ufs')
|
||||
{
|
||||
if (!isset($this->pagado[$tipo])) {
|
||||
if (abs($this->saldo()) / $this->valor() > 0.01 or $tipo == 'pesos') {
|
||||
$total = 0;
|
||||
$total += $this->anticipo($tipo);
|
||||
|
||||
if ($this->subsidio != 0) {
|
||||
$total += $this->subsidio()->total($tipo);
|
||||
}
|
||||
if ($this->credito != 0) {
|
||||
$total += $this->credito()->pago()->valor($tipo);
|
||||
}
|
||||
if ($this->devolucion) {
|
||||
$total -= $this->devolucion()->valor($tipo);
|
||||
}
|
||||
if ($this->resciliacion) {
|
||||
$total -= $this->resciliacion()->valor($tipo);
|
||||
}
|
||||
|
||||
$this->pagado[$tipo] = $total;
|
||||
} else {
|
||||
$this->pagado[$tipo] = $this->valor($tipo);
|
||||
}
|
||||
}
|
||||
return $this->pagado[$tipo];
|
||||
}
|
||||
public function pieReajustado($tipo = 'ufs')
|
||||
{
|
||||
if (abs($this->saldo()) / $this->valor() > 0.01) {
|
||||
return $this->pie()->valorPagado($tipo);
|
||||
}
|
||||
$valor = $this->pie()->valorPagado($tipo) - $this->saldo($tipo);
|
||||
return $valor;
|
||||
}
|
||||
public function uf()
|
||||
{
|
||||
if ($this->uf == null) {
|
||||
$f = $this->fecha();
|
||||
$uf = uf($f);
|
||||
if ($uf == null) {
|
||||
return 1;
|
||||
}
|
||||
$this->uf = $uf->uf->value;
|
||||
}
|
||||
|
||||
return $this->uf;
|
||||
}
|
||||
public function pagos($estado = 0)
|
||||
{
|
||||
$results = [];
|
||||
if ($this->pie != 0) {
|
||||
$results = array_merge($results, $this->pie()->pagos($estado));
|
||||
}
|
||||
if ($this->escritura != 0 and $this->escritura()->pago() and $this->escritura()->pago()->estado()->estado == $estado) {
|
||||
$results []= $this->escritura()->pago();
|
||||
}
|
||||
if ($this->credito != 0 and $this->credito()->pago()->estado()->estado == $estado) {
|
||||
$results []= $this->credito()->pago();
|
||||
}
|
||||
if ($this->subsidio != 0 and $this->subsidio()->pago()->estado()->estado == $estado) {
|
||||
$results []= $this->subsidio()->pago();
|
||||
}
|
||||
|
||||
usort($results, function($a, $b) {
|
||||
return $a->estado()->fecha()->diffInDays($b->estado()->fecha(), false);
|
||||
});
|
||||
return $results;
|
||||
}
|
||||
public function new()
|
||||
{
|
||||
parent::save();
|
||||
$tipo = model(TipoEstadoVenta::class)->where('descripcion', 'vigente')->findOne();
|
||||
$data = [
|
||||
'venta' => $this->id,
|
||||
'estado' => $tipo->id,
|
||||
'fecha' => $this->fecha
|
||||
];
|
||||
$estado = model(EstadoVenta::class)->create($data);
|
||||
$estado->save();
|
||||
}
|
||||
public function estados()
|
||||
{
|
||||
return $this->hasMany(EstadoVenta::class, 'venta')->findMany();
|
||||
}
|
||||
public function estado($estado = null)
|
||||
{
|
||||
if ($estado == null) {
|
||||
return model(EstadoVenta::class)
|
||||
->select('estado_venta.*')
|
||||
->rawJoin('JOIN (SELECT venta, MAX(id) AS id FROM estado_venta GROUP BY venta)', ['estado_venta.id', '=', 'e0.id'], 'e0')
|
||||
->where('estado_venta.venta', $this->id)
|
||||
->findOne();
|
||||
}
|
||||
return model(EstadoVenta::class)
|
||||
->select('estado_venta.*')
|
||||
->join('tipo_estado_venta', ['tipo_estado_venta.id', '=', 'estado_venta.estado'])
|
||||
->where('estado_venta.venta', $this->id)
|
||||
->where('tipo_estado_venta.descripcion', $estado)
|
||||
->orderByDesc('estado_venta.fecha')
|
||||
->findOne();
|
||||
}
|
||||
public function firmar(Carbon $fecha)
|
||||
{
|
||||
$estado = $this->estado();
|
||||
if ($estado->tipo()->descripcion == 'firmado por inmobiliaria') {
|
||||
return true;
|
||||
}
|
||||
$tipo = model(TipoEstadoVenta::class)->where('descripcion', 'firmado por inmobiliaria')->findOne();
|
||||
$data = [
|
||||
'venta' => $this->id,
|
||||
'estado' => $tipo->id,
|
||||
'fecha' => $fecha->format('Y-m-d')
|
||||
];
|
||||
$estado = model(EstadoVenta::class)->create($data)->save();
|
||||
return true;
|
||||
}
|
||||
public function archivar(Carbon $fecha)
|
||||
{
|
||||
$estado = $this->estado();
|
||||
if ($estado->estado()->tipo()->descripcion == 'archivado') {
|
||||
return true;
|
||||
}
|
||||
$tipo = model(TipoEstadoVenta::class)->where('descripcion', 'archivado')->findOne();
|
||||
$data = [
|
||||
'venta' => $this->id,
|
||||
'estado' => $tipo->id,
|
||||
'fecha' => $fecha->format('Y-m-d')
|
||||
];
|
||||
$estado = model(EstadoVenta::class)->create($data)->save();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
?>
|
Reference in New Issue
Block a user