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


class DBWorkEMailXML extends DBWorkStrict
{
  
  public $select;

  public function __construct($pDBTyp = 1, $pNewInstance = false)
  {
    parent::__construct($pDBTyp, $pNewInstance);

    $this->select = 'id, ewahl, typ, name, vorname, strasse, ort, geschlecht, beruf, '
      . 'gebdatum, gebjahr, altersklasse, kontonr, blz, bankbez, kontoinh, rechadr1, '
      . 'rechadr2, rechadr3, rechadr4, rechadr5, rechadr6, erm_grund, erm_proz, bemerkung, '
      . 'tnr, status, statusinfo, eingelesen, bearbeiter, bearbdatum, zusatz, '
      . 'zusatz2, zusatz3, sessionid, sessiontime, geburtsort, staatsang, nation, bundesland, '
      . 'krit1, krit2, krit3, krit4, krit5, krit6, krit7, krit8, krit9, krit10, krit11, krit12, '
      . 'krit13, krit14, krit15, krit16, newsl_erlaubt, pers_nr, bic, iban, last_erteilt, wblogin, '
      . 'keinebankverbindung, name_titel, erziehungsbername, erziehungsbervorname, erziehungsbertnr, '
      . 'epay_tid, epay_tid_merchant, epay_type, epay_amount, epay_institution, epay_expiry, epay_cardholder, epay_bank, epay_blz,'
      . 'epay_bic, epay_iban, epay_kontoinhaber, epay_kontonr, epay_ordertime, epay_payerid, epay_feeamount, abteilung, '
      . 'sozarbeitgeber, payonline, arbamtnr, ausweisnr, mitgliednr, epay_state, epay_pendingreason, mandatam, kommprio, anm_quelle, spendenbetrag, '
      . $this->clob('newsl_bemerk') . ', ' . $this->clob('orgaeinh') . ', sozposition, arbeit, ausbildung, homeoffice'
      . ', dse_unterschrieben_datum, dse_unterschrieben_bearbeiter, dse_unterschrieben_weg, dse_widerrufen_datum, '
      . 'dse_widerrufen_bearbeiter, dse_widerrufen_weg, weitergabekommunikationfg, zustimmung_hygiene, zustimmung_indiv,'
      . 'zustimmung_fuehrung, rech_tnr, rech_name, rech_vorname, rech_zusatz1, rech_zusatz2, rech_zusatz3, rech_strasse, rech_ort, rech_dauerhaft, ustid, '
      . 'rech_email';
  }

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

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

  
  public function getCurrentRow($pRefreshCache = false)
  {
    $tmp = $this->getNewObject();
    $tmp->id = $this->getDatamodulValue('id');
    $tmp->ewahl = $this->getDatamodulValue('ewahl');
    $tmp->typ = $this->getDatamodulValue('typ');
    $tmp->name = $this->getDatamodulValue('name');
    $tmp->vorname = $this->getDatamodulValue('vorname');
    $tmp->strasse = $this->getDatamodulValue('strasse');
    $tmp->ort = $this->getDatamodulValue('ort');
    $tmp->geschlecht = $this->getDatamodulValue('geschlecht');
    $tmp->beruf = $this->getDatamodulValue('beruf');
    $tmp->gebdatum = $this->getDatamodulValue('gebdatum');
    $tmp->gebjahr = $this->getDatamodulValue('gebjahr');
    $tmp->altersklasse = $this->getDatamodulValue('altersklasse');
    $tmp->kontonr = $this->getDatamodulValue('kontonr');
    $tmp->blz = $this->getDatamodulValue('blz');
    $tmp->bankbez = $this->getDatamodulValue('bankbez');
    $tmp->kontoinh = $this->getDatamodulValue('kontoinh');
    $tmp->rechadr1 = $this->getDatamodulValue('rechadr1');
    $tmp->rechadr2 = $this->getDatamodulValue('rechadr2');
    $tmp->rechadr3 = $this->getDatamodulValue('rechadr3');
    $tmp->rechadr4 = $this->getDatamodulValue('rechadr4');
    $tmp->rechadr5 = $this->getDatamodulValue('rechadr5');
    $tmp->rechadr6 = $this->getDatamodulValue('rechadr6');
    $tmp->erm_grund = $this->getDatamodulValue('erm_grund');
    $tmp->erm_proz = $this->getDatamodulValue('erm_proz');
    $tmp->bemerkung = $this->getDatamodulValue('bemerkung');
    $tmp->tnr = $this->getDatamodulValue('tnr');
    $tmp->status = $this->getDatamodulValue('status');
    $tmp->statusinfo = $this->getDatamodulValue('statusinfo');
    $tmp->eingelesen = $this->getDatamodulValue('eingelesen');
    $tmp->bearbeiter = $this->getDatamodulValue('bearbeiter');
    $tmp->bearbdatum = $this->getDatamodulValue('bearbdatum');
    $tmp->zusatz = $this->getDatamodulValue('zusatz');
    $tmp->zusatz2 = $this->getDatamodulValue('zusatz2');
    $tmp->zusatz3 = $this->getDatamodulValue('zusatz3');
    $tmp->sessionid = $this->getDatamodulValue('sessionid');
    $tmp->sessiontime = $this->getDatamodulValue('sessiontime');
    $tmp->geburtsort = $this->getDatamodulValue('geburtsort');
    $tmp->staatsang = $this->getDatamodulValue('staatsang');
    $tmp->nation = $this->getDatamodulValue('nation');
    $tmp->bundesland = $this->getDatamodulValue('bundesland');
    $tmp->krit1 = $this->getDatamodulValue('krit1');
    $tmp->krit2 = $this->getDatamodulValue('krit2');
    $tmp->krit3 = $this->getDatamodulValue('krit3');
    $tmp->krit4 = $this->getDatamodulValue('krit4');
    $tmp->krit5 = $this->getDatamodulValue('krit5');
    $tmp->krit6 = $this->getDatamodulValue('krit6');
    $tmp->krit7 = $this->getDatamodulValue('krit7');
    $tmp->krit8 = $this->getDatamodulValue('krit8');
    $tmp->krit9 = $this->getDatamodulValue('krit9');
    $tmp->krit10 = $this->getDatamodulValue('krit10');
    $tmp->krit11 = $this->getDatamodulValue('krit11');
    $tmp->krit12 = $this->getDatamodulValue('krit12');
    $tmp->krit13 = $this->getDatamodulValue('krit13');
    $tmp->krit14 = $this->getDatamodulValue('krit14');
    $tmp->krit15 = $this->getDatamodulValue('krit15');
    $tmp->krit16 = $this->getDatamodulValue('krit16');
    $tmp->newsl_erlaubt = $this->getDatamodulValue('newsl_erlaubt');
    $tmp->newsl_bemerk = $this->getDatamodulValue('newsl_bemerk');
    $tmp->pers_nr = $this->getDatamodulValue('pers_nr');
    $tmp->bic = $this->getDatamodulValue('bic');
    $tmp->iban = $this->getDatamodulValue('iban');
    $tmp->mandatam = $this->getDatamodulValue('mandatam');
    $tmp->last_erteilt = $this->getDatamodulValue('last_erteilt');
    $tmp->wblogin = $this->getDatamodulValue('wblogin');
    $tmp->keinebankverbindung = $this->getDatamodulValue('keinebankverbindung');
    $tmp->name_titel = $this->getDatamodulValue('name_titel');
    $tmp->erziehungsbername = $this->getDatamodulValue('erziehungsbername');
    $tmp->erziehungsbervorname = $this->getDatamodulValue('erziehungsbervorname');
    $tmp->erziehungsbertnr = $this->getDatamodulValue('erziehungsbertnr');
    $tmp->epay_tid = $this->getDatamodulValue('epay_tid');
    $tmp->epay_tid_merchant = $this->getDatamodulValue('epay_tid_merchant');
    $tmp->epay_type = $this->getDatamodulValue('epay_type');
    $tmp->epay_ordertime = $this->getDatamodulValue('epay_ordertime');
    $tmp->epay_payerid = $this->getDatamodulValue('epay_payerid');
    $tmp->epay_institution = $this->getDatamodulValue('epay_institution');
    $tmp->epay_feeamount = $this->getDatamodulValue('epay_feeamount');
    $tmp->epay_amount = $this->getDatamodulValue('epay_amount');
    $tmp->epay_blz = $this->getDatamodulValue('epay_blz');
    $tmp->epay_bank = $this->getDatamodulValue('epay_bank');
    $tmp->epay_bic = $this->getDatamodulValue('epay_bic');
    $tmp->epay_iban = $this->getDatamodulValue('epay_iban');
    $tmp->epay_kontoinhaber = $this->getDatamodulValue('epay_kontonr');
    $tmp->epay_kontonr = $this->getDatamodulValue('epay_kontonr');
    $tmp->epay_expiry = $this->getDatamodulValue('epay_expiry');
    $tmp->epay_cardholder = $this->getDatamodulValue('epay_cardholder');
    $tmp->abteilung = $this->getDatamodulValue('abteilung');
    $tmp->orgaeinh = $this->getDatamodulValue('orgaeinh');
    $tmp->sozarbeitgeber = $this->getDatamodulValue('sozarbeitgeber');
    $tmp->payonline = $this->getDatamodulValue('payonline');
    $tmp->epay_state = $this->getDatamodulValue('epay_state');
    $tmp->epay_pendingReason = $this->getDatamodulValue('epay_pendingreason');
    $tmp->arbamtnr = $this->getDatamodulValue('arbamtnr');
    $tmp->ausweisnr = $this->getDatamodulValue('ausweisnr');
    $tmp->mitgliednr = $this->getDatamodulValue('mitgliednr');
    $tmp->kommprio = $this->getDatamodulValue('kommprio');
    $tmp->anm_quelle = $this->getDatamodulValue('anm_quelle');
    $tmp->spendenbetrag = $this->getDatamodulValue('spendenbetrag');
    $tmp->homeoffice = $this->getDatamodulValue('homeoffice');
    $tmp->sozposition = $this->getDatamodulValue('sozposition');

    $otherStandardFields = array('dse_unterschrieben_datum', 'dse_unterschrieben_bearbeiter', 'dse_unterschrieben_weg',
        'dse_widerrufen_datum', 'dse_widerrufen_bearbeiter', 'dse_widerrufen_weg', 'weitergabekommunikationfg', 'zustimmung_hygiene',
        'zustimmung_indiv', 'zustimmung_fuehrung', 'rech_tnr', 'rech_name', 'rech_vorname', 'rech_zusatz1', 'rech_zusatz2',
        'rech_zusatz3', 'rech_strasse', 'rech_ort', 'rech_dauerhaft', 'ustid', 'rech_email');
    foreach ($otherStandardFields as $varName) {
      $tmp->{$varName} = $this->getDatamodulValue($this->translateVarName($varName));
    }

    return $tmp;
  }

  
  public function canupdate($varname)
  {
    switch ($varname) { default: return DBWork::canupdate($varname);
      case 'arykommunikation': return false;

      case 'ausweisdat': return false;
      
      
      case 'arykennzeichen': return false;
      case 'arykwerb': return false;
      case 'aryattachments': return false;
    }
  }

  
  public function getSQLValue($fieldname, $datamodul)
  {
    if ($fieldname == 'erm_grund') {
      if ($datamodul->getVarValue($fieldname) == '') {
        return "'-1'";
      }
    } elseif ($fieldname == 'erm_proz') {
      
      if ($this->getSQLValue('erm_grund', $datamodul) == "'-1'") {
        return -1;
      } else {
        return intval($datamodul->getVarValue($fieldname));
      }
    }

    return parent::getSQLValue($fieldname, $datamodul);
  }

  
  public function getXMLMail($pId)
  {
    $id = $this->dboperation->getInt4Sql($pId);
    $this->dboperation->clear();
    $this->dboperation->query(
      "SELECT $this->select FROM emailxml WHERE id = $id"
    );

    return $this->getFirstRow();
  }

  
  public function insertXMLMail($xmlmail)
  {
    $xmlmail = $this->_prepareInsertXMLMail($xmlmail);
    $xmlmail->id = $this->insertRecord('emailxml', 'id', '', true);
    $this->saveChanging('emailxml', '', '', $xmlmail->id, 'id', 'I');
    $orgxmlmail = $this->getXMLMail($xmlmail->id);
    $this->updateObject($xmlmail, $orgxmlmail, 'emailxml', 'id', 'A');
    $this->setActionIDs();
    
    if ($this->verifyXMLMail($xmlmail)) {
      return $xmlmail->id;
    } else {
      return false;
    }
  }

  
  public function _prepareInsertXMLMail($pXmlEmail)
  {
    
    
    $pXmlEmail->orgaeinh = str_replace("\n", "\r\n", $pXmlEmail->orgaeinh);

    return $pXmlEmail;
  }

  
  public function verifyXMLMail($xmlmail)
  {
    
    $verifyObject = $this->getXMLMail($xmlmail->id);

    
    $noCheck = array('gebdatum', 'eingelesen', 'ausweisdat', 'bearbdatum', 'arykommunikation', 'erm_grund', 'erm_proz', 'arykennzeichen', 'arykwerb', 'aryattachments', 'epay_ordertime', 'epay_feeamount', 'epay_amount', 'erziehungsbertnr', 'kommprio', 'epay_processing_mode', 'epay_payment_date');
    
    
    
    
    
    for ($i = 0; $i < $xmlmail->getVarCount(); $i++) {
      
      $varname = $xmlmail->getVarName($i);

      
      if (!in_array($varname, $noCheck)) {
        
        
        if (!$this->checkEquality($xmlmail, $verifyObject, $varname)) {
          Logger::logg(__CLASS__ . " Fehler bei Variable $varname : (original)"
              . $xmlmail->getVarValue($varname) . '!='
              . $verifyObject->getVarValue($varname) . '(ausgelesen)', 100);

          return false;
        }
      }
    }

    return true;
  }

  
  public function getLastEntryByName($pName, $pVorname)
  {
    $sql = 'SELECT ' . $this->select . " FROM emailxml WHERE name ='$pName' AND vorname = '$pVorname' order by id desc";
    $this->dboperation->clear();
    $this->dboperation->query($sql);

    return $this->getFirstRow();
  }

  
  public function updateEmailXml($pEmailXml)
  {
    $orgEmailXml = $this->getXMLMail($pEmailXml->id);
    $this->updateObject($pEmailXml, $orgEmailXml, 'emailxml', 'id', 'U', false);
    $this->setActionIDs();
  }
}