Agregar datos propietario

This commit is contained in:
Juan Pablo Vial
2025-05-15 15:23:34 -04:00
parent e33edc4d7b
commit 03317b3aa5
10 changed files with 441 additions and 132 deletions

View File

@ -3,8 +3,12 @@ namespace Incoviba\Service\Venta;
use Incoviba\Common\Ideal\Service;
use Incoviba\Common\Implement\Exception\EmptyResult;
use Incoviba\Exception\ServiceAction\Create;
use Incoviba\Exception\ServiceAction\Read;
use Incoviba\Exception\ServiceAction\Update;
use Incoviba\Repository;
use Incoviba\Model;
use PDOException;
use Psr\Log\LoggerInterface;
class Propietario extends Service
@ -17,10 +21,27 @@ class Propietario extends Service
parent::__construct($logger);
}
/**
* @param int $rut
* @return Model\Venta\Propietario
* @throws Read
*/
public function getByRut(int $rut): Model\Venta\Propietario
{
return $this->propietarioRepository->fetchById($rut);
try {
return $this->propietarioRepository->fetchById($rut);
} catch (EmptyResult $exception) {
throw new Read(__CLASS__, $exception);
}
}
/**
* @param Model\Venta\Propietario $propietario
* @param array $data
* @return Model\Venta\Propietario
* @throws Create
* @throws Update
*/
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'])) {
@ -28,8 +49,18 @@ class Propietario extends Service
$data['direccion'] = $direccion->id;
}
$filteredData = $this->propietarioRepository->filterData($data);
return $this->propietarioRepository->edit($propietario, $filteredData);
try {
return $this->propietarioRepository->edit($propietario, $filteredData);
} catch (PDOException | EmptyResult $exception) {
throw new Update(__CLASS__, $exception);
}
}
/**
* @param array $data
* @return Model\Venta\Propietario
* @throws Create
*/
public function addPropietario(array $data): Model\Venta\Propietario
{
$direccion = $this->addDireccion($data);
@ -42,14 +73,16 @@ class Propietario extends Service
$data['dv'] = $dv;
}
$fields = array_fill_keys([
$fields = array_flip([
'rut',
'dv',
'nombres',
'apellido_paterno',
'apellido_materno',
'direccion'
], 0);
'direccion',
'email',
'telefono'
]);
$filtered_data = array_intersect_key($data, $fields);
try {
@ -60,11 +93,22 @@ class Propietario extends Service
}
$propietario = $this->propietarioRepository->edit($propietario, $edits);
} catch (EmptyResult) {
$propietario = $this->propietarioRepository->create($filtered_data);
$propietario = $this->propietarioRepository->save($propietario);
try {
$propietario = $this->propietarioRepository->create($filtered_data);
$propietario = $this->propietarioRepository->save($propietario);
} catch (PDOException $exception) {
throw new Create(__CLASS__, $exception);
}
}
return $propietario;
}
/**
* @param array $data
* @return Model\Venta\Propietario
* @throws Create
* @throws Update
*/
public function addSociedad(array $data): Model\Venta\Propietario
{
$direccion = $this->addDireccion($data);
@ -74,12 +118,12 @@ class Propietario extends Service
$data['rut'] = explode('-', $data['rut'])[0];
}
$fields = array_fill_keys([
$fields = array_flip([
'rut',
'razon_social',
'direccion',
'representante'
], 0);
]);
$filtered_data = array_intersect_key($data, $fields);
$mapped_data = array_combine([
'rut',
@ -97,27 +141,45 @@ class Propietario extends Service
if ($sociedad->contacto->rut !== $mapped_data['representante']) {
$edits['representante'] = $mapped_data['representante'];
}
$sociedad = $this->propietarioRepository->edit($sociedad, $edits);
try {
$sociedad = $this->propietarioRepository->edit($sociedad, $edits);
} catch (PDOException $exception) {
throw new Update(__CLASS__, $exception);
}
} catch (EmptyResult) {
$sociedad = $this->propietarioRepository->create($mapped_data);
$sociedad = $this->propietarioRepository->save($sociedad);
try {
$sociedad = $this->propietarioRepository->create($mapped_data);
$sociedad = $this->propietarioRepository->save($sociedad);
} catch (PDOException $exception) {
throw new Create(__CLASS__, $exception);
}
}
return $sociedad;
}
/**
* @param array $data
* @return Model\Direccion
* @throws Create
*/
protected function addDireccion(array $data): Model\Direccion
{
$fields = array_fill_keys([
$fields = array_flip([
'calle',
'numero',
'extra',
'comuna'
], 0);
]);
$filtered_data = array_intersect_key($data, $fields);
try {
$direccion = $this->direccionRepository->fetchByCalleAndNumeroAndExtraAndComuna($filtered_data['calle'], $filtered_data['numero'], $filtered_data['extra'], $filtered_data['comuna']);
} catch (EmptyResult) {
$direccion = $this->direccionRepository->create($filtered_data);
$direccion = $this->direccionRepository->save($direccion);
try {
$direccion = $this->direccionRepository->create($filtered_data);
$direccion = $this->direccionRepository->save($direccion);
} catch (PDOException $exception) {
throw new Create(__CLASS__, $exception);
}
}
return $direccion;
}