<?php

namespace App\GenDocument\Vars;

class Values_Cashing extends ValuesAbstract
{

	protected $pivot = 'cashing';

	/**
	 * {@inheritDoc}
	 * @see \App\GenDocument\Vars\ValuesAbstract::checkPivot()
	 */
	protected function checkPivot() :void
	{
		$pivotObj = \Entity\InvoiceCashing::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 cashing documenyt object
		$doc=\Entity\InvoiceCashing::select($this->idPivot);

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

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

			// DOCUMENT GENERAL DETAILS ///////////////////////////////////////////////////////////

			case 'cashId':								$r=$this->idPivot; break;
			case 'cashObj':								$r=$doc; break;
			case 'cashSeriesId':						$r=$doc->idCashingSeries; break;
			case 'cashSeriesCode':						$r=$doc->cashingSeries->code; break;
			case 'cashType':							$r=\App\Config\CashingDocuments::getInstance()->getName($doc->docTypeCode); break;
			case 'cashDateIso':							$r=$doc->docDate; break;
			case 'cashDateStandard':					$r=\Engine\DateTime::isoToStd($doc->docDate); break;
			case 'cashNumber':							$r=$doc->docNumber; break;
			case 'cashValue':							$r=$doc->value; break;
			case 'cashValueAbs':						$r=abs($doc->value); break;
			case 'cashValueDefCurr':					$r=\App\CurrencyExchange\Rates::getInstance($doc->docDate)->convert($doc->value, $doc->currency, $CFG['currencies']['defaultCurrency']); break;
			case 'cashValueAbsDefCurr':					$r=abs(\App\CurrencyExchange\Rates::getInstance($doc->docDate)->convert($doc->value, $doc->currency, $CFG['currencies']['defaultCurrency'])); break;
			case 'cashValueDefCurrWithCurr':
				if ($doc->currency!=$CFG['currencies']['defaultCurrency'])
					$r=\App\CurrencyExchange\Rates::getInstance($doc->docDate)->convert($doc->value, $doc->currency, $CFG['currencies']['defaultCurrency']) . ' ' . $CFG['currencies']['defaultCurrency'];
				break;
			case 'cashCurrency':
				$r=$doc->currency;
				break;
			case 'cashCurrency2':
				$currDet=\App\Config\Currencies::getInstance()->get($doc->currency);
				$r=$currDet['currencyName2'];
				break;
			case 'cashBankName':
				$r=$doc->bankAccount->bank->name;
				if ($doc->bankAccount->idBankOffice) $r.=' ('.$doc->bankAccount->bankOffice->name.')';
				break;
			case 'cashBankCode':						$r=$doc->bankAccount->bank->code; break;
			case 'cashSwiftCode':						$r=$doc->bankAccount->bankOffice->swiftCode; break;
			case 'cashAccount':							$r=$doc->bankAccount->account; break;
			case 'cashExplanations':					$r=$doc->explanations; break;
			case 'cashComments':						$r=$doc->comments; break;
			case 'cashReferCurrencyTypeName':			$r=\App\Config\CurrencyTypes::getInstance()->getName($CFG['currencies']['defaultCurrencyType']); break;
			case 'cashReferCurrencyTypeRate':			$r=\App\CurrencyExchange\Rates::getInstance($doc->docDate)->convert(1, $doc->currency, $CFG['currencies']['defaultCurrency'], null); break;
			case 'cashReferCurrencyTypeRateWithCurr':
				if ($doc->currency!=$CFG['currencies']['defaultCurrency']) {
					$rate=\App\CurrencyExchange\Rates::getInstance($doc->docDate)->convert(1, $doc->currency, $CFG['currencies']['defaultCurrency'], null);
					$r='1'.$doc->currency.'='.$rate.$CFG['currencies']['defaultCurrency'];
				}
				break;
			case 'cashReferCurrency':					$r=$CFG['currencies']['defaultCurrency']; break;

			// DOCUMENT ADD USER //////////////////////////////////////////////////////////////////
			case 'cashAddUserName':						$r=$doc->addUser->name; break;
			case 'cashAddUserLastName':					$r=$doc->addUser->lastName; break;
			case 'cashAddUserFirstName':				$r=$doc->addUser->firstName; break;
			case 'cashAddUserIdSeries':					$r=$doc->addUser->ID_series; break;
			case 'cashAddUserIdNumber':					$r=$doc->addUser->ID_number; break;
			case 'cashAddUserIdReleaseBy':				$r=$doc->addUser->ID_releaseBy; break;
			case 'cashAddUserIdReleaseDateIso':			$r=$doc->addUser->ID_releaseDate; break;
			case 'cashAddUserIdReleaseDateStandard':	$r=\Engine\DateTime::isoToStd($doc->addUser->ID_releaseDate); break;
			case 'cashAddUserPNC':						$r=$doc->addUser->PNC; break;
			case 'cashAddUserJob':						$r=$doc->addUser->job; break;
			case 'cashAddUserEmail':					$r=$doc->addUser->email; break;
			case 'cashAddUserTelephone':				$r=$doc->addUser->telephone; break;
			case 'cashAddUserDeptName':					$r=$doc->addUser->department->name; break;
			case 'cashAddUserDeptCode':					$r=$doc->addUser->department->code; break;

			// DOCUMENT ISSUE USER ////////////////////////////////////////////////////////////////
			case 'cashIssueUserName':					$r=$doc->issueUser->name; break;
			case 'cashIssueUserLastName':				$r=$doc->issueUser->lastName; break;
			case 'cashIssueUserFirstName':				$r=$doc->issueUser->firstName; break;
			case 'cashIssueUserIdSeries':				$r=$doc->issueUser->ID_series; break;
			case 'cashIssueUserIdNumber':				$r=$doc->issueUser->ID_number; break;
			case 'cashIssueUserIdReleaseBy':			$r=$doc->issueUser->ID_releaseBy; break;
			case 'cashIssueUserIdReleaseDateIso':		$r=$doc->issueUser->ID_releaseDate; break;
			case 'cashIssueUserIdReleaseDateStandard':	$r=\Engine\DateTime::isoToStd($doc->issueUser->ID_releaseDate); break;
			case 'cashIssueUserPNC':					$r=$doc->issueUser->PNC; break;
			case 'cashIssueUserJob':					$r=$doc->issueUser->job; break;
			case 'cashIssueUserEmail':					$r=$doc->issueUser->email; break;
			case 'cashIssueUserTelephone':				$r=$doc->issueUser->telephone; break;
			case 'cashIssueUserDeptName':				$r=$doc->issueUser->department->name; break;
			case 'cashIssueUserDeptCode':				$r=$doc->issueUser->department->code; break;

			// INVOICE GENERAL DETAILS ////////////////////////////////////////////////////////////
			case 'invId':								$r=$doc->idInvoice; break;
			case 'invDateIso':							$r=$doc->invoice->invoiceDate; break;
			case 'invDateStandard':						$r=\Engine\DateTime::isoToStd($doc->invoice->invoiceDate); break;
			case 'invNumber':							$r=$doc->invoice->invoiceNumber; break;
			case 'invSeriesCode':						$r=$doc->invoice->invoiceSeries->code; break;
			case 'invDueDateIso':						$r=$doc->invoice->dueDate; break;
			case 'invDueDateStandard':					$r=\Engine\DateTime::isoToStd($doc->invoice->dueDate); break;
			case 'invFulfillmentDateIso':				$r=$doc->invoice->fulfillmentDate; break;
			case 'invFulfillmentDateStandard':			$r=\Engine\DateTime::isoToStd($doc->invoice->fulfillmentDate); break;
			case 'invFormOfPayment':					$r=$doc->invoice->formOfPaymentName; break;
			case 'invValue':							$r=$doc->invoice->printValue; break;
			case 'invVat':								$r=$doc->invoice->printVat; break;
			case 'invTotal':							$r=$doc->invoice->printTotal; break;
			case 'invCurrency':							$r=$doc->invoice->currency; break;
			case 'invReferCurrencyTypeName':			$r=\App\Config\CurrencyTypes::getInstance()->getName($CFG['currencies']['defaultCurrencyType']); break;
			case 'invReferCurrencyTypeRate':			$r=\App\CurrencyExchange\Rates::getInstance($doc->invoice->invoiceDate)->convert(1, $doc->invoice->currency, $CFG['currencies']['defaultCurrency'], null); break;
			case 'invReferCurrency':					$r=$CFG['currencies']['defaultCurrency']; break;
			case 'invServicesList':
				$servs=[];
				foreach($doc->invoice->rowsList as $row) $servs[ \App\Config\Services_Basic::getInstance()->getServiceName($row['serviceCode']) ]=true;
				$r=implode(', ', array_keys($servs));
				break;
			case 'invOrderConvId':
				$ordIds = [];
				foreach($doc->invoice->rowsList as $row) {
					$ord = \Entity\Service::select($row['idReservationService'])->order;
					$ordIds[$ord->getId()] = $ord->convertedId;
				}
				$r=implode(', ', $ordIds);
				break;

			case 'invOrderReservationNumber':
				$orderReservationNumbers = [];
				foreach ($doc->invoice->rowsList as $row) {
					$orderEntity = \Entity\Service::select($row['idReservationService'])->order;
					$orderReservationNumbers[$orderEntity->getId()] = $orderEntity->reservationNumber;
				}

				$r = implode(', ', $orderReservationNumbers);
			break;
			case 'invMiceProjects':
				$miceProjects = [];
				foreach($doc->invoice->rowsList as $row) {
					$ord = \Entity\Service::select($row['idReservationService'])->order;
					$miceProjects[$ord->idMiceProject] = $ord->miceProject->name;
				}
				$r=implode(', ', $miceProjects);
				break;
			case 'invPassengers':
				$pass = [];
				foreach($doc->invoice->rowsList as $row) {
					foreach (\Entity\Service::select($row['idReservationService'])->idCustomersList as $idPass) {
						if (!$pass[$idPass]) {
							$pass[$idPass] = \Entity\Customer::select($idPass)->name;
						}
					}
				}
				$r = implode(', ', $pass);
				break;
			case 'invClientGeneralType':
				if ($doc->invoice->idCorporate) {
					$r='corporate';
				}
				else {
					$r='customer';
				}
				break;


			// INVOICE CORPORATE CLIENT ///////////////////////////////////////////////////////////
			case 'invClientCorpId':						$r=$doc->invoice->idCorporate; break;
			case 'invClientCorpCompany':				$r=$doc->invoice->corporate->companyStandard; break;
			case 'invClientCorpCompanyFull':			$r=$doc->invoice->corporate->companyFullStandard; break;
			case 'invClientCorpCompanyPrefix':			$r=$doc->invoice->corporate->companyPrefixStandard; break;
			case 'invClientCorpCompanySuffix':			$r=$doc->invoice->corporate->companySuffixStandard; break;
			case 'invClientCorpCompany2':				$r=$doc->invoice->corporate->companyLocal; break;
			case 'invClientCorpCompanyFull2':			$r=$doc->invoice->corporate->companyFullLocal; break;
			case 'invClientCorpCompanyPrefix2':			$r=$doc->invoice->corporate->companyPrefixLocal; break;
			case 'invClientCorpCompanySuffix2':			$r=$doc->invoice->corporate->companySuffixLocal; break;
			case 'invClientCorpCompany_alt':
				$r=$doc->invoice->corporate->companyStandard;
				if (!$r) $r=$doc->invoice->corporate->companyLocal;
				break;
			case 'invClientCorpCompanyFull_alt':
				$r=$doc->invoice->corporate->companyFullStandard;
				if (!$r) $r=$doc->invoice->corporate->companyFullLocal;
				break;
			case 'invClientCorpCompany2_alt':
				$r=$doc->invoice->corporate->companyLocal;
				if (!$r) $r=$doc->invoice->corporate->companyStandard;
				break;
			case 'invClientCorpCompanyFull2_alt':
				$r=$doc->invoice->corporate->companyFullLocal;
				if (!$r) $r=$doc->invoice->corporate->companyFullStandard;
				break;
			case 'invClientCorpAlias':					$r=$doc->invoice->corporate->alias; break;
			case 'invClientCorpTrust':					$r=$doc->invoice->corporate->trust->name; break;
			case 'invClientCorpTelephone':				$r=$doc->invoice->corporate->telephone; break;
			case 'invClientCorpFax':					$r=$doc->invoice->corporate->fax; break;
			case 'invClientCorpEmail':					$r=$doc->invoice->corporate->email; break;
			case 'invClientCorpFiscalCode':				$r=$doc->invoice->corporate->fiscalCode; break;
			case 'invClientCorpRegNo':					$r=$doc->invoice->corporate->regNo; break;

			// INVOICE CORPORATE ACCOUNT MANAGER //////////////////////////////////////////////////
			case 'invClientCorpAccMngName':				$r=$doc->invoice->corporate->accountManager->name; break;
			case 'invClientCorpAccMngLastName':			$r=$doc->invoice->corporate->accountManager->lastName; break;
			case 'invClientCorpAccMngFirstName':		$r=$doc->invoice->corporate->accountManager->firstName; break;
			case 'invClientCorpAccMngIdSeries':			$r=$doc->invoice->corporate->accountManager->ID_series; break;
			case 'invClientCorpAccMngIdNumber':			$r=$doc->invoice->corporate->accountManager->ID_number; break;
			case 'invClientCorpAccMngIdReleaseBy':		$r=$doc->invoice->corporate->accountManager->ID_releaseBy; break;
			case 'invClientCorpAccMngIdReleaseDateIso':	$r=$doc->invoice->corporate->accountManager->ID_releaseDate; break;
			case 'invClientCorpAccMngIdReleaseDateStandard':$r=\Engine\DateTime::isoToStd($doc->invoice->corporate->accountManager->ID_releaseDate); break;
			case 'invClientCorpAccMngPNC':				$r=$doc->invoice->corporate->accountManager->PNC; break;
			case 'invClientCorpAccMngJob':				$r=$doc->invoice->corporate->accountManager->job; break;
			case 'invClientCorpAccMngEmail':			$r=$doc->invoice->corporate->accountManager->email; break;
			case 'invClientCorpAccMngTelephone':		$r=$doc->invoice->corporate->accountManager->telephone; break;
			case 'invClientCorpAccMngFax':				$r=$doc->invoice->corporate->accountManager->fax; break;
			case 'invClientCorpAccMngDeptName':			$r=$doc->invoice->corporate->accountManager->department->name; break;
			case 'invClientCorpAccMngDeptCode':			$r=$doc->invoice->corporate->accountManager->department->code; break;

			// INVOICE CORPORATE MAIN OFFICE //////////////////////////////////////////////////////
			case 'invClientCorpMainOffName':			$r=$doc->invoice->corporate->mainOffice->name; break;
			case 'invClientCorpMainOffCountry':			$r=$doc->invoice->corporate->mainOffice->country->name; break;
			case 'invClientCorpMainOffCity':			$r=$doc->invoice->corporate->mainOffice->city->name; break;
			case 'invClientCorpMainOffAddress':			$r=$doc->invoice->corporate->mainOffice->address; break;
			case 'invClientCorpMainOffDeliveryAddress':
				$r=$doc->invoice->corporate->mainOffice->deliveryAddress;
				if (!$r) $r=$doc->invoice->corporate->mainOffice->address;
				break;
			case 'invClientCorpMainOffZipCode':			$r=$doc->invoice->corporate->mainOffice->zipCode; break;
			case 'invClientCorpMainOffTelephone':		$r=$doc->invoice->corporate->mainOffice->telephone; break;
			case 'invClientCorpMainOffFax':				$r=$doc->invoice->corporate->mainOffice->fax; break;
			case 'invClientCorpMainOffEmail':			$r=$doc->invoice->corporate->mainOffice->email; break;
			case 'invClientCorpMainOffBankName':
				$defaultBankAccount=$doc->invoice->corporate->mainOffice->defaultBankAccountDetails;
				if ($defaultBankAccount['idBank']) $r=\Entity\Bank::select($defaultBankAccount['idBank'])->name;
				if ($defaultBankAccount['idBankOffice']) $r.=' ('.\Entity\BankOffice::select($defaultBankAccount['idBankOffice'])->name.')';
				break;
			case 'invClientCorpMainOffBankAccount':
				$defaultBankAccount=$doc->invoice->corporate->mainOffice->defaultBankAccountDetails;
				$r=$defaultBankAccount['account'];
				break;
			case 'invClientCorpMainOffSwiftCode':
				$defaultBankAccount=$doc->invoice->corporate->mainOffice->defaultBankAccountDetails;
				if ($defaultBankAccount['idBankOffice']) $r=\Entity\BankOffice::select($defaultBankAccount['idBankOffice'])->swiftCode;
				break;



			// INVOICE CORPORATE CURRENT OFFICE DETAILS ///////////////////////////////////////////
			case 'invClientCorpCurrOffName':			$r=$doc->invoice->corporateOffice->name; break;
			case 'invClientCorpCurrOffCountry':			$r=$doc->invoice->corporateOffice->country->name; break;
			case 'invClientCorpCurrOffCity':			$r=$doc->invoice->corporateOffice->city->name; break;
			case 'invClientCorpCurrOffAddress':			$r=$doc->invoice->corporateOffice->address; break;
			case 'invClientCorpCurrOffDeliveryAddress':
				$r=$doc->invoice->corporateOffice->deliveryAddress;
				if (!$r) $r=$doc->invoice->corporateOffice->address;
				break;
			case 'invClientCorpCurrOffZipCode':			$r=$doc->invoice->corporateOffice->zipCode; break;
			case 'invClientCorpCurrOffTelephone':		$r=$doc->invoice->corporateOffice->telephone; break;
			case 'invClientCorpCurrOffFax':				$r=$doc->invoice->corporateOffice->fax; break;
			case 'invClientCorpCurrOffEmail':			$r=$doc->invoice->corporateOffice->email; break;
			case 'invClientCorpCurrOffBankName':
				$defaultBankAccount=$doc->invoice->corporateOffice->defaultBankAccountDetails;
				if ($defaultBankAccount['idBank']) $r=\Entity\Bank::select($defaultBankAccount['idBank'])->name;
				if ($defaultBankAccount['idBankOffice']) $r.=' ('.\Entity\BankOffice::select($defaultBankAccount['idBankOffice'])->name.')';
				break;
			case 'invClientCorpCurrOffBankAccount':
				$defaultBankAccount=$doc->invoice->corporateOffice->defaultBankAccountDetails;
				$r=$defaultBankAccount['account'];
				break;
			case 'invClientCorpCurrOffSwiftCode':
				$defaultBankAccount=$doc->invoice->corporateOffice->defaultBankAccountDetails;
				if ($defaultBankAccount['idBankOffice']) $r=\Entity\BankOffice::select($defaultBankAccount['idBankOffice'])->swiftCode;
				break;

			// INVOICE CUSTOMER NAMES /////////////////////////////////////////////////////////////
			case 'invClientCustName':					$r=$doc->invoice->customer->nameStandard; break;
			case 'invClientCustLastName':				$r=$doc->invoice->customer->lastNameStandard; break;
			case 'invClientCustMiddleName':				$r=$doc->invoice->customer->middleNameStandard; break;
			case 'invClientCustFirstName':				$r=$doc->invoice->customer->firstNameStandard; break;
			case 'invClientCustName2':					$r=$doc->invoice->customer->nameLocal; break;
			case 'invClientCustLastName2':				$r=$doc->invoice->customer->lastNameLocal; break;
			case 'invClientCustMiddleName2':			$r=$doc->invoice->customer->middleNameLocal; break;
			case 'invClientCustFirstName2':				$r=$doc->invoice->customer->firstNameLocal; break;
			case 'invClientCustName_alt':
				$r=$doc->invoice->customer->nameStandard;
				if (!$r) $r=$doc->invoice->customer->nameLocal;
				break;
			case 'invClientCustLastName_alt':
				$r=$doc->invoice->customer->lastNameStandard;
				if (!$r) $r=$doc->invoice->customer->lastNameLocal;
				break;
			case 'invClientCustMiddleName_alt':
				$r=$doc->invoice->customer->middleNameStandard;
				if (!$r) $r=$doc->invoice->customer->middleNameLocal;
				break;
			case 'invClientCustFirstName_alt':
				$r=$doc->invoice->customer->firstNameStandard;
				if (!$r) $r=$doc->invoice->customer->firstNameLocal;
				break;
			case 'invClientCustName2_alt':
				$r=$doc->invoice->customer->nameLocal;
				if (!$r) $r=$doc->invoice->customer->nameStandard;
				break;
			case 'invClientCustLastName2_alt':
				$r=$doc->invoice->customer->lastNameLocal;
				if (!$r) $r=$doc->invoice->customer->lastNameStandard;
				break;
			case 'invClientCustMiddleName2_alt':
				$r=$doc->invoice->customer->middleNameLocal;
				if (!$r) $r=$doc->invoice->customer->middleNameStandard;
				break;
			case 'invClientCustFirstName2_alt':
				$r=$doc->invoice->customer->firstNameLocal;
				if (!$r) $r=$doc->invoice->customer->firstNameStandard;
				break;


			// INVOICE CUSTOMER DETAILS ///////////////////////////////////////////////////////////
			case 'invClientCustId':						$r=$doc->invoice->idCustomer; break;
			case 'invClientCustTitle':					$r=$doc->invoice->customer->titleName; break;
			case 'invClientCustType':					$r=$doc->invoice->customer->typeName; break;
			case 'invClientCustCountry':				$r=$doc->invoice->customer->country->name; break;
			case 'invClientCustCity':					$r=$doc->invoice->customer->city->name; break;
			case 'invClientCustPNC':					$r=$doc->invoice->customer->PNC; break;
			case 'invClientCustIdSeries':				$r=$doc->invoice->customer->ID_series; break;
			case 'invClientCustIdNumber':				$r=$doc->invoice->customer->ID_number; break;
			case 'invClientCustIdReleaseBy':			$r=$doc->invoice->customer->ID_releaseBy; break;
			case 'invClientCustIdReleaseDateIso':		$r=$doc->invoice->customer->ID_releaseDate; break;
			case 'invClientCustIdReleaseDateStandard':	$r=\Engine\DateTime::isoToStd($doc->invoice->customer->ID_releaseDate); break;
			case 'invClientCustPassportNo':				$pass=$doc->invoice->customer->defaultPassport;$r=$pass['number']; break;
			case 'invClientCustPassportType':			$pass=$doc->invoice->customer->defaultPassport;$r=$pass['type']; break;
			case 'invClientCustPassportExpireDateIso':	$pass=$doc->invoice->customer->defaultPassport;$r=$pass['expireDate']; break;
			case 'invClientCustPassportExpireDateStandard': $pass=$doc->invoice->customer->defaultPassport;$r=\Engine\DateTime::isoToStd($pass['expireDate']); break;
			case 'invClientCustAddress':				$r=$doc->invoice->customer->address; break;
			case 'invClientCustDeliveryAddress':
				$r=$doc->invoice->customer->deliveryAddress;
				if (!$r) $r=$doc->invoice->customer->address;
				break;
			case 'invClientCustZipCode':				$r=$doc->invoice->customer->zipCode; break;
			case 'invClientCustBankName':				$r=$doc->invoice->customer->bank->name; break;
			case 'invClientCustBankAccount':			$r=$doc->invoice->customer->bankAccount; break;
			case 'invClientCustEmail':					$r=$doc->invoice->customer->email; break;
			case 'invClientCustTelephone':				$r=$doc->invoice->customer->telephone; break;
			case 'invClientCustFax':					$r=$doc->invoice->customer->fax; break;

			// INVOICE RESPONSIBLE USER ///////////////////////////////////////////////////////////
			case 'invRespUserName':						$r=$doc->invoice->user->name; break;
			case 'invRespUserLastName':					$r=$doc->invoice->user->lastName; break;
			case 'invRespUserFirstName':				$r=$doc->invoice->user->firstName; break;
			case 'invRespUserIdSeries':					$r=$doc->invoice->user->ID_series; break;
			case 'invRespUserIdNumber':					$r=$doc->invoice->user->ID_number; break;
			case 'invRespUserIdReleaseBy':				$r=$doc->invoice->user->ID_releaseBy; break;
			case 'invRespUserIdReleaseDateIso':			$r=$doc->invoice->user->ID_releaseDate; break;
			case 'invRespUserIdReleaseDateStandard':	$r=\Engine\DateTime::isoToStd($doc->invoice->user->ID_releaseDate); break;
			case 'invRespUserPNC':						$r=$doc->invoice->user->PNC; break;
			case 'invRespUserJob':						$r=$doc->invoice->user->job; break;
			case 'invRespUserEmail':					$r=$doc->invoice->user->email; break;
			case 'invRespUserTelephone':				$r=$doc->invoice->user->telephone; break;
			case 'invRespUserDeptName':					$r=$doc->invoice->user->department->name; break;
			case 'invRespUserDeptCode':					$r=$doc->invoice->user->department->code; break;

			// INVOICE RESPONSIBLE OFFICE /////////////////////////////////////////////////////////
			case 'invRespOffName':						$r=$doc->invoice->office->name; break;
			case 'invRespOfficeIataCode':				$r=implode(",", $doc->invoice->office->iataCode); break;
			case 'invRespOffAddress':					$r=$doc->invoice->office->address; break;
			case 'invRespOffDeliveryAddress':			$r=$doc->invoice->office->deliveryAddress; break;
			case 'invRespOffZipCode':					$r=$doc->invoice->office->zipCode; break;
			case 'invRespOffCountry':					$r=$doc->invoice->office->country->name; break;
			case 'invRespOffCity':						$r=$doc->invoice->office->city->name; break;
			case 'invRespOffTelephone':					$r=$doc->invoice->office->telephone; break;
			case 'invRespOffFax':						$r=$doc->invoice->office->fax; break;
			case 'invRespOffEmail':						$r=$doc->invoice->office->email; break;
			case 'invRespOffWeb':						$r=$doc->invoice->office->web; break;

			// INVOICE ADD USER ///////////////////////////////////////////////////////////////////
			case 'invAddUserName':						$r=$doc->invoice->addUser->name; break;
			case 'invAddUserLastName':					$r=$doc->invoice->addUser->lastName; break;
			case 'invAddUserFirstName':					$r=$doc->invoice->addUser->firstName; break;
			case 'invAddUserIdSeries':					$r=$doc->invoice->addUser->ID_series; break;
			case 'invAddUserIdNumber':					$r=$doc->invoice->addUser->ID_number; break;
			case 'invAddUserIdReleaseBy':				$r=$doc->invoice->addUser->ID_releaseBy; break;
			case 'invAddUserIdReleaseDateIso':			$r=$doc->invoice->addUser->ID_releaseDate; break;
			case 'invAddUserIdReleaseDateStandard':		$r=\Engine\DateTime::isoToStd($doc->invoice->addUser->ID_releaseDate); break;
			case 'invAddUserPNC':						$r=$doc->invoice->addUser->PNC; break;
			case 'invAddUserJob':						$r=$doc->invoice->addUser->job; break;
			case 'invAddUserEmail':						$r=$doc->invoice->addUser->email; break;
			case 'invAddUserTelephone':					$r=$doc->invoice->addUser->telephone; break;
			case 'invAddUserDeptName':					$r=$doc->invoice->addUser->department->name; break;
			case 'invAddUserDeptCode':					$r=$doc->invoice->addUser->department->code; break;

			// INVOICE ISSUE USER/TIME ////////////////////////////////////////////////////////////
			case 'invIssueUserName':					$r=$doc->invoice->issueUser->name; break;
			case 'invIssueUserLastName':				$r=$doc->invoice->issueUser->lastName; break;
			case 'invIssueUserFirstName':				$r=$doc->invoice->issueUser->firstName; break;
			case 'invIssueUserIdSeries':				$r=$doc->invoice->issueUser->ID_series; break;
			case 'invIssueUserIdNumber':				$r=$doc->invoice->issueUser->ID_number; break;
			case 'invIssueUserIdReleaseBy':				$r=$doc->invoice->issueUser->ID_releaseBy; break;
			case 'invIssueUserIdReleaseDateIso':		$r=$doc->invoice->issueUser->ID_releaseDate; break;
			case 'invIssueUserIdReleaseDateStandard':	$r=\Engine\DateTime::isoToStd($doc->invoice->issueUser->ID_releaseDate); break;
			case 'invIssueUserPNC':						$r=$doc->invoice->issueUser->PNC; break;
			case 'invIssueUserJob':						$r=$doc->invoice->issueUser->job; break;
			case 'invIssueUserEmail':					$r=$doc->invoice->issueUser->email; break;
			case 'invIssueUserTelephone':				$r=$doc->invoice->issueUser->telephone; break;
			case 'invIssueUserDeptName':				$r=$doc->invoice->issueUser->department->name; break;
			case 'invIssueUserDeptCode':				$r=$doc->invoice->issueUser->department->code; break;
			case 'invIssueTimestamp':					if ($doc->invoice->issueTime) $r=date(DATETIME_FORMAT, $doc->invoice->issueTime); break;


			// AGENCY MAIN BANK ACCOUNT ///////////////////////////////////////////////////////////
			case 'compBankMainAccAccount': case 'compBankMainAccBankName': case 'compBankMainAccBankCode': case 'compBankMainAccCurrency': case 'compBankMainAccSwiftCode':
				// determine either office bank account or the agency bank account (the office is the first choice)
				$idBa=$doc->invoice->office->idDefaultBankAccount;
				if (!$idBa) $idBa = \Entity\AgencyBankAccount::getDefaultId();
				if ($idBa) $r = \Entity\AgencyBankAccount::select($idBa)->account;
				// fill in the fields if some bank account found
				if ($idBa) {
					if ($field=='compBankMainAccAccount') 					$r = \Entity\AgencyBankAccount::select($idBa)->account;
					elseif ($field=='compBankMainAccBankName') {
						$r = \Entity\AgencyBankAccount::select($idBa)->bank->name;
						if (\Entity\AgencyBankAccount::select($idBa)->idBankOffice) $r.=' ('.\Entity\AgencyBankAccount::select($idBa)->bankOffice->name.')';
					} elseif ($field=='compBankMainAccBankCode') 			$r = \Entity\AgencyBankAccount::select($idBa)->bank->code;
					elseif ($field=='compBankMainAccCurrency') 				$r = \Entity\AgencyBankAccount::select($idBa)->currency;
					elseif ($field=='compBankMainAccSwiftCode') 			$r = \Entity\AgencyBankAccount::select($idBa)->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;

	}


}

