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