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