CRM-21305, fixed sorting of column on batch transaction listing page
[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 * Fetch object based on array of properties.
44 *
45 * @param array $params
46 * (reference ) an assoc array of name/value pairs.
47 * @param array $defaults
48 * (reference ) an assoc array to hold the flattened values.
49 *
50 * @param array $allValues
51 *
52 * @return CRM_Contribute_BAO_ContributionType
53 */
54 public static function retrieve(&$params, &$defaults, &$allValues = array()) {
55 $financialTypeAccount = new CRM_Financial_DAO_EntityFinancialAccount();
56 $financialTypeAccount->copyValues($params);
57 $financialTypeAccount->find();
58 while ($financialTypeAccount->fetch()) {
59 CRM_Core_DAO::storeValues($financialTypeAccount, $defaults);
60 $allValues[] = $defaults;
61 }
62 return $defaults;
63 }
64
65 /**
66 * Add the financial types.
67 *
68 * @param array $params
69 * Reference array contains the values submitted by the form.
70 * @param array $ids
71 * Reference array contains the id.
72 *
73 * @return object
74 */
75 public static function add(&$params, &$ids = NULL) {
76 // action is taken depending upon the mode
77 $financialTypeAccount = new CRM_Financial_DAO_EntityFinancialAccount();
78 if ($params['entity_table'] != 'civicrm_financial_type') {
79 $financialTypeAccount->entity_id = $params['entity_id'];
80 $financialTypeAccount->entity_table = $params['entity_table'];
81 $financialTypeAccount->find(TRUE);
82 }
83 if (!empty($ids['entityFinancialAccount'])) {
84 $financialTypeAccount->id = $ids['entityFinancialAccount'];
85 $financialTypeAccount->find(TRUE);
86 }
87 $financialTypeAccount->copyValues($params);
88 self::validateRelationship($financialTypeAccount);
89 $financialTypeAccount->save();
90 return $financialTypeAccount;
91 }
92
93 /**
94 * Delete financial Types.
95 *
96 * @param int $financialTypeAccountId
97 * @param int $accountId
98 *
99 */
100 public static function del($financialTypeAccountId, $accountId = NULL) {
101 // check if financial type is present
102 $check = FALSE;
103 $relationValues = CRM_Core_PseudoConstant::get('CRM_Financial_DAO_EntityFinancialAccount', 'account_relationship');
104
105 $financialTypeId = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_EntityFinancialAccount', $financialTypeAccountId, 'entity_id');
106 // check dependencies
107 // FIXME more table containing financial_type_id to come
108 $dependency = array(
109 array('Contribute', 'Contribution'),
110 array('Contribute', 'ContributionPage'),
111 array('Member', 'MembershipType'),
112 array('Price', 'PriceFieldValue'),
113 array('Grant', 'Grant'),
114 array('Contribute', 'PremiumsProduct'),
115 array('Contribute', 'Product'),
116 array('Price', 'LineItem'),
117 );
118
119 foreach ($dependency as $name) {
120 $daoString = 'CRM_' . $name[0] . '_DAO_' . $name[1];
121 $dao = new $daoString();
122 $dao->financial_type_id = $financialTypeId;
123 if ($dao->find(TRUE)) {
124 $check = TRUE;
125 break;
126 }
127 }
128
129 if ($check) {
130 if ($name[1] == 'PremiumsProduct' || $name[1] == 'Product') {
131 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])));
132 }
133 else {
134 $accountRelationShipId = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_EntityFinancialAccount', $financialTypeAccountId, 'account_relationship');
135 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');
136 }
137 return CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/financial/financialType/accounts', "reset=1&action=browse&aid={$accountId}"));
138 }
139
140 // delete from financial Type table
141 $financialType = new CRM_Financial_DAO_EntityFinancialAccount();
142 $financialType->id = $financialTypeAccountId;
143 $financialType->find(TRUE);
144 $financialType->delete();
145 CRM_Core_Session::setStatus(ts('Unbalanced transactions may be created if you delete the account of type: %1.', array(1 => $relationValues[$financialType->account_relationship])));
146 }
147
148 /**
149 * Financial Account for payment instrument.
150 *
151 * @param int $paymentInstrumentValue
152 * Payment instrument value.
153 *
154 * @return null|int
155 */
156 public static function getInstrumentFinancialAccount($paymentInstrumentValue) {
157 $paymentInstrument = civicrm_api3('OptionValue', 'getsingle', array(
158 'return' => array("id"),
159 'value' => $paymentInstrumentValue,
160 'option_group_id' => "payment_instrument",
161 ));
162 $financialAccountId = CRM_Contribute_PseudoConstant::getRelationalFinancialAccount(
163 $paymentInstrument['id'],
164 NULL,
165 'civicrm_option_value'
166 );
167 return $financialAccountId;
168 }
169
170 /**
171 * Create default entity financial accounts
172 * for financial type
173 * CRM-12470
174 *
175 * @param $financialType
176 *
177 * @return array
178 */
179 public static function createDefaultFinancialAccounts($financialType) {
180 $titles = array();
181 $financialAccountTypeID = CRM_Core_OptionGroup::values('financial_account_type', FALSE, FALSE, FALSE, NULL, 'name');
182 $accountRelationship = CRM_Core_OptionGroup::values('account_relationship', FALSE, FALSE, FALSE, NULL, 'name');
183
184 $relationships = array(
185 array_search('Accounts Receivable Account is', $accountRelationship) => array_search('Asset', $financialAccountTypeID),
186 array_search('Expense Account is', $accountRelationship) => array_search('Expenses', $financialAccountTypeID),
187 array_search('Cost of Sales Account is', $accountRelationship) => array_search('Cost of Sales', $financialAccountTypeID),
188 array_search('Income Account is', $accountRelationship) => array_search('Revenue', $financialAccountTypeID),
189 );
190
191 $dao = CRM_Core_DAO::executeQuery('SELECT id, financial_account_type_id FROM civicrm_financial_account WHERE name LIKE %1',
192 array(1 => array($financialType->name, 'String'))
193 );
194 $dao->fetch();
195 $existingFinancialAccount = array();
196 if (!$dao->N) {
197 $params = array(
198 'name' => $financialType->name,
199 'contact_id' => CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Domain', CRM_Core_Config::domainID(), 'contact_id'),
200 'financial_account_type_id' => array_search('Revenue', $financialAccountTypeID),
201 'description' => $financialType->description,
202 'account_type_code' => 'INC',
203 'is_active' => 1,
204 );
205 $financialAccount = CRM_Financial_BAO_FinancialAccount::add($params);
206 }
207 else {
208 $existingFinancialAccount[$dao->financial_account_type_id] = $dao->id;
209 }
210 $params = array(
211 'entity_table' => 'civicrm_financial_type',
212 'entity_id' => $financialType->id,
213 );
214 foreach ($relationships as $key => $value) {
215 if (!array_key_exists($value, $existingFinancialAccount)) {
216 if ($accountRelationship[$key] == 'Accounts Receivable Account is') {
217 $params['financial_account_id'] = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialAccount', 'Accounts Receivable', 'id', 'name');
218 if (!empty($params['financial_account_id'])) {
219 $titles[] = 'Accounts Receivable';
220 }
221 else {
222 $query = "SELECT financial_account_id, name FROM civicrm_entity_financial_account
223 LEFT JOIN civicrm_financial_account ON civicrm_financial_account.id = civicrm_entity_financial_account.financial_account_id
224 WHERE account_relationship = {$key} AND entity_table = 'civicrm_financial_type' LIMIT 1";
225 $dao = CRM_Core_DAO::executeQuery($query);
226 $dao->fetch();
227 $params['financial_account_id'] = $dao->financial_account_id;
228 $titles[] = $dao->name;
229 }
230 }
231 elseif ($accountRelationship[$key] == 'Income Account is' && empty($existingFinancialAccount)) {
232 $params['financial_account_id'] = $financialAccount->id;
233 }
234 else {
235 $query = "SELECT id, name FROM civicrm_financial_account WHERE is_default = 1 AND financial_account_type_id = {$value}";
236 $dao = CRM_Core_DAO::executeQuery($query);
237 $dao->fetch();
238 $params['financial_account_id'] = $dao->id;
239 $titles[] = $dao->name;
240 }
241 }
242 else {
243 $params['financial_account_id'] = $existingFinancialAccount[$value];
244 $titles[] = $financialType->name;
245 }
246 $params['account_relationship'] = $key;
247 self::add($params);
248 }
249 if (!empty($existingFinancialAccount)) {
250 $titles = array();
251 }
252 return $titles;
253 }
254
255 /**
256 * Validate account relationship with financial account type
257 *
258 * @param obj $financialTypeAccount of CRM_Financial_DAO_EntityFinancialAccount
259 *
260 * @throws CRM_Core_Exception
261 */
262 public static function validateRelationship($financialTypeAccount) {
263 $financialAccountLinks = CRM_Financial_BAO_FinancialAccount::getfinancialAccountRelations();
264 $financialAccountType = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialAccount', $financialTypeAccount->financial_account_id, 'financial_account_type_id');
265 if (CRM_Utils_Array::value($financialTypeAccount->account_relationship, $financialAccountLinks) != $financialAccountType) {
266 $accountRelationships = CRM_Core_PseudoConstant::get('CRM_Financial_DAO_EntityFinancialAccount', 'account_relationship');
267 $params = array(
268 1 => $accountRelationships[$financialTypeAccount->account_relationship],
269 );
270 throw new CRM_Core_Exception(ts("This financial account cannot have '%1' relationship.", $params));
271 }
272 }
273
274 }