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

class Stundennachweis extends DruckListe
{
  
  protected $_summeUe = 0.0;
  
  protected $_unterrichtsTageAry = array();

  
  protected $_aktKnr;

  
  protected $_aktDozId;

  public function getListID()
  {
    return 1000;
  }

  public function getListTyp()
  {
    
    
    return 'N';
  }

  
  public function header($pKurs = '')
  {
    $this->_createHeader($pKurs);
  }

  
  public function footer()
  {
    $heightFooter = $this->height * 6;
    if ($this->pdf->TriggersPageBreak($heightFooter)) {
      $this->pdf->AddPage();
    }

    $beginnAry = $this->_getBeginnAry4HeaderAndFooter();

    
    $summeUeStr = MathOperations::floatToString($this->_summeUe);
    $this->pdf->CellAtX($beginnAry['left'], 'Termine: ' . count($this->kursetage), '', '', '', '20%', '', $this->height);
    $this->pdf->CellAtX($this->spaltenbeginnary[4] + $this->_getXFactor4Ue($summeUeStr) . '%', $summeUeStr, '', '', '', '25%', '', $this->height);
    $this->pdf->ln($this->height);
    $this->pdf->CellAtX($beginnAry['left'], 'Unterrichtstage: ' . count($this->_unterrichtsTageAry), '', '', '', '25%', '', $this->height);

    if (isset($this->settings->endbem) && trim($this->settings->endbem) !== '') {
      $text = array_slice($this->pdf->splitString($this->settings->endbem, 180), 0, 4);
      $this->pdf->SetY($this->pdf->getPageBreakTrigger() - (count($text) - 1) * ($this->height + $this->pdf->getCMargin()));
      foreach ($text as $txt) {
        $this->pdf->CellAtX('3%', $txt, '', '', 10);
        $this->pdf->ln($this->height);
      }
    } else {
      $this->pdf->ln(10);
      $text[] = 'Ich versichere, dass von mir tatsächlich .................. Unterrichtsstunden gegeben worden sind.';
      $text[] = 'Fahrstrecke: von .......................................... nach .......................................... = ....................... km (einfache Entfernung)';
      $text[] = 'Die Überweisung des Honorars soll auf das bekannte Konto erfolgen. Kontoänderungen ggfs. hier eintragen.';
      $text[] = 'Ort: ...........................................    Datum: ..............................             Unterschrift: ..................................................';
      $this->pdf->SetY($this->pdf->getPageBreakTrigger() - (count($text) - 1) * ($this->height + $this->pdf->getCMargin()));
      foreach ($text as $txt) {
        $this->pdf->CellAtX('3%', $txt, '', '', 10);
        $this->pdf->ln($this->height);
      }
    }
  }

  public function setTitle1()
  {
    $this->settings->title1 = 'Stundennachweis';
  }

  public function loadFactoryDefaults()
  {
    parent::loadFactoryDefaults();
    $this->setTitle1();  
  }

  
  public function getKurstageEnabled()
  {
    return true;
  }

  
  public function setDrucksettings()
  {
    $this->settings = new \webtools\drucklisten\LehrplanlisteSettings();
  }

  public function _getSubtitle()
  {
    return '';
  }

  public function _getEndbemerkung()
  {
    return '';
  }

  
  public function _prepare($pKnr = '', $pDozId = null)
  {
    $this->_aktKnr = $pKnr;
    $this->_aktDozId = $pDozId;

    parent::_prepare($pKnr);
    

    
    if (isset($this->settings->druckbereich->hochformat) && $this->settings->druckbereich->hochformat) {
      
      $this->spaltenbeginnary = array(6.5, 9.5, 19.5, 26.5, 33.5, 40, 72.5);
      $this->spaltenbreiteary = array(4, 10, 7, 7, 6, 30, 21);
    } else { 
      $this->spaltenbeginnary = array(4, 7, 14, 19, 24, 27.5, 77);
      $this->spaltenbreiteary = array(3, 7, 5, 5, 4, 45, 18);
    }
  }

  
  public function setKurstagary($pDbwkurstage)
  {
    
    $user = SessionOperations::getCurrentWebToolsUser();
    $pDbwkurstage->fromobject->dnr = $user->dnr;
    $pDbwkurstage->toobject->dnr = $user->dnr;

    $WithoutKtStatus = array('A');
    
    if (isset($this->settings->einstellDarstellung->option4) && $this->settings->einstellDarstellung->option4) {
      $WithoutKtStatus = array();
    }

    $tool0Settings = \ToolSettingsAPI::getToolSettings(0);
    $wtUser = \SessionOperations::getCurrentWebToolsUser();
    if (isset($this->_aktDozId)) {
      $this->kursetage = $pDbwkurstage->getKurstageByKursNrAndDozentNr($this->_aktKnr, $this->_aktDozId, $WithoutKtStatus);
    } elseif ($tool0Settings->kurstageNurVonEingeloggtemDozent && $wtUser->dnr > 0) {
      $this->kursetage = $pDbwkurstage->getKurstageByKursNrAndDozentNr($this->_aktKnr, $wtUser->dnr, $WithoutKtStatus);
    } else {
      $this->kursetage = $pDbwkurstage->getKurstagebyKursNR($this->_aktKnr, -1, $WithoutKtStatus);
    }

    usort($this->kursetage, array('DruckListe', 'kTageCmp'));
  }

  public function _createContent($pKurs = '')
  {
    $this->_printDruckListe($pKurs);
  }

  
  public function _getEinstellDarstellung()
  {
    $result = '<fieldset id="einstelldarstellung"><legend>Einstellungen f&uuml;r Lehrpl&auml;ne</legend>';

    $vars = get_object_vars($this->settings->einstellDarstellung);
    $arykeys = array_keys($vars);
    for ($i = 1; $i <= 7; $i++) {
      $result .= '<div class="einstelldarst_lehrplan_' . $i . '">';
      $name = $arykeys[$i - 1];
      $displayName = $this->getEinstellDarstellungText($name);
      $checked = $this->settings->einstellDarstellung->{$name};
      $indiv = $this->getEinstellIndiv($name);
      $result .= HTMLOperations::CreateInputTag($name, 'checkbox', $name, '', '1', $indiv, $checked);
      $result .= '<label for="' . CleanOperations::cleanStringForHtml($name) . '">' . CleanOperations::cleanStringForHtml($displayName) . '</label>' . BR_WT;
      $result .= '</div>';
    }

    $result .= '</fieldset>';

    return $result;
  }

  
  public function getEinstellDarstellungText($pOptionName)
  {
    switch ($pOptionName) {
      case 'option4':
        return 'mit Ausfalltage';
      default:
        return '';
    }
  }

  
  public function _createHeader($pKurs = '')
  {
    $allgsettings = AllgSettingsAPI::getAllgSettings();
    
    
    $logoFilePath = PDFLogo::getLogoFilePath();
    if (PDFLogo::printLogoImg($this->pdf, $logoFilePath)) {
      $this->pdf->ln(9);
    } else {
      if (!isset($this->settings->druckbereich->hochformat) || !$this->settings->druckbereich->hochformat) {
        $this->pdf->ln(1.5);
      }
    }

    $beginnAry = $this->_getBeginnAry4HeaderAndFooter();

    
    
    $this->pdf->CellAtX($beginnAry['left'], StringOperations::html_entity_decode($allgsettings->vhsname), 'B', '', 16);
    $this->pdf->CellAtX($beginnAry['right'], 'Stand: ' . $this->dtop->getDateNow());
    $this->pdf->ln(6.5);

    
    if (!($this->settings->title1)) {
      $this->setTitle1();
    }
    $this->pdf->CellAtX($beginnAry['right'], 'Seite:           ' . $this->pdf->PageNo());
    $maxwidth = $this->getWidth4Title();

    if ($this->pdf->GetStringWidth($this->settings->title1 . ' für Kurs: ' . $pKurs->id . ' ' . $pKurs->kurzbez) > $maxwidth) {
      $this->pdf->ln(7);
    }
    $this->pdf->CellAtX($beginnAry['left'], $this->settings->title1 . ' für Kurs: ' . $pKurs->id . ' ' . $pKurs->kurzbez, 'B', '', '', '93%');
    $this->pdf->ln(6.0);

    $dozent = \SessionOperations::getCurrentWebToolsDozent();
    $this->pdf->CellAtX($beginnAry['left'], 'DozentIn: ' . $dozent->getVarValue('name_ggf_vorname'), 'B', '', '', '93%');
    $this->pdf->ln(7);
  }

  
  protected function _printDruckListe($pKurs = '')
  {
    $this->_summeUe = 0.0;
    $this->_unterrichtsTageAry = array();
    
    $this->pdf->CellAtX($this->spaltenbeginnary[0] . '%', '', '', '', '', $this->spaltenbreiteary[0] . '%', 0, $this->height);
    $this->pdf->CellAtX(($this->spaltenbeginnary[1] - 3) . '%', 'Datum', 'B', '', '', ($this->spaltenbreiteary[1] + 3) . '%', 0, $this->height);
    $this->pdf->CellAtX($this->spaltenbeginnary[2] . '%', 'Beginn', 'B', '', '', $this->spaltenbreiteary[2] . '%', 0, $this->height);
    $this->pdf->CellAtX($this->spaltenbeginnary[3] . '%', 'Ende', 'B', '', '', $this->spaltenbreiteary[3] . '%', 0, $this->height);
    $this->pdf->CellAtX(($this->spaltenbeginnary[4] + 0.5) . '%', 'UE', 'B', '', '', ($this->spaltenbreiteary[4] - 0.5) . '%', 0, $this->height);
    $this->pdf->CellAtX($this->spaltenbeginnary[5] . '%', 'Veranstaltungsort', 'B', '', '', $this->spaltenbreiteary[5] . '%', 0, $this->height);
    $this->pdf->CellAtX($this->spaltenbeginnary[6] . '%', 'Bemerkung/Unterschrift', 'B', '', '', $this->spaltenbreiteary[6] . '%', 0, $this->height);

    $lineValues = $this->_getXAndWidth4Line();

    
    $this->pdf->ln(5);
    $this->pdf->CellAtX($lineValues['x'], '', '', '', '', $lineValues['width'], 1, 0);
    $this->pdf->ln(1);

    
    foreach ($this->kursetage as $kurstag) {
      $this->_printKursCell($kurstag);
    }

    
    $this->pdf->CellAtX($lineValues['x'], '', '', '', '', $lineValues['width'], 1, 0);
    $this->pdf->ln(1);
  }

  
  public function _printKursCell($pKursTag)
  {
    
    $ue = MathOperations::calculateUE($pKursTag->minuten, null, $pKursTag->knr);
    $this->_summeUe += $ue;
    $ue = MathOperations::floatToString($ue);
    if (!in_array($pKursTag->datum, $this->_unterrichtsTageAry)) {
      $this->_unterrichtsTageAry[] = $pKursTag->datum;
    }

    $this->pdf->CellAtX($this->spaltenbeginnary[0] . '%', str_replace('.', '', $pKursTag->wotagkurz) . ',', '', '', '', $this->spaltenbreiteary[0] . '%', '', $this->height);
    $this->pdf->CellAtX(($this->spaltenbeginnary[1]) . '%', $pKursTag->datum, '', '', '', ($this->spaltenbreiteary[1]) . '%', '', $this->height);
    $this->pdf->CellAtX($this->spaltenbeginnary[2] . '%', $pKursTag->zeit_beg, '', '', '', $this->spaltenbreiteary[2] . '%', '', $this->height);
    $this->pdf->CellAtX($this->spaltenbeginnary[3] . '%', $pKursTag->zeit_ende, '', '', '', $this->spaltenbreiteary[3] . '%', 0, $this->height);
    $this->pdf->CellAtX(($this->spaltenbeginnary[4] + $this->_getXFactor4Ue($ue)) . '%', $ue, '', '', '', ($this->spaltenbreiteary[4] - $this->_getXFactor4Ue($ue)) . '%', '', $this->height);
    $this->pdf->CellAtX($this->spaltenbeginnary[5] . '%', $pKursTag->ort, '', '', '', $this->spaltenbreiteary[5] . '%', 0, $this->height);
    $this->pdf->CellAtX($this->spaltenbeginnary[6] . '%', ' ', '', 'B', 6, $this->spaltenbreiteary[6] . '%', 'B', $this->height * 1.5);

    
    
    $this->pdf->ln($this->height * 2);
  }

  
  public function create($pDozId = -1, $pPrintAllowed4User = false)
  {
    $this->setDozId($pDozId);

    if (!empty($this->selnr)) {
      $selNrAry = explode(':', $this->selnr);
      
      
      if (!$this->checkDozTable($selNrAry) || !$pPrintAllowed4User) {
        $this->ausgabeFault($this->getWarntext($pPrintAllowed4User));
      } else {
        
        $dbwkurse = new DBWorkKurse(1, true);
        
        $this->pdf = $this->__initPDF();

        foreach ($selNrAry as $knr) {
          $kurs = $dbwkurse->getKurs($knr);

          $this->_createPage($kurs);
        }

        $this->pdf->Output('', $this->getOutputName());
      }
    }
  }

  protected function getOutputName(): string
  {
    return 'Stundennachweis.pdf';
  }

  
  protected function _createPage($pKurs, $pDozId = null)
  {
    
    $this->pdf->SetHeaderCallback(array($this, 'header'), $pKurs);
    $this->_prepare($pKurs->id, $pDozId);
    
    $this->pdf->AddPage();
    $this->_createContent();
    $this->footer();
  }

  
  protected function _getBeginnAry4HeaderAndFooter()
  {
    if (isset($this->settings->druckbereich->hochformat) && $this->settings->druckbereich->hochformat) {
      $beginnLeft = '6.5%';
      $beginnRight = '75%';
    } else {
      $beginnLeft = '4%';
      $beginnRight = '83.25%';
    }

    return array('left' => $beginnLeft, 'right' => $beginnRight);
  }

  
  protected function _getXAndWidth4Line()
  {
    if (isset($this->settings->druckbereich->hochformat) && $this->settings->druckbereich->hochformat) {
      $x = '7%';
      $width = '87%';
    } else {
      $x = '4%';
      $width = '91.5%';
    }

    return array('x' => $x, 'width' => $width);
  }

  
  protected function _getXFactor4Ue($pUeStr)
  {
    if (isset($this->settings->druckbereich->hochformat) && $this->settings->druckbereich->hochformat) {
      $mapping = array(5 => -0.9, 6 => -1.8, 7 => -2.7);
    } else {
      $mapping = array(5 => -0.6, 6 => -1.3, 7 => -2);
    }

    if (array_key_exists(StringOperations::strlen($pUeStr), $mapping)) {
      return $mapping[StringOperations::strlen($pUeStr)];
    }

    return 0;
  }

  
  protected function _getUnderLines()
  {
    if (isset($this->settings->druckbereich->hochformat) && $this->settings->druckbereich->hochformat) {
      return str_repeat('_ ', 11);
    } else {
      return str_repeat('_ ', 15);
    }
  }

  
  protected function _getBemerkungWithLineBreaks($pKursTag)
  {
    $bemerkungWidth = $this->_getBemerkungWidth();

    return $this->stringLineBreak($pKursTag->bemerkungen, $bemerkungWidth);
  }

  
  protected function _getBemerkungWidth()
  {
    
    if (isset($this->settings->druckbereich->hochformat) && $this->settings->druckbereich->hochformat) {
      $result = '88%';
    } else {
      $result = '92%';
    }

    return $result;
  }
}