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


class DBWorkZimmerBeleg extends DBWork
{
  public function __construct($pDBTyp = 1, $pNewInstance = false)
  {
    parent::__construct($pDBTyp, $pNewInstance);
    $this->select = 'id, ortnr, preis, status, knr, bettnr, faktor, preis*faktor as uepreis, preis_erm, preis_erm*faktor as uepreiserm';
    $this->orderby = ' preis ';
  }

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

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

  
  public function getCurrentRow($pRefreshCache = false)
  {
    $tmp = $this->getNewObject();
    $tmp->id = $this->getDatamodulValue('id');
    $tmp->ortnr = $this->getDatamodulValue('ortnr');
    $tmp->status = $this->getDatamodulValue('status');
    $tmp->bettnr = $this->getDatamodulValue('bettnr');
    $tmp->knr = $this->getDatamodulValue('knr');
    $tmp->faktor = $this->getDatamodulValue('faktor');
    $tmp->preis = $this->getDatamodulValue('preis');
    $tmp->uepreis = $this->getDatamodulValue('uepreis');
    $tmp->preis_erm = $this->getDatamodulValue('preis_erm');
    $tmp->uepreiserm = $this->getDatamodulValue('uepreiserm');

    return $tmp;
  }

  
  public function getCurrentRowSmall()
  {
    $tmp = $this->getNewObject();
    $tmp->bettnr = $this->getDatamodulValue('bettnr');
    $tmp->knr = $this->getDatamodulValue('knr');
    $tmp->preis = $this->getDatamodulValue('preis');
    $tmp->uepreis = $this->getDatamodulValue('uepreis');
    $tmp->preis_erm = $this->getDatamodulValue('preis_erm');
    $tmp->uepreiserm = $this->getDatamodulValue('uepreiserm');

    return $tmp;
  }

  
  public function getAllRowsSmall()
  {
    $ary = array(); 
    $rows = $this->dboperation->rowcount();
    if ($rows > 0) {
      for ($i = 0; $i < $rows; $i++) {
        $this->dboperation->selectrow($i);
        $tmp = $this->getCurrentRowSmall();
        if (isset($tmp)) {
          array_push($ary, $tmp);
        }
      }
    }

    return $ary;
  }

  
  
  

  
  public function getUebernachtungspreise($pKnr, $pFaktor)
  {
    $faktor = $this->dboperation->getInt4Sql($pFaktor);
    $knr = $this->dboperation->getString4Sql($pKnr);

    $sql = 'select bettnr, knr, preis, preis*' . $faktor . ' as uepreis, preis_erm, preis_erm*' . $faktor . ' as uepreiserm from zimmer_beleg '
      . " where (knr='" . $knr . "') and ((status='V') or (status='R'))"
      . " and (faktor>0) and typ<>'D' group by bettnr, preis, preis_erm, knr order by preis";

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

    return $this->getAllRowsSmall();
  }

  public function zimmerVerfuegbar($pKnr)
  {
    $knr = $this->dboperation->getString4Sql($pKnr);
    $sql = 'select count(*) as anz from zimmer_beleg '
        . " where (knr='" . $knr . "') and ((status='V') or (status='R'))"
        . " and (faktor>0) and typ<>'D'";
    $this->dboperation->clear();
    $this->dboperation->query($sql);
    $this->dboperation->selectrow(0);

    return $this->getDatamodulValue('anz') > 0;
  }
}