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

namespace Kufer\Web\KuferTools\Dozent\KursUebersicht\Detailsreiter\Honorar\GlindeVhs\HonorarPdf;

include_once_ex('extern/composer/vendor/autoload.php');
use Spipu\Html2Pdf\Html2Pdf;

class HonorarAbrechnung
{
  public const UE_45_MIN_BETRAG_PRO_STD = 21;
  public const UE_60_MIN_BETRAG_PRO_STD = 28;
  public const MWST_FAKTOR = 1.19;

  private $request;
  private $kurs;
  private $dozent;

  
  public function __construct($pRequest, $pKurs, $pDozent)
  {
    $this->request = $pRequest;
    $this->kurs = $pKurs;
    $this->dozent = $pDozent;
  }

  public function output()
  {
    $html2pdf = $this->preparePdf();

    return $html2pdf->Output('honorarabrechnung_' . $this->kurs->id . '.pdf');
  }

  private function preparePdf()
  {
    static $html2pdf = null;
    if (is_null($html2pdf)) {
      $html2pdf = new Html2Pdf('P', 'A4', 'de');
      $html2pdf->setDefaultFont('Arial');
      $html2pdf->writeHTML(\StringOperations::utf8_encode($this->getHtmlTemplate()));
    }

    return $html2pdf;
  }

  private function getHtmlTemplate()
  {
    $html = file_get_contents(\FSOperations::path(dirname(__FILE__), 'HonorarAbrechnung.html'));

    return $this->replaceVars($html);
  }

  private function replaceVars($html)
  {
    $kursObj = $this->kurs;
    $dozentObj = $this->dozent;
    $ueMin45 = \MathOperations::floatToStringTruncateNull(\MathOperations::stringToFloat($this->request->ueMin45));
    $ueMin60 = \MathOperations::floatToStringTruncateNull(\MathOperations::stringToFloat($this->request->ueMin60));
    $auslagenBetrag = \MathOperations::floatToString(\MathOperations::stringToFloat($this->request->auslagenBetrag));
    $abschlagBetrag = \MathOperations::floatToString(\MathOperations::stringToFloat($this->request->abschlagBetrag));

    $faktor = 1;
    if ($this->request->mitMwst) {
      $faktor = self::MWST_FAKTOR;
    }

    
    $ue45MinBetragMitFaktorFloat = self::UE_45_MIN_BETRAG_PRO_STD * $faktor;
    $ue60MinBetragMitFaktorFloat = self::UE_60_MIN_BETRAG_PRO_STD * $faktor;

    $gesamtBetragUeMin45Float = \MathOperations::stringToFloat($ueMin45) * $ue45MinBetragMitFaktorFloat;
    $gesamtBetragUeMin60Float = \MathOperations::stringToFloat($ueMin60) * $ue60MinBetragMitFaktorFloat;

    $gesamtBetragUe45Min = \MathOperations::floatToString($gesamtBetragUeMin45Float);
    $gesamtBetragUe60Min = \MathOperations::floatToString($gesamtBetragUeMin60Float);
    $gesamtBetrag = \MathOperations::floatToString($gesamtBetragUeMin45Float + $gesamtBetragUeMin60Float + \MathOperations::stringToFloat($auslagenBetrag) + \MathOperations::stringToFloat($abschlagBetrag));

    $allgsettings = \AllgSettingsAPI::getAllgSettings();
    $adminSettings = \ToolSettingsAPI::getToolSettings(0);
    $map = array(
      '{%d_name%}' => $dozentObj->name,
      '{%d_vorname%}' => $dozentObj->vorname,
      '{%d_titel%}' => $dozentObj->name_titel,
      '{%d_strasse_und_nr%}' => $dozentObj->strasse,
      '{%d_plz_ort%}' => $dozentObj->ort,
      '{%d_bank%}' => $dozentObj->bankbez,
      '{%d_bic%}' => $dozentObj->bic,
      '{%d_iban%}' => $dozentObj->iban,

      '{%k_nr%}' => $kursObj->nr_anzeige,
      '{%k_beginn_dat%}' => $kursObj->beginn_dat,
      '{%k_ende_dat%}' => $kursObj->ende_dat,
      '{%k_titel%}' => $kursObj->getVarValue('kurzbez_titel'),
      '{%k_beginn_uhrzeit%}' => $kursObj->beginn,
      '{%k_ende_uhrzeit%}' => $kursObj->ende,

      '{%r_ue_min_45%}' => $ueMin45,
      '{%r_ue_min_60%}' => $ueMin60,
      '{%r_auslagen_betrag%}' => $auslagenBetrag,
      '{%r_abschlag_betrag%}' => $abschlagBetrag,

      '{%c_ue_min_45_betrag_pro_std%}' => \MathOperations::floatToString($ue45MinBetragMitFaktorFloat),
      '{%c_ue_min_60_betrag_pro_std%}' => \MathOperations::floatToString($ue60MinBetragMitFaktorFloat),

      '{%ue_min_45_gesamt_betrag%}' => $gesamtBetragUe45Min,
      '{%ue_min_60_gesamt_betrag%}' => $gesamtBetragUe60Min,
      '{%gesamt_betrag%}' => $gesamtBetrag,

      '{%admin_vhsname%}' => \StringOperations::html_entity_decode($allgsettings->vhsname),
      '{%admin_vhsname_novhs%}' => \StringOperations::html_entity_decode(\StringOperations::substr($allgsettings->vhsname, 4)),
      '{%admin_vhsstrasse%}' => \StringOperations::html_entity_decode($allgsettings->vhsstrasse),
      '{%admin_vhsort%}' => \StringOperations::html_entity_decode($allgsettings->vhsort),
      '{%admin_vhstelefon%}' => \StringOperations::html_entity_decode($allgsettings->vhstelefon),
      '{%admin_vhsfax%}' => \StringOperations::html_entity_decode($allgsettings->vhsfax),
      '{%admin_vhsemail%}' => \StringOperations::html_entity_decode($allgsettings->vhsemail),
      '{%admin_vhsagb%}' => \StringOperations::html_entity_decode($allgsettings->vhsagb),
      '{%admin_vhswiderruf%}' => \StringOperations::html_entity_decode($allgsettings->vhswiderruf),
      '{%admin_vhsimpressum%}' => \StringOperations::html_entity_decode($allgsettings->impressum),
      '{%admin_vhskontakt%}' => \StringOperations::html_entity_decode($allgsettings->kontakt),
      '{%admin_vhswebsite%}' => \StringOperations::html_entity_decode($adminSettings->vhswebsite)
    );

    foreach ($map as &$val) {
      $val = \StringOperations::htmlentities($val);
    }

    return \StringOperations::str_fast_parallel_replace($html, $map);
  }
}