--ICM-15, added condition to check if FT-ACL is enabled or not
[civicrm-core.git] / CRM / Financial / BAO / FinancialType.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
006389de 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
6a488035
TO
32 * $Id$
33 *
34 */
6a488035
TO
35class CRM_Financial_BAO_FinancialType extends CRM_Financial_DAO_FinancialType {
36
37 /**
fe482240 38 * Static holder for the default LT.
6a488035 39 */
045f52a3 40 static $_defaultContributionType = NULL;
7f2b2c0d
JM
41
42 /**
58c3bf5f 43 * Static cache holder of available financial types for this session
7f2b2c0d 44 */
58c3bf5f 45 static $_availableFinancialTypes = array();
5c4bbd48
PN
46
47 /**
48 * Static cache holder of status of ACL-FT enabled/disabled for this session
49 */
50 static $_statusACLFt = array();
6a488035
TO
51
52 /**
fe482240 53 * Class constructor.
6a488035 54 */
045f52a3 55 public function __construct() {
481a74f4 56 parent::__construct();
6a488035
TO
57 }
58
59 /**
fe482240 60 * Fetch object based on array of properties.
6a488035 61 *
ed5dd492
TO
62 * @param array $params
63 * (reference ) an assoc array of name/value pairs.
64 * @param array $defaults
65 * (reference ) an assoc array to hold the flattened values.
6a488035 66 *
16b10e64 67 * @return CRM_Contribute_BAO_ContributionType
6a488035 68 */
045f52a3 69 public static function retrieve(&$params, &$defaults) {
481a74f4
TO
70 $financialType = new CRM_Financial_DAO_FinancialType();
71 $financialType->copyValues($params);
045f52a3 72 if ($financialType->find(TRUE)) {
481a74f4 73 CRM_Core_DAO::storeValues($financialType, $defaults);
6a488035
TO
74 return $financialType;
75 }
045f52a3 76 return NULL;
6a488035
TO
77 }
78
79 /**
fe482240 80 * Update the is_active flag in the db.
6a488035 81 *
ed5dd492
TO
82 * @param int $id
83 * Id of the database record.
84 * @param bool $is_active
85 * Value we want to set the is_active field.
6a488035 86 *
a6c01b45 87 * @return Object
b44e3f84 88 * DAO object on success, null otherwise
6a488035 89 */
045f52a3 90 public static function setIsActive($id, $is_active) {
481a74f4 91 return CRM_Core_DAO::setFieldValue('CRM_Financial_DAO_FinancialType', $id, 'is_active', $is_active);
6a488035
TO
92 }
93
94 /**
fe482240 95 * Add the financial types.
6a488035 96 *
ed5dd492
TO
97 * @param array $params
98 * Reference array contains the values submitted by the form.
99 * @param array $ids
100 * Reference array contains the id.
6a488035 101 *
6a488035
TO
102 * @return object
103 */
00be9182 104 public static function add(&$params, &$ids = array()) {
22e263ad 105 if (empty($params['id'])) {
045f52a3
TO
106 $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE);
107 $params['is_deductible'] = CRM_Utils_Array::value('is_deductible', $params, FALSE);
108 $params['is_reserved'] = CRM_Utils_Array::value('is_reserved', $params, FALSE);
bc2bc079 109 }
6a488035
TO
110
111 // action is taken depending upon the mode
ddaa8ef1
PN
112 $financialType = new CRM_Financial_DAO_FinancialType();
113 $financialType->copyValues($params);
a7488080 114 if (!empty($ids['financialType'])) {
6a488035
TO
115 $financialType->id = CRM_Utils_Array::value('financialType', $ids);
116 }
ddaa8ef1
PN
117 $financialType->save();
118 // CRM-12470
bc2bc079 119 if (empty($ids['financialType']) && empty($params['id'])) {
ddaa8ef1
PN
120 $titles = CRM_Financial_BAO_FinancialTypeAccount::createDefaultFinancialAccounts($financialType);
121 $financialType->titles = $titles;
122 }
6a488035
TO
123 return $financialType;
124 }
125
126 /**
fe482240 127 * Delete financial Types.
6a488035 128 *
c490a46a 129 * @param int $financialTypeId
77b97be7
EM
130 *
131 * @return array|bool
6a488035 132 */
00be9182 133 public static function del($financialTypeId) {
481a74f4 134 $financialType = new CRM_Financial_DAO_FinancialType();
71e5aa5c 135 $financialType->id = $financialTypeId;
045f52a3 136 $financialType->find(TRUE);
cf1d2db7
PN
137 // tables to ingore checks for financial_type_id
138 $ignoreTables = array('CRM_Financial_DAO_EntityFinancialAccount');
8ef12e64 139
71e5aa5c 140 //TODO: if (!$financialType->find(true)) {
6a488035 141
71e5aa5c
ARW
142 // ensure that we have no objects that have an FK to this financial type id TODO: that cannot be null
143 $occurrences = $financialType->findReferences();
144 if ($occurrences) {
145 $tables = array();
b44e3f84
DS
146 foreach ($occurrences as $occurrence) {
147 $className = get_class($occurrence);
3611044f 148 if (!in_array($className, $tables) && !in_array($className, $ignoreTables)) {
045f52a3 149 $tables[] = $className;
cf1d2db7 150 }
71e5aa5c 151 }
cf1d2db7
PN
152 if (!empty($tables)) {
153 $message = ts('The following tables have an entry for this financial type: %1', array('%1' => implode(', ', $tables)));
6a488035 154
cf1d2db7
PN
155 $errors = array();
156 $errors['is_error'] = 1;
157 $errors['error_message'] = $message;
158 return $errors;
159 }
6a488035
TO
160 }
161
162 //delete from financial Type table
6a488035
TO
163 $financialType->delete();
164
481a74f4 165 $entityFinancialType = new CRM_Financial_DAO_EntityFinancialAccount();
6a488035
TO
166 $entityFinancialType->entity_id = $financialTypeId;
167 $entityFinancialType->entity_table = 'civicrm_financial_type';
71e5aa5c 168 $entityFinancialType->delete();
6a488035
TO
169 return FALSE;
170 }
8ef12e64 171
6a488035 172 /**
fe482240 173 * fetch financial type having relationship as Income Account is.
6a488035
TO
174 *
175 *
a6c01b45
CW
176 * @return array
177 * all financial type with income account is relationship
6a488035 178 */
00be9182 179 public static function getIncomeFinancialType() {
6a488035
TO
180 // Financial Type
181 $financialType = CRM_Contribute_PseudoConstant::financialType();
182 $revenueFinancialType = array();
f743a6eb 183 $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Income Account is' "));
8ef12e64 184 CRM_Core_PseudoConstant::populate(
6a488035
TO
185 $revenueFinancialType,
186 'CRM_Financial_DAO_EntityFinancialAccount',
045f52a3 187 $all = TRUE,
8ef12e64 188 $retrieve = 'entity_id',
045f52a3 189 $filter = NULL,
8ef12e64 190 "account_relationship = $relationTypeId AND entity_table = 'civicrm_financial_type' "
6a488035 191 );
8ef12e64 192
6a488035 193 foreach ($financialType as $key => $financialTypeName) {
8173dae3 194 if (!in_array($key, $revenueFinancialType)
4323dc6c
PN
195 || (CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus()
196 && !CRM_Core_Permission::check('add contributions of type ' . $financialTypeName))
197 ) {
6a488035 198 unset($financialType[$key]);
a8b59c2c 199 }
6a488035
TO
200 }
201 return $financialType;
202 }
96025800 203
4aed5ac0
E
204 /**
205 * adding permissions for financial types
206 *
207 *
8173dae3 208 * @param array $permissions
7cebf167 209 * an array of permissions
4aed5ac0
E
210 */
211 public static function permissionedFinancialTypes(&$permissions) {
2435064f 212 if (!self::isACLFinancialTypeStatus()) {
92420262 213 return FALSE;
2435064f 214 }
4aed5ac0
E
215 $financialTypes = CRM_Contribute_PseudoConstant::financialType();
216 $prefix = ts('CiviCRM') . ': ';
217 foreach ($financialTypes as $id => $type) {
92420262
PN
218 $permissions['add contributions of type ' . $type] = array(
219 $prefix . ts('add contributions of type ') . $type,
3bf61d05 220 ts('Add contributions of type ') . $type,
92420262
PN
221 );
222 $permissions['view contributions of type ' . $type] = array(
223 $prefix . ts('view contributions of type ') . $type,
224 ts('View contributions of type ') . $type,
225 );
226 $permissions['edit contributions of type ' . $type] = array(
227 $prefix . ts('edit contributions of type ') . $type,
228 ts('Edit contributions of type ') . $type,
229 );
230 $permissions['delete contributions of type ' . $type] = array(
231 $prefix . ts('delete contributions of type ') . $type,
232 ts('Delete contributions of type ') . $type,
233 );
4aed5ac0 234 }
92420262
PN
235 $permissions['administer CiviCRM Financial Types'] = array(
236 $prefix . ts('administer CiviCRM Financial Types'),
237 ts('Administer access to Financial Types'),
238 );
4aed5ac0 239 }
bb08e888 240
7f2b2c0d 241 public static function getAvailableFinancialTypes(&$financialTypes = NULL, $action = 'view', $resetCache = FALSE) {
84e489b7
E
242 if (empty($financialTypes)) {
243 $financialTypes = CRM_Contribute_PseudoConstant::financialType();
244 }
2435064f
PN
245 if (!self::isACLFinancialTypeStatus()) {
246 return $financialTypes;
58c3bf5f
PN
247 }
248 // check cached value
249 if (CRM_Utils_Array::value($action, self::$_availableFinancialTypes) && !$resetCache) {
250 $financialTypes = self::$_availableFinancialTypes[$action];
251 return self::$_availableFinancialTypes[$action];
2435064f 252 }
bb08e888 253 foreach ($financialTypes as $finTypeId => $type) {
a4ef4eac 254 if (!CRM_Core_Permission::check($action . ' contributions of type ' . $type)) {
bb08e888
E
255 unset($financialTypes[$finTypeId]);
256 }
257 }
58c3bf5f 258 self::$_availableFinancialTypes[$action] = $financialTypes;
803e39be 259 return $financialTypes;
bb08e888 260 }
7f2b2c0d 261
27e7373d
E
262 public static function getAvailableMembershipTypes(&$membershipTypes = NULL, $action = 'view') {
263 if (empty($membershipTypes)) {
264 $membershipTypes = CRM_Member_PseudoConstant::membershipType();
265 }
2435064f
PN
266 if (!self::isACLFinancialTypeStatus()) {
267 return $membershipTypes;
268 }
27e7373d
E
269 foreach ($membershipTypes as $memTypeId => $type) {
270 $finTypeId = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $memTypeId, 'financial_type_id');
271 $finType = CRM_Contribute_PseudoConstant::financialType($finTypeId);
272 if (!CRM_Core_Permission::check($action . ' contributions of type ' . $finType)) {
273 unset($membershipTypes[$memTypeId]);
274 }
275 }
276 return $membershipTypes;
277 }
81318f84 278
1f76c4cd 279 public static function buildPermissionedClause(&$whereClauses, $component = NULL, $alias = NULL) {
2435064f
PN
280 if (!self::isACLFinancialTypeStatus()) {
281 return FALSE;
282 }
be274d8a 283 if (is_array($whereClauses)) {
d1bb399a 284 self::getAvailableFinancialTypes($types);
f71679cf 285 if (empty($types)) {
8173dae3 286 $whereClauses[] = ' ' . $alias . '.financial_type_id IN (0)';
f71679cf
E
287 }
288 else {
a8b59c2c 289 $whereClauses[] = ' ' . $alias . '.financial_type_id IN (' . implode(',', array_keys($types)) . ')';
f71679cf 290 }
be274d8a
E
291 }
292 else {
293 if ($component == 'contribution') {
d1bb399a 294 self::getAvailableFinancialTypes($types);
be274d8a
E
295 $column = "financial_type_id";
296 }
297 if ($component == 'membership') {
be274d8a
E
298 self::getAvailableMembershipTypes($types, 'view');
299 $column = "membership_type_id";
25cc088b
E
300 }
301 if (empty($types)) {
302 $whereClauses .= " AND civicrm_{$component}.{$column} IN (0)";
303 return;
be274d8a 304 }
a8b59c2c 305 $whereClauses .= " AND civicrm_{$component}.{$column} IN (" . implode(',', array_keys($types)) . ")";
81318f84 306 }
81318f84 307 }
59ccc8cd
E
308
309 public static function checkPermissionedLineItems($id, $op, $force = TRUE) {
895d596d
E
310 if (!self::isACLFinancialTypeStatus()) {
311 return TRUE;
312 }
59ccc8cd 313 $lineItems = CRM_Price_BAO_LineItem::getLineItemsByContributionID($id);
b0c2be34 314 $flag = FALSE;
8173dae3 315 foreach ($lineItems as $items) {
59ccc8cd
E
316 if (!CRM_Core_Permission::check($op . ' contributions of type ' . CRM_Contribute_PseudoConstant::financialType($items['financial_type_id']))) {
317 if ($force) {
318 CRM_Core_Error::fatal(ts('You do not have permission to access this page.'));
319 break;
320 }
b0c2be34
E
321 $flag = FALSE;
322 break;
59ccc8cd
E
323 }
324 else {
b0c2be34 325 $flag = TRUE;
59ccc8cd
E
326 }
327 }
b0c2be34 328 return $flag;
59ccc8cd 329 }
2435064f
PN
330 /**
331 * Check if FT-ACL is turned on or off
332 *
8173dae3 333 * @return bool
2435064f
PN
334 */
335 public static function isACLFinancialTypeStatus() {
5c4bbd48
PN
336 if (array_key_exists('acl_financial_type', self::$_statusACLFt)) {
337 return self::$_statusACLFt['acl_financial_type'];
338 }
2435064f
PN
339 $contributeSettings = CRM_Core_BAO_Setting::getItem(
340 CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME, 'contribution_invoice_settings'
341 );
5c4bbd48 342 self::$_statusACLFt['acl_financial_type'] = FALSE;
2435064f 343 if (CRM_Utils_Array::value('acl_financial_type', $contributeSettings)) {
5c4bbd48 344 self::$_statusACLFt['acl_financial_type'] = TRUE;
2435064f 345 }
5c4bbd48 346 return self::$_statusACLFt['acl_financial_type'];
2435064f 347 }
a8b59c2c 348
6a488035 349}