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


class Stundenabrechnung extends Kurstageliste
{
  
  public $sumktage;

  
  public $kursetage;

  
  public $kursdargestellt;

  public function getListID()
  {
    return 14;
  }

  

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

  

  
  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;
    
    $this->kursetageary = $dbwkurstage->getKurstagebyKursNRDozIDOnlyDifferent($pkurs->id, $this->dozId);

    
    if ($this->kursetageary) {

            
      $this->kursdargestellt = true;

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

      
      $hoeheKursDaten = 5 * $this->height;
      
      
      $hoeheKursTage = ((int)((count($this->kursetageary) - 1) / 5) + 1) * 2 * $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(-4 * $this->height);

      
      $count = 0;

      foreach ($this->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);
    }
  }

  
}