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


class DBWorkFeedBackCode extends DBWork
{
  public function __construct($pDBTyp = 1, $pNewInstance = false)
  {
    parent::__construct($pDBTyp, $pNewInstance);

    
    
    $this->select = 'id, code, bogenid, knr, tnr, belegid, gueltigbis, eingeloestam, erfasstam, erfasstvon, dnr, dozkursid, is_kurscode, fuer_dozent';
  }

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

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

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

    $tmp->id = $this->getDatamodulValue('id');
    $tmp->code = $this->getDatamodulValue('code');
    $tmp->bogenid = $this->getDatamodulValue('bogenid');
    $tmp->knr = $this->getDatamodulValue('knr');
    $tmp->tnr = $this->getDatamodulValue('tnr');
    $tmp->belegid = $this->getDatamodulValue('belegid');
    $tmp->gueltigbis = $this->getDatamodulValue('gueltigbis');
    $tmp->eingeloestam = $this->getDatamodulValue('eingeloestam');
    $tmp->erfasstam = $this->getDatamodulValue('erfasstam');
    $tmp->erfasstvon = $this->getDatamodulValue('erfasstvon');
    $tmp->dnr = $this->getDatamodulValue('dnr');
    $tmp->dozkursid = $this->getDatamodulValue('dozkursid');
    $tmp->is_kurscode = $this->getDatamodulValue('is_kurscode');
    $tmp->fuer_dozent = $this->getDatamodulValue('fuer_dozent');

    return $tmp;
  }

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

  
  public function getByCode($pCode)
  {
    $code = $this->dboperation->getString4Sql($pCode);

    $sql = 'SELECT ' . $this->select . ' FROM ' . $this->getTableName() . ' WHERE code = \'' . $code . '\'';

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

    return $this->getFirstRow();
  }

  
  public function getByBelegIdAndBogenId($pBelegId, $pBogenId)
  {
    $belegId = $this->dboperation->getInt4Sql($pBelegId);
    $bogenId = $this->dboperation->getInt4Sql($pBogenId);

    $sql = 'SELECT ' . $this->select . ' FROM ' . $this->getTableName() . ' WHERE belegid = ' . $belegId . ' AND bogenid = ' . $bogenId;

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

    return $this->getFirstRow();
  }

  
  public function insertFeedBackCode($pFeedBackCode)
  {
    $pFeedBackCode->id = $this->insertRecord($this->getTableName(), 'id', 'knr');
    $this->saveChanging($this->getTableName(), '', '', $pFeedBackCode->id, 'id', 'I', SessionOperations::isReplikat());
    $origFeedBackCode = $this->getById($pFeedBackCode->id);
    $this->updateObject($pFeedBackCode, $origFeedBackCode, $this->getTableName(), 'id', 'A');
    $this->setActionIDs();

    return $pFeedBackCode->id;
  }

  
  public function updateFeedBackCode($pFeedBackCode)
  {
    $origCode = $this->getByCode($pFeedBackCode->code);
    $this->updateObject($pFeedBackCode, $origCode, $this->getTableName(), 'id');
    $this->setActionIDs();
  }
}