<?php

namespace App\ImportedOrder;

class Office extends EntityAbstract
{

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

	/**
	 * The office id in the external system.
	 * @var String
	 */
	public $idSystem;


	/**
	 * The office iata code
	 * @var String
	 */
	public $iataCode; // [notInIoXML] de add in XML

	/**
	 * The office name.
	 * @var String
	 */
	public $name;


	/**
	 * (non-PHPdoc)
	 * @see \App\ImportedOrder\EntityAbstract::validateEntityFields()
	 */
	protected function validateEntityFields(\App\MessagesCollector $messagesCollector): void
	{

		if ($this->idTina and !is_numeric($this->idTina) or $this->idTina<0) {
			$messagesCollector->addInfo('The office TINA id is invalid - it should be a positive integer.');
		}

	}


	/**
	 * Gets the TINA matching office.
	 * @param Int $idExternalSystem
	 * @return Int - the id from TINA or 0 if no matches found
	 */
	public function getMatchedId($idExternalSystem)
	{

		$matchObj = new \App\ImportedOrder\EntityMatch\Office('intersect');
		if ($this->idTina) {
			$matchObj->setId($this->idTina);
		}
		if ($idExternalSystem and $this->idSystem) {
			$matchObj->setIdInExtSys($this->idSystem, $idExternalSystem);
		}
		if ($this->name) {
			$matchObj->setName($this->name, true);
		}
		$matchObj->search();
		if ($matchObj->getResultsCount()==1) {
			return $matchObj->getFirstMatchId();
		}

		return 0;

	}

}
