Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize CRM_Core_BAO_FinancialTrxn::getTotalPayment #13187

Merged
merged 1 commit into from
Feb 10, 2019

Conversation

monishdeb
Copy link
Member

Overview

This patch optimizes the getContributionBalance() function to use CRM_Core_BAO_FinancialTrxn::getTotalPayments to fetch the total paid amount.

Before

getContributionBalance() uses codes already present in CRM_Core_BAO_FinancialTrxn::getTotalPayments

After

  1. Generalize CRM_Core_BAO_FinancialTrxn::getTotalPayments consider refund trxns
  2. Cleaned up getContributionBalance()

Comments

This is a followup PR of #13151

ping @eileenmcnaughton @jitendrapurohit @seamuslee001

@civibot
Copy link

civibot bot commented Nov 30, 2018

(Standard links)

@monishdeb
Copy link
Member Author

And I wonder what CRM_Core_BAO_ActionScheduleTest.testRepetitionFrequencyUnit test fail has to do with this patch :/

@seamuslee001
Copy link
Contributor

Jenkins re test this please

@jitendrapurohit
Copy link
Contributor

Refund amount is not properly calculated after this change. Balance amount still equals the refund amount after the payment is refunded. To replicate -

  1. Register an event using amount = $1500.
  2. Change selection and use a lower amount eg $1000.
  3. Record a refund amount of $500 from the payment form.
  4. Balance calculated from "getContributionBalance()" is still = "-500". Hence the link to Record Refund still appears for the contribution.

image

Payment already refunded -

image

@monishdeb
Copy link
Member Author

monishdeb commented Dec 5, 2018

@jitendrapurohit thanks for your review. But this PR is meant only to optimize the getTotalPayment() by reusing the code of getTotalPayments() and it is a followup PR of #13151 (see my comment here). Am I right @eileenmcnaughton ?

I am expecting it shouldn't break any scenario nor should fix any issue for partial and refund. After merging this PR can you bring necessary change in #12319 ?

@monishdeb
Copy link
Member Author

Hi @eileenmcnaughton , did you get a chance to review this PR?

@eileenmcnaughton
Copy link
Contributor

@monishdeb I just checked & the issue @jitendrapurohit raises is in fact a regression - here is how the query winds up

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 = 135 AND ft.is_payment = 1 AND ft.status_id IN ('1, 7') 

Note the apostrophes around '1, 7' - that is invalid, I think you need to use string concatenation not CRM_Core_DAO interpolation here - I guess you can validate as commaSeparatedIntegers for good practice - although it's not immediately obvious how someone could get a user value into that field

@colemanw colemanw changed the title Optimize CRM_Core_BAO_FinancialTrxn::getTotalPayment WIP Optimize CRM_Core_BAO_FinancialTrxn::getTotalPayment Jan 22, 2019
@colemanw
Copy link
Member

@monishdeb I'm marking this WIP until you get the chance to make the suggested changes.

@monishdeb
Copy link
Member Author

Hi @colemanw @eileenmcnaughton sorry for the delay. I have updated this PR, please have a look now.

return CRM_Core_DAO::singleValueQuery($sql, $params);
return CRM_Core_DAO::singleValueQuery($sql, [
1 => [$contributionID, 'Integer'],
2 => [implode(', ', $statusIDs), 'CommaSeparatedIntegers'],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the test fail is due to the space here - not sure if we should fix the rule to permit but easy to change here for now

@monishdeb
Copy link
Member Author

Jenkins test this please

@monishdeb monishdeb changed the title WIP Optimize CRM_Core_BAO_FinancialTrxn::getTotalPayment Optimize CRM_Core_BAO_FinancialTrxn::getTotalPayment Feb 4, 2019
@monishdeb
Copy link
Member Author

@eileenmcnaughton can you please merge it?

public static function getTotalPayments($contributionID, $includeRefund = FALSE) {
$statusIDs = [CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed')];

if ($includeRefund) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why have we split out this parameter? There are no places calling it as yet

Copy link
Member Author

@monishdeb monishdeb Feb 6, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eileenmcnaughton this is old followup PR of #13151 where we agreed here to have this function modified to include refunded payments rather than only Completed. The need is to get correct contribution balance that earlier considers only Completed payments and is used in the patch at L4178 at Contribution BAO.

@eileenmcnaughton
Copy link
Contributor

I've run through this in a test & am satisified the result is the same with this change - I'll push my test up separately

@eileenmcnaughton eileenmcnaughton merged commit e13bdce into civicrm:master Feb 10, 2019
eileenmcnaughton added a commit to eileenmcnaughton/civicrm-core that referenced this pull request Feb 10, 2019
eileenmcnaughton added a commit to eileenmcnaughton/civicrm-core that referenced this pull request Feb 11, 2019
seamuslee001 added a commit that referenced this pull request Feb 11, 2019
Add unit test on getContributionBalance fn (#13187)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants