Merge pull request #5680 from elinw/restauthenticationjoomla
[civicrm-core.git] / CRM / Financial / BAO / FinancialType.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
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;
6a488035
TO
41
42 /**
fe482240 43 * Class constructor.
6a488035 44 */
045f52a3 45 public function __construct() {
481a74f4 46 parent::__construct();
6a488035
TO
47 }
48
49 /**
fe482240 50 * Fetch object based on array of properties.
6a488035 51 *
ed5dd492
TO
52 * @param array $params
53 * (reference ) an assoc array of name/value pairs.
54 * @param array $defaults
55 * (reference ) an assoc array to hold the flattened values.
6a488035 56 *
16b10e64 57 * @return CRM_Contribute_BAO_ContributionType
6a488035 58 */
045f52a3 59 public static function retrieve(&$params, &$defaults) {
481a74f4
TO
60 $financialType = new CRM_Financial_DAO_FinancialType();
61 $financialType->copyValues($params);
045f52a3 62 if ($financialType->find(TRUE)) {
481a74f4 63 CRM_Core_DAO::storeValues($financialType, $defaults);
6a488035
TO
64 return $financialType;
65 }
045f52a3 66 return NULL;
6a488035
TO
67 }
68
69 /**
fe482240 70 * Update the is_active flag in the db.
6a488035 71 *
ed5dd492
TO
72 * @param int $id
73 * Id of the database record.
74 * @param bool $is_active
75 * Value we want to set the is_active field.
6a488035 76 *
a6c01b45 77 * @return Object
b44e3f84 78 * DAO object on success, null otherwise
6a488035 79 */
045f52a3 80 public static function setIsActive($id, $is_active) {
481a74f4 81 return CRM_Core_DAO::setFieldValue('CRM_Financial_DAO_FinancialType', $id, 'is_active', $is_active);
6a488035
TO
82 }
83
84 /**
fe482240 85 * Add the financial types.
6a488035 86 *
ed5dd492
TO
87 * @param array $params
88 * Reference array contains the values submitted by the form.
89 * @param array $ids
90 * Reference array contains the id.
6a488035 91 *
6a488035
TO
92 * @return object
93 */
00be9182 94 public static function add(&$params, &$ids = array()) {
22e263ad 95 if (empty($params['id'])) {
045f52a3
TO
96 $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE);
97 $params['is_deductible'] = CRM_Utils_Array::value('is_deductible', $params, FALSE);
98 $params['is_reserved'] = CRM_Utils_Array::value('is_reserved', $params, FALSE);
bc2bc079 99 }
6a488035
TO
100
101 // action is taken depending upon the mode
ddaa8ef1
PN
102 $financialType = new CRM_Financial_DAO_FinancialType();
103 $financialType->copyValues($params);
a7488080 104 if (!empty($ids['financialType'])) {
6a488035
TO
105 $financialType->id = CRM_Utils_Array::value('financialType', $ids);
106 }
ddaa8ef1
PN
107 $financialType->save();
108 // CRM-12470
bc2bc079 109 if (empty($ids['financialType']) && empty($params['id'])) {
ddaa8ef1
PN
110 $titles = CRM_Financial_BAO_FinancialTypeAccount::createDefaultFinancialAccounts($financialType);
111 $financialType->titles = $titles;
112 }
6a488035
TO
113 return $financialType;
114 }
115
116 /**
fe482240 117 * Delete financial Types.
6a488035 118 *
c490a46a 119 * @param int $financialTypeId
77b97be7
EM
120 *
121 * @return array|bool
6a488035 122 */
00be9182 123 public static function del($financialTypeId) {
481a74f4 124 $financialType = new CRM_Financial_DAO_FinancialType();
71e5aa5c 125 $financialType->id = $financialTypeId;
045f52a3 126 $financialType->find(TRUE);
cf1d2db7
PN
127 // tables to ingore checks for financial_type_id
128 $ignoreTables = array('CRM_Financial_DAO_EntityFinancialAccount');
8ef12e64 129
71e5aa5c 130 //TODO: if (!$financialType->find(true)) {
6a488035 131
71e5aa5c
ARW
132 // ensure that we have no objects that have an FK to this financial type id TODO: that cannot be null
133 $occurrences = $financialType->findReferences();
134 if ($occurrences) {
135 $tables = array();
b44e3f84
DS
136 foreach ($occurrences as $occurrence) {
137 $className = get_class($occurrence);
3611044f 138 if (!in_array($className, $tables) && !in_array($className, $ignoreTables)) {
045f52a3 139 $tables[] = $className;
cf1d2db7 140 }
71e5aa5c 141 }
cf1d2db7
PN
142 if (!empty($tables)) {
143 $message = ts('The following tables have an entry for this financial type: %1', array('%1' => implode(', ', $tables)));
6a488035 144
cf1d2db7
PN
145 $errors = array();
146 $errors['is_error'] = 1;
147 $errors['error_message'] = $message;
148 return $errors;
149 }
6a488035
TO
150 }
151
152 //delete from financial Type table
6a488035
TO
153 $financialType->delete();
154
481a74f4 155 $entityFinancialType = new CRM_Financial_DAO_EntityFinancialAccount();
6a488035
TO
156 $entityFinancialType->entity_id = $financialTypeId;
157 $entityFinancialType->entity_table = 'civicrm_financial_type';
71e5aa5c 158 $entityFinancialType->delete();
6a488035
TO
159 return FALSE;
160 }
8ef12e64 161
6a488035 162 /**
fe482240 163 * fetch financial type having relationship as Income Account is.
6a488035
TO
164 *
165 *
a6c01b45
CW
166 * @return array
167 * all financial type with income account is relationship
6a488035 168 */
00be9182 169 public static function getIncomeFinancialType() {
6a488035
TO
170 // Financial Type
171 $financialType = CRM_Contribute_PseudoConstant::financialType();
172 $revenueFinancialType = array();
f743a6eb 173 $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Income Account is' "));
8ef12e64 174 CRM_Core_PseudoConstant::populate(
6a488035
TO
175 $revenueFinancialType,
176 'CRM_Financial_DAO_EntityFinancialAccount',
045f52a3 177 $all = TRUE,
8ef12e64 178 $retrieve = 'entity_id',
045f52a3 179 $filter = NULL,
8ef12e64 180 "account_relationship = $relationTypeId AND entity_table = 'civicrm_financial_type' "
6a488035 181 );
8ef12e64 182
6a488035
TO
183 foreach ($financialType as $key => $financialTypeName) {
184 if (!in_array($key, $revenueFinancialType)) {
185 unset($financialType[$key]);
8ef12e64 186 }
6a488035
TO
187 }
188 return $financialType;
189 }
96025800 190
6a488035 191}