<?php
namespace App\Amadeus\FileParser;

/**
 * NOTE: i've asked if we could extract some extra data, and didn't get clear response. @see MAIL:Intrebari Amadeus
 */

/**
 * FM Fare Commission
 * @author Lucian Sirbu
 */
class Line_FM extends \App\Amadeus\FileParser\LineAbstract
{
	/**
	 * 4.125. FM Fare Commission
	 * see page 318(360)
	 * @see http://www.amadeus.com/pk/documents/aco/pk/en/ETKT_Handout.pdf
	 * @see http://www.amadeus.com/lt/documents/Amadeus%20Reissue%20and%20Ticketing%20Manual.pdf
	 * @see http://aeu.amadeus.com/aeu/Templates/Blogs.aspx?id=3859&blogid=217
	 * @see http://www.scribd.com/doc/18542444/Ticketing-Manual-2
	 *
	 * DETAILS
	 */
	protected $structure = [
		['name'=> 'MSGTAG',  'val' => 'FM',    'type' => 'AN',      'req' => 'M',  'len' => 2,],                          // 1 : Message Tag (FM)
		['name'=> 'FARECOM',                   'type' => 'AN',      'req' => 'M',  'len_min' => 1,   'len_max' => 127,],  // 2 : Fare Commission
		['name'=> 'SEGASS',  'val' => ';S',    'type' => 'ASSOC',],
		['name'=> 'PSGASS',  'val' => ';P',    'type' => 'ASSOC',],
	];

	protected $FARECOM_type = [
		'*P*' => 'Commission through manual capping or semi-auto capping',
		'*C*' => 'System generated commission',
		'*M*' => 'Manual commission',
		'*F*' => 'Commission data from negotiated fare database',	// @see MAIL:[AMA] FM line, @see RO/AS/2012-10-12_110922_192.168.1.197__AIR01036.Txt
		'*G*' => 'Negotiated fare auto-commission generated',		// @see MAIL:[AMA] FM line, @see LT/Gaublija/2012-02-29_130612_82.135.209.236__0229_130601_00.txt
		'*D*' => 'Dynamic discounted fare commission',				// @see MAIL:[AMA] FM line
		'*PR*' => 'Reuse of manual capped or semi-auto capped commission',// @see MAIL:[AMA] FM line, @see LT/Gaublija/2012-05-14_103133_82.135.209.236__0513_222619_00.txt

		'*V*' => 'Commission is subject to VAT',					// @see JIRA: TD-1608: New element that indicates the comission type is present in FM line
																	// indicates whether the commission is subject to VAT or not

		'*AM*' => '',												// @see RO/AS/2012-06-07_171109_192.168.1.197__AIR00175.Txt
	];

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

		/**
		 * FM line format:
		 * In the PNR you will see FMINF*P*9/OLD/S2/P1 for ex
		 *
		 * FM9 		Commission 9%
		 * FM9P 	Commission 9%
		 * FM9N 	Commission ????? TODO, what does N stands for?
		 * FM20.00A	Commission fixed at a specific amount(eg£20.00)
		 * FM0		No commission
		 *
		 * @see MAIL:Intrebari Amadeus, @see MAIL:[AMA] FM line
		 *
		 * /OLD represents the old commission
		 *
		 * TODO: what does amountType code N, S(2011-06-29_154837_192.168.93.1__@2900799.06) represent???
		 *
		 */
		$this->tree['commissionType'] = 'none';
		$m = [];
		if (preg_match("/^(?P<commissionType>\*(P|C|M|F|G|D|V|AM|PR)\*)?(?P<value>[0-9.]+)?(?P<amountType>(A|P|N|S|V))?(?P<old>\/.+)?$/", $this->tree['FARECOM'], $m)) {
			$this->tree['commissionType'] = 	$this->FARECOM_type[$m['commissionType']];
			if ($m['value']) {
				$this->tree['amountType'] = 		($m['amountType']=='A'?'value':'percent');
				if ($this->tree['amountType']=='value') {
					$this->tree['amountValue'] = 	(float)$m['value'];
				}
				elseif ($this->tree['amountType']=='percent') {
					$this->tree['amountPercent'] = 	(float)$m['value'];
				}
				else {
					throw \App\Amadeus\FileParser\ParserException::internal("Unknown commission type detected");
				}
			}
			else {
				$this->tree['amountType'] =  'none';
				$this->tree['amountValue'] = 0;
			}
		}
		else {
			throw \App\Amadeus\FileParser\ParserException::internal("Unknown FM line format detected in field FARECOM: '{$this->tree['FARECOM']}'");
		}
	}
}
