<?php
namespace App\ImportedDocument\Parser\Xml;

/**
 * This class will populate the payment document
 * @author cristiana.matache
 */

class Payment extends GeneralAbstract
{

	/**
	 * {@inheritDoc}
	 * @see \App\ImportedDocument\Parser\Xml\GeneralAbstract::createImportedDocument()
	 */
	public function populate(\App\ImportedDocument\DocumentAbstract $impPaymentDoc)
	{

		if (!$impPaymentDoc instanceof \App\ImportedDocument\Payment) {
			throw new \Exception("The imported document object isn't valid - it must be 'Payment'!");
		}

		//general information
		$impPaymentDoc->operation 			= (String)$this->xml['operation'];
		$impPaymentDoc->externalSystemCode 	= (String)$this->xml['system'];
		$impPaymentDoc->idDocumentSystem 	= (String)$this->xml['documentId'];
		$impPaymentDoc->systemStatus 		= (String)$this->xml['operation'];
		if ($this->xml->docNumber) {
			$impPaymentDoc->docNumber 		= (String)$this->xml->docNumber;
		}
		if ($this->xml->docType) {
			$impPaymentDoc->docType 		= (String)$this->xml->docType;
		}
		if ($this->xml->docDate) {
			$impPaymentDoc->docDate 		= (String)$this->xml->docDate;
		}
		if ($this->xml->value) {
			$impPaymentDoc->value 			= (Float)$this->xml->value;
		}
		if ($this->xml->currency) {
			$impPaymentDoc->currency 		= (String)$this->xml->currency;
		}
		if ($this->xml->remarks) {
			$impPaymentDoc->remarks 		= (String)$this->xml->remarks;
		}
		if ($this->xml->comments) {
			$impPaymentDoc->comments		= (String)$this->xml->comments;
		}
		if ($this->xml->explanations) {
			$impPaymentDoc->explanations	= (String)$this->xml->explanations;
		}

		if ($this->xml['forceUpdate']) {
			$impPaymentDoc->forceOperation 	= (Int)$this->xml['forceUpdate'];
		}

		// responsible user
		if ($this->xml['responsibleUser']) {
			$respUserObj = new \App\ImportedDocument\User();
			$respUserObj->idSystem = (String)$this->xml['responsibleUser'];
			$impPaymentDoc->setResponsibleUser($respUserObj);
		}

		if ($this->xml['responsibleOffice']) {
			$respOfficeObj = new \App\ImportedDocument\Office();
			$respOfficeObj->idSystem = (String)$this->xml['responsibleUser'];
			$impPaymentDoc->setResponsibleOffice($respOfficeObj);
		}

		if ($this->xml->bankAccount) {
			$bankAccObj = new \App\ImportedDocument\AgencyBankAccount();
			if ($this->xml->bankAccount) {
				$bankAccObj->number = (String)$this->xml->bankAccount;
			}
			if ($this->xml->bankAccount['tinaId']) {
				$bankAccObj->idTina = (String)$this->xml->bankAccount['tinaId'];
			}
			$impPaymentDoc->setAgencyBankAccount($bankAccObj);
		}

		// populate the supplier
		if ($this->xml->supplier) {
			$supplier = new \App\ImportedDocument\Supplier();

			if ($this->xml->supplier['id']) {
				$supplier->idSystem 	= (String)$this->xml->supplier['id'];
			}

			if ($this->xml->supplier['tinaId']) {
				$supplier->idTina 		= (Int)$this->xml->supplier['tinaId'];
			}

			if ($this->xml->supplier->company) {
				$supplier->name			= (String)$this->xml->supplier->company;
			}

			if ($this->xml->supplier->fiscalCode) {
				$supplier->fiscalCode	= (String)$this->xml->supplier->fiscalCode;
			}

			if ($this->xml->supplier->zipCode) {
				$supplier->zipCode		= (String)$this->xml->supplier->zipCode;
			}

			if ($this->xml->supplier->bankAccount) {
				$partnerBankAccount = new \App\ImportedDocument\SupplierBankAccount();
				if ($this->xml->supplier->bankAccount) {
					$partnerBankAccount->number = (String)$this->xml->supplier->bankAccount;
				}
				if ($this->xml->supplier->bankAccount['tinaId']) {
					$partnerBankAccount->idTina = (String)$this->xml->supplier->bankAccount['tinaId'];
				}
				$supplier->setBankAccount($partnerBankAccount);
			}

			$impPaymentDoc->setSupplier($supplier);
		}

		if ($this->xml->invoices) {

			foreach ($this->xml->invoices->invoice as $suppInvTag) {
				$suppInvObj = new \App\ImportedDocument\SupplierInvoice();

				$suppInvObj->idTina 				= (String)$suppInvTag['tinaId'];
				$suppInvObj->date 					= (String)$suppInvTag->date;
				$suppInvObj->number 				= (String)$suppInvTag->number;
				$suppInvObj->dueDate	 			= (String)$suppInvTag->dueDate;
				if ($suppInvTag->limitProcessingValue and strlen((String)$suppInvTag->limitProcessingValue)) {
					$suppInvObj->limitProcessingValue = (Float)$suppInvTag->limitProcessingValue;
				}
				$suppInvObj->currency				= (String)$suppInvTag->currency;

				$impPaymentDoc->addSupplierInvoice($suppInvObj);
			}
		}

	}

}