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