<?php

namespace Modules\RemoteData\InfoBox;


class View_Supplier extends \App\Mvc\View_RemoteDataInfoBoxEntityAbstract
{

	/**
	 * (non-PHPdoc)
	 * @see \App\Mvc\View_RemoteDataInfoBoxEntityAbstract::getEntityRemoteDetails()
	 */
	protected function getEntityRemoteDetails(\App\Entity\SelectAbstract $entity)
	{

		// load global config
		$CFG = \Engine\Loader_Cfg::getInstance()->getGlobal();
		$_LANG = \App\Loader_Lang::getInstance()->getAll();

		$defCurr = \App\Config\Currencies::getInstance()->getDefaultCurrency();

		// the output object
		$output = \Engine\RemoteData\InfoBox\Container_Main::create( $entity->alias )
			->setShortTitle($entity->alias)
			->setId(sprintf("%s.%s", __CLASS__, $entity->getId()));

		// check access to remote details option
		if (!\App\UserAccess\CurrentUserAccessFast::getInstance()->hasAccessToOption('Modules/Partners/Suppliers-remoteDetails')) {
			$output->addField(\Engine\RemoteData\InfoBox\Field_String::create(null, $_LANG['noAccessToOption']));
			return $output;
		}

		// supplier update button
		if (\App\UserAccess\CurrentUserAccessFast::getInstance()->hasAccessToOption('Modules/Partners/Suppliers-update')) {
			$icon = \Engine\RemoteData\InfoBox\Icon\Icon::create('edit');
			$icon->setTooltip($this->lang['supplier']['option']['update']);
			$icon->setJsCallback("supplierUpdate('{$entity->getId()}');");
			$output->addIcon($icon);
		}


		// BASIC INFO -----------------------------------------------------------------------------
		$cont1 = \Engine\RemoteData\InfoBox\Container_Simple::create($this->lang['general']['titles']['general']);
		$output->addContainer($cont1);

		$cont1->addField( \Engine\RemoteData\InfoBox\Field_String::create($this->lang['general']['fields']['id'], $entity->getId()) );
		$cont1->addField( \Engine\RemoteData\InfoBox\Field_String::create($this->lang['supplier']['fields']['alias'], $entity->alias) );
		$cont1->addField( \Engine\RemoteData\InfoBox\Field_String::create($this->lang['supplier']['fields']['company'], $entity->companyFull) );
		$cont1->addField( \Engine\RemoteData\InfoBox\Field_String::create($this->lang['supplier']['fields']['trust'], $entity->trust->name) );
		if ($entity->locked) {
			$cont1->addField( \Engine\RemoteData\InfoBox\Field_String::create($this->lang['supplier']['fields']['locked'], $_LANG['yes']) );
			$cont1->addField( \Engine\RemoteData\InfoBox\Field_String::create($this->lang['supplier']['fields']['lockedComments'], $entity->lockedComments) );
		}
		if ($entity->idRealSupplier) {
			$cont1->addField( \App\RemoteData\InfoBox\Field_EntityInfoBox::create($this->lang['supplier']['fields']['idRealSupplier'], \Entity\Supplier::select($entity->idRealSupplier)->alias)->setEntity('supplier', $entity->idRealSupplier) );
		}

		if ($entity->duplicatedSuppliersList){
			$cont7 = \Engine\RemoteData\InfoBox\Container_Simple::create($this->lang['supplier']['titles']['duplicatedSuppliers'])->setHidden(true);
			$output->addContainer($cont7);
			foreach ($entity->duplicatedSuppliersList as $idSupplier) {
				$cont7->addField( \App\RemoteData\InfoBox\Field_EntityInfoBox::create(null, \Entity\Supplier::select($idSupplier)->alias)->setEntity('supplier', $idSupplier) );
			}
		}

		// LOCATION
		$cont2 = \Engine\RemoteData\InfoBox\Container_Simple::create($this->lang['general']['titles']['contact']);
		$output->addContainer($cont2);
		$cont2->addField( \Engine\RemoteData\InfoBox\Field_String::create($this->lang['supplier']['fields']['country'], \Entity\Country::select($entity->countryCode)->name) );
		$cont2->addField( \App\RemoteData\InfoBox\Field_EntityInfoBox::create($this->lang['supplier']['fields']['city'], \Entity\City::select($entity->cityId)->name)->setEntity('city', $entity->cityId) );
		$cont2->addField( \Engine\RemoteData\InfoBox\Field_String::create($this->lang['supplier']['fields']['address'],  $entity->address) );
		// composedAddress
		if ($entity->composedAddress) {
			$cont2->addField( \Engine\RemoteData\InfoBox\Field_String::create($this->lang['supplier']['fields']['mainAddressDetails'], $entity->composedAddress));
		}
		$cont2->addField( \Engine\RemoteData\InfoBox\Field_String::create($this->lang['supplier']['fields']['deliveryAddress'],  $entity->deliveryAddress) );
		// composedDeliveryAddress
		if ($entity->composedDeliveryAddress) {
			$cont2->addField( \Engine\RemoteData\InfoBox\Field_String::create($this->lang['supplier']['fields']['addressDeliveryDetails'], $entity->composedDeliveryAddress));
		}
		$cont2->addField( \Engine\RemoteData\InfoBox\Field_String::create($this->lang['supplier']['fields']['zipCode'],  $entity->zipCode) );
		$cont2->addField( \Engine\RemoteData\InfoBox\Field_Telephone::create($this->lang['supplier']['fields']['telephone'],  $entity->telephone) );
		$cont2->addField( \Engine\RemoteData\InfoBox\Field_String::create($this->lang['supplier']['fields']['fax'],  $entity->fax) );
		$cont2->addField( \Engine\RemoteData\InfoBox\Field_Email::create($this->lang['supplier']['fields']['email'],  $entity->email) );
		if ($entity->website) {
			$cont2->addField( \Engine\RemoteData\InfoBox\Field_Url::create($this->lang['supplier']['fields']['website'],  ['label'=>$entity->website, 'value'=>$entity->website]) );
		}

		// FISCAL INFO
		$cont3 = \Engine\RemoteData\InfoBox\Container_Simple::create($this->lang['supplier']['titles']['fiscalInfo']);
		$output->addContainer($cont3);
		$cont3->addField( \Engine\RemoteData\InfoBox\Field_String::create($this->lang['supplier']['fields']['fiscalCode'],  $entity->fiscalCode) );
		$cont3->addField( \Engine\RemoteData\InfoBox\Field_String::create($this->lang['supplier']['fields']['regNo'],  $entity->regNo) );
		if ($entity->vatRegistered) {
			$cont3->addField( \Engine\RemoteData\InfoBox\Field_String::create($this->lang['supplier']['fields']['vatRegistered'], $entity->vatRegistered) );
		}
		$cont3->addField( \Engine\RemoteData\InfoBox\Field_String::create($this->lang['supplier']['fields']['vatCertificateNumber'], $entity->vatCertificateNumber) );
		if ($entity->paymentPossibilitiesNames) {
			$cont3->addField( \Engine\RemoteData\InfoBox\Field_String::create($this->lang['supplier']['fields']['paymentPossibilities'], implode(", ", $entity->paymentPossibilitiesNames)) );
		}

		if ($entity->incomeTaxPayer) {
			$cont3->addField(\Engine\RemoteData\InfoBox\Field_String::create($this->lang['supplier']['fields']['incomeTaxPayer'], $_LANG['yes']));
		}

		if ($entity->bankAccounts) {
			$accounts = '';
			foreach ($entity->bankAccounts as $account) {
				//$accounts .= print_r($account, 1);
				$accounts .= $account['account'];
				if ($account['idBank']) {
					$accounts .= sprintf("- %s", \Entity\Bank::select($account['idBank'])->name);
				}
				if ($account['idBankOffice']) {
					$accounts .= sprintf("(%s)", \Entity\BankOffice::select($account['idBankOffice'])->name);
				}
				if ($account['currency']) {
					$accounts .= sprintf(" [%s]", $account['currency']);
				}
				$accounts .= "\n";
			}
			$cont3->addField( \Engine\RemoteData\InfoBox\Field_MultiLine::create($this->lang['supplier']['fields']['accounts'], $accounts) );
		}

		if ($entity->equivDebitBalance) {
			$equivBalance = ['amount'=>$entity->equivDebitBalance, 'currency'=>$defCurr, 'displayCurrency'=>sprintf("%s (%s)", $defCurr, $this->lang['supplier']['fields']['equivalent'])];
			$cont3->addField( \Engine\RemoteData\InfoBox\Field_Price::create($this->lang['supplier']['fields']['debitBalance'], $equivBalance) );

			$c3 = \Engine\RemoteData\InfoBox\Container_Simple::create();
			$cont3->addContainer($c3);

			$c3->setCollapsible(false);
			$c3->setType('section');

			foreach ($entity->debitBalanceDetails as $currency=>$value) {
				$c3->addField(  \Engine\RemoteData\InfoBox\Field_Price::create($currency, ['amount'=>$value, 'currency'=>$currency]));
			}
		}

		// credit balance
		if ($entity->equivCreditBalance) {
			$equivCreditBalance = ['amount'=>$entity->equivCreditBalance, 'currency'=>$defCurr, 'displayCurrency'=>sprintf("%s (%s)", $defCurr, $this->lang['corporate']['fields']['equivalent'])];
			$cont3->addField( \Engine\RemoteData\InfoBox\Field_Price::create($this->lang['supplier']['fields']['creditBalance'], $equivCreditBalance) );

			$c4 = \Engine\RemoteData\InfoBox\Container_Simple::create();
			$cont3->addContainer($c4);

			$c4->setCollapsible(false);
			$c4->setType('section');

			foreach ($entity->creditBalanceDetails as $currency=>$value) {
				$c4->addField(  \Engine\RemoteData\InfoBox\Field_Price::create($currency, ['amount'=>$value, 'currency'=>$currency]));
			}
		}

		// SUPPLIER CODES
		$cont4 = \Engine\RemoteData\InfoBox\Container_Simple::create($this->lang['supplier']['titles']['codes']);
		$output->addContainer($cont4);
		if ($entity->codes) {
			foreach ($entity->codes as $key=>$code) {
				$cont4->addField( \Engine\RemoteData\InfoBox\Field_String::create($CFG['travel']['standardCodes'][$key], $code) );
			}
		}

		// GROUPS INFO ----------------------------------------------------------------------------
		if ($entity->idGroupElements) {
			$contGr = \Engine\RemoteData\InfoBox\Container_Simple::create($this->lang['general']['titles']['groups'])->setHidden(true);
			$output->addContainer($contGr);
			foreach ($entity->idGroupElements as $idGroupElem) {
				$groupElemDetails = \App\Config\SupplierGroups::getInstance()->getGroupElementDetailsById($idGroupElem);
				$contGr->addField( \Engine\RemoteData\InfoBox\Field_String::create($groupElemDetails['groupName'], $groupElemDetails['groupElementName']) );
			}
		}

		// MISC INFO ------------------------------------------------------------------------------
		$cont5 = \Engine\RemoteData\InfoBox\Container_Simple::create($this->lang['general']['titles']['misc'])->setHidden(true);
		$output->addContainer($cont5);
		$cont5->addField( \Engine\RemoteData\InfoBox\Field_MultiLine::create($this->lang['general']['fields']['comments'], $entity->comments) );
		$cont5->addField( \Engine\RemoteData\InfoBox\Field_Remarks::create($this->lang['general']['fields']['remarks'], $entity->remarks) );

		// PROCESSED REMARKS
		$cRmk = \Engine\RemoteData\InfoBox\Container_ProcessedRemarks::create($this->lang['general']['fields']['processedRemarks'])->setHidden(true);
		$cRmk->addRemarkFields('supplier', $entity->processedRemarks);
		$output->addContainer($cRmk);


		// UPDATE INFO
		$cont6 = \Engine\RemoteData\InfoBox\Container_Simple::create($this->lang['general']['titles']['updates'])->setHidden(true);
		$output->addContainer($cont6);

		$cont6->addField( \Engine\RemoteData\InfoBox\Field_DateTime::create($this->lang['general']['fields']['addTime'], \Engine\DateTime::createFromTimestamp($entity->addTime)) );
		$cont6->addField( \App\RemoteData\InfoBox\Field_EntityInfoBox::create($this->lang['general']['fields']['addUser'], $entity->addUser->name)->setEntity('user', $entity->idAddUser) );
		$cont6->addField( \Engine\RemoteData\InfoBox\Field_DateTime::create($this->lang['general']['fields']['updateTime'], \Engine\DateTime::createFromTimestamp($entity->lastUpdateTime)) );
		$cont6->addField( \App\RemoteData\InfoBox\Field_EntityInfoBox::create($this->lang['general']['fields']['updateUser'], $entity->updateUser->name)->setEntity('user', $entity->idUpdateUser) );
		if ($entity->updatesCount) {
			$cont6->addField( \Engine\RemoteData\InfoBox\Field_String::create($this->lang['general']['fields']['updatesCount'], $entity->updatesCount) );
		}

		return $output;

	}

}
