<?php

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

class WBAnmeldungHotel extends WBAnmeldungWeiterePersonen
{
  public function __construct($urlparameter)
  {
    parent::__construct($urlparameter);
    $this->anmeldeart = 6;
  }

  
  public function _checkStep($ziel, $geprüft = 0)
  {
    if (!is_numeric($ziel) || $ziel <= 0) {
      $ziel = 1;
    }

    if ($ziel >= 1 && $geprüft < 1) {
      $this->step = 1;
      SessionOperations::setVarValueWk('last_validated_step', 1);
    }

    if ($ziel >= 2 && $geprüft < 2) {
      $this->step = 2;
      SessionOperations::setVarValueWk('last_validated_step', 2);
    }

    if ($ziel >= 3 && $geprüft < 3) {
      $tmpurlparam = new URLParameterAnmeldungZusatzPerson();
      
      if ($ziel == 3 && $_SERVER['REQUEST_METHOD'] == 'POST' && $tmpurlparam->addhauptperson == 1) {
        $this->getTeilnehmerFromRequest2Session();
        $this->getBelegungFromRequest2Session();
      }
      $this->getSight(62);
      if (!$this->checkEingaben()) {
        $this->step = 2;
        $this->setBackToStep(2);

        return;
      }
      $this->step = 3;
      SessionOperations::setVarValueWk('last_validated_step', 3);
    }
    
    if ($ziel >= 4 && $geprüft < 4) {
      $tmpurlparam = new URLParameterAnmeldungZusatzPerson();
      
      if ($ziel == 4 && $_SERVER['REQUEST_METHOD'] == 'POST' && $tmpurlparam->schrittback == '') {
        $this->getHoteldatenFromRequest2Session();
      }
      $this->getSight(133);
      $check = $this->_getWbAnmeldungCheck();
      $check->vorlage = $this->sight->body;
      
      $tmp = SessionOperations::getHoteldatenFromSession(true);
      $check->datamodul = $tmp[0];

      if (!$check->check()) {
        $this->pflichtfeldFehlerArray = $check->pflichtfeldFehlerArray;
        $this->formatFehlerArray = $check->formatFehlerArray;
        $this->step = 3;
        $this->setBackToStep(3);

        return;
      }
      $this->step = 4;
      SessionOperations::setVarValueWk('last_validated_step', 4);
    }
    if ($ziel >= 5 && $geprüft <= 5) {
      $this->step = 5;
      SessionOperations::setVarValueWk('last_validated_step', 5);
    }

    
    if ($ziel >= 6 && $geprüft <= 6) {
      $this->step = 6;
      SessionOperations::setVarValueWk('last_validated_step', 6);
    }
    if ($ziel >= 99 && $geprüft <= 99) {
      $this->step = 99;
      SessionOperations::setVarValueWk('last_validated_step', 99);
    }
  }

  
  public function content()
  {
    SessionOperations::setVarValueAnm(ANMELDEART, ANMELDEART_INTERN_HOTEL);
    $isFehlerAufgetreten = (count($this->pflichtfeldFehlerArray) > 0 || count($this->formatFehlerArray) > 0);
    switch ($this->step) {
      default:
      case 1:
        SessionOperations::setVarValueAnm(ANMELDEART, null);
        $this->showsightcomplete(60, new ExpTeilnehmer());
        break;
      case 2: 
        $this->dataarraybody = SessionOperations::getTeilnehmerFromSession();
        if ($isFehlerAufgetreten) {
          $this->warnungpflichtfelder(62, new ExpTeilnehmer());
        } else {
          $this->showsightcomplete(62, new ExpTeilnehmer());
        }
        break;
      case 3:
        
        
        
        $this->dataarraybody = SessionOperations::getHoteldatenFromSession(true);

        if ($isFehlerAufgetreten) {
          $this->warnungpflichtfelder(133, new ExpHotel());
          break;
        } else {
          $this->showsightcomplete(133, new ExpHotel()); 
        }
        break;
      case 4: 
        if (isset($this->urlparameter->payOnline) && $this->urlparameter->payOnline) {
          $this->ePaymentStart($this->urlparameter);
        } else {
          $this->anmeldungbestaetigen();
        }
        break;
      case 5: 
        if (ePaymentApi::isAnyPayonlineInSession() || (isset($this->urlparameter->payOnline) && $this->urlparameter->payOnline)) {
          $this->ePaymentCommit($this->urlparameter);
        } else {
          $this->anmeldungabschliessen();
        }
        SessionOperations::setVarValueAnm(ANMELDEART, null);
        break;
      case 6: 
        if (isset($this->urlparameter->payOnline) && $this->urlparameter->payOnline) {
          $this->ePaymentExecute($this->urlparameter);
        }
        break;
      case 99:
        if (isset($this->urlparameter->payOnline) && $this->urlparameter->payOnline) {
          $this->doSchritt99($this->urlparameter);
        }
        break;
    }
    SessionOperations::setVarValueAnm(ANMELDEART, ANMELDEART_INTERN_HOTEL);
  }

  public function getHoteldatenFromRequest2Session()
  {
    $this->pflichtfeldfehlerarray = array();
    $this->formatFehlerArray = array();
    $tmphotel = new AnmeldedatenHotel();
    
    for ($i = 0; $i < $tmphotel->getVarCount(); $i++) {
      if (isset($_REQUEST['hedit_' . $tmphotel->getVarName($i)])) {
        $value = trim($_REQUEST['hedit_' . $tmphotel->getVarName($i)]);
      } else {
        $value = '';
      } 

      
      SessionOperations::setVarValueWk('wh_' . $tmphotel->getVarName($i), $value);
    }
    
    SessionOperations::setVarValueWk('wh_knr', $this->warenkorb[0]->kurs->id);

    
    if (PostOperations::getVarValue('hedit_uebernachtung_jn', '--') == '1') { 
      switch (PostOperations::getVarValue('hedit_zimmerart')) {
        case 'EZ': SessionOperations::setVarValueWk(
          'wh_zimmergebuehr',
          $this->_getVarValue4HotelFromWarenkorb('{%kh_ezpreis%}')
        );
          break;
        case 'DZ': SessionOperations::setVarValueWk(
          'wh_zimmergebuehr',
          $this->_getVarValue4HotelFromWarenkorb('{%kh_dzpreis%}')
        );
          break;
        case '3Z': SessionOperations::setVarValueWk(
          'wh_zimmergebuehr',
          $this->_getVarValue4HotelFromWarenkorb('{%kh_3zpreis%}')
        );
          break;
        case '4Z': SessionOperations::setVarValueWk(
          'wh_zimmergebuehr',
          $this->_getVarValue4HotelFromWarenkorb('{%kh_4zpreis%}')
        );
          break;
      }
    } else {
      SessionOperations::setVarValueWk('wh_kein_fruehstueck', '1');
      SessionOperations::setVarValueWk('wh_kein_abendessen', '1');
    }
    
    $dbwke = new DBWorkKursEssen();
    $knr = $this->_getOnlyKnrInWarenkorb();
    $essgeb = $dbwke->getPrice4AllMealsForKurs($knr);
    if (PostOperations::getVarValue('hedit_essen_jn')) {
      $sett = ToolSettingsAPI::getToolSettings(4);
      if (PostOperations::getVarValue('hedit_kein_fruehstueck')) { 
        $essgeb -= $dbwke->getPrice4OptionalMealsForKursByType($knr, $sett->typeFS);
      } 
      if (PostOperations::getVarValue('hedit_kein_abendessen')) { 
        $essgeb -= $dbwke->getPrice4OptionalMealsForKursByType($knr, $sett->typeAE);
      } 
    } else {
      $essgeb = 0;
    }
    SessionOperations::setVarValueWk('wh_essengebuehr', MathOperations::floatToString($essgeb));
  }

  public function _anmeldungbestaetigenPersonenPart()
  {
    $retour = '';

    
    $this->dataarrayheader = SessionOperations::getTeilnehmerFromSession();
    $retour .= $this->getsightcomplete(61, new ExpTeilnehmer());

    
    $this->dataarraybody = SessionOperations::getHoteldatenFromSession(true);
    $kurs = array();
    for ($i = 0; $i < count($this->warenkorb); $i++) {
      $kurs[$i] = $this->warenkorb[$i]->kurs;
    }
    if (count($kurs) === 0) {
      $kurs[0] = new Kurs();
    }
    $this->dataarrayfooter = $this->dataarraybody;
    $retour .= $this->getsightcomplete(205, new ExpHotel());

    return $retour;
  }

  
  public function _getOnlyKnrInWarenkorb()
  {
    $this->dbwWk->explodeEntries();

    return StringOperations::substr($this->dbwWk->ary[0], 0, StringOperations::strpos($this->dbwWk->ary[0], ':'));
  }

  public function _getVarValue4HotelFromWarenkorb($pVarname)
  {
    return HotelOperations::getVarValue4HotelFromWarenkorb($pVarname);
  }

  
  protected function _getWbAnmeldungCheck()
  {
    return new WBAnmeldungCheckHoteldaten();
  }
}