<?php

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


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

    
    
    $this->select = 'id, nr, typ, knr, bogenid, druckdatum, bearbeiter, bearbdatum, erfasstvon, erfasstam, dnr';
    $this->_dataModulFields = array('id', 'nr', 'typ', 'knr', 'bogenid', 'druckdatum', 'bearbeiter', 'bearbdatum', 'erfasstvon', 'erfasstam', 'dnr');
  }

  
  public function getNewObject()
  {
    return new \FeedBackGedruckt();
  }

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

  
  public function insertFeedBackGedruckt($pFeedBackGedruckt)
  {
    $pFeedBackGedruckt->id = $this->insertRecord($this->getTableName(), 'id', 'nr', true);

    $originalEintrag = new FeedBackGedruckt();
    $originalEintrag->id = $pFeedBackGedruckt->id;

    $this->updateObject($pFeedBackGedruckt, $originalEintrag, $this->getTableName(), 'id', 'U', true);

    return $pFeedBackGedruckt;
  }

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

  
  public function getAlldataByKnrAndTypAndTnrQuery($pKnrs, $pTyp, $pTnrs)
  {
    $builder = $this->getAlldataByKnrAndTypQuery($pKnrs, $pTyp);
    if (\DBWorkBASysSettings::isInlinguaMuenchen()) {
      $builder->and('(nr IN (:tnrs:) OR nr = -1)');
    } else {
      $builder->and('nr IN (:tnrs:)');
    }

    if ($pTnrs instanceof \SelectQueryBuilder) {
      $builder = $builder->paramSelectQuery('tnrs', $pTnrs);
    } else {
      $builder = $builder->paramIntArray('tnrs', $pTnrs);
    }

    return $builder;
  }

  
  public function getAlldataByKnrAndTypAndTnr($pKnrs, $pTyp, $pTnrs)
  {
    $builder = $this->getAlldataByKnrAndTypAndTnrQuery($pKnrs, $pTyp, $pTnrs);
    $this->dboperation->clear();
    $this->dboperation->query($builder->getSQL());

    return $this->getAllRows();
  }

  
  public function getAlldataByKnrAndTypQuery($pKnrs, $pTyp)
  {
    return $this->getAllQuery()
        ->and('knr IN (:knrs:)')
        ->and('typ = :typ:')
        ->paramStringArray('knrs', $pKnrs)
        ->paramString('typ', $pTyp);
  }

  
  public function getAlldataByKnrAndTyp($pKnrs, $pTyp)
  {
    $sqlstr = $this->getAlldataByKnrAndTypQuery($pKnrs, $pTyp)->getSQL();

    
    $this->dboperation->clear();

    
    $this->dboperation->query($sqlstr);

    return $this->getAllRows();
  }

  
  public function getCountFeedBackGedruckt($pKnr, $pTyp, $pUserNr)
  {
    $builder = $this->getAlldataByKnrAndTypQuery(array($pKnr), $pTyp)
      ->select('COUNT(*) AS anzahl')
      ->and('nr = :userId:')
      ->paramInt('userId', $pUserNr);

    $this->dboperation->clear();
    $this->dboperation->query($builder->getSQLForInnerQuery());
    $this->dboperation->selectrow(0);

    return $this->getValue('anzahl', \DatamodulVarType::TYPE_INT);
  }
}