<?php

namespace App\Config;

/**
 * The class is used for managing the details about service offer: quantity type (multiplier), valability
 * and sets if can generate an invoice which has only the components that represent revenue for the agency
 * (commission, discount and service fee)
 */
class Services_OfferDetails 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_OFFER_DETAILS;


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

		// get the list of offer details with quantity type, valability (array with keys relativeTo and days)
		// and ignoreSupplierOnDocuments grouped by services
		$result=[];

		// scan the services list from XML file
		foreach($xml->service as $serviceOfferDetails) {

			$serviceOfferDetailsList = [];

			// store quantity type (simple, duration or distance )
			$serviceOfferDetailsList['quantityType'] = (string)$serviceOfferDetails->quantityType;

			// store expiration as an array ('relativeTo' => (noValability, offerAddDate, serviceStartDate)
			$serviceOfferDetailsList['expiration'] = [
				'type' => (string)$serviceOfferDetails->valability['relativeTo'],
				'period'=>(int)$serviceOfferDetails->valability['days']
			];

			// get ignoreSupplierOnDocuments
			$serviceOfferDetailsList['ignoreSupplierOnDocuments'] = (string)$serviceOfferDetails->ignoreSupplierOnDocuments;

			// put service offer details in array which will be returned
			$result[(string)$serviceOfferDetails['code']]=$serviceOfferDetailsList;
		}

		return $result;
	}


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

		// scan each service from the session
		foreach (Services_Basic::getAdmin()->getListLinear() as $serviceCode => $serviceDetails) {

			// check the quantity type
			if ( !in_array($this->itemsList[$serviceCode]['quantityType'], ['simple', 'duration', 'distance'])) {
				$this->addErrorMessage('When setting the service offer details, the offer `quantity type` is not valid for service `'.$serviceDetails['name'].'`.');
			}
			// check ignoreSupplierOnDocuments
			if ( !in_array($this->itemsList[$serviceCode]['ignoreSupplierOnDocuments'], ['hidden', 'visible', 'defaultSelected']) ) {
				$this->addErrorMessage('When setting the service offer details, the element `ignoreSupplierOnDocuments` is not valid for service `'.$serviceDetails['name'].'`.');
			}

			// check the offer expiration
			if ( !in_array($this->itemsList[$serviceCode]['expiration']['type'], ['noValability', 'offerAddDate', 'serviceStartDate'])) {
				$this->addErrorMessage('When setting the service offer details, the offer expiration is not valid for service `'.$serviceDetails['name'].'`.');
			}

			if ( $this->itemsList[$serviceCode]['expiration']['type']=='offerAddDate' or $this->itemsList[$serviceCode]['expiration']['type']=='serviceStartDate' ) {
				if (!is_numeric($this->itemsList[$serviceCode]['expiration']['period']) or $this->itemsList[$serviceCode]['expiration']['period']<0 or $this->itemsList[$serviceCode]['expiration']['period']>366 ) {
					$this->addErrorMessage('When setting the service offer details, the expiration period of offer must be a positive value less than 366 for service `'.$serviceDetails['name'].'`.');
				}
			}
		}
	}


	/**
	 * (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"?><offerDetails />');

		// scan each service from the session
		foreach (Services_Basic::getAdmin()->getListLinear() as $serviceCode => $serviceDetails) {

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

			// add the quantity type of service offer
			$serviceObj->quantityType = $this->itemsList[$serviceCode]['quantityType'];

			// add the offer valability
			$valabilityObj=$serviceObj->addChild('valability');
			$valabilityObj['relativeTo']=$this->itemsList[$serviceCode]['expiration']['type'];

			// add the days if valability has offerAddDate or serviceStartDate values
			if (
				$this->itemsList[$serviceCode]['expiration']['type'] == 'offerAddDate' or
				$this->itemsList[$serviceCode]['expiration']['type'] == 'serviceStartDate'
			) {
				$valabilityObj['days']=$this->itemsList[$serviceCode]['expiration']['period'];
			}

			// add ignoreSupplierOnDocuments
			$serviceObj->ignoreSupplierOnDocuments = $this->itemsList[$serviceCode]['ignoreSupplierOnDocuments'];

		}

		// return the simplexml object
		return $xml;
	}


	/**
	 * Returns the value of 'ignoreSupplierOnDocuments' flag for a cerain service.
	 * @param String $serviceCode
	 * @return String: 'hidden', 'visible', 'defaultSelected'
	 */
	public function getIgnoreSupplierOnDocuments($serviceCode)
	{
		$servCfg = $this->get($serviceCode);
		return $servCfg['ignoreSupplierOnDocuments'];
	}

}
