<?php

namespace App\ImportedOrder\PopulateEntity;

/**
 * The class is used for populating a supplier product entity object with data from an imported supplier product (from imported order).
 * @author Gabriel
 */
class SupplierProduct extends GeneralAbstract
{

	/**
	 * The imported entity (service); this is redundant, it's added only for the IDE autocomplete to work.
	 * @var \App\ImportedOrder\SupplierProduct
	 */
	protected $importedEntity;

	/**
	 * The imported entity class name.
	 * @var String
	 */
	protected $importedEntityClass = '\App\ImportedOrder\SupplierProduct';

	/**
	 * The populating entity class name.
	 * @var String
	 */
	protected $populatingEntityClass = '\Entity\SupplierProduct';


	/**
	 * (non-PHPdoc)
	 * @see \App\ImportedOrder\PopulateEntity\GeneralAbstract::populate()
	 */
	public function populate()
	{

		// match the service code
		if ($this->populatingEntity->getOperation()=='insert') {
			if ($this->importedEntity->serviceType) {
				$matchedServiceCode = $this->importedEntity->serviceType->getMatchedId($this->idExternalSystem);
				if ($matchedServiceCode) {
					$this->populatingEntity->serviceCode = $matchedServiceCode;
				}
			}
		}
		// attach the product name
		if ($this->importedEntity->name!==null) {
			$this->populatingEntity->productName =		htmlClean($this->importedEntity->name);
		}
		// group name
		if ($this->importedEntity->groupName!==null) {
			$this->populatingEntity->chainCode =		$this->importedEntity->groupName;
		}
		// attach the category
		if ($this->importedEntity->categoryCode!==null) {
			$this->populatingEntity->category =		$this->importedEntity->categoryCode;
		}
		// address
		if ($this->importedEntity->address!==null) {
			$this->populatingEntity->address =			htmlClean($this->importedEntity->address);
		}
		// contact details
		if ($this->importedEntity->telephone!==null) {
			$this->populatingEntity->telephone =		$this->importedEntity->telephone;
		}
		if ($this->importedEntity->fax!==null) {
			$this->populatingEntity->fax =				$this->importedEntity->fax;
		}
		if ($this->importedEntity->email!==null) {
			$this->populatingEntity->email =			$this->importedEntity->email;
		}
		// match place
		if ($this->importedEntity->place) {
			$importedStation = $this->importedEntity->place->station;
			if ($importedStation) {
				$importedCity = $importedStation->city;
				if ($importedCity) {
					// the city was matched
					$matchedCityId = $importedCity->getMatchedId($this->idExternalSystem);
					if ($matchedCityId) {
						$this->populatingEntity->destinationCityId = $matchedCityId;
						$this->populatingEntity->destinationCountryCode = \Entity\City::select($matchedCityId)->countryCode;
					}
					// no city matching - try country match
					else {
						$importedCountry = $importedCity->country;
						if ($importedCountry) {
							$matchedCountryCode = $importedCountry->getMatchedId($this->idExternalSystem);
							if ($matchedCountryCode) {
								$this->populatingEntity->destinationCityId = 0;
								$this->populatingEntity->destinationCountryCode = $matchedCountryCode;
							}
						}
					}
				}

			}
		}

	}

}