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

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

		$item = parent::_getData();

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

		if (!$item->docNumber) {
			$item->docNumber = 			   $M5['documentNumber'];
		}

		return $item;
	}

	public function populateTransactionCode(\App\Sabre\StructureParser\Types\Document $item, $parsedStructure, $transactionType, $transactionIndexes, $internalAssociations)
	{
		$M5 = $parsedStructure['M5'][$transactionIndexes['km5']];

		$transactionCodeComponents = [];
		$transactionCodeComponents[] = $item->RECLOC;	// RECLOC
		$transactionCodeComponents[] = $M5['IU5MIN'];	// pax index, always 'A' for documents
		$transactionCodeComponents[] = substr(md5(implode(';', [	// segment index
			$M5['typeWithSubkey_'],
			$M5['vendorCode'],
			$M5['transactionType2'],
			$M5['documentNumber'],
			$M5['baseFareAmount'],
			$M5['tax3Amount'],
			$M5['commissionValue'],
		])), 0, 6);

		$item->transactionCode = implode("-", $transactionCodeComponents);

		return $item;
	}

}
