INFRA-132 - Fix spacing of @return tag in comments
[civicrm-core.git] / CRM / Financial / BAO / FinancialType.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.6 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2014 |
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
32 * @copyright CiviCRM LLC (c) 2004-2014
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 public function __construct() {
48 parent::__construct();
49 }
50
51 /**
52 * Fetch object based on array of properties
53 *
54 * @param array $params
55 * (reference ) an assoc array of name/value pairs.
56 * @param array $defaults
57 * (reference ) an assoc array to hold the flattened values.
58 *
59 * @return CRM_Contribute_BAO_ContributionType object
60 * @static
61 */
62 public static function retrieve(&$params, &$defaults) {
63 $financialType = new CRM_Financial_DAO_FinancialType();
64 $financialType->copyValues($params);
65 if ($financialType->find(TRUE)) {
66 CRM_Core_DAO::storeValues($financialType, $defaults);
67 return $financialType;
68 }
69 return NULL;
70 }
71
72 /**
73 * Update the is_active flag in the db
74 *
75 * @param int $id
76 * Id of the database record.
77 * @param bool $is_active
78 * Value we want to set the is_active field.
79 *
80 * @return Object
81 * DAO object on sucess, null otherwise
82 * @static
83 */
84 public 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 * Add the financial types
90 *
91 * @param array $params
92 * Reference array contains the values submitted by the form.
93 * @param array $ids
94 * Reference array contains the id.
95 *
96 * @static
97 * @return object
98 */
99 public static function add(&$params, &$ids = array()) {
100 if (empty($params['id'])) {
101 $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE);
102 $params['is_deductible'] = CRM_Utils_Array::value('is_deductible', $params, FALSE);
103 $params['is_reserved'] = CRM_Utils_Array::value('is_reserved', $params, FALSE);
104 }
105
106 // action is taken depending upon the mode
107 $financialType = new CRM_Financial_DAO_FinancialType();
108 $financialType->copyValues($params);
109 if (!empty($ids['financialType'])) {
110 $financialType->id = CRM_Utils_Array::value('financialType', $ids);
111 }
112 $financialType->save();
113 // CRM-12470
114 if (empty($ids['financialType']) && empty($params['id'])) {
115 $titles = CRM_Financial_BAO_FinancialTypeAccount::createDefaultFinancialAccounts($financialType);
116 $financialType->titles = $titles;
117 }
118 return $financialType;
119 }
120
121 /**
122 * Delete financial Types
123 *
124 * @param int $financialTypeId
125 *
126 * @return array|bool
127 * @static
128 */
129 public static function del($financialTypeId) {
130 $financialType = new CRM_Financial_DAO_FinancialType();
131 $financialType->id = $financialTypeId;
132 $financialType->find(TRUE);
133 // tables to ingore checks for financial_type_id
134 $ignoreTables = array('CRM_Financial_DAO_EntityFinancialAccount');
135
136 //TODO: if (!$financialType->find(true)) {
137
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) {
143 $className = get_class($occurence);
144 if (!in_array($className, $ignoreTables)) {
145 $tables[] = $className;
146 }
147 }
148 if (!empty($tables)) {
149 $message = ts('The following tables have an entry for this financial type: %1', array('%1' => implode(', ', $tables)));
150
151 $errors = array();
152 $errors['is_error'] = 1;
153 $errors['error_message'] = $message;
154 return $errors;
155 }
156 }
157
158 //delete from financial Type table
159 $financialType->delete();
160
161 $entityFinancialType = new CRM_Financial_DAO_EntityFinancialAccount();
162 $entityFinancialType->entity_id = $financialTypeId;
163 $entityFinancialType->entity_table = 'civicrm_financial_type';
164 $entityFinancialType->delete();
165 return FALSE;
166 }
167
168 /**
169 * fetch financial type having relationship as Income Account is
170 *
171 *
172 * @return array
173 * all financial type with income account is relationship
174 * @static
175 */
176 public static function getIncomeFinancialType() {
177 // Financial Type
178 $financialType = CRM_Contribute_PseudoConstant::financialType();
179 $revenueFinancialType = array();
180 $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Income Account is' "));
181 CRM_Core_PseudoConstant::populate(
182 $revenueFinancialType,
183 'CRM_Financial_DAO_EntityFinancialAccount',
184 $all = TRUE,
185 $retrieve = 'entity_id',
186 $filter = NULL,
187 "account_relationship = $relationTypeId AND entity_table = 'civicrm_financial_type' "
188 );
189
190 foreach ($financialType as $key => $financialTypeName) {
191 if (!in_array($key, $revenueFinancialType)) {
192 unset($financialType[$key]);
193 }
194 }
195 return $financialType;
196 }
197 }