<?php

namespace Entity;

class Validator_MiceProject extends \App\Entity\Utils\Extension_ValidateDefaultAbstract
{

	/**
	 * Check if the user has access to this option.
	 */
	protected function isAccessible_insert()
	{
		// current collected messages
		$currMessages=$this->entity->getMessages();

		// check user access
		if (!\App\UserAccess\CurrentUserAccessFast::getInstance()->hasAccessToOption('Modules/Orders/MiceProjects-insert')) {
			$currMessages->addWarningCode('noAccessToOption');
		}

	}

	/**
	 * Validates the fields for inserting
	 */
	protected function check_insert()
	{

		// get current fields details
		$fields=$this->entity->getFields();

		// current collected messages
		$currMessages=$this->entity->getMessages();

		// check that the project name is unique
		if ($fields['name']) {
			$prjListObj = new \App\EntityList\MiceProjects();
			$prjListObj->setFilter('name', 'LIKE', $fields['name']);
			$prjListObj->setLimit(1);
			$prjList = $prjListObj->exportRecords();
			if ($prjList) {
				$currMessages->addWarningCode('nameNotUnique', 'name');
			}
		}

		// check requester
		if ($fields['idCorporate']) {
			$corp = \Entity\Corporate::select($fields['idCorporate']);
			if (!$corp->recordExists()) {
				$currMessages->addErrorCode('requesterInvalidId', 'idCorporate');
			} elseif ($corp->deleted) {
				$currMessages->addWarningCode('requesterDeleted', 'idCorporate');
			}
		}

		// check contact person
		if ($fields['idContactPerson']) {
			$ctc = ContactPerson::select($fields['idContactPerson']);
			if (!$ctc->recordExists()) {
				$currMessages->addErrorCode('ctcPersInvalidId', 'idContactPerson');
			} elseif ($ctc->deleted) {
				$currMessages->addWarningCode('ctcPersDeleted', 'idContactPerson');
			}
			if (!$fields['idCorporate']) {
				$currMessages->addWarningCode('ctcPersNoCorp', 'idCorporate');
			} elseif ($ctc->idCompany!=$fields['idCorporate']) {
				$currMessages->addErrorCode('ctcPersInvalidCorp', 'idCorporate');
			}
		}

		// check project type
		if ($fields['idType']) {
			$prt = MiceProjectType::select($fields['idType']);
			if (!$prt->recordExists() or $prt->deleted) {
				$currMessages->addErrorCode('prjTypeInvalid', 'idType');
			}
		}

		// check responsible user
		if ($fields['idResponsibleUser']) {
			$resp = User::select($fields['idResponsibleUser']);
			if (!$resp->recordExists() or $resp->deleted) {
				$currMessages->addErrorCode('respUserInvalid', 'idResponsibleUser');
			}
		}

		// check location
		if ($fields['countryCode']) {
			$cntr = Country::select($fields['countryCode']);
			if (!$cntr->recordExists()) {
				$currMessages->addErrorCode('cntrInvalid', 'location');
			}
		}
		if ($fields['cityId']) {
			$city = City::select($fields['cityId']);
			if (!$city->recordExists() or $city->deleted) {
				$currMessages->addErrorCode('cityInvalid', 'location');
			}
			if ($city->countryCode!=$fields['countryCode']) {
				$currMessages->addErrorCode('cityInvalidCntr', 'location');
			}
		}

		// check that the code is unique
		if ($fields['code']) {
			$prjListObj = new \App\EntityList\MiceProjects();
			$prjListObj->setColumn('name');
			$prjListObj->setFilter('code', 'LIKE', $fields['code']);
			$prjListObj->setLimit(1);
			$prjList = $prjListObj->exportRecords();
			if ($prjList) {
				$currMessages->addWarningCode('codeNotUnique', 'code', $prjList[0]['name']);
			}
		}

	}

	/**
	 * Check if the user has access to this option.
	 */
	protected function isAccessible_update()
	{
		// current collected messages
		$currMessages=$this->entity->getMessages();

		// check user access
		if (!\App\UserAccess\CurrentUserAccessFast::getInstance()->hasAccessToOption('Modules/Orders/MiceProjects-update')) {
			$currMessages->addWarningCode('noAccessToOption');
		}

	}


	protected function check_update()
	{

		// get current fields details
		$fields=$this->entity->getFields();

		// current collected messages
		$currMessages=$this->entity->getMessages();

		$currentIdEntity = $this->entity->getId();

		// check the project id
		$prj = MiceProject::selectForUpdate($currentIdEntity);
		if (!$prj->recordExists()) {
			$currMessages->addErrorCode('update_invalidId');
		} elseif ($prj->deleted) {
			$currMessages->addWarningCode('update_deletedPrj');
		}

		// check that the project name is unique
		if ($fields['name']) {
			$prjListObj = new \App\EntityList\MiceProjects();
			$prjListObj->setFilter('name', 'LIKE', $fields['name']);
			$prjListObj->setFilter('id', '!=', $currentIdEntity);
			$prjListObj->setLimit(1);
			$prjList = $prjListObj->exportRecords();
			if ($prjList) {
				$currMessages->addWarningCode('nameNotUnique', 'name');
			}
		}

		// check requester
		if ($fields['idCorporate']) {
			$corp = \Entity\Corporate::select($fields['idCorporate']);
			if (!$corp->recordExists()) {
				$currMessages->addErrorCode('requesterInvalidId', 'idCorporate');
			} elseif ($corp->deleted) {
				$currMessages->addWarningCode('requesterDeleted', 'idCorporate');
			}
		}

		// check contact person
		if ($fields['idContactPerson']) {
			$ctc = ContactPerson::select($fields['idContactPerson']);
			if (!$ctc->recordExists()) {
				$currMessages->addErrorCode('ctcPersInvalidId', 'idContactPerson');
			} elseif ($ctc->deleted) {
				$currMessages->addWarningCode('ctcPersDeleted', 'idContactPerson');
			}
			if (!$fields['idCorporate']) {
				$currMessages->addWarningCode('ctcPersNoCorp', 'idCorporate');
			} elseif ($ctc->idCompany!=$fields['idCorporate']) {
				$currMessages->addErrorCode('ctcPersInvalidCorp', 'idCorporate');
			}
		}

		// check project type
		if ($fields['idType']) {
			$prt = MiceProjectType::select($fields['idType']);
			if (!$prt->recordExists() or $prt->deleted) {
				$currMessages->addErrorCode('prjTypeInvalid', 'idType');
			}
		}

		// check responsible user
		if ($fields['idResponsibleUser']) {
			$resp = User::select($fields['idResponsibleUser']);
			if (!$resp->recordExists() or $resp->deleted) {
				$currMessages->addErrorCode('respUserInvalid', 'idResponsibleUser');
			}
		}

		// check location
		if ($fields['countryCode']) {
			$cntr = Country::select($fields['countryCode']);
			if (!$cntr->recordExists()) {
				$currMessages->addErrorCode('cntrInvalid', 'location');
			}
		}
		if ($fields['cityId']) {
			$city = City::select($fields['cityId']);
			if (!$city->recordExists() or $city->deleted) {
				$currMessages->addErrorCode('cityInvalid', 'location');
			}
			if ($city->countryCode!=$fields['countryCode']) {
				$currMessages->addErrorCode('cityInvalidCntr', 'location');
			}
		}

		// check that the code is unique
		if ($fields['code']) {
			$prjListObj = new \App\EntityList\MiceProjects();
			$prjListObj->setColumn('name');
			$prjListObj->setFilter('code', 'LIKE', $fields['code']);
			$prjListObj->setFilter('id', '!=', $currentIdEntity);
			$prjListObj->setLimit(1);
			$prjList = $prjListObj->exportRecords();
			if ($prjList) {
				$currMessages->addWarningCode('codeNotUnique', 'code', $prjList[0]['name']);
			}
		}

	}


	/**
	 * Check if the user has access to this option.
	 */
	protected function isAccessible_delete()
	{
		// current collected messages
		$currMessages=$this->entity->getMessages();

		// check user access
		if (!\App\UserAccess\CurrentUserAccessFast::getInstance()->hasAccessToOption('Modules/Orders/MiceProjects-delete')) {
			$currMessages->addWarningCode('noAccessToOption');
		}

	}



	/**
	 * Validates the fields for deleting
	 */
	protected function check_delete()
	{

		// current entity id
		$currentIdEntity=$this->entity->getId();

		// current collected messages
		$currMessages=$this->entity->getMessages();

		// the current project
		$prj = MiceProject::selectForUpdate($currentIdEntity);

		if (!$prj->recordExists()) {
			$currMessages->addErrorCode('delete_invalidId');
		} elseif ($prj->deleted) {
			$currMessages->addWarningCode('delete_deletedPrj');
		}

	}
}

