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


class DBWorkWebEpaystate extends DBWorkGenerated
{
  protected static $_cache_Epaystate = array();

  public function __construct($pDBTyp = 1, $pNewInstance = false)
  {
    parent::__construct($pDBTyp, $pNewInstance);
    $this->select = 'tid, payment_state, creation_date, attempts, id ';
    $this->_dataModulFields = array('tid', 'payment_state', 'creation_date', 'attempts', 'id');
  }

  public function getTableName(): string
  {
    return 'web_epaystate';
  }

  
  public function insertWebEpaystate($pWebEpaystate)
  {
    $epaystate = $pWebEpaystate;
    $epaystate->id = $this->insertRecord('web_epaystate', 'id', array('tid', 'payment_state'));
    $this->saveChanging('web_epaystate', '', '', $epaystate->id, 'id', 'I', true);
    $orgepaystate = $this->getEpaystate($epaystate->id);
    $this->updateObject($epaystate, $orgepaystate, 'web_epaystate', 'id', 'U', false, true);
    $this->setActionIDs();

    return $epaystate->id;
  }

  
  public function updateState($pTid, $pState)
  {
    $sql = 'UPDATE web_epaystate'
           . ' SET payment_state = \'' . $pState . '\''
           . ' WHERE tid =\'' . $pTid . '\'';
    $this->dboperation->clear();
    $this->dboperation->query($sql);
  }

  
  public function insertErrorProcessed($pTid)
  {
    $dtops = new DateTimeOperations();
    $sql = 'UPDATE web_epaystate'
           . ' SET error_processed = ' . $dtops->getFormateDateTime4ServerNow()
           . ' WHERE tid =\'' . $pTid . '\'';
    $this->dboperation->clear();
    $this->dboperation->query($sql);
  }

  
  public function getNewObject()
  {
    return new WebEpaystate();
  }

  
  public function getEpaystate($pWebEpaystate_Id, $pRefreshCache = false)
  {
    $epaystate_id = $this->dboperation->getInt4Sql($pWebEpaystate_Id);
    if (!is_numeric($epaystate_id)) {
      return $this->getNewObject();
    }
    if ($pRefreshCache || !array_key_exists($epaystate_id, self::$_cache_Epaystate)) {
      $this->dboperation->clear();
      $this->dboperation->query('SELECT ' . $this->select . ' FROM web_epaystate es WHERE es.id=' . $epaystate_id);
    }
    self::$_cache_Epaystate[$epaystate_id] = $this->getFirstRow($pRefreshCache);
    
    return unserialize(serialize(self::$_cache_Epaystate[$epaystate_id]));
  }

  

  
  public function getEpaystateByTransactionId($pTid)
  {
    $sql = 'SELECT ' . $this->select
           . ' FROM web_epaystate es '
           . 'WHERE es.tid =\'' . $pTid . '\'';
    $this->dboperation->clear();
    $this->dboperation->query($sql);

    return $this->getFirstRow();
  }

  
  public function incrementAttempts($pTid)
  {
    $sql = 'UPDATE web_epaystate'
           . ' SET attempts = ' . $this->dboperation->dbparser->isNull('attempts', 0) . ' + 1'
           . ' WHERE tid =\'' . $pTid . '\'';
    $this->dboperation->clear();
    $this->dboperation->query($sql);
  }

  
  public function clearOldEntries()
  {
    $sql = 'DELETE '
      . 'FROM web_epaystate '
      . 'WHERE creation_date <= ' . $this->datetimeop->getFormatDateTime4Server($this->datetimeop->getDateDaysBefore($this->datetimeop->getDateNow(), 15)); 
    $this->dboperation->clear();
    $this->dboperation->query($sql);

    return $this->getFirstRow();
  }
}