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

class Document extends \App\Amadeus\StructureParser\Parser\Document
{
	/**
	 * (non-PHPdoc)
	 * @see \App\Amadeus\StructureParser\Parser\Document::document()
	 */
	public function document()
	{
		$index = 		$this->transaction->getIndex();

		if ($index and $index->distribuiteTransaction) {
			$doc = $this->document_RFD_distribuited();
			if($doc){
				return $doc;
			}
		}

		$doc = $this->document_RFD();
		if($doc){
			return $doc;
		}

		throw new \Exception("Unknown refund document");

	}

	/**
	 * Used for distribuited refunds, returns null("skip") when reference records aren't found
	 */
	protected function document_RFD_distribuited()
	{
		$index = 		$this->transaction->getIndex();

		$item = $this->document_RFD();

		$reference = \Entity\AmadeusService::select($index->referenceTransaction);
		if ($reference->recordExists()) {
			// for "direct parents", this will allow docNumbers to pass-trough, without any modifications, so the parser will be able to correctly handle conjunctions, when encountered
			if ($reference->docNumber != $item->docNumber) {
				$item->docNumber = $reference->docNumber;
				$item->docNumbers = [$item->docNumber, ];	// override the detected docNumbers

				$item->autoIgnored = 'fakeRefund';

				$item->transactionCode = $reference->transactionCode;
			}

			return $item;
		}

		return null;
	}

	protected function document_RFD()
	{
		$parsedStructure = $this->transaction->getParsedStructure();
		// initialize the return result
		$item = null;
		if (count($parsedStructure['I'])>1) {
			$this->transaction->addLog(__METHOD__.": This reservation has more than one passenger, picking the first one as 'owner'");
		}

		$I = $parsedStructure['I'][0];

		if (isset($I['R'])) {
			$item = new \App\Amadeus\StructureParser\Types\Document();
			$item->createDate= 		$parsedStructure['D']['PNRDTE'];
			$item->issueDate= 		$parsedStructure['D']['AIRDTE'];

			if ($I['T']['TKTNBR_']) {
				$item->docDate = 		$I['R']['RFDDATE'];
				$item->docNumber = 		$I['T']['TKTNBR'];
				foreach($I['T']['TKTNBR_'] as $TKT){
					$item->docNumbers[] = 	$TKT;
				}
			}
			elseif ($I['R']['RFDDATE']) {
				$item->docDate = 		$I['R']['RFDDATE'];
				$item->docNumber = 		$I['R']['RFDNBR_TKTNBR'];
				$item->docNumbers[] = 	$item->docNumber;
			}



			/**
			 * $item->transactionCode is not generated for refunds, because we dont have enough info in the file.
			 * We lookup the ticket into our DB and use that
			 */
			$transactions = \App\Amadeus\Storage\StorageHandler::selectByDocNumber($item->docNumber, $item->docDate);
			if(!$transactions){
				/**
				 * because ['AMD']['DATE'] doesn't have a year set, it may be interpreted incorectly, and may be wrong(by default will be extracted from the filename or filectime)
				 *  this may cause problems for RFND's done in a different year than the current year, so with this code we're completly ignoring the refund date, and search more generally only by ticket number
				 */
				$transactions = \App\Amadeus\Storage\StorageHandler::selectByDocNumber($item->docNumber);
			}
			$last = end($transactions);
			$item->transactionCode = \Entity\AmadeusService::select($last['id']??null)->transactionCode;
		}

		return $item;
	}


	/**
	 * (non-PHPdoc)
	 * @see \App\Amadeus\StructureParser\Parser\Document::supplier()
	 */
	public function supplier()
	{
		$parsedStructure = $this->transaction->getParsedStructure();

		// initialize the return result
		$item = null;
		$I = $parsedStructure['I'][0];

		if(	$I['R']) {
			$item = new \App\Amadeus\StructureParser\Types\Supplier();
			$item->code3 = 		$I['R']['RFDNBR_NUMAIR'];
		}

		return $item;
	}
}
