Listado de Precios para Contrato Broker

This commit is contained in:
Juan Pablo Vial
2025-03-13 12:18:08 -03:00
parent 346001db8e
commit 68aebdb4fe
18 changed files with 826 additions and 14 deletions

View File

@ -187,4 +187,5 @@ class Proyectos
}
return $this->withJson($response, $output);
}
}

View File

@ -5,9 +5,11 @@ use DateTimeImmutable;
use DateMalformedStringException;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Incoviba\Common\Implement;
use Incoviba\Controller\API\withJson;
use Incoviba\Service;
use Incoviba\Exception\ServiceAction;
use Incoviba\Repository;
use Incoviba\Service;
class Contracts
{
@ -140,4 +142,34 @@ class Contracts
return $this->withJson($response, $output);
}
public function promotions(ServerRequestInterface $request, ResponseInterface $response,
Service\Proyecto\Broker\Contract $contractService,
Repository\Venta\Unidad $unitRepository,
Repository\Venta\Promotion $promotionRepository,
int $contract_id): ResponseInterface
{
$input = $request->getParsedBody();
$output = [
'contract_id' => $contract_id,
'input' => $input,
'unidades' => [],
];
$unit_ids = $input['unidad_ids'];
if (is_string($unit_ids)) {
$unit_ids = json_decode($input['unidad_ids'], true);
}
foreach ($unit_ids as $unit_id) {
try {
$unit = $unitRepository->fetchById($unit_id);
$contractService->getById($contract_id);
$promotion = $promotionRepository->fetchByContractAndUnit($contract_id, $unit->id);
$output['unidades'] []= [
'id' => $unit->id,
'promotion' => $promotion
];
} catch (ServiceAction\Read | Implement\Exception\EmptyResult) {}
}
return $this->withJson($response, $output);
}
}

View File

@ -0,0 +1,85 @@
<?php
namespace Incoviba\Controller\API\Proyectos;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Incoviba\Common\Ideal;
use Incoviba\Common\Implement;
use Incoviba\Controller\API\withJson;
use Incoviba\Controller\withRedis;
use Incoviba\Exception;
use Incoviba\Model;
use Incoviba\Repository;
use Incoviba\Service;
class Unidades extends Ideal\Controller
{
use withJson;
use withRedis;
public function precios(ServerRequestInterface $request, ResponseInterface $response,
Service\Proyecto $proyectoService,
Repository\Venta\Precio $precioRepository,
int $proyecto_id): ResponseInterface
{
$input = $request->getParsedBody();
$output = [
'proyecto_id' => $proyecto_id,
'input' => $input,
'precios' => []
];
$unidad_ids = $input['unidad_ids'];
if (is_string($unidad_ids)) {
$unidad_ids = json_decode($input['unidad_ids'], true);
}
try {
$proyecto = $proyectoService->getById($proyecto_id);
foreach ($unidad_ids as $unidad_id) {
try {
$output['precios'][] = [
'id' => $unidad_id,
'precio' => $precioRepository->fetchVigenteByUnidad((int) $unidad_id)
];
} catch (Implement\Exception\EmptyResult) {}
}
} catch (Implement\Exception\EmptyResult) {}
return $this->withJson($response, $output);
}
public function estados(ServerRequestInterface $request, ResponseInterface $response,
Service\Proyecto $proyectoService,
Repository\Venta\Unidad $unidadRepository,
int $proyecto_id): ResponseInterface
{
$input = $request->getParsedBody();
$output = [
'proyecto_id' => $proyecto_id,
'input' => $input,
'estados' => []
];
$unidad_ids = $input['unidad_ids'];
if (is_string($unidad_ids)) {
$unidad_ids = json_decode($input['unidad_ids'], true);
}
try {
$proyecto = $proyectoService->getById($proyecto_id);
foreach ($unidad_ids as $unidad_id) {
try {
$unidad = $unidadRepository->fetchById($unidad_id);
} catch (Implement\Exception\EmptyResult) {
try {
$output['estados'][] = [
'id' => $unidad_id,
'sold' => $unidadRepository->fetchSoldByUnidad((int) $unidad_id)
];
} catch (Implement\Exception\EmptyResult) {
$output['estados'][] = [
'id' => $unidad_id,
'sold' => false
];
}
}
}
} catch (Implement\Exception\EmptyResult) {}
return $this->withJson($response, $output);
}
}

View File

@ -0,0 +1,24 @@
<?php
namespace Incoviba\Controller\Proyectos\Brokers;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Incoviba\Common\Alias\View;
use Incoviba\Common\Ideal\Controller;
use Incoviba\Exception;
use Incoviba\Service;
class Contracts extends Controller
{
public function show(ServerRequestInterface $request, ResponseInterface $response, View $view,
Service\Proyecto\Broker\Contract $contractService,
int $broker_rut, int $contract_id): ResponseInterface
{
$contract = null;
try {
$contract = $contractService->getById($contract_id);
} catch (Exception\ServiceAction\Read) {}
return $view->render($response, 'proyectos.brokers.contracts.show', compact('contract'));
}
}

View File

@ -32,6 +32,15 @@ class Contract extends Common\Ideal\Model
return $this->current;
}
protected array $promotions = [];
public function promotions(): array
{
if (count($this->promotions) === 0) {
$this->promotions = $this->runFactory('promotions');
}
return $this->promotions;
}
protected function jsonComplement(): array
{
return [

View File

@ -3,9 +3,11 @@ namespace Incoviba\Model\Venta;
use DateTimeInterface;
use Incoviba\Common;
use Incoviba\Model\Proyecto\Broker;
class Promotion extends Common\Ideal\Model
{
public Broker\Contract $contract;
public Precio $price;
public float $amount;
public DateTimeInterface $startDate;
@ -13,11 +15,18 @@ class Promotion extends Common\Ideal\Model
public DateTimeInterface $validUntil;
public int $state;
public function price(): float
{
return $this->price->valor * $this->amount;
}
protected function jsonComplement(): array
{
return [
'contract_rut' => $this->contract->id,
'price_id' => $this->price->id,
'amount' => $this->amount,
'price' => $this->price(),
'start_date' => $this->startDate->format('Y-m-d'),
'end_date' => $this->endDate->format('Y-m-d'),
'valid_until' => $this->validUntil->format('Y-m-d'),
@ -27,4 +36,4 @@ class Promotion extends Common\Ideal\Model
]
];
}
}
}

View File

@ -45,6 +45,18 @@ class Unidad extends Ideal\Model
return $precio;
}
protected bool $sold;
public function sold(): bool
{
if (!isset($this->sold)) {
$this->sold = false;
try {
$this->sold = $this->runFactory('sold') ?? false;
} catch (EmptyResult) {}
}
return $this->sold;
}
public function jsonSerialize(): mixed
{
$output = array_merge(parent::jsonSerialize(), [
@ -59,6 +71,7 @@ class Unidad extends Ideal\Model
$output['precios'] = $this->precios;
$output['current_precio'] = $this->currentPrecio;
}
$output['sold'] = $this->sold() ?? false;
return $output;
}
}

View File

@ -3,10 +3,11 @@ namespace Incoviba\Repository\Venta;
use Incoviba\Common;
use Incoviba\Model;
use Incoviba\Repository\Proyecto\Broker;
class Promotion extends Common\Ideal\Repository
{
public function __construct(Common\Define\Connection $connection, protected Precio $precioRepository)
public function __construct(Common\Define\Connection $connection, protected Broker\Contract $contractRepository, protected Precio $precioRepository)
{
parent::__construct($connection);
}
@ -18,31 +19,70 @@ class Promotion extends Common\Ideal\Repository
public function create(?array $data = null): Model\Venta\Promotion
{
$map = (new Implement\Repository\MapperParser(['amount', 'type']))
->register('price_id', (new Implement\Repository\Mapper())
$map = (new Common\Implement\Repository\MapperParser(['amount', 'type']))
->register('contract_id', (new Common\Implement\Repository\Mapper())
->setProperty('contract')
->setFunction(function($data) {
return $this->contractRepository->fetchById($data['contract_id']);
}))
->register('price_id', (new Common\Implement\Repository\Mapper())
->setProperty('price')
->setFunction(function($data) {
return $this->precioRepository->create($data);
}))
->register('start_date', new Implement\Repository\Mapper\DateTime('start_date', 'startDate'))
->register('end_date', new Implement\Repository\Mapper\DateTime('end_date', 'endDate'))
->register('valid_until', new Implement\Repository\Mapper\DateTime('valid_until', 'validUntil'));
->register('start_date', new Common\Implement\Repository\Mapper\DateTime('start_date', 'startDate'))
->register('end_date', new Common\Implement\Repository\Mapper\DateTime('end_date', 'endDate'))
->register('valid_until', new Common\Implement\Repository\Mapper\DateTime('valid_until', 'validUntil'));
return $this->parseData(new Model\Venta\Promotion(), $data, $map);
}
public function save(Common\Define\Model $model): Model\Venta\Promotion
{
$model->id = $this->saveNew(
['amount', 'type', 'start_date', 'end_date', 'valid_until', 'price_id'],
[$model->amount, $model->type, $model->startDate->format('Y-m-d'), $model->endDate->format('Y-m-d'), $model->validUntil->format('Y-m-d'), $model->price->id]
['contract_id', 'amount', 'type', 'start_date', 'end_date', 'valid_until', 'price_id'],
[$model->contract->id, $model->amount, $model->type, $model->startDate->format('Y-m-d'),
$model->endDate->format('Y-m-d'), $model->validUntil->format('Y-m-d'), $model->price->id]
);
return $model;
}
public function edit(Common\Define\Model $model, array $new_data): Model\Venta\Promotion
{
return $this->update($model, ['amount', 'type', 'start_date', 'end_date', 'valid_until', 'price_id'], $new_data);
return $this->update($model, ['contract_id', 'amount', 'type', 'start_date', 'end_date', 'valid_until', 'price_id'], $new_data);
}
/**
* @param int $contract_id
* @return array
* @throws Common\Implement\Exception\EmptyResult
*/
public function fetchByContract(int $contract_id): array
{
$query = $this->connection->getQueryBuilder()
->select()
->from($this->getTable())
->where('contract_id = :contract_id');
return $this->fetchMany($query, ['contract_id' => $contract_id]);
}
/**
* @param int $contract_id
* @return array
* @throws Common\Implement\Exception\EmptyResult
*/
public function fetchActiveByContract(int $contract_id): array
{
$query = $this->connection->getQueryBuilder()
->select()
->from($this->getTable())
->where('contract_id = :contract_id AND state = :state');
return $this->fetchMany($query, ['contract_id' => $contract_id, 'state' => Model\Venta\Promotion\State::ACTIVE]);
}
/**
* @param int $price_id
* @return array
* @throws Common\Implement\Exception\EmptyResult
*/
public function fetchByPrice(int $price_id): array
{
$query = $this->connection->getQueryBuilder()
@ -51,6 +91,12 @@ class Promotion extends Common\Ideal\Repository
->where('price_id = :price_id');
return $this->fetchMany($query, ['price_id' => $price_id]);
}
/**
* @param int $price_id
* @return array
* @throws Common\Implement\Exception\EmptyResult
*/
public function fetchActiveByPrice(int $price_id): array
{
$query = $this->connection->getQueryBuilder()
@ -59,4 +105,57 @@ class Promotion extends Common\Ideal\Repository
->where('price_id = :price_id AND state = :state');
return $this->fetchMany($query, ['price_id' => $price_id, 'state' => Model\Venta\Promotion\State::ACTIVE]);
}
/**
* @param int $project_id
* @return array
* @throws Common\Implement\Exception\EmptyResult
*/
public function fetchByProject(int $project_id): array
{
$query = $this->connection->getQueryBuilder()
->select('a.*')
->from("{$this->getTable()} a")
->joined('INNER JOIN precio ON precio.id = a.price_id')
->joined('INNER JOIN unidad ON unidad.id = precio.unidad')
->joined('INNER JOIN proyecto_tipo_unidad ON proyecto_tipo_unidad.id = unidad.pt')
->joined('INNER JOIN proyecto ON proyecto.id = proyecto_tipo_unidad.proyecto')
->where('proyecto.id = :project_id');
return $this->fetchMany($query, ['project_id' => $project_id]);
}
/**
* @param int $project_id
* @return array
* @throws Common\Implement\Exception\EmptyResult
*/
public function fetchActiveByProject(int $project_id): array
{
$query = $this->connection->getQueryBuilder()
->select('a.*')
->from("{$this->getTable()} a")
->joined('INNER JOIN precio ON precio.id = a.price_id')
->joined('INNER JOIN unidad ON unidad.id = precio.unidad')
->joined('INNER JOIN proyecto_tipo_unidad ON proyecto_tipo_unidad.id = unidad.pt')
->joined('INNER JOIN proyecto ON proyecto.id = proyecto_tipo_unidad.proyecto')
->where('proyecto.id = :project_id AND a.state = :state');
return $this->fetchMany($query, ['project_id' => $project_id, 'state' => Model\Venta\Promotion\State::ACTIVE]);
}
/**
* @param int $contract_id
* @param int $unit_id
* @return Model\Venta\Promotion
* @throws Common\Implement\Exception\EmptyResult
*/
public function fetchByContractAndUnit(int $contract_id, int $unit_id): Model\Venta\Promotion
{
$query = $this->connection->getQueryBuilder()
->select('a.*')
->from("{$this->getTable()} a")
->joined('INNER JOIN precio ON precio.id = a.price_id')
->joined('INNER JOIN unidad ON unidad.id = precio.unidad')
->where('a.contract_id = :contract_id AND unidad.id = :unit_id');
return $this->fetchOne($query, ['contract_id' => $contract_id, 'unit_id' => $unit_id]);
}
}

View File

@ -172,6 +172,18 @@ class Unidad extends Ideal\Repository
->group('unidad.id');
return $this->connection->execute($query, [$unidad_id])->fetch(PDO::FETCH_ASSOC);
}
public function fetchSoldByUnidad(int $unidad_id): Model\Venta\Unidad
{
$query = $this->connection->getQueryBuilder()
->select('a.*')
->from("{$this->getTable()} a")
->joined('INNER JOIN `propiedad_unidad` pu ON pu.`unidad` = a.`id`')
->joined('INNER JOIN `venta` ON `venta`.`propiedad` = `pu`.`propiedad`')
->joined('LEFT OUTER JOIN (SELECT ev1.* FROM `estado_venta` ev1 JOIN (SELECT MAX(`id`) as `id`, `venta` FROM `estado_venta`) ev0 ON ev0.`id` = ev1.`id`) ev ON ev.`venta` = `venta`.`id`')
->joined('LEFT OUTER JOIN `tipo_estado_venta` tev ON tev.`id` = ev.`estado`')
->where('a.id = :unidad_id AND tev.activa = 1');
return $this->fetchOne($query, ['unidad_id' => $unidad_id]);
}
protected function joinProrrateo(): string
{

View File

@ -16,7 +16,8 @@ class Contract extends Ideal\Service
{
public function __construct(LoggerInterface $logger,
protected Repository\Proyecto\Broker\Contract $contractRepository,
protected Repository\Proyecto\Broker\Contract\State $stateRepository)
protected Repository\Proyecto\Broker\Contract\State $stateRepository,
protected Repository\Venta\Promotion $promotionRepository)
{
parent::__construct($logger);
}
@ -145,6 +146,9 @@ class Contract extends Ideal\Service
$contract->addFactory('states', (new Implement\Repository\Factory())
->setCallable([$this->stateRepository, 'fetchByContract'])
->setArgs(['contract_id' => $contract->id]));
$contract->addFactory('promotions', (new Implement\Repository\Factory())
->setCallable([$this->promotionRepository, 'fetchByContract'])
->setArgs(['contract_id' => $contract->id]));
return $contract;
}
}

View File

@ -0,0 +1,85 @@
<?php
namespace Incoviba\Service\Venta;
use Psr\Log\LoggerInterface;
use Incoviba\Common\Ideal;
use Incoviba\Common\Implement;
use Incoviba\Exception;
use Incoviba\Model;
use Incoviba\Repository;
use Incoviba\Service;
class Promotion extends Ideal\Service
{
public function __construct(LoggerInterface $logger,
protected Repository\Venta\Promotion $promotionRepository,
protected Repository\Proyecto\Broker\Contract $contractRepository)
{
parent::__construct($logger);
}
public function getAll(null|string|array $order = null): array
{
try {
return array_map([$this, 'process'], $this->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 [];
}
}
}
protected function process(Model\Venta\Promotion $model): Model\Venta\Promotion
{
return $model;
}
}

View File

@ -1,9 +1,10 @@
<?php
namespace Incoviba\Service\Venta;
use Incoviba\Exception\ServiceAction\Read;
use PDOException;
use Incoviba\Common\Implement\Repository\Factory;
use Incoviba\Common\Implement\Exception\EmptyResult;
use Incoviba\Exception\ServiceAction\Read;
use Incoviba\Repository;
use Incoviba\Model;
@ -31,6 +32,10 @@ class Unidad
{
return array_map([$this, 'process'], $this->unidadRepository->fetchByCierre($cierre_id));
}
public function getByProyecto(int $proyecto_id): array
{
return array_map([$this, 'process'], $this->unidadRepository->fetchByProyecto($proyecto_id));
}
public function getDisponiblesByProyecto(int $proyecto_id): array
{
return $this->unidadRepository->fetchDisponiblesByProyecto($proyecto_id);
@ -62,6 +67,10 @@ class Unidad
$unidad->precios = $this->precioService->getByUnidad($unidad->id);
$unidad->currentPrecio = $this->precioService->getVigenteByUnidad($unidad->id);
} catch (Read) {}
$unidad->addFactory('sold', (new Factory())
->setCallable([$this->unidadRepository, 'fetchSoldByUnidad'])
->setArgs(['unidad_id' => $unidad->id])
);
return $unidad;
}
}