<?php

namespace App\EntityMatch;

/**
 * The class manages the matching of supplier products.
 * @author Gabriel
 */
class SupplierProduct extends \App\EntityMatch\BaseAbstract
{

	/**
	 * The export fields for supplier products.
	 * @var array
	 */
	protected $exportFields = ['productName', 'serviceCode', 'destinationCountryCode', 'destinationCityId'];

	/**
	 * Extra filters - deleted and duplicated suppliers are not allwed.
	 * @var String
	 */
	protected $matchingFilteringSQL = 'AND ent.deleted=0 AND ent.idRealSupplierProduct=0';

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

	/**
	 * The service code filtering used when matching supplier products.
	 * @var String
	 */
	private $serviceCodeFilter;

	/**
	 * The country code filtering used when matching supplier products.
	 * @var String
	 */
	private $countryCodeFilter;

	/**
	 * The city id filtering used when matching supplier products.
	 * @var String
	 */
	private $cityIdFilter;

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

	/**
	 * Sets a search by supplier product product name.
	 * @param String $value - the supplier product name.
	 * @param Boolean $useWildcard - uses wildcard for this search or not.
	 */
	public function setProductName($value, $useWildcard=true)
	{
		$this->setSearchCriterion('productName', $value, $useWildcard);
	}

	/**
	 * Sets the filter by service code.
	 * @param String $value - the service code.
	 */
	public function filterByServiceCode($value)
	{
		$this->serviceCodeFilter = $value;
	}

	/**
	 * Sets the filter by country code.
	 * @param String $value - the country code.
	 */
	public function filterByCountryCode($value)
	{
		$this->countryCodeFilter = $value;
	}

	/**
	 * Sets the filter by city id.
	 * @param Int $value - the city id.
	 */
	public function filterByCityId($value)
	{
		$this->cityIdFilter = $value;
	}


	/**
	 * (non-PHPdoc)
	 * @see \App\EntityMatch\BaseAbstract::cleanMatches()
	 */
	protected function cleanMatches()
	{

		// FILTER BULK MATCHES (by country code) --------------------------------------------------
		if ($this->serviceCodeFilter or $this->countryCodeFilter or $this->cityIdFilter) {
			foreach($this->bulkMatches as $idx=>$matchesList)
			{
				foreach($matchesList as $entCode=>$entDet)
				{

					// filter by service code
					if ($this->serviceCodeFilter and $entDet['serviceCode']!=$this->serviceCodeFilter)
					{
						unset($this->bulkMatches[$idx][$entCode]);
					}

					// filter by country code
					if ($this->countryCodeFilter and $entDet['destinationCountryCode'] and $entDet['destinationCountryCode']!=$this->countryCodeFilter)
					{
						unset($this->bulkMatches[$idx][$entCode]);
					}

					// filter by city id
					if ($this->cityIdFilter and $entDet['destinationCityId'] and $entDet['destinationCityId']!=$this->cityIdFilter)
					{
						unset($this->bulkMatches[$idx][$entCode]);
					}

				}
			}

			// eliminate empty values
			$this->bulkMatches = array_filter($this->bulkMatches);

		}

	}

}

