<?php
namespace App\Sabre\StructureParser\Structures\DocumentIssue;

class Price extends \App\Sabre\StructureParser\Structures\Base\Price
{
	protected function _getData()
	{
		list($parsedStructure, , $transactionIndexes, ) = $this->getTransactionDetails();

		$item = new \App\Sabre\StructureParser\Types\Price();

		$M5 = $parsedStructure['M5'][$transactionIndexes['km5']];

		$currency = $this->determineCurrency($parsedStructure, [$M5], [], null, []);

		$item->componentSet(new \App\Utils\BaseParser\Structure\StructureParser\Types\PriceComponent(
			'total',
			'M5:baseFareAmount-tax1Amount',
			$M5['baseFareAmount'] + $M5['tax1Amount'],
			null,
			$currency
		));

		$item->componentSet(new \App\Utils\BaseParser\Structure\StructureParser\Types\PriceComponent(
			'supplier',
			'M5:baseFareAmount-commissionValue',
			$M5['baseFareAmount'] - $M5['commissionValue'],
			null,
			$currency
		));

		if ($M5['commissionValue']) {
			$item->componentSet(new \App\Utils\BaseParser\Structure\StructureParser\Types\PriceComponent(
				'commission',
				'M5:commissionValue',
				$M5['commissionValue'],
				null,
				$currency
			));
		}

		if ($M5['tax1Amount']) {
			$item->tax->append(new \App\Sabre\StructureParser\Types\PriceTaxComponent(
				'default',
				$M5['tax1Amount'],
				$currency
			));
		}
		if ($M5['tax2Amount']) {
			$item->tax->append(new \App\Sabre\StructureParser\Types\PriceTaxComponent(
				'vat',
				$M5['tax2Amount'],
				$currency
			));
		}

		if (in_array(\App\Sabre\Config::getInstance()->get("importCommissionForService"), ['all', ])) {
			$item = $this->internal_extract_commission($item, [$M5,], []);
		}

		$fop = new \App\Sabre\StructureParser\Types\PriceFormOfPaymentComponent();
		$fop->type = $M5['formOfPayment_']['type'];
		$item->formOfPayment->append($fop);

		return $item;
	}
}
