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