Promociones

This commit is contained in:
Juan Pablo Vial
2025-03-25 19:22:38 -03:00
parent d3b0026ca4
commit b191a01313
12 changed files with 286 additions and 8 deletions

View File

@ -10,10 +10,20 @@ use Incoviba\Service;
class Promotions extends Ideal\Controller
{
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, View $view, Service\Venta\Promotion $promotionService): ResponseInterface
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, View $view,
Service\Venta\Promotion $promotionService): ResponseInterface
{
$promotions = $promotionService->getAll('description');
return $view->render($response, 'ventas.promotions', ['promotions' => $promotions]);
}
public function show(ServerRequestInterface $request, ResponseInterface $response, View $view,
Service\Venta\Promotion $promotionService, int $promotion_id): ResponseInterface
{
$promotion = null;
try {
$promotion = $promotionService->getById($promotion_id);
} catch (ServiceAction\Read) {}
return $view->render($response, 'ventas.promotions.show', ['promotion' => $promotion]);
}
}

View File

@ -17,6 +17,15 @@ class Promotion extends Common\Ideal\Model
public Type $type;
public State $state = State::ACTIVE;
protected array $projects;
public function projects(): array
{
if (empty($this->projects)) {
$this->projects = $this->runFactory('projects');
}
return $this->projects;
}
protected array $contracts;
public function contracts(): array
{
@ -35,6 +44,15 @@ class Promotion extends Common\Ideal\Model
return $this->units;
}
protected array $contractUnits;
public function contractUnits(): array
{
if (empty($this->contractUnits)) {
$this->contractUnits = $this->runFactory('contractUnits');
}
return $this->contractUnits;
}
public function value(float $price): float
{
if ($this->type === Type::FIXED) {
@ -53,6 +71,7 @@ class Promotion extends Common\Ideal\Model
'valid_until' => $this->validUntil?->format('Y-m-d'),
'type' => $this->type,
'state' => $this->state,
'projects' => $this->projects() ?? [],
'contracts' => $this->contracts() ?? [],
'units' => $this->units() ?? []
];

View File

@ -164,6 +164,21 @@ class Proyecto extends Ideal\Repository
->where('inmobiliaria = ?');
return $this->fetchMany($query, [$inmobiliaria_rut]);
}
/**
* @param int $promotion_id
* @return array
* @throws Implement\Exception\EmptyResult
*/
public function fetchByPromotion(int $promotion_id): array
{
$query = $this->connection->getQueryBuilder()
->select('a.*')
->from("{$this->getTable()} a")
->joined('INNER JOIN promotion_projects pp ON pp.project_id = a.id')
->where('pp.promotion_id = :promotion_id');
return $this->fetchMany($query, ['promotion_id' => $promotion_id]);
}
/*public function fetchSuperficieVendido(int $proyecto_id): float
{

View File

@ -1,6 +1,8 @@
<?php
namespace Incoviba\Repository\Venta;
use PDO;
use PDOException;
use Incoviba\Common;
use Incoviba\Model;
use Incoviba\Repository\Proyecto\Broker;
@ -177,4 +179,29 @@ class Promotion extends Common\Ideal\Repository
->where('pc.contract_id = :contract_id AND pu.unit_id = :unit_id');
return $this->fetchOne($query, ['contract_id' => $contract_id, 'unit_id' => $unit_id]);
}
/**
* @param int $promotion_id
* @return array
* @throws Common\Implement\Exception\EmptyResult
*/
public function fetchContractUnitsByPromotion(int $promotion_id): array
{
$query = $this->connection->getQueryBuilder()
->select('contracts.id, unidad.id')
->from("{$this->getTable()} a")
->joined('INNER JOIN promotion_contract_units pcu ON pcu.promotion_id = a.id')
->joined('INNER JOIN unidad ON unidad.id = pcu.unit_id')
->joined('INNER JOIN contracts ON contracts.id = pcu.contract_id')
->where('a.id = :promotion_id');
try {
$result = $this->connection->execute($query, ['promotion_id' => $promotion_id])->fetchAll(PDO::FETCH_ASSOC);
if (empty($result)) {
throw new Common\Implement\Exception\EmptyResult($query);
}
return $result;
} catch (PDOException $exception) {
throw new Common\Implement\Exception\EmptyResult($query, $exception);
}
}
}

View File

@ -15,6 +15,7 @@ class Promotion extends Ideal\Service
{
public function __construct(LoggerInterface $logger,
protected Repository\Venta\Promotion $promotionRepository,
protected Repository\Proyecto $projectRepository,
protected Repository\Proyecto\Broker\Contract $contractRepository,
protected Repository\Venta\Unidad $unidadRepository)
{
@ -135,11 +136,50 @@ class Promotion extends Ideal\Service
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([$this->contractRepository, 'fetchByPromotion'])
->setCallable(function($promotion_id) {
try {
return $this->contractRepository->fetchByPromotion($promotion_id);
} catch (Implement\Exception\EmptyResult) {
return [];
}
})
->setArgs(['promotion_id' => $model->id]));
$model->addFactory('units', (new Implement\Repository\Factory())
->setCallable([$this->unidadRepository, 'fetchByPromotion'])
->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;
}