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

namespace Kufer\Web\Classes\Db;

use Kufer\Web\Classes\Datamodul\RaumreservierungTermine;

class DBWorkRaumReservierungTermine extends \DBWorkGenerated
{

  public function __construct($pDBnr = 1, $pNewInstance = false)
  {
    parent::__construct($pDBnr, $pNewInstance);

    $this->_dataModulFields = array('id', 'antrag_id', 'raum', 'datum', 'uhrVon', 'uhrBis', 'bearbeiter', 'bearbDatum', 'erfasstVon', 'erfasstAm', 'minuten', 'pause');
    $this->select = implode(', ', $this->_dataModulFields);
  }

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

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

  public function insertOne($pDatamodul, $pDummyField, string $pIdFieldName = 'id', bool $pDontProtocol = false, bool $pDmzBetrieb = false): int
  {
    $pDatamodul->bearbDatum = \DateTimeOperations::getDateTimeNow();
    $pDatamodul->erfasstAm = \DateTimeOperations::getDateTimeNow();
    $tool0settings = \ToolSettingsAPI::getToolSettings(0);
    $pDatamodul->bearbeiter = $tool0settings->direktMaKuerzel;
    $pDatamodul->erfasstVon = $tool0settings->direktMaKuerzel;

    $inserter = new \DBInserterMitGenTable($this, $this->getTableName(), $this->_dataModulFields);
    $pDatamodul->id = $inserter->insert($pDatamodul, $pIdFieldName, $pDontProtocol, $pDmzBetrieb);

    return $pDatamodul->id;
  }

  public function deleteByAntragId($pAntragId, $pDontProtocol = false, $pDmzBetrieb = false)
  {
    $cleanAntragId = $this->dboperation->getInt4Sql($pAntragId);
    $this->dboperation->clear();
    $this->dboperation->execute('DELETE FROM rrantragtermine WHERE antrag_id=' . $cleanAntragId);
    
    
    
    
    $this->setActionIDs();
  }

  public function findByFilterQuery(\Kufer\Web\Classes\Dbfilter\RaumreservierungenSQLFilter $filter): \SelectQueryBuilder
  {
    $condition = $filter->createSql($this->dboperation);

    return $this->getAllQuery()
      ->and($condition);
  }

  
  public function findByFilterNurOffeneReservierungen(\Kufer\Web\Classes\Dbfilter\RaumreservierungenSQLFilter $filter): array
  {
    $builder = $this->findByFilterQuery($filter);

    $builder->and("antrag_id IN(SELECT id FROM rrantrag WHERE (typ is null OR typ IN ('', ' ', 'A')))");

    $this->dboperation->clear();
    $this->dboperation->query($builder->getSQL());

    return $this->getAllRows();
  }

  
  public function findByFilter(\Kufer\Web\Classes\Dbfilter\RaumreservierungenSQLFilter $filter): array
  {
    $builder = $this->findByFilterQuery($filter);

    $this->dboperation->clear();
    $this->dboperation->query($builder->getSQL());

    return $this->getAllRows();
  }

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