<?php
namespace App\Amadeus\FileParser;

class Line_KS extends \App\Amadeus\FileParser\LineAbstract
{
	/**
	 * 4.79. KS- Selling Fare
	 * see page 237(279)
	 *
	 * This field is used to transmit Selling Fare Data.
	 * For Negotiated Fare Data, when pricing indicator in the C- line reference 9 is "F" this field will contain the Selling Fare.
	 */
	protected $structure = [
		['name'=> 'MSGTAG',     'val' => 'KS-',   'type' => 'AN',   'req' => 'M',                                'len' => 3,],                    // 1 : Message Tag (KS-)
		['name'=> 'ISSUE',                        'type' => 'AN',   'req' => 'M',                                'len' => 1,],                    // 2 : Issue Identifier from the pricing record (refer to A-02-3500-AMD for specific details) F if blank in the TSTB if the value is U in the TSTValue of the TST in other cases
		['name'=> 'CURRTYP',                      'type' => 'AN',                                                'len' => 3,],                    // 3 : Currency Type Code
		['name'=> 'BASE',                         'type' => 'N',    'req' => 'M',                                'len' => 11,],                   // 4 : Base Fare
		['name'=> 'DELIM',      'val' => ';',     'type' => 'AN',   'req' => 'M',                                'len' => 1,],                    // 5 : Delimiter (Constant value of ';')
		['name'=> 'CURCODE',                      'type' => 'AN',                                                'len' => 3,],                    // 6 : Currency code.
		['name'=> 'EQVAMT',                       'type' => 'AN',                                                'len' => 11,],                   // 7 : Equivalent Amount or One/Two Currency Codes
		['name'=> 'DELIM',      'val' => ';',     'type' => 'AN',   'req' => 'M',                                'len' => 1,],                    // 8 : Delimiter (Constant value of ';')
		['name' => 'TAXFLD',                      'type' =>'composite',                                          'rep_min' =>1, 'rep_max' =>10,   // 9 TAX FIELD
		 'childs' => [
		   ['name'=> 'OTAXI',                     'type' => 'S:trim',                                            'len' => 1,],                    // 10 : Tax Indicator (blank = new tax) (refer to A-02-3500-AMD for specific details)
		   ['name'=> 'TCURR',                     'type' => 'AN',                                                'len' => 3,],                    // 11 : Currency Type Code
		   ['name'=> 'TAXA',                      'type' => 'S:trim',                                            'len' => 7,],                    // 12 : Tax Amount
		   ['name'=> 'TAXC',                      'type' => 'S:trim',              'requires' => ['TAXA'], 'len' => 3,],                    // 13 : Tax Code
		   ['name'=> 'TAXN',                      'type' => 'AN',                  'requires' => ['TAXC'], 'len' => 2,],                    // 14 : Nature Code. Blanks if no nature code.
		   ['name'=> 'DELIM',   'val' => ';',     'type' => 'AN',   'req' => 'M',                                'len' => 1,],                    // 15 : Delimiter (Constant value of ';')
		 ]],                                                                                                                                             // 16 END OF COMPOSITE ELEMENT
		['name'=> 'CURRCDE',                      'type' => 'AN',                                                'len' => 3,],                    // 17 : Currency Type Code
		['name'=> 'TOTAMT',                       'type' => 'N',    'req' => 'M',                                'len' => 11,],                   // 18 : Total Amount Collected
		['name'=> 'DELIM',      'val' => ';',     'type' => 'AN',   'req' => 'M',                                'len' => 1,],                    // 19 : Delimiter (Constant value of ';')
		['name'=> 'SELLBUY1',                     'type' => 'N',                                                 'len' => 11,],                   // 20 : Bankers Selling/Buying Rate (First)
		['name'=> 'DELIM',      'val' => ';',     'type' => 'AN',   'req' => 'M',                                'len' => 1,],                    // 21 : Delimiter (Constant value of ';')
		['name'=> 'SELLBUY2',                     'type' => 'N',                                                 'len' => 11,],                   // 22 : Bankers Selling/Buying Rate (Second)
		['name'=> 'DELIM',      'val' => ';',     'type' => 'AN',   'req' => 'M',                                'len' => 1,],                    // 23 : Delimiter (Constant value of ';')
		['name'=> 'TRANCURR',                     'type' => 'AN',                                                'len' => 3,],                    // 24 : Transitional Currency Code Indicates transitional currency when second bankers selling/buying rate exists.
	];

	/**
	 * (non-PHPdoc)
	 * @see \App\Amadeus\FileParser\LineAbstract::postParse()
	 */
	protected function postParse()
	{
		// call the parent first
		parent::postParse();

		if($this->tree['TAXFLD']){
			$this->tree['TAXFLD'] = self::rtrimArrDeep($this->tree['TAXFLD'], 2);
			$this->tree['TAXFLD'] = self::extractTaxNameFromCode($this->tree['TAXFLD']);
			$this->tree['TAXFLD'] = self::fillTAXA($this->tree['TAXFLD']);

			$this->computeStats_taxes();
		}

		$this->tree['_OneTwoCurrencyCodes'] = true;
		$EQVAMT = trim($this->tree['EQVAMT']);
		if(is_numeric($EQVAMT)){
			$this->tree['_OneTwoCurrencyCodes'] = false;
			$this->tree['EQVAMT'] = (float)$EQVAMT;
		}
	}
}
