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