<?php
namespace App\Worldspan\StructureParser\Structures\RefundRaw;

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

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

		$RJ = $parsedStructure['passenger'][$transactionIndexes['paxIdx']]['RJ'];

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

		$item->total = 				-1*($RJ['TotalRefundAmount']);
		$item->totalCurrency = 		($RJ['CurrencyCodeOfTotalFare']?:$currency);

		$item->supplier = 			-1*($RJ['BaseFareAmountOfRefund']);
		$item->supplierCurrency = 	($RJ['CurrencyCode']?:$currency);

		$item->penalty = 			1*($RJ['AdminPenaltyFeeAmount']);
		$item->penaltyCurrency = 	$item->supplierCurrency;
		$item->total+= 				$item->penalty;

		// NOTE: apparently Worldspan doesn't specify refunded commission

		/*
		 * this method of caclulating the taxes fails for RO/hotelcon/17324638.prt, from feb 2017.
		 * */
// 		for ($i=1; $i<=20; $i++) {
// 			$k = sprintf('%02d', $i);
// 			if ($RJ["Tax{$k}Code"]) {
// 				$item->tax->add(new \App\Worldspan\StructureParser\Types\PriceTaxComponent($RJ["Tax{$k}Code"], -1*$RJ["Tax{$k}RefundAmount"], $RJ["CurrencyCode"]));
// 			}
// 		}

		if ($item->totalCurrency==$item->supplierCurrency && $item->totalCurrency==$item->penaltyCurrency ) {
			$item->tax->add(new \App\Worldspan\StructureParser\Types\PriceTaxComponent('custom', ($item->total - $item->supplier - $item->penalty), $item->supplierCurrency));
		}

		return $item;
	}
}
