<?php
namespace App\Amadeus\StructureParser\Structures\Transfer;

class Price extends \App\Amadeus\StructureParser\Parser\Price
{
	/**
	 * (non-PHPdoc)
	 * @see \App\Amadeus\StructureParser\Parser\Price::passenger()
	 */
	public function passenger()
	{
		if($this->CACHE[__METHOD__]) return $this->CACHE[__METHOD__];

		$parsedStructure = $this->transaction->getParsedStructure();
		$indexType = $this->transaction->getIndexType();
		$index = $this->transaction->getIndex();
		$U = $parsedStructure['U'][$index->segment];

		if($indexType=='U' && $U['_type']=='17'){
			$price = $this->passenger_U_17();
			if($price){
				$price->checkIntegrity($this->transaction);
				$this->CACHE[__METHOD__] = $price;
				return $price;
			}
		}

		return null;
	}


	/**
	 * Extract data from a specific line
	 * @return NULL
	 */
	protected function passenger_U_17()
	{
		$parsedStructure = $this->transaction->getParsedStructure();
		$index = $this->transaction->getIndex();

		$item = null;
		$U = $parsedStructure['U'][$index->segment];

		if ($U) {
			$item = new \App\Amadeus\StructureParser\Types\Price();

			// extract supplier price
			if ($U['FREE_']['transferPrice']) {
				$item->componentSet(new \App\Amadeus\StructureParser\Types\PriceComponent(
					'supplier',
					'U_17:FREE_:transferPrice',
					$U['FREE_']['transferPrice'],
					null,
					$U['FREE_']['transferPriceCurrency']
				));
			}

			if ($U['FREE_']['taxAmount']) {
				$item->tax->insert(new \App\Amadeus\StructureParser\Types\PriceTaxComponent($U['FREE_']['taxAmount'], $U['FREE_']['taxCurrency'], 'default'));
			}

			$item->total = 			$item->calculateTotal();
			$item->totalCurrency = 	$item->supplierCurrency;

			if (!$item->formOfPayment->hasData()) {
				$item = $this->internal_getFormOfPaymentFromRIU($parsedStructure, $index, $item);
			}

			// extract commission
			if (in_array(\App\Amadeus\Config::getInstance()->get("importCommissionForService"), ['all',  ])) {
				$item = $this->internal_extractCommission($item, $parsedStructure, $index, []);
			}

		}

		return $item;
	}
}
