Skip to content

Commit

Permalink
Merge pull request #14098 from eileenmcnaughton/event_income
Browse files Browse the repository at this point in the history
[REF] extract buildClause from CRM_Report_Form_Event_Income
  • Loading branch information
eileenmcnaughton authored May 28, 2019
2 parents 3900218 + c442c85 commit e345d7f
Showing 1 changed file with 51 additions and 37 deletions.
88 changes: 51 additions & 37 deletions CRM/Report/Form/Event/Income.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class CRM_Report_Form_Event_Income extends CRM_Report_Form {

protected $_summary = NULL;
protected $_noFields = TRUE;
protected $eventIDs = [];

protected $_add2groupSupported = FALSE;

Expand Down Expand Up @@ -75,47 +76,13 @@ public function preProcess() {
public function buildEventReport($eventIDs) {

$this->assign('events', $eventIDs);

$eventID = implode(',', $eventIDs);

$this->eventIDs = $eventIDs;
$eventID = implode(',', $this->eventIDs);
$participantRole = CRM_Event_PseudoConstant::participantRole();
$paymentInstruments = CRM_Contribute_PseudoConstant::paymentInstrument();

$rows = $eventSummary = $roleRows = $statusRows = $instrumentRows = $count = [];

$optionGroupDAO = new CRM_Core_DAO_OptionGroup();
$optionGroupDAO->name = 'event_type';
$optionGroupId = NULL;
if ($optionGroupDAO->find(TRUE)) {
$optionGroupId = $optionGroupDAO->id;
}

$activeParticipantClause = " AND civicrm_participant.status_id IN ( " . implode(',', array_keys($this->getActiveParticipantStatuses())) . " ) ";
$select = [
"civicrm_event.id as event_id",
"civicrm_event.title as event_title",
"civicrm_event.max_participants as max_participants",
"civicrm_event.start_date as start_date",
"civicrm_event.end_date as end_date",
"civicrm_option_value.label as event_type",
"civicrm_participant.fee_currency as currency",
];

$groupBy = CRM_Contact_BAO_Query::getGroupByFromSelectColumns($select, 'civicrm_event.id');
$sql = "
SELECT " . implode(', ', $select) . ",
SUM(civicrm_participant.fee_amount) as total,
COUNT(civicrm_participant.id) as participant
FROM civicrm_event
LEFT JOIN civicrm_option_value
ON ( civicrm_event.event_type_id = civicrm_option_value.value AND
civicrm_option_value.option_group_id = {$optionGroupId} )
LEFT JOIN civicrm_participant ON ( civicrm_event.id = civicrm_participant.event_id
{$activeParticipantClause} AND civicrm_participant.is_test = 0 )
WHERE civicrm_event.id IN( {$eventID}) {$groupBy}";

$sql = $this->buildQuery();
$eventDAO = $this->executeReportQuery($sql);
$currency = [];
while ($eventDAO->fetch()) {
Expand All @@ -130,6 +97,7 @@ public function buildEventReport($eventIDs) {
}
$this->assign_by_ref('summary', $eventSummary);

$activeParticipantClause = " AND civicrm_participant.status_id IN ( " . implode(',', array_keys($this->getActiveParticipantStatuses())) . " ) ";
//Total Participant Registerd for the Event
$pariticipantCount = "
SELECT COUNT(civicrm_participant.id ) as count, civicrm_participant.event_id as event_id
Expand Down Expand Up @@ -384,4 +352,50 @@ protected function getActiveParticipantStatuses() {
return CRM_Event_PseudoConstant::participantStatus(NULL, "is_counted = 1", "label");
}

/**
* Build main report sql query.
*
* @param bool $applyLimit
*
* @return string
*/
public function buildQuery($applyLimit = FALSE) {
$eventID = implode(',', $this->eventIDs);

$optionGroupDAO = new CRM_Core_DAO_OptionGroup();
$optionGroupDAO->name = 'event_type';
$optionGroupId = NULL;
if ($optionGroupDAO->find(TRUE)) {
$optionGroupId = $optionGroupDAO->id;
}

$activeParticipantClause = " AND civicrm_participant.status_id IN ( " . implode(',', array_keys($this->getActiveParticipantStatuses())) . " ) ";
$select = [
"civicrm_event.id as event_id",
"civicrm_event.title as event_title",
"civicrm_event.max_participants as max_participants",
"civicrm_event.start_date as start_date",
"civicrm_event.end_date as end_date",
"civicrm_option_value.label as event_type",
"civicrm_participant.fee_currency as currency",
];

$groupBy = CRM_Contact_BAO_Query::getGroupByFromSelectColumns($select, 'civicrm_event.id');
$sql = "
SELECT " . implode(', ', $select) . ",
SUM(civicrm_participant.fee_amount) as total,
COUNT(civicrm_participant.id) as participant
FROM civicrm_event
LEFT JOIN civicrm_option_value
ON ( civicrm_event.event_type_id = civicrm_option_value.value AND
civicrm_option_value.option_group_id = {$optionGroupId} )
LEFT JOIN civicrm_participant ON ( civicrm_event.id = civicrm_participant.event_id
{$activeParticipantClause} AND civicrm_participant.is_test = 0 )
WHERE civicrm_event.id IN( {$eventID}) {$groupBy}";

return $sql;
}

}

0 comments on commit e345d7f

Please sign in to comment.