<?php
/**
 * @copyright (c) Kufer Software GmbH, Oderstraße 7, D-84453 Mühldorf
 */

if (!defined('BASEDIR')) {
  include_once '..' . DIRECTORY_SEPARATOR . 'constants.php';
}



class DBWorkFerien extends DBWorkGenerated
{
  public function __construct($pDBTyp = 1, $pNewInstance = false)
  {
    parent::__construct($pDBTyp, $pNewInstance);
    $this->select = 'id, kal_nr, beginn, ende, text, bearbeiter, bearbdatum, erfasstam, erfasstvon';
  }

  
  public function getNewObject()
  {
    return new Ferien();
  }

  public function getTableName(): string
  {
    return 'ferien';
  }

  
  public function getCurrentRow($pRefreshCache = false)
  {
    $tmp = $this->getNewObject();

    $tmp->id = $this->getDatamodulValue('id');
    $tmp->text = $this->getDatamodulValue('text');
    $tmp->bearbeiter = $this->getDatamodulValue('bearbeiter');
    $tmp->erfasstvon = $this->getDatamodulValue('erfasstvon');
    $tmp->beginn = $this->getDatamodulValue('beginn');
    $tmp->ende = $this->getDatamodulValue('ende');
    $tmp->bearbdatum = $this->getDatamodulValue('bearbdatum');
    $tmp->erfasstam = $this->getDatamodulValue('erfasstam');
    $tmp->kal_nr = $this->getDatamodulValue('kal_nr');

    return $tmp;
  }

  
  public function getFerienFromKalender($pKalId)
  {
    $kalId = $this->dboperation->getInt4Sql($pKalId);
    $this->dboperation->clear();
    $sql = 'SELECT ' . $this->select . " FROM ferien fe \n
             WHERE fe.kal_nr='" . $kalId . "' \n";
    
    $sql .= $this->getSearchSQL('fe');
    $this->dboperation->query($sql);

    return $this->getAllRows();
  }

  
  public function getSearchSQL($pPraeffix)
  {
    $praeffix = $this->dboperation->getString4Sql($pPraeffix);
    
    
    
    $sql = '';
    if (isset($this->fromobject)) {
      if (isset($this->fromobject->beginn)) {
        $sql .= ' AND ' . $praeffix . '.ende >= ' . $this->datetimeop->getFormatDate4Server($this->fromobject->beginn);
      }
      if (isset($this->fromobject->ende)) {
        $sql .= ' AND ' . $praeffix . '.beginn <= ' . $this->datetimeop->getFormatDate4Server($this->fromobject->ende);
      }
    }

    return $sql;
  }

  public function getAllQuery(): \SelectQueryBuilder
  {
    return $this->getSelectQueryBuilder()
      ->select($this->select)
      ->from($this->getTableNameWithoutAlias(), $this->getTablePraeffix())
      ->paramRaw('table', $this->getTablePraeffix());
  }

  public function findByFilterQuery(\Kufer\Web\Classes\Dbfilter\FerienSQLFilter $filter): \SelectQueryBuilder
  {
    $condition = $filter->createSql($this->dboperation);

    return $this->getAllQuery()
      ->and($condition)
      ->orderBySrcCode($this->getCompleteOrderByDefault('id', $this->getTablePraeffix()));
  }

  
  public function findByFilter(\Kufer\Web\Classes\Dbfilter\FerienSQLFilter $filter): array
  {
    $builder = $this->findByFilterQuery($filter);

    $this->dboperation->clear();
    $this->dboperation->query($builder->getSQL());

    return $this->getAllRows();
  }

  
  public function findIdsByFilter(\Kufer\Web\Classes\Dbfilter\FerienSQLFilter $filter): array
  {
    $builder = $this->findByFilterQuery($filter)
      ->select(':table:.id');

    $this->dboperation->clear();
    $this->dboperation->query($builder->getSQL());

    return $this->getAllRowsSingleColumn('id');
  }

  
  public function countByFilter(\Kufer\Web\Classes\Dbfilter\FerienSQLFilter $filter): int
  {
    $builder = $this->findByFilterQuery($filter)
      ->select('COUNT(*) AS anzahl');

    $this->dboperation->clear();
    $this->dboperation->query($builder->getSQLForInnerQuery());

    $this->dboperation->selectrow(0);

    return (int)$this->getValue('anzahl', \DatamodulVarType::TYPE_INT);
  }

  public function existsByFilter(\Kufer\Web\Classes\Dbfilter\FerienSQLFilter $filter): bool
  {
    return $this->countByFilter($filter) > 0;
  }
}