Listado de Precios para Contrato Broker
This commit is contained in:
@ -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]);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user