This commit is contained in:
Juan Pablo Vial
2025-08-22 09:28:53 -04:00
parent 78222eb9f1
commit 454ba41d9c
26 changed files with 1036 additions and 135 deletions

View File

@ -60,8 +60,8 @@ class Reservations
'partial' => false,
'errors' => [],
];
try {
var_dump($input);
/*try {
$output['reservations'] []= [
'reservation' => $reservationService->add($input),
'success' => true
@ -69,7 +69,7 @@ class Reservations
$output['partial'] = true;
} catch (ServiceAction\Create $exception) {
$output['errors'] []= $this->parseError($exception);
}
}*/
if (count($input['reservations']) === count($output['reservations'])) {
$output['success'] = true;
@ -77,6 +77,31 @@ class Reservations
return $this->withJson($response, $output);
}
public function addOne(ServerRequestInterface $request, ResponseInterface $response, Service\Venta\Reservation $reservationService): ResponseInterface
{
$input = $request->getParsedBody();
$output = [
'input' => $input,
'reservation' => null,
'success' => false,
'errors' => [],
];
$data = [];
foreach ($input as $key => $value) {
if (!str_starts_with($key, 'add_')) {
continue;
}
$data[substr($key, 4)] = $value;
}
try {
$output['reservation'] = $reservationService->add($data);
$output['success'] = true;
} catch (ServiceAction\Create $exception) {
$output['errors'] []= $this->parseError($exception);
}
return $this->withJson($response, $output);
}
public function edit(ServerRequestInterface $request, ResponseInterface $response, Service\Venta\Reservation $reservationService, int $reservation_id): ResponseInterface
{
$input = $request->getParsedBody();

View File

@ -1,6 +1,7 @@
<?php
namespace Incoviba\Model\Venta;
use DateTimeInterface;
use JsonSerializable;
use Incoviba\Model\Direccion;
@ -12,6 +13,7 @@ class Datos
public ?Direccion $direccion = null;
public ?int $telefono = null;
public ?string $email = null;
public ?DateTimeInterface $fecha_nacimiento = null;
public function jsonSerialize(): mixed
{
@ -21,7 +23,8 @@ class Datos
'profesion' => $this->profesion ?? '',
'direccion' => $this->direccion ?? '',
'telefono' => $this->telefono ?? '',
'email' => $this->email ?? ''
'email' => $this->email ?? '',
'fecha_nacimiento' => $this->fecha_nacimiento?->format('Y-m-d') ?? '',
];
}
}

View File

@ -68,7 +68,7 @@ class Reservation extends Common\Ideal\Model
public function addUnit(Model\Venta\Unidad $unit, float $value): self
{
if (($i = $this->findUnit($unit->id)) !== null) {
$this->units[$i]['value'] = $value;
$this->units[$i]->value = $value;
return $this;
}
$this->units[] = (object) [
@ -88,12 +88,7 @@ class Reservation extends Common\Ideal\Model
}
public function findUnit(int $unit_id): ?int
{
foreach ($this->units as $idx => $unit) {
if ($unit['unit']->id == $unit_id) {
return $idx;
}
}
return null;
return array_find_key($this->units, fn($unit) => $unit->unit->id == $unit_id);
}
public function hasUnit(int $unit_id): bool
{

View File

@ -3,8 +3,9 @@ namespace Incoviba\Model\Venta\Reservation\State;
enum Type: int
{
case ACTIVE = 1;
case INACTIVE = 0;
case ACTIVE = 1;
case PROMISED = 2;
case REJECTED = -1;
public function jsonSerialize(): array

View File

@ -93,7 +93,7 @@ class Contract extends Common\Ideal\Repository
->select('a.*')
->from("{$this->getTable()} a")
->joined($this->statusJoin())
->where('a.broker_rut = :broker_rut AND bcs.state = :state');
->where('a.broker_rut = :broker_rut AND bcs.type = :state');
return $this->fetchMany($query, ['broker_rut' => $brokerRut, 'state' => Model\Proyecto\Broker\Contract\Type::ACTIVE]);
}
@ -108,8 +108,8 @@ class Contract extends Common\Ideal\Repository
->select('a.*')
->from("{$this->getTable()} a")
->joined($this->statusJoin())
->where('a.proyecto_id = :proyecto_id AND bcs.state = :state');
return $this->fetchMany($query, ['proyecto_id' => $projectId, 'state' => Model\Proyecto\Broker\Contract\State\Type::ACTIVE->value]);
->where('a.project_id = :project_id AND bcs.type = :state');
return $this->fetchMany($query, ['project_id' => $projectId, 'state' => Model\Proyecto\Broker\Contract\State\Type::ACTIVE->value]);
}
/**

View File

@ -212,7 +212,7 @@ class Promotion extends Common\Ideal\Repository
->joined('LEFT OUTER JOIN proyecto_tipo_unidad ptu1 ON ptu.id = unidad.pt')
->where('(pp.project_id = :project_id OR put.project_id = :project_id OR ptu.proyecto = :project_id OR ptu1.proyecto = :project_id) AND a.state = :state')
->group('a.id');
return $this->fetchMany($query, ['project_id' => $project_id, 'state' => Model\Venta\Promotion\State::ACTIVE]);
return $this->fetchMany($query, ['project_id' => $project_id, 'state' => Model\Venta\Promotion\State::ACTIVE->value]);
}
/**

View File

@ -45,15 +45,17 @@ class Reservation extends Common\Ideal\Repository
}
public function save(Common\Define\Model $model): Model\Venta\Reservation
{
$model->id = $this->saveNew([
'project_id',
'buyer_rut',
'date'
], [
$model->project->id,
$model->buyer->rut,
$model->date->format('Y-m-d')
]);
if (!isset($model->id)) {
$model->id = $this->saveNew([
'project_id',
'buyer_rut',
'date'
], [
$model->project->id,
$model->buyer->rut,
$model->date->format('Y-m-d')
]);
}
$this->saveUnits($model);
$this->savePromotions($model);
$this->saveBroker($model);
@ -440,10 +442,10 @@ class Reservation extends Common\Ideal\Repository
$query = $this->connection->getQueryBuilder()
->select()
->from('reservation_details')
->where('reservation_id = :id AND type = ?');
->where('reservation_id = :id AND type = :type');
$statement = $this->connection->execute($query, [
'id' => $reservation->id,
Model\Venta\Reservation\Detail\Type::Unit->value
'type' =>Model\Venta\Reservation\Detail\Type::Unit->value
]);
while ($result = $statement->fetch(PDO::FETCH_ASSOC)) {
@ -479,6 +481,13 @@ class Reservation extends Common\Ideal\Repository
}
return $reservation;
}
/**
* @param Model\Venta\Reservation $reservation
* @param array $new_data
* @return Model\Venta\Reservation
* @throws Common\Implement\Exception\EmptyResult
*/
protected function fetchBroker(Model\Venta\Reservation &$reservation, array $new_data): Model\Venta\Reservation
{
if (!array_key_exists('broker_id', $new_data)) {
@ -492,6 +501,9 @@ class Reservation extends Common\Ideal\Repository
'type' => Model\Venta\Reservation\Detail\Type::Broker->value
]);
$result = $statement->fetch(PDO::FETCH_ASSOC);
if ($result === false) {
throw new Common\Implement\Exception\EmptyResult($query);
}
$reservation->broker = $this->brokerRepository->fetchById($result['reference_id']);
} catch (PDOException) {}

View File

@ -19,12 +19,16 @@ class State extends Common\Ideal\Repository
public function create(?array $data = null): Model\Venta\Reservation\State
{
$map = (new Common\Implement\Repository\MapperParser(['type']))
$map = (new Common\Implement\Repository\MapperParser())
->register('reservation_id', (new Common\Implement\Repository\Mapper())
->setProperty('reservation')
->setFunction(function($data) {
return $this->reservationRepository->fetchById($data['reservation_id']);
}))
->register('type', (new Common\Implement\Repository\Mapper())
->setFunction(function($data) {
return Model\Venta\Reservation\State\Type::from($data['type']);
}))
->register('date', new Common\Implement\Repository\Mapper\DateTime('date'));
return $this->parseData(new Model\Venta\Reservation\State(), $data, $map);
}
@ -32,7 +36,7 @@ class State extends Common\Ideal\Repository
{
$model->id = $this->saveNew(
['reservation_id', 'date', 'type'],
[$model->reservation->id, $model->date->format('Y-m-d'), $model->type]
[$model->reservation->id, $model->date->format('Y-m-d'), $model->type->value]
);
return $model;
}

View File

@ -0,0 +1,49 @@
<?php
namespace Incoviba\Service;
use PDOException;
use Psr\Log\LoggerInterface;
use Incoviba\Common\Ideal;
use Incoviba\Common\Implement;
use Incoviba\Exception\ServiceAction;
use Incoviba\Model;
use Incoviba\Repository;
class Direccion extends Ideal\Service
{
public function __construct(LoggerInterface $logger, protected Repository\Direccion $direccionRepository)
{
parent::__construct($logger);
}
/**
* @param array $data
* @return Model\Direccion
* @throws ServiceAction\Create
*/
public function add(array $data): Model\Direccion
{
$dataMap = [
'street' => 'calle',
'number' => 'numero',
'comuna_id' => 'comuna',
];
foreach ($data as $key => $value) {
if (array_key_exists($key, $dataMap)) {
$data[$dataMap[$key]] = $value;
unset($data[$key]);
}
}
try {
return $this->direccionRepository->fetchByCalleAndNumeroAndExtraAndComuna($data['calle'], $data['numero'], $data['extra'], $data['comuna']);
} catch (Implement\Exception\EmptyResult) {
$filteredData = $this->direccionRepository->filterData($data);
$direccion = $this->direccionRepository->create($filteredData);
try {
return $this->direccionRepository->save($direccion);
} catch (PDOException $exception) {
throw new ServiceAction\Create(__CLASS__, $exception);
}
}
}
}

View File

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

View File

@ -11,12 +11,16 @@ use Incoviba\Common\Implement;
use Incoviba\Exception\ServiceAction;
use Incoviba\Model;
use Incoviba\Repository;
use Incoviba\Service;
class Reservation extends Ideal\Service\API
{
public function __construct(LoggerInterface $logger,
protected Repository\Venta\Reservation $reservationRepository,
protected Repository\Venta\Reservation\State $stateRepository)
protected Repository\Venta\Reservation\State $stateRepository,
protected Service\Persona $personaService,
protected Service\Proyecto\Broker $brokerService,
protected Promotion $promotionService, protected Unidad $unitService)
{
parent::__construct($logger);
}
@ -91,22 +95,70 @@ class Reservation extends Ideal\Service\API
public function add(array $data): Model\Venta\Reservation
{
$date = new DateTimeImmutable();
try {
$date = new DateTimeImmutable();
try {
$date = new DateTimeImmutable($data['date']);
} catch (DateMalformedStringException) {}
return $this->process($this->reservationRepository->fetchByBuyerAndDate($data['buyer_rut'], $date));
} catch (Implement\Exception\EmptyResult) {}
$date = new DateTimeImmutable($data['date']);
} catch (DateMalformedStringException) {}
try {
$reservation = $this->reservationRepository->fetchByBuyerAndDate($data['buyer_rut'], $date);
try {
$reservationData = $this->reservationRepository->filterData($data);
$reservation = $this->reservationRepository->create($reservationData);
$this->reservationRepository->save($reservation);
return $this->process($reservation);
} catch (PDOException $exception) {
throw new ServiceAction\Create(__CLASS__, $exception);
if (array_key_exists('broker_rut', $data) and $data['broker_rut'] !== '') {
try {
$broker = $this->brokerService->get($data['broker_rut']);
$reservation = $this->reservationRepository->edit($reservation, ['broker_rut' => $broker->rut]);
} catch (ServiceAction\Read $exception) {}
}
} catch (Implement\Exception\EmptyResult) {
$buyerData = [];
foreach ($data as $key => $value) {
if (!str_starts_with($key, 'buyer_')) {
continue;
}
$buyerData[substr($key, 6)] = $value;
}
$this->personaService->add($buyerData);
if (array_key_exists('broker_rut', $data)) {
if (empty($data['broker_rut'])) {
unset($data['broker_rut']);
} else {
try {
$this->brokerService->get($data['broker_rut']);
} catch (ServiceAction\Read) {
unset($data['broker_rut']);
}
}
}
$data['date'] = $date->format('Y-m-d');
try {
$reservationData = $this->reservationRepository->filterData($data);
$reservation = $this->reservationRepository->create($reservationData);
$reservation = $this->reservationRepository->save($reservation);
try {
$stateType = Model\Venta\Reservation\State\Type::ACTIVE;
$stateData = [
'reservation_id' => $reservation->id,
'date' => $data['date'],
'type' => $stateType->value,
];
$state = $this->stateRepository->create($stateData);
$this->stateRepository->save($state);
} catch (PDOException $exception) {
$this->logger->warning($exception->getMessage(), ['reservation_id' => $reservation->id, 'exception' => $exception->getTraceAsString()]);
}
} catch (PDOException $exception) {
throw new ServiceAction\Create(__CLASS__, $exception);
}
}
$units = array_combine($data['units'], $data['units_value']);
$this->addUnits($reservation, $units);
if (isset($data['promotions'])) {
$this->addPromotions($reservation, $data['promotions']);
}
return $this->process($reservation);
}
public function edit(Define\Model $model, array $new_data): Model\Venta\Reservation
{
@ -136,4 +188,28 @@ class Reservation extends Ideal\Service\API
);
return $model;
}
protected function addUnits(Model\Venta\Reservation $reservation, array $units): void
{
foreach ($units as $unit_id => $value) {
try {
$unit = $this->unitService->getById($unit_id);
} catch (ServiceAction\Read) {
continue;
}
$reservation->addUnit($unit, $value);
}
$this->reservationRepository->save($reservation);
}
protected function addPromotions(Model\Venta\Reservation $reservation, array $promotions): void
{
foreach ($promotions as $promotion_id) {
try {
$promotion = $this->promotionService->getById($promotion_id);
} catch (ServiceAction\Read) {
continue;
}
$reservation->promotions []= $promotion;
}
$this->reservationRepository->save($reservation);
}
}