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


class DBWorkAnwesenheitProtocol extends DBWorkGenerated
{
  public function __construct($pDBTyp = 1, $pNewInstance = false)
  {
    parent::__construct($pDBTyp, $pNewInstance);
    $this->select = 'id, abelegid, aktid, astatus, abemerkung, afehlminanfang, afehlminende, aprogresscheck, ahausaufgaben, bearbeiter, bearbdatum';
  }

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

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

  
  public function getCurrentRow($pRefreshCache = false)
  {
    $tmp = $this->getNewObject();

    $tmp->id = $this->getDatamodulValue('id');
    $tmp->astatus = $this->getDatamodulValue('astatus');
    $tmp->abemerkung = $this->getDatamodulValue('abemerkung');
    $tmp->aprogresscheck = $this->getDatamodulValue('aprogresscheck');
    $tmp->ahausaufgaben = $this->getDatamodulValue('ahausaufgaben');
    $tmp->bearbeiter = $this->getDatamodulValue('bearbeiter');
    $tmp->bearbdatum = $this->getDatamodulValue('bearbdatum');
    $tmp->abelegid = $this->getDatamodulValue('abelegid');
    $tmp->aktid = $this->getDatamodulValue('aktid');
    $tmp->afehlminanfang = $this->getDatamodulValue('afehlminanfang');
    $tmp->afehlminende = $this->getDatamodulValue('afehlminende');

    return $tmp;
  }

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

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

  
  public function protocolAnwesenheit($pAnwesenheit)
  {
    if (SessionOperations::isReplikat()) {
      throw new Exception('Diese Methode ist NICHT für den Replikatbetrieb vorgesehen');
    }
    $anwesenheitProtocol = $this->getNewObject();
    $anwesenheitProtocol = $anwesenheitProtocol->copyFrom($pAnwesenheit);
    $anwesenheitProtocol->id = $this->insertRecord($this->getTableName(), 'id', 'astatus', false);
    $this->saveChanging($this->getTableName(), '', '', $anwesenheitProtocol->id, 'id', 'I');
    $orgobj = $this->getAnwesenheitProtocol($anwesenheitProtocol->id);
    $this->updateObject($anwesenheitProtocol, $orgobj, $this->getTableName(), 'id', 'A', false);
    $this->setActionIDs();

    return $anwesenheitProtocol->id;
  }

  
  public function getAnwesenheitProtocol($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();
  }
}