2023-07-24 20:55:26 -04:00
< ? php
namespace Incoviba\Repository ;
2023-11-30 18:40:15 -03:00
use PDO ;
use PDOException ;
2023-11-22 19:08:19 -03:00
use DateTimeImmutable ;
2023-07-24 20:55:26 -04:00
use Incoviba\Common\Define ;
use Incoviba\Common\Ideal ;
2023-08-08 23:53:49 -04:00
use Incoviba\Common\Implement ;
2023-07-24 20:55:26 -04:00
use Incoviba\Model ;
class Proyecto extends Ideal\Repository
{
2023-10-13 10:45:21 -03:00
public function __construct (
Define\Connection $connection ,
protected Inmobiliaria $inmobiliariaRepository ,
2023-10-19 18:20:37 -03:00
protected Direccion $direccionRepository ,
protected Proyecto\Etapa $etapaRepository
2023-10-13 10:45:21 -03:00
)
2023-07-24 20:55:26 -04:00
{
parent :: __construct ( $connection );
$this -> setTable ( 'proyecto' );
}
public function create ( ? array $data = null ) : Define\Model
{
2023-08-08 23:53:49 -04:00
$map = ( new Implement\Repository\MapperParser ([ 'descripcion' , 'corredor' , 'pisos' , 'subterraneos' ]))
-> register ( 'inmobiliaria' , ( new Implement\Repository\Mapper ())
-> setFactory (( new Implement\Repository\Factory ())
-> setCallable ([ $this -> inmobiliariaRepository , 'fetchById' ])
-> setArgs ([ $data [ 'inmobiliaria' ]])))
-> register ( 'direccion' , ( new Implement\Repository\Mapper ())
-> setFactory (( new Implement\Repository\Factory ())
2023-09-07 23:03:21 -03:00
-> setCallable ([ $this -> direccionRepository , 'fetchById' ])
-> setArgs ([ $data [ 'direccion' ]])))
2023-08-08 23:53:49 -04:00
-> register ( 'superficie_terreno' , ( new Implement\Repository\Mapper ())
-> setProperty ( 'terreno' )
-> setFunction ( function ( $data ) {
2023-07-24 20:55:26 -04:00
$terreno = new Model\Proyecto\Terreno ();
$terreno -> superficie = $data [ 'superficie_terreno' ];
2023-12-20 10:50:49 -03:00
$terreno -> valorUnitario = $data [ 'valor_unitario_terreno' ] ? ? 0 ;
2023-11-30 19:06:59 -03:00
$terreno -> valor = 0 ;
2023-11-30 18:40:15 -03:00
$terreno -> fecha = null ;
2023-11-22 19:08:19 -03:00
if ( isset ( $data [ 'fecha_terreno' ]) and $data [ 'fecha_terreno' ] !== '' ) {
2023-11-30 19:06:59 -03:00
$terreno -> valor = $data [ 'valor_terreno' ];
2023-11-30 18:40:15 -03:00
$terreno -> fecha = new DateTimeImmutable ( $data [ 'fecha_terreno' ]);
2023-11-22 19:08:19 -03:00
}
2023-07-24 20:55:26 -04:00
return $terreno ;
2023-08-08 23:53:49 -04:00
}))
-> register ( 'superficie_sobre_nivel' , ( new Implement\Repository\Mapper ())
-> setProperty ( 'superficie' )
-> setFunction ( function ( $data ) {
2023-07-24 20:55:26 -04:00
$superficie = new Model\Proyecto\Superficie ();
$superficie -> sobre_nivel = $data [ 'superficie_sobre_nivel' ];
$superficie -> bajo_nivel = $data [ 'superficie_bajo_nivel' ];
return $superficie ;
2023-08-08 23:53:49 -04:00
}));
2023-07-24 20:55:26 -04:00
return $this -> parseData ( new Model\Proyecto (), $data , $map );
}
public function save ( Define\Model $model ) : Define\Model
{
$model -> id = $this -> saveNew (
[ 'inmobiliaria' , 'descripcion' , 'direccion' , 'superficie_terreno' , 'valor_terreno' , 'corredor' ,
'superficie_sobre_nivel' , 'superficie_bajo_nivel' , 'pisos' , 'subterraneos' ],
2023-08-08 23:53:49 -04:00
[ $model -> inmobiliaria () -> rut , $model -> descripcion , $model -> direccion () -> id , $model -> terreno -> superficie ,
2023-11-30 19:06:59 -03:00
$model -> terreno -> valorUnitario , $model -> corredor , $model -> superficie -> sobre_nivel ,
2023-07-24 20:55:26 -04:00
$model -> superficie -> bajo_nivel , $model -> pisos , $model -> subterraneos ]
);
return $model ;
}
public function edit ( Define\Model $model , array $new_data ) : Define\Model
{
return $this -> update ( $model , [ 'inmobiliaria' , 'descripcion' , 'direccion' , 'superficie_terreno' ,
'valor_terreno' , 'corredor' , 'superficie_sobre_nivel' , 'superficie_bajo_nivel' , 'pisos' ,
'subterraneos' ], $new_data );
}
2023-11-29 20:09:08 -03:00
2023-11-30 18:40:15 -03:00
public function fetchById ( int $id ) : Model\Proyecto
2023-11-29 20:09:08 -03:00
{
$query = $this -> connection -> getQueryBuilder ()
-> select ( $this -> columns ())
-> from ( " { $this -> getTable () } a " )
-> joined ( $this -> joinTerreno ())
-> where ( " a.id = ? " );
return $this -> fetchOne ( $query , [ $id ]);
}
2023-11-30 18:40:15 -03:00
public function fetchByName ( string $name ) : Model\Proyecto
2023-07-24 20:55:26 -04:00
{
2023-11-22 19:08:19 -03:00
$query = $this -> connection -> getQueryBuilder ()
-> select ( $this -> columns ())
-> from ( " { $this -> getTable () } a " )
-> joined ( $this -> joinTerreno ())
2023-12-20 12:02:52 -03:00
-> where ( " a.descripcion LIKE ? " );
return $this -> fetchOne ( $query , [ " % { $name } % " ]);
2023-07-24 20:55:26 -04:00
}
public function fetchAllActive () : array
{
2023-10-19 18:20:37 -03:00
$etapaProyecto = $this -> etapaRepository -> fetchByDescripcion ( 'Proyecto' );
$etapaTerminado = $this -> etapaRepository -> fetchByDescripcion ( 'Terminado' );
2023-11-22 19:08:19 -03:00
$query = $this -> connection -> getQueryBuilder ()
-> select ( $this -> columns ())
-> from ( " { $this -> getTable () } a " )
-> joined ( $this -> joinTerreno ())
-> joined ( $this -> joinEstado ())
-> where ( " et.orden BETWEEN { $etapaProyecto -> orden } AND ( { $etapaTerminado -> orden } - 1) " )
-> order ( 'a.descripcion' );
2023-07-24 20:55:26 -04:00
return $this -> fetchMany ( $query );
}
2023-10-19 18:20:37 -03:00
public function fetchAllEscriturando () : array
{
$etapaRecepcion = $this -> etapaRepository -> fetchByDescripcion ( 'Recepción' );
$etapaTerminado = $this -> etapaRepository -> fetchByDescripcion ( 'Terminado' );
2023-11-22 19:08:19 -03:00
$query = $this -> connection -> getQueryBuilder ()
-> select ( $this -> columns ())
-> from ( " { $this -> getTable () } a " )
-> joined ( $this -> joinTerreno ())
-> joined ( $this -> joinEstado ())
-> where ( " et.orden BETWEEN { $etapaRecepcion -> orden } AND ( { $etapaTerminado -> orden } - 1) " )
-> order ( 'a.descripcion' );
2023-10-19 18:20:37 -03:00
return $this -> fetchMany ( $query );
}
2023-11-30 18:40:15 -03:00
public function editTerreno ( Model\Proyecto $proyecto , array $data ) : Model\Proyecto
{
$fecha = new DateTimeImmutable ( $data [ 'fecha' ]);
try {
$query = $this -> connection -> getQueryBuilder ()
-> select ( 'valor' )
-> from ( 'proyecto_terreno' )
-> where ( 'proyecto_id = ? AND fecha = ?' );
$result = $this -> connection -> execute ( $query , [ $proyecto -> id , $fecha -> format ( 'Y-m-d' )]) -> fetch ( PDO :: FETCH_ASSOC );
if ( $result === false ) {
throw new Implement\Exception\EmptyResult ( $query );
}
if ( $result [ 'valor' ] !== $data [ 'valor' ]) {
$query = $this -> connection -> getQueryBuilder ()
-> update ( 'proyecto_terreno' )
-> set ( 'valor = ?' )
-> where ( 'proyecto_id = ? AND fecha = ?' );
$this -> connection -> execute ( $query , [ $data [ 'valor' ], $proyecto -> id , $fecha -> format ( 'Y-m-d' )]);
$proyecto -> terreno -> valor = $data [ 'valor' ];
}
} catch ( PDOException | Implement\Exception\EmptyResult ) {
$query = $this -> connection -> getQueryBuilder ()
-> insert ()
-> into ( 'proyecto_terreno' )
-> columns ([ 'proyecto_id' , 'fecha' , 'valor' , 'tipo_moneda_id' ])
-> values ([ '?' , '?' , '?' , '1' ]);
try {
$this -> connection -> execute ( $query , [ $proyecto -> id , $fecha -> format ( 'Y-m-d' ), $data [ 'valor' ]]);
$proyecto -> terreno -> fecha = $fecha ;
$proyecto -> terreno -> valor = $data [ 'valor' ];
} catch ( PDOException $exception ) {
throw new Implement\Exception\EmptyResult ( $query );
}
}
return $proyecto ;
}
2024-01-17 11:13:27 -03:00
public function fetchByInmobiliaria ( int $inmobiliaria_rut ) : array
{
$query = $this -> connection -> getQueryBuilder ()
-> select ()
-> from ( $this -> getTable ())
-> where ( 'inmobiliaria = ?' );
return $this -> fetchMany ( $query , [ $inmobiliaria_rut ]);
}
2023-11-22 19:08:19 -03:00
/* public function fetchSuperficieVendido ( int $proyecto_id ) : float
2023-10-20 19:03:29 -03:00
{
2023-11-22 19:08:19 -03:00
} */
2023-10-19 18:20:37 -03:00
2023-11-22 19:08:19 -03:00
protected function columns () : string
{
2023-11-30 19:06:59 -03:00
return " a.id, a.inmobiliaria, a.descripcion, a.direccion, a.superficie_terreno, valor_terreno AS valor_unitario_terreno,
COALESCE ( pt . valor , '' ) AS valor_terreno , COALESCE ( pt . fecha , '' ) AS fecha_terreno , a . corredor ,
2023-11-22 19:08:19 -03:00
a . superficie_sobre_nivel , a . superficie_bajo_nivel , a . pisos , a . subterraneos " ;
}
protected function joinTerreno () : string
{
2023-11-30 19:06:59 -03:00
return " LEFT OUTER JOIN (SELECT pt1.* FROM proyecto_terreno pt1 JOIN (SELECT MAX(id) AS id, proyecto_id FROM proyecto_terreno GROUP BY proyecto_id) pt0 ON pt0.id = pt1.id) pt ON pt.proyecto_id = a.id " ;
2023-11-22 19:08:19 -03:00
}
2023-10-19 18:20:37 -03:00
protected function joinEstado () : string
{
return " JOIN (
SELECT e2 .*
FROM `estado_proyecto` e2
JOIN (
SELECT MAX ( e1 . `id` ) AS 'id' , e1 . `proyecto`
FROM `estado_proyecto` e1
JOIN (
2023-10-19 22:43:21 -03:00
SELECT `id` , `proyecto` , MAX ( `fecha` ) as 'fecha'
2023-10-19 18:20:37 -03:00
FROM `estado_proyecto`
GROUP BY `proyecto` , `fecha`
) e0 ON e1 . `id` = e0 . `id`
GROUP BY `proyecto`
) e01 ON e01 . `id` = e2 . `id`
) ep ON ep . `proyecto` = a . `id`
JOIN `tipo_estado_proyecto` tep ON tep . `id` = ep . `estado`
JOIN `etapa_proyecto` et ON et . `id` = tep . `etapa` " ;
}
2023-07-24 20:55:26 -04:00
}