Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
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 | ||
36 | class CRM_Core_BAO_FinancialTrxn extends CRM_Financial_DAO_FinancialTrxn { | |
b5c2afd0 | 37 | /** |
100fef9d | 38 | * Class constructor |
b5c2afd0 | 39 | * |
b5c2afd0 EM |
40 | * @return \CRM_Financial_DAO_FinancialTrxn |
41 | */ | |
42 | /** | |
43 | * | |
44 | */ | |
00be9182 | 45 | public function __construct() { |
6a488035 TO |
46 | parent::__construct(); |
47 | } | |
48 | ||
49 | /** | |
100fef9d | 50 | * Takes an associative array and creates a financial transaction object |
6a488035 | 51 | * |
6a0b768e TO |
52 | * @param array $params |
53 | * (reference ) an assoc array of name/value pairs. | |
6a488035 | 54 | * |
6a0b768e TO |
55 | * @param string $trxnEntityTable |
56 | * Entity_table. | |
6a488035 | 57 | * |
c490a46a | 58 | * @return CRM_Core_BAO_FinancialTrxn object |
6a488035 TO |
59 | * @static |
60 | */ | |
e60f24eb | 61 | public static function create(&$params, $trxnEntityTable = NULL) { |
6a488035 TO |
62 | $trxn = new CRM_Financial_DAO_FinancialTrxn(); |
63 | $trxn->copyValues($params); | |
64 | $fids = array(); | |
65 | if (!CRM_Utils_Rule::currencyCode($trxn->currency)) { | |
66 | $config = CRM_Core_Config::singleton(); | |
67 | $trxn->currency = $config->defaultCurrency; | |
68 | } | |
69 | ||
70 | $trxn->save(); | |
71 | ||
72 | // save to entity_financial_trxn table | |
73 | $entityFinancialTrxnParams = | |
74 | array( | |
75 | 'entity_table' => "civicrm_contribution", | |
76 | 'financial_trxn_id' => $trxn->id, | |
77 | 'amount' => $params['total_amount'], | |
78 | 'currency' => $trxn->currency, | |
79 | ); | |
80 | ||
81 | if (!empty($trxnEntityTable)) { | |
82 | $entityFinancialTrxnParams['entity_table'] = $trxnEntityTable['entity_table']; | |
83 | $entityFinancialTrxnParams['entity_id'] = $trxnEntityTable['entity_id']; | |
84 | } | |
85 | else { | |
86 | $entityFinancialTrxnParams['entity_id'] = $params['contribution_id']; | |
87 | } | |
88 | ||
89 | $entityTrxn = new CRM_Financial_DAO_EntityFinancialTrxn(); | |
90 | $entityTrxn->copyValues($entityFinancialTrxnParams); | |
91 | $entityTrxn->save(); | |
92 | return $trxn; | |
93 | } | |
94 | ||
b5c2afd0 | 95 | /** |
100fef9d CW |
96 | * @param int $contributionId |
97 | * @param int $contributionFinancialTypeId | |
b5c2afd0 EM |
98 | * |
99 | * @return array | |
100 | */ | |
00be9182 | 101 | public static function getBalanceTrxnAmt($contributionId, $contributionFinancialTypeId = NULL) { |
29c61b58 PJ |
102 | if (!$contributionFinancialTypeId) { |
103 | $contributionFinancialTypeId = CRM_Core_DAO::getFieldValue('CRM_Contribute_BAO_Contribution', $contributionId, 'financial_type_id'); | |
104 | } | |
ede1935f PJ |
105 | $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' ")); |
106 | $toFinancialAccount = CRM_Contribute_PseudoConstant::financialAccountType($contributionFinancialTypeId, $relationTypeId); | |
520ec403 | 107 | $q = "SELECT ft.id, ft.total_amount FROM civicrm_financial_trxn ft INNER JOIN civicrm_entity_financial_trxn eft ON (eft.financial_trxn_id = ft.id AND eft.entity_table = 'civicrm_contribution') WHERE eft.entity_id = %1 AND ft.to_financial_account_id = %2"; |
1010c4e1 | 108 | |
ede1935f PJ |
109 | $p[1] = array($contributionId, 'Integer'); |
110 | $p[2] = array($toFinancialAccount, 'Integer'); | |
1010c4e1 | 111 | |
ede1935f PJ |
112 | $balanceAmtDAO = CRM_Core_DAO::executeQuery($q, $p); |
113 | $ret = array(); | |
22e263ad | 114 | while ($balanceAmtDAO->fetch()) { |
ede1935f PJ |
115 | $ret['trxn_id'] = $balanceAmtDAO->id; |
116 | $ret['total_amount'] = $balanceAmtDAO->total_amount; | |
117 | } | |
118 | ||
119 | return $ret; | |
120 | } | |
121 | ||
6a488035 | 122 | /** |
c490a46a | 123 | * Fetch object based on array of properties |
6a488035 | 124 | * |
6a0b768e TO |
125 | * @param array $params |
126 | * (reference ) an assoc array of name/value pairs. | |
127 | * @param array $defaults | |
128 | * (reference ) an assoc array to hold the flattened values. | |
6a488035 | 129 | * |
c490a46a | 130 | * @return CRM_Contribute_BAO_ContributionType object |
6a488035 TO |
131 | * @static |
132 | */ | |
481a74f4 TO |
133 | public static function retrieve(&$params, &$defaults) { |
134 | $financialItem = new CRM_Financial_DAO_FinancialTrxn(); | |
6a488035 | 135 | $financialItem->copyValues($params); |
4eeb9a5b | 136 | if ($financialItem->find(TRUE)) { |
481a74f4 | 137 | CRM_Core_DAO::storeValues($financialItem, $defaults); |
6a488035 TO |
138 | return $financialItem; |
139 | } | |
e60f24eb | 140 | return NULL; |
6a488035 TO |
141 | } |
142 | ||
143 | /** | |
6a488035 TO |
144 | * Given an entity_id and entity_table, check for corresponding entity_financial_trxn and financial_trxn record. |
145 | * NOTE: This should be moved to separate BAO for EntityFinancialTrxn when we start adding more code for that object. | |
146 | * | |
6a0b768e TO |
147 | * @param $entity_id |
148 | * Id of the entity usually the contactID. | |
149 | * @param string $orderBy | |
150 | * To get single trxn id for a entity table i.e last or first. | |
dd244018 EM |
151 | * @param bool $newTrxn |
152 | * | |
c490a46a | 153 | * @return array $tag array of category id's the contact belongs to. |
6a488035 | 154 | * |
6a488035 TO |
155 | * @static |
156 | */ | |
00be9182 | 157 | public static function getFinancialTrxnId($entity_id, $orderBy = 'ASC', $newTrxn = FALSE) { |
6a488035 TO |
158 | $ids = array('entityFinancialTrxnId' => NULL, 'financialTrxnId' => NULL); |
159 | ||
160 | $condition = ""; | |
161 | if (!$newTrxn) { | |
162 | $condition = " AND ((ceft1.entity_table IS NOT NULL) OR (cft.payment_instrument_id IS NOT NULL AND ceft1.entity_table IS NULL)) "; | |
163 | } | |
21d32567 DL |
164 | |
165 | if ($orderBy) { | |
166 | $orderBy = CRM_Utils_Type::escape($orderBy, 'String'); | |
167 | } | |
168 | ||
6a488035 | 169 | $query = "SELECT ceft.id, ceft.financial_trxn_id FROM `civicrm_financial_trxn` cft |
cbb7c7e0 | 170 | LEFT JOIN civicrm_entity_financial_trxn ceft |
6a488035 TO |
171 | ON ceft.financial_trxn_id = cft.id AND ceft.entity_table = 'civicrm_contribution' |
172 | LEFT JOIN civicrm_entity_financial_trxn ceft1 | |
173 | ON ceft1.financial_trxn_id = cft.id AND ceft1.entity_table = 'civicrm_financial_item' | |
174 | LEFT JOIN civicrm_financial_item cfi ON ceft1.entity_table = 'civicrm_financial_item' and cfi.id = ceft1.entity_id | |
175 | WHERE ceft.entity_id = %1 AND (cfi.entity_table <> 'civicrm_financial_trxn' or cfi.entity_table is NULL) | |
176 | {$condition} | |
177 | ORDER BY cft.id {$orderBy} | |
cbb7c7e0 | 178 | LIMIT 1;"; |
179 | ||
6a488035 TO |
180 | $params = array(1 => array($entity_id, 'Integer')); |
181 | $dao = CRM_Core_DAO::executeQuery($query, $params); | |
182 | if ($dao->fetch()) { | |
183 | $ids['entityFinancialTrxnId'] = $dao->id; | |
184 | $ids['financialTrxnId'] = $dao->financial_trxn_id; | |
185 | } | |
186 | return $ids; | |
187 | } | |
188 | ||
189 | /** | |
190 | * Given an entity_id and entity_table, check for corresponding entity_financial_trxn and financial_trxn record. | |
c490a46a | 191 | * @todo This should be moved to separate BAO for EntityFinancialTrxn when we start adding more code for that object. |
da6b46f4 | 192 | * |
6a0b768e TO |
193 | * @param int $entity_id |
194 | * Id of the entity usually the contactID. | |
6a488035 | 195 | * |
c490a46a | 196 | * @return array $tag array of catagory id's the contact belongs to. |
6a488035 | 197 | * |
6a488035 TO |
198 | * @static |
199 | */ | |
00be9182 | 200 | public static function getFinancialTrxnTotal($entity_id) { |
6a488035 TO |
201 | $query = " |
202 | SELECT (ft.amount+SUM(ceft.amount)) AS total FROM civicrm_entity_financial_trxn AS ft | |
cbb7c7e0 | 203 | LEFT JOIN civicrm_entity_financial_trxn AS ceft ON ft.financial_trxn_id = ceft.entity_id |
6a488035 TO |
204 | WHERE ft.entity_table = 'civicrm_contribution' AND ft.entity_id = %1 |
205 | "; | |
206 | ||
207 | $sqlParams = array(1 => array($entity_id, 'Integer')); | |
d3e86119 | 208 | return CRM_Core_DAO::singleValueQuery($query, $sqlParams); |
6a488035 TO |
209 | |
210 | } | |
77b97be7 | 211 | |
6a488035 TO |
212 | /** |
213 | * Given an financial_trxn_id check for previous entity_financial_trxn. | |
214 | * | |
6a0b768e TO |
215 | * @param $financial_trxn_id |
216 | * Id of the latest payment. | |
77b97be7 | 217 | * |
6a488035 | 218 | * |
c490a46a | 219 | * @return array $payment array of previous payments |
6a488035 | 220 | * |
6a488035 TO |
221 | * @static |
222 | */ | |
00be9182 | 223 | public static function getPayments($financial_trxn_id) { |
6a488035 TO |
224 | $query = " |
225 | SELECT ef1.financial_trxn_id, sum(ef1.amount) amount | |
226 | FROM civicrm_entity_financial_trxn ef1 | |
227 | LEFT JOIN civicrm_entity_financial_trxn ef2 ON ef1.financial_trxn_id = ef2.entity_id | |
228 | WHERE ef2.financial_trxn_id =%1 | |
229 | AND ef2.entity_table = 'civicrm_financial_trxn' | |
230 | AND ef1.entity_table = 'civicrm_financial_item' | |
231 | GROUP BY ef1.financial_trxn_id | |
232 | UNION | |
233 | SELECT ef1.financial_trxn_id, ef1.amount | |
234 | FROM civicrm_entity_financial_trxn ef1 | |
235 | LEFT JOIN civicrm_entity_financial_trxn ef2 ON ef1.entity_id = ef2.entity_id | |
236 | WHERE ef2.financial_trxn_id =%1 | |
237 | AND ef2.entity_table = 'civicrm_financial_trxn' | |
238 | AND ef1.entity_table = 'civicrm_financial_trxn'"; | |
239 | ||
240 | $sqlParams = array(1 => array($financial_trxn_id, 'Integer')); | |
241 | $dao = CRM_Core_DAO::executeQuery($query, $sqlParams); | |
242 | $i = 0; | |
243 | $result = array(); | |
244 | while ($dao->fetch()) { | |
245 | $result[$i]['financial_trxn_id'] = $dao->financial_trxn_id; | |
246 | $result[$i]['amount'] = $dao->amount; | |
247 | $i++; | |
248 | } | |
249 | ||
250 | if (empty($result)) { | |
251 | $query = "SELECT sum( amount ) amount FROM civicrm_entity_financial_trxn WHERE financial_trxn_id =%1 AND entity_table = 'civicrm_financial_item'"; | |
252 | $sqlParams = array(1 => array($financial_trxn_id, 'Integer')); | |
253 | $dao = CRM_Core_DAO::executeQuery($query, $sqlParams); | |
254 | ||
255 | if ($dao->fetch()) { | |
256 | $result[0]['financial_trxn_id'] = $financial_trxn_id; | |
257 | $result[0]['amount'] = $dao->amount; | |
258 | } | |
259 | } | |
260 | return $result; | |
261 | } | |
262 | ||
263 | /** | |
264 | * Given an entity_id and entity_table, check for corresponding entity_financial_trxn and financial_trxn record. | |
265 | * NOTE: This should be moved to separate BAO for EntityFinancialTrxn when we start adding more code for that object. | |
266 | * | |
6a0b768e TO |
267 | * @param $entity_id |
268 | * Id of the entity usually the contactID. | |
269 | * @param string $entity_table | |
270 | * Name of the entity table usually 'civicrm_contact'. | |
6a488035 | 271 | * |
c490a46a | 272 | * @return array $tag array of catagory id's the contact belongs to. |
6a488035 | 273 | * |
6a488035 TO |
274 | * @static |
275 | */ | |
00be9182 | 276 | public static function getFinancialTrxnLineTotal($entity_id, $entity_table = 'civicrm_contribution') { |
6a488035 TO |
277 | $query = "SELECT lt.price_field_value_id AS id, ft.financial_trxn_id,ft.amount AS amount FROM civicrm_entity_financial_trxn AS ft |
278 | LEFT JOIN civicrm_financial_item AS fi ON fi.id = ft.entity_id AND fi.entity_table = 'civicrm_line_item' AND ft.entity_table = 'civicrm_financial_item' | |
cbb7c7e0 | 279 | LEFT JOIN civicrm_line_item AS lt ON lt.id = fi.entity_id AND lt.entity_table = %2 |
6a488035 TO |
280 | WHERE lt.entity_id = %1 "; |
281 | ||
282 | $sqlParams = array(1 => array($entity_id, 'Integer'), 2 => array($entity_table, 'String')); | |
283 | $dao = CRM_Core_DAO::executeQuery($query, $sqlParams); | |
9b873358 | 284 | while ($dao->fetch()) { |
6a488035 TO |
285 | $result[$dao->financial_trxn_id][$dao->id] = $dao->amount; |
286 | } | |
287 | if (!empty($result)) { | |
288 | return $result; | |
289 | } | |
290 | else { | |
e60f24eb | 291 | return NULL; |
6a488035 TO |
292 | } |
293 | } | |
294 | ||
295 | /** | |
296 | * Delete financial transaction | |
297 | * | |
100fef9d | 298 | * @param int $entity_id |
4eeb9a5b | 299 | * @return bool |
ab8a593e | 300 | * TRUE on success, FALSE otherwise. |
6a488035 TO |
301 | * @static |
302 | */ | |
00be9182 | 303 | public static function deleteFinancialTrxn($entity_id) { |
de1c25e1 | 304 | $query = "DELETE ceft1, cfi, ceft, cft FROM `civicrm_financial_trxn` cft |
cbb7c7e0 | 305 | LEFT JOIN civicrm_entity_financial_trxn ceft |
de1c25e1 PN |
306 | ON ceft.financial_trxn_id = cft.id AND ceft.entity_table = 'civicrm_contribution' |
307 | LEFT JOIN civicrm_entity_financial_trxn ceft1 | |
308 | ON ceft1.financial_trxn_id = cft.id AND ceft1.entity_table = 'civicrm_financial_item' | |
cbb7c7e0 | 309 | LEFT JOIN civicrm_financial_item cfi |
de1c25e1 PN |
310 | ON ceft1.entity_table = 'civicrm_financial_item' and cfi.id = ceft1.entity_id |
311 | WHERE ceft.entity_id = %1"; | |
312 | CRM_Core_DAO::executeQuery($query, array(1 => array($entity_id, 'Integer'))); | |
313 | return TRUE; | |
6a488035 TO |
314 | } |
315 | ||
316 | /** | |
100fef9d | 317 | * Create financial transaction for premium |
6a488035 | 318 | * |
6a488035 TO |
319 | * @static |
320 | */ | |
00be9182 | 321 | public static function createPremiumTrxn($params) { |
8cc574cf | 322 | if ((empty($params['financial_type_id']) || empty($params['contributionId'])) && empty($params['oldPremium'])) { |
6a488035 TO |
323 | return; |
324 | } | |
cbb7c7e0 | 325 | |
a7488080 | 326 | if (!empty($params['cost'])) { |
6a488035 TO |
327 | $contributionStatuses = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); |
328 | $financialAccountType = CRM_Contribute_PseudoConstant::financialAccountType($params['financial_type_id']); | |
f743a6eb | 329 | $accountRelationship = CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND label IN ('Premiums Inventory Account is', 'Cost of Sales Account is')"); |
0d8afee2 | 330 | $toFinancialAccount = !empty($params['isDeleted']) ? 'Premiums Inventory Account is' : 'Cost of Sales Account is'; |
d3e86119 | 331 | $fromFinancialAccount = !empty($params['isDeleted']) ? 'Cost of Sales Account is' : 'Premiums Inventory Account is'; |
6a488035 TO |
332 | $accountRelationship = array_flip($accountRelationship); |
333 | $financialtrxn = array( | |
334 | 'to_financial_account_id' => $financialAccountType[$accountRelationship[$toFinancialAccount]], | |
335 | 'from_financial_account_id' => $financialAccountType[$accountRelationship[$fromFinancialAccount]], | |
336 | 'trxn_date' => date('YmdHis'), | |
337 | 'total_amount' => CRM_Utils_Array::value('cost', $params) ? $params['cost'] : 0, | |
338 | 'currency' => CRM_Utils_Array::value('currency', $params), | |
21dfd5f5 | 339 | 'status_id' => array_search('Completed', $contributionStatuses), |
6a488035 TO |
340 | ); |
341 | $trxnEntityTable['entity_table'] = 'civicrm_contribution'; | |
342 | $trxnEntityTable['entity_id'] = $params['contributionId']; | |
343 | CRM_Core_BAO_FinancialTrxn::create($financialtrxn, $trxnEntityTable); | |
344 | } | |
345 | ||
a7488080 | 346 | if (!empty($params['oldPremium'])) { |
6a488035 | 347 | $premiumParams = array( |
21dfd5f5 | 348 | 'id' => $params['oldPremium']['product_id'], |
6a488035 TO |
349 | ); |
350 | $productDetails = array(); | |
351 | CRM_Contribute_BAO_ManagePremiums::retrieve($premiumParams, $productDetails); | |
352 | $params = array( | |
353 | 'cost' => CRM_Utils_Array::value('cost', $productDetails), | |
354 | 'currency' => CRM_Utils_Array::value('currency', $productDetails), | |
355 | 'financial_type_id' => CRM_Utils_Array::value('financial_type_id', $productDetails), | |
356 | 'contributionId' => $params['oldPremium']['contribution_id'], | |
21dfd5f5 | 357 | 'isDeleted' => TRUE, |
6a488035 TO |
358 | ); |
359 | CRM_Core_BAO_FinancialTrxn::createPremiumTrxn($params); | |
360 | } | |
361 | } | |
c490a46a | 362 | |
6a488035 | 363 | /** |
100fef9d | 364 | * Create financial trxn and items when fee is charged |
6a488035 | 365 | * |
6a0b768e TO |
366 | * @param array $params |
367 | * To create trxn entries. | |
6a488035 | 368 | * |
6a488035 TO |
369 | * @static |
370 | */ | |
00be9182 | 371 | public static function recordFees($params) { |
f743a6eb | 372 | $expenseTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Expense Account is' ")); |
6a488035 TO |
373 | $domainId = CRM_Core_Config::domainID(); |
374 | $amount = 0; | |
a7488080 | 375 | if (!empty($params['prevContribution'])) { |
6a488035 TO |
376 | $amount = $params['prevContribution']->fee_amount; |
377 | } | |
378 | $amount = $params['fee_amount'] - $amount; | |
78b79549 PN |
379 | if (!$amount) { |
380 | return FALSE; | |
381 | } | |
39383f5f PJ |
382 | $contributionId = isset($params['contribution']->id) ? $params['contribution']->id : $params['contribution_id']; |
383 | if (empty($params['financial_type_id'])) { | |
384 | $financialTypeId = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $contributionId, 'financial_type_id', 'id'); | |
385 | } | |
386 | else { | |
387 | $financialTypeId = $params['financial_type_id']; | |
388 | } | |
389 | $financialAccount = CRM_Contribute_PseudoConstant::financialAccountType($financialTypeId, $expenseTypeId); | |
390 | ||
6a488035 TO |
391 | $params['trxnParams']['from_financial_account_id'] = $params['to_financial_account_id']; |
392 | $params['trxnParams']['to_financial_account_id'] = $financialAccount; | |
393 | $params['trxnParams']['total_amount'] = $amount; | |
cbb7c7e0 | 394 | $params['trxnParams']['fee_amount'] = |
6a488035 TO |
395 | $params['trxnParams']['net_amount'] = 0; |
396 | $params['trxnParams']['status_id'] = CRM_Core_OptionGroup::getValue('contribution_status','Completed','name'); | |
39383f5f | 397 | $params['trxnParams']['contribution_id'] = $contributionId; |
6a488035 | 398 | $trxn = self::create($params['trxnParams']); |
a7488080 | 399 | if (empty($params['entity_id'])) { |
6a488035 TO |
400 | $financialTrxnID = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($params['trxnParams']['contribution_id'], 'DESC'); |
401 | $params['entity_id'] = $financialTrxnID['financialTrxnId']; | |
402 | } | |
cbb7c7e0 | 403 | $fItemParams = |
6a488035 TO |
404 | array( |
405 | 'financial_account_id' => $financialAccount, | |
406 | 'contact_id' => CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Domain', $domainId, 'contact_id'), | |
407 | 'created_date' => date('YmdHis'), | |
408 | 'transaction_date' => date('YmdHis'), | |
409 | 'amount' => $amount, | |
410 | 'description' => 'Fee', | |
411 | 'status_id' => CRM_Core_OptionGroup::getValue('financial_item_status','Paid','name'), | |
412 | 'entity_table' => 'civicrm_financial_trxn', | |
413 | 'entity_id' => $params['entity_id'], | |
414 | 'currency' => $params['trxnParams']['currency'], | |
415 | ); | |
416 | $trxnIDS['id'] = $trxn->id; | |
417 | $financialItem = CRM_Financial_BAO_FinancialItem::create($fItemParams, NULL, $trxnIDS); | |
418 | } | |
6a488035 | 419 | |
0f602e3f | 420 | /* |
c490a46a | 421 | * get partial payment amount and type of it |
0f602e3f PJ |
422 | * return @array : payment type => amount |
423 | * payment type : 'amount_owed' or 'refund_due' | |
424 | */ | |
b5c2afd0 | 425 | /** |
100fef9d | 426 | * @param int $entityId |
b5c2afd0 EM |
427 | * @param string $entityName |
428 | * @param bool $returnType | |
e60f24eb | 429 | * @param NULL $lineItemTotal |
b5c2afd0 | 430 | * |
e60f24eb | 431 | * @return array|int|NULL|string |
b5c2afd0 | 432 | */ |
00be9182 | 433 | public static function getPartialPaymentWithType($entityId, $entityName = 'participant', $returnType = TRUE, $lineItemTotal = NULL) { |
0f602e3f PJ |
434 | $value = NULL; |
435 | if (empty($entityName)) { | |
436 | return $value; | |
437 | } | |
438 | ||
439 | if ($entityName == 'participant') { | |
bc2eeabb | 440 | $contributionId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_ParticipantPayment', $entityId, 'contribution_id', 'participant_id'); |
e8cf3013 | 441 | $financialTypeId = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $contributionId, 'financial_type_id'); |
0f602e3f PJ |
442 | |
443 | if ($contributionId && $financialTypeId) { | |
444 | $statusId = CRM_Core_OptionGroup::getValue('contribution_status', 'Completed', 'name'); | |
1010c4e1 PJ |
445 | $refundStatusId = CRM_Core_OptionGroup::getValue('contribution_status', 'Refunded', 'name'); |
446 | ||
0f602e3f PJ |
447 | $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' ")); |
448 | $toFinancialAccount = CRM_Contribute_PseudoConstant::financialAccountType($financialTypeId, $relationTypeId); | |
8cf01b22 DG |
449 | $feeRelationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Expense Account is' ")); |
450 | $feeFinancialAccount = CRM_Contribute_PseudoConstant::financialAccountType($financialTypeId, $feeRelationTypeId); | |
0f602e3f | 451 | |
bc2eeabb | 452 | if (empty($lineItemTotal)) { |
28e4e707 PJ |
453 | $ids = CRM_Event_BAO_Participant::getParticipantIds($contributionId); |
454 | if (count($ids) > 1) { | |
455 | $total = 0; | |
456 | foreach ($ids as $val) { | |
457 | $total += CRM_Price_BAO_LineItem::getLineTotal($val, 'civicrm_participant'); | |
458 | } | |
459 | $lineItemTotal = $total; | |
460 | } | |
461 | else { | |
462 | $lineItemTotal = CRM_Price_BAO_LineItem::getLineTotal($entityId, 'civicrm_participant'); | |
463 | } | |
bc2eeabb PJ |
464 | } |
465 | $sqlFtTotalAmt = " | |
466 | SELECT SUM(ft.total_amount) | |
0f602e3f | 467 | FROM civicrm_financial_trxn ft |
bc2eeabb PJ |
468 | LEFT JOIN civicrm_entity_financial_trxn eft ON (ft.id = eft.financial_trxn_id AND eft.entity_table = 'civicrm_contribution') |
469 | LEFT JOIN civicrm_contribution c ON (eft.entity_id = c.id) | |
470 | LEFT JOIN civicrm_participant_payment pp ON (pp.contribution_id = c.id) | |
8cf01b22 | 471 | WHERE pp.participant_id = {$entityId} AND ft.to_financial_account_id != {$toFinancialAccount} AND ft.to_financial_account_id != {$feeFinancialAccount} |
1010c4e1 | 472 | AND ft.status_id IN ({$statusId}, {$refundStatusId}) |
0f602e3f PJ |
473 | "; |
474 | $ftTotalAmt = CRM_Core_DAO::singleValueQuery($sqlFtTotalAmt); | |
bc2eeabb PJ |
475 | $value = 0; |
476 | if ($ftTotalAmt) { | |
477 | $value = $paymentVal = $lineItemTotal - $ftTotalAmt; | |
478 | } | |
29c61b58 PJ |
479 | if ($returnType) { |
480 | $value = array(); | |
481 | if ($paymentVal < 0) { | |
482 | $value['refund_due'] = $paymentVal; | |
483 | } | |
484 | elseif ($paymentVal > 0) { | |
485 | $value['amount_owed'] = $paymentVal; | |
486 | } | |
bc2eeabb PJ |
487 | elseif ($lineItemTotal == $ftTotalAmt) { |
488 | $value['full_paid'] = $ftTotalAmt; | |
489 | } | |
0f602e3f PJ |
490 | } |
491 | } | |
492 | } | |
493 | return $value; | |
494 | } | |
0d8afee2 | 495 | } |