From 57579a52f143f241778c34a3fc3f5576a0ce28d8 Mon Sep 17 00:00:00 2001 From: Aldarien Date: Fri, 1 Dec 2023 15:00:25 -0300 Subject: [PATCH] Editar propiedad en venta --- .../api/ventas/propiedades/unidades.php | 4 ++ .../views/ventas/propiedades/edit.blade.php | 69 +++++++++++++++++-- .../API/Ventas/PropiedadesUnidades.php | 32 +++++++++ app/src/Model/Venta/PropiedadUnidad.php | 2 + app/src/Repository/Venta/PropiedadUnidad.php | 7 ++ app/src/Service/Venta/PropiedadUnidad.php | 21 +++++- 6 files changed, 127 insertions(+), 8 deletions(-) diff --git a/app/resources/routes/api/ventas/propiedades/unidades.php b/app/resources/routes/api/ventas/propiedades/unidades.php index d48884e..6cb7871 100644 --- a/app/resources/routes/api/ventas/propiedades/unidades.php +++ b/app/resources/routes/api/ventas/propiedades/unidades.php @@ -1,6 +1,10 @@ group('/unidades', function($app) { + $app->post('/add[/]', [PropiedadesUnidades::class, 'add']); +}); $app->group('/unidad/{pu_id}', function($app) { $app->post('/edit[/]', [PropiedadesUnidades::class, 'edit']); + $app->delete('[/]', [PropiedadesUnidades::class, 'remove']); }); diff --git a/app/resources/views/ventas/propiedades/edit.blade.php b/app/resources/views/ventas/propiedades/edit.blade.php index 00dc589..80ce28d 100644 --- a/app/resources/views/ventas/propiedades/edit.blade.php +++ b/app/resources/views/ventas/propiedades/edit.blade.php @@ -45,7 +45,7 @@ @endsection @@ -72,6 +78,7 @@ { id: {{$unidad->id}}, tipo: '{{ucwords($unidad->proyectoTipoUnidad->tipoUnidad->descripcion)}}', + tipo_id: {{$unidad->proyectoTipoUnidad->tipoUnidad->id}}, descripcion: '{{$unidad->descripcion}}', pid: {{$unidad->pu_id}}, valor: {{($unidad->valor > 0) ? $unidad->valor : $unidad->precio($venta->fecha)->valor}} @@ -99,8 +106,51 @@ addUnidad: function() { $('#add_modal').modal('show') }, + doAddUnidad: function() { + const url = '{{$urls->api}}/ventas/propiedades/unidades/add' + const data = new FormData(document.getElementById('add_form')) + data.set('propiedad', {{$propiedad->id}}) + return fetchAPI(url, {method: 'post', body: data}).then(response => { + if (response.ok) { + return response.json() + } + }).then(json => { + if (!json.added) { + return + } + const tipo = json.propiedad_unidad.proyecto_tipo_unidad.tipo_unidad.descripcion + this.unidades.push({ + id: json.propiedad_unidad.id, + tipo: tipo.charAt(0).toUpperCase() + tipo.slice(1), + tipo_id: json.propiedad_unidad.proyecto_tipo_unidad.tipo_unidad.id, + descripcion: json.propiedad_unidad.descripcion, + pid: json.propiedad_unidad.pu_id, + valor: parseFloat(json.propiedad_unidad.valor) + }) + this.draw() + const idx = this.tipos[json.propiedad_unidad.proyecto_tipo_unidad.tipo_unidad.id].findIndex(unidad => unidad.value === json.propiedad_unidad.id) + this.tipos[json.propiedad_unidad.proyecto_tipo_unidad.tipo_unidad.id].splice(idx,1) + }) + }, removeUnidad: function(unidad_id) { - console.debug(unidad_id) + const url = '{{$urls->api}}/ventas/propiedades/unidad/' + unidad_id + return fetchAPI(url, {method: 'delete'}).then(response => { + if (response.ok) { + return response.json() + } + }).then(json => { + if (!json.removed) { + return + } + const idx = this.unidades.findIndex(unidad => unidad.pid === json.propiedad_unidad_id) + const unidad = this.unidades.splice(idx,1)[0] + this.draw() + this.tipos[unidad.tipo_id].push({ + value: unidad.id, + text: unidad.descripcion, + name: unidad.descripcion + }) + }) }, updatePrecio: function(pid, valor) { const idx = this.unidades.findIndex(unidad => unidad.pid === pid) @@ -113,7 +163,7 @@ } const url = '{{$urls->api}}/ventas/propiedades/unidad/' + id + '/edit' const data = new FormData() - data.set('valor', value) + data.set('valor', valor) return fetchAPI(url, {method: 'post', body: data}).then(response => { if (response.ok) { return response.json() @@ -170,7 +220,11 @@ const unidad_id = $(event.currentTarget).data('pid') this.removeUnidad(unidad_id) }) - $('#add_modal').modal() + $('#add_modal').modal({ + onApprove: ($element) => { + this.doAddUnidad() + } + }) const tipo = $('#tipo') tipo.dropdown() tipo.change(event => { @@ -180,7 +234,8 @@ this.changeTipoUnidad(tipo.val()) $('#add_form').submit(event => { event.preventDefault() - tipo.model('hide') + this.doAddUnidad() + tipo.modal('hide') return false }) $('.precio').change(event => { diff --git a/app/src/Controller/API/Ventas/PropiedadesUnidades.php b/app/src/Controller/API/Ventas/PropiedadesUnidades.php index 9b658c4..74ba123 100644 --- a/app/src/Controller/API/Ventas/PropiedadesUnidades.php +++ b/app/src/Controller/API/Ventas/PropiedadesUnidades.php @@ -7,12 +7,30 @@ use Psr\Http\Message\ResponseInterface; use Incoviba\Controller\API\{withJson, emptyBody}; use Incoviba\Controller\withRedis; use Incoviba\Common\Implement\Exception\{EmptyResult,EmptyRedis}; +use Incoviba\Repository; use Incoviba\Service; class PropiedadesUnidades { use emptyBody, withJson, withRedis; + public function add(ServerRequestInterface $request, ResponseInterface $response, Service\Venta\PropiedadUnidad $propiedadUnidadService): ResponseInterface + { + $body = $request->getParsedBody(); + $output = [ + 'input' => $body, + 'propiedad_unidad' => null, + 'added' => false + ]; + try { + $pu = $propiedadUnidadService->add($body); + $output['propiedad_unidad'] = $pu; + $output['added'] = true; + } catch (EmptyResult $exception) { + error_log($exception); + } + return $this->withJson($response, $output); + } public function edit(ServerRequestInterface $request, ResponseInterface $response, Service\Venta\PropiedadUnidad $propiedadUnidadService, int $pu_id): ResponseInterface { @@ -29,4 +47,18 @@ class PropiedadesUnidades } catch (PDOException | EmptyResult) {} return $this->withJson($response, $output); } + public function remove(ServerRequestInterface $request, ResponseInterface $response, + Repository\Venta\PropiedadUnidad $propiedadUnidadRepository, int $pu_id): ResponseInterface + { + $output = [ + 'propiedad_unidad_id' => $pu_id, + 'removed' => false + ]; + try { + $pu = $propiedadUnidadRepository->fetchById($pu_id); + $propiedadUnidadRepository->remove($pu); + $output['removed'] = true; + } catch (EmptyResult) {} + return $this->withJson($response, $output); + } } diff --git a/app/src/Model/Venta/PropiedadUnidad.php b/app/src/Model/Venta/PropiedadUnidad.php index a1bbb3a..9545477 100644 --- a/app/src/Model/Venta/PropiedadUnidad.php +++ b/app/src/Model/Venta/PropiedadUnidad.php @@ -10,6 +10,8 @@ class PropiedadUnidad extends Unidad public function jsonSerialize(): mixed { return array_merge(parent::jsonSerialize(), [ + 'pu_id' => $this->pu_id, + 'propiedad_id' => $this->propiedad_id, 'valor' => $this->valor ]); } diff --git a/app/src/Repository/Venta/PropiedadUnidad.php b/app/src/Repository/Venta/PropiedadUnidad.php index 9402e5f..0febced 100644 --- a/app/src/Repository/Venta/PropiedadUnidad.php +++ b/app/src/Repository/Venta/PropiedadUnidad.php @@ -86,6 +86,13 @@ class PropiedadUnidad extends Ideal\Repository ->group('`unidad`.`id`'); return $this->fetchMany($query, [$propiedad_id]); } + public function remove(Define\Model $model): void + { + $query = $this->connection->getQueryBuilder() + ->delete()->from($this->getTable()) + ->where("id = ?"); + $this->connection->execute($query, [$model->pu_id]); + } protected function update(Define\Model $model, array $columns, array $data): Define\Model { diff --git a/app/src/Service/Venta/PropiedadUnidad.php b/app/src/Service/Venta/PropiedadUnidad.php index 441f5d7..29000ec 100644 --- a/app/src/Service/Venta/PropiedadUnidad.php +++ b/app/src/Service/Venta/PropiedadUnidad.php @@ -8,7 +8,9 @@ use Incoviba\Model; class PropiedadUnidad { - public function __construct(protected Repository\Venta\PropiedadUnidad $propiedadUnidadRepository, protected Precio $precioService) {} + public function __construct(protected Repository\Venta\PropiedadUnidad $propiedadUnidadRepository, + protected Repository\Venta\Unidad $unidadRepository, + protected Precio $precioService) {} public function getById(int $unidad_id): Model\Venta\PropiedadUnidad { @@ -22,6 +24,23 @@ class PropiedadUnidad { return array_map([$this, 'process'], $this->propiedadUnidadRepository->fetchByPropiedad($propiedad_id)); } + public function add(array $data): Model\Venta\PropiedadUnidad + { + $unidad = $this->unidadRepository->fetchById($data['unidad']); + $temp = json_decode(json_encode($unidad), JSON_OBJECT_AS_ARRAY); + $columnMap = [ + 'proyecto_tipo_unidad' => 'pt' + ]; + foreach ($temp as $key => $value) { + if (isset($columnMap[$key])) { + $temp[$columnMap[$key]] = $value['id']; + } + } + $temp['propiedad'] = $data['propiedad']; + $temp['valor'] = $data['valor']; + $pu = $this->propiedadUnidadRepository->create($temp); + return $this->process($this->propiedadUnidadRepository->save($pu)); + } public function edit(Model\Venta\PropiedadUnidad $propiedadUnidad, array $data): Model\Venta\PropiedadUnidad { return $this->process($this->propiedadUnidadRepository->edit($propiedadUnidad, $data));