diff --git a/app/src/Model/Persona.php b/app/src/Model/Persona.php index 3d3a465..0c564b6 100644 --- a/app/src/Model/Persona.php +++ b/app/src/Model/Persona.php @@ -39,15 +39,15 @@ class Persona extends Ideal\Model 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; } + if (property_exists($this, $name)) { + return $this->{$name}; + } throw new InvalidArgumentException("Property {$name} is not found in " . __CLASS__); } diff --git a/app/src/Model/Persona/Datos.php b/app/src/Model/Persona/Datos.php index 6ac09b6..3fa1df4 100644 --- a/app/src/Model/Persona/Datos.php +++ b/app/src/Model/Persona/Datos.php @@ -21,6 +21,7 @@ class Datos extends Ideal\Model public function jsonSerialize(): mixed { return [ + 'persona_rut' => $this->persona->rut, 'direccion' => $this->direccion ?? null, 'telefono' => $this->telefono ?? null, 'email' => $this->email ?? null, diff --git a/app/src/Model/Venta/Reservation.php b/app/src/Model/Venta/Reservation.php index 8090ba4..3b1578b 100644 --- a/app/src/Model/Venta/Reservation.php +++ b/app/src/Model/Venta/Reservation.php @@ -40,7 +40,7 @@ class Reservation extends Common\Ideal\Model { $price = 0; foreach ($this->units as $unit) { - $price += $unit->unit->precio($this->date); + $price += $unit->unit->precio($this->date)?->valor ?? 0; } return $price; } diff --git a/app/src/Repository/Persona/Datos.php b/app/src/Repository/Persona/Datos.php index 3d71052..06cd4b7 100644 --- a/app/src/Repository/Persona/Datos.php +++ b/app/src/Repository/Persona/Datos.php @@ -59,7 +59,7 @@ class Datos extends Ideal\Repository } public function save(Define\Model $model): Model\Persona\Datos { - $this->saveNew([ + $model->id = $this->saveNew([ 'persona_rut', 'direccion_id', 'telefono', 'email', 'fecha_nacimiento', 'sexo', 'estado_civil', 'nacionalidad', 'ocupacion' ], [ @@ -116,4 +116,11 @@ class Datos extends Ideal\Repository } $this->connection->execute($query, $flattened); } + + public function filterData(array $data): array + { + $fields = ['persona_rut', 'direccion_id', 'telefono', 'email', 'fecha_nacimiento', 'sexo', 'estado_civil', + 'nacionalidad', 'ocupacion']; + return array_intersect_key($data, array_flip($fields)); + } } diff --git a/app/src/Service/Persona.php b/app/src/Service/Persona.php index 17ea5aa..e41a8a6 100644 --- a/app/src/Service/Persona.php +++ b/app/src/Service/Persona.php @@ -241,6 +241,11 @@ class Persona extends Ideal\Service } if (!empty($addressData)) { $address = $this->direccionService->add($addressData); + foreach ($data as $key => $value) { + if (str_starts_with($key, 'address_') or str_starts_with($key, 'direccion_')) { + unset($data[$key]); + } + } $data['direccion_id'] = $address->id; } @@ -262,29 +267,8 @@ class Persona extends Ideal\Service 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']; - }*/ + } catch (Implement\Exception\EmptyResult $exception) { + $datosData = ['persona_rut' => $persona->rut, ...$data]; $datosData = $this->datosPersonaRepository->filterData($datosData); $datos = $this->datosPersonaRepository->create($datosData); try { diff --git a/app/src/Service/Venta/Reservation.php b/app/src/Service/Venta/Reservation.php index 4f9c713..bfcf618 100644 --- a/app/src/Service/Venta/Reservation.php +++ b/app/src/Service/Venta/Reservation.php @@ -1,8 +1,10 @@ process($this->reservationRepository->fetchByBuyerAndDate($buyer_rut, $date)); + } catch (Implement\Exception\EmptyResult $exception) { + throw new ServiceAction\Read(__CLASS__, $exception); + } + } + /** * @param int $project_id * @return array @@ -106,7 +129,7 @@ class Reservation extends Ideal\Service\API try { $broker = $this->brokerService->get($data['broker_rut']); $reservation = $this->reservationRepository->edit($reservation, ['broker_rut' => $broker->rut]); - } catch (ServiceAction\Read $exception) {} + } catch (ServiceAction\Read) {} } } catch (Implement\Exception\EmptyResult) { $buyerData = []; @@ -114,7 +137,7 @@ class Reservation extends Ideal\Service\API if (!str_starts_with($key, 'buyer_')) { continue; } - $buyerData[substr($key, 6)] = $value; + $buyerData[substr($key, strlen('buyer_'))] = $value; } $this->personaService->add($buyerData); if (array_key_exists('broker_rut', $data)) { @@ -186,11 +209,11 @@ class Reservation extends Ideal\Service\API return $this->stateRepository->fetchByReservation($reservation_id); }) ); + $model->buyer = $this->personaService->getById($model->buyer->rut); return $model; } protected function addUnits(Model\Venta\Reservation $reservation, array $units): void { - //var_dump(__LINE__, $units); foreach ($units as $unit_id => $value) { try { $unit = $this->unitService->getById($unit_id); @@ -203,7 +226,6 @@ class Reservation extends Ideal\Service\API } protected function addPromotions(Model\Venta\Reservation $reservation, array $promotions): void { - var_dump(__LINE__, $promotions); foreach ($promotions as $promotion_id) { try { $promotion = $this->promotionService->getById($promotion_id);