<?php
namespace App\ImportedDocument;

/**
 * The payment document class
 * This class contains specific attributes and methods for payment
 * @author cristiana.matache
 */

class Payment extends DocumentAbstract
{

	protected $docCategory = 'payment';

	/**
	 * the supplier from the payment document
	 * @var \App\ImportedDocument\Supplier
	 */
	protected $supplier;

	/**
	 * List with all supplier invoices from the payment document
	 * @var array
	 */
	protected $supplierInvoicesList=null;

	/**
	 * Set the payment supplier
	 * @param \App\ImportedDocument\Supplier $supplier
	 */
	public function setSupplier(\App\ImportedDocument\Supplier $supplier)
	{
		$this->supplier = $supplier;
	}

	/**
	 * Returns the payment supplier
	 */
	public function getSupplier()
	{
		return $this->supplier;
	}

	/**
	 * Add a new supplier invoice in the supplier invoices list
	 * @param \App\ImportedDocument\SupplierInvoice $invoice
	 */
	public function addSupplierInvoice(\App\ImportedDocument\SupplierInvoice $invoice)
	{
		$this->supplierInvoicesList[] = $invoice;
	}

	/**
	 * Returns the supplier invoices list
	 * @return array
	 */
	public function getSupplierInvoicesList()
	{
		return $this->supplierInvoicesList;

	}

	/**
	 * Reset the supplier invoices list from the document
	 */
	public function clearSupplierInvoicesList()
	{
		$this->supplierInvoicesList = [];
	}

	/**
	 * {@inheritDoc}
	 * @see \App\ImportedDocument\DocumentAbstract::validateEntityFields_Custom()
	 */
	protected function validateEntityFields_specificDoc(\App\MessagesCollector $messagesCollector) :void
	{

		// supplier
		if ($this->supplier) {
			$messagesCollector->appendMessages($this->supplier->check(), 'Supplier: %s');
		}

		// supplier invoices list
		if ($this->supplierInvoicesList) {
			foreach ($this->supplierInvoicesList as $suppInv) {
				$messagesCollector->appendMessages($suppInv->check(), 'Supplier invoice: %s');
				// check that the currency of the invoice is the same as the doc's when limitProcessingValue is set
				if ($suppInv->limitProcessingValue and  $suppInv->currency and $suppInv->currency!=$this->currency) {
					$messagesCollector->addWarning(sprintf('The supplier invoice currency (%s) is different than the document currency (%s).', $suppInv->currency, $this->currency));
				}
				// check that the currency of the invoice is the same as the doc's when limitProcessingValue is set
				if ($suppInv->limitProcessingValue) {
					if ( ($suppInv->limitProcessingValue>0 and $this->value<0) or ($suppInv->limitProcessingValue<0 and $this->value>0) ) {
						$messagesCollector->addWarning('The supplier invoice limit processing value must have the same sign as the document.');
					}
				}
			}
		}
	}



	/**
	 *
	 * {@inheritDoc}
	 * @see \App\ImportedDocument\DocumentAbstract::getMatchedDocumentType()
	 */
	public function getMatchedDocumentType($idExternalSystem)
	{

		if (!strlen($this->docType)) {
			return null;
		}

		$matchedDocType = \App\Config\ImportedDocuments\Matches::getInstance()->getDocType($idExternalSystem, $this->docType, $this->docCategory);
		if ($matchedDocType) {
			return $matchedDocType;
		}
		elseif (\App\Config\PaymentDocuments::getInstance()->isActive($this->docType)) {
			return $this->docType;
		}
		else {
			return null;
		}

	}

	/**
	 * array('type'=>'', 'id'=>'')
	 */
	public function getMatchedPartnerDetails()
	{

		$externalSystemId = \Entity\ImportedDocumentsSystem::getSystemIdByCode($this->externalSystemCode);
		$suppMatching = null;

		// corporate
		$suppObj = $this->getSupplier();
		if (!$suppObj) {
			$suppObj = new \App\ImportedDocument\Supplier();
		}
		$suppMatching = $suppObj->getMatchedId($externalSystemId, $this);

		if ($suppMatching) {
			return ['type'=>'supplier', 'id'=>$suppMatching];
		}

		return [];

	}
}