Compare commits
4 Commits
993e4ff3b8
...
fc776e6cec
Author | SHA1 | Date | |
---|---|---|---|
fc776e6cec | |||
5d79ea83c3 | |||
c34048a53a | |||
f7af93b815 |
@ -21,6 +21,7 @@ $app->group('/ventas', function($app) {
|
||||
});
|
||||
$app->group('/by', function($app) {
|
||||
$app->get('/unidad/{unidad_id}', [Ventas::class, 'unidad']);
|
||||
$app->post('/unidades[/]', [Ventas::class, 'byUnidades']);
|
||||
});
|
||||
$app->post('/get[/]', [Ventas::class, 'getMany']);
|
||||
$app->post('[/]', [Ventas::class, 'proyecto']);
|
||||
|
@ -20,7 +20,7 @@
|
||||
prices(units) {
|
||||
const prices = []
|
||||
units.forEach(unit => {
|
||||
let price = unit.precio?.valor ?? 0
|
||||
let price = unit.valor ?? (unit.precio?.valor ?? 0)
|
||||
let amount = 1
|
||||
let diff = 0
|
||||
unit.promotions?.forEach(promotion => {
|
||||
@ -71,7 +71,9 @@
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="ui active inline loader"></div>
|
||||
<div class="ui very basic segment">
|
||||
<div class="ui active inline loader"></div>
|
||||
</div>
|
||||
<div id="results">
|
||||
<div class="ui top attached tabular menu">
|
||||
<a class="item active" data-tab="tipos">Tipos</a>
|
||||
@ -155,10 +157,11 @@
|
||||
return Promise.all(promises)
|
||||
},
|
||||
prices: () => {
|
||||
const unsold = [...units.data.units.filter(unit => !unit.sold), ...units.data.units.filter(unit => unit.sold && unit.proyecto_tipo_unidad.tipo_unidad.descripcion !== 'departamento')]
|
||||
const chunkSize = 100
|
||||
const chunks = []
|
||||
for (let i = 0; i < units.data.units.length; i += chunkSize) {
|
||||
chunks.push(units.data.units.slice(i, i + chunkSize).map(u => u.id))
|
||||
for (let i = 0; i < unsold.length; i += chunkSize) {
|
||||
chunks.push(unsold.slice(i, i + chunkSize).map(u => u.id))
|
||||
}
|
||||
const promises = []
|
||||
const url = `{{ $urls->api }}/proyecto/{{ $contract->project->id }}/unidades/precios`
|
||||
@ -178,6 +181,51 @@
|
||||
})
|
||||
return Promise.all(promises)
|
||||
},
|
||||
values: () => {
|
||||
const sold = units.data.units.filter(unit => unit.sold && unit.proyecto_tipo_unidad.tipo_unidad.descripcion === 'departamento')
|
||||
const chunkSize = 10
|
||||
const chunks = []
|
||||
for (let i = 0; i < sold.length; i += chunkSize) {
|
||||
chunks.push(sold.slice(i, i + chunkSize).map(u => u.id))
|
||||
}
|
||||
const promises = []
|
||||
const url = `{{ $urls->api }}/ventas/by/unidades`
|
||||
const method = 'post'
|
||||
chunks.forEach(chunk => {
|
||||
const body = new FormData()
|
||||
chunk.forEach(id => body.append('unidad_ids[]', id))
|
||||
promises.push(APIClient.fetch(url, {method, body}).then(response => response.json()).then(json => {
|
||||
if (json.ventas.length === 0) {
|
||||
return
|
||||
}
|
||||
json.ventas.forEach(({unidad_id, venta}) => {
|
||||
const unidades = venta.propiedad.unidades
|
||||
const otras_unidades = unidades.filter(unit => unit.id !== parseInt(unidad_id) && unit.proyecto_tipo_unidad.tipo_unidad.descripcion !== 'departamento')
|
||||
const departamentos = unidades.filter(unit => unit.proyecto_tipo_unidad.tipo_unidad.descripcion === 'departamento' && unit.id !== parseInt(unidad_id))
|
||||
const precios = otras_unidades.map(unit => {
|
||||
const idx = units.data.units.findIndex(u => u.id === unit.id)
|
||||
return units.data.units[idx].precio?.valor ?? 0
|
||||
}).reduce((sum, precio) => sum + precio, 0)
|
||||
if (departamentos.length === 0) {
|
||||
const idx = units.data.units.findIndex(unit => unit.id === parseInt(unidad_id))
|
||||
units.data.units[idx].valor = venta.valor - precios
|
||||
units.data.units[idx].venta = venta
|
||||
return
|
||||
}
|
||||
const sum_precios = departamentos.map(departamento => {
|
||||
return departamento.current_precio.valor
|
||||
}).reduce((sum, precio) => sum + precio, 0)
|
||||
departamentos.forEach(departamento => {
|
||||
const idx = units.data.units.findIndex(unit => unit.id === departamento.id)
|
||||
const saldo = venta.valor - precios
|
||||
units.data.units[idx].valor = saldo / sum_precios * departamento.current_precio.valor
|
||||
units.data.units[idx].venta = venta
|
||||
})
|
||||
})
|
||||
}))
|
||||
})
|
||||
return Promise.all(promises)
|
||||
},
|
||||
sold: () => {
|
||||
const chunkSize = 100
|
||||
const chunks = []
|
||||
@ -236,16 +284,23 @@
|
||||
document.getElementById(units.ids.results).style.visibility = 'hidden'
|
||||
|
||||
units.get().units().then(() => {
|
||||
units.get().prices().then(() => {
|
||||
units.get().promotions().then(() => {
|
||||
units.get().sold().then(() => {
|
||||
$(units.ids.loader).hide()
|
||||
|
||||
units.get().promotions().then(() => {
|
||||
units.get().sold().then(() => {
|
||||
units.get().prices().then(() => {
|
||||
document.getElementById(units.ids.results).style.visibility = 'visible'
|
||||
|
||||
units.draw().units()
|
||||
units.draw().tipos()
|
||||
units.draw().lineas()
|
||||
|
||||
units.get().values().then(() => {
|
||||
$(units.ids.loader).hide()
|
||||
$(units.ids.loader).parent().hide()
|
||||
|
||||
units.draw().units()
|
||||
units.draw().tipos()
|
||||
units.draw().lineas()
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
@ -118,10 +118,10 @@
|
||||
const tipo = unidad.proyecto_tipo_unidad.tipo_unidad.descripcion
|
||||
const price = prices.find(p => p.id === unidad.id)
|
||||
tableData.push([
|
||||
unidad.sold ? '<span class="ui yellow text">Vendida</span>' : 'Libre',
|
||||
unidad.sold ? `<a href="{{ $urls->base }}/venta/${unidad.venta?.id }" class="ui yellow text">Vendida</a>` : 'Libre',
|
||||
tipo.charAt(0).toUpperCase() + tipo.slice(1),
|
||||
unidad.proyecto_tipo_unidad.tipo_unidad.orden,
|
||||
unidad.descripcion,
|
||||
unidad.sold ? `<span data-tooltip="Valor Promesa: UF ${formatters.ufs.format(unidad.venta?.valor ?? 0)}" data-position="right center">${unidad.descripcion}</span>` : unidad.descripcion,
|
||||
unidad.descripcion.padStart(4, '0'),
|
||||
unidad.proyecto_tipo_unidad.tipologia,
|
||||
unidad.piso,
|
||||
|
@ -9,6 +9,7 @@ use Incoviba\Common\Ideal\Controller;
|
||||
use Incoviba\Common\Implement\Exception\EmptyRedis;
|
||||
use Incoviba\Common\Implement\Exception\EmptyResult;
|
||||
use Incoviba\Controller\withRedis;
|
||||
use Incoviba\Exception\ServiceAction\Read;
|
||||
use Incoviba\Model;
|
||||
use Incoviba\Repository;
|
||||
use Incoviba\Service;
|
||||
@ -368,4 +369,23 @@ class Ventas extends Controller
|
||||
} catch (EmptyResult) {}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function byUnidades(ServerRequestInterface $request, ResponseInterface $response,
|
||||
Service\Venta $ventaService, Service\Venta\Unidad $unidadService): ResponseInterface
|
||||
{
|
||||
$input = $request->getParsedBody();
|
||||
$output = [
|
||||
'input' => $input,
|
||||
'ventas' => []
|
||||
];
|
||||
foreach ($input['unidad_ids'] as $unidad_id) {
|
||||
try {
|
||||
$venta = $ventaService->getByUnidadId($unidad_id);
|
||||
$output['ventas'][] = [
|
||||
'unidad_id' => $unidad_id,
|
||||
'venta' => $venta
|
||||
];
|
||||
} catch (Read) {}
|
||||
}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
}
|
||||
|
@ -69,14 +69,35 @@ class Venta extends Service
|
||||
$venta = $this->ventaRepository->fetchByProyectoAndUnidad($proyecto_nombre, $unidad_descripcion);
|
||||
return $this->process($venta);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $unidad
|
||||
* @param string $tipo
|
||||
* @return array
|
||||
* @throws Read
|
||||
*/
|
||||
public function getByUnidad(string $unidad, string $tipo): array
|
||||
{
|
||||
$ventas = $this->ventaRepository->fetchByUnidad($unidad, $tipo);
|
||||
return array_map([$this, 'process'], $ventas);
|
||||
try {
|
||||
$ventas = $this->ventaRepository->fetchByUnidad($unidad, $tipo);
|
||||
return array_map([$this, 'process'], $ventas);
|
||||
} catch (Implement\Exception\EmptyResult $exception) {
|
||||
throw new Read(__CLASS__, $exception);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $unidad_id
|
||||
* @return Model\Venta
|
||||
* @throws Read
|
||||
*/
|
||||
public function getByUnidadId(int $unidad_id): Model\Venta
|
||||
{
|
||||
return $this->process($this->ventaRepository->fetchByUnidadId($unidad_id));
|
||||
try {
|
||||
return $this->process($this->ventaRepository->fetchByUnidadId($unidad_id));
|
||||
} catch (Implement\Exception\EmptyResult $exception) {
|
||||
throw new Read(__CLASS__, $exception);
|
||||
}
|
||||
}
|
||||
public function getByPropietario(string $propietario): array
|
||||
{
|
||||
|
@ -16,9 +16,18 @@ class Unidad
|
||||
protected Precio $precioService
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @param int $unidad_id
|
||||
* @return Model\Venta\Unidad
|
||||
* @throws Read
|
||||
*/
|
||||
public function getById(int $unidad_id): Model\Venta\Unidad
|
||||
{
|
||||
return $this->process($this->unidadRepository->fetchById($unidad_id));
|
||||
try {
|
||||
return $this->process($this->unidadRepository->fetchById($unidad_id));
|
||||
} catch (EmptyResult $exception) {
|
||||
throw new Read(__CLASS__, $exception);
|
||||
}
|
||||
}
|
||||
public function getByVenta(int $venta_id): array
|
||||
{
|
||||
|
Reference in New Issue
Block a user