Reservas
This commit is contained in:
@ -15,7 +15,8 @@ class Persona extends Ideal\Service
|
||||
public function __construct(LoggerInterface $logger,
|
||||
protected Repository\Persona $personaRepository,
|
||||
protected Repository\Persona\Datos $datosPersonaRepository,
|
||||
protected Repository\Venta\Propietario $propietarioRepository)
|
||||
protected Repository\Venta\Propietario $propietarioRepository,
|
||||
protected Direccion $direccionService)
|
||||
{
|
||||
parent::__construct($logger);
|
||||
}
|
||||
@ -67,44 +68,34 @@ class Persona extends Ideal\Service
|
||||
} catch (Implement\Exception\EmptyResult) {
|
||||
try {
|
||||
$propietario = $this->propietarioRepository->fetchById($data['rut']);
|
||||
} 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);
|
||||
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];
|
||||
if (isset($data['direccion_id'])) {
|
||||
$datosData['direccion_id'] = $data['direccion_id'];
|
||||
}
|
||||
if (isset($data['email'])) {
|
||||
$datosData['email'] = $data['email'];
|
||||
}
|
||||
if (isset($data['telefono'])) {
|
||||
$datosData['telefono'] = $data['telefono'];
|
||||
}
|
||||
try {
|
||||
$datos = $this->datosPersonaRepository->fetchByPersona($persona->rut);
|
||||
$this->datosPersonaRepository->edit($datos, $data);
|
||||
$persona = $this->addFromPropietario($propietario);
|
||||
} catch (Implement\Exception\EmptyResult) {
|
||||
$datos = $this->datosPersonaRepository->create($datosData);
|
||||
$dataMap = [
|
||||
'digit' => 'digito',
|
||||
'name' => 'nombres',
|
||||
'names' => 'nombres',
|
||||
'last_name' => 'apellido_paterno',
|
||||
'last_name2' => 'apellido_materno',
|
||||
];
|
||||
foreach ($data as $key => $value) {
|
||||
if (array_key_exists($key, $dataMap)) {
|
||||
$data[$dataMap[$key]] = $value;
|
||||
unset($data[$key]);
|
||||
}
|
||||
}
|
||||
|
||||
$filteredData = $this->personaRepository->filterData($data);
|
||||
try {
|
||||
$this->datosPersonaRepository->save($datos);
|
||||
$persona = $this->personaRepository->create($filteredData);
|
||||
$persona = $this->personaRepository->save($persona);
|
||||
} catch (PDOException $exception) {
|
||||
throw new Create(__CLASS__, $exception);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
$this->addDatos($persona, $data);
|
||||
|
||||
return $this->process($persona);
|
||||
}
|
||||
|
||||
@ -180,4 +171,129 @@ class Persona extends Ideal\Service
|
||||
->setArgs(['persona_rut' => $persona->rut]));
|
||||
return $persona;
|
||||
}
|
||||
/**
|
||||
* @param Model\Venta\Propietario $propietario
|
||||
* @return Model\Persona
|
||||
* @throws Create
|
||||
*/
|
||||
protected function addFromPropietario(Model\Venta\Propietario $propietario): Model\Persona
|
||||
{
|
||||
$data = [
|
||||
'rut' => $propietario->rut,
|
||||
'digito' => $propietario->dv,
|
||||
'nombres' => $propietario->nombres,
|
||||
'apellido_paterno' => $propietario->apellidos['paterno'],
|
||||
'apellido_materno' => $propietario->apellidos['materno'] ?? '',
|
||||
];
|
||||
try {
|
||||
$persona = $this->personaRepository->create($data);
|
||||
$persona = $this->personaRepository->save($persona);
|
||||
} catch (PDOException $exception) {
|
||||
throw new Create(__CLASS__, $exception);
|
||||
}
|
||||
|
||||
$datosData = [];
|
||||
if ($propietario->datos->direccion) {
|
||||
$datosData['direccion_id'] = $propietario->datos?->direccion->id;
|
||||
}
|
||||
if ($propietario->datos->email) {
|
||||
$datosData['email'] = $propietario->datos->email;
|
||||
}
|
||||
if ($propietario->datos->telefono) {
|
||||
$datosData['telefono'] = $propietario->datos->telefono;
|
||||
}
|
||||
if ($propietario->datos->estado_civil) {
|
||||
$datosData['estado_civil'] = $propietario->datos->estado_civil;
|
||||
}
|
||||
if ($propietario->datos->fecha_nacimiento) {
|
||||
$datosData['fecha_nacimiento'] = $propietario->datos->fecha_nacimiento;
|
||||
}
|
||||
if ($propietario->datos->profesion) {
|
||||
$datosData['ocupacion'] = $propietario->datos->profesion;
|
||||
}
|
||||
if ($propietario->datos->sexo) {
|
||||
$datosData['sexo'] = $propietario->datos->sexo;
|
||||
}
|
||||
|
||||
$this->addDatos($persona, $datosData);
|
||||
return $persona;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Model\Persona $persona
|
||||
* @param array $data
|
||||
* @return Model\Persona
|
||||
* @throws Create
|
||||
*/
|
||||
protected function addDatos(Model\Persona $persona, array $data): Model\Persona
|
||||
{
|
||||
$addressData = [];
|
||||
foreach ($data as $key => $value) {
|
||||
if (!str_starts_with($key, 'address_') and !str_starts_with($key, 'direccion_')) {
|
||||
continue;
|
||||
}
|
||||
if (str_starts_with($key, 'direccion_')) {
|
||||
$newKey = substr($key, strlen('direccion_'));
|
||||
} else {
|
||||
$newKey = substr($key, strlen('address_'));
|
||||
}
|
||||
$addressData[$newKey] = $value;
|
||||
}
|
||||
if (!empty($addressData)) {
|
||||
$address = $this->direccionService->add($addressData);
|
||||
$data['direccion_id'] = $address->id;
|
||||
}
|
||||
|
||||
$dataMap = [
|
||||
'phone' => 'telefono',
|
||||
'profession' => 'ocupacion',
|
||||
'profesion' => 'ocupacion',
|
||||
'sex' => 'sexo',
|
||||
'marital_status' => 'estado_civil',
|
||||
'birth_date' => 'fecha_nacimiento',
|
||||
'birthdate' => 'fecha_nacimiento',
|
||||
];
|
||||
foreach ($data as $key => $value) {
|
||||
if (array_key_exists($key, $dataMap)) {
|
||||
$data[$dataMap[$key]] = $value;
|
||||
unset($data[$key]);
|
||||
}
|
||||
}
|
||||
try {
|
||||
$datos = $this->datosPersonaRepository->fetchByPersona($persona->rut);
|
||||
$this->datosPersonaRepository->edit($datos, $data);
|
||||
} catch (Implement\Exception\EmptyResult) {
|
||||
$datosData = ['persona_rut' => $persona->rut];
|
||||
/*if (isset($data['direccion_id'])) {
|
||||
$datosData['direccion_id'] = $data['direccion_id'];
|
||||
}
|
||||
if (isset($data['email'])) {
|
||||
$datosData['email'] = $data['email'];
|
||||
}
|
||||
if (isset($data['telefono'])) {
|
||||
$datosData['telefono'] = $data['telefono'];
|
||||
}
|
||||
if (isset($data['estado_civil'])) {
|
||||
$datosData['estado_civil'] = $data['estado_civil'];
|
||||
}
|
||||
if (isset($data['fecha_nacimiento'])) {
|
||||
$datosData['fecha_nacimiento'] = $data['fecha_nacimiento'];
|
||||
}
|
||||
if (isset($data['ocupacion'])) {
|
||||
$datosData['ocupacion'] = $data['ocupacion'];
|
||||
}
|
||||
if (isset($data['sexo'])) {
|
||||
$datosData['sexo'] = $data['sexo'];
|
||||
}*/
|
||||
$datosData = $this->datosPersonaRepository->filterData($datosData);
|
||||
$datos = $this->datosPersonaRepository->create($datosData);
|
||||
try {
|
||||
$this->datosPersonaRepository->save($datos);
|
||||
} catch (PDOException $exception) {
|
||||
throw new Create(__CLASS__, $exception);
|
||||
}
|
||||
}
|
||||
|
||||
return $persona;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user