feature/cierres (#25)

Varios cambios

Co-authored-by: Juan Pablo Vial <jpvialb@incoviba.cl>
Reviewed-on: #25
This commit is contained in:
2025-07-22 13:18:00 +00:00
parent ba57cad514
commit 307f2ac7d7
418 changed files with 20045 additions and 984 deletions

View File

@ -1,12 +1,14 @@
<?php
namespace Incoviba\Service;
use PDOException;
use Psr\Log\LoggerInterface;
use Incoviba\Common\Ideal;
use Incoviba\Common\Implement;
use Incoviba\Exception\ServiceAction\Create;
use Incoviba\Exception\ServiceAction\Read;
use Incoviba\Model;
use Incoviba\Repository;
use Psr\Log\LoggerInterface;
class Persona extends Ideal\Service
{
@ -18,19 +20,42 @@ class Persona extends Ideal\Service
parent::__construct($logger);
}
public function getAll(null|string|array $orderBy = null): array
{
try {
try {
$ruts = $this->personaRepository->fetchMissing();
$this->addMissingPropietarios($ruts);
} catch (Implement\Exception\EmptyResult | Read | Create) {}
return array_map([$this, 'process'], $this->personaRepository->fetchAll($orderBy));
} catch (Implement\Exception\EmptyResult) {
return [];
}
}
/**
* @param int $rut
* @return Model\Persona
* @throws Read
* @throws Read|Create
*/
public function getById(int $rut): Model\Persona
{
try {
return $this->process($this->personaRepository->fetchById($rut));
} catch (Implement\Exception\EmptyResult $exception) {
throw new Read(__CLASS__, $exception);
} catch (Implement\Exception\EmptyResult) {
try {
$this->propietarioRepository->fetchById($rut);
return $this->add(compact('rut'));
} catch (Implement\Exception\EmptyResult $exception) {
throw new Read(__CLASS__, $exception);
}
}
}
/**
* @param array $data
* @return Model\Persona
* @throws Create
*/
public function add(array $data): Model\Persona
{
try {
@ -38,13 +63,20 @@ class Persona extends Ideal\Service
} catch (Implement\Exception\EmptyResult) {
try {
$propietario = $this->propietarioRepository->fetchById($data['rut']);
$data['nombres'] = $propietario->nombres;
$data['apellido_paterno'] = $propietario->apellidos['paterno'];
$data['apellido_materno'] = $propietario->apellidos['materno'];
$data['direccion_id'] = $propietario->datos->direccion->id;
} catch (Implement\Exception\EmptyResult) {}
} catch (Implement\Exception\EmptyResult $exception) {
throw new Create(__CLASS__, $exception);
}
$data['rut'] = $propietario->rut;
$data['digito'] = $propietario->dv;
$data['nombres'] = $propietario->nombres;
$data['apellido_paterno'] = $propietario->apellidos['paterno'];
$data['apellido_materno'] = $propietario->apellidos['materno'] ?? '';
$persona = $this->personaRepository->create($data);
$persona = $this->personaRepository->save($persona);
try {
$persona = $this->personaRepository->save($persona);
} catch (PDOException $exception) {
throw new Create(__CLASS__, $exception);
}
}
if (isset($data['direccion_id']) or isset($data['email']) or isset($data['telefono'])) {
$datosData = ['persona_rut' => $persona->rut];
@ -62,11 +94,69 @@ class Persona extends Ideal\Service
$this->datosPersonaRepository->edit($datos, $data);
} catch (Implement\Exception\EmptyResult) {
$datos = $this->datosPersonaRepository->create($datosData);
$this->datosPersonaRepository->save($datos);
try {
$this->datosPersonaRepository->save($datos);
} catch (PDOException $exception) {
throw new Create(__CLASS__, $exception);
}
}
}
return $this->process($persona);
}
/**
* @param array $ruts
* @param bool $load
* @return array|null
* @throws Create
* @throws Read
*/
public function addMissingPropietarios(array $ruts, bool $load = false): ?array
{
try {
$propietarios = $this->propietarioRepository->fetchByRuts($ruts);
} catch (Implement\Exception\EmptyResult $exception) {
throw new Read(__CLASS__, $exception);
}
$data = [];
$datos = [];
foreach ($propietarios as $propietario) {
$data []= [
'rut' => $propietario->rut,
'digito' => $propietario->dv,
'nombres' => $propietario->nombres,
'apellido_paterno' => $propietario->apellidos['paterno'],
'apellido_materno' => $propietario->apellidos['materno'] ?? '',
];
$datos []= [
'persona_rut' => $propietario->rut,
'direccion_id' => $propietario->datos?->direccion_id,
'email' => $propietario->datos?->email,
'telefono' => $propietario->datos?->telefono,
];
}
try {
$personas = $this->personaRepository->saveMissing($data);
} catch (PDOException|Implement\Exception\EmptyResult $exception) {
throw new Create(__CLASS__, $exception);
}
$personasRuts = array_map(function(Model\Persona $persona) {
return $persona->rut;
}, $personas);
$datos = array_filter($datos, function($row) use ($personasRuts) {
return in_array($row['persona_rut'], $personasRuts);
});
try {
$this->datosPersonaRepository->saveMissing($datos);
} catch (PDOException $exception) {
throw new Create(__CLASS__, $exception);
}
if ($load) {
return array_map([$this, 'process'], $personas);
}
return null;
}
public function edit(Model\Persona $persona, array $data): Model\Persona
{
$filteredData = $this->personaRepository->filterData($data);