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'], [$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, ['amount', 'type', 'start_date', 'end_date', 'valid_until'], $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('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]); } /** * @param int $contract_id * @return array * @throws Common\Implement\Exception\EmptyResult */ public function fetchActiveByContract(int $contract_id): array { $query = $this->connection->getQueryBuilder() ->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 $unit_id * @return array * @throws Common\Implement\Exception\EmptyResult */ public function fetchByUnit(int $unit_id): array { $query = $this->connection->getQueryBuilder() ->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 $unit_id * @return array * @throws Common\Implement\Exception\EmptyResult */ public function fetchActiveByUnit(int $unit_id): array { $query = $this->connection->getQueryBuilder() ->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]); } /** * @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 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'); 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 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'); 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 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]); } }