/** * @param $config */ public static function jqFinancialRelation($config) { if (!isset($_GET['_value']) || empty($_GET['_value'])) { CRM_Utils_System::civiExit(); } if ($_GET['_value'] != 'select') { $financialAccountType = CRM_Financial_BAO_FinancialAccount::getfinancialAccountRelations(TRUE); $financialAccountId = CRM_Utils_Request::retrieve('_value', 'Positive', CRM_Core_DAO::$_nullObject); $financialAccountTypeId = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialAccount', $financialAccountId, 'financial_account_type_id'); } $params['orderColumn'] = 'label'; $result = CRM_Core_PseudoConstant::get('CRM_Financial_DAO_EntityFinancialAccount', 'account_relationship', $params); $elements = array(array('name' => ts('- Select Financial Account Relationship -'), 'value' => 'select')); $countResult = count($financialAccountType[$financialAccountTypeId]); if (!empty($result)) { foreach ($result as $id => $name) { if (in_array($id, $financialAccountType[$financialAccountTypeId]) && $_GET['_value'] != 'select') { if ($countResult != 1) { $elements[] = array('name' => $name, 'value' => $id); } else { $elements[] = array('name' => $name, 'value' => $id, 'selected' => 'Selected'); } } elseif ($_GET['_value'] == 'select') { $elements[] = array('name' => $name, 'value' => $id); } } } CRM_Utils_JSON::output($elements); }
/** * Validate account relationship with financial account type * * @param obj $financialTypeAccount of CRM_Financial_DAO_EntityFinancialAccount * */ public static function validateRelationship($financialTypeAccount) { $financialAccountLinks = CRM_Financial_BAO_FinancialAccount::getfinancialAccountRelations(); $financialAccountType = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialAccount', $financialTypeAccount->financial_account_id, 'financial_account_type_id'); if (CRM_Utils_Array::value($financialTypeAccount->account_relationship, $financialAccountLinks) != $financialAccountType) { $accountRelationships = CRM_Core_PseudoConstant::get('CRM_Financial_DAO_EntityFinancialAccount', 'account_relationship'); $params = array(1 => $accountRelationships[$financialTypeAccount->account_relationship]); throw new Exception(ts("This financial account cannot have '%1' relationship.", $params)); } }
/** * Test getting financial account relations for a given financial type. */ public function testGetFinancialAccountRelations() { $fAccounts = $rAccounts = array(); $relations = CRM_Financial_BAO_FinancialAccount::getfinancialAccountRelations(); $links = array('Expense Account is' => 'Expenses', 'Accounts Receivable Account is' => 'Asset', 'Income Account is' => 'Revenue', 'Asset Account is' => 'Asset', 'Cost of Sales Account is' => 'Cost of Sales', 'Premiums Inventory Account is' => 'Asset', 'Discounts Account is' => 'Revenue', 'Sales Tax Account is' => 'Liability', 'Deferred Revenue Account is' => 'Liability'); $dao = CRM_Core_DAO::executeQuery("SELECT ov.value, ov.name\n FROM civicrm_option_value ov\n INNER JOIN civicrm_option_group og ON og.id = ov.option_group_id\n AND og.name = 'financial_account_type'"); while ($dao->fetch()) { $fAccounts[$dao->value] = $dao->name; } $dao = CRM_Core_DAO::executeQuery("SELECT ov.value, ov.name\n FROM civicrm_option_value ov\n INNER JOIN civicrm_option_group og ON og.id = ov.option_group_id\n AND og.name = 'account_relationship'"); while ($dao->fetch()) { $rAccounts[$dao->value] = $dao->name; } foreach ($links as $accountRelation => $accountType) { $financialAccountLinks[array_search($accountRelation, $rAccounts)] = array_search($accountType, $fAccounts); } $this->assertTrue($relations == $financialAccountLinks, "The two arrays are not the same"); }
/** * Build the form object. */ public function buildQuickForm() { parent::buildQuickForm(); $this->setPageTitle(ts('Financial Type Account')); if ($this->_action & CRM_Core_Action::DELETE) { return; } if (isset($this->_id)) { $params = array('id' => $this->_id); CRM_Financial_BAO_FinancialTypeAccount::retrieve($params, $defaults); $this->setDefaults($defaults); $financialAccountTitle = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialAccount', $defaults['financial_account_id'], 'name'); } $this->applyFilter('__ALL__', 'trim'); if ($this->_action == CRM_Core_Action::UPDATE) { $this->assign('aid', $this->_id); // hidden field to catch the group id in profile $this->add('hidden', 'financial_type_id', $this->_aid); // hidden field to catch the field id in profile $this->add('hidden', 'account_type_id', $this->_id); } $params['orderColumn'] = 'label'; $AccountTypeRelationship = CRM_Core_PseudoConstant::get('CRM_Financial_DAO_EntityFinancialAccount', 'account_relationship', $params); if (!empty($AccountTypeRelationship)) { $element = $this->add('select', 'account_relationship', ts('Financial Account Relationship'), array('select' => ts('- Select Financial Account Relationship -')) + $AccountTypeRelationship, TRUE); } if ($this->_isARFlag) { $element->freeze(); } if ($this->_action == CRM_Core_Action::ADD) { if (!empty($this->_submitValues['account_relationship']) || !empty($this->_submitValues['financial_account_id'])) { $financialAccountType = CRM_Financial_BAO_FinancialAccount::getfinancialAccountRelations(); $financialAccountType = CRM_Utils_Array::value($this->_submitValues['account_relationship'], $financialAccountType); $result = CRM_Contribute_PseudoConstant::financialAccount(NULL, $financialAccountType); $financialAccountSelect = array('' => ts('- select -')) + $result; } else { $financialAccountSelect = array('select' => ts('- select -')) + CRM_Contribute_PseudoConstant::financialAccount(); } } if ($this->_action == CRM_Core_Action::UPDATE) { $financialAccountType = CRM_Financial_BAO_FinancialAccount::getfinancialAccountRelations(); $financialAccountType = $financialAccountType[$this->_defaultValues['account_relationship']]; $result = CRM_Contribute_PseudoConstant::financialAccount(NULL, $financialAccountType); $financialAccountSelect = array('' => ts('- select -')) + $result; } $this->add('select', 'financial_account_id', ts('Financial Account'), $financialAccountSelect, TRUE); $this->addButtons(array(array('type' => 'next', 'name' => ts('Save'), 'isDefault' => TRUE), array('type' => 'next', 'name' => ts('Save and New'), 'subName' => 'new'), array('type' => 'cancel', 'name' => ts('Cancel')))); $this->addFormRule(array('CRM_Financial_Form_FinancialTypeAccount', 'formRule'), $this); }