Separacion de Promocion de Contrato y Precio

"Simplificacion" de datos en listado de precios
This commit is contained in:
Juan Pablo Vial
2025-03-17 22:49:48 -03:00
parent 2e49e2c947
commit bae0f1f555
13 changed files with 479 additions and 278 deletions

View File

@ -7,33 +7,43 @@ use Incoviba\Model\Proyecto\Broker;
class Promotion extends Common\Ideal\Model
{
public Broker\Contract $contract;
public Precio $price;
public float $amount;
public DateTimeInterface $startDate;
public DateTimeInterface $endDate;
public DateTimeInterface $validUntil;
public int $state;
public function price(): float
protected array $contracts;
public function contracts(): array
{
return $this->price->valor * $this->amount;
if (empty($this->contracts)) {
$this->contracts = $this->runFactory('contracts');
}
return $this->contracts;
}
protected array $units;
public function units(): array
{
if (empty($this->units)) {
$this->units = $this->runFactory('units');
}
return $this->units;
}
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'),
'state' => [
'id' => $this->state,
'description' => Promotion\State::name($this->state)
]
],
'contracts' => $this->contracts() ?? [],
'units' => $this->units() ?? []
];
}
}

View File

@ -128,6 +128,21 @@ class Contract extends Common\Ideal\Repository
return $this->fetchOne($query, ['project_id' => $projectId, 'broker_rut' => $brokerRut, 'state' => Model\Proyecto\Broker\Contract\State\Type::ACTIVE->value]);
}
/**
* @param int $promotion_id
* @return array
* @throws Common\Implement\Exception\EmptyResult
*/
public function fetchByPromotion(int $promotion_id): array
{
$query = $this->connection->getQueryBuilder()
->select('a.*')
->from("{$this->getTable()} a")
->joined('INNER JOIN promotion_contracts pc ON pc.contract_id = a.id')
->where('pc.promotion_id = :promotion_id');
return $this->fetchMany($query, ['promotion_id' => $promotion_id]);
}
protected function statusJoin(): string
{
return 'INNER JOIN (SELECT bcs1.* FROM broker_contract_states bcs1 INNER JOIN (SELECT MAX(id) AS id, contract_id FROM broker_contract_states GROUP BY contract_id) bcs0 ON bcs0.id = bcs1.id) bcs ON bcs.contract_id = a.id';

View File

@ -20,16 +20,6 @@ class Promotion extends Common\Ideal\Repository
public function create(?array $data = null): Model\Venta\Promotion
{
$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 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'));
@ -39,15 +29,15 @@ class Promotion extends Common\Ideal\Repository
public function save(Common\Define\Model $model): Model\Venta\Promotion
{
$model->id = $this->saveNew(
['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]
['amount', 'type', 'start_date', 'end_date', 'valid_until'],
[$model->amount, $model->type, $model->startDate->format('Y-m-d'),
$model->endDate->format('Y-m-d'), $model->validUntil->format('Y-m-d')]
);
return $model;
}
public function edit(Common\Define\Model $model, array $new_data): Model\Venta\Promotion
{
return $this->update($model, ['contract_id', 'amount', 'type', 'start_date', 'end_date', 'valid_until', 'price_id'], $new_data);
return $this->update($model, ['amount', 'type', 'start_date', 'end_date', 'valid_until'], $new_data);
}
/**
@ -58,9 +48,10 @@ class Promotion extends Common\Ideal\Repository
public function fetchByContract(int $contract_id): array
{
$query = $this->connection->getQueryBuilder()
->select()
->from($this->getTable())
->where('contract_id = :contract_id');
->select('a.*')
->from("{$this->getTable()} a")
->joined('INNER JOIN promotion_contracts pc ON pc.promotion_id = a.id')
->where('pc.contract_id = :contract_id');
return $this->fetchMany($query, ['contract_id' => $contract_id]);
}
@ -72,38 +63,41 @@ class Promotion extends Common\Ideal\Repository
public function fetchActiveByContract(int $contract_id): array
{
$query = $this->connection->getQueryBuilder()
->select()
->from($this->getTable())
->where('contract_id = :contract_id AND state = :state');
->select('a.*')
->from("{$this->getTable()} a")
->joined('INNER JOIN promotion_contracts pc ON pc.promotion_id = a.id')
->where('pc.contract_id = :contract_id AND a.state = :state');
return $this->fetchMany($query, ['contract_id' => $contract_id, 'state' => Model\Venta\Promotion\State::ACTIVE]);
}
/**
* @param int $price_id
* @param int $unit_id
* @return array
* @throws Common\Implement\Exception\EmptyResult
*/
public function fetchByPrice(int $price_id): array
public function fetchByUnit(int $unit_id): array
{
$query = $this->connection->getQueryBuilder()
->select()
->from($this->getTable())
->where('price_id = :price_id');
return $this->fetchMany($query, ['price_id' => $price_id]);
->select('a.*')
->from("{$this->getTable()} a")
->joined('INNER JOIN promotion_units pu ON pu.promotion_id = a.id')
->where('pu.unit_id = :unit_id');
return $this->fetchMany($query, ['unit_id' => $unit_id]);
}
/**
* @param int $price_id
* @param int $unit_id
* @return array
* @throws Common\Implement\Exception\EmptyResult
*/
public function fetchActiveByPrice(int $price_id): array
public function fetchActiveByUnit(int $unit_id): array
{
$query = $this->connection->getQueryBuilder()
->select()
->from($this->getTable())
->where('price_id = :price_id AND state = :state');
return $this->fetchMany($query, ['price_id' => $price_id, 'state' => Model\Venta\Promotion\State::ACTIVE]);
->select('a.*')
->from("{$this->getTable()} a")
->joined('INNER JOIN promotion_units pu ON pu.promotion_id = a.id')
->where('pu.unit_id = :unit_id AND a.state = :state');
return $this->fetchMany($query, ['unit_id' => $unit_id, 'state' => Model\Venta\Promotion\State::ACTIVE]);
}
/**
@ -116,8 +110,8 @@ class Promotion extends Common\Ideal\Repository
$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 promotion_units pu ON pu.promotion_id = a.id')
->joined('INNER JOIN unidad ON unidad.id = pu.unit_id')
->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');
@ -134,8 +128,8 @@ class Promotion extends Common\Ideal\Repository
$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 promotion_units pu ON pu.promotion_id = a.id')
->joined('INNER JOIN unidad ON unidad.id = pu.unit_id')
->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');
@ -153,9 +147,9 @@ class Promotion extends Common\Ideal\Repository
$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');
->joined('INNER JOIN promotion_contracts pc ON pc.promotion_id = a.id')
->joined('INNER JOIN promotion_units pu ON pu.promotion_id = a.id')
->where('pc.contract_id = :contract_id AND pu.unit_id = :unit_id');
return $this->fetchOne($query, ['contract_id' => $contract_id, 'unit_id' => $unit_id]);
}
}

View File

@ -185,6 +185,21 @@ class Unidad extends Ideal\Repository
return $this->fetchOne($query, ['unidad_id' => $unidad_id]);
}
/**
* @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_units` pu ON pu.`unit_id` = a.`id`')
->where('pu.`promotion_id` = :promotion_id');
return $this->fetchMany($query, ['promotion_id' => $promotion_id]);
}
protected function joinProrrateo(): string
{
return "LEFT OUTER JOIN unidad_prorrateo up ON up.unidad_id = a.id";

View File

@ -13,7 +13,8 @@ class Promotion extends Ideal\Service
{
public function __construct(LoggerInterface $logger,
protected Repository\Venta\Promotion $promotionRepository,
protected Repository\Proyecto\Broker\Contract $contractRepository)
protected Repository\Proyecto\Broker\Contract $contractRepository,
protected Repository\Venta\Unidad $unidadRepository)
{
parent::__construct($logger);
}
@ -80,6 +81,12 @@ class Promotion extends Ideal\Service
protected function process(Model\Venta\Promotion $model): Model\Venta\Promotion
{
$model->addFactory('contracts', (new Implement\Repository\Factory())
->setCallable([$this->contractRepository, 'fetchByPromotion'])
->setArgs(['promotion_id' => $model->id]));
$model->addFactory('units', (new Implement\Repository\Factory())
->setCallable([$this->unidadRepository, 'fetchByPromotion'])
->setArgs(['promotion_id' => $model->id]));
return $model;
}
}