reservationRepository->fetchAll($order)); } catch (Implement\Exception\EmptyResult) { return []; } } /** * @param int $project_id * @return array * @throws ServiceAction\Read */ 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); } } 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); } } /** * @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); } } /** * @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); } } public function add(array $data): Model\Venta\Reservation { $date = new DateTimeImmutable(); try { $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]); } catch (ServiceAction\Read) {} } } catch (Implement\Exception\EmptyResult) { $buyerData = []; foreach ($data as $key => $value) { if (!str_starts_with($key, 'buyer_')) { continue; } $buyerData[substr($key, strlen('buyer_'))] = $value; } $this->personaService->add($buyerData); $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 (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 { 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); } } protected function process(Define\Model $model): Model\Venta\Reservation { $model->addFactory('states', new Implement\Repository\Factory() ->setArgs(['reservation_id' => $model->id]) ->setCallable(function(int $reservation_id) { return $this->stateRepository->fetchByReservation($reservation_id); }) ); $model->buyer = $this->personaService->getById($model->buyer->rut); 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 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) {} } 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); } }