<?php

namespace App\Config;

/**
 * The class is used for managing the default details about service offer currency: service fee, total, type, commision
 */
class Services_Calculations_OfferDefaults extends XmlBasicAbstract {

	/**
	 * Stores the configuration file (relative to the "files/config/" dir). For config files stored
	 * inside subdirectories, the subdirectory is included .
	 * @var String
	 */
	protected $fileName=CONFIG_FILE_SERVICE_CALCULATION_OFFER_DEFAULT;


	/**
	 * (non-PHPdoc)
	 * @see \App\Config\XmlBasicAbstract::getAll_rawData_extractDataFromFile()
	 */
	protected function getAll_rawData_extractDataFromFile(\SimpleXMLElement $xml)
	{

		$return=[];

		// loop through each service
		foreach($xml->service as $serviceDetails) {

			// scan each calculation type
			foreach ($serviceDetails as $calcCurrencyDetails) {

				$offerDetailsList = [];

				// offer currency
				$offerDetailsList['offerCurrency']	= (string)$calcCurrencyDetails->offerCurrency;

				// service fee currency
				$offerDetailsList['serviceFeeCurrency']	= (string)$calcCurrencyDetails->serviceFeeCurrency;

				// total currency
				$offerDetailsList['totalCurrency']	= (string)$calcCurrencyDetails->totalCurrency;

				// currency type
				$offerDetailsList['currencyType']	= (string)$calcCurrencyDetails->currencyType;

				$return[(String)$serviceDetails['code'].'_'.(String)$calcCurrencyDetails['location'].'_'.(string)$calcCurrencyDetails['code']]=$offerDetailsList;

			}

		}

		// return linear list with  client type from the calculation type
		return $return;
	}


	/**
	 * (non-PHPdoc)
	 * @see \App\Config\BaseAbstract::save_check()
	 */
	protected function save_check()
	{

		global $_LANG;

		// scan each calculation type from the session
		foreach (Services_Calculations::getAdmin()->getAll() as $calculationCode => $calculationDetails) {

			// check for those calculations that are not removed
			if ( !$calculationDetails['deleted'] ) {

				// check offer currency
				if ( !Currencies::getInstance()->check($this->itemsList[$calculationCode]['offerCurrency']) ) {
					$this->addErrorMessage(sprintf('When setting the service offer details, the currency `%s` for service default offer is not valid for calculation type `%s` of service `%s` at location `%s` ', $this->itemsList[$calculationCode]['offerCurrency'], $calculationDetails['name'], Services_Basic::getInstance()->getServiceName($calculationDetails['serviceCode']), $_LANG['locations']['countriesCriteria']['long'][$calculationDetails['location']] ));
				}

				// check service fee currency
				if ( !Currencies::getInstance()->check($this->itemsList[$calculationCode]['serviceFeeCurrency']) ) {
					$this->addErrorMessage(sprintf('When setting the service offer details, the currency `%s` for service default fee is not valid for calculation type `%s` of service `%s` at location `%s`', $this->itemsList[$calculationCode]['serviceFeeCurrency'], $calculationDetails['name'], Services_Basic::getInstance()->getServiceName($calculationDetails['serviceCode']), $_LANG['locations']['countriesCriteria']['long'][$calculationDetails['location']] ));
				}

				// total currency
				if ( !Currencies::getInstance()->check($this->itemsList[$calculationCode]['totalCurrency'] )) {
					$this->addErrorMessage(sprintf('When setting the service offer details, the currency `%s` for offer default total is not valid for calculation type `%s` of service `%s` at location `%s`', $this->itemsList[$calculationCode]['totalCurrency'], $calculationDetails['name'], Services_Basic::getInstance()->getServiceName($calculationDetails['serviceCode']), $_LANG['locations']['countriesCriteria']['long'][$calculationDetails['location']] ));
				}

				// currency type
				if ( !CurrencyTypes::getInstance()->check($this->itemsList[$calculationCode]['currencyType']) ) {
					$this->addErrorMessage(sprintf('When setting the service offer details, the currency type `%s` for offer default details is not valid for calculation type `%s` of service `%s` at location `%s`', $this->itemsList[$calculationCode]['currencyType'], $calculationDetails['name'], Services_Basic::getInstance()->getServiceName($calculationDetails['serviceCode']), $_LANG['locations']['countriesCriteria']['long'][$calculationDetails['location']] ));
				}

			}
		}
	}


	/**
	 * (non-PHPdoc)
	 * @see \App\Config\XmlBasicAbstract::save_write_do_getXml()
	 */
	protected function save_write_do_getXml()
	{

		// start composing the XML object
		$xml=simplexml_load_string('<?xml version="1.0" encoding="UTF-8"?><calculationsCurrencies />');

		// scan each calculation type from the session
		foreach (Services_Calculations::getAdmin()->getExtendedList() as $serviceCode => $locationDetails) {

			// add the service obj
			$serviceObj=$xml->addChild('service');
			$serviceObj['code']=$serviceCode;

			// scan each location from this service
			foreach ($locationDetails as $locationCode => $calculationDetails) {

				// looop through each calculation type
				foreach ($calculationDetails as $calculationCode => $nothing) {

					// add the client type
					$calcCurrencyObj=$serviceObj->addChild('calculationCurrency');

					// add the location
					$calcCurrencyObj['location']=$locationCode;

					// add the location
					$calcCurrencyObj['code']=$calculationCode;

					// add offer currency
					$calcCurrencyObj->offerCurrency = $this->itemsList[$serviceCode.'_'.$locationCode.'_'.$calculationCode]['offerCurrency'];

					// add service fee currency
					$calcCurrencyObj->serviceFeeCurrency = $this->itemsList[$serviceCode.'_'.$locationCode.'_'.$calculationCode]['serviceFeeCurrency'];

					// add total currency
					$calcCurrencyObj->totalCurrency = $this->itemsList[$serviceCode.'_'.$locationCode.'_'.$calculationCode]['totalCurrency'];

					// add currency type
					$calcCurrencyObj->currencyType = $this->itemsList[$serviceCode.'_'.$locationCode.'_'.$calculationCode]['currencyType'];

				}
			}
		}

		// return the simplexml object
		return $xml;
	}


	/**
	 * Returns the defaults for a certain calculation.
	 * @param String $serviceCode
	 * @param String $location
	 * @param Int $calculationType
	 * @return Array - array of offer components and their VAT details attached
	 */
	public function getCalculationDetails($serviceCode, $location, $calculation)
	{
		// if input is invalid - return an empty array
		if (!$serviceCode) throw new \Exception('The service code was not set.');
		if (!$location) throw new \Exception('The location code was not set.');
		if (!strlen((string)$calculation)) throw new \Exception('The calculation code was not set.');

		$searchKey = $serviceCode.'_'.$location.'_'.$calculation;
		$allCfg = $this->getAll();
		return (Array)$allCfg[$searchKey];
	}

}

