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


class Stundenabrechnung extends Kurstageliste
{
  
  public $sumktage;

  
  public $kursetage;

  
  public $kursdargestellt;

  public function getListID()
  {
    return 14;
  }

  public function getEinstellDarstellungNameForOption(string $optionName): ?string
  {
    switch ($optionName) {
      case 'option1':
        return 'Dozentenhonorar (pro UE) auch ausgegeben';
      default:
        return parent::getEinstellDarstellungNameForOption($optionName); 
    }
  }

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

  public function setDrucksettings()
  {
    $this->settings = new \Kufer\Web\WebTools\DruckListen\StundenabrechnungListSettings();
  }

  
  public function loadSettings()
  {
    parent::loadSettings();
    $this->settings->druckbereich->hochformat = 0; 
  }

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

  

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

  

  
  public function _prepare($pKnr = '')
  {
    parent::_prepare($pKnr);

    
    
    $this->spaltenbeginnary = array(3, 15, 57);
    $this->spaltenbreiteary = array(12, 48, 8);

    $this->kursdargestellt = false;
  }

  

  

  
  protected function _printDruckListe($pKurs = '')
  {
    
    $this->pdf->CellAtX($this->spaltenbeginnary[0] . '%', 'Kursnr', '', '', '', $this->spaltenbreiteary[0] . '%', 0, $this->height);
    $this->pdf->CellAtX($this->spaltenbeginnary[1] . '%', 'Kurstitel', '', '', '', $this->spaltenbreiteary[1] . '%', 0, $this->height);
    $this->pdf->CellAtX($this->spaltenbeginnary[2] . '%', 'Kurstage', '', '', '', '32%', 0, $this->height);
    $this->pdf->ln($this->height);

    $this->pdf->CellAtX('3%', '', '', '', '', '94%', 1, 0.1);

    foreach ($this->kursary as $kurs) {
      $this->_printKursCell($kurs);
    }

    if ($this->kursdargestellt == false) {
      $this->pdf->ln(2 * $this->height);
      $this->pdf->CellAtX($this->spaltenbeginnary[0] . '%', 'Für den oben angegebenen Zeitbereich wurden keine Kurse gefunden', 'B', '', 14, '90%', 0, $this->height);
    }
  }

  

  
  public function _printKursCell($pkurs)
  {
    

    $dbwkurstage = new DBWorkKurseTage(1, true);
    
    $dbwkurstage->newSearch();

    
    
    if ($this->toolId == \ToolsAPI::TOOL_KURSUEBERSICHT_DOZ) {
      $dbwkurstage->toobject->nodoztool = true;
    }
    $dbwkurstage->fromobject->datum = $this->kurstage->von;
    $dbwkurstage->toobject->datum = $this->kurstage->bis;
    
    $kursetageary = $dbwkurstage->getKurstagebyKursNRDozIDOnlyDifferent($pkurs->id, $this->dozId);

    
    if ($kursetageary) {
      
      $this->kursdargestellt = true;

      
      $dbhonorar = new DBWorkDozentenHonorar();
      $honorar = $dbhonorar->getHonorarbyKNRDozentNR($pkurs->id, $this->dozId);

      
      $hoeheKursDaten = 5 * $this->height;
      
      
      $hoeheKursTage = ((int)((count($kursetageary) - 1) / 5) + 1) * 2 * $this->height;

      if ($this->showDozentenhonorarProUe()) {
        $hoeheKursDaten += 3 * $this->height;
      }

      $hoeheBlock = max($hoeheKursDaten, $hoeheKursTage);
      if ($this->pdf->TriggersPageBreak($hoeheBlock)) {
        $this->pdf->AddPage();
      }

      
      $this->pdf->CellAtX($this->spaltenbeginnary[0] . '%', $pkurs->id, '', '', '', $this->spaltenbreiteary[0] . '%', 0, $this->height);
      $this->pdf->CellAtX($this->spaltenbeginnary[1] . '%', $pkurs->kurzbez, '', '', '', $this->spaltenbreiteary[1] - 6 . '%', 0, $this->height);
      $this->pdf->ln($this->height);
      $this->pdf->CellAtX($this->spaltenbeginnary[1] . '%', $pkurs->ort, '', '', '', $this->spaltenbreiteary[1] . '%', 0, $this->height);
      $this->pdf->ln($this->height);
      $this->pdf->CellAtX($this->spaltenbeginnary[1] . '%', $pkurs->beginn_dat . ' - ' . $pkurs->ende_dat, '', '', '', $this->spaltenbreiteary[1] . '%', 0, $this->height);
      $this->pdf->ln($this->height);
      $this->pdf->CellAtX($this->spaltenbeginnary[1] . '%', $pkurs->beginn . ' - ' . $pkurs->ende . ' Uhr', '', '', '', $this->spaltenbreiteary[1] . '%', 0, $this->height);
      $this->pdf->ln($this->height);

      $ue = $this->mop->floatToString($this->mop->stringToFloat($pkurs->stundenanz));

      if ($honorar->honorarstatus == 'P') { 
        $honfaktor = $ue;
      } else {
        $honfaktor = $this->mop->floatToString($this->mop->stringToFloat($honorar->getVarValue('honfaktorMitKomma')));
      }

      $this->pdf->CellAtX($this->spaltenbeginnary[1] . '%', $pkurs->dauer . ' / ' . $honfaktor . ' von ' . $ue . ' UE', '', '', '', $this->spaltenbreiteary[1] . '%', 0, $this->height);
      $this->pdf->ln($this->height);

      if ($this->showDozentenhonorarProUe()) {
        $this->pdf->CellAtX(
          $this->spaltenbeginnary[1] . '%',
          'Honorar: ' . \MathOperations::floatToStringWithThousandSeps(\MathOperations::stringToFloat($honorar->hongesamt)) . ' EUR ('
          . \MathOperations::floatToStringWithThousandSeps(\MathOperations::stringToFloat($honorar->honsatz)) . ' EUR/UE)'
        );
        $this->pdf->ln($this->height);
        $this->pdf->CellAtX(
          $this->spaltenbeginnary[1] . '%',
          'Fahrkosten: ' . $honorar->fahrtgesamt . ' EUR ('
          . \MathOperations::floatToStringWithThousandSeps(\MathOperations::stringToFloat($honorar->fahrtgesamt) / count($kursetageary))
          . ' EUR/Kurstag)'
        );
        $this->pdf->ln($this->height);
        $this->pdf->CellAtX(
          $this->spaltenbeginnary[1] . '%',
          'Gesamt: ' . \MathOperations::floatToStringWithThousandSeps(\MathOperations::stringToFloat($honorar->gesamt)) . ' EUR'
        );
        $this->pdf->ln($this->height);

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

      $this->pdf->ln(-5 * $this->height);

      
      $count = 0;

      foreach ($kursetageary as $kurstag) {
        
        $this->pdf->CellAtX(($this->spaltenbeginnary[2] + $this->spaltenbreiteary[2] * ($count % 5)) . '%', $this->dtop->getShortForm4DateStr($kurstag->datum), '', '', '', $this->spaltenbreiteary[2] . '%', 1, $this->height);
        $this->pdf->ln($this->height);
        $this->pdf->CellAtX(($this->spaltenbeginnary[2] + $this->spaltenbreiteary[2] * ($count % 5)) . '%', '', '', '', '', $this->spaltenbreiteary[2] . '%', 1, $this->height);
        $this->pdf->ln(-$this->height);
        $count++;
        if ($count % 5 == 0) {
          $this->pdf->ln(2 * $this->height);
        }
      }

      if ($count > 11) {
        $this->pdf->ln($this->height);
      } else {
        $this->pdf->ln((-2 * (int)($count / 5)) * $this->height + 3 * $this->height);
      }

      $this->pdf->ln(3 * $this->height);

      if ($this->showDozentenhonorarProUe()) {
        $this->pdf->ln(3 * $this->height);
      }
    }
  }

  public function showDozentenhonorarProUe(): bool
  {
    return $this->settings->einstellDarstellung->option1 ?? false;
  }
}