2023-07-28 16:22:20 -04:00
|
|
|
<?php
|
|
|
|
namespace Incoviba\Repository;
|
|
|
|
|
2023-11-23 23:35:19 -03:00
|
|
|
use PDO;
|
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,
|
|
|
|
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']])))
|
|
|
|
->register('pie', (new Implement\Repository\Mapper())
|
|
|
|
->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
|
|
|
|
], $data])))
|
|
|
|
/*->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')
|
2023-12-21 19:15:11 -03:00
|
|
|
->register('resciliacion', (new Implement\Repository\Mapper())
|
|
|
|
->setFactory((new Implement\Repository\Factory())
|
|
|
|
->setCallable([$this->pagoService, 'getById'])
|
|
|
|
->setArgs(['pago_id' => $data['resciliacion']])))
|
2023-08-08 23:53:49 -04:00
|
|
|
->register('relacionado', new Implement\Repository\Mapper\Boolean('relacionado'));
|
|
|
|
//->register('promocion')
|
|
|
|
//->register('devolucion');
|
2023-07-28 16:22:20 -04:00
|
|
|
return $this->parseData(new Model\Venta(), $data, $map);
|
|
|
|
}
|
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'],
|
2023-09-13 18:51:46 -03:00
|
|
|
[$model->propietario()->rut, $model->propiedad()->id, $model->formaPago()->pie?->id, $model->formaPago()->bonoPie?->id,
|
2023-08-08 23:53:49 -04:00
|
|
|
$model->formaPago()->credito?->id, $model->formaPago()->escritura?->id, $model->formaPago()->subsidio?->id,
|
2023-09-13 18:51:46 -03:00
|
|
|
$model->formaPago()->escritura !== null ? $model->formaPago()->escritura->pago->fecha->format('Y-m-d') : null,
|
|
|
|
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;
|
|
|
|
}
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function fetchByProyecto(int $proyecto_id): array
|
|
|
|
{
|
|
|
|
$query = "SELECT a.*
|
|
|
|
FROM `{$this->getTable()}` a
|
|
|
|
JOIN `propiedad_unidad` pu ON pu.`propiedad` = a.`propiedad`
|
|
|
|
JOIN `unidad` ON `unidad`.`id` = pu.`unidad` AND pu.`principal` = 1
|
|
|
|
JOIN `proyecto_tipo_unidad` ptu ON ptu.`id` = `unidad`.`pt`
|
|
|
|
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`
|
|
|
|
JOIN `tipo_estado_venta` tev ON tev.`id` = ev.`estado`
|
|
|
|
WHERE ptu.`proyecto` = ? AND tev.`activa`
|
2023-09-28 21:05:16 -03:00
|
|
|
GROUP BY a.`id`";
|
|
|
|
return $this->fetchMany($query, [$proyecto_id]);
|
|
|
|
}
|
2023-11-23 23:35:19 -03:00
|
|
|
public function fetchIdsByProyecto(int $proyecto_id): array
|
|
|
|
{
|
|
|
|
$query = "SELECT a.`id`
|
|
|
|
FROM `{$this->getTable()}` a
|
|
|
|
JOIN `propiedad_unidad` pu ON pu.`propiedad` = a.`propiedad`
|
|
|
|
JOIN `unidad` ON `unidad`.`id` = pu.`unidad` AND pu.`principal` = 1
|
|
|
|
JOIN `proyecto_tipo_unidad` ptu ON ptu.`id` = `unidad`.`pt`
|
|
|
|
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`
|
|
|
|
JOIN `tipo_estado_venta` tev ON tev.`id` = ev.`estado`
|
|
|
|
WHERE ptu.`proyecto` = ? AND tev.`activa`
|
|
|
|
GROUP BY a.`id`";
|
|
|
|
return $this->connection->execute($query, [$proyecto_id])->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
}
|
2023-09-28 21:05:16 -03:00
|
|
|
public function fetchActivaByProyecto(int $proyecto_id): array
|
|
|
|
{
|
|
|
|
$query = "SELECT a.*
|
|
|
|
FROM `{$this->getTable()}` a
|
|
|
|
JOIN `propiedad_unidad` pu ON pu.`propiedad` = a.`propiedad`
|
|
|
|
JOIN `unidad` ON `unidad`.`id` = pu.`unidad` AND pu.`principal` = 1
|
|
|
|
JOIN `proyecto_tipo_unidad` ptu ON ptu.`id` = `unidad`.`pt`
|
|
|
|
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`
|
|
|
|
JOIN `tipo_estado_venta` tev ON tev.`id` = ev.`estado`
|
|
|
|
WHERE ptu.`proyecto` = ? AND tev.`activa`
|
2023-07-28 16:22:20 -04:00
|
|
|
GROUP BY a.`id`";
|
|
|
|
return $this->fetchMany($query, [$proyecto_id]);
|
|
|
|
}
|
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
|
|
|
{
|
|
|
|
$query = "SELECT a.*
|
|
|
|
FROM `{$this->getTable()}` a
|
|
|
|
JOIN `propiedad_unidad` pu ON pu.`propiedad` = a.`propiedad`
|
|
|
|
JOIN `unidad` ON `unidad`.`id` = pu.`unidad` AND pu.`principal` = 1
|
|
|
|
JOIN `proyecto_tipo_unidad` ptu ON ptu.`id` = `unidad`.`pt`
|
|
|
|
JOIN `proyecto` ON `proyecto`.`id` = ptu.`proyecto`
|
|
|
|
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`
|
|
|
|
JOIN `tipo_estado_venta` tev ON tev.`id` = ev.`estado`
|
|
|
|
WHERE `proyecto`.`descripcion` = ? AND `unidad`.`descripcion` = ? AND tev.`activa`";
|
|
|
|
return $this->fetchOne($query, [$proyecto_nombre, $unidad_descripcion]);
|
|
|
|
}
|
2023-09-13 18:51:46 -03:00
|
|
|
public function fetchByPie(int $pie_id): Model\Venta
|
|
|
|
{
|
|
|
|
$query = "SELECT * FROM `{$this->getTable()}` WHERE `pie` = ?";
|
|
|
|
return $this->fetchOne($query, [$pie_id]);
|
|
|
|
}
|
2024-01-08 17:33:42 -03:00
|
|
|
public function fetchIdByPie(int $pie_id): array
|
|
|
|
{
|
|
|
|
$query = "SELECT `id` FROM `{$this->getTable()}` WHERE `pie` = ?";
|
|
|
|
return $this->connection->execute($query, [$pie_id])->fetch(PDO::FETCH_ASSOC);
|
|
|
|
}
|
2023-09-28 21:05:16 -03:00
|
|
|
public function fetchByUnidad(string $unidad, string $tipo): array
|
|
|
|
{
|
|
|
|
$query = "SELECT a.*
|
|
|
|
FROM `{$this->getTable()}` a
|
|
|
|
JOIN `propiedad_unidad` pu ON pu.`propiedad` = a.`propiedad`
|
|
|
|
JOIN `unidad` ON `unidad`.`id` = pu.`unidad`
|
|
|
|
JOIN `proyecto_tipo_unidad` ptu ON ptu.`id` = `unidad`.`pt`
|
|
|
|
JOIN `tipo_unidad` tu ON tu.`id` = ptu.`tipo`
|
|
|
|
WHERE `unidad`.`descripcion` LIKE ? AND tu.`descripcion` = ?";
|
|
|
|
return $this->fetchMany($query, [$unidad, $tipo]);
|
|
|
|
}
|
2023-11-23 23:35:19 -03:00
|
|
|
public function fetchIdsByUnidad(string $unidad, string $tipo): array
|
|
|
|
{
|
|
|
|
$query = "SELECT a.id
|
|
|
|
FROM `{$this->getTable()}` a
|
|
|
|
JOIN `propiedad_unidad` pu ON pu.`propiedad` = a.`propiedad`
|
|
|
|
JOIN `unidad` ON `unidad`.`id` = pu.`unidad`
|
|
|
|
JOIN `proyecto_tipo_unidad` ptu ON ptu.`id` = `unidad`.`pt`
|
|
|
|
JOIN `tipo_unidad` tu ON tu.`id` = ptu.`tipo`
|
|
|
|
WHERE `unidad`.`descripcion` LIKE ? AND tu.`descripcion` = ?";
|
|
|
|
return $this->connection->execute($query, [$unidad, $tipo])->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
}
|
2023-09-28 21:05:16 -03:00
|
|
|
public function fetchByPrecio(string $precio): array
|
|
|
|
{
|
|
|
|
$query = "SELECT * FROM `{$this->getTable()}` WHERE `valor_uf` = ?";
|
|
|
|
return $this->fetchMany($query, [$precio]);
|
|
|
|
}
|
2023-11-23 23:35:19 -03:00
|
|
|
public function fetchIdsByPrecio(string $precio): array
|
|
|
|
{
|
|
|
|
$query = "SELECT `id` FROM `{$this->getTable()}` WHERE `valor_uf` = ?";
|
|
|
|
return $this->connection->execute($query, [$precio])->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
}
|
2023-09-28 21:05:16 -03:00
|
|
|
public function fetchByPropietario(string $propietario): array
|
|
|
|
{
|
|
|
|
$query = "SELECT a.*
|
|
|
|
FROM `{$this->getTable()}` a
|
|
|
|
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";
|
|
|
|
return $this->fetchMany($query, [':propietario' => "%{$propietario}%"]);
|
|
|
|
}
|
2023-11-23 23:35:19 -03:00
|
|
|
public function fetchIdsByPropietario(string $propietario): array
|
|
|
|
{
|
|
|
|
$query = "SELECT a.id
|
|
|
|
FROM `{$this->getTable()}` a
|
|
|
|
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";
|
|
|
|
return $this->connection->execute($query, [':propietario' => "%{$propietario}%"])->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
}
|
2023-09-28 21:05:16 -03:00
|
|
|
public function fetchByPropietarioNombreCompleto(string $propietario): array
|
|
|
|
{
|
|
|
|
$query = "SELECT a.*
|
|
|
|
FROM `{$this->getTable()}` a
|
|
|
|
JOIN `propietario` ON `propietario`.`rut` = a.`propietario`
|
|
|
|
WHERE CONCAT_WS(' ', `propietario`.`nombres`, `propietario`.`apellido_paterno`, `propietario`.`apellido_materno`) LIKE ?";
|
|
|
|
return $this->fetchMany($query, [$propietario]);
|
|
|
|
}
|
2023-10-19 18:20:37 -03:00
|
|
|
public function fetchEscriturasByProyecto(int $proyecto_id): array
|
|
|
|
{
|
|
|
|
$query = "SELECT DISTINCT a.*
|
|
|
|
FROM `{$this->getTable()}` a
|
|
|
|
JOIN `propiedad_unidad` pu ON pu.`propiedad` = a.`propiedad`
|
|
|
|
JOIN `unidad` ON `unidad`.`id` = pu.`unidad`
|
|
|
|
JOIN `proyecto_tipo_unidad` ptu ON ptu.`id` = `unidad`.`id`
|
|
|
|
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`
|
|
|
|
JOIN `tipo_estado_venta` tev ON tev.`id` = ev.`estado`
|
|
|
|
WHERE ptu.`proyecto` = ? AND tev.`descripcion` IN ('firmado por inmobiliaria', 'escriturando')
|
|
|
|
GROUP BY a.`id`";
|
|
|
|
return $this->fetchMany($query, [$proyecto_id]);
|
|
|
|
}
|
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 = ?');
|
|
|
|
return $this->connection->execute($query, [$escritura_id])->fetch(PDO::FETCH_ASSOC);
|
|
|
|
}
|
|
|
|
public function fetchIdBySubsidio(int $subsidio_id): array
|
|
|
|
{
|
|
|
|
$query = $this->connection->getQueryBuilder()
|
|
|
|
->select('id')
|
|
|
|
->from($this->getTable())
|
|
|
|
->where('subsidio = ?');
|
|
|
|
return $this->connection->execute($query, [$subsidio_id])->fetch(PDO::FETCH_ASSOC);
|
|
|
|
}
|
|
|
|
public function fetchIdByCredito(int $credito_id): array
|
|
|
|
{
|
|
|
|
$query = $this->connection->getQueryBuilder()
|
|
|
|
->select('id')
|
|
|
|
->from($this->getTable())
|
|
|
|
->where('credito = ?');
|
|
|
|
return $this->connection->execute($query, [$credito_id])->fetch(PDO::FETCH_ASSOC);
|
|
|
|
}
|
|
|
|
public function fetchIdByBono(int $bono_id): array
|
|
|
|
{
|
|
|
|
$query = $this->connection->getQueryBuilder()
|
|
|
|
->select('id')
|
|
|
|
->from($this->getTable())
|
|
|
|
->where('bono_pie = ?');
|
|
|
|
return $this->connection->execute($query, [$bono_id])->fetch(PDO::FETCH_ASSOC);
|
|
|
|
}
|
2023-07-28 16:22:20 -04:00
|
|
|
}
|