<?php
namespace App\ImportedDocument\EntityMatch;

/**
 * The class is used for matching client invoices
 * @author cristiana.matache
 */

class ClientInvoice extends \App\EntityMatch\BaseAbstract
{

	/**
	 * The export fields for client invoices
	 * @var array
	 */
	protected $exportFields = ['invoiceNumber', 'invoiceDate', 'currency', 'temporary', 'cashedStatus', 'voidTime', 'issueTime', 'printTotal', 'idCorporate', 'idCustomer'];

	/**
	 * The class name which will instantiate the Entity - used for primaryKey search.
	 * @var String
	 */
	protected $entityClassName = '\Entity\Invoice';

	/**
	 * (non-PHPdoc)
	 * @see \App\EntityMatch\BaseAbstract::setTableName()
	 */
	protected function setTableName()
	{
		global $DBTReservationServiceInvoices;
		$this->tableName = $DBTReservationServiceInvoices;
	}

	/**
	 * Set the Tina id criterion
	 * @param Int $value
	 */
	public function setTinaId($value)
	{
		$this->setSearchCriterion('id', $value, false);
	}

	/**
	 * Set the invoice number and date criterion
	 * @param String $number
	 * @param String $date
	 */
	public function setInvoiceNumberDate($invoiceNumber, $invoiceDate)
	{
		$this->setSearchCriterion('invoiceNumberAndDate', $invoiceNumber, false, ['invoiceDate'=>$invoiceDate]);
	}

	/**
	 *
	 * {@inheritDoc}
	 * @see \App\EntityMatch\BaseAbstract::dispatchSearchRecords()
	 */
	protected function dispatchSearchRecords($field, $value, $useWildcard, Array $options = [])
	{
		if ($field=='invoiceNumberAndDate') {
			return $this->searchRecords_invoiceNumberAndDate($value, $options['invoiceDate']);
		} else {
			return parent::dispatchSearchRecords($field, $value, $useWildcard, $options);
		}
	}

	/**
	 * This method search the client invoices by number/date
	 */
	protected function searchRecords_invoiceNumberAndDate($invoiceNumber, $invoiceDate)
	{

		// check that invoice number is numeric
		if (!is_numeric($invoiceNumber) or $invoiceNumber<=0) {
			return [];
		}

		// determine the cache key and check if it already exists
		if ($this->useCacheinSqls) {
			$cacheKey = 'EntityMatch-ClientInvoice-invoiceNumberAndDate-'.$invoiceDate.'/'.$invoiceNumber;
			$cachedMatches = \Engine\Cache\General::getInstance(self::CACHE_TYPE)->get($cacheKey);
			// if something cached - return the cached result
			if ($cachedMatches!==null) {
				return $cachedMatches;
			}
		}

		// no cache found - manually searches the db for invoice by number and date
		$return = [];

		$entityList = new \App\EntityList\Invoices();
		$entityList->applyUserRestriction(false);
		$entityList->setColumns($this->exportFields);
		$entityList->setFilter('invoiceNumber', '=', $invoiceNumber);
		$entityList->setFilter('invoiceDate', 'LIKE', $invoiceDate);
		$entityList->setLimit($this->limitResultsPerCriterion);
		$records = $entityList->exportRecords();
		// groups the output records by id
		foreach ($records as $row) {
			$return[$row[$this->tablePrimaryKey]] = $row;
		}

		// store the records in cache
		if ($this->useCacheinSqls) {
			\Engine\Cache\General::getInstance(self::CACHE_TYPE)->set($cacheKey, $return, $this->cacheTtl);
		}

		return $return;
	}
}