4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.6 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2014 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
32 * @copyright CiviCRM LLC (c) 2004-2014
36 class CRM_Financial_BAO_FinancialType
extends CRM_Financial_DAO_FinancialType
{
39 * Static holder for the default LT
41 static $_defaultContributionType = NULL;
46 public function __construct() {
47 parent
::__construct();
51 * Fetch object based on array of properties
53 * @param array $params
54 * (reference ) an assoc array of name/value pairs.
55 * @param array $defaults
56 * (reference ) an assoc array to hold the flattened values.
58 * @return CRM_Contribute_BAO_ContributionType
61 public static function retrieve(&$params, &$defaults) {
62 $financialType = new CRM_Financial_DAO_FinancialType();
63 $financialType->copyValues($params);
64 if ($financialType->find(TRUE)) {
65 CRM_Core_DAO
::storeValues($financialType, $defaults);
66 return $financialType;
72 * Update the is_active flag in the db
75 * Id of the database record.
76 * @param bool $is_active
77 * Value we want to set the is_active field.
80 * DAO object on sucess, null otherwise
83 public static function setIsActive($id, $is_active) {
84 return CRM_Core_DAO
::setFieldValue('CRM_Financial_DAO_FinancialType', $id, 'is_active', $is_active);
88 * Add the financial types
90 * @param array $params
91 * Reference array contains the values submitted by the form.
93 * Reference array contains the id.
98 public static function add(&$params, &$ids = array()) {
99 if (empty($params['id'])) {
100 $params['is_active'] = CRM_Utils_Array
::value('is_active', $params, FALSE);
101 $params['is_deductible'] = CRM_Utils_Array
::value('is_deductible', $params, FALSE);
102 $params['is_reserved'] = CRM_Utils_Array
::value('is_reserved', $params, FALSE);
105 // action is taken depending upon the mode
106 $financialType = new CRM_Financial_DAO_FinancialType();
107 $financialType->copyValues($params);
108 if (!empty($ids['financialType'])) {
109 $financialType->id
= CRM_Utils_Array
::value('financialType', $ids);
111 $financialType->save();
113 if (empty($ids['financialType']) && empty($params['id'])) {
114 $titles = CRM_Financial_BAO_FinancialTypeAccount
::createDefaultFinancialAccounts($financialType);
115 $financialType->titles
= $titles;
117 return $financialType;
121 * Delete financial Types
123 * @param int $financialTypeId
128 public static function del($financialTypeId) {
129 $financialType = new CRM_Financial_DAO_FinancialType();
130 $financialType->id
= $financialTypeId;
131 $financialType->find(TRUE);
132 // tables to ingore checks for financial_type_id
133 $ignoreTables = array('CRM_Financial_DAO_EntityFinancialAccount');
135 //TODO: if (!$financialType->find(true)) {
137 // ensure that we have no objects that have an FK to this financial type id TODO: that cannot be null
138 $occurrences = $financialType->findReferences();
141 foreach ($occurrences as $occurence) {
142 $className = get_class($occurence);
143 if (!in_array($className, $ignoreTables)) {
144 $tables[] = $className;
147 if (!empty($tables)) {
148 $message = ts('The following tables have an entry for this financial type: %1', array('%1' => implode(', ', $tables)));
151 $errors['is_error'] = 1;
152 $errors['error_message'] = $message;
157 //delete from financial Type table
158 $financialType->delete();
160 $entityFinancialType = new CRM_Financial_DAO_EntityFinancialAccount();
161 $entityFinancialType->entity_id
= $financialTypeId;
162 $entityFinancialType->entity_table
= 'civicrm_financial_type';
163 $entityFinancialType->delete();
168 * fetch financial type having relationship as Income Account is
172 * all financial type with income account is relationship
175 public static function getIncomeFinancialType() {
177 $financialType = CRM_Contribute_PseudoConstant
::financialType();
178 $revenueFinancialType = array();
179 $relationTypeId = key(CRM_Core_PseudoConstant
::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Income Account is' "));
180 CRM_Core_PseudoConstant
::populate(
181 $revenueFinancialType,
182 'CRM_Financial_DAO_EntityFinancialAccount',
184 $retrieve = 'entity_id',
186 "account_relationship = $relationTypeId AND entity_table = 'civicrm_financial_type' "
189 foreach ($financialType as $key => $financialTypeName) {
190 if (!in_array($key, $revenueFinancialType)) {
191 unset($financialType[$key]);
194 return $financialType;