diff --git a/app/resources/views/proyectos/brokers/contracts/show.blade.php b/app/resources/views/proyectos/brokers/contracts/show.blade.php index 8222756..8c03aba 100644 --- a/app/resources/views/proyectos/brokers/contracts/show.blade.php +++ b/app/resources/views/proyectos/brokers/contracts/show.blade.php @@ -305,14 +305,14 @@ return Promise.all(promises) }, prices: progress_bar => { - 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 unsold = [...units.data.units.filter(unit => !unit.sold), ...units.data.units.filter(unit => unit.sold && unit.proyecto_tipo_unidad.tipo_unidad.descripcion !== 'departamento')] const current_total = progress_bar.progress('get total') - progress_bar.progress('set total', current_total + unsold.length) + progress_bar.progress('set total', current_total + unsold.length)*/ const chunkSize = 100 const chunks = [] - for (let i = 0; i < unsold.length; i += chunkSize) { - chunks.push(unsold.slice(i, i + chunkSize).map(u => u.id)) + for (let i = 0; i < units.data.units.length; i += chunkSize) { + chunks.push(units.data.units.slice(i, i + chunkSize).map(u => u.id)) } const promises = [] const url = `{{ $urls->api }}/proyecto/{{ $contract->project->id }}/unidades/precios` @@ -368,12 +368,13 @@ return } const sum_precios = departamentos.map(departamento => { - return departamento.valor + const idx = units.data.units.findIndex(unit => unit.id === departamento.id) + return units.data.units[idx].precio }).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.valor + units.data.units[idx].valor = saldo / sum_precios * departamento.precio units.data.units[idx].venta = venta }) }) @@ -448,7 +449,7 @@ const units_length = units.data.units.length const progress_bar = $(`#${units.ids.load_progress}`) - progress_bar.progress({ total: units_length * 2 }) + progress_bar.progress({ total: units_length * 3 }) loader.hide() diff --git a/app/src/Repository/Venta/Propiedad.php b/app/src/Repository/Venta/Propiedad.php index 745f3b4..230be0e 100644 --- a/app/src/Repository/Venta/Propiedad.php +++ b/app/src/Repository/Venta/Propiedad.php @@ -11,7 +11,9 @@ use Incoviba\Service; class Propiedad extends Ideal\Repository { - public function __construct(Define\Connection $connection, protected Service\Venta\PropiedadUnidad $propiedadUnidadService, protected Service\Venta\Unidad $unidadService) + public function __construct(Define\Connection $connection, + protected Service\Venta\PropiedadUnidad $propiedadUnidadService, + protected Service\Venta\Unidad $unidadService) { parent::__construct($connection); $this->setTable('propiedad'); @@ -71,4 +73,21 @@ class Propiedad extends Ideal\Repository ->where('`unidad_principal` = ? AND `estado` = 1'); return $this->fetchOne($query, [$unidad_id]); } + + /** + * @param int $propiedad_id + * @return array + * @throws Implement\Exception\EmptyResult + */ + public function fetchArrayById(int $propiedad_id): array + { + $query = $this->connection->getQueryBuilder() + ->select() + ->from($this->getTable()) + ->where('`id` = ?') + ->limit(1); + $propiedad = $this->fetchAsArray($query, [$propiedad_id])[0]; + $propiedad['unidades'] = $this->propiedadUnidadService->getArrayByPropiedad($propiedad_id); + return $propiedad; + } } diff --git a/app/src/Repository/Venta/PropiedadUnidad.php b/app/src/Repository/Venta/PropiedadUnidad.php index 3ef43bd..3bc3a67 100644 --- a/app/src/Repository/Venta/PropiedadUnidad.php +++ b/app/src/Repository/Venta/PropiedadUnidad.php @@ -104,6 +104,26 @@ class PropiedadUnidad extends Ideal\Repository return $this->fetchMany($query, [$propiedad_id]); } + /** + * @param int $propiedad_id + * @return array + * @throws Implement\Exception\EmptyResult + */ + public function fetchArrayByPropiedad(int $propiedad_id): array + { + $query = $this->connection->getQueryBuilder() + ->select('a.id AS pu_id, a.valor, a.propiedad, unidad.*') + ->from("{$this->getTable()} a") + ->joined('INNER JOIN `unidad` ON a.`unidad` = `unidad`.`id`') + ->where('a.`propiedad` = ?') + ->group('`unidad`.`id`'); + $unidades = $this->fetchAsArray($query, [$propiedad_id]); + array_walk($unidades, function(&$unidad) { + $unidad['proyecto_tipo_unidad'] = json_decode(json_encode($this->proyectoTipoUnidadService->getById($unidad['pt'])), true); + }); + return $unidades; + } + /** * @param Define\Model $model * @return void diff --git a/app/src/Service/Venta/Propiedad.php b/app/src/Service/Venta/Propiedad.php index eed57ce..d965e6c 100644 --- a/app/src/Service/Venta/Propiedad.php +++ b/app/src/Service/Venta/Propiedad.php @@ -44,7 +44,7 @@ class Propiedad extends Service public function getArrayById(int $propiedad_id): array { try { - return json_decode(json_encode($this->propiedadRepository->fetchById($propiedad_id)), true); + return $this->propiedadRepository->fetchArrayById($propiedad_id); } catch (EmptyResult $exception) { throw new Exception\ServiceAction\Read(__CLASS__, $exception); } diff --git a/app/src/Service/Venta/PropiedadUnidad.php b/app/src/Service/Venta/PropiedadUnidad.php index 59aa9ae..4fee5ee 100644 --- a/app/src/Service/Venta/PropiedadUnidad.php +++ b/app/src/Service/Venta/PropiedadUnidad.php @@ -42,6 +42,14 @@ class PropiedadUnidad return []; } } + public function getArrayByPropiedad(int $propiedad_id): array + { + try { + return $this->propiedadUnidadRepository->fetchArrayByPropiedad($propiedad_id); + } catch (EmptyResult) { + return []; + } + } /** * @param array $data