<?php
namespace App\Amadeus\FileParser;

class Line_MCO extends \App\Amadeus\FileParser\LineAbstract
{
	/**
	 * 4.63. MCO - MCO Segment
	 * see page 205(245)
	 *
	 * In this pseudo message Edifact Segment no separators are used. The "segment" will be terminated by a Carriage Return.
	 */
	protected $structure = [
		['name'=> 'MSGTAG',     'val' => 'MCO',   'type' => 'AN',      'req' => 'M',                                      'len' => 3,],                          // 1 : Message Tag (MCO)
		['name'=> 'TTONBR',                       'type' => 'N',       'req' => 'M',                                      'len' => 3,],                          // 2 : Tattoo Number in the PNR
		['name'=> 'DELIM',      'val' => ';',     'type' => 'AN',      'req' => 'M',                                      'len' => 1,],                          // 3 : Delimiter (Constant value of ";")
		['name'=> 'ELMNBR',                       'type' => 'NI',      'req' => 'M',                                      'len' => 3,],                          // 4 : Element number in the PNR
		['name'=> 'AIRCDE',                       'type' => 'AN',      'req' => 'M',                                      'len_min' => 1,   'len_max' => 3,],    // 5 : Airline Code/Supplier Code. "XD" for ARC MCO for Service Fees. Airline Code for other
		['name'=> 'DELIM',      'val' => ';',     'type' => 'AN',      'req' => 'M',                                      'len' => 1,],                          // 6 : Delimiter (Constant value of ";")
		['name'=> 'NUMAIR',                       'type' => 'N',       'req' => 'M',                                      'len' => 3,],                          // 7 : Numeric Airline Code including Check digit
		['name'=> 'NUMAIRCK',                     'type' => 'N',       'req' => 'M',                                      'len' => 1,],
		['name'=> 'DELIM',      'val' => ';',     'type' => 'AN',      'req' => 'M',                                      'len' => 1,],                          // 8 : Delimiter (Constant value of ";")
		['name'=> 'ISSAIR',                       'type' => 'AN',      'req' => 'M',                                      'len_min' => 1,   'len_max' => 24,],   // 9 : Issuing Airline Name
		['name'=> 'DELIM',      'val' => ';',     'type' => 'AN',      'req' => 'M',                                      'len' => 1,],                          // 10 : Delimiter (Constant value of ";")
		['name'=> 'CITYCDE',                      'type' => 'AN',      'req' => 'M',                                      'len_min' => 3,   'len_max' => 5,],    // 11 : Airport/City Code
		['name'=> 'DELIM',      'val' => ';',     'type' => 'AN',      'req' => 'M',                                      'len' => 1,],                          // 12 : Delimiter (Constant value of ";")
		['name'=> 'TOTAG',      'val' => 'TO-',   'type' => 'AN',      'req' => 'M',                                      'len' => 3,],                          // 13 : "TO" tag (constant value of "TO-")
		['name'=> 'TOINFO',                       'type' => 'AN',                                                         'len_min' => 1,   'len_max' => 74,],   // 14 : "TO" Carrier/Operator to whom document will be presented. If blank in TSM then equals decode of AIRCDE
		['name'=> 'DELIM',      'val' => ';',     'type' => 'AN',      'req' => 'M',                                      'len' => 1,],                          // 15 : Delimiter (Constant value of ";")
		['name'=> 'ATTAG',      'val' => 'AT-',   'type' => 'AN',      'req' => 'M',                                      'len' => 3,],                          // 16 : "AT" tag (constant value of "AT-")
		['name'=> 'ATINFO',                       'type' => 'AN',                                                         'len_min' => 1,   'len_max' => 74,],   // 17 : "AT" Location where document will be presented
		['name'=> 'DELIM',      'val' => ';',     'type' => 'AN',      'req' => 'M',                                      'len' => 1,],                          // 18 : Delimiter (Constant value of ";")
		['name'=> 'DEPDATE',                      'type' => 'D:DDMMM:PNRDTE', 'req' => 'M',                               'len' => 5,],                          // 19 : Date (DDMMM)
		['name'=> 'DELIM',      'val' => ';',     'type' => 'AN',      'req' => 'M',                                      'len' => 1,],                          // 20 : Delimiter (Constant value of ";")
		['name'=> 'MCOID',                        'type' => 'AN',      'req' => 'M',                                      'len' => 1,],                          // 21 : MCO element identifier/Document type
		['name'=> 'DELIM',      'val' => ';',     'type' => 'AN',      'req' => 'M',                                      'len' => 1,],                          // 22 : Delimiter (Constant value of ";")
		['name'=> 'REASON',                       'type' => 'AN',      'req' => 'M',                                      'len' => 1,],                          // 23 : Reason for Issuance Code
		['name'=> 'DELIM',      'val' => ';',     'type' => 'AN',      'req' => 'M',                                      'len' => 1,],                          // 24 : Delimiter (Constant value of ";")
		['name'=> 'SVCTYPE',                      'type' => 'AN',                                                         'len_min' => 1,   'len_max' => 70,],   // 25 : Type of Service
		['name'=> 'DELIM',      'val' => ';',     'type' => 'AN',      'req' => 'M',                                      'len' => 1,],                          // 26 : Delimiter (Constant value of ";")
		['name'=> 'ADTAG',      'val' => '**-',   'type' => 'AN',      'req' => 'M',                                      'len' => 3,],                          // 27 : Additional information tag (constant value of "**-")
		['name'=> 'ADINFO',                       'type' => 'AN',                                                         'len_min' => 1,   'len_max' => 25,],   // 28 : Additional information
		['name'=> 'DELIM',      'val' => ';',     'type' => 'AN',      'req' => 'M',                                      'len' => 1,],                          // 29 : Delimiter (Constant value of ";")
		['name'=> 'REMARK',                       'type' => 'AN',                                                         'len_min' => 1,   'len_max' => 260,],  // 30 : Additional Remarks
		['name'=> 'DELIM',      'val' => ';',     'type' => 'AN',      'req' => 'M',                                      'len' => 1,],                          // 31 : Delimiter (Constant value of ";")
		['name'=> 'ISSUE',                        'type' => 'AN',                                                         'len' => 1,],                          // 32 : Issue Identifier("F" = First Issue, "R" = Reissue)
		['name'=> 'DELIM',      'val' => ';',     'type' => 'AN',      'req' => 'M',                                      'len' => 1,],                          // 33 : Delimiter (Constant value of ";")
		['name'=> 'BASECURCODE',                  'type' => 'AN',                                                         'len' => 3,],                          // 34 : Currency Code
		['name'=> 'BASE',                         'type' => 'N',       'req' => 'M',                                      'len' => 11,],                         // 35 : Total Base Amount (Exchange Value)
		['name'=> 'DELIM',      'val' => ';',     'type' => 'AN',      'req' => 'M',                                      'len' => 1,],                          // 36 : Delimiter (Constant value of ";")
		['name'=> 'TAX',                          'type' => 'AN',                                                         'len' => 1,],                          // 37 : Inclusive of Tax Indicator
		['name'=> 'DELIM',      'val' => ';',     'type' => 'AN',      'req' => 'M',                                      'len' => 1,],                          // 38 : Delimiter (Constant value of ";")
		['name'=> 'EQVCURCODE',                   'type' => 'AN',                                                         'len' => 3,],                          // 39 : Currency Code
		['name'=> 'EQVAMNT',                      'type' => 'N',                                                          'len' => 11,],                         // 40 : Equivalent Amount or one/two currency codes
		['name'=> 'DELIM',      'val' => ';',     'type' => 'AN',      'req' => 'M',                                      'len' => 1,],                          // 41 : Delimiter (Constant value of ";")
		['name' => 'TAXFLDE',                     'type' =>'composite',                                                   'rep_min' =>1,    'rep_max' =>30,      // 42 TAX FIELD - TAX ON MCO
		 'childs' => [
		   ['name'=> 'OTAXI',                     'type' => 'S:trim',                                                     'len' => 1,],                          // 43 : Tax Indicator Blank = new tax
		   ['name'=> 'TCURR',                     'type' => 'AN',                                                         'len' => 3,],                          // 44 : Currency Code Blank = new tax
		   ['name'=> 'TAXA',                      'type' => 'N',                                                          'len' => 9,],                          // 45 : Tax amount
		   ['name'=> 'TAXC',                      'type' => 'S:trim',                 'requires' => ['TAXA'],       'len' => 3,],                          // 46 : Tax code
		   ['name'=> 'TAXN',                      'type' => 'AN',                     'requires' => ['TAXA'],       'len' => 2,],                          // 47 : Nature code
		   ['name'=> 'DELIM',   'val' => ';',     'type' => 'AN',      'req' => 'M',                                      'len' => 1,],                          // 48 : Delimiter (Constant value of ";")
		 ]],                                                                                                                                                            // 49 END OF COMPOSITE ELEMENT
		['name'=> 'CHARGESCURCODE',               'type' => 'AN',                                                         'len' => 3,],                          // 50 : Currency Code
		['name'=> 'CHARGES',                      'type' => 'N',                      'requires' => ['TAXFLD',],   'len' => 11,],                         // 51 : Amount of other fees or charges
		['name'=> 'DELIM',      'val' => ';',     'type' => 'AN',      'req' => 'M',                                      'len' => 1,],                          // 52 : Delimiter (Constant value of ";")
		['name'=> 'TOTCURCODE',                   'type' => 'AN',                                                         'len' => 3,],                          // 53 : Currency Code
		['name'=> 'TOTAMT',                       'type' => 'N',       'req' => 'M',                                      'len' => 11,],                         // 54 : Total amount collected
		['name'=> 'DELIM',      'val' => ';',     'type' => 'AN',      'req' => 'M',                                      'len' => 1,],                          // 55 : Delimiter (Constant value of ";")
		['name'=> 'SELLBUY1',                     'type' => 'N',                                                          'len' => 11,],                         // 56 : Banker's Selling/Buying Rate (First)
		['name'=> 'DELIM',      'val' => ';',     'type' => 'AN',      'req' => 'M',                                      'len' => 1,],                          // 57 : Delimiter (Constant value of ";")
		['name'=> 'SELLBUY2',                     'type' => 'N',                                                          'len' => 11,],                         // 58 : Banker's Selling/Buying Rate (Second)
		['name'=> 'DELIM',      'val' => ';',     'type' => 'AN',      'req' => 'M',                                      'len' => 1,],                          // 59 : Delimiter (Constant value of ";")
		['name'=> 'CONTKT',                       'type' => 'AN',                                                         'len_min' => 1,   'len_max' => 40,],   // 60 : Connection Ticket Number
		['name'=> 'DELIM',      'val' => ';',     'type' => 'AN',      'req' => 'M',                                      'len' => 1,],                          // 61 : Delimiter (Constant value of ";")
		['name'=> 'OPTALINF',                     'type' => 'AN',                                                         'len_min' => 1,   'len_max' => 26,],   // 62 : Optional Airline Information
		['name'=> 'DELIM',      'val' => ';',     'type' => 'AN',      'req' => 'M',                                      'len' => 1,],                          // 63 : Delimiter (Constant value of ";")
		['name'=> 'INDALUSE',                     'type' => 'AN',                                                         'len_min' => 1,   'len_max' => 23,],   // 64 : Individual Airline Use
		['name'=> 'DELIM',      'val' => ';',     'type' => 'AN',      'req' => 'M',                                      'len' => 1,],                          // 65 : Delimiter (Constant value of ';')
		['name'=> 'TRANCURR',                     'type' => 'AN',                                                         'len' => 3,],                          // 66 : Transitional Currency Code. Indicates transitional currency when second bankers selling/buying rate exists.
		['name'=> 'EQOCURCODE',                   'type' => 'AN',                                                         'len' => 3,],                          // 67 : Equivalent other charges currency
		['name'=> 'EQOCHARGES',                   'type' => 'N',],                                      // 68 : Equivalent other charges amount
		['name'=> 'DELIM',      'val' => ';',     'type' => 'AN',      'req' => 'M',                                      'len' => 1,],                          // 69 : Delimiter (Constant value of ';')
		['name' => 'PTAFARE',                     'type' =>'composite',                                                   'rep_min' =>0,    'rep_max' =>1,       // 70 PTA fares. This is a composite element
		 'childs' => [
		 // TODO: find an example with this included... the one from the manual is broken somehow, and we'll need a way to detect if this is present or not in real-life (some kind of lookahead)
// 		   Array( 'name'=> 'EXCHTYP',                   'type' => 'AN',                                                         'len' => 1,),                          // 71 : Exchange value type
		   ['name'=> 'EXCHCURCODE',               'type' => 'AN',                                                         'len' => 3,],                          // 72 : Exchange value currency code
		   ['name'=> 'EXCHAMT',                   'type' => 'N',],                                                                           // 73 : Exchange value amount
		   ['name'=> 'DELIM',   'val' => ';',     'type' => 'AN',      'req' => 'M',                                      'len' => 1,],                          // 74 : Delimiter (constant value of ";")
		   ['name'=> 'EQVXCHCURCODE',             'type' => 'AN',                                                         'len' => 3,],                          // 75 : Equivalent exchange value currently
		   ['name'=> 'EQVXCHAMNT',                'type' => 'N',],                                                                           // 76 : Equivalent exchange value amount
		   ['name'=> 'DELIM',   'val' => ';',     'type' => 'AN',      'req' => 'M',                                      'len' => 1,],                          // 77 : Delimiter (constant value of ";")
		   ['name'=> 'SVCTYP',                    'type' => 'AN',                                                         'len' => 1,],                          // 78 : Service charges type
		   ['name'=> 'SVCCURCODE',                'type' => 'AN',                                                         'len' => 3,],                          // 79 : Service charges currency code
		   ['name'=> 'SVCAMT',                    'type' => 'N',],                                                                           // 80 : Service charges amount
		   ['name'=> 'DELIM',                     'type' => 'AN',      'req' => 'M',  'requires' => ['EXCHTYP'],    'len' => 1,],
		 ]],                                                                                                                                                            // 81 END OF COMPOSITE ELEMENT
		['name'=> 'DELIM',      'val' => ';',     'type' => 'AN',      'req' => 'M',                                      'len' => 1,],
		['name'=> 'NETCURCODE',                   'type' => 'AN',                                                         'len' => 3,],                          // 83 : Net fare currency code
		['name'=> 'NETFARE',                      'type' => 'N',],                                      // 84 : Net fare amount
		['name'=> 'DELIM',      'val' => ';',     'type' => 'AN',                                                         'len' => 1,],                          // 85 : Delimiter (constant value of ";")
		['name'=> 'INTIND',                       'type' => 'AN',                                                         'len' => 1,
			'isValidRegexp' => "/^(D|E|I| )$/"
		],                          // 86 : International Indicator. Possible values are: 'D' stands for "Domestic" 'E' stands for "International within Europe" 'I' stands for "Other International"[blank] , if no value entered
		['name'=> 'DELIM',      'val' => ';',     'type' => 'AN',                     'requires' => ['INTIND'],     'len' => 1,],                          // 85 : Delimiter (constant value of ";")

		['name'=> 'PSGASS',     'val' => 'P',    'type' => 'ASSOC:loose',],
		['name'=> 'SEGASS',     'val' => 'S',    'type' => 'ASSOC:loose',],
	];

	/**
	 * (non-PHPdoc)
	 * @see \App\Amadeus\FileParser\LineAbstract::postParse()
	 */
	protected function postParse()
	{
		parent::postParse();
		$this->tree['ISSUE_'] = ($this->tree['ISSUE']=='F'?'issue':'reissue');

		if($this->tree['CONTKT']){
			$m = [];

			// 	match 125-9659329500
			preg_match("/^(?P<airline>[0-9]{3})\-(?P<docNumber>.+)$/", $this->tree['CONTKT'], $m);
			if(!$m){
				// 	match 1259659329500
				preg_match("/^(?P<airline>[0-9]{3})(?P<docNumber>.+)$/", $this->tree['CONTKT'], $m);
			}
			if($m){
				$this->tree['CONTKT_'] = [
					'airline' => $m['airline'],
					'docNumber' => self::conjunctionTktToArr($m['docNumber']),
				];
			}
		}

		$this->tree['TAXFLDE'] = self::rtrimArrDeep($this->tree['TAXFLDE']);
	}
}
