array_search('Cost of Sales Account is', $accountRelationship) => array_search('Cost of Sales', $financialAccountTypeID),
array_search('Income Account is', $accountRelationship) => array_search('Revenue', $financialAccountTypeID),
);
- $params = array(
- 'name' => $financialType->name,
- 'contact_id' => CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Domain', CRM_Core_Config::domainID(), 'contact_id'),
- 'financial_account_type_id' => array_search('Revenue', $financialAccountTypeID),
- 'description' => $financialType->description,
- 'account_type_code' => 'INC',
- 'is_active' => 1,
+
+ $dao = CRM_Core_DAO::executeQuery('SELECT id, financial_account_type_id FROM civicrm_financial_account WHERE name LIKE %1',
+ array(1 => array($financialType->name, 'String'))
);
- $financialAccount = CRM_Financial_BAO_FinancialAccount::add($params, CRM_Core_DAO::$_nullArray);
+ $dao->fetch();
+ $existingFinancialAccount = array();
+ if (!$dao->N) {
+ $params = array(
+ 'name' => $financialType->name,
+ 'contact_id' => CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Domain', CRM_Core_Config::domainID(), 'contact_id'),
+ 'financial_account_type_id' => array_search('Revenue', $financialAccountTypeID),
+ 'description' => $financialType->description,
+ 'account_type_code' => 'INC',
+ 'is_active' => 1,
+ );
+ $financialAccount = CRM_Financial_BAO_FinancialAccount::add($params, CRM_Core_DAO::$_nullArray);
+ }
+ else {
+ $existingFinancialAccount[$dao->financial_account_type_id] = $dao->id;
+ }
$params = array (
'entity_table' => 'civicrm_financial_type',
'entity_id' => $financialType->id,
);
foreach ($relationships as $key => $value) {
- if ($accountRelationship[$key] == 'Accounts Receivable Account is') {
- $params['financial_account_id'] = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialAccount', 'Accounts Receivable', 'id', 'name');
- if (!empty($params['financial_account_id'])) {
- $titles[] = 'Accounts Receivable';
- }
- else {
- $query = "SELECT financial_account_id, name FROM civicrm_entity_financial_account
+ if (!array_key_exists($value, $existingFinancialAccount)) {
+ if ($accountRelationship[$key] == 'Accounts Receivable Account is') {
+ $params['financial_account_id'] = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialAccount', 'Accounts Receivable', 'id', 'name');
+ if (!empty($params['financial_account_id'])) {
+ $titles[] = 'Accounts Receivable';
+ }
+ else {
+ $query = "SELECT financial_account_id, name FROM civicrm_entity_financial_account
LEFT JOIN civicrm_financial_account ON civicrm_financial_account.id = civicrm_entity_financial_account.financial_account_id
WHERE account_relationship = {$key} AND entity_table = 'civicrm_financial_type' LIMIT 1";
+ $dao = CRM_Core_DAO::executeQuery($query);
+ $dao->fetch();
+ $params['financial_account_id'] = $dao->financial_account_id;
+ $titles[] = $dao->name;
+ }
+ }
+ elseif ($accountRelationship[$key] == 'Income Account is' && empty($existingFinancialAccount)) {
+ $params['financial_account_id'] = $financialAccount->id;
+ }
+ else {
+ $query = "SELECT id, name FROM civicrm_financial_account WHERE is_default = 1 AND financial_account_type_id = {$value}";
$dao = CRM_Core_DAO::executeQuery($query);
$dao->fetch();
- $params['financial_account_id'] = $dao->financial_account_id;
+ $params['financial_account_id'] = $dao->id;
$titles[] = $dao->name;
}
}
- elseif ($accountRelationship[$key] == 'Income Account is') {
- $params['financial_account_id'] = $financialAccount->id;
- }
else {
- $query = "SELECT id, name FROM civicrm_financial_account WHERE is_default = 1 AND financial_account_type_id = {$value}";
- $dao = CRM_Core_DAO::executeQuery($query);
- $dao->fetch();
- $params['financial_account_id'] = $dao->id;
- $titles[] = $dao->name;
+ $params['financial_account_id'] = $existingFinancialAccount[$value];
+ $titles[] = $financialType->name;
}
$params['account_relationship'] = $key;
self::add($params);
}
+ if (!empty($existingFinancialAccount)) {
+ $titles = array();
+ }
return $titles;
}
}
if ($this->_action == CRM_Core_Action::UPDATE && CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialType', $this->_id, 'is_reserved','vid')) {
$this->freeze(array('is_active'));
}
-
- //$this->addFormRule( array( 'CRM_Financial_Form_FinancialType', 'formRule'), $this );
+
+ $this->addRule('name', ts('A financial type with this name already exists. Please select another name.'),'objectExists',
+ array('CRM_Financial_DAO_FinancialType', $this->_id)
+ );
}
/**
$statusArray = array(
1 => $financialType->name,
2 => $financialType->name,
- 3 => $financialType->titles[0],
- 4 => $financialType->titles[1],
- 5 => $financialType->titles[2],
+ 3 => CRM_Utils_Array::value(0, $financialType->titles),
+ 4 => CRM_Utils_Array::value(1, $financialType->titles),
+ 5 => CRM_Utils_Array::value(2, $financialType->titles),
);
- CRM_Core_Session::setStatus(ts('Your Financial \'%1\' Type has been created, along with a corresponding income account \'%2\'. That income account, along with standard financial accounts \'%3\', \'%4\' and \'%5\' have been linked to the financial type. You may edit or replace those relationships here.', $statusArray));
+ if (empty($financialType->titles)) {
+ $text = 'Your Financial \'%1\' Type has been created and assigned to an existing financial account with the same title. You should review the assigned account and determine whether additional account relationships are needed.';
+ }
+ else {
+ $text = 'Your Financial \'%1\' Type has been created, along with a corresponding income account \'%2\'. That income account, along with standard financial accounts \'%3\', \'%4\' and \'%5\' have been linked to the financial type. You may edit or replace those relationships here.';
+ }
+ CRM_Core_Session::setStatus(ts($text, $statusArray));
}
$session = CRM_Core_Session::singleton();