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


class DBWorkEpayNotify extends DBWorkGenerated
{
  public function __construct($pDBTyp = 1, $pNewInstance = false)
  {
    parent::__construct($pDBTyp, $pNewInstance);
    $this->select = 'id, processing_status, initial_step_status, final_step_status, name, vorname, strasse, ort, zusatz, zusatz2, payonline, epay_tid, epay_state, epay_amount, epay_type, epay_institution, epay_expiry, epay_cardholder, epay_blz, epay_bank, epay_bic, epay_iban, epay_kontoinhaber, epay_kontonr, epay_ordertime, epay_payerid, epay_feeamount, epay_pendingreason, epay_tid_merchant, epay_statechange, epay_payment_date, lock_id, erfasstvon, erfasstam, bearbeiter, bearbdatum, epay_processing_mode, epay_order_id, epay_capture_id, epay_authorizationid';
    $this->_dataModulFields = array('name', 'vorname', 'strasse', 'ort', 'zusatz', 'zusatz2', 'payonline', 'epay_tid', 'epay_state', 'epay_type', 'epay_institution', 'epay_expiry', 'epay_cardholder', 'epay_blz', 'epay_bank', 'epay_bic', 'epay_iban', 'epay_kontoinhaber', 'epay_kontonr', 'epay_payerid', 'epay_pendingreason', 'epay_tid_merchant', 'epay_statechange', 'lock_id', 'erfasstvon', 'bearbeiter', 'epay_ordertime', 'epay_payment_date', 'erfasstam', 'bearbdatum', 'processing_status', 'initial_step_status', 'final_step_status', 'epay_processing_mode', 'epay_amount', 'epay_feeamount', 'id', 'epay_order_id', 'epay_capture_id', 'epay_authorizationid');
  }

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

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

  public function getById($id)
  {
    $qb = $this->getSelectQueryBuilder();
    $sql = $qb->select($this->select)
              ->from($this->getTableName())
              ->where('id = :id:')
              ->paramString('id', $id)
              ->getSQL();
    $this->dboperation->clear();
    $this->dboperation->query($sql);

    return $this->getFirstRow();
  }

  public function getEpayNotifyByMtId($mtid, $tid = '')
  {
    $qb = $this->getSelectQueryBuilder();
    $sql = $qb->select($this->select)
              ->from($this->getTableName())
              ->where('(epay_tid_merchant != \'\' and epay_tid_merchant = :mtid:) or (epay_tid != \'\' and epay_tid = :tid:)')
              ->paramString('mtid', $mtid)
              ->paramString('tid', $tid)
              ->getSQL();
    $this->dboperation->clear();
    $this->dboperation->query($sql);

    return $this->getFirstRow();
  }

  public function insertEpayNotify($epayNotify, $pUserId)
  {
    $en = new EpayNotify();
    $en->id = $this->insertRecord('epay_notify', 'id', 'epay_tid', false);
    $en = $this->getById($en->id);
    $this->updateObject($epayNotify, $en, $this->getTableName(), 'id', 'A', true);
    $this->setActionIDs();

    return $en->id;
  }

  public function updateEpayNotify($epayNotify)
  {
    $orig = $this->getById($epayNotify->id);
    $this->updateObject($epayNotify, $orig, $this->getTableName(), 'id', 'A', true);
  }

  public function insertRecord($pTablename, $pIdField, $pDummyfield, $pGentableexists = false)
  {
    $this->dboperation->clear();
    $this->dboperation->execute('
      INSERT INTO ' . $this->getTableName() . " (processing_status, initial_step_status, final_step_status,
      payonline, epay_tid, epay_state, epay_amount, erfasstvon)
      VALUES (0, 0, 0, '0', '', '', 0, '')");

    return $this->dboperation->getLastID($this->getTableName());
  }
}