# ValidateDocument

# Pflichtfeldprüfung - Entweder Oder

Hin und wieder reicht die einfache Pflichtfeldprüfung nicht aus z.B. wenn eines von zwei Feldern gefüllt sein muss. Für diesen Fall kann unten angegebene Funktion im UserExit ValidateDocument aufgerufen werden.

Die letzten beiden Parameter der Funktion müssen den technischen Feldnamen der zu prüfenden Feldern entsprechen.  
**Beispiel:**

```PHP
checkEitherOrField($db, $fieldList, $logger, 'VatId', 'TaxId');
```

Hier die Funktion, die als Entweder-Order Prüfung genutzt werden kann:

```PHP
function checkEitherOrField(PDO $db, xDocumentFieldCollection $fieldList, Logger $logger, string $firstFieldName, string $secondFieldName){
	
	$firstField = $fieldList->getDocumentFieldByName($firstFieldName);
	$secondField = $fieldList->getDocumentFieldByName($secondFieldName);
	
	if ($firstField == null) {
        $logger->error("Field with name '".$firstFieldName."' does not exist");
		return false;
    }
	
	if ($secondField == null) {
        $logger->error("Field with name '".$secondFieldName."' does not exist");
		return false;
    }
	
	if ($firstField->getValue()->value == "" and $secondField->getValue()->value == "") {
		$firstField->state = "ERROR";
        $firstField->errortext = __('The field %s may not be empty!', $firstField->description);
        $fieldList->updateField($firstField);
		
		$secondField->state = "ERROR";
        $secondField->errortext = __('The field %s may not be empty!', $secondField->description);
        $fieldList->updateField($secondField);
	} else {
		$firstField->state = "";
        $firstField->errortext = __("The field is valid!");
        $fieldList->updateField($firstField);
		
		$secondField->state = "";
        $secondField->errortext = __("The field is valid!");
        $fieldList->updateField($secondField);
	}
}

```

# Documents Mappen-Id ermitteln

Wenn es nötig ist, kann eine Documents Mappen-Id ermittelt werden um eine bestimmte Mappe in Documents zu aktualisieren, statt eine neue Mappe anzulegen. Um die Documents Mappen-Id zu ermitteln, kann eine FileResultSet abgefragt werden.  
Hier ein Besipiel für die Funktion die Im UserExit ValidateDocument hinzugefügt und an entsprechnder Stelle aufgerufen werden kann:

```PHP
/**
 * @param PDO $db
 * @param xDocumentFieldCollection $fieldList
 * @param Logger $logger
 */
function getDocumentsFileId(PDO $db, xDocumentFieldCollection $fieldList, Logger $logger)
{
    try {
        $otris = new \App\Squeeze\Connectors\Otris\OtrisSoapClient('https://documents.soap.server/', 'username', 'password', 'principal');
        $fileTypes = array('Mailroom');
        $archives = array();
        $fields = array('MailroomID','DocumentType');
        $filter = "MailroomID='MAIL21001333'";
        $fileResultSet = $otris->getFileResultSet($fileTypes, $archives, $fields, null, $filter);

        // It is important to logout because Otris only allows 3 sessions
        $otris->logout();

        // Check the result if it has exactly 1 fileId
        if(count($fileResultSet) === 0) {
            throw new Exception('Documents Mappe konnte nicht gefunden werden');
        } elseif (count($fileResultSet) > 1) {
            throw new Exception('Documents liefert mehr als eine Mappen-Id');
        }

        // If just one fileId has been found set the field value and state
        $resultField = $fieldList->getDocumentFieldByName("DocumentReference");
        $resultField->getValue()->value = $fileResultSet[0]['fileId'];
        $resultField->getValue()->text = $fileResultSet[0]['fileId'];
        $resultField->state = "OK";
        $resultField->errortext = __("The field is valid!");
        $fieldList->updateField($resultField);

    } catch (Exception $e) {
        $resultField = $fieldList->getDocumentFieldByName("DocumentReference");
        $resultField->getValue()->value = '';
        $resultField->getValue()->text = '';
        $resultField->state = 'ERROR';
        $resultField->errortext = $e->getMessage();
        $fieldList->updateField($resultField);
    }
}
```