Persona conectada con Propietario

This commit is contained in:
Juan Pablo Vial
2025-05-07 19:24:33 -04:00
parent 3903551176
commit f3e15b34a8
2 changed files with 47 additions and 5 deletions

View File

@ -1,6 +1,7 @@
<?php <?php
namespace Incoviba\Model; namespace Incoviba\Model;
use InvalidArgumentException;
use Incoviba\Common\Ideal; use Incoviba\Common\Ideal;
use Incoviba\Model\Persona\Datos; use Incoviba\Model\Persona\Datos;
@ -31,6 +32,20 @@ class Persona extends Ideal\Model
return $this->datos; return $this->datos;
} }
public function __get(string $name): mixed
{
if (property_exists($this, $name)) {
return $this->{$name};
}
if ($name === 'datos') {
return $this->datos();
}
if ($name === 'dv') {
return $this->digito;
}
throw new InvalidArgumentException("Property {$name} is not found in " . __CLASS__);
}
public function jsonSerialize(): mixed public function jsonSerialize(): mixed
{ {
return [ return [

View File

@ -1,12 +1,14 @@
<?php <?php
namespace Incoviba\Service; namespace Incoviba\Service;
use PDOException;
use Psr\Log\LoggerInterface;
use Incoviba\Common\Ideal; use Incoviba\Common\Ideal;
use Incoviba\Common\Implement; use Incoviba\Common\Implement;
use Incoviba\Exception\ServiceAction\Create;
use Incoviba\Exception\ServiceAction\Read; use Incoviba\Exception\ServiceAction\Read;
use Incoviba\Model; use Incoviba\Model;
use Incoviba\Repository; use Incoviba\Repository;
use Psr\Log\LoggerInterface;
class Persona extends Ideal\Service class Persona extends Ideal\Service
{ {
@ -21,16 +23,27 @@ class Persona extends Ideal\Service
/** /**
* @param int $rut * @param int $rut
* @return Model\Persona * @return Model\Persona
* @throws Read * @throws Read|Create
*/ */
public function getById(int $rut): Model\Persona public function getById(int $rut): Model\Persona
{ {
try { try {
return $this->process($this->personaRepository->fetchById($rut)); return $this->process($this->personaRepository->fetchById($rut));
} catch (Implement\Exception\EmptyResult $exception) { } catch (Implement\Exception\EmptyResult $exception) {
throw new Read(__CLASS__, $exception); 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 public function add(array $data): Model\Persona
{ {
try { try {
@ -42,9 +55,19 @@ class Persona extends Ideal\Service
$data['apellido_paterno'] = $propietario->apellidos['paterno']; $data['apellido_paterno'] = $propietario->apellidos['paterno'];
$data['apellido_materno'] = $propietario->apellidos['materno']; $data['apellido_materno'] = $propietario->apellidos['materno'];
$data['direccion_id'] = $propietario->datos->direccion->id; $data['direccion_id'] = $propietario->datos->direccion->id;
if (isset($propietario->datos?->email)) {
$data['email'] = $propietario->datos->email;
}
if (isset($propietario->datos?->telefono)) {
$data['telefono'] = $propietario->datos->telefono;
}
} catch (Implement\Exception\EmptyResult) {} } catch (Implement\Exception\EmptyResult) {}
$persona = $this->personaRepository->create($data); $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'])) { if (isset($data['direccion_id']) or isset($data['email']) or isset($data['telefono'])) {
$datosData = ['persona_rut' => $persona->rut]; $datosData = ['persona_rut' => $persona->rut];
@ -62,7 +85,11 @@ class Persona extends Ideal\Service
$this->datosPersonaRepository->edit($datos, $data); $this->datosPersonaRepository->edit($datos, $data);
} catch (Implement\Exception\EmptyResult) { } catch (Implement\Exception\EmptyResult) {
$datos = $this->datosPersonaRepository->create($datosData); $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); return $this->process($persona);