copyValues($params); if ($financialType->find(TRUE)) { CRM_Core_DAO::storeValues($financialType, $defaults); return $financialType; } return NULL; } /** * Update the is_active flag in the db. * * @param int $id * Id of the database record. * @param bool $is_active * Value we want to set the is_active field. * * @return Object * DAO object on success, null otherwise */ public static function setIsActive($id, $is_active) { return CRM_Core_DAO::setFieldValue('CRM_Financial_DAO_FinancialType', $id, 'is_active', $is_active); } /** * Add the financial types. * * @param array $params * Reference array contains the values submitted by the form. * @param array $ids * Reference array contains the id. * * @return object */ public static function add(&$params, &$ids = array()) { 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_reserved'] = CRM_Utils_Array::value('is_reserved', $params, FALSE); } // action is taken depending upon the mode $financialType = new CRM_Financial_DAO_FinancialType(); $financialType->copyValues($params); if (!empty($ids['financialType'])) { $financialType->id = CRM_Utils_Array::value('financialType', $ids); } $financialType->save(); // CRM-12470 if (empty($ids['financialType']) && empty($params['id'])) { $titles = CRM_Financial_BAO_FinancialTypeAccount::createDefaultFinancialAccounts($financialType); $financialType->titles = $titles; } return $financialType; } /** * Delete financial Types. * * @param int $financialTypeId * * @return array|bool */ public static function del($financialTypeId) { $financialType = new CRM_Financial_DAO_FinancialType(); $financialType->id = $financialTypeId; $financialType->find(TRUE); // tables to ingore checks for financial_type_id $ignoreTables = array('CRM_Financial_DAO_EntityFinancialAccount'); //TODO: if (!$financialType->find(true)) { // ensure that we have no objects that have an FK to this financial type id TODO: that cannot be null $occurrences = $financialType->findReferences(); if ($occurrences) { $tables = array(); foreach ($occurrences as $occurrence) { $className = get_class($occurrence); if (!in_array($className, $tables) && !in_array($className, $ignoreTables)) { $tables[] = $className; } } if (!empty($tables)) { $message = ts('The following tables have an entry for this financial type: %1', array('%1' => implode(', ', $tables))); $errors = array(); $errors['is_error'] = 1; $errors['error_message'] = $message; return $errors; } } //delete from financial Type table $financialType->delete(); $entityFinancialType = new CRM_Financial_DAO_EntityFinancialAccount(); $entityFinancialType->entity_id = $financialTypeId; $entityFinancialType->entity_table = 'civicrm_financial_type'; $entityFinancialType->delete(); return FALSE; } /** * fetch financial type having relationship as Income Account is. * * * @return array * all financial type with income account is relationship */ public static function getIncomeFinancialType() { // Financial Type $financialType = CRM_Contribute_PseudoConstant::financialType(); $revenueFinancialType = array(); $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Income Account is' ")); CRM_Core_PseudoConstant::populate( $revenueFinancialType, 'CRM_Financial_DAO_EntityFinancialAccount', $all = TRUE, $retrieve = 'entity_id', $filter = NULL, "account_relationship = $relationTypeId AND entity_table = 'civicrm_financial_type' " ); foreach ($financialType as $key => $financialTypeName) { if (!in_array($key, $revenueFinancialType)) { unset($financialType[$key]); } } return $financialType; } }