FIX: Fetch by state
This commit is contained in:
@ -132,6 +132,9 @@ class Reservation extends Ideal\Service\API
|
||||
} catch (ServiceAction\Read) {}
|
||||
}
|
||||
} catch (Implement\Exception\EmptyResult) {
|
||||
if (!$this->reservationRepository->getConnection()->getPDO()->inTransaction()) {
|
||||
$this->reservationRepository->getConnection()->getPDO()->beginTransaction();
|
||||
}
|
||||
$buyerData = [];
|
||||
foreach ($data as $key => $value) {
|
||||
if (!str_starts_with($key, 'buyer_')) {
|
||||
@ -147,34 +150,38 @@ class Reservation extends Ideal\Service\API
|
||||
$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()]);
|
||||
$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();
|
||||
}
|
||||
} catch (PDOException $exception) {
|
||||
$this->logger->warning($exception->getMessage(), ['exception' => $exception->getTraceAsString()]);
|
||||
if ($this->reservationRepository->getConnection()->getPDO()->inTransaction()) {
|
||||
$this->reservationRepository->getConnection()->getPDO()->rollBack();
|
||||
}
|
||||
throw new ServiceAction\Create(__CLASS__, $exception);
|
||||
}
|
||||
}
|
||||
|
||||
$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']);
|
||||
}
|
||||
|
||||
return $this->process($reservation);
|
||||
}
|
||||
public function edit(Define\Model $model, array $new_data): Model\Venta\Reservation
|
||||
@ -195,6 +202,41 @@ class Reservation extends Ideal\Service\API
|
||||
throw new ServiceAction\Delete(__CLASS__, $exception);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @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);
|
||||
}
|
||||
}
|
||||
|
||||
protected function process(Define\Model $model): Model\Venta\Reservation
|
||||
{
|
||||
$model->addFactory('states', new Implement\Repository\Factory()
|
||||
|
Reference in New Issue
Block a user