promotionRepository->fetchAll($order)); } catch (Implement\Exception\EmptyResult) { return []; } } /** * @param int $promotion_id * @return Model\Venta\Promotion * @throws Exception\ServiceAction\Read */ public function getById(int $promotion_id): Model\Venta\Promotion { try { return $this->process($this->promotionRepository->fetchById($promotion_id)); } catch (Implement\Exception\EmptyResult $exception) { throw new Exception\ServiceAction\Read(__CLASS__, $exception); } } /** * @param int $contract_id * @return array */ public function getByContract(int $contract_id): array { try { return array_map([$this, 'process'], $this->promotionRepository->fetchByContract($contract_id)); } catch (Implement\Exception\EmptyResult) { try { $contract = $this->contractRepository->fetchById($contract_id); return array_map([$this, 'process'], $this->promotionRepository->fetchByProject($contract->project->id)); } catch (Implement\Exception\EmptyResult) { return []; } } } /** * @param int $contract_id * @return array */ public function getActiveByContract(int $contract_id): array { try { return array_map([$this, 'process'], $this->promotionRepository->fetchActiveByContract($contract_id)); } catch (Implement\Exception\EmptyResult) { try { $contract = $this->contractRepository->fetchById($contract_id); return array_map([$this, 'process'], $this->promotionRepository->fetchActiveByProject($contract->project->id)); } catch (Implement\Exception\EmptyResult) { return []; } } } /** * @param array $data * @return Model\Venta\Promotion * @throws Exception\ServiceAction\Create */ public function add(array $data): Model\Venta\Promotion { try { $filteredData = $this->promotionRepository->filterData($data); #throw new \Exception(var_export($filteredData, true)); $promotion = $this->promotionRepository->create($filteredData); #throw new \Exception(var_export($promotion, true)); $promotion = $this->promotionRepository->save($promotion); return $this->process($promotion); } catch (PDOException $exception) { throw new Exception\ServiceAction\Create(__CLASS__, $exception); } } /** * @param Model\Venta\Promotion $promotion * @param array $data * @return Model\Venta\Promotion * @throws Exception\ServiceAction\Update */ public function edit(Model\Venta\Promotion $promotion, array $data): Model\Venta\Promotion { try { $filteredData = $this->promotionRepository->filterData($data); $promotion = $this->promotionRepository->edit($promotion, $filteredData); return $this->process($promotion); } catch (PDOException | Implement\Exception\EmptyResult $exception) { throw new Exception\ServiceAction\Update(__CLASS__, $exception); } } /** * @param int $promotion_id * @return Model\Venta\Promotion * @throws Exception\ServiceAction\Delete */ public function remove(int $promotion_id): Model\Venta\Promotion { try { $promotion = $this->promotionRepository->fetchById($promotion_id); $this->promotionRepository->remove($promotion); return $promotion; } catch (PDOException | Implement\Exception\EmptyResult $exception) { throw new Exception\ServiceAction\Delete(__CLASS__, $exception); } } /** * @param int $promotion_id * @param int $project_id * @return Model\Venta\Promotion * @throws Exception\ServiceAction\Create */ public function addProject(int $promotion_id, int $project_id): Model\Venta\Promotion { try { $promotion = $this->promotionRepository->fetchById($promotion_id); } catch (Implement\Exception\EmptyResult $exception) { throw new Exception\ServiceAction\Create(__CLASS__, $exception); } try { $project = $this->projectRepository->fetchById($project_id); } catch (Implement\Exception\EmptyResult $exception) { throw new Exception\ServiceAction\Create(__CLASS__, $exception); } if (in_array($project, $promotion->projects())) { return $this->process($promotion); } try { $this->promotionRepository->insertProjectForPromotion($promotion, $project->id); return $this->process($promotion); } catch (PDOException $exception) { throw new Exception\ServiceAction\Create(__CLASS__, $exception); } } /** * @param int $promotion_id * @param int $contract_id * @return Model\Venta\Promotion * @throws Exception\ServiceAction\Create */ public function addContract(int $promotion_id, int $contract_id): Model\Venta\Promotion { try { $promotion = $this->promotionRepository->fetchById($promotion_id); } catch (Implement\Exception\EmptyResult $exception) { throw new Exception\ServiceAction\Create(__CLASS__, $exception); } try { $contract = $this->contractRepository->fetchById($contract_id); } catch (Implement\Exception\EmptyResult $exception) { throw new Exception\ServiceAction\Create(__CLASS__, $exception); } if (in_array($contract, $promotion->contracts())) { return $this->process($promotion); } try { $this->promotionRepository->insertContractForPromotion($promotion, $contract->id); return $this->process($promotion); } catch (PDOException $exception) { throw new Exception\ServiceAction\Create(__CLASS__, $exception); } } /** * @param int $promotion_id * @param int $project_id * @param int $unitType_id * @return Model\Venta\Promotion * @throws Exception\ServiceAction\Create */ public function addUnitType(int $promotion_id, int $project_id, int $unitType_id): Model\Venta\Promotion { try { $promotion = $this->promotionRepository->fetchById($promotion_id); } catch (Implement\Exception\EmptyResult $exception) { throw new Exception\ServiceAction\Create(__CLASS__, $exception); } try { $project = $this->projectRepository->fetchById($project_id); } catch (Implement\Exception\EmptyResult $exception) { throw new Exception\ServiceAction\Create(__CLASS__, $exception); } try { $unitType = $this->tipoUnidadRepository->fetchById($unitType_id); } catch (Implement\Exception\EmptyResult $exception) { throw new Exception\ServiceAction\Create(__CLASS__, $exception); } if (in_array(['project' => $project, 'unitType' => $unitType], $promotion->unitTypes())) { return $this->process($promotion); } try { $units = $this->unidadRepository->fetchByProyectoAndTipo($project->id, $unitType->id); } catch (Implement\Exception\EmptyResult $exception) { throw new Exception\ServiceAction\Create(__CLASS__, $exception); } return $this->insertUnits($promotion, $units); } /** * @param int $promotion_id * @param int $unit_line_id * @return Model\Venta\Promotion * @throws Exception\ServiceAction\Create */ public function addUnitLine(int $promotion_id, int $unit_line_id): Model\Venta\Promotion { try { $promotion = $this->promotionRepository->fetchById($promotion_id); } catch (Implement\Exception\EmptyResult $exception) { throw new Exception\ServiceAction\Create(__CLASS__, $exception); } try { $unitLine = $this->proyectoTipoUnidadRepository->fetchById($unit_line_id); } catch (Implement\Exception\EmptyResult $exception) { throw new Exception\ServiceAction\Create(__CLASS__, $exception); } if (in_array($unitLine, $promotion->unitLines())) { return $this->process($promotion); } try { $units = $this->unidadRepository->fetchByProyectoTipoUnidad($unitLine->id); } catch (Implement\Exception\EmptyResult $exception) { throw new Exception\ServiceAction\Create(__CLASS__, $exception); } return $this->insertUnits($promotion, $units); } /** * @param int $promotion_id * @param int $unit_id * @return Model\Venta\Promotion * @throws Exception\ServiceAction\Create */ public function addUnit(int $promotion_id, int $unit_id): Model\Venta\Promotion { try { $promotion = $this->promotionRepository->fetchById($promotion_id); } catch (Implement\Exception\EmptyResult $exception) { throw new Exception\ServiceAction\Create(__CLASS__, $exception); } try { $unit = $this->unidadRepository->fetchById($unit_id); } catch (Implement\Exception\EmptyResult $exception) { throw new Exception\ServiceAction\Create(__CLASS__, $exception); } if (in_array($unit, $promotion->units())) { return $this->process($promotion); } try { $this->promotionRepository->insertUnitForPromotion($promotion, $unit->id); return $this->process($promotion); } catch (PDOException $exception) { throw new Exception\ServiceAction\Create(__CLASS__, $exception); } } public function removeProject(int $promotion_id, int $project_id): array { try { $promotion = $this->promotionRepository->fetchById($promotion_id); } catch (Implement\Exception\EmptyResult $exception) { throw new Exception\ServiceAction\Delete(__CLASS__, $exception); } try { $project = $this->projectRepository->fetchById($project_id); } catch (Implement\Exception\EmptyResult $exception) { throw new Exception\ServiceAction\Delete(__CLASS__, $exception); } try { $this->promotionRepository->removeProjectForPromotion($promotion, $project->id); return [ 'id' => '', 'promotion_id' => $promotion_id, 'project_id' => $project_id, ]; } catch (PDOException $exception) { throw new Exception\ServiceAction\Delete(__CLASS__, $exception); } } /** * @param Model\Venta\Promotion $promotion * @param array $units * @return Model\Venta\Promotion * @throws Exception\ServiceAction\Create */ protected function insertUnits(Model\Venta\Promotion $promotion, array $units): Model\Venta\Promotion { $errors = []; foreach ($units as $unit) { try { $this->promotionRepository->insertUnitForPromotion($promotion, $unit->id); } catch (PDOException | \Throwable $exception) { $this->logger->debug($exception); $errors []= $exception; } } if (count($errors) > 0) { $exception = new Exception\AggregateException($errors); throw new Exception\ServiceAction\Create(__CLASS__, $exception); } return $this->process($promotion); } protected function process(Model\Venta\Promotion $model): Model\Venta\Promotion { $model->addFactory('projects', (new Implement\Repository\Factory()) ->setCallable(function($promotion_id) { try { return $this->projectRepository->fetchByPromotion($promotion_id); } catch (Implement\Exception\EmptyResult) { return []; } }) ->setArgs(['promotion_id' => $model->id])); $model->addFactory('contracts', (new Implement\Repository\Factory()) ->setCallable(function($promotion_id) { try { return $this->contractRepository->fetchByPromotion($promotion_id); } catch (Implement\Exception\EmptyResult) { return []; } }) ->setArgs(['promotion_id' => $model->id])); $model->addFactory('unitTypes', (new Implement\Repository\Factory()) ->setCallable(function($promotion_id) { try { return array_map(function(array $ids) { return [ 'project' => $this->projectRepository->fetchById($ids['project_id']), 'unitType' => $this->tipoUnidadRepository->fetchById($ids['id']) ]; }, $this->tipoUnidadRepository->fetchByPromotion($promotion_id)); } catch (Implement\Exception\EmptyResult) { return []; } }) ->setArgs(['promotion_id' => $model->id]) ); $model->addFactory('unitLines', (new Implement\Repository\Factory()) ->setCallable(function($promotion_id) { try { return $this->proyectoTipoUnidadRepository->fetchByPromotion($promotion_id); } catch (Implement\Exception\EmptyResult) { return []; } }) ->setArgs(['promotion_id' => $model->id]) ); $model->addFactory('units', (new Implement\Repository\Factory()) ->setCallable(function($promotion_id) { try { return $this->unidadRepository->fetchByPromotion($promotion_id); } catch (Implement\Exception\EmptyResult) { return []; } }) ->setArgs(['promotion_id' => $model->id])); $model->addFactory('contractUnits', (new Implement\Repository\Factory()) ->setCallable(function($promotion_id) { try { $ids = $this->promotionRepository->fetchContractUnitsByPromotion($promotion_id); $contractUnits = []; foreach ($ids as $id) { try { $contract = $this->contractRepository->fetchById($id['contract_id']); $unidad = $this->unidadRepository->fetchById($id['unidad_id']); $contractUnits[]= (object) ['contract' => $contract, 'unit' => $unidad]; } catch (Implement\Exception\EmptyResult) {} } return $contractUnits; } catch (Implement\Exception\EmptyResult) { return []; } }) ->setArgs(['promotion_id' => $model->id])); return $model; } }