diff --git a/app/resources/routes/api/ventas.php b/app/resources/routes/api/ventas.php index 9ff578f..1b9a936 100644 --- a/app/resources/routes/api/ventas.php +++ b/app/resources/routes/api/ventas.php @@ -39,6 +39,9 @@ $app->group('/venta/{venta_id}', function($app) { $app->get('/eliminar[/]', [Ventas::class, 'insistir']); $app->post('[/]', [Ventas::class, 'desistir']); }); + $app->group('/propietario', function($app) { + $app->put('/edit[/]', [Ventas::class, 'propietario']); + }); $app->post('[/]', [Ventas::class, 'edit']); $app->get('[/]', [Ventas::class, 'get']); }); diff --git a/app/resources/views/ventas/propietarios/edit.blade.php b/app/resources/views/ventas/propietarios/edit.blade.php index 24dad58..5fd9227 100644 --- a/app/resources/views/ventas/propietarios/edit.blade.php +++ b/app/resources/views/ventas/propietarios/edit.blade.php @@ -9,9 +9,12 @@ Editar Propietario

Editar Propietario

-
+
- {{$propietario->rut()}} +
+ +
-{{$propietario->dv}}
+
@@ -62,165 +65,460 @@ Editar Propietario @push('page_scripts') @endpush diff --git a/app/src/Controller/API/Direcciones.php b/app/src/Controller/API/Direcciones.php index f7527c9..1f59948 100644 --- a/app/src/Controller/API/Direcciones.php +++ b/app/src/Controller/API/Direcciones.php @@ -15,15 +15,30 @@ class Direcciones use withRedis, withJson; public function comunas(ServerRequestInterface $request, ResponseInterface $response, Service\Redis $redisService, - Repository\Provincia $provinciaRepository, Repository\Comuna $comunaRepository, int $region_id) : ResponseInterface + Repository\Region $regionRepository, Repository\Comuna $comunaRepository, + int $region_id) : ResponseInterface { $output = ['total' => 0, 'comunas' => []]; - $redisKey = 'comunas'; + $redisKey = "comunas:region:{$region_id}"; + try { $output['comunas'] = $this->fetchRedis($redisService, $redisKey); $output['total'] = count($output['comunas']); } catch (EmptyRedis) { - $provinciaKey = 'provincias'; + $regionKey = "regiones:{$region_id}"; + try { + $region = $this->fetchRedis($redisService, $regionKey); + } catch (EmptyRedis) { + $region = $regionRepository->fetchById($region_id); + $this->saveRedis($redisService, $regionKey, $region, 60 * 60 * 24 * 30); + } + $comunas = $comunaRepository->fetchByRegion($region->id); + usort($comunas, function(Model\Comuna $a, Model\Comuna $b) { + return strcoll($a->descripcion, $b->descripcion); + }); + $output = ['comunas' => $comunas, 'total' => count($comunas)]; + $this->saveRedis($redisService, $redisKey, $comunas, 60 * 60 * 24 * 30); + /*$provinciaKey = 'provincias'; try { $temp_provincias = $this->fetchRedis($redisService, $provinciaKey); } catch (EmptyRedis) { @@ -39,7 +54,7 @@ class Direcciones return strcoll($a->descripcion, $b->descripcion); }); $output = ['comunas' => $comunas, 'total' => count($comunas)]; - $this->saveRedis($redisService, $redisKey, $comunas, 60 * 60 * 24 * 30); + $this->saveRedis($redisService, $redisKey, $comunas, 60 * 60 * 24 * 30);*/ } return $this->withJson($response, $output); } diff --git a/app/src/Controller/API/Ventas.php b/app/src/Controller/API/Ventas.php index 94fc0c9..ef8fbc2 100644 --- a/app/src/Controller/API/Ventas.php +++ b/app/src/Controller/API/Ventas.php @@ -302,4 +302,39 @@ class Ventas extends Controller } catch (EmptyResult) {} return $this->withJson($response, $output); } + public function propietario(ServerRequestInterface $request, ResponseInterface $response, + Service\Venta\Propietario $propietarioService, + Repository\Venta $ventaRepository, int $venta_id): ResponseInterface + { + $body = $request->getBody(); + $data = json_decode($body->getContents(), true); + $this->logger->error(var_export($data, true)); + $output = [ + 'input' => $data, + 'venta_id' => $venta_id, + 'propietario' => false, + 'edited' => false + ]; + try { + $venta = $ventaRepository->fetchById($venta_id); + $propietario = $propietarioService->getByRut($venta->propietario()->rut); + $output['propietario'] = $propietario; + if (isset($data['rut'])) { + try { + $propietario = $propietarioService->getByRut($data['rut']); + $propietario = $propietarioService->edit($propietario, $data); + } catch (EmptyResult) { + $propietario = $propietarioService->addPropietario($data); + } + $data = [ + 'propietario' => $propietario->rut + ]; + $ventaRepository->edit($venta, $data); + } else { + $propietario = $propietarioService->edit($propietario, $data); + } + $output['edited'] = true; + } catch (EmptyResult) {} + return $this->withJson($response, $output); + } } diff --git a/app/src/Repository/Comuna.php b/app/src/Repository/Comuna.php index c7567ec..45101bf 100644 --- a/app/src/Repository/Comuna.php +++ b/app/src/Repository/Comuna.php @@ -37,20 +37,36 @@ class Comuna extends Ideal\Repository public function fetchByDescripcion(string $descripcion): Define\Model { - $query = "SELECT * FROM `{$this->getTable()}` WHERE `descripcion` = ?"; + $query = $this->connection->getQueryBuilder() + ->select() + ->from($this->getTable()) + ->where('descripcion = ?'); return $this->fetchOne($query, [$descripcion]); } public function fetchByProvincia(int $provincia_id): array { - $query = "SELECT * FROM `{$this->getTable()}` WHERE `provincia` = ?"; + $query = $this->connection->getQueryBuilder() + ->select() + ->from($this->getTable()) + ->where('provincia = ?'); return $this->fetchMany($query, [$provincia_id]); } + public function fetchByRegion(int $region_id): array + { + $query = $this->connection->getQueryBuilder() + ->select('c.*') + ->from("{$this->getTable()} c") + ->joined('JOIN provincia p ON c.provincia = p.id') + ->where('p.region = ?'); + return $this->fetchMany($query, [$region_id]); + } public function fetchByDireccion(string $direccion): array { - $query = "SELECT a.* -FROM `{$this->getTable()}` a - JOIN `direccion` ON `direccion`.`comuna` = a.`id` -WHERE TRIM(CONCAT_WS(' ', `direccion`.`calle`, `direccion`.`numero`, `direccion`.`extra`)) LIKE ?"; + $query = $this->connection->getQueryBuilder() + ->select('c.*') + ->from("{$this->getTable()} c") + ->joined('JOIN direccion d ON c.id = d.comuna') + ->where('TRIM(CONCAT_WS(" ", d.calle, d.numero, d.extra)) LIKE ?'); return $this->fetchMany($query, ["%{$direccion}%"]); } } diff --git a/app/src/Repository/Venta/Propietario.php b/app/src/Repository/Venta/Propietario.php index ff23398..3334ec0 100644 --- a/app/src/Repository/Venta/Propietario.php +++ b/app/src/Repository/Venta/Propietario.php @@ -74,4 +74,8 @@ class Propietario extends Ideal\Repository { return $this->update($model, ['dv', 'nombres', 'apellido_paterno', 'apellido_materno', 'direccion', 'otro', 'representante'], $new_data); } + public function filterData(array $data): array + { + return array_intersect_key($data, array_flip(['rut', 'dv', 'nombres', 'apellido_paterno', 'apellido_materno', 'direccion', 'representante'])); + } } diff --git a/app/src/Service/Venta/Propietario.php b/app/src/Service/Venta/Propietario.php index 68db0f8..4227292 100644 --- a/app/src/Service/Venta/Propietario.php +++ b/app/src/Service/Venta/Propietario.php @@ -17,6 +17,19 @@ class Propietario extends Service parent::__construct($logger); } + public function getByRut(int $rut): Model\Venta\Propietario + { + return $this->propietarioRepository->fetchById($rut); + } + public function edit(Model\Venta\Propietario $propietario, array $data): Model\Venta\Propietario + { + if (isset($data['calle']) or isset($data['numero']) or isset($data['extra']) or isset($data['comuna'])) { + $direccion = $this->addDireccion($data); + $data['direccion'] = $direccion->id; + } + $filteredData = $this->propietarioRepository->filterData($data); + return $this->propietarioRepository->edit($propietario, $filteredData); + } public function addPropietario(array $data): Model\Venta\Propietario { $direccion = $this->addDireccion($data);