<?php

namespace Entity;

/**
 * Base class definition for service offer item.
 */
class OfferItem extends \App\Entity\GeneralMtAbstract
{

	/**
	 * Indicate if the operations available for the current entity must be done inside a transaction or not.
	 * @var Boolean
	 */
	protected $saveInsideTransaction = false;

	protected $addAndUpdateDateTimeFormat = 'timestamp'; // TODO: to be eliminated after the table columns are renamed and replaced with datetime

	/**
	 * (non-PHPdoc)
	 * @see \App\Entity\GeneralMtAbstract::setTableGroup()
	 */
	protected function setTableGroup(): void
	{
		global $DBTReservationServiceOfferItems;
		$this->tableGroup=$DBTReservationServiceOfferItems;
	}


	static public function update($id) {
		throw new \Exception(__CLASS__.'::'.__METHOD__.' will not be developed.');
	}

	static public function delete($id) {
		throw new \Exception(__CLASS__.'::'.__METHOD__.' will not be developed.');
	}

	/**
	 * (non-PHPdoc)
	 * @see \App\Entity\SelectAbstract::getAttribute()
	 */
	protected function getAttribute($var)
	{

		// CHECKS EACH FIELD THAT CAN BE EXTRACTED FOR THIS INVOICE ///////////////////////////////
		switch ($var) {
			case 'idCustomer': case 'customerType': case 'productMultiplier': case 'quantity':
			case 'supplierInitialValue': case 'supplierInitialVat': case 'supplierInitialCurrency': case 'supplierWorkValue': case 'supplierWorkVat':
			case 'commissionPercent': case 'commissionInitialValue': case 'commissionInitialCurrency': case 'commissionWorkValue': case 'commissionWorkVat':
			case 'discountPercent': case 'discountInitialValue': case 'discountInitialCurrency':
			case 'discountWorkValue': case 'discountWorkVat':
			case 'serviceFeePercent': case 'serviceFeeInitialValue': case 'serviceFeeInitialCurrency': case 'serviceFeeWorkValue': case 'serviceFeeWorkVat':
			case 'taxInitialValue': case 'taxInitialVat': case 'taxInitialCurrency': case 'taxWorkValue': case 'taxWorkVat':
			case 'netCompanyWorkTotal': case 'offerWorkTotal': {
				return $this->fields[$var];
			}

			// $var IS NOT VALID - throws Exception -----------------------------------------------
			default: {
				return parent::getAttribute($var);
			}

		}

	}


	/**
	 * (non-PHPdoc)
	 * @see \App\Entity\GeneralAbstract::setAttribute()
	 */
	protected function setAttribute($var, $value)
	{

		// check operation
		if ($this->operation!='insert') {
			throw new \Exception("You cannot use __set magic method with `{$this->operation}` operation in `".__CLASS__."` class.");
		}

		// SET THE ATTRIBUTE //////////////////////////////////////////////////////////////////////
		switch ($var) {

			// positive numeric fields and currencies
			case 'productMultiplier': case 'quantity': case 'supplierInitialValue': case 'supplierInitialVat':
			case 'commissionPercent': case 'commissionInitialValue': case 'discountPercent': case 'discountInitialValue':
			case 'serviceFeePercent': case 'serviceFeeInitialValue': case 'taxInitialValue': case 'taxInitialVat':
			case 'supplierInitialCurrency': case 'commissionInitialCurrency': case 'discountInitialCurrency':
			case 'serviceFeeInitialCurrency': case 'taxInitialCurrency': {
				$this->fields[$var]=$value;
				break;
			}

			// check id customer
			case 'idCustomer': {
				if ($value and (!is_numeric($value) or $value<0) ) throw new \Exception('Customer id must be a positive numeric value in `'.__CLASS__.'` class.');
				$this->fields[$var]=$value;
				break;
			}
			// check customer type
			case 'customerType': {
				global $CFG;
				if ($value and !in_array($value, $CFG['generalData']['personTypes'])) throw new \Exception("The customer type `$value` is not valid.");
				$this->fields[$var]=$value;
				break;
			}

			// $var IS NOT VALID - throws Exception
			default: {
				throw new \Exception("Wrong var ({$var}) for '".__METHOD__."'.");
			}

		}

	}

	/**
	 * Populate the offer item with information from another offer item
	 * @param \Entity\OfferItem $offerItem
	 */
	public function populateFromOfferItem(\Entity\OfferItem $offerItem)
	{
		if ($offerItem->getOperation()!='select' or $this->getOperation()!='insert') {
			throw new \Exception("Invalid operation for '".__METHOD__."'.");
		}

		// POPULATE THE OFFER ITEM
		$this->idCustomer 			= $offerItem->idCustomer;
		$this->customerType			= $offerItem->customerType;
		$this->productMultiplier	= $offerItem->productMultiplier;

		$this->quantity				= $offerItem->quantity;

		// supplier
		if ($offerItem->supplierInitialCurrency) {
			$this->supplierInitialValue		= $offerItem->supplierInitialValue;
			$this->supplierInitialVat		= $offerItem->supplierInitialVat;
			$this->supplierInitialCurrency	= $offerItem->supplierInitialCurrency;
		}

		// commission
		if ($offerItem->commissionInitialCurrency) {
			$this->commissionInitialValue	= $offerItem->commissionInitialValue;
			$this->commissionInitialCurrency= $offerItem->commissionInitialCurrency;
			$this->commissionPercent		= $offerItem->commissionPercent;
		}

		// discount
		if ($offerItem->discountInitialCurrency) {
			$this->discountInitialValue  				= $offerItem->discountInitialValue;
			$this->discountInitialCurrency				= $offerItem->discountInitialCurrency;
			$this->discountPercent						= $offerItem->discountPercent;
		}

		// serviceFee
		if ($offerItem->serviceFeeInitialCurrency) {
			$this->serviceFeeInitialValue	= $offerItem->serviceFeeInitialValue;
			$this->serviceFeeInitialCurrency= $offerItem->serviceFeeInitialCurrency;
		}

		//  tax
		if ($offerItem->taxInitialCurrency) {
			$this->taxInitialValue		= $offerItem->taxInitialValue;
			$this->taxInitialVat  		= $offerItem->taxInitialVat;
			$this->taxInitialCurrency	= $offerItem->taxInitialCurrency;
		}


	}

}

