Search
This commit is contained in:
@ -20,6 +20,10 @@ class Login
|
||||
if ($request->hasHeader('X-Redirect-URI')) {
|
||||
$redirect_uri = $request->getHeaderLine('X-Redirect-URI');
|
||||
}
|
||||
$query = $request->getQueryParams();
|
||||
if (isset($query['url'])) {
|
||||
$redirect_uri = base64_decode(urldecode($query['url']));
|
||||
}
|
||||
return $view->render($response, 'login.form', compact('redirect_uri'));
|
||||
}
|
||||
public function login(ServerRequestInterface $request, ResponseInterface $response, Repository\User $userRepository, Service\Login $service): ResponseInterface
|
||||
|
23
app/src/Controller/Search.php
Normal file
23
app/src/Controller/Search.php
Normal file
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
namespace Incoviba\Controller;
|
||||
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Incoviba\Common\Alias\View;
|
||||
use Incoviba\Service;
|
||||
|
||||
class Search
|
||||
{
|
||||
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, View $view, ?string $query = '', ?string $tipo = '*'): ResponseInterface
|
||||
{
|
||||
$post = $request->getParsedBody() ?? '';
|
||||
return $view->render($response, 'search', compact('post', 'query', 'tipo'));
|
||||
}
|
||||
public function query(ServerRequestInterface $request, ResponseInterface $response, Service\Search $service): ResponseInterface
|
||||
{
|
||||
$data = $request->getParsedBody();
|
||||
$results = $service->query($data['query'], $data['tipo']);
|
||||
$response->getBody()->write(json_encode(compact('results')));
|
||||
return $response->withHeader('Content-Type', 'application/json');
|
||||
}
|
||||
}
|
@ -40,7 +40,7 @@ class Ventas
|
||||
'total' => 0
|
||||
];
|
||||
try {
|
||||
$ventas = $service->fetchByProyecto($proyecto_id);
|
||||
$ventas = $service->fetchActivaByProyecto($proyecto_id);
|
||||
$output['ventas'] = array_map(function(Model\Venta $venta) {return $venta->id;}, $ventas);
|
||||
$output['proyecto']['descripcion'] = $ventas[0]->proyecto()->descripcion;
|
||||
$output['total'] = count($ventas);
|
||||
|
@ -17,7 +17,9 @@ class Authentication
|
||||
return $handler->handle($request);
|
||||
}
|
||||
$response = $this->responseFactory->createResponse(301, 'Not logged in');
|
||||
return $response->withHeader('Location', $this->login_url)
|
||||
$uri = urlencode(base64_encode((string) $request->getUri()));
|
||||
return $response->withHeader('Location', implode('?', [$this->login_url, "url={$uri}"]))
|
||||
->withHeader('Referer', (string) $request->getUri())
|
||||
->withHeader('X-Redirected-URI', (string) $request->getUri());
|
||||
}
|
||||
|
||||
|
@ -99,6 +99,7 @@ class Venta extends Ideal\Model
|
||||
'fecha_ingreso' => $this->fechaIngreso->format('Y-m-d'),
|
||||
'valor' => $this->valor,
|
||||
'relacionado' => $this->relacionado,
|
||||
'proyecto' => $this->proyecto(),
|
||||
'estados' => array_map(function(Venta\EstadoVenta $estado) {return $estado->id;}, $this->estados()),
|
||||
'current_estado' => $this->currentEstado()
|
||||
]);
|
||||
|
@ -12,7 +12,7 @@ class Propietario extends Model
|
||||
public array $apellidos;
|
||||
public Datos $datos;
|
||||
public ?Propietario $representante;
|
||||
public ?Propietario $otro;
|
||||
public ?bool $otro;
|
||||
|
||||
public function rut(): string
|
||||
{
|
||||
|
@ -152,6 +152,19 @@ FROM `{$this->getTable()}` a
|
||||
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->fetchMany($query, [$proyecto_id]);
|
||||
}
|
||||
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`
|
||||
GROUP BY a.`id`";
|
||||
return $this->fetchMany($query, [$proyecto_id]);
|
||||
}
|
||||
@ -173,4 +186,38 @@ WHERE `proyecto`.`descripcion` = ? AND `unidad`.`descripcion` = ? AND tev.`activ
|
||||
$query = "SELECT * FROM `{$this->getTable()}` WHERE `pie` = ?";
|
||||
return $this->fetchOne($query, [$pie_id]);
|
||||
}
|
||||
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]);
|
||||
}
|
||||
public function fetchByPrecio(string $precio): array
|
||||
{
|
||||
$query = "SELECT * FROM `{$this->getTable()}` WHERE `valor_uf` = ?";
|
||||
return $this->fetchMany($query, [$precio]);
|
||||
}
|
||||
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}%"]);
|
||||
}
|
||||
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]);
|
||||
}
|
||||
}
|
||||
|
@ -52,10 +52,10 @@ class Propietario extends Ideal\Repository
|
||||
}))
|
||||
->register('otro', (new Implement\Repository\Mapper())
|
||||
->setFunction(function($data) {
|
||||
if ($data['otro'] === null or $data['otro'] === 0) {
|
||||
if ($data['otro'] === null) {
|
||||
return null;
|
||||
}
|
||||
return $this->fetchById($data['otro']);
|
||||
return $data['otro'] !== 0;
|
||||
})
|
||||
->setDefault(null));
|
||||
return $this->parseData(new Model\Venta\Propietario(), $data, $map);
|
||||
|
@ -15,7 +15,7 @@ class Unidad extends Ideal\Repository
|
||||
$this->setTable('unidad');
|
||||
}
|
||||
|
||||
public function create(?array $data = null): Define\Model
|
||||
public function create(?array $data = null): Model\Venta\Unidad
|
||||
{
|
||||
$map = (new Implement\Repository\MapperParser(['subtipo', 'piso', 'descripcion', 'orientacion']))
|
||||
->register('pt', (new Implement\Repository\Mapper())
|
||||
@ -25,7 +25,7 @@ class Unidad extends Ideal\Repository
|
||||
}));
|
||||
return $this->parseData(new Model\Venta\Unidad(), $data, $map);
|
||||
}
|
||||
public function save(Define\Model $model): Define\Model
|
||||
public function save(Define\Model $model): Model\Venta\Unidad
|
||||
{
|
||||
$model->id = $this->saveNew(
|
||||
['subtipo', 'piso', 'descripcion', 'orientacion', 'pt'],
|
||||
@ -33,7 +33,7 @@ class Unidad extends Ideal\Repository
|
||||
);
|
||||
return $model;
|
||||
}
|
||||
public function edit(Define\Model $model, array $new_data): Define\Model
|
||||
public function edit(Define\Model $model, array $new_data): Model\Venta\Unidad
|
||||
{
|
||||
return $this->update($model, ['subtipo', 'piso', 'descripcion', 'orientacion', 'pt'], $new_data);
|
||||
}
|
||||
@ -83,4 +83,17 @@ WHERE ptu.`proyecto` = ? AND (pu.`id` IS NULL OR `venta`.`id` IS NULL OR tev.`ac
|
||||
ORDER BY tu.`orden`";
|
||||
return $this->fetchMany($query, [$proyecto_id]);
|
||||
}
|
||||
public function fetchDisponiblesByDescripcionAndTipo(string $descripcion, string $tipo): array
|
||||
{
|
||||
$query = "SELECT DISTINCT a.*
|
||||
FROM `{$this->getTable()}` a
|
||||
JOIN `proyecto_tipo_unidad` ptu ON ptu.`id` = a.`pt`
|
||||
JOIN `tipo_unidad` tu ON tu.`id` = ptu.`tipo`
|
||||
LEFT OUTER JOIN `propiedad_unidad` pu ON pu.`unidad` = a.`id`
|
||||
LEFT OUTER JOIN `venta` ON `venta`.`propiedad` = pu.`propiedad`
|
||||
LEFT OUTER JOIN (SELECT ev1.* FROM `estado_venta` ev1 JOIN (SELECT MAX(`id`) as 'id', `venta` FROM `estado_venta`) ev0 ON ev0.`id` = ev1.`id`) ev ON ev.`venta` = `venta`.`id`
|
||||
LEFT OUTER JOIN `tipo_estado_venta` tev ON tev.`id` = ev.`estado`
|
||||
WHERE a.`descripcion` LIKE ? AND tu.`descripcion` = ? AND (pu.`id` IS NULL OR `venta`.`id` IS NULL OR tev.`activa` = 0)";
|
||||
return $this->fetchMany($query, [$descripcion, $tipo]);
|
||||
}
|
||||
}
|
||||
|
186
app/src/Service/Search.php
Normal file
186
app/src/Service/Search.php
Normal file
@ -0,0 +1,186 @@
|
||||
<?php
|
||||
namespace Incoviba\Service;
|
||||
|
||||
use Incoviba\Common\Implement\Exception\EmptyResult;
|
||||
use Incoviba\Repository;
|
||||
use Incoviba\Model;
|
||||
|
||||
class Search
|
||||
{
|
||||
public function __construct(protected Venta $ventaService, protected Repository\Venta $ventaRepository, protected Repository\Venta\Unidad $unidadRepository, protected Repository\Venta\TipoUnidad $tipoUnidadRepository) {}
|
||||
|
||||
public function query(string $query, string $tipo): array
|
||||
{
|
||||
if ($tipo === '*') {
|
||||
$results = $this->findCualquiera($query);
|
||||
} else {
|
||||
$results = $this->find($query, $tipo);
|
||||
}
|
||||
return $this->sort($results);
|
||||
}
|
||||
|
||||
protected function findCualquiera(string $query): array
|
||||
{
|
||||
$tipos = [
|
||||
'departamento',
|
||||
'estacionamiento',
|
||||
'bodega',
|
||||
'propietario',
|
||||
'precio_venta',
|
||||
'proyecto',
|
||||
'pago',
|
||||
'unidad'
|
||||
];
|
||||
$results = [];
|
||||
foreach ($tipos as $t) {
|
||||
$this->add($results, $this->find($query, $t));
|
||||
}
|
||||
return $results;
|
||||
}
|
||||
protected function find(string $query, string $tipo): array
|
||||
{
|
||||
$queries = explode(' ', $query);
|
||||
$tiposUnidades = $this->getTiposUnidades();
|
||||
$results = [];
|
||||
foreach ($queries as $q) {
|
||||
$this->add($results, $this->findVentas($q, $tipo));
|
||||
if (in_array($tipo, $tiposUnidades)) {
|
||||
$this->add($results, $this->findUnidadesDisponibles($q, $tipo));
|
||||
}
|
||||
}
|
||||
return $results;
|
||||
}
|
||||
protected function findVentas(string $query, string $tipo): array
|
||||
{
|
||||
$tiposUnidades = $this->getTiposUnidades();
|
||||
if ($tipo === 'unidad') {
|
||||
$results = [];
|
||||
foreach ($tiposUnidades as $t) {
|
||||
$this->add($results, $this->findVentas($query, $t));
|
||||
}
|
||||
return $results;
|
||||
}
|
||||
if (in_array($tipo, $tiposUnidades)) {
|
||||
return $this->findUnidad($query, $tipo);
|
||||
}
|
||||
if ($tipo === 'propietario') {
|
||||
return $this->findPropietario($query);
|
||||
}
|
||||
if ($tipo === 'precio_venta') {
|
||||
return $this->findPrecio($query);
|
||||
}
|
||||
if ($tipo === 'proyecto') {
|
||||
return $this->findProyecto($query);
|
||||
}
|
||||
if ($tipo === 'pago') {
|
||||
return $this->findPago($query);
|
||||
}
|
||||
return [];
|
||||
}
|
||||
protected function findUnidadesDisponibles(string $query, string $tipo): array
|
||||
{
|
||||
try {
|
||||
return $this->unidadRepository->fetchDisponiblesByDescripcionAndTipo($query, $tipo);
|
||||
} catch (EmptyResponse) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
protected function findUnidad(string $query, string $tipo): array
|
||||
{
|
||||
try {
|
||||
return $this->ventaService->getByUnidad($query, $tipo);
|
||||
} catch (EmptyResult) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
protected function findPropietario(string $query): array
|
||||
{
|
||||
try {
|
||||
return $this->ventaService->getByPropietario($query);
|
||||
} catch (EmptyResult) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
protected function findPrecio(string $query): array
|
||||
{
|
||||
try {
|
||||
$precio = str_replace(['$', '.', ','], ['', '', '.'], $query);
|
||||
return $this->ventaService->getByPrecio($precio);
|
||||
} catch (EmptyResult) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
protected function findProyecto(string $query): array
|
||||
{
|
||||
try {
|
||||
return $this->ventaService->getByProyecto($query);
|
||||
} catch (EmptyResult) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
protected function findPago(string $query): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
protected array $tipos;
|
||||
protected function getTiposUnidades(): array
|
||||
{
|
||||
if (!isset($this->tipos)) {
|
||||
$this->tipos = array_map(function(Model\Venta\TipoUnidad $tipoUnidad) {
|
||||
return $tipoUnidad->descripcion;
|
||||
}, $this->tipoUnidadRepository->fetchAll());
|
||||
}
|
||||
return $this->tipos;
|
||||
}
|
||||
protected function add(array &$results, array $found): void
|
||||
{
|
||||
foreach ($found as $item) {
|
||||
if (!$this->inResults($item, $results)) {
|
||||
$results []= $item;
|
||||
}
|
||||
}
|
||||
}
|
||||
protected function inResults($item, array $results): bool
|
||||
{
|
||||
foreach ($results as $result) {
|
||||
if (get_class($item) === get_class($result) and $item->id === $result->id) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
protected function sort(&$results): array
|
||||
{
|
||||
usort($results, function($a, $b) {
|
||||
if (is_a($a, Model\Venta::class)) {
|
||||
$pa = $a->proyecto()->descripcion;
|
||||
$ta = $a->propiedad()->departamentos()[0]->proyectoTipoUnidad->tipoUnidad->descripcion;
|
||||
$ua = $a->propiedad()->departamentos()[0]->descripcion;
|
||||
} else {
|
||||
$pa = $a->proyectoTipoUnidad->proyecto->descripcion;
|
||||
$ta = $a->proyectoTipoUnidad->tipoUnidad->descripcion;
|
||||
$ua = $a->descripcion;
|
||||
}
|
||||
if (is_a($b, Model\Venta::class)) {
|
||||
$pb = $b->proyecto()->descripcion;
|
||||
$tb = $b->propiedad()->departamentos()[0]->proyectoTipoUnidad->tipoUnidad->descripcion;
|
||||
$ub = $b->propiedad()->departamentos()[0]->descripcion;
|
||||
} else {
|
||||
$pb = $b->proyectoTipoUnidad->proyecto->descripcion;
|
||||
$tb = $b->proyectoTipoUnidad->tipoUnidad->descripcion;
|
||||
$ub = $b->descripcion;
|
||||
}
|
||||
$p = strcmp($pa, $pb);
|
||||
if ($p !== 0) {
|
||||
return $p;
|
||||
}
|
||||
$t = strcmp($ta, $tb);
|
||||
if ($t !== 0) {
|
||||
return $t;
|
||||
}
|
||||
return strcmp($ua, $ub);
|
||||
});
|
||||
return $results;
|
||||
}
|
||||
}
|
@ -23,26 +23,56 @@ class Venta
|
||||
|
||||
public function getById(int $venta_id): Model\Venta
|
||||
{
|
||||
return ($this->ventaRepository->fetchById($venta_id))
|
||||
->addFactory('estados', (new Implement\Repository\Factory())
|
||||
->setCallable([$this->estadoVentaRepository, 'fetchByVenta'])
|
||||
->setArgs([$venta_id]))
|
||||
->addFactory('currentEstado', (new Implement\Repository\Factory())
|
||||
->setCallable([$this->estadoVentaRepository, 'fetchCurrentByVenta'])
|
||||
->setArgs([$venta_id]));
|
||||
return $this->process($this->ventaRepository->fetchById($venta_id));
|
||||
}
|
||||
public function getByProyecto(int $proyecto_id): array
|
||||
{
|
||||
$ventas = $this->ventaRepository->fetchByProyecto($proyecto_id);
|
||||
foreach ($ventas as &$venta) {
|
||||
$venta->estados = $this->estadoVentaRepository->fetchByVenta($venta->id);
|
||||
$venta->currentEstado = $this->estadoVentaRepository->fetchCurrentByVenta($venta->id);
|
||||
$venta = $this->process($venta);
|
||||
}
|
||||
return $ventas;
|
||||
}
|
||||
public function getActivaByProyecto(int $proyecto_id): array
|
||||
{
|
||||
$ventas = $this->ventaRepository->fetchActivaByProyecto($proyecto_id);
|
||||
foreach ($ventas as &$venta) {
|
||||
$venta = $this->process($venta);
|
||||
}
|
||||
return $ventas;
|
||||
}
|
||||
public function getByProyectoAndUnidad(string $proyecto_nombre, int $unidad_descripcion): Model\Venta
|
||||
{
|
||||
$venta = $this->ventaRepository->fetchByProyectoAndUnidad($proyecto_nombre, $unidad_descripcion);
|
||||
return $this->process($venta);
|
||||
}
|
||||
public function getByUnidad(string $unidad, string $tipo): array
|
||||
{
|
||||
$ventas = $this->ventaRepository->fetchByUnidad($unidad, $tipo);
|
||||
foreach ($ventas as &$venta) {
|
||||
$venta = $this->process($venta);
|
||||
}
|
||||
return $ventas;
|
||||
}
|
||||
public function getByPropietario(string $propietario): array
|
||||
{
|
||||
$ventas = $this->ventaRepository->fetchByPropietario($propietario);
|
||||
foreach ($ventas as &$venta) {
|
||||
$venta = $this->process($venta);
|
||||
}
|
||||
return $ventas;
|
||||
}
|
||||
public function getByPrecio(string $precio): array
|
||||
{
|
||||
$ventas = $this->ventaRepository->fetchByPrecio($precio);
|
||||
foreach ($ventas as &$venta) {
|
||||
$venta = $this->process($venta);
|
||||
}
|
||||
return $ventas;
|
||||
}
|
||||
|
||||
protected function process(Model\Venta $venta): Model\Venta
|
||||
{
|
||||
$venta->addFactory('estados', (new Implement\Repository\Factory())->setCallable([$this->estadoVentaRepository, 'fetchByVenta'])->setArgs([$venta->id]));
|
||||
$venta->addFactory('currentEstado', (new Implement\Repository\Factory())->setCallable([$this->estadoVentaRepository, 'fetchCurrentByVenta'])->setArgs([$venta->id]));
|
||||
return $venta;
|
||||
|
Reference in New Issue
Block a user