Poder editar propietario de ventas, cambiando el propietario en si

This commit is contained in:
Juan Pablo Vial
2024-07-04 15:42:10 -04:00
parent 0d5c9efd68
commit 964d8d4237
7 changed files with 546 additions and 162 deletions

View File

@ -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);
}

View File

@ -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);
}
}

View File

@ -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}%"]);
}
}

View File

@ -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']));
}
}

View File

@ -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);