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


class TNKursstammblatt extends TNDruckliste
{
  public $belid;
  public $belegung;
  
  protected $offset;
  
  protected $offset2;
  
  protected $fontbig;
  
  protected $fontsmall;
  
  protected $border;
  
  protected $randleft;
  
  protected $randright;

  
  public function create($belid = -1, $pTnr = -1, $pTerminbereich = '', $pVonDate = '', $pBisDate = '', $pDozId = -1, $pPrintAllowed4User = false)
  {
    $this->belid = $belid;

    $this->settings->druckbereich->hochformat = 1;
    $this->pdf = $this->__initPDF();
    $this->pdf->SetAutoPageBreak(true);

    $this->_prepare();
    $this->pdf->AddPage();
    $this->_createHeader();
    $this->_createContent();
    
    $this->pdf->Output('', $this->getOutputName());
  }

  public function getOutputName(): string
  {
    return 'kursstammblatt.pdf';
  }

  
  public function __initPDF($pSetHeaderCallback = true)
  {
    $pdf = new ExtPDF(5, true, 'P');
    $pdf->SetFont('OpenSans', '', 10);

    return $pdf;
  }

  
  public function _prepare($pData = array(), $pTnr = -1, $pTerminbereich = '', $pVonDate = '', $pBisDate = '', $pKnr = '')
  {
    $dbwbeleg = new DBWorkBelegung();
    $this->belegung = $dbwbeleg->getBelegung($this->belid);

    
    $this->_setRand();
    $this->fontbig = $this->getfontSize('big');
    $this->fontsmall = $this->getfontSize('small');
    $this->height = 5;
    $this->offset = '17%';
    $this->offset2 = '62%';
    $this->border = 0;
  }

  

  
  public function _createHeader($pKurs = '')
  {
    $allgsettings = AllgSettingsAPI::getAllgSettings();

    
    
    $logoFilePath = PDFLogo::getLogoFilePath();
    if (PDFLogo::printLogoImg($this->pdf, $logoFilePath)) {
      $this->pdf->ln(9);
    } else {
      $this->pdf->ln();
    }

    
    $this->pdf->ln();
    $this->pdf->CellAtX('3%', StringOperations::html_entity_decode($allgsettings->vhsname), 'B', '', 16);
    $this->pdf->CellAtX('80%', 'Stand: ' . $this->dtop->getDateNow());
    $this->pdf->ln(10);
  }

  

  
  public function _createContent($pKurs = '')
  {
    $this->_createLine(true);
    $this->_printSimpleOptionRows('Kurs:', $this->belegung->knr, $this->offset);
    $this->_printSimpleOptionRows('Titel:', $this->belegung->kurs->kurzbez, $this->offset);
    $this->_createLine(false);
    if (DBWorkBASysSettings::isUnnaForum()) {
      $this->_printSimpleOptionRows('Zeitraum:', $this->belegung->kurs->getVarValue('beginn_ende_komplett'), $this->offset);
    } else {
      $this->_printSimpleOptionRows('Beginn / Ende:', $this->belegung->kurs->getVarValue('beginn_dat_wotagkurz') . ', ' . $this->belegung->kurs->beginn_dat . ' - ' . $this->belegung->kurs->getVarValue('ende_dat_wotagkurz') . ', ' . $this->belegung->kurs->ende_dat, $this->offset);
      $this->_printSimpleOptionRows('Uhrzeit:', $this->belegung->kurs->beginn . ' - ' . $this->belegung->kurs->ende, $this->offset);
    }
    $this->_printSimpleOptionRows('Kursort:', $this->belegung->kurs->ort, $this->offset);
    $this->_printSimpleOptionRows('Dozent:', $this->belegung->kurs->getVarValue('hauptdozent')->namevorname, $this->offset);
    $this->_createLine(false);
    $this->_printSimpleOptionRows('Info:', $this->belegung->kurs->info, $this->offset);
    $this->_createLine(false);
    if (!DBWorkBASysSettings::isUnnaForum()) {
      $this->_printSimpleOptionRows('Vorspann:', $this->belegung->kurs->vorspann, $this->offset);
      $this->_printSimpleOptionRows('Nachspann:', $this->belegung->kurs->nachspann, $this->offset);
      $this->_createLine(false);
      $this->_printSimpleOptionRows('Warn-Hinweis:', $this->belegung->kurs->warnung, $this->offset);
    }

    if (DBWorkBASysSettings::isUnnaForum()) {
      
      $this->_printSimpleOptionRows('Leistungen:', $this->belegung->kurs->material, $this->offset);
    } else {
      $this->_printSimpleOptionRows('Mitzubringen:', $this->belegung->kurs->material, $this->offset);
    }
    $this->_createLine(false);
    if (DBWorkBASysSettings::isUnnaForum()) {
      $this->_printEntgeltUnnaForum();
    } else {
      $this->_printEntgelt();
    }
    $this->_createLine(true);
  }

  

  
  public function _printEntgelt()
  {
    $betrag = number_format(str_replace(',', '.', $this->belegung->entgelt) - str_replace(',', '.', $this->belegung->bet_offen), 2, ',', '');

    $this->zbreak($this->height);
    $this->pdf->CellAtX('4%', 'Entgelt:', '', '', '', '17%', $this->border, $this->height);
    $this->pdf->CellAtX('17%', $this->belegung->entgelt, 'B', 'R', $this->fontbig, '8%', $this->border, $this->height);
    $this->ln($this->height);

    $this->zbreak($this->height);
    $this->pdf->CellAtX('4%', 'bereits bezahlt:', '', '', '', '17%', $this->border, $this->height);
    $this->pdf->CellAtX('17%', $betrag, 'B', 'R', $this->fontbig, '8%', $this->border, $this->height);
    $this->ln($this->height);

    $this->zbreak($this->height);
    $this->pdf->CellAtX('4%', 'noch offen:', '', '', '', '17%', $this->border, $this->height);
    $this->pdf->CellAtX('17%', $this->belegung->bet_offen, 'B', 'R', $this->fontbig, '8%', $this->border, $this->height);
    $this->ln($this->height);
  }

  public function _printEntgeltUnnaForum()
  {
    $this->zbreak($this->height);
    $this->pdf->CellAtX('4%', 'Kursgebühr:', '', '', '', '17%', $this->border, $this->height);
    $this->pdf->CellAtX('17%', $this->belegung->entgelt, 'B', '', $this->fontbig, '80%', $this->border, $this->height);
    $this->ln($this->height);

    $this->zbreak($this->height);
    $this->pdf->CellAtX('4%', 'Konto-', '', '', '', '17%', $this->border, $this->height);
    $this->ln($this->height);
    $this->pdf->CellAtX('4%', 'verbindung:', '', '', '', '17%', $this->border, $this->height);
    $this->pdf->CellAtX('17%', 'FORUM für Politik, Wirtschaft und int. Begegnung e.V.', 'B', '', $this->fontbig, '80%', $this->border, $this->height);
    $this->ln($this->height);

    $this->pdf->CellAtX('17%', 'DE94 4435 0060 0000 0407 09', 'B', '', $this->fontbig, '80%', $this->border, $this->height);
    $this->ln($this->height);
    $this->pdf->CellAtX('17%', 'WELADED1UNN', 'B', '', $this->fontbig, '80%', $this->border, $this->height);

    $this->ln($this->height);

    $this->zbreak($this->height);
    $this->pdf->CellAtX('4%', 'Verwendungs-', '', '', '', '17%', $this->border, $this->height);

    $this->ln($this->height);
    $this->pdf->CellAtX('4%', 'zweck:', '', '', '', '17%', $this->border, $this->height);
    $this->pdf->CellAtX('17%', 'Kurs-Nr ' . $this->belegung->knr . ' und Kunden-Nr ' . $this->belegung->teilnehmer->pers_nr, 'B', '', $this->fontbig, '80%', $this->border, $this->height);
    $this->ln($this->height);
  }

  
  public function zbreak($height)
  {
    if ($this->pdf->TriggersPageBreak($height)) {
      $this->pdf->CellAtX('3%', ' ', '', '', '', '92%', 'T', 0, 0);
    }
  }

  

  
  public function ln($height)
  {
    $this->pdf->CellAtX('3%', ' ', '', '', '', '92%', 'LR', $height);
    $this->pdf->ln($height);
  }

  

  
  public function _createLine($bold = false)
  {
    if ($bold) {
      $this->pdf->setLineWidth(0.4);
    }
    $this->pdf->CellAtX($this->randleft, ' ', '', '', '', $this->randright, 'T', 0);
    if ($bold) {
      $this->pdf->setLineWidth(0.2);
    }
  }

  
  public function _printSimpleOptionRows($optCaption, $optValue, $pValOffset)
  {
    $this->zbreak($this->height);
    $this->pdf->CellAtX('4%', $optCaption, '', '', '', $pValOffset, $this->border, $this->height);
    
    $strary = $this->stringLineBreak($optValue, 150);
    
    if (count($strary) == 0) {
      $strary[0] = ' ';
    }

    foreach ($strary as $aryzeile) {
      
      $this->zbreak($this->height);
      $this->pdf->CellAtX($pValOffset, $aryzeile, 'b', '', $this->fontbig, '77%', $this->border, $this->height);
      $this->ln($this->height);
    }
  }

  

  
  public function getfontSize($pValue)
  {
    if ($pValue == 'big') {
      return 10;
    }

    return 9;
  }

  

  
  public function _setRand()
  {
    
    $this->randleft = '3.1%';
    if ($this->settings->druckbereich->hochformat == 1) {
      $this->randright = '91.8%';
    } else {
      $this->randright = '91.9%';
    }
  }

  
}