Skip to content

Commit

Permalink
Optimize CRM_Core_BAO_FinancialTrxn::getTotalPayment
Browse files Browse the repository at this point in the history
  • Loading branch information
monishdeb committed Nov 30, 2018
1 parent c08e3ee commit a87fff9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 26 deletions.
22 changes: 5 additions & 17 deletions CRM/Contribute/BAO/Contribution.php
Original file line number Diff line number Diff line change
Expand Up @@ -4178,27 +4178,15 @@ public static function getPaymentInfo($id, $component, $getTrxnInfo = FALSE, $us
* @return float
*/
public static function getContributionBalance($contributionId, $contributionTotal = NULL) {

if ($contributionTotal === NULL) {
$contributionTotal = CRM_Price_BAO_LineItem::getLineTotal($contributionId);
}
$statusId = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed');
$refundStatusId = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Refunded');

$sqlFtTotalAmt = "
SELECT SUM(ft.total_amount)
FROM civicrm_financial_trxn ft
INNER JOIN civicrm_entity_financial_trxn eft ON (ft.id = eft.financial_trxn_id AND eft.entity_table = 'civicrm_contribution' AND eft.entity_id = {$contributionId})
WHERE ft.is_payment = 1
AND ft.status_id IN ({$statusId}, {$refundStatusId})
";

$ftTotalAmt = CRM_Core_DAO::singleValueQuery($sqlFtTotalAmt);
if (!$ftTotalAmt) {
$ftTotalAmt = 0;
}
$currency = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $contributionId, 'currency');
return CRM_Utils_Money::subtractCurrencies($contributionTotal, $ftTotalAmt, $currency);
return CRM_Utils_Money::subtractCurrencies(
$contributionTotal,
CRM_Core_BAO_FinancialTrxn::getTotalPayments($contributionId, TRUE) ?: 0,
CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $contributionId, 'currency')
);
}

/**
Expand Down
18 changes: 9 additions & 9 deletions CRM/Core/BAO/FinancialTrxn.php
Original file line number Diff line number Diff line change
Expand Up @@ -485,22 +485,22 @@ public static function getPartialPaymentWithType($entityId, $entityName = 'parti

/**
* @param int $contributionId
* @param bool $includeRefund
*
* @return string
*/
public static function getTotalPayments($contributionId) {
$statusId = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed');
public static function getTotalPayments($contributionID, $includeRefund = FALSE) {
$statusIDs = [CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed')];
if ($includeRefund) $statusIDs[] = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Refunded');

$sql = "SELECT SUM(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.is_payment = 1 AND ft.status_id = %2";
WHERE eft.entity_id = %1 AND ft.is_payment = 1 AND ft.status_id IN (%2) ";

$params = array(
1 => array($contributionId, 'Integer'),
2 => array($statusId, 'Integer'),
);

return CRM_Core_DAO::singleValueQuery($sql, $params);
return CRM_Core_DAO::singleValueQuery($sql, [
1 => [$contributionID, 'Integer'],
2 => [implode(', ', $statusIDs), 'String'],
]);
}

/**
Expand Down

0 comments on commit a87fff9

Please sign in to comment.