<?php

namespace Modules\RemoteData\InfoBox;


class View_BspPeriod 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( sprintf($this->lang['bspPeriod']['title'], $entity->getId()) )
			->setShortTitle($entity->getId())
			->setId(sprintf("%s.%s", __CLASS__, $entity->getId()));


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

		$c1->addField( \Engine\RemoteData\InfoBox\Field_String::create($this->lang['general']['fields']['id'], $entity->getId()) );
		$c1->addField( \Engine\RemoteData\InfoBox\Field_DateInterval::create($this->lang['bspPeriod']['fields']['period'], [
			'start'=> 	\Engine\DateTime::createFromIsoDate($entity->periodStart),
			'end'=> 	\Engine\DateTime::createFromIsoDate($entity->periodEnd),
		]) );
		$c1->addField( \Engine\RemoteData\InfoBox\Field_String::create($this->lang['bspPeriod']['fields']['filename'], $entity->filename) );
		$c1->addField( \App\RemoteData\InfoBox\Field_EntityInfoBox::create($this->lang['bspPeriod']['fields']['idOffice'], sprintf("%s(%s)", $entity->office->name, $entity->iataCode))->setEntity('office', $entity->idOffice) );

		$stats = $this->getStats($entity);
		$c2 = \Engine\RemoteData\InfoBox\Container_Simple::create($this->lang['bspPeriod']['titles']['priceDetails']);
		$output->addContainer($c2);

		$c2->addField( \Engine\RemoteData\InfoBox\Field_Price::create($this->lang['bspPeriod']['fields']['priceBalancePayable'],
			[
				'amount'=>$stats['priceBalancePayableAmount'],
				'currency'=>$stats['priceBalancePayableCurrency'],
			]));
		$c2->addField( \Engine\RemoteData\InfoBox\Field_Price::create($this->lang['bspPeriod']['fields']['priceSupplierCash'],
			[
				'amount'=>$stats['priceSupplierCashAmount'],
				'currency'=>$stats['priceSupplierCashCurrency'],
			]));
		$c2->addField( \Engine\RemoteData\InfoBox\Field_Price::create($this->lang['bspPeriod']['fields']['priceSupplierCc'],
			[
				'amount'=>$stats['priceSupplierCcAmount'],
				'currency'=>$stats['priceSupplierCcCurrency'],
			]));
		$c2->addField( \Engine\RemoteData\InfoBox\Field_Price::create($this->lang['bspPeriod']['fields']['priceTaxCash'],
			[
				'amount'=>$stats['priceTaxCashAmount'],
				'currency'=>$stats['priceTaxCashCurrency'],
			]));
		$c2->addField( \Engine\RemoteData\InfoBox\Field_Price::create($this->lang['bspPeriod']['fields']['priceTaxCc'],
			[
				'amount'=>$stats['priceTaxCcAmount'],
				'currency'=>$stats['priceTaxCcCurrency'],
			]));
		$c2->addField( \Engine\RemoteData\InfoBox\Field_Price::create($this->lang['bspPeriod']['fields']['priceCommission'],
			[
				'amount'=>$stats['priceCommissionAmount'],
				'currency'=>$stats['priceCommissionCurrency'],
			]));
		$c2->addField( \Engine\RemoteData\InfoBox\Field_Price::create($this->lang['bspPeriod']['fields']['priceCommissionVat'],
			[
				'amount'=>$stats['priceCommissionVatAmount'],
				'currency'=>$stats['priceCommissionVatCurrency'],
			]));


		// 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::createFromIsoDatetime($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::createFromIsoDatetime($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;

	}


	protected function getStats(\App\Entity\SelectAbstract $entity)
	{
		global $CFG;
		// extract file info, return fields that will get pre-filled in the next step, or an error message, if the file isn't valid
		$parserFactory = \App\Bsp\Parser\BspTraverserFactory::getInstance();
		$traverser = $parserFactory->getParser($CFG['paths']['bsp'].$entity->filename, $entity->fileType);

		$stats = [];
		if (!$traverser->hasContent()) {
			return $stats;
		}

		$traverser->setOption('taxManipulationsMatrix', unserialize(@gzdecode(base64_decode(\App\Bsp\Config::getInstance()->get("taxManipulationsMatrix")))));

		$stats['fileDetails'] = $traverser->getDetails();

		while ($bspTicket = $traverser->next()) {
			$stats['rows']++;
			$stats['priceBalancePayableAmount']+= $bspTicket->priceBalancePayable->amount;
			$stats['priceBalancePayableCurrency'] = $bspTicket->priceBalancePayable->currency;

			$stats['priceSupplierCashAmount']+= $bspTicket->priceSupplierCash->amount;
			$stats['priceSupplierCashCurrency'] = $bspTicket->priceSupplierCash->currency;

			$stats['priceSupplierCcAmount']+= $bspTicket->priceSupplierCc->amount;
			$stats['priceSupplierCcCurrency'] = $bspTicket->priceSupplierCc->currency;

			$stats['priceTaxCashAmount']+= $bspTicket->priceTaxCash->amount;
			$stats['priceTaxCashCurrency'] = $bspTicket->priceTaxCash->currency;

			$stats['priceTaxCcAmount']+= $bspTicket->priceTaxCc->amount;
			$stats['priceTaxCcCurrency'] = $bspTicket->priceTaxCc->currency;

			$stats['priceCommissionAmount']+= $bspTicket->priceCommission->amount;
			$stats['priceCommissionCurrency'] = $bspTicket->priceCommission->currency;

			$stats['priceCommissionVatAmount']+= $bspTicket->priceCommissionVat->amount;
			$stats['priceCommissionVatCurrency'] = $bspTicket->priceCommissionVat->currency;
		}

		return $stats;
	}
}
