<?php
namespace Modules\Documents\ImportedDocuments\ImportStatements\Model;

abstract class Camt052Abstract extends GeneralAbstract
{
	protected $externalSystemCode;

	/**
	 * 
	 * {@inheritDoc}
	 * @see \Modules\Documents\ImportedDocuments\ImportStatements\Model\GeneralAbstract::validateFormat()
	 */
	public function validateFormat() : void
	{
		$xml = simplexml_load_string($this->statementContents);
		
		if (!$xml) {
			throw new \Modules\Documents\ImportedDocuments\ImportStatements\Model\Exception("The file format is not XML.");
		}
		
		if (!$xml->BkToCstmrStmt) {
			throw new \Modules\Documents\ImportedDocuments\ImportStatements\Model\Exception("The file format is different than CAMT .052.");
		}
		
		if (!$xml->BkToCstmrStmt->Stmt) {
			throw new \Modules\Documents\ImportedDocuments\ImportStatements\Model\Exception("The statement is missing from the file.");
		}
		
		if (!$xml->BkToCstmrStmt->Stmt->Ntry) {
			throw new \Modules\Documents\ImportedDocuments\ImportStatements\Model\Exception("No entries found in the statement.");
		}
	}
	
	/**
	 * 
	 * {@inheritDoc}
	 * @see \Modules\Documents\ImportedDocuments\ImportStatements\Model\GeneralAbstract::createImportedDocumentObjects()
	 */
	protected function createImportedDocumentObjects() : void
	{
		$xml = new \SimpleXMLElement($this->statementContents);

		foreach ($xml->BkToCstmrStmt->Stmt->Ntry as $entry) {
			
			if ((string)$entry->CdtDbtInd == 'CRDT') {
				$impDocObj = new \App\ImportedDocument\Cashing();
				
				// check if corporate
				$clientName = (string)$entry->NtryDtls->TxDtls->RltdPties->Dbtr->Pty->Nm;
				
				if ($this->getClientType($clientName) == 'legalEntity') {
					$corporateObj = new \App\ImportedDocument\Corporate();
					$corporateObj->name = $clientName;
					$corporateObj->idSystem = $clientName;
					$impDocObj->setCorporate($corporateObj);
				}
				elseif ($this->getClientType($clientName) == 'individual') {
					$customerObj = new \App\ImportedDocument\Customer();
					list($lastName, $firstName) = explode(' ', $clientName, 2);
					$customerObj->firstName = $firstName;
					$customerObj->lastName = $lastName;
					
					$customerObj->idSystem = $clientName;
					$impDocObj->setCustomer($customerObj);
				}
				
				$impDocObj->explanations = (string)$entry->NtryDtls->TxDtls->RltdPties->Dbtr->Pty->Nm." / IBAN: ".(string)$entry->NtryDtls->TxDtls->RltdPties->DbtrAcct->Id->IBAN;
				
			}
			elseif ((string)$entry->CdtDbtInd == 'DBIT') {
				$impDocObj = new \App\ImportedDocument\Payment();
				
				$supplier = new \App\ImportedDocument\Supplier();
				$supplier->idSystem = (string)$entry->NtryDtls->TxDtls->RltdPties->Cdtr->Pty->Nm;
				$impDocObj->setSupplier($supplier);
				
				$impDocObj->explanations = (string)$entry->NtryDtls->TxDtls->RltdPties->Cdtr->Pty->Nm." / IBAN: ".(string)$entry->NtryDtls->TxDtls->RltdPties->CdtrAcct->Id->IBAN;
				
			}
			
			$impDocObj->operation = 'add';
			$docNumber = (string)$entry->NtryDtls->TxDtls->Refs->AcctSvcrRef;
			
			$impDocObj->externalSystemCode 	= $this->externalSystemCode;
			$impDocObj->idDocumentSystem 	= uniqid();
			$impDocObj->docNumber 			= $docNumber;
			$impDocObj->docType				= 'paymentOrder';
			$impDocObj->docDate 			= (string)$entry->BookgDt->Dt;
			$impDocObj->value 				= (float)$entry->Amt;
			$impDocObj->currency 			= (string)$entry->Amt['Ccy'];
			
			$this->importedDocObjList []= $impDocObj;
		}
	}
}