2023-07-28 16:22:20 -04:00
< ? php
namespace Incoviba\Repository ;
2023-11-23 23:35:19 -03:00
use PDO ;
2025-03-03 14:57:22 -03:00
use PDOException ;
2024-07-03 17:20:26 -04:00
use Psr\Log\LoggerInterface ;
2023-07-28 16:22:20 -04:00
use Incoviba\Common\Ideal ;
use Incoviba\Common\Define ;
2023-08-08 23:53:49 -04:00
use Incoviba\Common\Implement ;
2023-07-28 16:22:20 -04:00
use Incoviba\Model ;
2023-08-08 23:53:49 -04:00
use Incoviba\Service ;
2023-07-28 16:22:20 -04:00
class Venta extends Ideal\Repository
{
public function __construct (
Define\Connection $connection ,
2024-07-03 17:20:26 -04:00
protected LoggerInterface $logger ,
2023-07-28 16:22:20 -04:00
protected Venta\Propietario $propietarioRepository ,
protected Venta\Propiedad $propiedadRepository ,
2023-08-08 23:53:49 -04:00
protected Service\Venta\Pie $pieService ,
2023-07-28 16:22:20 -04:00
protected Venta\BonoPie $bonoPieRepository ,
protected Venta\Credito $creditoRepository ,
protected Venta\Escritura $escrituraRepository ,
protected Venta\Subsidio $subsidioRepository ,
protected Venta\Entrega $entregaRepository ,
2023-08-08 23:53:49 -04:00
protected Service\Venta\Pago $pagoService
2023-07-28 16:22:20 -04:00
)
{
parent :: __construct ( $connection );
$this -> setTable ( 'venta' );
}
2023-09-13 18:51:46 -03:00
public function create ( ? array $data = null ) : Model\Venta
2023-07-28 16:22:20 -04:00
{
2023-09-13 18:51:46 -03:00
$map = ( new Implement\Repository\MapperParser ([ 'uf' ]))
2023-08-08 23:53:49 -04:00
-> register ( 'propietario' , ( new Implement\Repository\Mapper ())
-> setFactory (( new Implement\Repository\Factory ())
-> setCallable ([ $this -> propietarioRepository , 'fetchById' ])
-> setArgs ([ $data [ 'propietario' ]])))
-> register ( 'propiedad' , ( new Implement\Repository\Mapper ())
-> setFactory (( new Implement\Repository\Factory ())
-> setCallable ([ $this -> propiedadRepository , 'fetchById' ])
-> setArgs ([ $data [ 'propiedad' ]])))
2024-03-13 14:38:44 -03:00
/*-> register ( 'pie' , ( new Implement\Repository\Mapper ())
2023-08-08 23:53:49 -04:00
-> setProperty ( 'formaPago' )
-> setFactory (( new Implement\Repository\Factory ())
-> setCallable ( function ( $repositories , $data ) {
$fp = new Model\Venta\FormaPago ();
$map = [
'pie' => [
'service' => $repositories -> pieService
],
'bono_pie' => [
'property' => 'bonoPie' ,
'repository' => $repositories -> bonoPieRepository
],
'credito' => [
'repository' => $repositories -> creditoRepository
],
'escritura' => [
'repository' => $repositories -> escrituraRepository
],
'subsidio' => [
'repository' => $repositories -> subsidioRepository
],
'devolucion' => [
'service' => $repositories -> pagoService
]
];
foreach ( $map as $column => $settings ) {
2023-09-13 18:51:46 -03:00
if ( isset ( $data [ $column ]) and $data [ $column ] !== 0 ) {
2023-08-08 23:53:49 -04:00
if ( isset ( $settings [ 'repository' ])) {
$fp -> { $settings [ 'property' ] ? ? $column } = $settings [ 'repository' ] -> fetchById ( $data [ $column ]);
continue ;
}
$fp -> { $settings [ 'property' ] ? ? $column } = $settings [ 'service' ] -> getById ( $data [ $column ]);
continue ;
}
$fp -> { $settings [ 'property' ] ? ? $column } = null ;
2023-07-28 16:22:20 -04:00
}
2023-08-08 23:53:49 -04:00
return $fp ;
})
-> setArgs ([( object ) [
'pieService' => $this -> pieService ,
'bonoPieRepository' => $this -> bonoPieRepository ,
'creditoRepository' => $this -> creditoRepository ,
'escrituraRepository' => $this -> escrituraRepository ,
'subsidioRepository' => $this -> subsidioRepository ,
'pagoService' => $this -> pagoService
2024-03-13 14:38:44 -03:00
], $data ]))) */
2023-08-08 23:53:49 -04:00
/*-> register ( 'escriturado' , ( new Implement\Repository\Mapper ())
-> setFunction ( function ( $data ) {
2023-07-28 16:22:20 -04:00
return $data [ 'escritura' ] !== null ;
2023-08-08 23:53:49 -04:00
})) */
2023-09-13 18:51:46 -03:00
/*-> register ( 'entrega' , ( new Implement\Repository\Mapper ())
2023-08-08 23:53:49 -04:00
-> setFactory (( new Implement\Repository\Factory ())
-> setCallable ( function ( $entrega_id ) {
if ( $entrega_id !== null and $entrega_id !== 0 ) {
return $this -> entregaRepository -> fetchById ( $entrega_id );
}
return null ;
})
2023-09-13 18:51:46 -03:00
-> setArgs ([ $data [ 'entrega' ]]))
-> setDefault ( null )) */
2023-08-08 23:53:49 -04:00
/*-> register ( 'entregado' , ( new Implement\Repository\Mapper ())
-> setFactory (( new Implement\Repository\Factory ())
-> setCallable ( function ( $entrega_id ) {
if ( $entrega_id !== null and $entrega_id !== 0 ) {
return $entrega_id != null ;
}
return false ;
})
-> setArgs ([ $data [ 'entrega' ]]))) */
-> register ( 'fecha' , new Implement\Repository\Mapper\DateTime ( 'fecha' ))
-> register ( 'valor_uf' , ( new Implement\Repository\Mapper ())
-> setProperty ( 'valor' ))
//->register('estado')
-> register ( 'fecha_ingreso' , new Implement\Repository\Mapper\DateTime ( 'fecha_ingreso' , 'fechaIngreso' ))
//->register('avalchile')
//->register('agente')
-> register ( 'relacionado' , new Implement\Repository\Mapper\Boolean ( 'relacionado' ));
//->register('promocion')
//->register('devolucion');
2024-01-19 23:10:20 -03:00
if ( array_key_exists ( 'resciliacion' , $data )) {
$map = $map -> register ( 'resciliacion' , ( new Implement\Repository\Mapper ())
-> setFactory (( new Implement\Repository\Factory ())
-> setCallable ([ $this -> pagoService , 'getById' ])
-> setArgs ([ 'pago_id' => $data [ 'resciliacion' ]])));
}
2023-07-28 16:22:20 -04:00
return $this -> parseData ( new Model\Venta (), $data , $map );
}
2025-03-03 14:57:22 -03:00
/**
* @ param Define\Model $model
* @ return Model\Venta
* @ throws PDOException
*/
2023-09-13 18:51:46 -03:00
public function save ( Define\Model $model ) : Model\Venta
2023-07-28 16:22:20 -04:00
{
$model -> id = $this -> saveNew (
[ 'propietario' , 'propiedad' , 'pie' , 'bono_pie' , 'credito' , 'escritura' , 'subsidio' , 'escriturado' ,
'entrega' , 'entregado' , 'fecha' , 'valor_uf' , 'estado' , 'fecha_ingreso' , 'avalchile' , 'agente' , 'uf' ,
'relacionado' , 'promocion' , 'resciliacion' , 'devolucion' ],
2024-03-13 22:43:37 -03:00
[ $model -> propietario () -> rut , $model -> propiedad () -> id , $model -> formaPago () ? -> pie ? -> id , $model -> formaPago () ? -> bonoPie ? -> id ,
$model -> formaPago () ? -> credito ? -> id , $model -> formaPago () ? -> escritura ? -> id , $model -> formaPago () ? -> subsidio ? -> id ,
$model -> formaPago () ? -> escritura !== null ? $model -> formaPago () ? -> escritura -> pago -> fecha -> format ( 'Y-m-d' ) : null ,
2023-09-13 18:51:46 -03:00
null , null , $model -> fecha -> format ( 'Y-m-d' ), $model -> valor , 1 , $model -> fechaIngreso -> format ( 'Y-m-d' ),
null , null , $model -> uf , $model -> relacionado ? 1 : 0 , null , null , null ]
2023-07-28 16:22:20 -04:00
);
return $model ;
}
2025-03-03 14:57:22 -03:00
/**
* @ param Define\Model $model
* @ param array $new_data
* @ return Model\Venta
* @ throws Implement\Exception\EmptyResult
*/
2023-09-13 18:51:46 -03:00
public function edit ( Define\Model $model , array $new_data ) : Model\Venta
2023-07-28 16:22:20 -04:00
{
return $this -> update ( $model , [ 'propietario' , 'propiedad' , 'pie' , 'bono_pie' , 'credito' , 'escritura' , 'subsidio' , 'escriturado' ,
'entrega' , 'entregado' , 'fecha' , 'valor_uf' , 'estado' , 'fecha_ingreso' , 'avalchile' , 'agente' , 'uf' ,
'relacionado' , 'promocion' , 'resciliacion' , 'devolucion' ], $new_data );
}
2025-03-03 14:57:22 -03:00
/**
* @ param int $proyecto_id
* @ return array
* @ throws Implement\Exception\EmptyResult
*/
2023-07-28 16:22:20 -04:00
public function fetchByProyecto ( int $proyecto_id ) : array
{
2024-01-19 23:10:20 -03:00
$query = $this -> connection -> getQueryBuilder ()
-> select ( 'a.*' )
-> from ( " { $this -> getTable () } a " )
-> joined ( 'JOIN propiedad_unidad pu ON pu.propiedad = a.propiedad' )
-> joined ( 'JOIN unidad ON unidad.id = pu.unidad AND pu.principal = 1' )
-> joined ( 'JOIN proyecto_tipo_unidad ptu ON ptu.id = unidad.pt' )
-> joined ( 'JOIN (SELECT ev1.* FROM estado_venta ev1 JOIN (SELECT MAX(id) AS id, venta FROM estado_venta GROUP BY venta) ev0 ON ev0.id = ev1.id) ev ON ev.venta = a.id' )
-> joined ( 'JOIN tipo_estado_venta tev ON tev.id = ev.estado' )
-> where ( 'ptu.proyecto = ? AND tev.activa' )
-> group ( 'a.id' );
2023-09-28 21:05:16 -03:00
return $this -> fetchMany ( $query , [ $proyecto_id ]);
}
2025-03-03 14:57:22 -03:00
/**
* @ param int $proyecto_id
* @ return array
* @ throws Implement\Exception\EmptyResult
*/
2023-11-23 23:35:19 -03:00
public function fetchIdsByProyecto ( int $proyecto_id ) : array
{
2024-01-19 23:10:20 -03:00
$query = $this -> connection -> getQueryBuilder ()
-> select ( 'a.id' )
-> from ( " { $this -> getTable () } a " )
-> joined ( 'JOIN propiedad_unidad pu ON pu.propiedad = a.propiedad' )
-> joined ( 'JOIN unidad ON unidad.id = pu.unidad AND unidad.pt' )
2024-01-22 12:10:16 -03:00
-> joined ( 'JOIN proyecto_tipo_unidad ptu ON ptu.id = unidad.pt' )
2024-01-19 23:10:20 -03:00
-> joined ( 'JOIN (SELECT ev1.* FROM estado_venta ev1 JOIN (SELECT MAX(id) AS id, venta FROM estado_venta GROUP BY venta) ev0 ON ev0.id = ev1.id) ev ON ev.venta = a.id' )
-> joined ( 'JOIN tipo_estado_venta tev ON tev.id = ev.estado' )
2024-04-19 23:19:35 -04:00
-> where ( 'ptu.proyecto = ? AND tev.activa = 1' )
2024-01-19 23:10:20 -03:00
-> group ( 'a.id' );
2024-01-22 12:10:16 -03:00
return $this -> fetchIds ( $query , [ $proyecto_id ]);
2023-11-23 23:35:19 -03:00
}
2025-03-03 14:57:22 -03:00
/**
* @ param int $proyecto_id
* @ return array
* @ throws Implement\Exception\EmptyResult
*/
2023-09-28 21:05:16 -03:00
public function fetchActivaByProyecto ( int $proyecto_id ) : array
{
2024-01-19 23:10:20 -03:00
$query = $this -> connection -> getQueryBuilder ()
-> select ( 'a.*' )
-> from ( " { $this -> getTable () } a " )
-> joined ( 'JOIN `propiedad_unidad` pu ON pu.`propiedad` = a.`propiedad`' )
-> joined ( 'JOIN `unidad` ON `unidad`.`id` = pu.`unidad` AND pu.`principal` = 1' )
-> joined ( 'JOIN `proyecto_tipo_unidad` ptu ON ptu.`id` = `unidad`.`pt`' )
-> joined ( " JOIN (SELECT e1.* FROM `estado_venta` e1 JOIN (SELECT MAX(`id`) AS 'id', `venta` FROM `estado_venta` GROUP BY `venta`) e0 ON e0.`id` = e1.`id`) ev ON ev.`venta` = a.`id` " )
-> joined ( 'JOIN `tipo_estado_venta` tev ON tev.`id` = ev.`estado`' )
2024-04-19 23:19:35 -04:00
-> where ( 'ptu.`proyecto` = ? AND tev.`activa` = 1' )
2024-01-19 23:10:20 -03:00
-> group ( 'a.id' );
2023-07-28 16:22:20 -04:00
return $this -> fetchMany ( $query , [ $proyecto_id ]);
}
2025-03-03 14:57:22 -03:00
/**
* @ param string $proyecto_nombre
* @ param int $unidad_descripcion
* @ return Model\Venta
* @ throws Implement\Exception\EmptyResult
*/
2023-09-13 18:51:46 -03:00
public function fetchByProyectoAndUnidad ( string $proyecto_nombre , int $unidad_descripcion ) : Model\Venta
2023-08-08 23:53:49 -04:00
{
2024-01-19 23:10:20 -03:00
$query = $this -> connection -> getQueryBuilder ()
-> select ( " a.* " )
-> from ( " ` { $this -> getTable () } ` a " )
-> joined ( 'JOIN `propiedad_unidad` pu ON pu.`propiedad` = a.`propiedad`' )
-> joined ( 'JOIN `unidad` ON `unidad`.`id` = pu.`unidad` AND pu.`principal` = 1' )
-> joined ( 'JOIN `proyecto_tipo_unidad` ptu ON ptu.`id` = `unidad`.`pt`' )
-> joined ( 'JOIN `proyecto` ON `proyecto`.`id` = ptu.`proyecto`' )
-> joined ( " JOIN (SELECT e1.* FROM `estado_venta` e1 JOIN (SELECT MAX(`id`) AS 'id', `venta` FROM `estado_venta` GROUP BY `venta`) e0 ON e0.`id` = e1.`id`) ev ON ev.`venta` = a.`id` " )
-> joined ( 'JOIN `tipo_estado_venta` tev ON tev.`id` = ev.`estado`' )
2024-01-30 12:07:43 -03:00
-> where ( '`proyecto`.`descripcion` = ? AND `unidad`.`descripcion` = ? AND tev.`activa`' );
2023-08-08 23:53:49 -04:00
return $this -> fetchOne ( $query , [ $proyecto_nombre , $unidad_descripcion ]);
}
2025-03-03 14:57:22 -03:00
/**
* @ param int $pie_id
* @ return Model\Venta
* @ throws Implement\Exception\EmptyResult
*/
2023-09-13 18:51:46 -03:00
public function fetchByPie ( int $pie_id ) : Model\Venta
{
2024-01-19 23:10:20 -03:00
$query = $this -> connection -> getQueryBuilder ()
-> select ()
-> from ( $this -> getTable ())
-> where ( 'pie = ?' );
2023-09-13 18:51:46 -03:00
return $this -> fetchOne ( $query , [ $pie_id ]);
}
2025-03-03 14:57:22 -03:00
/**
* @ param int $pie_id
* @ return array
* @ throws Implement\Exception\EmptyResult
*/
2024-01-08 17:33:42 -03:00
public function fetchIdByPie ( int $pie_id ) : array
{
2024-01-19 23:10:20 -03:00
$query = $this -> connection -> getQueryBuilder ()
-> select ( 'id' )
-> from ( $this -> getTable ())
-> where ( 'pie = ?' );
2024-01-22 12:10:16 -03:00
return $this -> fetchId ( $query , [ $pie_id ]);
2024-01-08 17:33:42 -03:00
}
2025-03-03 14:57:22 -03:00
/**
* @ param string $unidad
* @ param string $tipo
* @ return array
* @ throws Implement\Exception\EmptyResult
*/
2023-09-28 21:05:16 -03:00
public function fetchByUnidad ( string $unidad , string $tipo ) : array
{
2024-01-19 23:10:20 -03:00
$query = $this -> connection -> getQueryBuilder ()
-> select ( 'a.*' )
-> from ( " { $this -> getTable () } a " )
-> joined ( 'JOIN `propiedad_unidad` pu ON pu.`propiedad` = a.`propiedad`' )
-> joined ( 'JOIN `unidad` ON `unidad`.`id` = pu.`unidad`' )
-> joined ( 'JOIN `proyecto_tipo_unidad` ptu ON ptu.`id` = `unidad`.`pt`' )
-> joined ( 'JOIN `tipo_unidad` tu ON tu.`id` = ptu.`tipo`' )
-> where ( '`unidad`.`descripcion` LIKE ? AND tu.`descripcion` = ?' );
2023-09-28 21:05:16 -03:00
return $this -> fetchMany ( $query , [ $unidad , $tipo ]);
}
2025-03-03 14:57:22 -03:00
/**
* @ param int $unidad_id
* @ return Model\Venta
* @ throws Implement\Exception\EmptyResult
*/
2024-02-13 17:59:46 -03:00
public function fetchByUnidadId ( int $unidad_id ) : Model\Venta
{
$query = $this -> connection -> getQueryBuilder ()
-> select ( 'a.*' )
-> from ( " { $this -> getTable () } a " )
-> joined ( 'JOIN propiedad_unidad pu ON pu.propiedad = a.propiedad' )
-> where ( 'pu.unidad = ?' );
return $this -> fetchOne ( $query , [ $unidad_id ]);
}
2025-03-03 14:57:22 -03:00
/**
* @ param string $unidad
* @ param string $tipo
* @ return array
* @ throws Implement\Exception\EmptyResult
*/
2023-11-23 23:35:19 -03:00
public function fetchIdsByUnidad ( string $unidad , string $tipo ) : array
{
2024-01-19 23:10:20 -03:00
$query = $this -> connection -> getQueryBuilder ()
-> select ( 'a.id' )
-> from ( " { $this -> getTable () } a " )
-> joined ( 'JOIN `propiedad_unidad` pu ON pu.`propiedad` = a.`propiedad`' )
-> joined ( 'JOIN `unidad` ON `unidad`.`id` = pu.`unidad`' )
-> joined ( 'JOIN `proyecto_tipo_unidad` ptu ON ptu.`id` = `unidad`.`pt`' )
-> joined ( 'JOIN `tipo_unidad` tu ON tu.`id` = ptu.`tipo`' )
-> where ( '`unidad`.`descripcion` LIKE ? AND tu.`descripcion` = ?' );
2024-01-22 12:10:16 -03:00
return $this -> fetchIds ( $query , [ $unidad , $tipo ]);
2023-11-23 23:35:19 -03:00
}
2025-03-03 14:57:22 -03:00
/**
* @ param string $precio
* @ return array
* @ throws Implement\Exception\EmptyResult
*/
2023-09-28 21:05:16 -03:00
public function fetchByPrecio ( string $precio ) : array
{
2024-01-19 23:10:20 -03:00
$query = $this -> connection -> getQueryBuilder ()
-> select ()
-> from ( $this -> getTable ())
-> where ( 'valor_uf = ?' );
2023-09-28 21:05:16 -03:00
return $this -> fetchMany ( $query , [ $precio ]);
}
2025-03-03 14:57:22 -03:00
/**
* @ param string $precio
* @ return array
* @ throws Implement\Exception\EmptyResult
*/
2023-11-23 23:35:19 -03:00
public function fetchIdsByPrecio ( string $precio ) : array
{
2024-01-19 23:10:20 -03:00
$query = $this -> connection -> getQueryBuilder ()
-> select ( 'id' )
-> from ( $this -> getTable ())
-> where ( 'valor_uf = ?' );
2024-01-22 12:10:16 -03:00
return $this -> fetchIds ( $query , [ $precio ]);
2023-11-23 23:35:19 -03:00
}
2025-03-03 14:57:22 -03:00
/**
* @ param int $propietario_rut
* @ param int $propiedad_id
* @ return Model\Venta
* @ throws Implement\Exception\EmptyResult
*/
2024-01-19 23:10:20 -03:00
public function fetchByPropietarioAndPropiedad ( int $propietario_rut , int $propiedad_id ) : Model\Venta
{
$query = $this -> connection -> getQueryBuilder ()
-> select ()
-> from ( $this -> getTable ())
-> where ( 'propietario = ? AND propiedad = ?' );
return $this -> fetchOne ( $query , [ $propietario_rut , $propiedad_id ]);
}
2025-03-03 14:57:22 -03:00
/**
* @ param string $propietario
* @ return array
* @ throws Implement\Exception\EmptyResult
*/
2023-09-28 21:05:16 -03:00
public function fetchByPropietario ( string $propietario ) : array
{
2024-01-19 23:10:20 -03:00
$query = $this -> connection -> getQueryBuilder ()
-> select ( 'a.*' )
-> from ( " { $this -> getTable () } a " )
-> joined ( 'JOIN `propietario` ON `propietario`.`rut` = a.`propietario`' )
-> where ( " CONCAT_WS('-', `propietario`.`rut`, `propietario`.`dv`) LIKE :propietario OR `propietario`.`nombres` LIKE :propietario
OR `propietario` . `apellido_paterno` LIKE : propietario OR `propietario` . `apellido_materno` LIKE : propietario
OR CONCAT_WS ( ' ' , `propietario` . `nombres` , `propietario` . `apellido_paterno` , `propietario` . `apellido_materno` ) LIKE : propietario " );
2023-09-28 21:05:16 -03:00
return $this -> fetchMany ( $query , [ ':propietario' => " % { $propietario } % " ]);
}
2025-03-03 14:57:22 -03:00
/**
* @ param string $propietario
* @ return array
* @ throws Implement\Exception\EmptyResult
*/
2023-11-23 23:35:19 -03:00
public function fetchIdsByPropietario ( string $propietario ) : array
{
2024-01-19 23:10:20 -03:00
$query = $this -> connection -> getQueryBuilder ()
-> select ( 'a.id' )
-> from ( " { $this -> getTable () } a " )
-> joined ( 'JOIN `propietario` ON `propietario`.`rut` = a.`propietario`' )
-> where ( " CONCAT_WS('-', `propietario`.`rut`, `propietario`.`dv`) LIKE :propietario OR `propietario`.`nombres` LIKE :propietario
OR `propietario` . `apellido_paterno` LIKE : propietario OR `propietario` . `apellido_materno` LIKE : propietario
2024-02-27 16:11:48 -03:00
OR CONCAT_WS ( ' ' , `propietario` . `nombres` , `propietario` . `apellido_paterno` , `propietario` . `apellido_materno` ) LIKE : propietario
OR rut = : rut
OR CONCAT_WS ( '-' , rut , dv ) = : rut " );
return $this -> fetchIds ( $query , [ ':propietario' => " % { $propietario } % " , ':rut' => $propietario ]);
2023-11-23 23:35:19 -03:00
}
2025-03-03 14:57:22 -03:00
/**
* @ param string $propietario
* @ return array
* @ throws Implement\Exception\EmptyResult
*/
2023-09-28 21:05:16 -03:00
public function fetchByPropietarioNombreCompleto ( string $propietario ) : array
{
2024-01-19 23:10:20 -03:00
$query = $this -> connection -> getQueryBuilder ()
-> select ( 'a.*' )
-> from ( " { $this -> getTable () } a " )
-> joined ( 'JOIN `propietario` ON `propietario`.`rut` = a.`propietario`' )
-> where ( " CONCAT_WS(' ', `propietario`.`nombres`, `propietario`.`apellido_paterno`, `propietario`.`apellido_materno`) LIKE ? " );
2023-09-28 21:05:16 -03:00
return $this -> fetchMany ( $query , [ $propietario ]);
}
2025-03-03 14:57:22 -03:00
/**
* @ param int $proyecto_id
* @ return array
* @ throws Implement\Exception\EmptyResult
*/
2023-10-19 18:20:37 -03:00
public function fetchEscriturasByProyecto ( int $proyecto_id ) : array
{
2024-01-19 23:10:20 -03:00
$query = $this -> connection -> getQueryBuilder ()
-> select ( 'DISTINCT a.*' )
-> from ( " { $this -> getTable () } a " )
-> joined ( 'JOIN `propiedad_unidad` pu ON pu.`propiedad` = a.`propiedad`' )
-> joined ( 'JOIN `unidad` ON `unidad`.`id` = pu.`unidad`' )
-> joined ( 'JOIN `proyecto_tipo_unidad` ptu ON ptu.`id` = `unidad`.`id`' )
-> joined ( " JOIN (SELECT e1.* FROM `estado_venta` e1 JOIN (SELECT MAX(`id`) AS 'id', `venta` FROM `estado_venta` GROUP BY `venta`) e0 ON e0.`id` = e1.`id`) ev ON ev.`venta` = a.`id` " )
-> joined ( 'JOIN `tipo_estado_venta` tev ON tev.`id` = ev.`estado`' )
-> where ( " ptu.`proyecto` = ? AND tev.`descripcion` IN ('firmado por inmobiliaria', 'escriturando') " )
-> group ( 'a.id' );
2023-10-19 18:20:37 -03:00
return $this -> fetchMany ( $query , [ $proyecto_id ]);
}
2025-03-03 14:57:22 -03:00
/**
* @ param int $escritura_id
* @ return array
* @ throws Implement\Exception\EmptyResult
*/
2024-01-08 17:33:42 -03:00
public function fetchIdByEscritura ( int $escritura_id ) : array
{
$query = $this -> connection -> getQueryBuilder ()
-> select ( 'id' )
-> from ( $this -> getTable ())
-> where ( 'escritura = ?' );
2024-01-22 12:10:16 -03:00
return $this -> fetchId ( $query , [ $escritura_id ]);
2024-01-08 17:33:42 -03:00
}
2025-03-03 14:57:22 -03:00
/**
* @ param int $subsidio_id
* @ return array
* @ throws Implement\Exception\EmptyResult
*/
2024-01-08 17:33:42 -03:00
public function fetchIdBySubsidio ( int $subsidio_id ) : array
{
$query = $this -> connection -> getQueryBuilder ()
-> select ( 'id' )
-> from ( $this -> getTable ())
-> where ( 'subsidio = ?' );
2024-01-22 12:10:16 -03:00
return $this -> fetchId ( $query , [ $subsidio_id ]);
2024-01-08 17:33:42 -03:00
}
2025-03-03 14:57:22 -03:00
/**
* @ param int $credito_id
* @ return array
* @ throws Implement\Exception\EmptyResult
*/
2024-01-08 17:33:42 -03:00
public function fetchIdByCredito ( int $credito_id ) : array
{
$query = $this -> connection -> getQueryBuilder ()
-> select ( 'id' )
-> from ( $this -> getTable ())
-> where ( 'credito = ?' );
2024-01-22 12:10:16 -03:00
return $this -> fetchId ( $query , [ $credito_id ]);
2024-01-08 17:33:42 -03:00
}
2025-03-03 14:57:22 -03:00
/**
* @ param int $bono_id
* @ return array
* @ throws Implement\Exception\EmptyResult
*/
2024-01-08 17:33:42 -03:00
public function fetchIdByBono ( int $bono_id ) : array
{
$query = $this -> connection -> getQueryBuilder ()
-> select ( 'id' )
-> from ( $this -> getTable ())
-> where ( 'bono_pie = ?' );
2024-01-22 12:10:16 -03:00
return $this -> fetchId ( $query , [ $bono_id ]);
}
2025-03-03 14:57:22 -03:00
/**
* @ param int $venta_id
* @ return array
*/
2024-02-28 21:44:37 -03:00
public function fetchByIdForSearch ( int $venta_id ) : array
{
$query = $this -> connection -> getQueryBuilder ()
-> select ( 'venta.id AS id, venta.fecha AS fecha, venta.valor_uf AS valor' )
-> columns ( 'proyecto.id AS proyecto_id, proyecto.descripcion AS proyecto_descripcion' )
-> columns ( 'CONCAT_WS(" ", propietario.nombres, propietario.apellido_paterno, propietario.apellido_materno) AS propietario' )
-> columns ( 'unidad.descripcion AS unidad_descripcion, tu.descripcion AS tipo_unidad_descripcion, ptu.m2 + ptu.logia + ptu.terraza AS superficie' )
2024-11-18 22:25:42 -03:00
-> columns ( 'GROUP_CONCAT(CONCAT(UPPER(LEFT(tu.descripcion, 1)), unidad.descripcion) SEPARATOR " - ") AS tipologia' )
2024-02-28 21:44:37 -03:00
-> columns ( 'tev.activa' )
-> from ( $this -> getTable ())
-> joined ( 'JOIN propietario ON propietario.rut = venta.propietario' )
-> joined ( 'JOIN propiedad_unidad pu ON pu.propiedad = venta.propiedad' )
-> joined ( 'JOIN unidad ON unidad.id = pu.unidad' )
-> joined ( 'JOIN proyecto_tipo_unidad ptu ON unidad.pt = ptu.id' )
-> joined ( 'JOIN proyecto ON proyecto.id = ptu.proyecto' )
-> joined ( 'JOIN tipo_unidad tu ON tu.id = ptu.tipo' )
-> joined ( 'JOIN (SELECT ev1.* FROM estado_venta ev1 JOIN (SELECT MAX(id) AS id, venta FROM estado_venta GROUP BY venta) ev0 ON ev0.id = ev1.id) ev ON ev.venta = venta.id' )
-> joined ( 'JOIN tipo_estado_venta tev ON ev.estado = tev.id' )
2024-06-11 18:16:34 -04:00
-> where ( 'venta.id = ?' )
2024-07-03 17:20:26 -04:00
-> order ( 'tu.orden' )
2024-11-18 22:25:42 -03:00
-> order ( 'LPAD(unidad.descripcion, 4, "0")' )
-> group ( 'venta.id' )
2024-07-03 17:20:26 -04:00
-> limit ( 1 );
2024-02-28 21:44:37 -03:00
return $this -> connection -> execute ( $query , [ $venta_id ]) -> fetch ( PDO :: FETCH_ASSOC );
}
2025-03-03 14:57:22 -03:00
/**
* @ param int $venta_id
* @ return array
*/
2024-03-13 16:17:14 -03:00
public function fetchByIdForList ( int $venta_id ) : array
{
$query = $this -> connection -> getQueryBuilder ()
-> select ( 'venta.id AS id, venta.fecha AS fecha, venta.valor_uf AS valor' )
-> columns ( 'proyecto.id AS proyecto_id, proyecto.descripcion AS proyecto_descripcion' )
-> columns ( 'CONCAT_WS(" ", propietario.nombres, propietario.apellido_paterno, propietario.apellido_materno) AS propietario' )
-> columns ( " GROUP_CONCAT(unidad.descripcion SEPARATOR ' - ') AS unidad_descripcion, tu.descripcion AS tipo_unidad_descripcion, ptu.m2 + ptu.logia + ptu.terraza AS superficie " )
-> columns ( 'tev.activa' )
-> from ( $this -> getTable ())
-> joined ( 'JOIN propietario ON propietario.rut = venta.propietario' )
-> joined ( 'JOIN propiedad_unidad pu ON pu.propiedad = venta.propiedad' )
-> joined ( 'JOIN unidad ON unidad.id = pu.unidad' )
-> joined ( 'JOIN proyecto_tipo_unidad ptu ON unidad.pt = ptu.id' )
-> joined ( 'JOIN proyecto ON proyecto.id = ptu.proyecto' )
-> joined ( 'JOIN tipo_unidad tu ON tu.id = ptu.tipo' )
-> joined ( 'JOIN (SELECT ev1.* FROM estado_venta ev1 JOIN (SELECT MAX(id) AS id, venta FROM estado_venta GROUP BY venta) ev0 ON ev0.id = ev1.id) ev ON ev.venta = venta.id' )
-> joined ( 'JOIN tipo_estado_venta tev ON ev.estado = tev.id' )
-> where ( 'venta.id = ?' )
-> group ( 'venta.id' );
return $this -> connection -> execute ( $query , [ $venta_id ]) -> fetch ( PDO :: FETCH_ASSOC );
}
2024-01-22 12:10:16 -03:00
2025-03-03 14:57:22 -03:00
/**
* @ param string $query
* @ param array | null $data
* @ return array
* @ throws Implement\Exception\EmptyResult
*/
2024-01-22 12:10:16 -03:00
protected function fetchIds ( string $query , ? array $data = null ) : array
{
2025-03-03 14:57:22 -03:00
try {
return $this -> connection -> execute ( $query , $data ) -> fetchAll ( PDO :: FETCH_ASSOC );
} catch ( PDOException $exception ) {
throw new Implement\Exception\EmptyResult ( $query , $exception );
2024-01-22 12:10:16 -03:00
}
}
2025-03-03 14:57:22 -03:00
/**
* @ param string $query
* @ param array | null $data
* @ return array
* @ throws Implement\Exception\EmptyResult
*/
2024-01-22 12:10:16 -03:00
protected function fetchId ( string $query , ? array $data = null ) : array
{
2025-03-03 14:57:22 -03:00
try {
return $this -> connection -> execute ( $query , $data ) -> fetch ( PDO :: FETCH_ASSOC );
} catch ( PDOException $exception ) {
throw new Implement\Exception\EmptyResult ( $query , $exception );
2024-01-22 12:10:16 -03:00
}
2024-01-08 17:33:42 -03:00
}
2023-07-28 16:22:20 -04:00
}