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


class FinanzamtVorlage extends DruckListeZahlung
{
  
  public $kurs;

  
  public $vhs;

  
  public $dozent;

  
  public $zahljahr;

  public function getListID()
  {
    return 11;
  }

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

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

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

  public function setTitle1()
  {
    $this->settings->title1 = 'Zur Vorlage beim Finanzamt';
  }

  
  public function getDruckbereichEnabled($optionName)
  {
    switch ($optionName) {
      case 'hochformat':
        return true;
    }

    return false;
  }

  public function isAllowedToPrint($pToolId)
  {
    
    return true;
  }

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

    $this->pdf = $this->__initPDF();
    $this->_prepare();

    $this->zahlungary = array();
    if (is_numeric($this->dozent->id)) {
      try {
        $this->zahlungary = \Kufer\Web\KuferTools\Utils\Dozent\HonorarZahlungenUtils::findByDozentId(\Kufer\Web\Classes\Identifier\Identifier::fromId($this->dozent->id), $this->zahljahr);
      } catch (AbgleichApiException $ex) {
        $this->keineDaten = true;

        return;
      }
    }

    if ($this->zahlungary) {
      $this->pdf->AddPage();
      $this->_createContent();
      $this->footer();
      $this->pdf->Output('', $this->getOutputName());
    } else {
      if (!defined('ISKUFERTOOLS_RESPONSIVE') || !ISKUFERTOOLS_RESPONSIVE) { 
        $listname = get_class($this);
        echo DruckListen::ausgabeNoSingleDruckliste($listname, true);
      } else {
        $this->keineDaten = true;
      }
    }
  }

  public function initKurstage()
  {
    $aktDate = DateTimeOperations::getDateNow();
    $aktuellesJahr = DateTimeOperations::getYearFromValidDate($aktDate);
    
    $this->zahljahr = $aktuellesJahr - 1;
    $this->kurstage = new webtools\drucklisten\Kurstage();
  }

  public function header($pKnr = '')
  {
    $this->_printKurstageHeader();
  }

  
  public function _prepare($pKnr = '')
  {
    
    
    
    $this->height = $this->_getKursCellHeight();
    
    $this->getDoznames();

    
    if ($this->settings->kurstage->zahljahr != '') {
      $this->zahljahr = $this->settings->kurstage->zahljahr;
    }

    $this->setZahlungsdatum2Object();
    
    $this->kurstage->von = '01.01.' . $this->zahljahr;
    $this->kurstage->bis = '31.12.' . $this->zahljahr;

    $this->vhs = AllgSettingsAPI::getAllgSettings();

    
    $this->pdf->SetFillColor(0, 0, 0);


    $dbwDozenten = new DBWorkDozenten(1, true);
    $this->dozent = $dbwDozenten->getDozent($this->dozId);
  }

  
  protected function setZahlungsdatum2Object()
  {
    

    $this->dbwzahlung = new DBWorkZahlung();
    $this->dbwzahlung->newSearch();
    
    $this->dbwzahlung->fromobject->datum = '01.01.' . $this->zahljahr;
    $this->dbwzahlung->toobject->datum = '31.12.' . $this->zahljahr;
  }

  
  public function setKurstagary($pDbwkurstage)
  {
    return;
  }

  
  public function _printKurstageHeader()
  {
    
    
    $logoFilePath = PDFLogo::getLogoFilePath(array('finanzamt_logo.gif', 'finanzamt_logo.jpg'));
    if (!PDFLogo::printLogoImg($this->pdf, $logoFilePath)) {
      
      $this->pdf->ln(-2);
      $this->pdf->CellAtX('43%', '', '', '', '', '53%', 1, 28);
      $this->pdf->ln(1);
      $this->pdf->CellAtX('96%', '', '', '', '', '0.5%', 0, 28, 1);
      $this->pdf->ln(2);
      
      $onePercentWidth = $this->pdf->GetPageWidth() / 100;
      
      $this->pdf->SetFontSize(18);
      if ($this->pdf->GetStringWidth($this->vhs->vhsname) <= $onePercentWidth * 47) {
        
        $this->pdf->CellAtX('44%', StringOperations::html_entity_decode($this->vhs->vhsname), 'B', '', '', '52%', 0, 8);
      } else {
        $bool = true;
        for ($size = 19; $size > 0 && $bool == true; $size--) {
          $this->pdf->SetFontSize($size);
          if ($this->pdf->GetStringWidth($this->vhs->vhsname) <= $onePercentWidth * 47) {
            $this->pdf->CellAtX('44%', StringOperations::html_entity_decode($this->vhs->vhsname), 'B', '', '', '52%', 0, 8);
            $bool = false;
          }
        }
      }
      $this->pdf->SetFontSize(10);
      $this->pdf->ln(9);
      $this->pdf->CellAtX('44%', StringOperations::html_entity_decode($this->vhs->vhsstrasse), '', '', '', '52%', 0, 8);
      $this->pdf->ln(5);
      $this->pdf->CellAtX('44%', StringOperations::html_entity_decode($this->vhs->vhsort), 'B', '', '', '52%', 0, 8);
      $this->pdf->ln(5);
      $this->pdf->CellAtX('44%', 'Tel: ' . $this->vhs->vhstelefon, '', '', '', '22%', 0, 8);
      $this->pdf->ln(6);
      $this->pdf->CellAtX('43.5%', '', '', '', '', '53%', 0, 1, 1);
      $this->pdf->ln(9);
    } else {
      $this->pdf->ln(9);
    }

    $ln_count = 0;
    
    
    $this->pdf->CellAtX('11.75%', StringOperations::html_entity_decode($this->vhs->vhsname) . ', ' . StringOperations::html_entity_decode($this->vhs->vhsstrasse) . ', ' . StringOperations::html_entity_decode($this->vhs->vhsort), 'B', '', 8, '80%', 0, $this->height);
    if ($this->dozent->zusatz && $this->dozent->zusatz2 && $this->dozent->nation) {
      $this->pdf->ln(-$this->height);
      $ln_count = -1;
    }
    $this->pdf->ln(2 * $this->height);
    $ln_count += 2;
    
    $this->pdf->CellAtX('11.75%', $this->dozent->anrede, '', '', 10, '80%', 0, $this->height);
    $this->pdf->ln($this->height);
    $ln_count++;
    $this->pdf->CellAtX('11.75%', $this->dozent->vorname . ' ' . $this->dozent->name, '', '', 10, '80%', 0, $this->height);
    $this->pdf->ln($this->height);
    $ln_count++;
    if ($this->dozent->zusatz) {
      $this->pdf->CellAtX('11.75%', $this->dozent->zusatz, '', '', 10, '80%', 0, $this->height);
      $this->pdf->ln($this->height);
      $ln_count++;
    }
    if ($this->dozent->zusatz2) {
      $this->pdf->CellAtX('11.75%', $this->dozent->zusatz2, '', '', 10, '80%', 0, $this->height);
      $this->pdf->ln($this->height);
      $ln_count++;
    }
    $this->pdf->CellAtX('11.75%', $this->dozent->strasse, '', '', 10, '80%', 0, $this->height);
    $this->pdf->ln($this->height);
    $ln_count++;
    $this->pdf->CellAtX('11.75%', $this->dozent->ort, '', '', 10, '80%', 0, $this->height);
    if ($this->dozent->nation) {
      $this->pdf->ln($this->height);
      $ln_count++;
      $this->pdf->CellAtX('11.75%', $this->dozent->nation, '', '', 10, '80%', 0, $this->height);
    }
    $this->pdf->ln((8 - $ln_count) * $this->height);


    
    $this->pdf->CellAtX('80%', 'Datum: ' . $this->dtop->getDateNow(), '', '', '', '20%', 0, $this->height);
    $this->pdf->ln(3 * $this->height);
  }

  
  protected function _printDruckListe($pKurs = '')
  {
    if (!($this->settings->title1)) {
      $this->setTitle1();
    }

    $this->pdf->CellAtX('11.75%', $this->settings->title1, 'B', '', 14, '80%', 0, 2 * $this->height);
    $this->pdf->ln($this->height);
    $this->pdf->CellAtX('11.75%', $this->settings->title2, '', '', 8, '80%', 0, $this->height);
    $this->pdf->ln(7 * $this->height);

    
    $summeHonorar = 0;
    $summeFahrtkosten = 0;
    $summeSpesen = 0;
    $summeSonstige = 0;

    
    if ($this->zahlungary) {
      foreach ($this->zahlungary as $zahlung) {
        switch ($zahlung->art) {
          case 1: $summeHonorar += $this->mop->stringToFloat($zahlung->betrag);
            break;
          case 2: $summeHonorar += $this->mop->stringToFloat($zahlung->betrag);
            break;
          case 3: $summeFahrtkosten += $this->mop->stringToFloat($zahlung->betrag);
            break;
          case 4: $summeSpesen += $this->mop->stringToFloat($zahlung->betrag);
            break;
          default: $summeSonstige += $this->mop->stringToFloat($zahlung->betrag);
            break;
        }
      }
    }

    
    $this->pdf->CellAtX('11.75%', $this->dozent->anrede . ' ' . $this->dozent->vorname . ' ' . $this->dozent->name . ' hat im Zeitraum vom', '', '', 8, '80%', 0, $this->height);
    $this->pdf->ln($this->height);
    $this->pdf->CellAtX('11.75%', $this->kurstage->von . ' bis zum ' . $this->kurstage->bis . ' für nebenberufliche Lehrtätigkeit', '', '', 8, '80%', 0, $this->height);
    $this->pdf->ln($this->height);
    $this->pdf->CellAtX('11.75%', 'bei der ' . StringOperations::html_entity_decode($this->vhs->vhsname) . ' Honorare in Höhe', '', '', 8, '80%', 0, $this->height);
    $this->pdf->ln($this->height);
    $this->pdf->CellAtX('11.75%', 'von EUR ' . $this->mop->floatToString(($summeHonorar == 0) ? '0' : $summeHonorar) . ' erhalten.', '', '', 8, '80%', 0, $this->height);
    $this->pdf->ln(3 * $this->height);

    if ($summeFahrtkosten != 0) {
      $this->pdf->CellAtX('11.75%', 'Für Fahrtkosten wurden EUR ' . $this->mop->floatToString($summeFahrtkosten) . ' erstattet.', '', '', 8, '80%', 0, $this->height);
      $this->pdf->ln(2 * $this->height);
    }
    if ($summeSpesen != 0) {
      $this->pdf->CellAtX('11.75%', 'Für Lehrvorbereitung u. Ä. wurden EUR ' . $this->mop->floatToString($summeSpesen) . ' erstattet.', '', '', 8, '80%', 0, $this->height);
      $this->pdf->ln(2 * $this->height);
    }
    if ($summeSonstige != 0) {
      $this->pdf->CellAtX('11.75%', 'Für Sonstiges wurden EUR ' . $this->mop->floatToString($summeSonstige) . ' erstattet.', '', '', 8, '80%', 0, $this->height);
      $this->pdf->ln(2 * $this->height);
    }
  }

  
  public function __createFusszeilen()
  {
    if (trim($this->settings->endbem)) {
      $this->pdf->ln(10);
      $text = $this->stringLineBreak(trim($this->settings->endbem), 180);
      if (count($text) <= 4) {
        foreach ($text as $txt) {
          $this->pdf->CellAtX('11.75%', $txt, '', '', 8);
          $this->pdf->ln();
        }
      } else {
        exit('Der Text für die Fusszeile ist zu lang! Bitte maximal 4 Zeilen verwenden.');
      }
    }

    
    $this->pdf->ln(4 * $this->height);
    $this->pdf->CellAtX('11.75%', 'Dieses Schreiben wurde maschinell erstellt und ist ohne Unterschrift gültig', '', '', 8, '80%', 0, $this->height);
    $this->pdf->ln($this->height);
  }

  

  
  public function _getKurstage($changeHL)
  {
    $aktDate = DateTimeOperations::getDateNow();
    $aktuellesJahr = DateTimeOperations::getYearFromValidDate($aktDate);

    
    
    $disabled = $changeHL || (!$this->getKurstageEnabled());
    $result = $this->fieldname();

    $options = array();
    $options[1] = '<option selected value=' . ($aktuellesJahr - 1) . '>' . ($aktuellesJahr - 1) . '</option>';
    $options[2] = '<option value=' . ($aktuellesJahr - 2) . '>' . ($aktuellesJahr - 2) . '</option>';
    $options[3] = '<option value=' . ($aktuellesJahr - 3) . '>' . ($aktuellesJahr - 3) . '</option>';
    $indiv = '';

    $result .= '<label id="l_zahljahr" for="zahljahr">Jahr:</label>'
                . HTMLOperations::CreateSelectTag($options, 'zahljahr', '', 'zahljahr', 1, $indiv, false, $disabled);

    $result .= '</fieldset>';

    return $result;
  }

  
  public function fieldname()
  {
    return "<fieldset id=\"kurstage\" style='height:60px;'><legend>Zahlungszeitraum</legend>";
  }
}