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


class DBWorkTNFach extends DBWork
{
  public function __construct($pDBTyp = 1, $pNewInstance = false)
  {
    parent::__construct($pDBTyp, $pNewInstance);
    $this->select = ' id as tnfid, beleg_id, fach_id, bemerkung, artwp, von, bis, bearbeiter, bearbdatum, erfasstvon, erfasstam, nichtbeurteilt ';
  }

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

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

  
  public function getCurrentRow($pRefreshCache = false)
  {
    $tmp = $this->getNewObject();
    $tmp->id = $this->getDatamodulValue('tnfid');
    $tmp->beleg_id = $this->getDatamodulValue('beleg_id');
    $tmp->fach_id = $this->getDatamodulValue('fach_id');
    $tmp->bemerkung = $this->getDatamodulValue('bemerkung');
    $tmp->artwp = $this->getDatamodulValue('artwp');
    $tmp->von = $this->getDatamodulValue('von');
    $tmp->bis = $this->getDatamodulValue('bis');
    $tmp->bearbeiter = $this->getDatamodulValue('bearbeiter');
    $tmp->bearbdatum = $this->getDatamodulValue('bearbdatum');
    $tmp->erfasstvon = $this->getDatamodulValue('erfasstvon');
    $tmp->erfasstam = $this->getDatamodulValue('erfasstam');
    $tmp->nichtbeurteilt = $this->getDatamodulValue('nichtbeurteilt');

    return $tmp;
  }

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

  
  public function getTNFachByFachID($pFachId)
  {
    $fachId = $this->dboperation->getInt4Sql($pFachId);
    $sql = 'SELECT ' . $this->select;
    $sql .= ' FROM tn_fach tnf';
    $sql .= ' WHERE tnf.fach_id = \'' . $fachId . '\'';
    $this->dboperation->clear();
    $this->dboperation->query($sql);

    return $this->getAllRows();
  }

  
  public function getTNFachByBelIDQuery($pBelegId): \SelectQueryBuilder
  {
    return $this->getAllQuery()
      ->and('beleg_id = :belegId:')
      ->paramInt('belegId', $pBelegId);
  }

  
  public function getTNFachByBelID($pBelegId)
  {
    $builder = $this->getTNFachByBelIDQuery($pBelegId);
    $this->dboperation->clear();
    $this->dboperation->query($builder->getSQL());

    return $this->getAllRows();
  }

  public function getTNFachIdsByBelID($pBelegId)
  {
    $builder = $this->getTNFachByBelIDQuery($pBelegId)
      ->select('tnf.fach_id');
    $this->dboperation->clear();
    $this->dboperation->query($builder->getSQL());

    return $this->getColumnFromRows('fach_id');
  }

  
  public function getTNFachByIDs($pBelegId, $pFachId)
  {
    $belegId = $this->dboperation->getInt4Sql($pBelegId);
    $fachId = $this->dboperation->getInt4Sql($pFachId);
    $sql = 'SELECT ' . $this->select;
    $sql .= ' FROM tn_fach tnf';
    $sql .= ' WHERE tnf.beleg_id = \'' . $belegId . '\'';
    $sql .= ' AND tnf.fach_id = \'' . $fachId . '\'';
    $this->dboperation->clear();
    $this->dboperation->query($sql);

    return $this->getFirstRow();
  }

  
  public function getTNFachByID($pid)
  {
    $id = $this->dboperation->getInt4Sql($pid);
    $sql = 'SELECT ' . $this->select;
    $sql .= ' FROM tn_fach tnf';
    $sql .= ' WHERE id = \'' . $id . '\'';
    $this->dboperation->clear();
    $this->dboperation->query($sql);

    return $this->getFirstRow();
  }

  
  public function getTNFachByBelIds($pBelegIds)
  {
    if (!is_array($pBelegIds) || count($pBelegIds) == 0) {
      return array();
    }

    $sql = 'SELECT ' . $this->select;
    $sql .= ' FROM tn_fach tnf';
    $sql .= ' WHERE tnf.beleg_id in (' . implode(', ', $pBelegIds) . ')';
    $this->dboperation->clear();
    $this->dboperation->query($sql);

    return $this->getAllRows();
  }

  
  public function canupdate($pVarname)
  {
    switch (StringOperations::strtolower($pVarname)) {
      case 'beleg_id':
      case 'fach_id':
      case 'bemerkung':
      case 'artwp':
      case 'von':
      case 'bis':
      case 'bearbeiter':
      case 'bearbdatum':
      case 'erfasstvon':
      case 'erfasstam':
      case 'nichtbeurteilt':
        return true;
      default: return false;
    }

    return false;
  }

  
  public function updateTNFach($fach, $pStatus5 = true)
  {
    $orgobj = $this->getTNFachByID($fach->id);
    
    
    $this->updateObject($fach, $orgobj, 'tn_fach', 'id', 'U', false, $pStatus5);
    $this->setActionIDs();
    $this->getTNFachByID($fach->id);
  }
}