Mass cleanup of docblocks/code/comments
[civicrm-core.git] / CRM / Financial / BAO / FinancialTypeAccount.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2014
32 * $Id$
33 *
34 */
35
36 class CRM_Financial_BAO_FinancialTypeAccount extends CRM_Financial_DAO_EntityFinancialAccount {
37
38 /**
39 * class constructor
40 */
41 function __construct( ) {
42 parent::__construct( );
43 }
44
45 /**
46 * financial account
47 * @var array
48 * @static
49 */
50 private static $financialAccount;
51
52 /**
53 * Fetch object based on array of properties
54 *
55 * @param array $params (reference ) an assoc array of name/value pairs
56 * @param array $defaults (reference ) an assoc array to hold the flattened values
57 *
58 * @param array $allValues
59 *
60 * @return CRM_Contribute_BAO_ContributionType object
61 * @access public
62 * @static
63 */
64 static function retrieve(&$params, &$defaults, &$allValues = array()) {
65 $financialTypeAccount = new CRM_Financial_DAO_EntityFinancialAccount();
66 $financialTypeAccount->copyValues($params);
67 $financialTypeAccount->find();
68 while ($financialTypeAccount->fetch()) {
69 CRM_Core_DAO::storeValues($financialTypeAccount, $defaults);
70 $allValues[] = $defaults;
71 }
72 return $defaults;
73 }
74
75 /**
76 * add the financial types
77 *
78 * @param array $params reference array contains the values submitted by the form
79 * @param array $ids reference array contains the id
80 *
81 * @access public
82 * @static
83 * @return object
84 */
85 static function add(&$params, &$ids = NULL) {
86 // action is taken depending upon the mode
87 $financialTypeAccount = new CRM_Financial_DAO_EntityFinancialAccount();
88 if ($params['entity_table'] != 'civicrm_financial_type') {
89 $financialTypeAccount->entity_id = $params['entity_id'];
90 $financialTypeAccount->entity_table = $params['entity_table'];
91 $financialTypeAccount->find(TRUE);
92 }
93 else {
94 $financialTypeAccount->id = CRM_Utils_Array::value('entityFinancialAccount', $ids);
95 }
96 if (!empty($ids['entityFinancialAccount'])) {
97 $financialTypeAccount->id = $ids['entityFinancialAccount'];
98 }
99 $financialTypeAccount->copyValues($params);
100 $financialTypeAccount->save();
101 return $financialTypeAccount;
102 }
103
104 /**
105 * delete financial Types
106 *
107 * @param int $financialTypeAccountId
108 * @param int $accountId
109 *
110 * @static
111 */
112 static function del($financialTypeAccountId, $accountId = null) {
113 //checking if financial type is present
114 $check = false;
115 $relationValues = CRM_Core_PseudoConstant::get('CRM_Financial_DAO_EntityFinancialAccount', 'account_relationship');
116
117 $financialTypeId = CRM_Core_DAO::getFieldValue( 'CRM_Financial_DAO_EntityFinancialAccount', $financialTypeAccountId, 'entity_id' );
118 //check dependencies
119 // FIXME more table containing financial_type_id to come
120 $dependancy = array(
121 array('Contribute', 'Contribution'),
122 array('Contribute', 'ContributionPage'),
123 array('Member', 'MembershipType'),
124 array('Price', 'PriceFieldValue'),
125 array('Grant', 'Grant'),
126 array('Contribute', 'PremiumsProduct'),
127 array('Contribute', 'Product'),
128 array('Price', 'LineItem'),
129 );
130
131 foreach ($dependancy as $name) {
132 $daoString = 'CRM_' . $name[0] . '_DAO_' . $name[1];
133 $dao = new $daoString();
134 $dao->financial_type_id = $financialTypeId;
135 if ($dao->find(true)) {
136 $check = true;
137 break;
138 }
139 }
140
141 if ($check) {
142 if ($name[1] == 'PremiumsProduct' || $name[1] == 'Product') {
143 CRM_Core_Session::setStatus(ts('You cannot remove an account with a %1 relationship while the Financial Type is used for a Premium.', array(1 => $relationValues[$financialTypeAccountId])));
144 }
145 else {
146 $accountRelationShipId = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_EntityFinancialAccount', $financialTypeAccountId, 'account_relationship');
147 CRM_Core_Session::setStatus(ts('You cannot remove an account with a %1 relationship because it is being referenced by one or more of the following types of records: Contributions, Contribution Pages, or Membership Types. Consider disabling this type instead if you no longer want it used.', array(1 => $relationValues[$accountRelationShipId])), NUll, 'error');
148 }
149 return CRM_Utils_System::redirect( CRM_Utils_System::url( 'civicrm/admin/financial/financialType/accounts', "reset=1&action=browse&aid={$accountId}" ));
150 }
151
152 //delete from financial Type table
153 $financialType = new CRM_Financial_DAO_EntityFinancialAccount( );
154 $financialType->id = $financialTypeAccountId;
155 $financialType->find(TRUE);
156 $financialType->delete();
157 CRM_Core_Session::setStatus(ts('Unbalanced transactions may be created if you delete the account of type: %1.', array(1 => $relationValues[$financialType->account_relationship])));
158 }
159
160 /**
161 * get Financial Account Name
162 *
163 * @param int $entityId
164 *
165 * @param string $entityTable
166 *
167 * @param string $columnName Column to fetch
168 *
169 * @return null|string
170 * @static
171 */
172 static function getFinancialAccount($entityId, $entityTable, $columnName = 'name') {
173 $join = $columnName == 'name' ? 'LEFT JOIN civicrm_financial_account ON civicrm_entity_financial_account.financial_account_id = civicrm_financial_account.id' : NULL;
174 $query = "
175 SELECT {$columnName}
176 FROM civicrm_entity_financial_account
177 {$join}
178 WHERE entity_table = %1
179 AND entity_id = %2";
180
181 $params = array(
182 1 => array($entityTable, 'String'),
183 2 => array($entityId, 'Integer'),
184 );
185 return CRM_Core_DAO::singleValueQuery($query, $params);
186 }
187
188 /**
189 * Function to financial Account for payment instrument
190 *
191 * @param int $paymentInstrumentValue payment instrument value
192 *
193 * @return array|null|string
194 * @static
195 */
196 static function getInstrumentFinancialAccount($paymentInstrumentValue = NULL) {
197 if (!self::$financialAccount) {
198 $query = "SELECT ceft.financial_account_id, cov.value
199 FROM civicrm_entity_financial_account ceft
200 INNER JOIN civicrm_option_value cov ON cov.id = ceft.entity_id AND ceft.entity_table = 'civicrm_option_value'
201 INNER JOIN civicrm_option_group cog ON cog.id = cov.option_group_id
202 WHERE cog.name = 'payment_instrument' ";
203
204 if ($paymentInstrumentValue) {
205 $query .= " AND cov.value = '{$paymentInstrumentValue}' ";
206 return CRM_Core_DAO::singleValueQuery($query);
207 }
208 else {
209 $result = CRM_Core_DAO::executeQuery($query);
210 while ($result->fetch()) {
211 self::$financialAccount[$result->value] = $result->financial_account_id;
212 }
213 return self::$financialAccount;
214 }
215 }
216
217 return $paymentInstrumentValue ? self::$financialAccount[$paymentInstrumentValue] : self::$financialAccount;
218 }
219
220 /**
221 * Function to create default entity financial accounts
222 * for financial type
223 * CRM-12470
224 *
225 * @param $financialType
226 *
227 * @return array
228 * @static
229 */
230 static function createDefaultFinancialAccounts($financialType) {
231 $titles = array();
232 $financialAccountTypeID = CRM_Core_PseudoConstant::accountOptionValues('financial_account_type');
233 $accountRelationship = CRM_Core_PseudoConstant::accountOptionValues('account_relationship');
234 $relationships = array (
235 array_search('Accounts Receivable Account is', $accountRelationship) => array_search('Asset', $financialAccountTypeID),
236 array_search('Expense Account is', $accountRelationship) => array_search('Expenses', $financialAccountTypeID),
237 array_search('Cost of Sales Account is', $accountRelationship) => array_search('Cost of Sales', $financialAccountTypeID),
238 array_search('Income Account is', $accountRelationship) => array_search('Revenue', $financialAccountTypeID),
239 );
240
241 $dao = CRM_Core_DAO::executeQuery('SELECT id, financial_account_type_id FROM civicrm_financial_account WHERE name LIKE %1',
242 array(1 => array($financialType->name, 'String'))
243 );
244 $dao->fetch();
245 $existingFinancialAccount = array();
246 if (!$dao->N) {
247 $params = array(
248 'name' => $financialType->name,
249 'contact_id' => CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Domain', CRM_Core_Config::domainID(), 'contact_id'),
250 'financial_account_type_id' => array_search('Revenue', $financialAccountTypeID),
251 'description' => $financialType->description,
252 'account_type_code' => 'INC',
253 'is_active' => 1,
254 );
255 $financialAccount = CRM_Financial_BAO_FinancialAccount::add($params, CRM_Core_DAO::$_nullArray);
256 }
257 else {
258 $existingFinancialAccount[$dao->financial_account_type_id] = $dao->id;
259 }
260 $params = array (
261 'entity_table' => 'civicrm_financial_type',
262 'entity_id' => $financialType->id,
263 );
264 foreach ($relationships as $key => $value) {
265 if (!array_key_exists($value, $existingFinancialAccount)) {
266 if ($accountRelationship[$key] == 'Accounts Receivable Account is') {
267 $params['financial_account_id'] = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialAccount', 'Accounts Receivable', 'id', 'name');
268 if (!empty($params['financial_account_id'])) {
269 $titles[] = 'Accounts Receivable';
270 }
271 else {
272 $query = "SELECT financial_account_id, name FROM civicrm_entity_financial_account
273 LEFT JOIN civicrm_financial_account ON civicrm_financial_account.id = civicrm_entity_financial_account.financial_account_id
274 WHERE account_relationship = {$key} AND entity_table = 'civicrm_financial_type' LIMIT 1";
275 $dao = CRM_Core_DAO::executeQuery($query);
276 $dao->fetch();
277 $params['financial_account_id'] = $dao->financial_account_id;
278 $titles[] = $dao->name;
279 }
280 }
281 elseif ($accountRelationship[$key] == 'Income Account is' && empty($existingFinancialAccount)) {
282 $params['financial_account_id'] = $financialAccount->id;
283 }
284 else {
285 $query = "SELECT id, name FROM civicrm_financial_account WHERE is_default = 1 AND financial_account_type_id = {$value}";
286 $dao = CRM_Core_DAO::executeQuery($query);
287 $dao->fetch();
288 $params['financial_account_id'] = $dao->id;
289 $titles[] = $dao->name;
290 }
291 }
292 else {
293 $params['financial_account_id'] = $existingFinancialAccount[$value];
294 $titles[] = $financialType->name;
295 }
296 $params['account_relationship'] = $key;
297 self::add($params);
298 }
299 if (!empty($existingFinancialAccount)) {
300 $titles = array();
301 }
302 return $titles;
303 }
304 }
305