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

class ValidationAPI
{
  
  private static function isValidVar4Bankdaten($pKontoArt, $pKontoWert, $pBankCodeArt, $pBankCodeWert, $pBankCodeMandatory = true)
  {
    $validation = ValidierungBasis::getInstance();
    if ($pBankCodeWert == '') {
      if ($pBankCodeMandatory) {
        return false;
      }
    } elseif ($validation->isValidVar($pBankCodeArt, $pBankCodeWert) !== true) {
      return false;
    }

    if ($pKontoWert == '') {
      return false;
    } elseif ($validation->isValidVar($pKontoArt, $pKontoWert, array($pBankCodeArt => $pBankCodeWert)) !== true) {
      return false;
    }

    return true;
  }

  public static function areIBANAndBicValid(string $iban, string $bic): bool
  {
    return self::isValidVar4Bankdaten('iban', $iban, 'bic', $bic, false);
  }

  
  public static function validateSepa($pIban, $pBic, $pTnr)
  {
    $sepaValid = self::isValidVar4Bankdaten('iban', $pIban, 'bic', $pBic, false);

    
    $hasMandat = false;
    if ($sepaValid) {
      $dbwStammBv = new DBWorkStammBv(1, true);
      $stammBv = $dbwStammBv->getStammBvByTnrAndIban($pTnr, $pIban);
      $hasMandat = \StammBvAPI::checkSepaMandatFromTn($stammBv);
    }

    return array('isKontoValid' => $sepaValid, 'hasMandat' => $hasMandat);
  }

  
  public static function validateDta($pKonto, $pBlz)
  {
    $dtaValid = self::isValidVar4Bankdaten('konto', $pKonto, 'blz', $pBlz);

    return array('isKontoValid' => $dtaValid);
  }
}