<?php

namespace App\ImportedOrder;

/**
 * This class contains info about the refund operation on the current service.
 * @author Gabriel
 */
class RefundDetails extends EntityAbstract
{

	/**
	 * The date when the refund occured.
	 * @var String
	 */
	public $date;

	/**
	 * The currency used on all values attached to the refund.
	 * @var String
	 */
	public $currency;

	/**
	 * The refund issue user.
	 * @var \App\ImportedOrder\User
	 */
	public ?\App\ImportedOrder\User $issueUser = null; // [notInIoXML] de add in XML

	/**
	 * The refund issue office.
	 * @var \App\ImportedOrder\Office
	 */
	public ?\App\ImportedOrder\Office $issueOffice = null; // [notInIoXML] de add in XML

	/**
	 * The used part of the supplier price (total value = base + VAT)
	 * @var Float
	 */
	public $fareUsedSupplierValue;

	/**
	 * The used part of the supplier price VAT (this is only the VAT component, included in the fareUsedSupplierValue)
	 * @var Float
	 */
	public $fareUsedSupplierVat;

	/**
	 * The used part of the commission offer component.
	 * @var Float
	 */
	public $fareUsedCommissionValue;

	/**
	 * The used part of the tax offer component (total value = base + VAT)
	 * @var Float
	 */
	public $fareUsedTaxValue;

	/**
	 * A list of tax codes and their details (containing:code, value, currency)
	 * @var array
	 */
	public ?array $fareUsedTaxCodes = null; // [notInIoXML] de add in XML

	/**
	 * The used part of the tax VAT offer component (this is only the VAT component, included in the fareUsedTaxValue)
	 * @var Float
	 */
	public $fareUsedTaxVat;

	/**
	 * The penalties added on the current refund operation.
	 * @var Float
	 */
	public $penaltyValue;

	/**
	 * Indicates the value of the service fee added on top of the existing offer components.
	 * @var Float
	 */
	public $serviceFeeValue;

	/**
	 * Indicates if the original service fee value from the service offer is returned to the client.
	 * @var Boolean
	 */
	public $returnOriginalServiceFee;

	/**
	 * Indicates the refund form of payment.
	 * @var String
	 */
	public $formOfPayment;

	/**
	 * An auxiliary reference (foreign key) to other tables in the databse.
	 * @var String
	 */
	public $auxReference;

	/**
	 * A list of extra fields stored as an associative array.
	 * @var ?array
	 */
	public ?array $extraInfo = null;


	/**
	 * Adds a new tax code (the code is mandatory, the other fields are not)
	 * @param String $code
	 * @param Double $value
	 * @param String $currency
	 */
	public function addFareUsedTaxCode($code, $value, $currency) :void
	{
		if (!$this->fareUsedTaxCodes) {
			$this->fareUsedTaxCodes = [];
		}
		$this->fareUsedTaxCodes[]= ['code'=>$code, 'value'=>$value, 'currency'=>$currency];
	}


	/**
	 * (non-PHPdoc)
	 * @see \App\ImportedOrder\EntityAbstract::validateEntityFields()
	 */
	protected function validateEntityFields(\App\MessagesCollector $messagesCollector): void
	{

		// check currency
		if ($this->fareUsedSupplierValue or $this->fareUsedCommissionValue or $this->fareUsedTaxValue or $this->penaltyValue or $this->serviceFeeValue) {
			if (!$this->currency) {
				$messagesCollector->addError('Currency not set for refund details.');
			} elseif (!\App\Config\Currencies::getInstance()->check($this->currency)) {
				$messagesCollector->addError("The currency `{$this->currency}` is not valid for refund details.");
			}
		}

		// check manual VATs for fare-used
		if ($this->fareUsedSupplierVat and !$this->fareUsedSupplierValue) {
			$messagesCollector->addWarning('If the fare-used supplier price VAT is set, then the value (total) must also be set.');
		}
		if ($this->fareUsedTaxVat and !$this->fareUsedTaxValue) {
			$messagesCollector->addWarning('If the fare-used tax VAT is set, then the value (total) must also be set for.');
		}

		// check refund date
		if (!$this->date) {
			$messagesCollector->addWarning('The refund date is not set.');
		} elseif (!validDate($this->date)) {
			$messagesCollector->addWarning('The refund date is not valid.');
		} elseif ($this->date>\Engine\DateTime::create('+1 day')->formatDateIso()) { // adds one day just to be sure that the GDS time is not after TINA time
			$messagesCollector->addWarning('The refund date cannot be in the future.');
		}

	}


	/**
	 * Returns the matched offer form of payment.
	 */
	public function getMatchedFormOfPayment($idExternalSystem)
	{

		// attempt to match it by imported order matches
		if ($idExternalSystem) {
			$matchedId = \App\Config\ImportedOrders_Matches::getInstance()->getOfferFormOfPayment($idExternalSystem, $this->formOfPayment);
			if ($matchedId) {
				return $matchedId;
			}
		}

		// attempt to match it by direct comparison (one of the values from $CFG['offers']['formOfPaymentList'])
		$CFG = \Engine\Loader_Cfg::getInstance()->getGlobal();
		if (in_array($this->formOfPayment, $CFG['offers']['formOfPaymentList'])) {
			return $this->formOfPayment;
		}

		// no match found
		return null;

	}

}