INFRA-132 - Put space after flow-control (if/switch/for/foreach/while)
[civicrm-core.git] / CRM / Core / BAO / FinancialTrxn.php
CommitLineData
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
36class 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 */
00be9182 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 */
00be9182 133 public static function retrieve( &$params, &$defaults ) {
6a488035
TO
134 $financialItem = new CRM_Financial_DAO_FinancialTrxn( );
135 $financialItem->copyValues($params);
136 if ($financialItem->find(true)) {
137 CRM_Core_DAO::storeValues( $financialItem, $defaults );
138 return $financialItem;
139 }
140 return null;
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 170LEFT JOIN civicrm_entity_financial_trxn ceft
6a488035
TO
171ON ceft.financial_trxn_id = cft.id AND ceft.entity_table = 'civicrm_contribution'
172LEFT JOIN civicrm_entity_financial_trxn ceft1
173ON ceft1.financial_trxn_id = cft.id AND ceft1.entity_table = 'civicrm_financial_item'
174LEFT JOIN civicrm_financial_item cfi ON ceft1.entity_table = 'civicrm_financial_item' and cfi.id = ceft1.entity_id
175WHERE ceft.entity_id = %1 AND (cfi.entity_table <> 'civicrm_financial_trxn' or cfi.entity_table is NULL)
176{$condition}
177ORDER BY cft.id {$orderBy}
cbb7c7e0 178LIMIT 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 203LEFT JOIN civicrm_entity_financial_trxn AS ceft ON ft.financial_trxn_id = ceft.entity_id
6a488035
TO
204WHERE ft.entity_table = 'civicrm_contribution' AND ft.entity_id = %1
205 ";
206
207 $sqlParams = array(1 => array($entity_id, 'Integer'));
208 return CRM_Core_DAO::singleValueQuery($query, $sqlParams);
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 = "
225SELECT ef1.financial_trxn_id, sum(ef1.amount) amount
226FROM civicrm_entity_financial_trxn ef1
227LEFT JOIN civicrm_entity_financial_trxn ef2 ON ef1.financial_trxn_id = ef2.entity_id
228WHERE ef2.financial_trxn_id =%1
229 AND ef2.entity_table = 'civicrm_financial_trxn'
230 AND ef1.entity_table = 'civicrm_financial_item'
231GROUP BY ef1.financial_trxn_id
232UNION
233SELECT ef1.financial_trxn_id, ef1.amount
234FROM civicrm_entity_financial_trxn ef1
235LEFT JOIN civicrm_entity_financial_trxn ef2 ON ef1.entity_id = ef2.entity_id
236WHERE 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
278LEFT 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 279LEFT JOIN civicrm_line_item AS lt ON lt.id = fi.entity_id AND lt.entity_table = %2
6a488035
TO
280WHERE 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);
22e263ad 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 {
291 return null;
292 }
293 }
294
295 /**
296 * Delete financial transaction
297 *
100fef9d 298 * @param int $entity_id
6a488035 299 * @return true on success, false otherwise
6a488035
TO
300 * @static
301 */
00be9182 302 public static function deleteFinancialTrxn($entity_id) {
de1c25e1 303 $query = "DELETE ceft1, cfi, ceft, cft FROM `civicrm_financial_trxn` cft
cbb7c7e0 304LEFT JOIN civicrm_entity_financial_trxn ceft
de1c25e1
PN
305 ON ceft.financial_trxn_id = cft.id AND ceft.entity_table = 'civicrm_contribution'
306LEFT JOIN civicrm_entity_financial_trxn ceft1
307 ON ceft1.financial_trxn_id = cft.id AND ceft1.entity_table = 'civicrm_financial_item'
cbb7c7e0 308LEFT JOIN civicrm_financial_item cfi
de1c25e1
PN
309 ON ceft1.entity_table = 'civicrm_financial_item' and cfi.id = ceft1.entity_id
310WHERE ceft.entity_id = %1";
311 CRM_Core_DAO::executeQuery($query, array(1 => array($entity_id, 'Integer')));
312 return TRUE;
6a488035
TO
313 }
314
315 /**
100fef9d 316 * Create financial transaction for premium
6a488035 317 *
6a488035
TO
318 * @static
319 */
00be9182 320 public static function createPremiumTrxn($params) {
8cc574cf 321 if ((empty($params['financial_type_id']) || empty($params['contributionId'])) && empty($params['oldPremium'])) {
6a488035
TO
322 return;
323 }
cbb7c7e0 324
a7488080 325 if (!empty($params['cost'])) {
6a488035
TO
326 $contributionStatuses = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
327 $financialAccountType = CRM_Contribute_PseudoConstant::financialAccountType($params['financial_type_id']);
f743a6eb 328 $accountRelationship = CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND label IN ('Premiums Inventory Account is', 'Cost of Sales Account is')");
0d8afee2
CW
329 $toFinancialAccount = !empty($params['isDeleted']) ? 'Premiums Inventory Account is' : 'Cost of Sales Account is';
330 $fromFinancialAccount = !empty($params['isDeleted']) ? 'Cost of Sales Account is': 'Premiums Inventory Account is';
6a488035
TO
331 $accountRelationship = array_flip($accountRelationship);
332 $financialtrxn = array(
333 'to_financial_account_id' => $financialAccountType[$accountRelationship[$toFinancialAccount]],
334 'from_financial_account_id' => $financialAccountType[$accountRelationship[$fromFinancialAccount]],
335 'trxn_date' => date('YmdHis'),
336 'total_amount' => CRM_Utils_Array::value('cost', $params) ? $params['cost'] : 0,
337 'currency' => CRM_Utils_Array::value('currency', $params),
338 'status_id' => array_search('Completed', $contributionStatuses)
339 );
340 $trxnEntityTable['entity_table'] = 'civicrm_contribution';
341 $trxnEntityTable['entity_id'] = $params['contributionId'];
342 CRM_Core_BAO_FinancialTrxn::create($financialtrxn, $trxnEntityTable);
343 }
344
a7488080 345 if (!empty($params['oldPremium'])) {
6a488035
TO
346 $premiumParams = array(
347 'id' => $params['oldPremium']['product_id']
348 );
349 $productDetails = array();
350 CRM_Contribute_BAO_ManagePremiums::retrieve($premiumParams, $productDetails);
351 $params = array(
352 'cost' => CRM_Utils_Array::value('cost', $productDetails),
353 'currency' => CRM_Utils_Array::value('currency', $productDetails),
354 'financial_type_id' => CRM_Utils_Array::value('financial_type_id', $productDetails),
355 'contributionId' => $params['oldPremium']['contribution_id'],
356 'isDeleted' => TRUE
357 );
358 CRM_Core_BAO_FinancialTrxn::createPremiumTrxn($params);
359 }
360 }
c490a46a 361
6a488035 362 /**
100fef9d 363 * Create financial trxn and items when fee is charged
6a488035 364 *
6a0b768e
TO
365 * @param array $params
366 * To create trxn entries.
6a488035 367 *
6a488035
TO
368 * @static
369 */
00be9182 370 public static function recordFees($params) {
f743a6eb 371 $expenseTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Expense Account is' "));
6a488035
TO
372 $domainId = CRM_Core_Config::domainID();
373 $amount = 0;
a7488080 374 if (!empty($params['prevContribution'])) {
6a488035
TO
375 $amount = $params['prevContribution']->fee_amount;
376 }
377 $amount = $params['fee_amount'] - $amount;
78b79549
PN
378 if (!$amount) {
379 return FALSE;
380 }
39383f5f
PJ
381 $contributionId = isset($params['contribution']->id) ? $params['contribution']->id : $params['contribution_id'];
382 if (empty($params['financial_type_id'])) {
383 $financialTypeId = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $contributionId, 'financial_type_id', 'id');
384 }
385 else {
386 $financialTypeId = $params['financial_type_id'];
387 }
388 $financialAccount = CRM_Contribute_PseudoConstant::financialAccountType($financialTypeId, $expenseTypeId);
389
6a488035
TO
390 $params['trxnParams']['from_financial_account_id'] = $params['to_financial_account_id'];
391 $params['trxnParams']['to_financial_account_id'] = $financialAccount;
392 $params['trxnParams']['total_amount'] = $amount;
cbb7c7e0 393 $params['trxnParams']['fee_amount'] =
6a488035
TO
394 $params['trxnParams']['net_amount'] = 0;
395 $params['trxnParams']['status_id'] = CRM_Core_OptionGroup::getValue('contribution_status','Completed','name');
39383f5f 396 $params['trxnParams']['contribution_id'] = $contributionId;
6a488035 397 $trxn = self::create($params['trxnParams']);
a7488080 398 if (empty($params['entity_id'])) {
6a488035
TO
399 $financialTrxnID = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($params['trxnParams']['contribution_id'], 'DESC');
400 $params['entity_id'] = $financialTrxnID['financialTrxnId'];
401 }
cbb7c7e0 402 $fItemParams =
6a488035
TO
403 array(
404 'financial_account_id' => $financialAccount,
405 'contact_id' => CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Domain', $domainId, 'contact_id'),
406 'created_date' => date('YmdHis'),
407 'transaction_date' => date('YmdHis'),
408 'amount' => $amount,
409 'description' => 'Fee',
410 'status_id' => CRM_Core_OptionGroup::getValue('financial_item_status','Paid','name'),
411 'entity_table' => 'civicrm_financial_trxn',
412 'entity_id' => $params['entity_id'],
413 'currency' => $params['trxnParams']['currency'],
414 );
415 $trxnIDS['id'] = $trxn->id;
416 $financialItem = CRM_Financial_BAO_FinancialItem::create($fItemParams, NULL, $trxnIDS);
417 }
6a488035 418
0f602e3f 419 /*
c490a46a 420 * get partial payment amount and type of it
0f602e3f
PJ
421 * return @array : payment type => amount
422 * payment type : 'amount_owed' or 'refund_due'
423 */
b5c2afd0 424 /**
100fef9d 425 * @param int $entityId
b5c2afd0
EM
426 * @param string $entityName
427 * @param bool $returnType
428 * @param null $lineItemTotal
429 *
430 * @return array|int|null|string
431 */
00be9182 432 public static function getPartialPaymentWithType($entityId, $entityName = 'participant', $returnType = TRUE, $lineItemTotal = NULL) {
0f602e3f
PJ
433 $value = NULL;
434 if (empty($entityName)) {
435 return $value;
436 }
437
438 if ($entityName == 'participant') {
bc2eeabb 439 $contributionId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_ParticipantPayment', $entityId, 'contribution_id', 'participant_id');
e8cf3013 440 $financialTypeId = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $contributionId, 'financial_type_id');
0f602e3f
PJ
441
442 if ($contributionId && $financialTypeId) {
443 $statusId = CRM_Core_OptionGroup::getValue('contribution_status', 'Completed', 'name');
1010c4e1
PJ
444 $refundStatusId = CRM_Core_OptionGroup::getValue('contribution_status', 'Refunded', 'name');
445
0f602e3f
PJ
446 $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' "));
447 $toFinancialAccount = CRM_Contribute_PseudoConstant::financialAccountType($financialTypeId, $relationTypeId);
8cf01b22
DG
448 $feeRelationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Expense Account is' "));
449 $feeFinancialAccount = CRM_Contribute_PseudoConstant::financialAccountType($financialTypeId, $feeRelationTypeId);
0f602e3f 450
bc2eeabb 451 if (empty($lineItemTotal)) {
28e4e707
PJ
452 $ids = CRM_Event_BAO_Participant::getParticipantIds($contributionId);
453 if (count($ids) > 1) {
454 $total = 0;
455 foreach ($ids as $val) {
456 $total += CRM_Price_BAO_LineItem::getLineTotal($val, 'civicrm_participant');
457 }
458 $lineItemTotal = $total;
459 }
460 else {
461 $lineItemTotal = CRM_Price_BAO_LineItem::getLineTotal($entityId, 'civicrm_participant');
462 }
bc2eeabb
PJ
463 }
464 $sqlFtTotalAmt = "
465SELECT SUM(ft.total_amount)
0f602e3f 466FROM civicrm_financial_trxn ft
bc2eeabb
PJ
467 LEFT JOIN civicrm_entity_financial_trxn eft ON (ft.id = eft.financial_trxn_id AND eft.entity_table = 'civicrm_contribution')
468 LEFT JOIN civicrm_contribution c ON (eft.entity_id = c.id)
469 LEFT JOIN civicrm_participant_payment pp ON (pp.contribution_id = c.id)
8cf01b22 470WHERE pp.participant_id = {$entityId} AND ft.to_financial_account_id != {$toFinancialAccount} AND ft.to_financial_account_id != {$feeFinancialAccount}
1010c4e1 471 AND ft.status_id IN ({$statusId}, {$refundStatusId})
0f602e3f
PJ
472";
473 $ftTotalAmt = CRM_Core_DAO::singleValueQuery($sqlFtTotalAmt);
bc2eeabb
PJ
474 $value = 0;
475 if ($ftTotalAmt) {
476 $value = $paymentVal = $lineItemTotal - $ftTotalAmt;
477 }
29c61b58
PJ
478 if ($returnType) {
479 $value = array();
480 if ($paymentVal < 0) {
481 $value['refund_due'] = $paymentVal;
482 }
483 elseif ($paymentVal > 0) {
484 $value['amount_owed'] = $paymentVal;
485 }
bc2eeabb
PJ
486 elseif ($lineItemTotal == $ftTotalAmt) {
487 $value['full_paid'] = $ftTotalAmt;
488 }
0f602e3f
PJ
489 }
490 }
491 }
492 return $value;
493 }
0d8afee2 494}