<?php
namespace Entity;

class BspSettlementRecord extends \App\Entity\GeneralAbstract
{

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

	/**
	 * Indicate how many seconds the external cache is valid. 0 for unlimited, -1 for no cache.
	 * @var Int
	 */
	const cacheTtl=7200;	// 60*60*2

	/**
	 * The table name containing service data
	 * @var string
	 */
	protected $dataTableName;

	protected $tablePrimaryKey = 'key';
	protected $primaryKeyType = 'string';

	/**
	 * (non-PHPdoc)
	 * @see \App\Entity\SelectAbstract::setTableName()
	 */
	protected function setTableName(): void
	{
		global $DBTBspSettlementRecords;
		$this->tableName = $DBTBspSettlementRecords;
	}

	/**
	 * Static method for delete a service.
	 * @return self
	 */
	static public function delete($idService)
	{
		throw new \Exception('Not developed.');
	}

	/**
	 * Update the operational status of an entity.
	 *
	 * @param string
	 * @return self
	 */
	static public function updateOperationalStatus($idEntity)
	{
		return new self('updateOperationalStatus', $idEntity);
	}


	/**
	 * Returns the corresponding field of the selected entity
	 *
	 * @param String $var
	 * @return Mixed
	 */
	protected function getAttribute($var)
	{
		// CHECK EVERY POSSIBLE VALUES THAT $var MAY HAVE -----------------------------------------
		switch ($var) {
			// serialized fields
			case 'status': {
				if (!is_array($this->fields[$var])) {
					$this->fields[$var] = explode(",", $this->fields[$var]);
				}

				return $this->fields[$var];
			}

			// the rest of the fields (most of them are just copied)
			default: {
				return parent::getAttribute($var);
			}
		}
	}
	/**
	 * Sets the corresponding field of the inserting/updating entity
	 *
	 * @param String $var
	 * @return Mixed
	 */
	protected function setAttribute($var, $value)
	{
		// CHECK EACH POSSIBLE FIELD VALUE --------------------------------------------------------
		switch ($var) {
			// simple fields
			case 'key': case 'bspRecordId': case 'importedServiceId': case 'importedServiceIsRefund': case 'status': case 'ticketStatus': case 'ticketNumber': case 'ticketDate': case 'idOffice': case 'comparisonInProgress':
			{
				$this->fields[$var] = $value;
				break;
			}

			case 'operationalStatus':  case 'operationalDateTime':  case 'operationalUser':
			{
				if ($this->operation != 'updateOperationalStatus') {
					throw new \Exception("The field '$var' can only be set when the operation is 'updateOperationalStatus'.");
				}

				$this->fields[$var] = $value;
				break;
			}

			// the field is not valid
			default: {
				throw new \Exception("The field '$var' is not valid for the `".__CLASS__."` entity.");
			}
		}
	}

	protected function save_filterSaveData(Array $saveData)
	{
		if ($saveData['status']) {
			$saveData['status'] = implode(",", $saveData['status']);
		}
		return $saveData;
	}

	/**
	 * (non-PHPdoc)
	 * @see \App\Entity\GeneralAbstract::save_finalSaveData()
	 */
	protected function save_finalSaveData(Array $saveData)
	{
		if ($this->operation == 'insert') {
			$saveData['addTime'] =  DATETIME;	// this should always remain DATETIME (the constant), because its used in the PluginReports Iterators, and the cronjob doing the matches depend on this to optimize the searched records to a smaller range
		}

		if ($this->operation == 'updateOperationalStatus') {
			$saveData['operationalDateTime'] =  DATETIME;
			$saveData['operationalUser'] = 		CURRENT_ID_USER;
		}

		return $saveData;
	}
}
