<?php
namespace App\EntityClone;

/**
 * this class clone populate an empty service entity with information from another service
 * @author dcs
 *
 */

class Service extends \App\EntityClone\BaseAbstract
{

	/**
	 * (non-PHPdoc)
	 * @see \App\EntityClone\BaseAbstract::attachFields()
	 */
	protected function attachFields()
	{
		$CFG = \Engine\Loader_Cfg::getInstance()->getGlobal();

		$entityOld = \Entity\Service::select($this->id);

		// ADD MAIN SERVICE DETAILS ///////////////////////////////////////////////////////////////
		$this->entityNew->idReservation=			$entityOld->idOrder;
		$this->entityNew->serviceCode=				$entityOld->serviceCode;
		if ($entityOld->serviceCode == $CFG['travel']['otherServiceCode']) {
			$this->entityNew->serviceName=			$entityOld->serviceName;
		}

		// the services attributes will be assigned to the new service checking the configs
		$servFld = \App\Config\Services_Fields::getInstance()->get($entityOld->serviceCode);

		if ($entityOld->categoryCode and $servFld['category']['type']!='invisible') {
			$this->entityNew->category=				$entityOld->categoryCode;
		}
		if ($entityOld->productTypeCode and $servFld['productType']['type']!='invisible') {
			$this->entityNew->productType=			$entityOld->productTypeCode;
		}

		if ($servFld['productMultiplier']['type']!='invisible') {
			$multiplierGenAttr = \App\Config\Services_GeneralAttributes::getInstance()->getServiceMultiplier($entityOld->serviceCode);

			/* THESE comments may be ignored: (don't know how to reproduce the problem mentioned here)
			// if the multiplier is set as list and it isn't mandatory, the user can left the multiplier empty
			// in this case, Tina stores 1 at multiplier because it is necesary for offer
			// => the multiplier is array(''=>1)
			// we will check if the multiplier is list and the array isn't array(''=>1)
			*/

			if ($multiplierGenAttr['type']=='list' and is_array($entityOld->multiplierCode)) {
				$this->entityNew->multiplier = $entityOld->multiplierCode;
			}
			// if the multiplier isn't list, we will check only if the multiplier is set
			elseif ($multiplierGenAttr['type']=='value' and is_numeric($entityOld->multiplierCode)) {
				$this->entityNew->multiplier = $entityOld->multiplierCode;
			}
		}

		if ($entityOld->serviceSourceCode and $servFld['serviceSource']['type']!='invisible') {
			$this->entityNew->serviceSource=		$entityOld->serviceSourceCode;
		}
		if ($entityOld->productName and $servFld['productName']['type']!='invisible') {
			$this->entityNew->productName=			$entityOld->productName;
		}

		if ($entityOld->idSupplierProduct)
			$this->entityNew->idSupplierProduct=	$entityOld->idSupplierProduct;

		$this->entityNew->idCustomers=				$entityOld->idCustomersList;
		$this->entityNew->undefinedPax=				$entityOld->undefinedPax;

		$this->entityNew->departureCountryCode=		$entityOld->departureCountryCode;
		$this->entityNew->departureCityId=			$entityOld->departureCityId;
		$this->entityNew->departureStationCode=		$entityOld->departureStationCode;
		$this->entityNew->destinationCountryCode=	$entityOld->destinationCountryCode;
		$this->entityNew->destinationCityId=		$entityOld->destinationCityId;
		$this->entityNew->destinationStationCode=	$entityOld->destinationStationCode;

		$this->entityNew->description=				$entityOld->description;
		$this->entityNew->preferences=				$entityOld->preferences;
		$this->entityNew->comments=					$entityOld->comments;

		if ($entityOld->agencyCostCenter)
			$this->entityNew->agencyCostCenter=		$entityOld->agencyCostCenter;
		if (defined("CURRENT_ID_USER"))
			$this->entityNew->idUser=				CURRENT_ID_USER;
		if (defined("CURRENT_ID_OFFICE"))
			$this->entityNew->idOffice=				CURRENT_ID_OFFICE;

		$this->entityNew->estInvoiceDate=			$entityOld->estInvoiceDate;
		$this->entityNew->estCashDate=				$entityOld->estCashDate;

		if ($entityOld->startDatetime)
			$this->entityNew->startDatetime=		$entityOld->startDatetime;
		if ($entityOld->endDatetime)
			$this->entityNew->endDatetime=			$entityOld->endDatetime;

		$this->entityNew->remarks=					$entityOld->remarks;

		return $this->entityNew;
	}
}
