Reservation Fixes

This commit is contained in:
Juan Pablo Vial
2025-09-09 16:24:48 -03:00
parent e5cf4e7067
commit e36281d924
6 changed files with 47 additions and 33 deletions

View File

@ -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__);
}

View File

@ -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,

View File

@ -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;
}

View File

@ -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));
}
}

View File

@ -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 {

View File

@ -1,8 +1,10 @@
<?php
namespace Incoviba\Service\Venta;
use DateTimeImmutable;
use DateMalformedStringException;
use DateTimeImmutable;
use DateTimeInterface;
use Incoviba\Exception\ServiceAction\Read;
use PDOException;
use Psr\Log\LoggerInterface;
use Incoviba\Common\Define;
@ -33,6 +35,12 @@ class Reservation extends Ideal\Service\API
return [];
}
}
/**
* @param int $project_id
* @return array
* @throws ServiceAction\Read
*/
public function getByProject(int $project_id): array
{
try {
@ -51,6 +59,21 @@ class Reservation extends Ideal\Service\API
}
}
/**
* @param int $buyer_rut
* @param DateTimeInterface $date
* @return Model\Venta\Reservation
* @throws Read
*/
public function getByBuyerAndDate(int $buyer_rut, DateTimeInterface $date): Model\Venta\Reservation
{
try {
return $this->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);