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

namespace Kufer\Web\KuferTools\Teilnehmer\KursUebersicht\Detailsreiter\Journal;

class JournalUseCase extends \Kufer\Web\KuferTools\Basis\UseCase\EinzelKursUseCase
{
  private $dbwZahlung;

  public function __construct($request)
  {
    parent::__construct($request, new JournalResponse());
    $this->dbwZahlung = new \DBWorkZahlung();
  }

  public static function getToolId()
  {
    return \ToolsAPI::TOOL_TN;
  }

  public static function getToolSectionId()
  {
    return 298;
  }

  public function hasAccess($pToolSectionId = null)
  {
    if (!parent::hasAccess(335)) {
      return false;
    }
    if (!$this->isRechtAllowed('335.1.7')) {
      return false;
    }

    return true;
  }

  protected function getAllowedIds()
  {
    $user = $this->getUser();
    $belegungshelper = new \Kufer\Web\KuferTools\Utils\DatamodulHelper\Belegungen\TeilnehmerBelegungen($this->toolSettings, $user->tnr);

    return $belegungshelper->getIdsOhneFilter();
  }

  protected function getRequestedId()
  {
    return $this->request->belegId;
  }

  public function action()
  {
    if ($this->request->filter) {
      $this->setzeFilter();
    }
    $journal = $this->dbwZahlung->getZahlungJournalByBelegId($this->request->belegId);

    return $this->getResponse($journal);
  }

  protected function getResponse($journal)
  {
    $this->handleResponseStrategies();
    $this->response->journal = $journal;
    $this->calculateSums($journal);

    $this->response->filter = $this->request->filter;

    return $this->response;
  }

  private function setzeFilter()
  {
    $this->dbwZahlung->newSearch();

    
    if ($this->request->journalTerminBereich === 'zwischen') {
      if ($this->request->journalVon !== '') {
        $this->dbwZahlung->fromobject->datum = $this->request->journalVon;
      }
      if ($this->request->journalBis !== '') {
        $this->dbwZahlung->toobject->datum = $this->request->journalBis;
      }
    }

    
    if ($this->request->journalfilterSollHaben !== '') {
      if ($this->request->journalfilterSollHaben == 'solljournal') {
        $this->dbwZahlung->fromobject->sollist = 'S';
      } elseif ($this->request->journalfilterSollHaben == 'habenjournal') {
        $this->dbwZahlung->fromobject->sollist = 'I';
      }
    }
  }

  private function calculateSums($journal)
  {
    $summeSoll = 0;
    $summeHaben = 0;

    foreach ($journal as $einzelJournal) {
      $sollVal = floatval(\MathOperations::stringToFloat($einzelJournal->betrag_p));
      $haben = \MathOperations::stringToFloat($einzelJournal->betrag_m);
      $habenVal = floatval($haben);

      if (!is_nan($sollVal)) {
        $summeSoll += $sollVal;
      }
      if (!is_nan($habenVal)) {
        $summeHaben += $haben;
      }
    }

    $nochOffen = $summeSoll - $summeHaben;

    $this->response->summeSoll = \MathOperations::floatToString($summeSoll);
    $this->response->summeHaben = \MathOperations::floatToString($summeHaben);
    $this->response->nochOffen = \MathOperations::floatToString($nochOffen);
    $this->response->saldoNegative = $nochOffen > 0;
  }

  protected function getResponseStrategies()
  {
    return array(
      new \Kufer\Web\KuferTools\Utils\ResponseStrategies\ResponseWss($this),
      new \Kufer\Web\KuferTools\Utils\ResponseStrategies\ResponseWithToolSettings($this)
    );
  }
}

class JournalRequest
{
  public $knr;
  public $belegId;
  public $filter = false;
}

class JournalResponse extends \Kufer\Web\KuferTools\Basis\UseCase\ToolResponse
{
  public $journal;
  public $columns;
  public $summeSoll;
  public $summeHaben;
  public $nochOffen;
  public $saldoNegative;

  public $filter;
}