Promociones para cada subdivicion

This commit is contained in:
Juan Pablo Vial
2025-04-03 16:32:40 -03:00
parent b5d6d0acb9
commit 7b2df74e4d
19 changed files with 495 additions and 247 deletions

View File

@ -17,6 +17,7 @@ class Promotion extends Ideal\Service
protected Repository\Venta\Promotion $promotionRepository,
protected Repository\Proyecto $projectRepository,
protected Repository\Proyecto\Broker\Contract $contractRepository,
protected Repository\Proyecto\Broker $brokerRepository,
protected Repository\Proyecto\TipoUnidad $tipoUnidadRepository,
protected Repository\Proyecto\ProyectoTipoUnidad $proyectoTipoUnidadRepository,
protected Repository\Venta\Unidad $unidadRepository)
@ -56,12 +57,7 @@ class Promotion extends Ideal\Service
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 [];
}
return [];
}
}
@ -74,12 +70,7 @@ class Promotion extends Ideal\Service
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 [];
}
return [];
}
}
@ -92,11 +83,8 @@ class Promotion extends Ideal\Service
{
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);
return $this->process($this->promotionRepository->save($promotion));
} catch (PDOException $exception) {
throw new Exception\ServiceAction\Create(__CLASS__, $exception);
}
@ -166,11 +154,11 @@ class Promotion extends Ideal\Service
/**
* @param int $promotion_id
* @param int $contract_id
* @param int $broker_rut
* @return Model\Venta\Promotion
* @throws Exception\ServiceAction\Create
*/
public function addContract(int $promotion_id, int $contract_id): Model\Venta\Promotion
public function addBroker(int $promotion_id, int $broker_rut): Model\Venta\Promotion
{
try {
$promotion = $this->promotionRepository->fetchById($promotion_id);
@ -178,15 +166,15 @@ class Promotion extends Ideal\Service
throw new Exception\ServiceAction\Create(__CLASS__, $exception);
}
try {
$contract = $this->contractRepository->fetchById($contract_id);
$broker = $this->brokerRepository->fetchById($broker_rut);
} catch (Implement\Exception\EmptyResult $exception) {
throw new Exception\ServiceAction\Create(__CLASS__, $exception);
}
if (in_array($contract, $promotion->contracts())) {
if (in_array($broker, $promotion->brokers())) {
return $this->process($promotion);
}
try {
$this->promotionRepository->insertContractForPromotion($promotion, $contract->id);
$this->promotionRepository->insertBrokerForPromotion($promotion, $broker->rut);
return $this->process($promotion);
} catch (PDOException $exception) {
throw new Exception\ServiceAction\Create(__CLASS__, $exception);
@ -221,11 +209,11 @@ class Promotion extends Ideal\Service
return $this->process($promotion);
}
try {
$units = $this->unidadRepository->fetchByProyectoAndTipo($project->id, $unitType->id);
} catch (Implement\Exception\EmptyResult $exception) {
$this->promotionRepository->insertUnitTypeForPromotion($promotion, $project->id, $unitType->id);
return $this->process($promotion);
} catch (PDOException $exception) {
throw new Exception\ServiceAction\Create(__CLASS__, $exception);
}
return $this->insertUnits($promotion, $units);
}
/**
@ -250,11 +238,11 @@ class Promotion extends Ideal\Service
return $this->process($promotion);
}
try {
$units = $this->unidadRepository->fetchByProyectoTipoUnidad($unitLine->id);
} catch (Implement\Exception\EmptyResult $exception) {
$this->promotionRepository->insertUnitLineForPromotion($promotion, $unitLine->id);
return $this->process($promotion);
} catch (PDOException $exception) {
throw new Exception\ServiceAction\Create(__CLASS__, $exception);
}
return $this->insertUnits($promotion, $units);
}
/**
@ -286,7 +274,13 @@ class Promotion extends Ideal\Service
}
}
public function removeProject(int $promotion_id, int $project_id): array
/**
* @param int $promotion_id
* @param int $project_id
* @return void
* @throws Exception\ServiceAction\Delete
*/
public function removeProject(int $promotion_id, int $project_id): void
{
try {
$promotion = $this->promotionRepository->fetchById($promotion_id);
@ -300,39 +294,115 @@ class Promotion extends Ideal\Service
}
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
* @param int $promotion_id
* @param int $broker_rut
* @return void
* @throws Exception\ServiceAction\Delete
*/
protected function insertUnits(Model\Venta\Promotion $promotion, array $units): Model\Venta\Promotion
public function removeBroker(int $promotion_id, int $broker_rut): void
{
$errors = [];
foreach ($units as $unit) {
try {
$this->promotionRepository->insertUnitForPromotion($promotion, $unit->id);
} catch (PDOException | \Throwable $exception) {
$this->logger->debug($exception);
$errors []= $exception;
}
try {
$promotion = $this->promotionRepository->fetchById($promotion_id);
} catch (Implement\Exception\EmptyResult $exception) {
throw new Exception\ServiceAction\Delete(__CLASS__, $exception);
}
if (count($errors) > 0) {
$exception = new Exception\AggregateException($errors);
throw new Exception\ServiceAction\Create(__CLASS__, $exception);
try {
$broker = $this->brokerRepository->fetchById($broker_rut);
} catch (Implement\Exception\EmptyResult $exception) {
throw new Exception\ServiceAction\Delete(__CLASS__, $exception);
}
try {
$this->promotionRepository->removeBrokerForPromotion($promotion, $broker->rut);
} catch (PDOException $exception) {
throw new Exception\ServiceAction\Delete(__CLASS__, $exception);
}
}
/**
* @param int $promotion_id
* @param int $project_id
* @param int $unitType_id
* @return void
* @throws Exception\ServiceAction\Delete
*/
public function removeUnitType(int $promotion_id, int $project_id, int $unitType_id): void
{
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 {
$unitType = $this->tipoUnidadRepository->fetchById($unitType_id);
} catch (Implement\Exception\EmptyResult $exception) {
throw new Exception\ServiceAction\Delete(__CLASS__, $exception);
}
try {
$this->promotionRepository->removeUnitTypeForPromotion($promotion, $project->id, $unitType->id);
} catch (PDOException $exception) {
throw new Exception\ServiceAction\Delete(__CLASS__, $exception);
}
}
/**
* @param int $promotion_id
* @param int $unit_line_id
* @return void
* @throws Exception\ServiceAction\Delete
*/
public function removeUnitLine(int $promotion_id, int $unit_line_id): void
{
try {
$promotion = $this->promotionRepository->fetchById($promotion_id);
} catch (Implement\Exception\EmptyResult $exception) {
throw new Exception\ServiceAction\Delete(__CLASS__, $exception);
}
try {
$unitLine = $this->proyectoTipoUnidadRepository->fetchById($unit_line_id);
} catch (Implement\Exception\EmptyResult $exception) {
throw new Exception\ServiceAction\Delete(__CLASS__, $exception);
}
try {
$this->promotionRepository->removeUnitLineForPromotion($promotion, $unitLine->id);
} catch (PDOException $exception) {
throw new Exception\ServiceAction\Delete(__CLASS__, $exception);
}
}
/**
* @param int $promotion_id
* @param int $unit_id
* @return void
* @throws Exception\ServiceAction\Delete
*/
public function removeUnit(int $promotion_id, int $unit_id): void
{
try {
$promotion = $this->promotionRepository->fetchById($promotion_id);
} catch (Implement\Exception\EmptyResult $exception) {
throw new Exception\ServiceAction\Delete(__CLASS__, $exception);
}
try {
$unit = $this->unidadRepository->fetchById($unit_id);
} catch (Implement\Exception\EmptyResult $exception) {
throw new Exception\ServiceAction\Delete(__CLASS__, $exception);
}
try {
$this->promotionRepository->removeUnitForPromotion($promotion, $unit->id);
} catch (PDOException $exception) {
throw new Exception\ServiceAction\Delete(__CLASS__, $exception);
}
return $this->process($promotion);
}
protected function process(Model\Venta\Promotion $model): Model\Venta\Promotion
@ -346,10 +416,10 @@ class Promotion extends Ideal\Service
}
})
->setArgs(['promotion_id' => $model->id]));
$model->addFactory('contracts', (new Implement\Repository\Factory())
$model->addFactory('brokers', (new Implement\Repository\Factory())
->setCallable(function($promotion_id) {
try {
return $this->contractRepository->fetchByPromotion($promotion_id);
return $this->brokerRepository->fetchByPromotion($promotion_id);
} catch (Implement\Exception\EmptyResult) {
return [];
}
@ -389,24 +459,6 @@ class Promotion extends Ideal\Service
}
})
->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;
}
}