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


class DBWorkHLMandant extends \DBWorkGenerated
{
  public function __construct($pDBTyp = 1, $pNewInstance = false)
  {
    parent::__construct($pDBTyp, $pNewInstance);
    $this->select = 'id, name, dvvnr, lsmdrefidprae, lsmdrefidpost, lsmdrefidstellen, kostenst, geb_kurtaxe, kennz, absender_name, absender_email';
    $this->_dataModulFields = array('id', 'name', 'dvvnr', 'lsmdrefidprae', 'lsmdrefidpost', 'lsmdrefidstellen', 'kostenst', 'geb_kurtaxe', 'kennz', 'absender_name', 'absender_email');
  }

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

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

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

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

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

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

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

    return $this->getAllRows();
  }

  
  public function findIdsByFilter(\Kufer\Web\Classes\Dbfilter\HLMandantenSQLFilter $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\HLMandantenSQLFilter $filter): int
  {
    $builder = $this->findByFilterQuery($filter)
      ->select('COUNT(*) AS anzahl');

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

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

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

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