<?php
namespace App\Galileo\StructureParser\Structures\LCCTicketing;

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

		$priceHelper = new \ModulesAbstract\PluginReports\Report\Model\Builder\Helpers\Prices();

		$A14 = 				$parsedStructure['A14_LCC'];
		$A14Service = 		$this->getA14Segment($parsedStructure, $transactionIndexes);
		$A14PassengerFare = $this->getA14PassengerFare($A14Service, $parsedStructure, $transactionIndexes);
		$A14Ancillaries = 	$this->getA14AncillariesLCC($A14Service, $parsedStructure, $transactionIndexes);
		$A14SSR = 			$this->getA14SSR($A14Service, $parsedStructure, $transactionIndexes);

		$item = $this->buildPriceObj();

		foreach ($A14PassengerFare as $A14PF) {
			$item->componentAddAndUpdateTotal(new \App\Utils\BaseParser\Structure\StructureParser\Types\PriceComponent(
				'supplier',
				'LCC:PassengerFare:baseFare',
				$A14PF['baseFare'],
				null,
				$parsedStructure['A14_LCC']['REF']['currency']
			));
		}

		// NOTE: these could also be loaded from PassengerTaxLCC, not sure which one is correct
		foreach ($A14PassengerFare as $A14PF) {
			if ($A14PF['tax']) {
				$item->tax->append($this->buildTaxComponentObj(
					'baseFare',
					$A14PF['tax'],
					$parsedStructure['A14_LCC']['REF']['currency']
				));
			}
		}

		foreach ($A14Ancillaries as $A14OA) {
			if ($A14OA['Ancillaries']['unitCost']) {
				$item->tax->append($this->buildTaxComponentObj(
					($A14OA['Ancillaries']['serviceCode'] && $A14OA['Ancillaries']['serviceCode2']
						? sprintf("%s:%s", $A14OA['Ancillaries']['serviceCode'], $A14OA['Ancillaries']['serviceCode2'])
						: ($A14OA['Ancillaries']['serviceCode']
							? $A14OA['Ancillaries']['serviceCode']
							: $A14OA['Ancillaries']['type_']
						)),
					$A14OA['Ancillaries']['unitCost'] * $A14OA['Ancillaries']['serviceQuantity'],
					$parsedStructure['A14_LCC']['REF']['currency']
				));
			}
		}

		foreach ($A14SSR as $A14SSR) {
			if ($A14SSR['SSR']['unitCost']) {
				$item->tax->append($this->buildTaxComponentObj(
					$A14SSR['SSR']['SSR'],
					$A14SSR['SSR']['unitCost'],
					$parsedStructure['A14_LCC']['REF']['currency']
				));
			}
		}

		$item->componentAddAndUpdateTotal(new \App\Utils\BaseParser\Structure\StructureParser\Types\PriceComponent(
			'serviceFee',
			'LCC:Fees:distribuited',
			$priceHelper->splitAmount($A14['Fees']['baseFare'], $transactionIndexes['kpfTotal'], $transactionIndexes['kpf'], 2),
			null,
			$parsedStructure['A14_LCC']['REF']['currency']
		));

		$item->recalculateTotal($this->transaction);

		$item = $this->populate_commissionFromRemarks($item, $parsedStructure);
		$item = $this->internal_add_commissionVat($item);

		$item = $this->populate_formOfPaymentLCC($item, $parsedStructure, $transactionIndexes, $A14);

		return $item;
	}

	protected function populate_formOfPaymentLCC($item, $parsedStructure, $transactionIndexes, $A14)
	{
		$fop = new \App\Apollo\StructureParser\Types\PriceFormOfPaymentComponent();
		$fop->type = 	$A14['FormOfPayment']['type_'];
		$fop->amount = 	$A14['FormOfPayment']['paymentTotal'];

		if ($fop->type=='CC') {
			$fop->ccCompanyIndicator = 	$A14['FormOfPayment']['CC_'][0]['company'];
			$fop->ccNumber = 			$A14['FormOfPayment']['CC_'][0]['number'];
		}

		$item->formOfPayment->append($fop);

		return $item;
	}

}
