92b8aa8d6199e39f2ca84770126e0e5c122222e9
[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 * Financial Account for payment instrument.
156 *
157 * @param int $paymentInstrumentValue
158 * Payment instrument value.
159 *
160 * @return array|null|string
161 */
162 public static function getInstrumentFinancialAccount($paymentInstrumentValue = NULL) {
163 if (!self::$financialAccount) {
164 $query = "SELECT ceft.financial_account_id, cov.value
165 FROM civicrm_entity_financial_account ceft
166 INNER JOIN civicrm_option_value cov ON cov.id = ceft.entity_id AND ceft.entity_table = 'civicrm_option_value'
167 INNER JOIN civicrm_option_group cog ON cog.id = cov.option_group_id
168 WHERE cog.name = 'payment_instrument' ";
169
170 if ($paymentInstrumentValue) {
171 $query .= " AND cov.value = '{$paymentInstrumentValue}' ";
172 return CRM_Core_DAO::singleValueQuery($query);
173 }
174 else {
175 $result = CRM_Core_DAO::executeQuery($query);
176 while ($result->fetch()) {
177 self::$financialAccount[$result->value] = $result->financial_account_id;
178 }
179 return self::$financialAccount;
180 }
181 }
182
183 return $paymentInstrumentValue ? self::$financialAccount[$paymentInstrumentValue] : self::$financialAccount;
184 }
185
186 /**
187 * Create default entity financial accounts
188 * for financial type
189 * CRM-12470
190 *
191 * @param $financialType
192 *
193 * @return array
194 */
195 public static function createDefaultFinancialAccounts($financialType) {
196 $titles = array();
197 $financialAccountTypeID = CRM_Core_OptionGroup::values('financial_account_type', FALSE, FALSE, FALSE, NULL, 'name');
198 $accountRelationship = CRM_Core_OptionGroup::values('account_relationship', FALSE, FALSE, FALSE, NULL, 'name');
199
200 $relationships = array(
201 array_search('Accounts Receivable Account is', $accountRelationship) => array_search('Asset', $financialAccountTypeID),
202 array_search('Expense Account is', $accountRelationship) => array_search('Expenses', $financialAccountTypeID),
203 array_search('Cost of Sales Account is', $accountRelationship) => array_search('Cost of Sales', $financialAccountTypeID),
204 array_search('Income Account is', $accountRelationship) => array_search('Revenue', $financialAccountTypeID),
205 );
206
207 $dao = CRM_Core_DAO::executeQuery('SELECT id, financial_account_type_id FROM civicrm_financial_account WHERE name LIKE %1',
208 array(1 => array($financialType->name, 'String'))
209 );
210 $dao->fetch();
211 $existingFinancialAccount = array();
212 if (!$dao->N) {
213 $params = array(
214 'name' => $financialType->name,
215 'contact_id' => CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Domain', CRM_Core_Config::domainID(), 'contact_id'),
216 'financial_account_type_id' => array_search('Revenue', $financialAccountTypeID),
217 'description' => $financialType->description,
218 'account_type_code' => 'INC',
219 'is_active' => 1,
220 );
221 $financialAccount = CRM_Financial_BAO_FinancialAccount::add($params);
222 }
223 else {
224 $existingFinancialAccount[$dao->financial_account_type_id] = $dao->id;
225 }
226 $params = array(
227 'entity_table' => 'civicrm_financial_type',
228 'entity_id' => $financialType->id,
229 );
230 foreach ($relationships as $key => $value) {
231 if (!array_key_exists($value, $existingFinancialAccount)) {
232 if ($accountRelationship[$key] == 'Accounts Receivable Account is') {
233 $params['financial_account_id'] = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialAccount', 'Accounts Receivable', 'id', 'name');
234 if (!empty($params['financial_account_id'])) {
235 $titles[] = 'Accounts Receivable';
236 }
237 else {
238 $query = "SELECT financial_account_id, name FROM civicrm_entity_financial_account
239 LEFT JOIN civicrm_financial_account ON civicrm_financial_account.id = civicrm_entity_financial_account.financial_account_id
240 WHERE account_relationship = {$key} AND entity_table = 'civicrm_financial_type' LIMIT 1";
241 $dao = CRM_Core_DAO::executeQuery($query);
242 $dao->fetch();
243 $params['financial_account_id'] = $dao->financial_account_id;
244 $titles[] = $dao->name;
245 }
246 }
247 elseif ($accountRelationship[$key] == 'Income Account is' && empty($existingFinancialAccount)) {
248 $params['financial_account_id'] = $financialAccount->id;
249 }
250 else {
251 $query = "SELECT id, name FROM civicrm_financial_account WHERE is_default = 1 AND financial_account_type_id = {$value}";
252 $dao = CRM_Core_DAO::executeQuery($query);
253 $dao->fetch();
254 $params['financial_account_id'] = $dao->id;
255 $titles[] = $dao->name;
256 }
257 }
258 else {
259 $params['financial_account_id'] = $existingFinancialAccount[$value];
260 $titles[] = $financialType->name;
261 }
262 $params['account_relationship'] = $key;
263 self::add($params);
264 }
265 if (!empty($existingFinancialAccount)) {
266 $titles = array();
267 }
268 return $titles;
269 }
270
271 /**
272 * Validate account relationship with financial account type
273 *
274 * @param obj $financialTypeAccount of CRM_Financial_DAO_EntityFinancialAccount
275 *
276 */
277 public static function validateRelationship($financialTypeAccount) {
278 $financialAccountLinks = CRM_Financial_BAO_FinancialAccount::getfinancialAccountRelations();
279 $financialAccountType = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialAccount', $financialTypeAccount->financial_account_id, 'financial_account_type_id');
280 if (CRM_Utils_Array::value($financialTypeAccount->account_relationship, $financialAccountLinks) != $financialAccountType) {
281 $accountRelationships = CRM_Core_PseudoConstant::get('CRM_Financial_DAO_EntityFinancialAccount', 'account_relationship');
282 $params = array(
283 1 => $accountRelationships[$financialTypeAccount->account_relationship],
284 );
285 throw new Exception(ts("This financial account cannot have '%1' relationship.", $params));
286 }
287 }
288
289 }