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

namespace Kufer\Web\KuferTools\Teilnehmer\BayerischerApothekertagEhrengaeste\AnmeldeFormular;

class AnmeldeFormularUseCase extends \Kufer\Web\KuferTools\Basis\UseCase\ToolUseCase
{
  public function __construct($request)
  {
    parent::__construct($request, new AnmeldeFormularResponse());
  }

  public static function getToolId()
  {
    return 47;
  }

  public static function getToolSectionId()
  {
    return 559;
  }

  public function action()
  {
    $user = $this->getUser();


    return $this->getResponse($user);
  }

  protected function getResponse($user)
  {
    $this->handleResponseStrategies();
    if ($this->request->changedPerson) {
      $this->response->person = $this->request->changedPerson;
    } else {
      $personInSession = \SessionOperations::getVarValueWTIntern('bat_ehrengast', null);
      if (is_a($personInSession, 'Teilnehmer')) {
        $this->response->person = $personInSession;
      } else {
        $this->response->person = new \Teilnehmer();
      }
    }


    $this->response->statusCode = $this->request->statusCode;

    $this->response->wrongFields = $this->request->wrongFields;

    $this->response->bearbeitbar = true;

    $this->response->toolSettings = $this->toolSettings;
    $this->response->toolTnAnmeldSettings = \ToolSettingsAPI::getToolSettings(14);

    $this->response->kurse = $this->getKurse();
    $this->response->gebuehr = 0;
    $this->response->kursdata4Js = $this->getKursData4Js($this->response->gebuehr, $this->response->kurse, $user);

    return $this->response;
  }

  protected function getKursData4Js($pGebuehr, $kurse, $user)
  {
    $kennzAry = $this->getJsKennzData();
    $dbwteilnehmer = new \DBWorkTeilnehmer(1, true);
    $teilnehm = $dbwteilnehmer->getTeilnehm($user->tnr);
    $tnrKonto = $teilnehm->konto;
    $tnrIban = $teilnehm->iban;
    if (!\SessionOperations::isReplikat()) {
      $tnrKonto = \SecurityOperations::anonymizeString($tnrKonto);
      $tnrIban = \SecurityOperations::anonymizeString($tnrIban);
    }
    $guthaben = '';
    if (\DBWorkBASysSettings::isBlak() && $pGebuehr != \MathOperations::floatToString(0)
       && \MathOperations::stringToFloat($teilnehm->guthaben) > 0) {
      $guthaben = \StringOperations::htmlentities($teilnehm->guthaben . ' ' . CURRENCY, ENT_QUOTES);
    }

    $resultAry = array(
        'Kennz' => $kennzAry,
        'Geschlecht' => $this->getJsGeschlechtData(),
        'Currency' => \StringOperations::htmlentities(CURRENCY, ENT_QUOTES),
        'Guthaben' => $guthaben,
        'Gebuehr' => \StringOperations::htmlentities($pGebuehr . ' ' . CURRENCY, ENT_QUOTES)
      );

    $tnToolAnmeldSettings = \ToolSettingsAPI::getToolSettings(14);
    $anmZf = $tnToolAnmeldSettings->anm_zahlverfahren;
    if ($anmZf == ANM_ZAHLVERFAHREN_SEPA_KTO || $anmZf == ANM_ZAHLVERFAHREN_NUR_KTO) {
      $resultAry['Konto'] = \StringOperations::htmlentities($tnrKonto, ENT_QUOTES);
    }
    if ($anmZf == ANM_ZAHLVERFAHREN_SEPA_KTO || $anmZf == ANM_ZAHLVERFAHREN_NUR_SEPA) {
      $resultAry['Iban'] = \StringOperations::htmlentities($tnrIban, ENT_QUOTES);
    }

    $resultAry['Kurse'] = array();

    foreach ($kurse as $kurs) {
      
      
      
      $toBeReplaced = array("\r\n", "\n\r", "\r", "\n");
      $gebuehrErgaenzText = \StringOperations::htmlentities($kurs->gebuehrergaenztext, ENT_QUOTES);
      $gebuehrErgaenzText = trim(str_replace($toBeReplaced, ' ', $gebuehrErgaenzText));

      $kursAry = array(
          'Knr' => \StringOperations::htmlentities($kurs->id, ENT_QUOTES),
          'Titel' => \StringOperations::htmlentities($kurs->getVarValue('titel_zeile1'), ENT_QUOTES),
          'Zeitraum' => \StringOperations::htmlentities($kurs->getVarValue('beginn_ende_komplett'), ENT_QUOTES),
          'Ort' => \StringOperations::htmlentities($kurs->ort, ENT_QUOTES),
          'Gebuehr' => \StringOperations::htmlentities($pGebuehr . ' ' . CURRENCY, ENT_QUOTES),
          'GebuehrenErgaenzText' => $gebuehrErgaenzText,
          'Aussenstelle' => \StringOperations::htmlentities($kurs->aussenst, ENT_QUOTES)
      );
      $resultAry['Kurse'][] = $kursAry;
    }

    return $resultAry;
  }

  
  protected function getJsGeschlechtData()
  {
    return '';
  }

  protected function getJsKennzData()
  {
    return array();
  }

  private function getKurse()
  {
    $dbwKurse = new \DBWorkKurseWT(1, true);

    return $dbwKurse->getKurseByKursnummern($this->request->knrAry);
  }

  protected function getResponseStrategies()
  {
    return array(
      new \Kufer\Web\KuferTools\Utils\ResponseStrategies\ResponseMitRahmen($this),
      new \Kufer\Web\KuferTools\Utils\ResponseStrategies\ResponseWss($this),
      new \Kufer\Web\KuferTools\Utils\ResponseStrategies\ResponseWithToolSettings($this)
    );
  }
}

class AnmeldeFormularRequest
{
  public $statusCode;
  public $changedPerson;
  public $wrongFields;
  public $knrAry = array();
}

class AnmeldeFormularResponse extends \Kufer\Web\KuferTools\Basis\UseCase\ToolResponse
{
  public $user;
  public $menuPunkte = array();
  public $person;
  public $statusCode;
  public $freeField = '';
  public $bearbeitbar = false;
  public $wrongFields;
}