<?php
/**
 * @copyright (c) Kufer Software GmbH, Loisachstrasse 4, D-84453 Mühldorf
 */

class DBWorkZustimmungAd extends DBWorkGenerated
{
  
  public const FUNCID_HYGIENEKONZEPT = 1;

  public function __construct($pDBnr = 1, $pNewInstance = false)
  {
    parent::__construct($pDBnr, $pNewInstance);
    $this->_dataModulFields = array(
      'id', 'funcid',
      'anztabtyp', 'anztabid',
      'oktabtyp', 'oktabid', 'okdatum', 'okweg', 'okdokdat', 'okwf',
      'erfasstvon', 'erfasstam', 'bearbeiter', 'bearbdatum',
      'info'
    );
    $this->select = implode(', ', $this->_dataModulFields);
  }

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

  public function getNewObject()
  {
    return new \ZustimmungAd();
  }

  public function getAllQuery(): \SelectQueryBuilder
  {
    return $this->getSelectQueryBuilder()
      ->select($this->select)
      ->from($this->getTableName());
  }

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

    return $this->getAllQuery()
      ->and($condition);
  }

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

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

    return $this->getAllRows();
  }

  
  public function countByFilter(\Kufer\Web\Classes\Dbfilter\ZustimmungAdSQLFilter $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\ZustimmungAdSQLFilter $filter): bool
  {
    return $this->countByFilter($filter) > 0;
  }
}