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