/**
  * Add the financial types.
  *
  * @param array $params
  *   Reference array contains the values submitted by the form.
  *
  * @return CRM_Financial_DAO_FinancialAccount
  */
 public static function add(&$params)
 {
     if (empty($params['id'])) {
         $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE);
         $params['is_deductible'] = CRM_Utils_Array::value('is_deductible', $params, FALSE);
         $params['is_tax'] = CRM_Utils_Array::value('is_tax', $params, FALSE);
         $params['is_header_account'] = CRM_Utils_Array::value('is_header_account', $params, FALSE);
         $params['is_default'] = CRM_Utils_Array::value('is_default', $params, FALSE);
     }
     if (!empty($params['id']) && !empty($params['financial_account_type_id']) && CRM_Financial_BAO_FinancialAccount::validateFinancialAccount($params['id'], $params['financial_account_type_id'])) {
         throw new CRM_Core_Exception(ts('You cannot change the account type since this financial account refers to a financial item having an account type of Revenue/Liability.'));
     }
     if (!empty($params['is_default'])) {
         $query = 'UPDATE civicrm_financial_account SET is_default = 0 WHERE financial_account_type_id = %1';
         $queryParams = array(1 => array($params['financial_account_type_id'], 'Integer'));
         CRM_Core_DAO::executeQuery($query, $queryParams);
     }
     // action is taken depending upon the mode
     $financialAccount = new CRM_Financial_DAO_FinancialAccount();
     if (!empty($params['id'])) {
         $financialAccount->id = $params['id'];
         $financialAccount->find(TRUE);
     }
     $financialAccount->copyValues($params);
     //CRM-16189
     $accountType = CRM_Core_PseudoConstant::accountOptionValues('financial_account_type', NULL, " AND v.name IN ('Liability', 'Asset') ");
     if (empty($params['id']) && !CRM_Utils_Array::value($financialAccount->financial_account_type_id, $accountType)) {
         $financialAccount->opening_balance = $financialAccount->current_period_opening_balance = '0.00';
     }
     $financialAccount->save();
     return $financialAccount;
 }
 /**
  * Test getting financial account for a given financial Type with a particular relationship.
  */
 public function testValidateFinancialAccount()
 {
     // Create a record with financial item having financial account as Event Fee.
     $this->createParticipantWithContribution();
     $financialAccounts = CRM_Contribute_PseudoConstant::financialAccount();
     $financialAccountId = array_search('Event Fee', $financialAccounts);
     $message = CRM_Financial_BAO_FinancialAccount::validateFinancialAccount($financialAccountId);
     $this->assertTrue($message, "The financial account cannot be deleted. Failed asserting this was true.");
     $financialAccountId = array_search('Member Dues', $financialAccounts);
     $message = CRM_Financial_BAO_FinancialAccount::validateFinancialAccount($financialAccountId);
     $this->assertFalse($message, "The financial account can be deleted. Failed asserting this was true.");
 }
 /**
  * Build the form object.
  */
 public function buildQuickForm()
 {
     parent::buildQuickForm();
     $this->setPageTitle(ts('Financial Account'));
     if ($this->_action & CRM_Core_Action::DELETE) {
         return;
     }
     $this->applyFilter('__ALL__', 'trim');
     $attributes = CRM_Core_DAO::getAttribute('CRM_Financial_DAO_FinancialAccount');
     $this->add('text', 'name', ts('Name'), $attributes['name'], TRUE);
     $this->addRule('name', ts('A financial type with this name already exists. Please select another name.'), 'objectExists', array('CRM_Financial_DAO_FinancialAccount', $this->_id));
     $this->add('text', 'description', ts('Description'), $attributes['description']);
     $this->add('text', 'accounting_code', ts('Accounting Code'), $attributes['accounting_code']);
     $elementAccounting = $this->add('text', 'account_type_code', ts('Account Type Code'), $attributes['account_type_code']);
     $this->addEntityRef('contact_id', ts('Owner'), array('api' => array('params' => array('contact_type' => 'Organization')), 'create' => TRUE));
     $this->add('text', 'tax_rate', ts('Tax Rate'), $attributes['tax_rate']);
     $this->add('checkbox', 'is_deductible', ts('Tax-Deductible?'));
     $elementActive = $this->add('checkbox', 'is_active', ts('Enabled?'));
     $this->add('checkbox', 'is_tax', ts('Is Tax?'));
     $element = $this->add('checkbox', 'is_default', ts('Default?'));
     // CRM-12470 freeze is default if is_default is set
     if ($this->_id && CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialAccount', $this->_id, 'is_default')) {
         $element->freeze();
     }
     //CRM-16189
     if (CRM_Contribute_BAO_Contribution::checkContributeSettings('financial_account_bal_enable')) {
         $this->add('text', 'opening_balance', ts('Opening Balance'), $attributes['opening_balance']);
         $this->add('text', 'current_period_opening_balance', ts('Current Period Opening Balance'), $attributes['current_period_opening_balance']);
         $financialAccountType = CRM_Core_PseudoConstant::get('CRM_Financial_DAO_FinancialAccount', 'financial_account_type_id', array('labelColumn' => 'name'));
         $limitedAccount = array(array_search('Asset', $financialAccountType), array_search('Liability', $financialAccountType));
         $this->assign('limitedAccount', json_encode($limitedAccount));
     }
     $financialAccountType = CRM_Core_PseudoConstant::get('CRM_Financial_DAO_FinancialAccount', 'financial_account_type_id');
     if (!empty($financialAccountType)) {
         $element = $this->add('select', 'financial_account_type_id', ts('Financial Account Type'), array('' => '- select -') + $financialAccountType, TRUE, array('class' => 'crm-select2 huge'));
         if ($this->_isARFlag) {
             $element->freeze();
             $elementAccounting->freeze();
             $elementActive->freeze();
         } elseif ($this->_id && CRM_Financial_BAO_FinancialAccount::validateFinancialAccount($this->_id)) {
             $element->freeze();
         }
     }
     if ($this->_action == CRM_Core_Action::UPDATE && CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialAccount', $this->_id, 'is_reserved')) {
         $this->freeze(array('name', 'description', 'is_active'));
     }
     $this->addFormRule(array('CRM_Financial_Form_FinancialAccount', 'formRule'), $this);
 }