<?php

namespace App\ImportedOrder\EntityMatch;

/**
 * The class is used for matching suppliers and also adds functionality for matching suppliers using the
 * imported orders associations.
 * @author Gabriel
 */
class Supplier extends \App\EntityMatch\Supplier
{

	/**
	 * Forces the overwrite of cleanMatches() which cleans up all matching except 'ordersExtSys'
	 * criterion (if any).
	 */
	use \App\ImportedOrder\EntityMatch\ExtSysCleanMatchesTrait;

	/**
	 * Sets the id of the supplier in external system.
	 * @param String $idEntityExtSys
	 * @param Int $idSystem - the external system id
	 */
	public function setIdInExtSys($idEntityExtSys, $idSystem)
	{
		// TODO rename ordersExtSys to externalSystem
		$this->setSearchCriterion('ordersExtSys', $idEntityExtSys, false, ['idSystem'=>$idSystem]);
	}


	/**
	 * Searches for matches using the imported order suppliers associations.
	 * @see \App\EntityMatch\Supplier::dispatchSearchRecords()
	 */
	protected function dispatchSearchRecords($field, $value, $useWildcard, Array $options=[])
	{

		if ($field=='ordersExtSys') {
			$matchedId = \App\Config\ImportedOrders_Matches::getInstance()->getSupplier($options['idSystem'], $value);
			if ($matchedId) {
				return $this->searchRecord_primaryKey($matchedId);
			} else {
				return null;
			}
		} else {
			return parent::dispatchSearchRecords($field, $value, $useWildcard, $options);
		}

	}


}
