<?php
namespace App\Config;

class SupplierInvoiceOverPaymentTolerance extends XmlBasicAbstract
{
	/**
	 * Stores the configuration file (relative to the "files/config/" dir). For config files stored
	 * inside subdirectories, the subdirectory is included (Ex: services/servicesList.xml).
	 * @var String
	 */
	protected $fileName = CONFIG_FILE_SUPPLIERINVOICES_OVERPAYMENTTOLERANCE;

	/**
	 * (non-PHPdoc)
	 * @see \App\Config\XmlBasicAbstract::getAll_rawData_extractDataFromFile()
	 */
	protected function getAll_rawData_extractDataFromFile(\SimpleXMLElement $xml)
	{
		// attach the properties
		return [
			'positiveLimit' => (Double)$xml->positiveLimit,
			'negativeLimit' => (Double)$xml->negativeLimit,
		];
	}

	/**
	 * (non-PHPdoc)
	 * @see \App\Config\XmlBasicAbstract::getAll_rawData_fileNotExists()
	 */
	protected function getAll_rawData_fileNotExists()
	{
		// get a list with three keys: currencyes, blockedServices and roundType
		$return= [
			'positiveLimit' => 0.01,
			'negativeLimit' => 0.01,
		];

		return $return;
	}

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

		global $CFG;

		if (!is_numeric($this->itemsList['positiveLimit']) or $this->itemsList['positiveLimit'] < $CFG['supplierInvoicePayment']['statusTolerancePercent'] or $this->itemsList['positiveLimit']>1) {
			$this->addErrorMessage('The positive limit of the overpayment tolerance is not valid.');
		}
		if (!is_numeric($this->itemsList['negativeLimit']) or $this->itemsList['negativeLimit'] < $CFG['supplierInvoicePayment']['statusTolerancePercent'] or $this->itemsList['negativeLimit']>1) {
			$this->addErrorMessage('The negative limit of the overpayment tolerance is not valid.');
		}

	}

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

		// set the limits
		$xml->positiveLimit = $this->itemsList['positiveLimit'];
		$xml->negativeLimit = $this->itemsList['negativeLimit'];

		// return the simplexml object
		return $xml;
	}
}