<?php
namespace Entity;

class BspPeriod 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 = true;

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

	/**
	 * (non-PHPdoc)
	 * @see \App\Entity\SelectAbstract::getAttribute()
	 */
	protected function getAttribute($var)
	{
		// CHECK EVERY POSSIBLE VALUES THAT $var MAY HAVE -----------------------------------------
		switch ($var) {
			// idOffice
			case 'office': {
				return \Entity\Office::select($this->fields['idOffice'], $this->selectForUpdate);
			}

			// addUser
			case 'addUser': {
				return \Entity\User::select($this->fields['idAddUser'], $this->selectForUpdate);
			}

			// updateUser
			case 'updateUser': {
				return \Entity\User::select($this->fields['idUpdateUser'], $this->selectForUpdate);
			}

			// the rest of the fields (most of them are just copied)
			default: {
				return parent::getAttribute($var);
			}
		}
	}

	/**
	 * (non-PHPdoc)
	 * @see App\Entity.GeneralAbstract::setAttribute()
	 */
	protected function setAttribute($var, $value)
	{
		// check that operation is okay
		if ($this->operation!='insert' and $this->operation!='update') {
			throw new \Exception('You can use __set magic method only with insert/update operation in `'.__CLASS__.'` class.');
		}

		// CHECK EACH POSSIBLE FIELD VALUE --------------------------------------------------------
		switch ($var) {
			// simple fields
			case 'group': case 'periodStart': case 'periodEnd': case 'filename': case 'fileType': case 'idOffice': case 'iataCode': case 'description': {
				$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_executeDelete(Array $saveData)
	{
		// EXECUTE SQL ----------------------------------------------------------------------------
		$this->save_executeSql_delete($this->tableName, $this->tablePrimaryKey, $this->id);

		// add success message
		$this->messages->addSuccessCode('delete_ok');

		$db=\DB::getInstance();

		// compose and run SQL
		global $DBTBspRecords, $DBTBspRecordsData, $DBTBspSettlementRecords;

		// first, clear the settlements table, as the data in it might interact with re-processing this same file
		$query = "UPDATE `{$DBTBspSettlementRecords}` SET `bspRecordId`=NULL WHERE `bspRecordId` IN (SELECT `id` FROM `{$DBTBspRecords}` WHERE `idPeriod`=?)";
		$db->Execute($query, [$this->id]);
		if ($db->ErrorNo()) {
			throw new \DBException("Error in method ".__METHOD__." - called class: ".get_called_class().".");
		}

		// clear all BSP-period related records
		$query = "DELETE FROM `{$DBTBspRecordsData}` WHERE `id` IN (SELECT `id` FROM `{$DBTBspRecords}` WHERE `idPeriod`=?)";
		$db->Execute($query, [$this->id]);
		if ($db->ErrorNo()) {
			throw new \DBException("Error in method ".__METHOD__." - called class: ".get_called_class().".");
		}

		$query = "DELETE FROM `{$DBTBspRecords}` WHERE `idPeriod`=?";
		$db->Execute($query, [$this->id]);
		if ($db->ErrorNo()) {
			throw new \DBException("Error in method ".__METHOD__." - called class: ".get_called_class().".");
		}
	}
}
