<?php

namespace App\ImportedDocument;

/**
 * This class contains the attributes and methods that are common for client invoices and supplier invoices
 * @author cristiana.matache
 */
class InvoiceAbstract extends EntityAbstract
{

	/**
	 * The invoice id from TINA
	 * @var Int
	 */
	public $idTina;

	/**
	 * The invoice number
	 * @var String
	 */
	public $number;

	/**
	 * The invoice date
	 * @var String
	 */
	public $date;

	/**
	 * The invoice due date
	 * @var String
	 */
	public $dueDate;

	/**
	 * The limited value when processing an imported document. Optional (if not set, TINA will consider the full invoice value).
	 * @var Float
	 */
	public $limitProcessingValue;

	/**
	 * The invoice currency
	 * @var String
	 */
	public $currency;


	/**
	 * {@inheritDoc}
	 * @see \App\ImportedDocument\EntityAbstract::validateEntityFields()
	 */
	protected function validateEntityFields(\App\MessagesCollector $messagesCollector)
	{
		// check date
		if ($this->date and !validDate($this->date)) {
			$messagesCollector->addInfo('The invoice date is not valid.');
		}

		// check dueDate
		if ($this->dueDate and !validDate($this->dueDate)) {
			$messagesCollector->addInfo('The invoice due date is not valid.');
		}

		// check currency
		if ($this->currency and !\App\Config\Currencies::getInstance()->check($this->currency)) {
			$messagesCollector->addWarning('The invoice currency is not valid.');
		}

		// check total value
		if (strlen((string)$this->limitProcessingValue) and !is_numeric($this->limitProcessingValue)) {
			$messagesCollector->addWarning('The invoice total value value is not valid.');
		}
		// check currency with value
		if (strlen((string)$this->limitProcessingValue) and !$this->currency) {
			$messagesCollector->addWarning('When indicating `limitProcessingValue` you must set the currency also.');
		}

	}

}