<?php
namespace Entity;

class Validator_CorporateCreditCard extends \App\Entity\Utils\Extension_ValidateDefaultAbstract
{
	protected function isAccessible_insert()
	{
		// current collected messages
		$currentMessages = $this->entity->getMessages();

		// check user access
		if (!\App\UserAccess\CurrentUserAccessFast::getInstance()->hasAccessToOption('Modules/Partners/Corporates/CreditCards-insert')) {
			$currentMessages->addWarningCode('noAccessToOption');
		}
	}

	protected function check_insert()
	{
		$fields = $this->entity->getFields();

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

		// check if corporate is valid
		if ($fields['idCorporate']) {
			$corporateEnt = \Entity\Corporate::selectForUpdate($fields['idCorporate']);

			if (!$corporateEnt->recordExists()) {
				$currentMessages->addErrorCode('insert_invalidCorporateId');
			}
			elseif ($corporateEnt->deleted) {
				$currentMessages->addWarningCode('insert_deletedCorporateId');
			}
		}

	}

	protected function isAccessible_update()
	{
		// current collected messages
		$currentMessages = $this->entity->getMessages();

		// check user access
		if (!\App\UserAccess\CurrentUserAccessFast::getInstance()->hasAccessToOption('Modules/Partners/Corporates/CreditCards-update')) {
			$currentMessages->addWarningCode('noAccessToOption');
		}
	}

	protected function isAvailable_update()
	{
		// current collected messages
		$currentMessages = $this->entity->getMessages();

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

		$corporateCC = \Entity\CorporateCreditCard::selectForUpdate($currentEntityId);

		if (!$corporateCC->recordExists()) {
			$currentMessages->addErrorCode('update_invalidId');
		}
		elseif ($corporateCC->deleted) {
			$currentMessages->addWarningCode('update_deletedId');
		}
	}

	protected function check_update()
	{
		// get current fields details
		$fields = $this->entity->getFields();

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

		if ($fields['idCorporate']) {
			$corporateEnt = \Entity\Corporate::selectForUpdate($fields['idCorporate']);

			if (!$corporateEnt->recordExists()) {
				$currentMessages->addErrorCode('update_invalidCorporateId');
			}
			elseif ($corporateEnt->deleted) {
				$currentMessages->addWarningCode('update_deletedCorporateId');
			}
		}

	}

	protected function isAccessible_delete()
	{
		// current collected messages
		$currentMessages = $this->entity->getMessages();

		if (!\App\UserAccess\CurrentUserAccessFast::getInstance()->hasAccessToOption('Modules/Partners/Corporates/CreditCards-delete')) {
			$currentMessages->addWarningCode('noAccessToOption');
		}
	}

	protected function isAvailable_delete()
	{
		// current entity id
		$currentEntityId = $this->entity->getId();

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

		$corporateCC = \Entity\CorporateCreditCard::selectForUpdate($currentEntityId);

		if (!$corporateCC->recordExists()) {
			$currentMessages->addErrorCode('delete_invalidId');
		}
		elseif ($corporateCC->deleted) {
			$currentMessages->addWarningCode('delete_deletedId');
		}
	}

}