<?php

namespace App\GenDocument\Vars;

class Values_Service extends ValuesAbstract
{

	protected $pivot = 'service';


	/**
	 * {@inheritDoc}
	 * @see \App\GenDocument\Vars\ValuesAbstract::checkPivot()
	 */
	protected function checkPivot() :void
	{
		$pivotObj = \Entity\Service::select($this->idPivot);
		if ($this->idPivot!=-1 and !$pivotObj->recordExists()) {
			throw new \Exception("The pivot `{$this->pivot}` with id = `{$this->idPivot}` is not valid.");
		}
	}

	/**
	 * The methd is used for getting the value of a certain field for the current pivot.
	 * Called from \App\GenDocument\Vars\ValuesInterface::getValue().
	 * @param String $field
	 * @return Mixed - the value of the field or NULL if the field is not present here
	 */
	protected function getSpecificPivotValue($field) {

		$CFG = \Engine\Loader_Cfg::getInstance()->getGlobal();
		$_LANG = \App\Loader_Lang::getInstance()->getGlobal();

		// the return value
		$r = null;

		// the current service
		$service=\Entity\Service::select($this->idPivot);

		// load the default number of decimals
		if ($service->chosenOffer->totalCurrency) {
			$serviceOfferCurrencyDecimals = \App\Config\CurrencyDecimals::getCurrencyDecimals($service->chosenOffer->totalCurrency);
		} else {
			$serviceOfferCurrencyDecimals = $CFG['localization']['maxDecimalsOnPrices'];
		}

		// field is found in this huge switch
		$fieldFound=true;

		// THE HUGE SWITCH WHICH WILL TREAT ALL THE VARIABLES /////////////////////////////////////
		switch ($field) {

			// GENERAL INVOICE DETAILS ////////////////////////////////////////////////////////////

			case 'servId' :									$r = $this->idPivot; break;
			case 'servObj' :								$r = $service; break;
			case 'servImportedFrom':						$r = $service->externalSystem->name; break;
			case 'servCustListNames': case 'servCustListNames2': case 'servCustListNamesWithBirth': case 'servCustListNames2WithBirth':
			case 'servCustListNamesChdWithBirth': case 'servCustListNamesChdWithAge': case 'servCustListNamesWithType':
			case 'servCustListNamesWithTitle': case 'servCustNationalities': case 'servCustListNamesWithPNC': case 'servCustListNames2WithPNC':
				$custList = $service->idCustomersList;
				$out = [];
				foreach ($custList as $idCust) {
					$elem = '';
					$cust = \Entity\Customer::select($idCust);
					if ($field == 'servCustListNames') {
						if ($cust->nameStandard) {
							$elem = $cust->nameStandard;
						}
						else {
							$elem = $cust->nameLocal;
						}
					}
					elseif ($field == 'servCustListNames2') {
						if ($cust->nameLocal) {
							$elem = $cust->nameLocal;
						}
						else {
							$elem = $cust->nameStandard;
						}
					}
					elseif ($field == 'servCustListNamesWithBirth') {
						if ($cust->nameStandard) {
							$elem=$cust->typeNameShort.' '.$cust->nameStandard;
							$b=$cust->birthday;
							if ($b and $b!=DATE_LIMIT_MIN) {
								$elem.=' ('.\Engine\DateTime::isoToStd($b).')';
							}
						}
					}
					elseif ($field=='servCustListNames2WithBirth') {
						if ($cust->nameLocal) {
							$elem=$cust->typeNameShort.' '.$cust->nameLocal;
							$b=$cust->birthday;
							if ($b and $b!=DATE_LIMIT_MIN) {
								$elem.=' ('.\Engine\DateTime::isoToStd($b).')';
							}
						}
					}
					elseif ($field=='servCustListNamesChdWithBirth') {
						$elem=$cust->typeNameShort.' '.$cust->name;
						$b=$cust->birthday;

						if ($b and $b!=DATE_LIMIT_MIN and ($cust->typeCode=='i' or $cust->typeCode=='c') ){
							$elem.=' ('.\Engine\DateTime::isoToStd($b).')';
						}
					}
					elseif ($field=='servCustListNamesChdWithAge') {
						$elem=$cust->typeNameShort.' '.$cust->name;
						$b = \Engine\DateTime::createFromIsoDate($cust->birthday);

						if (!$b->isEmpty() and $b->isValid() and ($cust->typeCode=='i' or $cust->typeCode=='c') ){
							$checkIn = \Engine\DateTime::createFromIsoDate($service->startDate);
							$years = $b->diff($checkIn)->y;
							if ($years == 1) {
								$elem.=' ('.$years.' '.$_LANG['timeUnitName']['year'].')';
							}
							else {
								$elem.=' ('.$years.' '.$_LANG['timeUnitName']['years'].')';
							}
						}
					}
					elseif ($field=='servCustListNamesWithType') {
						if ($cust->typeName) {
							$elem=sprintf("%s (%s)", $cust->nameStandard, $cust->typeNameShort);
						}
						else{
							$elem=$cust->nameStandard;

						}
					}
					elseif ($field=='servCustListNamesWithTitle') {
						if ($cust->titleName) {
							$elem=sprintf("%s (%s)", $cust->nameStandard, $cust->titleName);
						}
						else{
							$elem=$cust->nameStandard;

						}
					}
					elseif ($field=='servCustNationalities') {
						if ($cust->countryCode) $elem=$cust->country->name;
					}
					elseif ($field == 'servCustListNamesWithPNC') {
						if ($cust->nameStandard) {
							$elem = $cust->nameStandard;
							$b = $cust->PNC;
							if ($b) {
								$elem.=' ('.$b.')';
							}
						}
					}
					elseif ($field=='servCustListNames2WithPNC') {
						if ($cust->nameLocal) {
							$elem = $cust->nameLocal;
							$b = $cust->PNC;
							if ($b) {
								$elem.=' ('.$b.')';
							}
						}
					}
					if ($elem) $out[$elem]=true;
				}
				$r=implode(', ', array_keys($out));
				break;

			case 'servTotalPax' :							$r=$service->pax; break;
			case 'servPaxDetails' :							$r=$service->paxDetailsReadable; break;
			case 'servPaxAdultsCount' : 					$r=$service->paxDetails['a']+0; break;
			case 'servPaxChildrenCount' : 					$r=$service->paxDetails['c']+0; break;
			case 'servPaxInfantsCount' : 					$r=$service->paxDetails['i']+0; break;
			case 'servPaxFbCards':
				$cards = [];
				foreach($service->idCustomersList as $idCust) {
					foreach( \Entity\Customer::select($idCust)->frequentBuyerCards as $c) $cards[]=$c['cardNumber'];
				}
				$r=implode(' / ', $cards);
				break;

			case 'servName':								$r=$service->service; break;
			case 'servCategory':							$r=$service->categoryReadable; break;
			case 'servProductType':							$r=$service->productTypeReadable; break;
			case 'servMultiplier':							$r=$service->multiplierReadable; break;
			case 'servSource':								$r=$service->serviceSourceReadable; break;
			case 'servProductName':							$r=$service->productName; break;
			case 'servSupplierServiceInOrder':				$r=$service->supplierProduct->productName; break;

			case 'servStartDateIso':						$r=$service->startDate; break;
			case 'servStartDateStandard':					$r=\Engine\DateTime::isoToStd($service->startDate); break;
			case 'servEndDateIso':							$r=$service->endDate; break;
			case 'servEndDateStandard':						$r=\Engine\DateTime::isoToStd($service->endDate); break;
			case 'servDuration':							$r=$service->duration; break;
			case 'servDurationType':						$r=$_LANG['travel']['services']['serviceDurationType'][ $service->chosenOffer->durationType ]; break;

			case 'servDepartureCountry':					$r=$service->chosenOffer->departureCountry->nameStandard; break;
			case 'servDepartureCountry2':					$r=$service->chosenOffer->departureCountry->nameLocal; break;
			case 'servDepartureCity':						$r=$service->chosenOffer->departureCity->nameStandard; break;
			case 'servDepartureCity2':						$r=$service->chosenOffer->departureCity->nameLocal; break;
			case 'servDepartureAirport':					$r=$service->chosenOffer->departureStation->nameStandard; break;
			case 'servDepartureAirport2':					$r=$service->chosenOffer->departureStation->nameLocal; break;
			case 'servDepartureAirportCode':				$r=$service->chosenOffer->departureStationCode; break;
			case 'servDestinationCountry':					$r=$service->chosenOffer->destinationCountry->nameStandard; break;
			case 'servDestinationCountry2':					$r=$service->chosenOffer->destinationCountry->nameLocal; break;
			case 'servDestinationCity':						$r=$service->chosenOffer->destinationCity->nameStandard; break;
			case 'servDestinationCity2':					$r=$service->chosenOffer->destinationCity->nameLocal; break;
			case 'servDestinationCityOriginal':
				$importedServiceObj = $service->importedService->object;
				if ($importedServiceObj) {
					$chosenOffer = $importedServiceObj->chosenOffer;
				}

				if ($chosenOffer) {
					$destinationPlaceObj = $chosenOffer->destinationPlace;
				}

				if ($destinationPlaceObj) {
					$stationObj = $destinationPlaceObj->station;
				}

				if ($stationObj) {
					$cityObj = $stationObj->city;
				}

				if ($cityObj) {
					$r=(string)$cityObj->name;
				}
			break;

			case 'servDestinationAirport':					$r=$service->chosenOffer->destinationStation->nameStandard; break;
			case 'servDestinationAirport2':					$r=$service->chosenOffer->destinationStation->nameLocal; break;
			case 'servDestinationAirportCode':				$r=$service->chosenOffer->destinationStationCode; break;
			case 'servItinerary':							$r=$service->chosenOffer->itineraryReadable; break;
			case 'servItineraryShort':						$r=$service->chosenOffer->itineraryReadableCityStd; break;
			case 'servItineraryAirportsCodes':				$r=$service->chosenOffer->itineraryPassedAirportCodesReadable; break;

			case 'servCancellationPolicy':					$r=$service->chosenOffer->cancellationPolicy; break;
			case 'servDescription':							$r=$service->description; break;
			case 'servPreferences':							$r=$service->preferences; break;
			case 'servComments':							$r=$service->comments; break;
			case 'servRemarks':								$r=$service->remarks; break;

			case 'servTicketsDateIso':						$r=$service->passengersTicketsDate; break;
			case 'servTicketsDateStandard':					$r=\Engine\DateTime::isoToStd($service->passengersTicketsDate); break;

			case 'servTicketsList': case 'servTicketsList2':
				$tktsDetails=$service->passengersTicketsList;
				$suppCode=$service->chosenOffer->supplier->codes['tktCode'];
				$tkts=[];
				// add defined passengers tickets
				foreach($service->idCustomersList as $idCust) {
					if ($field=='servTicketsList') $elem= \Entity\Customer::select($idCust)->nameStandard;
					elseif ($field=='servTicketsList2') $elem= \Entity\Customer::select($idCust)->nameLocal;
					else $elem='';
					// if some ticket number defined
					if ($tktsDetails[$idCust]) {
						$elem.=' (';
						if ($suppCode) $elem.=$suppCode.'-';
						$elem.=$tktsDetails[$idCust].')';
					}
					$tkts[]=$elem;
				}
				// add defined passengers tickets
				foreach($tktsDetails as $cust=>$tkt) {
					if (!is_numeric($cust)) {
						if ($suppCode) $elem="({$suppCode}-{$tkt})";
						else $elem="({$tkt})";
						$tkts[]=$elem;
					}
				}
				$r=implode(', ', $tkts);
				break;

			case 'servTicketFlightNumber':
				$trspNos=[];
				foreach($service->chosenOffer->itinerary->exportAsArray() as $segm) {
					if ($segm['transportNumber']) $trspNos[]=$segm['transportNumber'];
				}
				$r=implode(' / ', $trspNos);
				break;

			case 'servExtSysResId':							$r=$service->chosenOffer->externalSystemReservationId; break;
			case 'servExtSysResDateIso':					$r=$service->chosenOffer->externalSystemReservationDate; break;
			case 'servExtSysResDateStandard':				$r=\Engine\DateTime::isoToStd($service->chosenOffer->externalSystemReservationDate); break;
			case 'servStatusOpName':						$r=$service->statusOpName; break;
			case 'servStatusOpDescription':					$r=$_LANG['reservationServices']['statusOpDescription'][ $service->statusOp ]; break;
			case 'servStatusFinName':						$r=$service->statusFinName; break;
			case 'servStatusFinDescription':				$r=$_LANG['reservationServices']['statusFinDescription'][ $service->statusFin ]; break;

			// SERVICE OFFER DETAILS --------------------------------------------------------------
			case 'servOfferProductName':
				if ($service->chosenOffer->idSupplierProduct) {
					$r=$service->chosenOffer->supplierProduct->productName;
				} else {
					$r=$service->chosenOffer->productName;
				}
				break;
			case 'servOfferType':							$r=$service->chosenOffer->offerTypeName; break;
			case 'servOfferService':						$r=$service->chosenOffer->serviceName; break;
			case 'servOfferLocation':						$r=$service->chosenOffer->locationNameLong; break;
			case 'servOfferCalculationType':				$r=$service->chosenOffer->calculationTypeName; break;

			case 'servOfferStartDateIso':					$r=$service->chosenOffer->startDate; break;
			case 'servOfferEndDateIso':						$r=$service->chosenOffer->endDate; break;

			case 'servOfferPriceSupplier':					$r=round($service->chosenOffer->supplierWorkValue+$service->chosenOffer->supplierWorkVat, $serviceOfferCurrencyDecimals); break;
			case 'servOfferPriceCommission':				$r=round($service->chosenOffer->commissionWorkValue+$service->chosenOffer->commissionWorkVat, $serviceOfferCurrencyDecimals); break;
			case 'servOfferPriceDiscount':					$r=round($service->chosenOffer->discountWorkValue+$service->chosenOffer->discountWorkVat, $serviceOfferCurrencyDecimals); break;
			case 'servOfferPriceServiceFee':				$r=round($service->chosenOffer->serviceFeeWorkValue+$service->chosenOffer->serviceFeeWorkVat, $serviceOfferCurrencyDecimals); break;
			case 'servOfferPriceTax':						$r=round($service->chosenOffer->taxWorkValue+$service->chosenOffer->taxWorkVat, $serviceOfferCurrencyDecimals); break;
			case 'servOfferPriceTotal':						$r=round($service->chosenOffer->offerWorkTotal, $serviceOfferCurrencyDecimals); break;
			case 'servOfferPriceCurrency':					$r=$service->chosenOffer->totalCurrency; break;
			case 'servOfferConfirmationNumber':				$r=$service->chosenOffer->confirmationNumber; break;
			case 'servOfferExpirationDateIso':				$r=$service->chosenOffer->expirationDate; break;
			case 'servOfferExpirationDateStandard':			$r=\Engine\DateTime::isoToStd($service->chosenOffer->expirationDate); break;
			case 'servOfferComments':						$r=$service->chosenOffer->comments; break;
			case 'servOfferOptionDateIso':					$r=$service->chosenOffer->optionDate; break;
			case 'servOfferOptionDateStd':					$r=\Engine\DateTime::isoToStd($service->chosenOffer->optionDate); break;
			case 'servOfferCurrencyExchRateDefCurr':		$r = \App\CurrencyExchange\Rates::getInstance($service->chosenOffer->currencyDate, $service->chosenOffer->currencyTypeCode)->convert(1, $service->chosenOffer->totalCurrency, \App\Config\Currencies::getInstance()->getDefaultCurrency(), null);break;
			case 'servOfferNoPenaltyOptionDateIso':			$r=$service->chosenOffer->noPenaltyOptionDate; break;
			case 'impServItineraryCO2Emissions':			$r=$service->importedService->CO2Emissions; break;

			case 'servItineraryFlightDistanceTotal':
				$flightDistanceTotal = 0;
				$itineraryObj = $service->chosenOffer->itinerary;
				if ($itineraryObj and $itineraryObj->getSegmentsCount()) {
					foreach($itineraryObj->getSegments() as $segmentObj) {
						$thisDist = $segmentObj->getDistance();
						if ($thisDist->value) {
							$flightDistanceTotal += is_numeric($thisDist->value)?$thisDist->value:0;
						}
					}

					$r = $flightDistanceTotal;
				}
				break;

			case 'servItineraryFlightDistances':
				$flightDistances = [];
				$unit = '';
				$itineraryObj = $service->chosenOffer->itinerary;
				if ($itineraryObj and $itineraryObj->getSegmentsCount()) {
					foreach($itineraryObj->getSegments() as $segmentObj) {
						$thisDist = $segmentObj->getDistance();
						if ($thisDist->value) {
							$flightDistances[]	= $thisDist->value;
							$unit = $thisDist->unit;
						}
					}

					$r = implode(" / ", array_filter($flightDistances))." [{$unit}]";
				}
				break;

			// SERVICE OFFER SUPPLIER DETAILS -----------------------------------------------------
			case 'servSuppAlias':							$r=$service->chosenOffer->supplier->alias; break;
			case 'servSuppCompany':							$r=$service->chosenOffer->supplier->company; break;
			case 'servSuppCompanyFull':						$r=$service->chosenOffer->supplier->companyFull; break;
			case 'servSuppCompanyPrefix':					$r=$service->chosenOffer->supplier->companyPrefix; break;
			case 'servSuppCompanySuffix':					$r=$service->chosenOffer->supplier->companySuffix; break;

			case 'servSuppTrust':							$r=$service->chosenOffer->supplier->trust->name; break;
			case 'servSuppNameInGds':						$r=$service->chosenOffer->supplier->codes['iata']; break;
			case 'servSuppAddress':							$r=$service->chosenOffer->supplier->address; break;
			case 'servSuppDeliveryAddress':
				$r=$service->chosenOffer->supplier->deliveryAddress;
				if (!$r) $r=$service->chosenOffer->supplier->address;
				break;
			case 'servSuppZipCode':							$r=$service->chosenOffer->supplier->zipCode; break;
			case 'servSuppTelephone':						$r=$service->chosenOffer->supplier->telephone; break;
			case 'servSuppFax':								$r=$service->chosenOffer->supplier->fax; break;
			case 'servSuppEmail':							$r=$service->chosenOffer->supplier->email; break;
			case 'servSuppWebsite':							$r=$service->chosenOffer->supplier->website; break;
			case 'servSuppRegNo':							$r=$service->chosenOffer->supplier->regNo; break;
			case 'servSuppFiscalCode':						$r=$service->chosenOffer->supplier->fiscalCode; break;

			// SERVICE SUPPLIER SERVICE DETAILS ---------------------------------------------------
			case 'servSuppServServiceName':					$r=$service->chosenOffer->supplierProduct->serviceName; break;
			case 'servSuppServCategory':					$r=$service->chosenOffer->supplierProduct->categoryReadable; break;
			case 'servSuppServProductName':					$r=$service->chosenOffer->supplierProduct->productName; break;
			case 'servSuppServDescription':					$r=$service->chosenOffer->supplierProduct->description; break;
			case 'servSuppServLocalization':				$r=$service->chosenOffer->supplierProduct->localization; break;
			case 'servSuppServAddress':						$r=$service->chosenOffer->supplierProduct->address; break;
			case 'servSuppServDepartureCountry':			$r=$service->chosenOffer->supplierProduct->departureCountry->name; break;
			case 'servSuppServDepartureCity':				$r=$service->chosenOffer->supplierProduct->departureCity->name; break;
			case 'servSuppServDestinationCountry':			$r=$service->chosenOffer->supplierProduct->destinationCountry->name; break;
			case 'servSuppServDestinationCity':				$r=$service->chosenOffer->supplierProduct->destinationCity->name; break;
			case 'servSuppServTelephone':					$r=$service->chosenOffer->supplierProduct->telephone; break;
			case 'servSuppServFax':							$r=$service->chosenOffer->supplierProduct->fax; break;
			case 'servSuppServEmail':						$r=$service->chosenOffer->supplierProduct->email; break;
			case 'servSuppServWebsite':						$r=$service->chosenOffer->supplierProduct->website; break;
			case 'impServOfferSupplierProductObj':
				if ($service->importedService->recordExists() and $service->importedService->object and $service->importedService->object->chosenOffer and $service->importedService->object->chosenOffer->supplierProduct) {
					$r = $service->importedService->object->chosenOffer->supplierProduct;
				} else {
					$r = new \App\ImportedOrder\SupplierProduct();
				}
				break;
			case 'servSupplierRecordLocator':
				$suppRecordLocator = [];
				if ($service->idImportedService and $service->importedService->object and $service->importedService->object->chosenOffer and $service->importedService->object->chosenOffer->otinerary and $service->importedService->object->chosenOffer->itinerary->segments) {
					foreach($service->importedService->object->chosenOffer->itinerary->segments as $segm) {
						$suppRecordLocator[] = $segm->supplierRecordLocator;
					}
				}
				$r = implode('//', array_unique($suppRecordLocator));
				break;

			// SERVICE RESPONSIBLE USER -----------------------------------------------------------
			case 'servRespUserName':						$r=$service->user->name; break;
			case 'servRespUserLastName':					$r=$service->user->lastName; break;
			case 'servRespUserFirstName':					$r=$service->user->firstName; break;
			case 'servRespUserIdSeries':					$r=$service->user->ID_series; break;
			case 'servRespUserIdNumber':					$r=$service->user->ID_number; break;
			case 'servRespUserIdReleaseBy':					$r=$service->user->ID_releaseBy; break;
			case 'servRespUserIdReleaseDateIso':			$r=$service->user->ID_releaseDate; break;
			case 'servRespUserIdReleaseDateStandard':		$r=\Engine\DateTime::isoToStd($service->user->ID_releaseDate); break;
			case 'servRespUserPNC':							$r=$service->user->PNC; break;
			case 'servRespUserJob':							$r=$service->user->job; break;
			case 'servRespUserEmail':						$r=$service->user->email; break;
			case 'servRespUserEmailAlternative':			$r=$service->user->emailAlternative; break;
			case 'servRespUserTelephone':					$r=$service->user->telephone; break;
			case 'servRespUserFax':							$r=$service->user->fax; break;
			case 'servRespUserDeptName':					$r=$service->user->department->name; break;
			case 'servRespUserDeptCode':					$r=$service->user->department->code; break;

			// SERVICE RESPONSIBLE OFFICE ---------------------------------------------------------
			case 'servRespOffName':							$r=$service->office->name; break;
			case 'servRespOfficeIataCode':					$r=implode(",", $service->office->iataCode); break;
			case 'servRespOffAddress':						$r=$service->office->address; break;
			case 'servRespOffDeliveryAddress':
				$r=$service->office->deliveryAddress;
				if (!$r) $r=$service->office->address;
				break;
			case 'servRespOffZipCode':						$r=$service->office->zipCode; break;
			case 'servRespOffCountry':						$r=$service->office->country->name; break;
			case 'servRespOffCity':							$r=$service->office->city->name; break;
			case 'servRespOffTelephone':					$r=$service->office->telephone; break;
			case 'servRespOffFax':							$r=$service->office->fax; break;
			case 'servRespOffEmail':						$r=$service->office->email; break;
			case 'servRespOffWeb':							$r=$service->office->web; break;

			// SERVICE ADD USER -------------------------------------------------------------------
			case 'servAddUserName':							$r=$service->addUser->name; break;
			case 'servAddUserLastName':						$r=$service->addUser->lastName; break;
			case 'servAddUserFirstName':					$r=$service->addUser->firstName; break;
			case 'servAddUserIdSeries':						$r=$service->addUser->ID_series; break;
			case 'servAddUserIdNumber':						$r=$service->addUser->ID_number; break;
			case 'servAddUserIdReleaseBy':					$r=$service->addUser->ID_releaseBy; break;
			case 'servAddUserIdReleaseDateIso':				$r=$service->addUser->ID_releaseDate; break;
			case 'servAddUserIdReleaseDateStandard':		$r=\Engine\DateTime::isoToStd($service->addUser->ID_releaseDate); break;
			case 'servAddUserPNC':							$r=$service->addUser->PNC; break;
			case 'servAddUserJob':							$r=$service->addUser->job; break;
			case 'servAddUserEmail':						$r=$service->addUser->email; break;
			case 'servAddUserEmailAlternative':				$r=$service->addUser->emailAlternative; break;
			case 'servAddUserTelephone':					$r=$service->addUser->telephone; break;
			case 'servAddUserFax':							$r=$service->addUser->fax; break;
			case 'servAddUserDeptName':						$r=$service->addUser->department->name; break;
			case 'servAddUserDeptCode':						$r=$service->addUser->department->code; break;

			// RESERVATION GENERAL DETAILS --------------------------------------------------------
			case 'resId':									$r=$service->order->getId(); break;
			case 'resConvId':								$r=$service->order->convertedId; break;
			case 'resNumber':								$r=$service->order->reservationNumber; break;
			case 'resCode':									$r=$service->order->reservationCode; break;
			case 'resDateIso':								$r=$service->order->date; break;
			case 'resDateStandard':							$r=\Engine\DateTime::isoToStd($service->order->date); break;
			case 'resFlag':									$r=$service->order->flagName; break;
			case 'resServicesType':							$r=$service->order->servicesTypeName; break;
			case 'resResType':								$r=$service->order->resTypeName; break;

			// RESERVATION OWNER DETAILS ----------------------------------------------------------
			case 'resOwnerCustId':							$r=$service->order->idCustomerOwner; break;
			case 'resOwnerCustName':
				$r=$service->order->customerOwner->name;
				$ttl=$service->order->customerOwner->titleName;
				if ($ttl) $r.=' / '.$ttl;
				break;
			case 'resOwnerCustLastName':					$r=$service->order->customerOwner->lastName; break;
			case 'resOwnerCustFirstName':					$r=$service->order->customerOwner->firstName; break;
			case 'resOwnerCustTitle':						$r=$service->order->customerOwner->titleName; break;
			case 'resOwnerCustCountryName':					$r=$service->order->customerOwner->country->name; break;
			case 'resOwnerCustCityName':					$r=$service->order->customerOwner->city->name; break;
			case 'resOwnerCustPNC':							$r=$service->order->customerOwner->PNC; break;
			case 'resOwnerCustIdSeries':					$r=$service->order->customerOwner->ID_series; break;
			case 'resOwnerCustIdNumber':					$r=$service->order->customerOwner->ID_number; break;
			case 'resOwnerCustIdReleaseBy':					$r=$service->order->customerOwner->ID_releaseBy; break;
			case 'resOwnerCustIdReleaseDateIso':			$r=$service->order->customerOwner->ID_releaseDate; break;
			case 'resOwnerCustIdReleaseDateStandard':		$r=\Engine\DateTime::isoToStd($service->order->customerOwner->ID_releaseDate); break;
			case 'resOwnerCustPassportNo':					$r=$service->order->customerOwner->defaultPassport['number']; break;
			case 'resOwnerCustPassportType':				$r=$service->order->customerOwner->defaultPassport['type']; break;
			case 'resOwnerCustPassportExpireDateIso':		$r=$service->order->customerOwner->defaultPassport['expireDate']; break;
			case 'resOwnerCustPassportExpireDateStandard':	$r=\Engine\DateTime::isoToStd($service->order->customerOwner->defaultPassport['expireDate']); break;
			case 'resOwnerCustAddress':						$r=$service->order->customerOwner->address; break;
			case 'resOwnerCustDeliveryAddress':
				$r=$service->order->customerOwner->deliveryAddress;
				if (!$r) $r=$service->order->customerOwner->address;
				break;
			case 'resOwnerCustZipCode':						$r=$service->order->customerOwner->zipCode; break;
			case 'resOwnerCustBankName':					$r=$service->order->customerOwner->bank->name; break;
			case 'resOwnerCustBankAccount':					$r=$service->order->customerOwner->bankAccount; break;
			case 'resOwnerCustTelephone':					$r=$service->order->customerOwner->telephone; break;
			case 'resOwnerCustFax':							$r=$service->order->customerOwner->fax; break;
			case 'resOwnerCustEmail':						$r=$service->order->customerOwner->email; break;
			case 'resCustListNames':
				$custs=[];
				foreach($service->order->idCustomersList as $idCust) $custs[]= \Entity\Customer::select($idCust)->nameWithTitle;
				$r=implode(', ', $custs);
				break;

			// CORPORATE OWNER DETAILS ------------------------------------------------------------
			case 'resOwnerCorpId':							$r=$service->order->idCorporateOwner; break;
			case 'resOwnerCorpCompany':						$r=$service->order->corporateOwner->company; break;
			case 'resOwnerCorpCompanyFull':					$r=$service->order->corporateOwner->companyFull; break;
			case 'resOwnerCorpCompanyPrefix':				$r=$service->order->corporateOwner->companyPrefix; break;
			case 'resOwnerCorpCompanySuffix':				$r=$service->order->corporateOwner->companySuffix; break;
			case 'resOwnerCorpAlias':						$r=$service->order->corporateOwner->alias; break;
			case 'resOwnerCorpTrust':						$r=$service->order->corporateOwner->trust->name; break;
			case 'resOwnerCorpFiscalCode':					$r=$service->order->corporateOwner->fiscalCode; break;
			case 'resOwnerCorpRegNo':						$r=$service->order->corporateOwner->regNo; break;
			case 'resOwnerCorpTelephone':					$r=$service->order->corporateOwner->telephone; break;
			case 'resOwnerCorpFax':							$r=$service->order->corporateOwner->fax; break;
			case 'resOwnerCorpEmail':						$r=$service->order->corporateOwner->email; break;
			case 'resOwnerCorpMainOffAddress':				$r=$service->order->corporateOwner->mainOffice->address; break;
			case 'resOwnerCorpMainOffCityId':				$r=$service->order->corporateOwner->mainOffice->cityId; break;
			case 'resOwnerCorpMainOffCityName':				$r=$service->order->corporateOwner->mainOffice->city->name; break;
			case 'resOwnerCorpMainOffCountryCode':			$r=$service->order->corporateOwner->mainOffice->countryCode; break;
			case 'resOwnerCorpMainOffCountryName':			$r=$service->order->corporateOwner->mainOffice->country->name; break;
			case 'resCorpListNames':
				$corps=[];
				foreach($service->order->idCorporatesList as $idCorp) $corps[] = \Entity\Corporate::select($idCorp)->company;
				$r=implode(', ', $corps);
				break;
			case 'resContactPersonName':					$r=$service->order->contactPerson->name; break;
			case 'resTotalPax':								$r=$service->order->pax; break;
			case 'resPaxDetails':							$r=$service->order->paxDetailsReadable; break;

			// RESERVATION GENERAL DETAILS --------------------------------------------------------
			case 'resDescription':							$r=$service->order->description; break;
			case 'resComments':								$r=$service->order->comments; break;
			case 'resStatusOpName':							$r=$service->order->statusOpName; break;
			case 'resStatusOpDescription':					$r=$_LANG['reservations']['statusOpDescription'][ $service->order->statusOp ]; break;
			case 'resStatusFinName':						$r=$service->order->statusFinName; break;
			case 'resStatusFinDescription':					$r=$_LANG['reservations']['statusFinDescription'][ $service->order->statusFin ]; break;

			// RESERVATION RESPONSIBLE USER -------------------------------------------------------
			case 'resRespUserName':							$r=$service->order->user->name; break;
			case 'resRespUserLastName':						$r=$service->order->user->lastName; break;
			case 'resRespUserFirstName':					$r=$service->order->user->firstName; break;
			case 'resRespUserIdSeries':						$r=$service->order->user->ID_series; break;
			case 'resRespUserIdNumber':						$r=$service->order->user->ID_number; break;
			case 'resRespUserIdReleaseBy':					$r=$service->order->user->ID_releaseBy; break;
			case 'resRespUserIdReleaseDateIso':				$r=$service->order->user->ID_releaseDate; break;
			case 'resRespUserIdReleaseDateStandard':		$r=\Engine\DateTime::isoToStd($service->order->user->ID_releaseDate); break;
			case 'resRespUserPNC':							$r=$service->order->user->PNC; break;
			case 'resRespUserJob':							$r=$service->order->user->job; break;
			case 'resRespUserEmail':						$r=$service->order->user->email; break;
			case 'resRespUserEmailAlternative':				$r=$service->order->user->emailAlternative; break;
			case 'resRespUserTelephone':					$r=$service->order->user->telephone; break;
			case 'resRespUserFax':							$r=$service->order->user->fax; break;
			case 'resRespUserDeptName':						$r=$service->order->user->department->name; break;
			case 'resRespUserDeptCode':						$r=$service->order->user->department->code; break;

			// RESERVATION RESPONSIBLE OFFICE -----------------------------------------------------
			case 'resRespOffName':							$r=$service->order->office->name; break;
			case 'resRespOffIataCode':						$r=implode(",", $service->order->office->iataCode); break;
			case 'resRespOffAddress':						$r=$service->order->office->address; break;
			case 'resRespOffDeliveryAddress':
				$r=$service->order->office->deliveryAddress;
				if (!$r) $r=$service->order->office->address;
				break;
			case 'resRespOffZipCode':						$r=$service->order->office->zipCode; break;
			case 'resRespOffCountry':						$r=$service->order->office->country->name; break;
			case 'resRespOffCity':							$r=$service->order->office->city->name; break;
			case 'resRespOffTelephone':						$r=$service->order->office->telephone; break;
			case 'resRespOffFax':							$r=$service->order->office->fax; break;
			case 'resRespOffEmail':							$r=$service->order->office->email; break;
			case 'resRespOffWeb':							$r=$service->order->office->web; break;

			// RESERVATION ADD USER ---------------------------------------------------------------
			case 'resAddUserName':							$r=$service->order->addUser->name; break;
			case 'resAddUserLastName':						$r=$service->order->addUser->lastName; break;
			case 'resAddUserFirstName':						$r=$service->order->addUser->firstName; break;
			case 'resAddUserIdSeries':						$r=$service->order->addUser->ID_series; break;
			case 'resAddUserIdNumber':						$r=$service->order->addUser->ID_number; break;
			case 'resAddUserIdReleaseBy':					$r=$service->order->addUser->ID_releaseBy; break;
			case 'resAddUserIdReleaseDateIso':				$r=$service->order->addUser->ID_releaseDate; break;
			case 'resAddUserIdReleaseDateStandard':			$r=\Engine\DateTime::isoToStd($service->order->addUser->ID_releaseDate); break;
			case 'resAddUserPNC':							$r=$service->order->addUser->PNC; break;
			case 'resAddUserJob':							$r=$service->order->addUser->job; break;
			case 'resAddUserEmail':							$r=$service->order->addUser->email; break;
			case 'resAddUserEmailAlternative':				$r=$service->order->addUser->emailAlternative; break;
			case 'resAddUserTelephone':						$r=$service->order->addUser->telephone; break;
			case 'resAddUserFax':							$r=$service->order->addUser->fax; break;
			case 'resAddUserDeptName':						$r=$service->order->addUser->department->name; break;
			case 'resAddUserDeptCode':						$r=$service->order->addUser->department->code; break;

			// COMPANY BANK ACCOUNTS --------------------------------------------------------------
			case 'compBankMainAccAccount':					$r = \Entity\AgencyBankAccount::select( \Entity\AgencyBankAccount::getDefaultId() )->account; break;
			case 'compBankMainAccBankName':
				$idDefBa = \Entity\AgencyBankAccount::getDefaultId();
				$r = \Entity\AgencyBankAccount::select($idDefBa)->bank->name;
				if (\Entity\AgencyBankAccount::select($idDefBa)->idBankOffice) $r.=' ('.\Entity\AgencyBankAccount::select($idDefBa)->bankOffice->name.')';
				break;
			case 'compBankMainAccBankCode':					$r = \Entity\AgencyBankAccount::select( \Entity\AgencyBankAccount::getDefaultId() )->bank->code; break;
			case 'compBankMainAccCurrency':					$r = \Entity\AgencyBankAccount::select( \Entity\AgencyBankAccount::getDefaultId() )->currency; break;
			case 'compBankMainAccSwiftCode':				$r = \Entity\AgencyBankAccount::select( \Entity\AgencyBankAccount::getDefaultId() )->bankOffice->swiftCode; break;

			// field is not found here
			default: {
				$fieldFound=false;
			}

		}

		// if the field is found but is null - transform it to blank
		if ($fieldFound and $r===null) {
			$r='';
		}

		return $r;

	}

}