<?php
namespace Entity;

// TODO: replace bankName and bankName2 with name and name2
// TODO: replace bankCode with code

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

	protected function isAccessible_insert()
	{
		// check user access
		if (!\App\UserAccess\CurrentUserAccessFast::getInstance()->hasAccessToOption('Modules/Admin/Banks-insert')) {
			$this->entity->getMessages()->addWarningCode('noAccessToOption');
		}
	}

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

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

		// check that name(standard) is unique
		if ($fields['nameStandard']) {
			$bankObj = new \App\EntityList\Banks();
			$bankObj->setCustomFilter('banks.bankName=? OR banks.bankName2=?', [$fields['nameStandard'], $fields['nameStandard']]);
			$bankObj->setLimit(1);
			$bankArray = $bankObj->exportRecords();

			if ($bankArray) {
				$currentMessages->addWarningCode('insert_nameNotUnique');
			}
		}

		// check that name(local) is unique
		if ($fields['nameLocal']) {
			$bankObj = new \App\EntityList\Banks();
			$bankObj->setCustomFilter('banks.bankName=? OR banks.bankName2=?', [$fields['nameLocal'], $fields['nameLocal']]);
			$bankObj->setLimit(1);
			$bankArray = $bankObj->exportRecords();

			if ($bankArray) {
				$currentMessages->addWarningCode('insert_nameNotUnique');
			}
		}
	}

	protected function isAccessible_update()
	{
		// check user access
		if (!\App\UserAccess\CurrentUserAccessFast::getInstance()->hasAccessToOption('Modules/Admin/Banks-update')) {
			$this->entity->getMessages()->addWarningCode('noAccessToOption');
		}
	}

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

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

		// lock records(if it is the case)
		$bank = Bank::select($currentEntityId);

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

	}

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

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

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

		// check that standard name is unique
		if ($fields['nameStandard']) {
			$bankOfficeObj = new \App\EntityList\Banks();
			$bankOfficeObj->setFilter('banks.id', '!=', $currentEntityId);
			$bankOfficeObj->setCustomFilter('banks.bankName=? OR banks.bankName2=?', [$fields['nameStandard'], $fields['nameStandard']]);
			$bankOfficeObj->setLimit(1);
			$bankOfficeArray = $bankOfficeObj->exportRecords();

			// check standart name
			if ($bankOfficeArray) {
				$currentMessages->addWarningCode('update_nameNotUnique');
			}
		}

		// check that local name is unique
		if ($fields['nameLocal']) {
			$bankOfficeObj = new \App\EntityList\Banks();
			$bankOfficeObj->setFilter('banks.id', '!=', $currentEntityId);
			$bankOfficeObj->setCustomFilter('banks.bankName=? OR banks.bankName2=?', [$fields['nameLocal'], $fields['nameLocal']]);
			$bankOfficeObj->setLimit(1);
			$bankOfficeArray = $bankOfficeObj->exportRecords();

			// check standart name
			if ($bankOfficeArray) {
				$currentMessages->addWarningCode('update_nameNotUnique');
			}
		}
	}

	protected function isAccessible_delete()
	{
		// check user access
		if (!\App\UserAccess\CurrentUserAccessFast::getInstance()->hasAccessToOption('Modules/Admin/Banks-delete')) {
			$this->entity->getMessages()->addWarningCode('noAccessToOption');
		}
	}

	protected function isAvailable_delete()
	{
		$OK = true;

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

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

		// check if task category is not already deleted
		$bank = \Entity\Bank::select($currentEntityId);

		if (!$bank->recordExists()) {
			$currentMessages->addErrorCode('delete_idNotValid');
		}
		elseif ($bank->deleted) {
			$currentMessages->addWarningCode('delete_alreadyDeleted');
			$OK = false;
		}


		// CHECK IF THE BANK HAS OFFICES ----------------------------------------------------------
		if ($OK) {
			$bankOfficesObj = new \App\EntityList\BankOffices();
			$bankOfficesObj->setFilter('bankOffices.idBank', 'LIKE', $currentEntityId);
			$bankOfficesObj->setLimit(1);
			$bankOfficesArray = $bankOfficesObj->exportRecords();

			if ($bankOfficesArray) {
				$currentMessages->addWarningCode('delete_offAssoc');
				$OK = false;
			}
		}


		// CHECK THAT THE BANK OFFICE IS NOT USED ON SUPPLIERS OFFICE BANK ACCOUNTS ---------------
		if ($OK) {
			$supplierBankAccountObj = new \App\EntityList\SupplierBankAccounts();
			$supplierBankAccountObj->setFilter('suppBankAccounts.idBank', '=', $currentEntityId);
			$supplierBankAccountObj->setLimit(1);
			$supplierBankAccountArray = $supplierBankAccountObj->exportRecords();

			if ($supplierBankAccountArray) {
				$currentMessages->addWarningCode('delete_suppAssoc');
				$OK = false;
			}

		}


		// CHECK THAT THE BANK OFFICE IS NOT USED ON CORPORATE OFFICE BANK ACCOUNTS ---------------
		if ($OK) {
			$corporateOfficeBankAccountObj = new \App\EntityList\CorporateOfficeBankAccounts();
			$corporateOfficeBankAccountObj->setFilter('corpOffBankAccounts.idBank', '=', $currentEntityId);
			$corporateOfficeBankAccountObj->setLimit(1);
			$corporateOfficeBankAccountArray = $corporateOfficeBankAccountObj->exportRecords();

			if ($corporateOfficeBankAccountArray) {
				$currentMessages->addWarningCode('delete_corpOffAssoc');
				$OK = false;
			}

		}

		// CHECK THAT THE BANK OFFICE IS NOT USED ON AGENCY BANK ACCOUNTS -------------------------
		if ($OK) {

			$agencyBankAccountsObj = new \App\EntityList\AgencyBankAccounts();
			$agencyBankAccountsObj->setFilter('bas.idBank', '=', $currentEntityId);
			$agencyBankAccountsObj->setLimit(1);
			$agencyBankAccountsArray = $agencyBankAccountsObj->exportRecords();

			if ($agencyBankAccountsArray) {
				$currentMessages->addWarningCode('delete_agencyAssoc');
				$OK = false;
			}
		}


		// CHECK THAT THE BANK OFFICE IS NOT USED ON CUSTOMERS ------------------------------------
		if ($OK) {

			$customersObj = new \App\EntityList\Customers();
			$customersObj->setFilter('cust.idBank', '=', $currentEntityId);
			$customersObj->setLimit(1);
			$customersArray = $customersObj->exportRecords();

			if ($customersArray) {
				$currentMessages->addWarningCode('delete_customerAssoc');
				$OK = false;
			}
		}
	}

}