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


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

  protected function postGetAllRowsHook($pDatamodules, $pRefreshCache = false)
  {
    $datamodules = parent::postGetAllRowsHook($pDatamodules, $pRefreshCache);

    return $this->fuegeFremdobjektHinzu($datamodules, 'tnr', 'teilnehmer', new DBWorkTeilnehmer(1, true), 'getTeilnehmByIdArray', 'id');
  }

  
  public function getNewObject()
  {
    $obj = new ZsaDs();

    $dbwTeilnehmer = new DBWorkTeilnehmer(1, true);
    $obj->teilnehmer = $dbwTeilnehmer->getNewObject();

    return $obj;
  }

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

  
  public function getZsaDsByZgIds($pTnr, $pZgIds)
  {
    $keys = $this->dboperation->getInts4Sql($pZgIds);
    $tnr = $this->dboperation->getInt4Sql($pTnr);
    $select = StringOperations::applyPrefix($this->select, 'zds');
    $sql = 'select ' . $select . ' from zsa_ds zds join zsa_bkbvtn zbkvtn on zbkvtn.ds = zds.id where zbkvtn.tnr = ' . $tnr
           . ' and zbkvtn.aktiv=1 and zds.aktiv=1 and zbkvtn.status = \'A\' and zbkvtn.id in (select tnzb from zsa_tnzb_zg where zg in (' . StringOperations::convertArray2InList($keys) . '))';
    $this->dboperation->clear();
    $this->dboperation->query($sql);

    return $this->getAllRows();
  }
}