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

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