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


class DBWorkErmaessigung extends DBWork
{
  
  public function getNewObject()
  {
    return new Ermaessigung();
  }

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

  
  public function getCurrentRow($pRefreshCache = false)
  {
    $tmp = $this->getNewObject();
    $tmp->nr = $this->getDatamodulValue('nr');
    $tmp->text = $this->getDatamodulValue('text');
    $tmp->prozente = $this->getDatamodulValue('prozente');
    $tmp->ungueltig = $this->getDatamodulValue('ungueltig');

    return $tmp;
  }

  
  public function getErmaessigungen($pOnlyValid = false, $pWB = false)
  {
    $sql = 'SELECT e.* FROM ermaess e';
    if ($pWB) {
      $pOnlyValid = true;
    }

    if ($pOnlyValid) {
      $sql .= ' WHERE ' . $this->dboperation->dbparser->isNull('e.ungueltig', '\'F\'') . '<> \'W\' '
      . 'and (e.gueltigbis > ' . $this->datetimeop->getFormateDate4ServerNow()
            . ' OR e.gueltigbis = ' . $this->datetimeop->getFormatDate4Server(DEFAULTDATE) . ' OR e.gueltigbis is null)';
    }

    if ($pWB) {
      $sql .= ' and ' . $this->dboperation->dbparser->isNull('e.sperreweb', '\'F\'') . '<> \'W\'';

      $zentralsettings = AllgSettingsAPI::getAllgSettings();
      $mandant = trim($zentralsettings->mandant) === '' ? 0 : $zentralsettings->mandant;
      if (\StringOperations::strpos($mandant, ',') !== false) {
        $mandanten = explode(',', $mandant);
        $ints = $this->dboperation->getInts4Sql($mandanten);
        $ints[] = 0;
        $sql .= ' AND e.mandant IN (' . \StringOperations::convertArray2InList($ints, true) . ')';
      } else {
        if ($mandant > 0) {
          $mandant = $this->dboperation->getInt4Sql($mandant);
          $sql .= ' and (' . $this->dboperation->dbparser->isNull('e.mandant', $mandant) . "=$mandant or e.mandant=0)";
        }
      }
    }
    $sql .= ' ORDER BY sort, nr, text';
    $this->dboperation->clear();
    $this->dboperation->query($sql);

    return $this->getAllRows();
  }

  
  public function isErmaessigungStillValid($pKnr, $pErmEnde, $pBeiKursbeginn = null)
  {
    if ($pBeiKursbeginn === null) {
      $dbwGrundsys = new DBWorkBASysSettings(1, true);
      $grundsys = $dbwGrundsys->getSettings();
      $beiKursbeginn = $grundsys->beruecksichtigeErmaessigungBeiKursbeginn;
    } else {
      $beiKursbeginn = $pBeiKursbeginn;
    }

    $dbwkurs = new DBWorkKurse($this->dbtyp, true);
    $kurs = $dbwkurs->getKurs($pKnr);
    
    
    if ($beiKursbeginn && DateTimeOperations::isValidDate($kurs->beginn_dat)) {
      $bezugsdatum = $this->datetimeop->getTimestampByDatestring(
        $kurs->beginn_dat
      );
    } else {
      
      $bezugsdatum = $this->datetimeop->getTimestampByDatestring(
        $this->datetimeop->getDateNow()
      );
    }

    return ($this->datetimeop->getTimestampByDatestring($pErmEnde ?? '') >= $bezugsdatum)
        || DateTimeOperations::isDateTimeEmptyOrDefault($pErmEnde);
  }

  
  public function getErmaessigungbyNr($pNr)
  {
    $nr = $this->dboperation->getInt4Sql($pNr);
    $this->dboperation->clear();
    $this->dboperation->query("SELECT e.* FROM ermaess e WHERE e.nr = '" . $nr . "'\n");

    return $this->getAllRows();
  }

  
  public function isKursForTnKarteAllowed($pKnr)
  {
    $knr = $this->dboperation->getString4Sql($pKnr);
    $sql = 'SELECT count(*) as anzahl FROM kurse_erm WHERE erlaubt = \'F\' AND knr=\'' . $knr . '\' AND art=\'P\'';
    $this->dboperation->clear();
    $this->dboperation->query($sql);
    $this->dboperation->selectrow(0);
    $count = $this->dboperation->fieldbyname('anzahl');

    return $count < 1;
  }
}