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