Search
This commit is contained in:
7
app/resources/routes/97_search.php
Normal file
7
app/resources/routes/97_search.php
Normal file
@ -0,0 +1,7 @@
|
||||
<?php
|
||||
use Incoviba\Controller\Search;
|
||||
|
||||
$app->group('/search', function($app) {
|
||||
$app->get('[/{query}[/{tipo}[/]]]', Search::class);
|
||||
$app->post('[/]', Search::class);
|
||||
});
|
4
app/resources/routes/api/search.php
Normal file
4
app/resources/routes/api/search.php
Normal file
@ -0,0 +1,4 @@
|
||||
<?php
|
||||
use Incoviba\Controller\Search;
|
||||
|
||||
$app->post('/search', [Search::class, 'query']);
|
220
app/resources/views/search.blade.php
Normal file
220
app/resources/views/search.blade.php
Normal file
@ -0,0 +1,220 @@
|
||||
@extends('layout.base')
|
||||
|
||||
@section('page_content')
|
||||
<div class="ui container">
|
||||
<h1>Búsqueda</h1>
|
||||
|
||||
<form id="search_form" class="ui form" action="{{$urls->base}}/search" method="post">
|
||||
<div class="field">
|
||||
<div class="ui fluid input">
|
||||
<input type="text" name="query" />
|
||||
</div>
|
||||
</div>
|
||||
<div class="ui search selection dropdown" id="tipo">
|
||||
<input type="hidden" name="tipo" />
|
||||
<i class="dropdown icon"></i>
|
||||
<div class="default text">Tipo</div>
|
||||
<div class="menu">
|
||||
<div class="item" data-value="*" data-selected="true">Cualquiera</div>
|
||||
@foreach (['departamento', 'estacionamiento', 'bodega', 'propietario', 'precio_venta', 'proyecto', 'pago', 'unidad'] as $value)
|
||||
<div class="item" data-value="{{$value}}">{{ucwords(str_replace('_', ' ', $value))}}</div>
|
||||
@endforeach
|
||||
</div>
|
||||
</div>
|
||||
<button class="ui button" type="submit">Buscar</button>
|
||||
</form>
|
||||
<div id="results"></div>
|
||||
</div>
|
||||
@endsection
|
||||
|
||||
@include('layout.head.styles.datatables')
|
||||
@include('layout.body.scripts.datatables')
|
||||
|
||||
@push('page_scripts')
|
||||
<script type="text/javascript">
|
||||
class Row
|
||||
{
|
||||
proyecto
|
||||
unidad
|
||||
venta
|
||||
|
||||
constructor({proyecto, unidad}) {
|
||||
this.proyecto = proyecto
|
||||
this.unidad = unidad
|
||||
}
|
||||
draw() {
|
||||
const tipo = this.unidad.proyecto_tipo_unidad.tipo_unidad.descripcion
|
||||
let unidad = tipo.charAt(0).toUpperCase() + tipo.slice(1) + ' ' + this.unidad.descripcion
|
||||
let propietario = ''
|
||||
let fecha = ''
|
||||
let fecha_entrega = ''
|
||||
if (typeof this.venta !== 'undefined') {
|
||||
const dateFormatter = new Intl.DateTimeFormat('es-CL', {dateStyle: 'medium'})
|
||||
unidad = $('<a></a>').attr('href', '{{$urls->base}}/venta/' + this.venta.id).html(unidad)
|
||||
if (!this.venta.current_estado.tipo_estado_venta.activa) {
|
||||
unidad.html(unidad.html() + ' (I)')
|
||||
}
|
||||
propietario = $('<a></a>')
|
||||
.attr('href','{{$urls->base}}/search/' + encodeURIComponent(this.venta.propietario.nombre_completo) + '/propietario')
|
||||
.html(this.venta.propietario.nombre_completo)
|
||||
fecha = dateFormatter.format(new Date(this.venta.fecha))
|
||||
if (typeof this.venta.entrega !== 'undefined') {
|
||||
fecha_entrega = dateFormatter.format(new Date(this.venta.entrega.fecha))
|
||||
}
|
||||
} else {
|
||||
unidad += '<i class="ban icon"></i>'
|
||||
}
|
||||
|
||||
return $('<tr></tr>').append(
|
||||
$('<td></td>').append(
|
||||
$('<a></a>').attr('href', '{{$urls->base}}/proyecto/' + this.proyecto.id).html(this.proyecto.descripcion)
|
||||
)
|
||||
).append(
|
||||
$('<td></td>').append(unidad)
|
||||
).append(
|
||||
$('<td></td>').append(propietario)
|
||||
).append(
|
||||
$('<td></td>').addClass('right aligned').html(Math.round(this.unidad.proyecto_tipo_unidad.superficie * 100) / 100 + ' m²')
|
||||
).append(
|
||||
$('<td></td>').addClass('right aligned').html(this.unidad.precio)
|
||||
).append(
|
||||
$('<td></td>').html(fecha)
|
||||
).append(
|
||||
$('<td></td>').html(fecha_entrega)
|
||||
)
|
||||
}
|
||||
}
|
||||
const results = {
|
||||
id: '',
|
||||
data: [],
|
||||
table: null,
|
||||
get: function() {
|
||||
return {
|
||||
results: () => {
|
||||
if ($("[name='query']").val().length < 1) {
|
||||
return
|
||||
}
|
||||
this.draw().loading()
|
||||
const data = new FormData(document.getElementById('search_form'))
|
||||
const uri = '{{$urls->api}}/search'
|
||||
this.data = []
|
||||
return fetch(uri, {method: 'post', body: data}).then(response => {
|
||||
this.draw().clear()
|
||||
if (response.ok) {
|
||||
return response.json()
|
||||
}
|
||||
}).then(data => {
|
||||
if (typeof data.results !== 'undefined' && data.results.length > 0) {
|
||||
data.results.forEach(row => {
|
||||
if (typeof row.proyecto_tipo_unidad === 'undefined') {
|
||||
const r = new Row({unidad: row.propiedad.departamentos[0], proyecto: row.proyecto})
|
||||
r.venta = row
|
||||
this.data.push(r)
|
||||
} else {
|
||||
this.data.push(new Row({unidad: row, proyecto: row.proyecto_tipo_unidad.proyecto}))
|
||||
}
|
||||
})
|
||||
this.draw().table()
|
||||
return
|
||||
}
|
||||
this.draw().empty()
|
||||
})
|
||||
}
|
||||
}
|
||||
},
|
||||
draw: function() {
|
||||
return {
|
||||
clear: () => {
|
||||
$(this.id).html('')
|
||||
},
|
||||
separator: () => {
|
||||
this.draw().clear()
|
||||
$(this.id).append(
|
||||
$('<div></div>').addClass('ui horizontal divider').html('Resultados')
|
||||
)
|
||||
},
|
||||
loading: () => {
|
||||
this.draw().separator()
|
||||
$(this.id).append(
|
||||
$('<div></div>').addClass('ui active centered inline loader')
|
||||
)
|
||||
},
|
||||
table: () => {
|
||||
const parent = $(this.id)
|
||||
this.draw().separator()
|
||||
|
||||
if (this.table !== null) {
|
||||
this.table.clear()
|
||||
.draw()
|
||||
.destroy()
|
||||
this.table = null
|
||||
}
|
||||
|
||||
const table = $('<table></table>').addClass('ui table')
|
||||
const thead = this.draw().head()
|
||||
const tbody = $('<tbody></tbody>')
|
||||
|
||||
this.data.forEach(row => {
|
||||
tbody.append(row.draw())
|
||||
})
|
||||
table.append(thead).append(tbody)
|
||||
parent.append(table)
|
||||
|
||||
this.table = new DataTable(table)
|
||||
},
|
||||
head: () => {
|
||||
return $('<thead></thead>').append(
|
||||
$('<tr></tr>').append(
|
||||
$('<th></th>').html('Proyecto')
|
||||
).append(
|
||||
$('<th></th>').html('Unidad')
|
||||
).append(
|
||||
$('<th></th>').html('Propietario')
|
||||
).append(
|
||||
$('<th></th>').html('Superficie')
|
||||
).append(
|
||||
$('<th></th>').html('Valor')
|
||||
).append(
|
||||
$('<th></th>').html('Fecha Venta')
|
||||
).append(
|
||||
$('<th></th>').html('Fecha Entrega')
|
||||
)
|
||||
)
|
||||
},
|
||||
empty: () => {
|
||||
this.draw().separator()
|
||||
$(this.id).append(
|
||||
$('<div></div>').addClass('ui icon info message').append(
|
||||
$('<i></i>').addClass('meh outline icon')
|
||||
).append(
|
||||
$('<div></div>').addClass('content').html('No se han encontrado resultados.')
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
},
|
||||
setup: function(id) {
|
||||
this.id = id
|
||||
this.get().results()
|
||||
|
||||
$('#search_form').submit(event => {
|
||||
event.preventDefault()
|
||||
this.get().results()
|
||||
return false
|
||||
})
|
||||
}
|
||||
}
|
||||
$(document).ready(() => {
|
||||
$('#tipo').dropdown().dropdown('set selected', '*')
|
||||
@if (trim($post) !== '')
|
||||
$("[name='query']").val('{{$post}}')
|
||||
@elseif (trim($query) !== '')
|
||||
$("[name='query']").val('{{$query}}')
|
||||
@endif
|
||||
@if (trim($tipo) !== '')
|
||||
$('#tipo').dropdown('set selected', '{{$tipo}}')
|
||||
@endif
|
||||
results.setup('#results')
|
||||
})
|
||||
</script>
|
||||
@endpush
|
@ -19,7 +19,7 @@
|
||||
<td class="right aligned">
|
||||
{{$format->pesos($credito->pago->valor)}}
|
||||
</td>
|
||||
<td id="credito_pago" class="{{$credito->pago->currentEstado->tipoEstadoPago->descripcion === 'no pagado' ? 'warning' : ($credito->pago->currentEstado->tipoEstado->descripcion === 'depositado' ? 'positive' : '')}}">
|
||||
<td id="credito_pago" class="{{$credito->pago->currentEstado->tipoEstadoPago->descripcion === 'no pagado' ? 'warning' : ($credito->pago->currentEstado->tipoEstadoPago->descripcion === 'depositado' ? 'positive' : '')}}">
|
||||
<span class="text">{{$credito->pago->currentEstado->fecha->format('d-m-Y')}}</span>
|
||||
@if ($credito->pago->currentEstado->tipoEstadoPago->descripcion === 'no pagado')
|
||||
<a href="javascript: depositar({row_id: '#credito_pago', pago_id: {{$credito->pago->id}}});" title="Depositar">
|
||||
|
@ -9,7 +9,7 @@
|
||||
</a>
|
||||
{{$venta->propietario()->rut()}}
|
||||
<div class="meta">
|
||||
{{$venta->propietario()->datos->direccion}}
|
||||
{{$venta->propietario()->datos->direccion ?? ''}}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
@ -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