<?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 $payonline;
  public $epay_tid;
  public $epay_tid_merchant;
  public $epay_type;
  public $epay_ordertime;
  public $epay_payerid;
  public $epay_institution;
  public $epay_amount;
  public $epay_bic;
  public $epay_iban;
  public $epay_kontoinhaber;
  public $epay_kontonr;
  public $epay_processing_mode;
  public $epay_authorizationid;
  public $epay_order_id;
  public $epay_capture_id;
  public $epay_state;
  public $epay_payment_date;

  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;
      case 'epay_amount':
        return \DatamodulVarType::TYPE_FLOAT;
      case 'payment_date':
        return \DatamodulVarType::TYPE_DATETIME;
    }
  }

  
  public function addEPayInfo(EPayInfo $pEPayInfo)
  {
    $this->epay_tid = $pEPayInfo->tid;
    $this->epay_tid_merchant = $pEPayInfo->tidMerchant;
    $this->epay_type = $pEPayInfo->type;
    $this->epay_ordertime = $pEPayInfo->ordertime;
    $this->epay_payerid = $pEPayInfo->payerid;
    $this->epay_institution = $pEPayInfo->institution;
    $this->epay_amount = $pEPayInfo->amount;
    $this->epay_state = $pEPayInfo->paymentState;
    $this->epay_payment_date = $pEPayInfo->paymentDate;
    $this->epay_order_id = $pEPayInfo->order_id;
    $this->epay_authorizationid = $pEPayInfo->authorizationid;
    $this->epay_capture_id = $pEPayInfo->capture_id;
    $this->epay_processing_mode = $pEPayInfo->processingMode;
  }

  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);
  }
}