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

class DBWorkBranchen extends DBWorkGenerated
{
  public function __construct($pDBTyp = 1, $pNewInstance = false)
  {
    parent::__construct($pDBTyp, $pNewInstance);
    $this->select = 'id, kennz, text';
    $this->_dataModulFields = array('id', 'kennz', 'text');
  }

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

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

  
  public function getBranchen4Dozent($dnr)
  {
    $subselect = $this->getSelectQueryBuilder();
    $subselect->select('bra_nr')
              ->from('bran_adr')
              ->where("typ='D' AND adr_nr=:dnr:")
              ->paramInt('dnr', $dnr);

    $query = $this->getSelectQueryBuilder();
    $query->select($this->select)
          ->from($this->getTableName())
          ->where('kennz IN (:subselect:)')
          ->paramRaw('subselect', $subselect->getSQLForInnerQuery());

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

    return $this->getAllRows();
  }

  public function getDnrsByBranchenNameQuery($branchenName): \SelectQueryBuilder
  {
    $query = $this->getSelectQueryBuilder();
    $query->select('adr_nr')
          ->from('bran_adr', 'ba')
          ->innerJoin('branchen b', 'b.kennz=ba.bra_nr')
          ->where("ba.typ='D' AND b.text like :branchenName:")
          ->paramString('branchenName', $branchenName . '%');

    return $query;
  }

  public function getDnrsByBranchenName($branchenName)
  {
    $query = $this->getDnrsByBranchenNameQuery($branchenName);

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

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