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