2025-02-18 16:02:10 -03:00
|
|
|
<?php
|
|
|
|
namespace Incoviba\Repository\Venta;
|
|
|
|
|
2025-03-25 19:22:38 -03:00
|
|
|
use PDO;
|
|
|
|
use PDOException;
|
2025-02-18 16:02:10 -03:00
|
|
|
use Incoviba\Common;
|
|
|
|
use Incoviba\Model;
|
2025-03-13 12:18:08 -03:00
|
|
|
use Incoviba\Repository\Proyecto\Broker;
|
2025-02-18 16:02:10 -03:00
|
|
|
|
|
|
|
class Promotion extends Common\Ideal\Repository
|
|
|
|
{
|
2025-03-13 12:18:08 -03:00
|
|
|
public function __construct(Common\Define\Connection $connection, protected Broker\Contract $contractRepository, protected Precio $precioRepository)
|
2025-02-18 16:02:10 -03:00
|
|
|
{
|
|
|
|
parent::__construct($connection);
|
|
|
|
}
|
|
|
|
|
2025-02-24 12:39:42 -03:00
|
|
|
public function getTable(): string
|
|
|
|
{
|
|
|
|
return 'promotions';
|
|
|
|
}
|
|
|
|
|
2025-02-18 16:02:10 -03:00
|
|
|
public function create(?array $data = null): Model\Venta\Promotion
|
|
|
|
{
|
2025-03-18 19:13:47 -03:00
|
|
|
$map = (new Common\Implement\Repository\MapperParser(['description', 'amount']))
|
|
|
|
->register('type', (new Common\Implement\Repository\Mapper())
|
|
|
|
->setFunction(function($data) {
|
|
|
|
return Model\Venta\Promotion\Type::from($data['type']);
|
|
|
|
}))
|
|
|
|
->register('state', (new Common\Implement\Repository\Mapper())
|
|
|
|
->setFunction(function($data) {
|
|
|
|
return Model\Venta\Promotion\State::from($data['state']);
|
|
|
|
})
|
|
|
|
->setDefault(Model\Venta\Promotion\State::ACTIVE))
|
2025-03-13 12:18:08 -03:00
|
|
|
->register('start_date', new Common\Implement\Repository\Mapper\DateTime('start_date', 'startDate'))
|
2025-03-18 19:13:47 -03:00
|
|
|
->register('end_date', (new Common\Implement\Repository\Mapper\DateTime('end_date', 'endDate'))
|
|
|
|
->setDefault(null))
|
|
|
|
->register('valid_until', (new Common\Implement\Repository\Mapper\DateTime('valid_until', 'validUntil'))
|
|
|
|
->setDefault(null));
|
2025-02-18 16:02:10 -03:00
|
|
|
|
|
|
|
return $this->parseData(new Model\Venta\Promotion(), $data, $map);
|
|
|
|
}
|
2025-03-18 19:13:47 -03:00
|
|
|
|
2025-02-18 16:02:10 -03:00
|
|
|
public function save(Common\Define\Model $model): Model\Venta\Promotion
|
|
|
|
{
|
|
|
|
$model->id = $this->saveNew(
|
2025-03-18 19:13:47 -03:00
|
|
|
['description', 'amount', 'type', 'start_date', 'end_date', 'valid_until'],
|
|
|
|
[$model->description, $model->amount, $model->type->value, $model->startDate->format('Y-m-d'),
|
|
|
|
$model->endDate?->format('Y-m-d'), $model->validUntil?->format('Y-m-d')]
|
2025-02-18 16:02:10 -03:00
|
|
|
);
|
|
|
|
return $model;
|
|
|
|
}
|
|
|
|
public function edit(Common\Define\Model $model, array $new_data): Model\Venta\Promotion
|
|
|
|
{
|
2025-03-18 19:13:47 -03:00
|
|
|
return $this->update($model, ['description', 'amount', 'type', 'start_date', 'end_date', 'valid_until'], $new_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function filterData(array $data): array
|
|
|
|
{
|
|
|
|
$filteredData = array_intersect_key($data, array_flip(['description', 'amount', 'type', 'start_date', 'end_date', 'valid_until']));
|
|
|
|
if (!isset($filteredData['amount']) and isset($data['value'])) {
|
|
|
|
$filteredData['amount'] = $data['value'];
|
|
|
|
}
|
|
|
|
$filteredData['type'] = (int) $filteredData['type'];
|
|
|
|
if ($filteredData['amount'] > 1 and $filteredData['type'] === Model\Venta\Promotion\Type::VARIABLE->value) {
|
|
|
|
$filteredData['amount'] = $filteredData['amount'] / 100;
|
|
|
|
}
|
|
|
|
return $filteredData;
|
2025-03-13 12:18:08 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $contract_id
|
|
|
|
* @return array
|
|
|
|
* @throws Common\Implement\Exception\EmptyResult
|
|
|
|
*/
|
|
|
|
public function fetchByContract(int $contract_id): array
|
|
|
|
{
|
|
|
|
$query = $this->connection->getQueryBuilder()
|
2025-03-17 22:49:48 -03:00
|
|
|
->select('a.*')
|
|
|
|
->from("{$this->getTable()} a")
|
|
|
|
->joined('INNER JOIN promotion_contracts pc ON pc.promotion_id = a.id')
|
|
|
|
->where('pc.contract_id = :contract_id');
|
2025-03-13 12:18:08 -03:00
|
|
|
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()
|
2025-03-17 22:49:48 -03:00
|
|
|
->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');
|
2025-03-13 12:18:08 -03:00
|
|
|
return $this->fetchMany($query, ['contract_id' => $contract_id, 'state' => Model\Venta\Promotion\State::ACTIVE]);
|
2025-02-18 16:02:10 -03:00
|
|
|
}
|
|
|
|
|
2025-03-13 12:18:08 -03:00
|
|
|
/**
|
2025-03-17 22:49:48 -03:00
|
|
|
* @param int $unit_id
|
2025-03-13 12:18:08 -03:00
|
|
|
* @return array
|
|
|
|
* @throws Common\Implement\Exception\EmptyResult
|
|
|
|
*/
|
2025-03-17 22:49:48 -03:00
|
|
|
public function fetchByUnit(int $unit_id): array
|
2025-02-18 16:02:10 -03:00
|
|
|
{
|
|
|
|
$query = $this->connection->getQueryBuilder()
|
2025-03-17 22:49:48 -03:00
|
|
|
->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]);
|
2025-02-18 16:02:10 -03:00
|
|
|
}
|
2025-03-13 12:18:08 -03:00
|
|
|
|
|
|
|
/**
|
2025-03-17 22:49:48 -03:00
|
|
|
* @param int $unit_id
|
2025-03-13 12:18:08 -03:00
|
|
|
* @return array
|
|
|
|
* @throws Common\Implement\Exception\EmptyResult
|
|
|
|
*/
|
2025-03-17 22:49:48 -03:00
|
|
|
public function fetchActiveByUnit(int $unit_id): array
|
2025-02-18 16:02:10 -03:00
|
|
|
{
|
|
|
|
$query = $this->connection->getQueryBuilder()
|
2025-03-17 22:49:48 -03:00
|
|
|
->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]);
|
2025-02-18 16:02:10 -03:00
|
|
|
}
|
2025-03-13 12:18:08 -03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @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")
|
2025-03-17 22:49:48 -03:00
|
|
|
->joined('INNER JOIN promotion_units pu ON pu.promotion_id = a.id')
|
|
|
|
->joined('INNER JOIN unidad ON unidad.id = pu.unit_id')
|
2025-03-13 12:18:08 -03:00
|
|
|
->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")
|
2025-03-17 22:49:48 -03:00
|
|
|
->joined('INNER JOIN promotion_units pu ON pu.promotion_id = a.id')
|
|
|
|
->joined('INNER JOIN unidad ON unidad.id = pu.unit_id')
|
2025-03-13 12:18:08 -03:00
|
|
|
->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")
|
2025-03-17 22:49:48 -03:00
|
|
|
->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');
|
2025-03-13 12:18:08 -03:00
|
|
|
return $this->fetchOne($query, ['contract_id' => $contract_id, 'unit_id' => $unit_id]);
|
|
|
|
}
|
2025-03-25 19:22:38 -03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @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);
|
|
|
|
}
|
|
|
|
}
|
2025-02-24 12:39:42 -03:00
|
|
|
}
|