Reservas
This commit is contained in:
49
app/src/Service/Direccion.php
Normal file
49
app/src/Service/Direccion.php
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user