<?php

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


class BestellungXml extends Datamodul
{
  public $name;
  public $vorname;
  public $name_titel;
  public $zusatz;
  public $strasse;
  public $ort;
  public $email;
  public $telefon;
  public $mobil;
  public $geschlecht;
  public $gebdat;
  public $iban;
  public $bankbez;
  public $abw_name;
  public $abw_vorname;
  public $abw_zusatz;
  public $abw_zusatz2;
  public $abw_geschlecht;
  public $abw_strasse;
  public $abw_ort;
  public $abw_nation;
  public $tnr;
  public $nation;
  public $telefon2;
  public $fax;
  public $bearbdatum;
  public $bearbeiter;
  public $eingelesen;
  public $sessionid;
  public $sessiontime;
  public $abw_tnr;
  public $nowerbung;

  public function getTyp($pFieldName)
  {
    switch ($pFieldName) {
      default:
        return parent::getTyp($pFieldName);
      case 'gebdat':
      case 'bearbdatum':
      case 'eingelesen':
        return \DatamodulVarType::TYPE_DATE;
      case 'tnr':
      case 'sessiontime':
      case 'abw_tnr':
        return \DatamodulVarType::TYPE_INT;
    }
  }

  public function fromTeilnehmerPflicht($pTnPflicht)
  {
    $varCount = $this->getVarCount();

    $varNameMap = array('telefon' => 'tel', 'telefon2' => 'tel2');

    for ($c = 0;$c < $varCount;$c++) {
      $varname = $this->getVarName($c);
      $varnameInTnPflicht = (isset($varNameMap[$varname]) ? $varNameMap[$varname] : $varname);
      $value = $pTnPflicht->getVarValue($varnameInTnPflicht);

      if ($value instanceof Kommunikation) {
        $this->setValue2Var($varname, $value->getVarValue('wert'));
      } else {
        $this->setValue2Var($varname, $value);
      }
    }

    return $this;
  }

  public function setAbwData(AbweichendeLieferadresse $pAbwLieferadresse)
  {
    if ($pAbwLieferadresse->verwenden) {
      $varCount = $pAbwLieferadresse->getVarCount();

      $varNameMap = array('zusatz1' => 'zusatz');

      for ($c = 0;$c < $varCount;$c++) {
        $varname = $pAbwLieferadresse->getVarName($c);
        $varnameInBestellungXml = 'abw_' . (isset($varNameMap[$varname]) ? $varNameMap[$varname] : $varname);
        $value = $pAbwLieferadresse->getVarValue($varname);

        $this->setValue2Var($varnameInBestellungXml, $value);
      }

      if ($pAbwLieferadresse->getVarValue('ort') == '' && ($pAbwLieferadresse->getVarValue('nurort') !== '' || $pAbwLieferadresse->getVarValue('nurplz') !== '')) {
        $this->setValue2Var('abw_ort', trim($pAbwLieferadresse->getVarValue('nurplz') . ' ' . $pAbwLieferadresse->getVarValue('nurort')));
      }

      if ($pAbwLieferadresse->getVarValue('strasse') == '' && ($pAbwLieferadresse->getVarValue('nurstrasse') !== '' || $pAbwLieferadresse->getVarValue('nurhausnr') !== '')) {
        $this->setValue2Var('abw_ort', trim($pAbwLieferadresse->getVarValue('nurstrasse') . ' ' . $pAbwLieferadresse->getVarValue('nurhausnr')));
      }
    }

    return $this;
  }

  
  public function ToXml($pExceptions = array(), $pForDMZ = false, $pForceVars = array(), $pNodeTag = false)
  {
    if ($this->tnr <= 0) {
      $pExceptions[] = 'tnr';
    }

    return parent::ToXml($pExceptions, $pForDMZ, $pForceVars, $pNodeTag);
  }

  public function getXMLNode($pNodeTag = 'stammdaten', $pForDmz = false)
  {
    return XMLNode::getXMLNode($this, $pNodeTag, array(), $pForDmz);
  }
}