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


class DBWorkAnwesenheitExtra extends DBWork
{
  public function __construct($pDBTyp = 1, $pNewInstance = false)
  {
    parent::__construct($pDBTyp, $pNewInstance);
    $this->select = 'id, eanwid, estatus, ebemerkung, evon, ebis, eamscode';
  }

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

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

  
  public function getCurrentRow($pRefreshCache = false)
  {
    $tmp = $this->getNewObject();
    $tmp->id = $this->getDatamodulValue('id');
    $tmp->estatus = StringOperations::strtoupper($this->getDatamodulValue('estatus'));
    $tmp->ebemerkung = $this->getDatamodulValue('ebemerkung');
    $tmp->evon = $this->getDatamodulValue('evon');
    $tmp->ebis = $this->getDatamodulValue('ebis');
    $tmp->eanwid = $this->getDatamodulValue('eanwid');
    $tmp->eamscode = $this->getDatamodulValue('eamscode');

    return $tmp;
  }

  
  public function insertAnwesenheitExtra($pAnwesenheitExtra, $pStatus5 = false)
  {
    $pAnwesenheitExtra->id = $this->insertRecord($this->getTableName(), 'id', 'estatus', false);
    $this->saveChanging($this->getTableName(), '', '', $pAnwesenheitExtra->id, 'id', 'I', $pStatus5);
    $orgobj = $this->getAnwesenheitExtra($pAnwesenheitExtra->id);
    
    $this->updateObject($pAnwesenheitExtra, $orgobj, $this->getTableName(), 'id', 'A', false, $pStatus5);
    $this->setActionIDs();

    return $pAnwesenheitExtra->id;
  }

  
  public function updateAnwesenheitExtra(\AnwesenheitExtra $pAnwesenheitExtra, bool $dmzBetrieb = false)
  {
    $organwExtra = $this->getAnwesenheitExtra($pAnwesenheitExtra->id);
    $this->updateObject($pAnwesenheitExtra, $organwExtra, $this->getTableName(), 'id', 'U', false, $dmzBetrieb);
    $this->setActionIDs();
  }

  
  public function updateAnwesenheitExtraWithAnwesenheitId(\AnwesenheitExtra $pAnwesenheitExtra, int $anwId, bool $dmzBetrieb = false)
  {
    $organwExtra = $this->getAnwesenheitExtra($pAnwesenheitExtra->id);
    if ($organwExtra->eanwid != $anwId) {
      return false;
    }
    if ($pAnwesenheitExtra->eanwid != $anwId) {
      return false;
    }

    $this->updateObject($pAnwesenheitExtra, $organwExtra, $this->getTableName(), 'id', 'U', false, $dmzBetrieb);
    $this->setActionIDs();

    return true;
  }

  
  public function insertRecord($pTablename, $pIdField, $pDummyfield, $pGentableexists = false)
  {
    $this->dboperation->clear();
    $this->dboperation->execute('INSERT INTO ' . $this->getTableName() . " (estatus, eanwid) VALUES ('',0)");

    return $this->dboperation->getLastID($this->getTableName());
  }

  
  public function deleteAnwesenheitExtraForAnwesenheit($pAnwId, $pStatus5)
  {
    $anwId = $this->dboperation->getInt4Sql($pAnwId);
    
    $sql = 'SELECT id FROM ' . $this->getTableName() . ' WHERE eanwid = ' . $anwId;
    $this->dboperation->clear();
    $this->dboperation->query($sql);

    $ids = $this->getAllRowsSingleColumn('id');
    foreach ($ids as $id) {
      
      $this->deleteAnwesenheitExtra($id, $pStatus5);
    }
  }

  
  public function deleteAnwesenheitExtra($pId, $pStatus5)
  {
    $id = $this->dboperation->getInt4Sql($pId);
    if ($id !== '') {
      $this->dboperation->clear();
      $this->dboperation->execute('DELETE FROM ' . $this->getTableName() . ' WHERE id=' . $id);
      $this->saveChanging($this->getTableName(), '', '', "'$id'", '', 'D', $pStatus5);
      $this->setActionIDs();
    }
  }

  
  public function deleteAnwesenheitExtraWithAnwesenheitId($pId, $anwId, $pStatus5)
  {
    $origAnwExtra = $this->getAnwesenheitExtra($pId);
    if ($origAnwExtra->eanwid != $anwId) {
      return false;
    }

    $this->deleteAnwesenheitExtra($pId, $pStatus5);

    return true;
  }

  
  public function getAnwesenheitenExtraForAnwesenheit($pAnwId)
  {
    $anwId = $this->dboperation->getInt4Sql($pAnwId);
    $sql = 'select ' . $this->select . ' from ' . $this->getTableName() . ' where eanwid=' . $anwId;
    $this->dboperation->clear();
    $this->dboperation->query($sql);

    return $this->getAllRows();
  }

  
  public function getAnwesenheitenExtraForAnwesenheitIds($pAnwesenheitsIdAry)
  {
    if (!is_array($pAnwesenheitsIdAry) || empty($pAnwesenheitsIdAry)) {
      return array();
    }
    $sql = 'select ' . $this->select . ' from ' . $this->getTableName() . ' where eanwid in ('
            . StringOperations::convertArray2InList($pAnwesenheitsIdAry) . ') ';
    $this->dboperation->clear();
    $this->dboperation->query($sql);

    return $this->getAllRows();
  }

  
  public function getAnwesenheitExtra($pId)
  {
    $id = $this->dboperation->getInt4Sql($pId);
    $sql = 'select ' . $this->select . ' from ' . $this->getTableName() . ' where id=' . $id;
    $this->dboperation->clear();
    $this->dboperation->query($sql);

    return $this->getFirstRow();
  }
}


class DBWorkAnwesenheitExtra2 extends DBWorkAnwesenheitExtra
{
  public function getTableName(): string
  {
    if ($this->isZweiteTabelleVerfuegbar()) {
      return 'anw_extra2';
    }

    return 'anw_extra';
  }

  
  public function isZweiteTabelleVerfuegbar($pRefreshCache = false)
  {
    static $cache = null;
    if (!is_null($cache) && !$pRefreshCache) {
      return $cache;
    }

    if (\DBWorkBASysSettings::isIHK() || \DBWorkBASysSettings::isWienUni() || \DBWorkBASysSettings::isBozenAlphaBeta()) {
      $cache = false;

      return false;
    }

    if ($this->dboperation->getDBName() !== 'MYSQL') {
      $cache = false;

      return $cache;
    }
    $sql = "show tables like 'anw_extra2'";
    $this->dboperation->clear();
    $this->dboperation->query($sql);
    if ($this->dboperation->rowcount() != 1) {
      $cache = false;
    } else {
      $cache = true;
    }

    return $cache;
  }
}