<?php

namespace Modules\RemoteData\InfoBox;

class View_SupplierInvoice extends \App\Mvc\View_RemoteDataInfoBoxEntityAbstract
{

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

		// the output object
		$output = \Engine\RemoteData\InfoBox\Container_Main::create($this->lang['supplierInvoice']['title'])
			->setShortTitle($entity->getId())
			->setId(sprintf("%s.%s", __CLASS__, $entity->getId()));

		// view invoice
		$icon = \Engine\RemoteData\InfoBox\Icon\Icon::create('view');
		$icon->setJsCallback("supplierInvoiceView({$entity->getId()})");
		$icon->setTooltip($this->lang['invoice']['options']['view']);
		$output->addIcon($icon);

		if (\App\UserAccess\CurrentUserAccessFast::getInstance()->hasAccessToSection('Modules/Invoices/SupplierInvoices')) {
			$icon = \Engine\RemoteData\InfoBox\Icon\Icon::create('filter');
			$icon->setJsCallback("window.open(MVC.appUrl('Invoices/SupplierInvoices', null, {'filters[id]': {$entity->getId()}, 'filters[invoiceDate][start]': '{$entity->invoiceDate}', 'filters[invoiceDate][end]': '{$entity->invoiceDate}', 'filters[user][id]': '',  'listing[reset]': '1' , 'listing[paramsSlot]':'1'} ), '_blank');");
			$icon->setTooltip($this->lang['supplierInvoice']['options']['filter']);
			$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_Date::create($this->lang['supplierInvoice']['fields']['invoiceDate'], \Engine\DateTime::createFromIsoDate($entity->invoiceDate)) );
		$cont1->addField( \App\RemoteData\InfoBox\Field_EntityInfoBox::create($this->lang['supplierInvoice']['fields']['supplier'], $entity->supplier->alias)->setEntity('supplier', $entity->idSupplier) );
		$cont1->addField( \Engine\RemoteData\InfoBox\Field_String::create($this->lang['supplierInvoice']['fields']['seriesNumber'], $entity->invoiceNumber . ($entity->invoiceSeries?' / '.$entity->invoiceSeries:'')) );

		// EXTRA INFO
		$cont2 = \Engine\RemoteData\InfoBox\Container_Simple::create($this->lang['supplierInvoice']['titles']['extraInfo']);
		$output->addContainer($cont2);

		if ($entity->statusOp == 'confirmed') {
			if ($entity->statusFin =='n')
				$status = $this->lang['supplierInvoice']['fields']['confirmed|n'];
			elseif ($entity->statusFin == 'p')
				$status = $this->lang['supplierInvoice']['fields']['confirmed|p'];
			elseif ($entity->statusFin == 't')
				$status = $this->lang['supplierInvoice']['fields']['confirmed|t'];
			else
				$status = $this->lang['supplierInvoice']['fields']['confirmed|'];
		}
		elseif ($entity->statusOp == 'notConfirmed'){
			$status = $this->lang['supplierInvoice']['fields']['notConfirmed'];
		}
		if ($status) {
			$cont2->addField( \Engine\RemoteData\InfoBox\Field_String::create($this->lang['supplierInvoice']['fields']['status'], $status) );
		}
		$cont2->addField( \Engine\RemoteData\InfoBox\Field_String::Create($this->lang['supplierInvoice']['fields']['formOfPayment'], $entity->formOfPayment) );
		$cont2->addField( \Engine\RemoteData\InfoBox\Field_Date::create($this->lang['supplierInvoice']['fields']['dueDate'], \Engine\DateTime::createFromIsoDate($entity->dueDate)) );
		$cont2->addField( \Engine\RemoteData\InfoBox\Field_Price::create($this->lang['supplierInvoice']['fields']['totalValue'], ['amount'=>sprintf("%s",$entity->totalValue),'currency'=>$entity->currency]) );
		if ($entity->payedValue) {
			$cont2->addField( \Engine\RemoteData\InfoBox\Field_Price::create($this->lang['supplierInvoice']['fields']['payedValue'], ['amount'=>sprintf("%s",$entity->payedValue),'currency'=>$entity->currency]) );
		}
		$cont2->addField( \App\RemoteData\InfoBox\Field_EntityInfoBox::create($this->lang['supplierInvoice']['fields']['idUser'], $entity->user->name)->setEntity('user', $entity->idUser) );
		$cont2->addField( \App\RemoteData\InfoBox\Field_EntityInfoBox::create($this->lang['supplierInvoice']['fields']['idOffice'], $entity->office->name)->setEntity('user', $entity->idOffice) );

		if ($entity->blockable) {
			$blockable = $this->lang['supplierInvoice']['fields']['yes'];
		}
		else {
			$blockable = $this->lang['supplierInvoice']['fields']['no'];
		}

		$cont2->addField( \Engine\RemoteData\InfoBox\Field_String::create($this->lang['supplierInvoice']['fields']['blockable'], $blockable) );
		$cont2->addField( \App\RemoteData\InfoBox\Field_EntityInfoBox::create($this->lang['supplierInvoice']['fields']['idBlockUser'],  \Entity\User::select($entity->idBlockUser)->name)->setEntity('user', $entity->idBlockUser) );
		$cont2->addField( \Engine\RemoteData\InfoBox\Field_DateTime::create($this->lang['supplierInvoice']['fields']['blockTime'],  \Engine\DateTime::createFromTimestamp($entity->blockTime)) );


		// MISC INFO ------------------------------------------------------------------------------
		$cont5 = \Engine\RemoteData\InfoBox\Container_Simple::create($this->lang['general']['titles']['misc'])->setHidden(true);
		$output->addContainer($cont5);
		$cont2->addField( \Engine\RemoteData\InfoBox\Field_MultiLine::create($this->lang['general']['fields']['comments'], $entity->invoiceComments) );
		$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('supplierInvoice', $entity->processedRemarks);
		$output->addContainer($cRmk);


		// FILES ----------------------------------------------------------------------------------
		$cont6 = \Engine\RemoteData\InfoBox\Container_Simple::create($this->lang['general']['titles']['files']);
		$cont6->setType('ol');
		$cont6->setCollapsible(true);
		$cont6->setHidden(true);
		$output->addContainer($cont6);

		// get the "related" file list. put the current file on the top file list
		foreach ($entity->filesList as $file) {
			$url = \Engine\Mvc\Dispatcher::getInstance()->getAppUrl( 'Modules/Invoices/SupplierInvoices/Files', 'downloadFile', ['idSuppInv'=>$entity->getId(), 'file'=>urlencode($file)] );
			$fld = \Engine\RemoteData\InfoBox\Field_Url::create( '', [ 'label' => $file, 'value' => $url ] );
			$cont6->addField($fld);
		}


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

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

		return $output;

	}

}
