-
-
Notifications
You must be signed in to change notification settings - Fork 825
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
[REF] extract calculation of basic stats #13608
Merged
colemanw
merged 1 commit into
civicrm:master
from
eileenmcnaughton:cont_summary_extract1
Feb 16, 2019
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5089,44 +5089,27 @@ public function setSkipPermission($val) { | |
*/ | ||
public function summaryContribution($context = NULL) { | ||
list($innerselect, $from, $where, $having) = $this->query(TRUE); | ||
if ($this->_permissionWhereClause) { | ||
$where .= " AND " . $this->_permissionWhereClause; | ||
} | ||
if ($context == 'search') { | ||
$where .= " AND contact_a.is_deleted = 0 "; | ||
} | ||
|
||
// hack $select | ||
$select = " | ||
SELECT COUNT( conts.total_amount ) as total_count, | ||
SUM( conts.total_amount ) as total_amount, | ||
AVG( conts.total_amount ) as total_avg, | ||
conts.currency as currency"; | ||
if ($this->_permissionWhereClause) { | ||
$where .= " AND " . $this->_permissionWhereClause; | ||
} | ||
if ($context == 'search') { | ||
$where .= " AND contact_a.is_deleted = 0 "; | ||
} | ||
|
||
$query = $this->appendFinancialTypeWhereAndFromToQueryStrings($where, $from); | ||
$this->appendFinancialTypeWhereAndFromToQueryStrings($where, $from); | ||
|
||
// make sure contribution is completed - CRM-4989 | ||
$completedWhere = $where . " AND civicrm_contribution.contribution_status_id = 1 "; | ||
$summary = ['total' => []]; | ||
$this->addBasicStatsToSummary($summary, $where, $from); | ||
|
||
$summary = array(); | ||
$summary['total'] = array(); | ||
$summary['total']['count'] = $summary['total']['amount'] = $summary['total']['avg'] = "n/a"; | ||
$innerQuery = "SELECT civicrm_contribution.total_amount, COUNT(civicrm_contribution.total_amount) as civicrm_contribution_total_amount_count, | ||
civicrm_contribution.currency $from $completedWhere"; | ||
$query = "$select FROM ( | ||
$innerQuery GROUP BY civicrm_contribution.id | ||
) as conts | ||
GROUP BY currency"; | ||
|
||
$dao = CRM_Core_DAO::executeQuery($query); | ||
|
||
$summary['total']['count'] = 0; | ||
$summary['total']['amount'] = $summary['total']['avg'] = array(); | ||
while ($dao->fetch()) { | ||
$summary['total']['count'] += $dao->total_count; | ||
$summary['total']['amount'][] = CRM_Utils_Money::format($dao->total_amount, $dao->currency); | ||
$summary['total']['avg'][] = CRM_Utils_Money::format($dao->total_avg, $dao->currency); | ||
} | ||
civicrm_contribution.currency $from $where AND civicrm_contribution.contribution_status_id = 1 "; | ||
|
||
$orderBy = 'ORDER BY civicrm_contribution_total_amount_count DESC'; | ||
$groupBy = 'GROUP BY currency, civicrm_contribution.total_amount'; | ||
|
@@ -5139,6 +5122,8 @@ public function summaryContribution($context = NULL) { | |
|
||
$summary['total']['mode'] = CRM_Contribute_BAO_Contribution::computeStats('mode', $modeSQL); | ||
|
||
// make sure contribution is completed - CRM-4989 | ||
$completedWhere = $where . " AND civicrm_contribution.contribution_status_id = 1 "; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for now - but this variable's days are numbered..... |
||
$medianSQL = "{$from} {$completedWhere}"; | ||
$summary['total']['median'] = CRM_Contribute_BAO_Contribution::computeStats('median', $medianSQL, 'civicrm_contribution'); | ||
$summary['total']['currencyCount'] = count($summary['total']['median']); | ||
|
@@ -6644,4 +6629,42 @@ public function getSelect() { | |
return $select; | ||
} | ||
|
||
/** | ||
* Add basic statistics to the summary. | ||
* | ||
* @param array $summary | ||
* @param string $where | ||
* @param string $from | ||
* | ||
* @return array | ||
*/ | ||
protected function addBasicStatsToSummary(&$summary, $where, $from) { | ||
$summary['total']['count'] = $summary['total']['amount'] = $summary['total']['avg'] = "n/a"; | ||
|
||
$query = " | ||
SELECT COUNT( conts.total_amount ) as total_count, | ||
SUM( conts.total_amount ) as total_amount, | ||
AVG( conts.total_amount ) as total_avg, | ||
conts.currency as currency | ||
FROM ( | ||
SELECT civicrm_contribution.total_amount, COUNT(civicrm_contribution.total_amount) as civicrm_contribution_total_amount_count, | ||
civicrm_contribution.currency | ||
$from | ||
$where AND civicrm_contribution.contribution_status_id = 1 | ||
GROUP BY civicrm_contribution.id | ||
) as conts | ||
GROUP BY currency"; | ||
|
||
$dao = CRM_Core_DAO::executeQuery($query); | ||
|
||
$summary['total']['count'] = 0; | ||
$summary['total']['amount'] = $summary['total']['avg'] = []; | ||
while ($dao->fetch()) { | ||
$summary['total']['count'] += $dao->total_count; | ||
$summary['total']['amount'][] = CRM_Utils_Money::format($dao->total_amount, $dao->currency); | ||
$summary['total']['avg'][] = CRM_Utils_Money::format($dao->total_avg, $dao->currency); | ||
} | ||
return $summary; | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've been moving this clause into the relevant queries - there just isn't enough gained by sharing bits of queries & it makes it more complicated