<?php
namespace App\Amadeus\CompatibilityWithImportedOrders;

class TaxManipulations extends \App\Utils\BaseParser\Structure\CompatibilityWithImportedOrders\TaxManipulations
{

	protected function getAssocMapFromTransactionTypeToEntity()
	{
		return [
			'ticketing' => [
				'serviceCode' => 		['ticketing', ],
				'transactionStatus' => 	['new', 'reissued', 'refunded', ],
			],
			'reissue' => [
				'serviceCode' => 		['ticketing', ],
				'transactionStatus' => 	['reissue', ],
			],
			'refund' => [
				'serviceCode' => 		['ticketing', ],
				'transactionStatus' => 	['refund', ],
			],
			'EMD' => [
				'serviceCode' => 		['EMD', ],
				'transactionStatus' => 	['new', ],
			],
			'MCO' => [
				'serviceCode' => 		['MCO', ],
				'transactionStatus' => 	['new', ],
			],
			'car' => [
				'serviceCode' => 		['car', ],
				'transactionStatus' => 	['new', ],
			],
			'hotel' => [
				'serviceCode' => 		['hotel', ],
				'transactionStatus' => 	['new', ],
			],
		];
	}


	protected function getGdsConfig()
	{
		return \App\Amadeus\Config::getInstance();
	}

	protected function getSystemCode()
	{
		return \Entity\AmadeusService::systemCode;
	}

	protected function getPriceTaxComponentObject()
	{
		return new \App\Amadeus\StructureParser\Types\PriceTaxComponent();
	}

	/**
	 * Get the service itinerary, or the service's parent itinerary, if this is a refund
	 * @param \Entity\AmadeusService $entity
	 * @param \Entity\AmadeusService $parentEntity
	 * @return Array
	 */
	public function TINA_getServiceItinerary($entity, $parentEntity=null)
	{
		$serviceSegments = $entity->transactionSegmentData->getSegments();

		if ($entity->transactionStatus=='refund') {
			$serviceSegments = $parentEntity->transactionSegmentData->getSegments(); // equivalent to $parentEntity->parsedFileTransaction->getItinerary()->getSegments();
		}

		return $serviceSegments;
	}

	/**
	 * Move taxes to the supplier price
	 * @param array $taxManipulationRule
	 * @param \App\Amadeus\StructureParser\Types\Price $priceData
	 * @throws \App\Amadeus\StructureParser\StructureException
	 * @return \App\Amadeus\StructureParser\Types\Price[]|boolean[]
	 */
	protected function action_moveTaxToSupplier(array $taxManipulationRule, $applyRule, $priceData)
	{
		$applyRule = true;

		// load tax
		$taxes = $priceData->tax->getTaxesByCode($taxManipulationRule['taxCode']);

		if ($applyRule) {
			foreach ($taxes as $tax) {
				if ($tax->currency != $priceData->supplierCurrency) {
					$applyRule = false;
					throw new \App\Amadeus\StructureParser\StructureException(sprintf("[%s] cannot move tax %s, the tax currency should be the same as the supplier currency(%s, %s)", __METHOD__, $taxManipulationRule['taxCode'], $tax->currency, $priceData->supplierCurrency));
				}
			}
		}

		if ($applyRule) {
			// start altering the transaction, remove affected taxes, store their sum
			$selectedTaxAmount = 0;
			foreach ($priceData->tax->components as $idx => $tax) {
				if ($tax->code == $taxManipulationRule['taxCode']) {
					$selectedTaxAmount+= $tax->amount + $tax->VAT;

					$priceData->tax->setAmount($priceData->tax->getAmount() - ($tax->amount + $tax->VAT));

					// remove taxes
					unset($priceData->tax->components[$idx]);
				}
			}

			// reindex taxes
			$priceData->tax->components = array_values($priceData->tax->components);

			// re-add tax back to the price component, but add it to the supplier this time
			$priceData->supplier+= $selectedTaxAmount;

			// recalc totals
			$priceData->total = $priceData->calculateTotal();
			$priceData->totalVAT = $priceData->calculateTotalVAT();
		}

		return [$applyRule, $priceData, ];
	}
}
