Files
oficial/app/src/Service/Venta/Reservation.php

284 lines
10 KiB
PHP
Raw Normal View History

2025-03-03 15:21:12 -03:00
<?php
namespace Incoviba\Service\Venta;
use DateMalformedStringException;
2025-09-09 16:24:48 -03:00
use DateTimeImmutable;
use DateTimeInterface;
use Incoviba\Exception\ServiceAction\Read;
2025-03-03 15:21:12 -03:00
use PDOException;
use Psr\Log\LoggerInterface;
use Incoviba\Common\Define;
use Incoviba\Common\Ideal;
use Incoviba\Common\Implement;
use Incoviba\Exception\ServiceAction;
use Incoviba\Model;
use Incoviba\Repository;
2025-08-22 09:28:53 -04:00
use Incoviba\Service;
2025-03-03 15:21:12 -03:00
class Reservation extends Ideal\Service\API
{
2025-08-08 17:04:50 -04:00
public function __construct(LoggerInterface $logger,
protected Repository\Venta\Reservation $reservationRepository,
2025-08-22 09:28:53 -04:00
protected Repository\Venta\Reservation\State $stateRepository,
protected Service\Persona $personaService,
protected Service\Proyecto\Broker $brokerService,
protected Promotion $promotionService, protected Unidad $unitService)
2025-03-03 15:21:12 -03:00
{
parent::__construct($logger);
}
public function getAll(null|string|array $order = null): array
{
try {
2025-07-22 18:20:48 -04:00
return array_map([$this, 'process'], $this->reservationRepository->fetchAll($order));
2025-03-03 15:21:12 -03:00
} catch (Implement\Exception\EmptyResult) {
return [];
}
}
2025-09-09 16:24:48 -03:00
/**
* @param int $project_id
* @return array
* @throws ServiceAction\Read
*/
2025-08-08 17:04:50 -04:00
public function getByProject(int $project_id): array
{
try {
return array_map([$this, 'process'], $this->reservationRepository->fetchByProject($project_id));
} catch (Implement\Exception\EmptyResult $exception) {
throw new ServiceAction\Read(__CLASS__, $exception);
}
}
2025-03-03 15:21:12 -03:00
public function get(int $id): Model\Venta\Reservation
{
try {
return $this->process($this->reservationRepository->fetchById($id));
} catch (Implement\Exception\EmptyResult $exception) {
throw new ServiceAction\Read(__CLASS__, $exception);
2025-08-08 17:04:50 -04:00
}
}
2025-09-09 16:24:48 -03:00
/**
* @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);
}
}
2025-08-08 17:04:50 -04:00
/**
* @param int $project_id
* @return array
* @throws ServiceAction\Read
*/
public function getActive(int $project_id): array
{
try {
return array_map([$this, 'process'], $this->reservationRepository->fetchActive($project_id));
} catch (Implement\Exception\EmptyResult $exception) {
throw new ServiceAction\Read(__CLASS__, $exception);
}
}
/**
* @param int $project_id
* @return array
* @throws ServiceAction\Read
*/
public function getPending(int $project_id): array
{
try {
return array_map([$this, 'process'], $this->reservationRepository->fetchPending($project_id));
} catch (Implement\Exception\EmptyResult $exception) {
throw new ServiceAction\Read(__CLASS__, $exception);
}
}
/**
* @param int $project_id
* @return array
* @throws ServiceAction\Read
*/
public function getRejected(int $project_id): array
{
try {
return array_map([$this, 'process'], $this->reservationRepository->fetchRejected($project_id));
} catch (Implement\Exception\EmptyResult $exception) {
throw new ServiceAction\Read(__CLASS__, $exception);
2025-03-03 15:21:12 -03:00
}
}
public function add(array $data): Model\Venta\Reservation
{
2025-08-22 09:28:53 -04:00
$date = new DateTimeImmutable();
2025-03-03 15:21:12 -03:00
try {
2025-08-22 09:28:53 -04:00
$date = new DateTimeImmutable($data['date']);
} catch (DateMalformedStringException) {}
try {
$reservation = $this->reservationRepository->fetchByBuyerAndDate($data['buyer_rut'], $date);
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]);
2025-09-09 16:24:48 -03:00
} catch (ServiceAction\Read) {}
2025-08-22 09:28:53 -04:00
}
} catch (Implement\Exception\EmptyResult) {
2025-09-11 15:05:07 -03:00
if (!$this->reservationRepository->getConnection()->getPDO()->inTransaction()) {
$this->reservationRepository->getConnection()->getPDO()->beginTransaction();
}
2025-08-22 09:28:53 -04:00
$buyerData = [];
foreach ($data as $key => $value) {
if (!str_starts_with($key, 'buyer_')) {
continue;
}
2025-09-09 16:24:48 -03:00
$buyerData[substr($key, strlen('buyer_'))] = $value;
2025-08-22 09:28:53 -04:00
}
$this->personaService->add($buyerData);
2025-09-09 20:16:04 -03:00
2025-08-22 09:28:53 -04:00
$data['date'] = $date->format('Y-m-d');
2025-03-03 15:21:12 -03:00
try {
2025-08-22 09:28:53 -04:00
$reservationData = $this->reservationRepository->filterData($data);
$reservation = $this->reservationRepository->create($reservationData);
$reservation = $this->reservationRepository->save($reservation);
2025-03-03 15:21:12 -03:00
2025-09-11 15:05:07 -03:00
$stateType = Model\Venta\Reservation\State\Type::INACTIVE;
$stateData = [
'reservation_id' => $reservation->id,
'date' => $data['date'],
'type' => $stateType->value,
];
$state = $this->stateRepository->create($stateData);
$this->stateRepository->save($state);
$units = array_combine($data['units'], $data['units_value']);
$this->addUnits($reservation, $units);
if (array_key_exists('broker_rut', $data) and !empty($data['broker_rut'])) {
$this->addBroker($reservation, $data['broker_rut']);
}
if (array_key_exists('promotions', $data)) {
$this->addPromotions($reservation, $data['promotions']);
}
if ($this->reservationRepository->getConnection()->getPDO()->inTransaction()) {
$this->reservationRepository->getConnection()->getPDO()->commit();
2025-08-22 09:28:53 -04:00
}
} catch (PDOException $exception) {
2025-09-11 15:05:07 -03:00
$this->logger->warning($exception->getMessage(), ['exception' => $exception->getTraceAsString()]);
if ($this->reservationRepository->getConnection()->getPDO()->inTransaction()) {
$this->reservationRepository->getConnection()->getPDO()->rollBack();
}
2025-08-22 09:28:53 -04:00
throw new ServiceAction\Create(__CLASS__, $exception);
}
2025-03-03 15:21:12 -03:00
}
2025-08-22 09:28:53 -04:00
return $this->process($reservation);
2025-03-03 15:21:12 -03:00
}
public function edit(Define\Model $model, array $new_data): Model\Venta\Reservation
{
try {
return $this->process($this->reservationRepository->edit($model, $new_data));
} catch (PDOException | Implement\Exception\EmptyResult $exception) {
throw new ServiceAction\Update(__CLASS__, $exception);
}
}
public function delete(int $id): Model\Venta\Reservation
{
try {
$reservation = $this->reservationRepository->fetchById($id);
$this->reservationRepository->remove($reservation);
return $reservation;
} catch (PDOException | Implement\Exception\EmptyResult $exception) {
throw new ServiceAction\Delete(__CLASS__, $exception);
}
}
2025-09-11 15:05:07 -03:00
/**
* @param Model\Venta\Reservation $reservation
* @return void
* @throws ServiceAction\Update
*/
public function approve(Model\Venta\Reservation $reservation): void
{
try {
$stateData = [
'reservation_id' => $reservation->id,
'date' => new DateTimeImmutable(),
'type' => Model\Venta\Reservation\State\Type::ACTIVE->value,
];
$state = $this->stateRepository->create($stateData);
$this->stateRepository->save($state);
} catch (PDOException $exception) {
throw new ServiceAction\Update(__CLASS__, $exception);
}
}
public function reject(Model\Venta\Reservation $reservation): void
{
try {
$stateData = [
'reservation_id' => $reservation->id,
'date' => new DateTimeImmutable(),
'type' => Model\Venta\Reservation\State\Type::REJECTED->value,
];
$state = $this->stateRepository->create($stateData);
$this->stateRepository->save($state);
} catch (PDOException $exception) {
throw new ServiceAction\Update(__CLASS__, $exception);
}
}
2025-03-03 15:21:12 -03:00
protected function process(Define\Model $model): Model\Venta\Reservation
{
2025-07-22 18:20:48 -04:00
$model->addFactory('states', new Implement\Repository\Factory()
->setArgs(['reservation_id' => $model->id])
->setCallable(function(int $reservation_id) {
return $this->stateRepository->fetchByReservation($reservation_id);
})
);
2025-09-09 16:24:48 -03:00
$model->buyer = $this->personaService->getById($model->buyer->rut);
2025-03-03 15:21:12 -03:00
return $model;
}
2025-08-22 09:28:53 -04:00
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);
}
2025-09-09 20:16:04 -03:00
protected function addBroker(Model\Venta\Reservation $reservation, int $broker_rut): void
{
try {
$broker = $this->brokerService->get($broker_rut);
$reservation->broker = $broker;
$this->reservationRepository->save($reservation);
} catch (ServiceAction\Read) {}
}
2025-08-22 09:28:53 -04:00
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);
}
2025-03-03 15:21:12 -03:00
}