Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | ||
3 | /* | |
4 | +--------------------------------------------------------------------+ | |
06b69b18 | 5 | | CiviCRM version 4.5 | |
6a488035 | 6 | +--------------------------------------------------------------------+ |
06b69b18 | 7 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
8 | +--------------------------------------------------------------------+ |
9 | | This file is a part of CiviCRM. | | |
10 | | | | |
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. | | |
14 | | | | |
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. | | |
19 | | | | |
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 | +--------------------------------------------------------------------+ | |
27 | */ | |
28 | ||
29 | /** | |
30 | * | |
31 | * @package CRM | |
06b69b18 | 32 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
33 | * $Id$ |
34 | * | |
35 | */ | |
36 | ||
37 | class CRM_Financial_BAO_FinancialType extends CRM_Financial_DAO_FinancialType { | |
38 | ||
39 | /** | |
40 | * static holder for the default LT | |
41 | */ | |
42 | static $_defaultContributionType = null; | |
43 | ||
44 | /** | |
45 | * class constructor | |
46 | */ | |
47 | function __construct( ) { | |
48 | parent::__construct( ); | |
49 | } | |
50 | ||
51 | /** | |
52 | * Takes a bunch of params that are needed to match certain criteria and | |
53 | * retrieves the relevant objects. Typically the valid params are only | |
54 | * contact_id. We'll tweak this function to be more full featured over a period | |
55 | * of time. This is the inverse function of create. It also stores all the retrieved | |
56 | * values in the default array | |
57 | * | |
58 | * @param array $params (reference ) an assoc array of name/value pairs | |
59 | * @param array $defaults (reference ) an assoc array to hold the flattened values | |
60 | * | |
61 | * @return object CRM_Contribute_BAO_ContributionType object | |
62 | * @access public | |
63 | * @static | |
64 | */ | |
65 | static function retrieve( &$params, &$defaults ) { | |
66 | $financialType = new CRM_Financial_DAO_FinancialType( ); | |
67 | $financialType->copyValues( $params ); | |
68 | if ($financialType->find(true)) { | |
69 | CRM_Core_DAO::storeValues( $financialType, $defaults ); | |
70 | return $financialType; | |
71 | } | |
72 | return null; | |
73 | } | |
74 | ||
75 | /** | |
76 | * update the is_active flag in the db | |
77 | * | |
78 | * @param int $id id of the database record | |
79 | * @param boolean $is_active value we want to set the is_active field | |
80 | * | |
81 | * @return Object DAO object on sucess, null otherwise | |
82 | * @static | |
83 | */ | |
84 | static function setIsActive( $id, $is_active ) { | |
85 | return CRM_Core_DAO::setFieldValue( 'CRM_Financial_DAO_FinancialType', $id, 'is_active', $is_active ); | |
86 | } | |
87 | ||
88 | /** | |
89 | * function to add the financial types | |
90 | * | |
91 | * @param array $params reference array contains the values submitted by the form | |
92 | * @param array $ids reference array contains the id | |
93 | * | |
94 | * @access public | |
95 | * @static | |
96 | * @return object | |
97 | */ | |
e5439581 | 98 | static function add(&$params, &$ids = array()) { |
bc2bc079 | 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); | |
103 | } | |
6a488035 TO |
104 | |
105 | // action is taken depending upon the mode | |
ddaa8ef1 PN |
106 | $financialType = new CRM_Financial_DAO_FinancialType(); |
107 | $financialType->copyValues($params); | |
a7488080 | 108 | if (!empty($ids['financialType'])) { |
6a488035 TO |
109 | $financialType->id = CRM_Utils_Array::value('financialType', $ids); |
110 | } | |
ddaa8ef1 PN |
111 | $financialType->save(); |
112 | // CRM-12470 | |
bc2bc079 | 113 | if (empty($ids['financialType']) && empty($params['id'])) { |
ddaa8ef1 PN |
114 | $titles = CRM_Financial_BAO_FinancialTypeAccount::createDefaultFinancialAccounts($financialType); |
115 | $financialType->titles = $titles; | |
116 | } | |
6a488035 TO |
117 | return $financialType; |
118 | } | |
119 | ||
120 | /** | |
121 | * Function to delete financial Types | |
122 | * | |
77b97be7 EM |
123 | * @param $financialTypeId |
124 | * | |
125 | * @return array|bool | |
126 | * @internal param int $contributionTypeId | |
6a488035 TO |
127 | * @static |
128 | */ | |
129 | static function del($financialTypeId) { | |
71e5aa5c ARW |
130 | $financialType = new CRM_Financial_DAO_FinancialType( ); |
131 | $financialType->id = $financialTypeId; | |
132 | $financialType->find(true); | |
cf1d2db7 PN |
133 | // tables to ingore checks for financial_type_id |
134 | $ignoreTables = array('CRM_Financial_DAO_EntityFinancialAccount'); | |
8ef12e64 | 135 | |
71e5aa5c | 136 | //TODO: if (!$financialType->find(true)) { |
6a488035 | 137 | |
71e5aa5c ARW |
138 | // ensure that we have no objects that have an FK to this financial type id TODO: that cannot be null |
139 | $occurrences = $financialType->findReferences(); | |
140 | if ($occurrences) { | |
141 | $tables = array(); | |
142 | foreach ($occurrences as $occurence) { | |
cf1d2db7 PN |
143 | $className = get_class($occurence); |
144 | if (!in_array($className, $ignoreTables)) { | |
145 | $tables[] = $className; | |
146 | } | |
71e5aa5c | 147 | } |
cf1d2db7 PN |
148 | if (!empty($tables)) { |
149 | $message = ts('The following tables have an entry for this financial type: %1', array('%1' => implode(', ', $tables))); | |
6a488035 | 150 | |
cf1d2db7 PN |
151 | $errors = array(); |
152 | $errors['is_error'] = 1; | |
153 | $errors['error_message'] = $message; | |
154 | return $errors; | |
155 | } | |
6a488035 TO |
156 | } |
157 | ||
158 | //delete from financial Type table | |
6a488035 TO |
159 | $financialType->delete(); |
160 | ||
161 | $entityFinancialType = new CRM_Financial_DAO_EntityFinancialAccount( ); | |
162 | $entityFinancialType->entity_id = $financialTypeId; | |
163 | $entityFinancialType->entity_table = 'civicrm_financial_type'; | |
71e5aa5c | 164 | $entityFinancialType->delete(); |
6a488035 TO |
165 | return FALSE; |
166 | } | |
8ef12e64 | 167 | |
6a488035 TO |
168 | /** |
169 | * to fetch financial type having relationship as Income Account is | |
170 | * | |
171 | * | |
172 | * @return array all financial type with income account is relationship | |
173 | * @static | |
174 | */ | |
8ef12e64 | 175 | static function getIncomeFinancialType() { |
6a488035 TO |
176 | // Financial Type |
177 | $financialType = CRM_Contribute_PseudoConstant::financialType(); | |
178 | $revenueFinancialType = array(); | |
f743a6eb | 179 | $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Income Account is' ")); |
8ef12e64 | 180 | CRM_Core_PseudoConstant::populate( |
6a488035 TO |
181 | $revenueFinancialType, |
182 | 'CRM_Financial_DAO_EntityFinancialAccount', | |
8ef12e64 | 183 | $all = True, |
184 | $retrieve = 'entity_id', | |
185 | $filter = null, | |
186 | "account_relationship = $relationTypeId AND entity_table = 'civicrm_financial_type' " | |
6a488035 | 187 | ); |
8ef12e64 | 188 | |
6a488035 TO |
189 | foreach ($financialType as $key => $financialTypeName) { |
190 | if (!in_array($key, $revenueFinancialType)) { | |
191 | unset($financialType[$key]); | |
8ef12e64 | 192 | } |
6a488035 TO |
193 | } |
194 | return $financialType; | |
195 | } | |
196 | } | |
197 |