Conecciones de Promociones

This commit is contained in:
Juan Pablo Vial
2025-04-03 13:15:56 -03:00
parent ced673e452
commit 8a1e6a7761
17 changed files with 1144 additions and 79 deletions

View File

@ -11,9 +11,22 @@ class Proyecto
protected Repository\Proyecto $proyectoRepository,
protected Repository\Proyecto\EstadoProyecto $estadoProyecto
) {}
public function getVendibles(): array
/**
* @param string|array|null $orderBy
* @return array
*/
public function getAll(null|string|array $orderBy = null): array
{
return $this->proyectoRepository->fetchAllActive();
try {
return array_map([$this, 'process'], $this->proyectoRepository->fetchAll($orderBy));
} catch (Implement\Exception\EmptyResult) {
return [];
}
}
public function getVendibles(null|string|array $orderBy = null): array
{
return $this->proyectoRepository->fetchAllActive($orderBy);
}
public function getEscriturando(): array
{

View File

@ -58,12 +58,13 @@ class Broker extends Ideal\Service
}
}
/**
* @param null|string|array $orderBy
* @return array
*/
public function getAll(): array
public function getAll(null|string|array $orderBy = null): array
{
try {
return array_map([$this, 'process'], $this->brokerRepository->fetchAll());
return array_map([$this, 'process'], $this->brokerRepository->fetchAll($orderBy));
} catch (EmptyResult) {
return [];
}

View File

@ -17,6 +17,8 @@ class Promotion extends Ideal\Service
protected Repository\Venta\Promotion $promotionRepository,
protected Repository\Proyecto $projectRepository,
protected Repository\Proyecto\Broker\Contract $contractRepository,
protected Repository\Proyecto\TipoUnidad $tipoUnidadRepository,
protected Repository\Proyecto\ProyectoTipoUnidad $proyectoTipoUnidadRepository,
protected Repository\Venta\Unidad $unidadRepository)
{
parent::__construct($logger);
@ -133,6 +135,205 @@ class Promotion extends Ideal\Service
}
}
/**
* @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
{
@ -154,6 +355,31 @@ class Promotion extends Ideal\Service
}
})
->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 {