Accelerar listado ventas
This commit is contained in:
@ -22,6 +22,7 @@ $app->group('/ventas', function($app) {
|
||||
$app->group('/by', function($app) {
|
||||
$app->get('/unidad/{unidad_id}', [Ventas::class, 'unidad']);
|
||||
});
|
||||
$app->post('/get[/]', [Ventas::class, 'getMany']);
|
||||
$app->post('[/]', [Ventas::class, 'proyecto']);
|
||||
});
|
||||
$app->group('/venta/{venta_id}', function($app) {
|
||||
|
@ -116,10 +116,15 @@
|
||||
this.data.id = data.proyecto.id
|
||||
this.data.proyecto = data.proyecto.descripcion
|
||||
this.data.venta_ids = data.ventas
|
||||
const chunkSize = 50
|
||||
const chunks = []
|
||||
for (let i = 0; i < data.ventas.length; i += chunkSize) {
|
||||
chunks.push(data.ventas.splice(i, i + chunkSize))
|
||||
}
|
||||
const promises = []
|
||||
data.ventas.forEach(venta_id => {
|
||||
const promise = this.get().venta(venta_id).then(() => {
|
||||
progress.progress('increment')
|
||||
chunks.forEach(chunk => {
|
||||
const promise = this.get().venta(chunk).then(count => {
|
||||
progress.progress('increment', count)
|
||||
})
|
||||
promises.push(promise)
|
||||
})
|
||||
@ -129,17 +134,22 @@
|
||||
}
|
||||
})
|
||||
},
|
||||
venta: venta_id => {
|
||||
return fetchAPI('{{$urls->api}}/venta/' + venta_id).then(response => {
|
||||
venta: chunk => {
|
||||
const body = new FormData()
|
||||
body.set('ventas', chunk.join(','))
|
||||
return fetchAPI('{{$urls->api}}/ventas/get', {method: 'post', body}).then(response => {
|
||||
if (response.ok) {
|
||||
return response.json()
|
||||
}
|
||||
}).then(data => {
|
||||
if (typeof data.venta === 'undefined') {
|
||||
console.error(venta_id, data.error)
|
||||
if (data.ventas.length === 0) {
|
||||
console.error(chunk, data.error)
|
||||
return
|
||||
}
|
||||
this.add().venta(data.venta)
|
||||
data.ventas.forEach(venta_id => {
|
||||
this.add().venta(venta_id)
|
||||
})
|
||||
return data.ventas.length
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -74,6 +74,32 @@ class Ventas extends Controller
|
||||
}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function getMany(ServerRequestInterface $request, ResponseInterface $response, Service\Redis $redisService,
|
||||
Service\Venta $service): ResponseInterface
|
||||
{
|
||||
$body = $request->getParsedBody();
|
||||
$output = [
|
||||
'input' => $body,
|
||||
'ventas' => []
|
||||
];
|
||||
$ventas = explode(',', $body['ventas']);
|
||||
foreach ($ventas as $venta_id) {
|
||||
$redisKey = "venta:{$venta_id}";
|
||||
try {
|
||||
$venta = $this->fetchRedis($redisService, $redisKey);
|
||||
$output['ventas'] []= $venta;
|
||||
} catch (EmptyRedis) {
|
||||
try {
|
||||
$venta = $service->getById($venta_id);
|
||||
$output['ventas'] []= $venta;
|
||||
$this->saveRedis($redisService, $redisKey, $venta);
|
||||
} catch (EmptyResult $exception) {
|
||||
$this->logger->notice($exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function porFirmar(ServerRequestInterface $request, ResponseInterface $response, Service\Venta $ventaService, Service\Redis $redisService): ResponseInterface
|
||||
{
|
||||
$body = $request->getBody();
|
||||
|
@ -376,6 +376,27 @@ class Venta extends Ideal\Repository
|
||||
->group('venta.id');
|
||||
return $this->connection->execute($query, [$venta_id])->fetch(PDO::FETCH_ASSOC);
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
protected function fetchIds(string $query, ?array $data = null): array
|
||||
{
|
||||
|
@ -79,6 +79,10 @@ class Venta extends Service
|
||||
{
|
||||
return $this->ventaRepository->fetchByIdForSearch($venta_id);
|
||||
}
|
||||
public function getByIdForList(int $venta_id): array
|
||||
{
|
||||
return $this->ventaRepository->fetchByIdForList($venta_id);
|
||||
}
|
||||
|
||||
protected function process(Model\Venta $venta): Model\Venta
|
||||
{
|
||||
|
Reference in New Issue
Block a user