Merge pull request #5554 from eileenmcnaughton/CRM-16255
[civicrm-core.git] / CRM / Report / Form / Event / IncomeCountSummary.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
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. |
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 along with this program; if not, contact CiviCRM LLC |
21 | at info[AT]civicrm[DOT]org. If you have questions about the |
22 | GNU Affero General Public License or the licensing of CiviCRM, |
23 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
24 +--------------------------------------------------------------------+
d25dd0ee 25 */
6a488035
TO
26
27/**
28 *
29 * @package CRM
e7112fa7 30 * @copyright CiviCRM LLC (c) 2004-2015
6a488035
TO
31 * $Id$
32 *
33 */
34class CRM_Report_Form_Event_IncomeCountSummary extends CRM_Report_Form_Event {
35
36 protected $_summary = NULL;
37
38 protected $_charts = array(
39 '' => 'Tabular',
40 'barChart' => 'Bar Chart',
41 'pieChart' => 'Pie Chart',
42 );
43
44 protected $_add2groupSupported = FALSE;
45
46 protected $_customGroupExtends = array(
21dfd5f5 47 'Event',
feebd4ea 48 );
6a488035
TO
49
50 public $_drilldownReport = array('event/participantlist' => 'Link to Detail Report');
51
74cf4551 52 /**
74cf4551
EM
53 */
54 /**
74cf4551 55 */
00be9182 56 public function __construct() {
6a488035
TO
57
58 $this->_columns = array(
feebd4ea 59 'civicrm_event' => array(
6a488035 60 'dao' => 'CRM_Event_DAO_Event',
feebd4ea
EM
61 'fields' => array(
62 'title' => array(
63 'title' => ts('Event'),
6a488035
TO
64 'required' => TRUE,
65 ),
66 'id' => array(
1d0d5aff 67 'title' => 'Event ID',
6a488035
TO
68 'no_display' => TRUE,
69 'required' => TRUE,
70 ),
feebd4ea
EM
71 'event_type_id' => array(
72 'title' => ts('Event Type'),
73 ),
74 'fee_label' => array(
21dfd5f5 75 'title' => ts('Fee Label'),
6a488035 76 ),
feebd4ea
EM
77 'event_start_date' => array(
78 'title' => ts('Event Start Date'),
6a488035 79 ),
feebd4ea
EM
80 'event_end_date' => array(
81 'title' => ts('Event End Date'),
6a488035 82 ),
feebd4ea
EM
83 'max_participants' => array(
84 'title' => ts('Capacity'),
6a488035
TO
85 'type' => CRM_Utils_Type::T_INT,
86 ),
87 ),
feebd4ea 88 'filters' => array(
2107cde9
CW
89 'id' => array(
90 'title' => ts('Event'),
91 'operatorType' => CRM_Report_Form::OP_ENTITYREF,
92 'type' => CRM_Utils_Type::T_INT,
93 'attributes' => array('select' => array('minimumInputLength' => 0)),
6a488035
TO
94 ),
95 'event_type_id' => array(
96 'name' => 'event_type_id',
97 'title' => ts('Event Type'),
98 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
99 'options' => CRM_Core_OptionGroup::values('event_type'),
100 ),
feebd4ea
EM
101 'event_start_date' => array(
102 'title' => ts('Event Start Date'),
6a488035
TO
103 'operatorType' => CRM_Report_Form::OP_DATE,
104 ),
feebd4ea
EM
105 'event_end_date' => array(
106 'title' => ts('Event End Date'),
6a488035
TO
107 'operatorType' => CRM_Report_Form::OP_DATE,
108 ),
109 ),
110 ),
feebd4ea 111 'civicrm_line_item' => array(
6a488035 112 'dao' => 'CRM_Price_DAO_LineItem',
feebd4ea 113 'fields' => array(
6a488035
TO
114 'participant_count' => array(
115 'title' => ts('Participants'),
116 'default' => TRUE,
9d72cede
EM
117 'statistics' => array(
118 'count' => ts('Participants'),
119 ),
6a488035
TO
120 ),
121 'line_total' => array(
122 'title' => ts('Income Statistics'),
123 'type' => CRM_Utils_Type::T_MONEY,
124 'default' => TRUE,
9d72cede
EM
125 'statistics' => array(
126 'sum' => ts('Income'),
127 'avg' => ts('Average'),
128 ),
6a488035
TO
129 ),
130 ),
131 ),
feebd4ea 132 'civicrm_participant' => array(
6a488035 133 'dao' => 'CRM_Event_DAO_Participant',
feebd4ea
EM
134 'filters' => array(
135 'sid' => array(
136 'name' => 'status_id',
6a488035
TO
137 'title' => ts('Participant Status'),
138 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
139 'options' => CRM_Event_PseudoConstant::participantStatus(),
140 ),
141 'rid' => array(
142 'name' => 'role_id',
143 'title' => ts('Participant Role'),
144 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
145 'options' => CRM_Event_PseudoConstant::participantRole(),
146 ),
feebd4ea
EM
147 'participant_register_date' => array(
148 'title' => ts('Registration Date'),
6a488035
TO
149 'operatorType' => CRM_Report_Form::OP_DATE,
150 ),
151 ),
152 ),
153 );
154 parent::__construct();
155 }
156
00be9182 157 public function preProcess() {
6a488035
TO
158 parent::preProcess();
159 }
160
00be9182 161 public function select() {
6a488035
TO
162 $select = array();
163 foreach ($this->_columns as $tableName => $table) {
164 if (array_key_exists('fields', $table)) {
165 foreach ($table['fields'] as $fieldName => $field) {
9d72cede
EM
166 if (!empty($field['required']) ||
167 !empty($this->_params['fields'][$fieldName])
168 ) {
a7488080 169 if (!empty($field['statistics'])) {
6a488035
TO
170 foreach ($field['statistics'] as $stat => $label) {
171 switch (strtolower($stat)) {
172 case 'count':
173 $select[] = "SUM({$field['dbAlias']}) as {$tableName}_{$fieldName}_{$stat}";
174 $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['type'] = CRM_Utils_Type::T_INT;
175 $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
176 $this->_statFields[] = "{$tableName}_{$fieldName}_{$stat}";
177 break;
178
179 case 'sum':
180 $select[] = "SUM({$field['dbAlias']}) as {$tableName}_{$fieldName}_{$stat}";
181 $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['type'] = CRM_Utils_Type::T_MONEY;
182
183 $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
184 $this->_statFields[] = "{$tableName}_{$fieldName}_{$stat}";
185 break;
186
187 case 'avg':
188 $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
189
190 $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['type'] = CRM_Utils_Type::T_MONEY;
191 $this->_statFields[] = "{$tableName}_{$fieldName}_{$stat}";
192 }
193 }
194 }
195 else {
196 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
197 $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field);
198 $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = $field['title'];
199 }
200 }
201 }
202 }
203 }
204
205 $this->_select = "SELECT " . implode(', ', $select);
206 }
207
00be9182 208 public function from() {
2f4c2f5d 209 $this->_from = "
6a488035 210 FROM civicrm_event {$this->_aliases['civicrm_event']}
2f4c2f5d 211 LEFT JOIN civicrm_participant {$this->_aliases['civicrm_participant']}
212 ON {$this->_aliases['civicrm_event']}.id = {$this->_aliases['civicrm_participant']}.event_id AND
213 {$this->_aliases['civicrm_participant']}.is_test = 0
6a488035 214 LEFT JOIN civicrm_line_item {$this->_aliases['civicrm_line_item']}
2f4c2f5d 215 ON {$this->_aliases['civicrm_participant']}.id ={$this->_aliases['civicrm_line_item']}.entity_id AND
6a488035
TO
216 {$this->_aliases['civicrm_line_item']}.entity_table = 'civicrm_participant' ";
217 }
218
00be9182 219 public function where() {
6a488035
TO
220 $clauses = array();
221 $this->_participantWhere = "";
222 foreach ($this->_columns as $tableName => $table) {
223 if (array_key_exists('filters', $table)) {
224 foreach ($table['filters'] as $fieldName => $field) {
225 $clause = NULL;
226 if (CRM_Utils_Array::value('type', $field) & CRM_Utils_Type::T_DATE) {
227 $relative = CRM_Utils_Array::value("{$fieldName}_relative", $this->_params);
feebd4ea
EM
228 $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params);
229 $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params);
6a488035
TO
230
231 if ($relative || $from || $to) {
232 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
233 }
234 }
235 else {
236 $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params);
237 if ($op) {
238 $clause = $this->whereClause($field,
239 $op,
240 CRM_Utils_Array::value("{$fieldName}_value", $this->_params),
241 CRM_Utils_Array::value("{$fieldName}_min", $this->_params),
242 CRM_Utils_Array::value("{$fieldName}_max", $this->_params)
243 );
244 }
245 }
246 if (!empty($this->_params['id_value'])) {
2107cde9 247 $this->_participantWhere = " AND civicrm_participant.event_id IN ( {$this->_params['id_value']} ) ";
6a488035
TO
248 }
249
250 if (!empty($clause)) {
251 $clauses[] = $clause;
252 }
253 }
254 }
255 }
99f13d8d 256 $clauses[] = "{$this->_aliases['civicrm_event']}.is_template = 0";
6a488035
TO
257 $this->_where = "WHERE " . implode(' AND ', $clauses);
258 }
259
74cf4551
EM
260 /**
261 * @param $rows
262 *
263 * @return array
264 */
00be9182 265 public function statistics(&$rows) {
6a488035
TO
266 $statistics = parent::statistics($rows);
267 $select = "
268 SELECT SUM( {$this->_aliases['civicrm_line_item']}.participant_count ) as count,
269 SUM( {$this->_aliases['civicrm_line_item']}.line_total ) as amount";
270
271 $sql = "{$select} {$this->_from} {$this->_where}";
272
273 $dao = CRM_Core_DAO::executeQuery($sql);
274
275 if ($dao->fetch()) {
1d0d5aff 276 $avg = 0;
6a488035
TO
277 if ($dao->count && $dao->amount) {
278 $avg = $dao->amount / $dao->count;
279 }
280 $statistics['counts']['count'] = array(
281 'value' => $dao->count,
282 'title' => 'Total Participants',
283 'type' => CRM_Utils_Type::T_INT,
284 );
285 $statistics['counts']['amount'] = array(
286 'value' => $dao->amount,
287 'title' => 'Total Income',
288 'type' => CRM_Utils_Type::T_MONEY,
289 );
1d0d5aff 290 $statistics['counts']['avg'] = array(
6a488035
TO
291 'value' => $avg,
292 'title' => 'Average',
293 'type' => CRM_Utils_Type::T_MONEY,
294 );
295 }
296 return $statistics;
297 }
298
00be9182 299 public function groupBy() {
6a488035
TO
300 $this->assign('chartSupported', TRUE);
301 $this->_rollup = " WITH ROLLUP";
302 $this->_groupBy = " GROUP BY {$this->_aliases['civicrm_event']}.id {$this->_rollup}";
303 }
304
00be9182 305 public function postProcess() {
6a488035
TO
306
307 $this->beginPostProcess();
308
309 $sql = $this->buildQuery(TRUE);
310
311 $dao = CRM_Core_DAO::executeQuery($sql);
312
313 //set pager before execution of query in function participantInfo()
314 $this->setPager();
315
316 $rows = $graphRows = array();
317 $count = 0;
318
319 while ($dao->fetch()) {
320 $row = array();
321 foreach ($this->_columnHeaders as $key => $value) {
9d72cede
EM
322 if (($key == 'civicrm_event_start_date') ||
323 ($key == 'civicrm_event_end_date')
324 ) {
6a488035
TO
325 //get event start date and end date in custom datetime format
326 $row[$key] = CRM_Utils_Date::customFormat($dao->$key);
327 }
328 elseif ($key == 'civicrm_participant_fee_amount_avg') {
9d72cede
EM
329 if ($dao->civicrm_participant_fee_amount_sum &&
330 $dao->civicrm_line_item_participant_count_count
331 ) {
332 $row[$key] = $dao->civicrm_participant_fee_amount_sum /
333 $dao->civicrm_line_item_participant_count_count;
6a488035
TO
334 }
335 }
336 elseif ($key == 'civicrm_line_item_line_total_avg') {
9d72cede
EM
337 if ($dao->civicrm_line_item_line_total_sum &&
338 $dao->civicrm_line_item_participant_count_count
339 ) {
340 $row[$key] = $dao->civicrm_line_item_line_total_sum /
341 $dao->civicrm_line_item_participant_count_count;
6a488035
TO
342 }
343 }
344 else {
345 if (isset($dao->$key)) {
346 $row[$key] = $dao->$key;
347 }
348 }
349 }
350 $rows[] = $row;
351 }
352
353 // do not call pager here
354 $this->formatDisplay($rows, FALSE);
355 unset($this->_columnHeaders['civicrm_event_id']);
356
357 $this->doTemplateAssignment($rows);
358
359 $this->endPostProcess($rows);
360 }
361
74cf4551
EM
362 /**
363 * @param $rows
364 */
00be9182 365 public function buildChart(&$rows) {
6a488035
TO
366
367 $this->_interval = 'events';
368 $countEvent = NULL;
a7488080 369 if (!empty($this->_params['charts'])) {
6a488035
TO
370 foreach ($rows as $key => $value) {
371 if ($value['civicrm_event_id']) {
372 $graphRows['totalParticipants'][] = ($rows[$key]['civicrm_line_item_participant_count_count']);
795492f3 373 $graphRows[$this->_interval][] = substr($rows[$key]['civicrm_event_title'], 0, 12) . "..(" .
9d72cede 374 $rows[$key]['civicrm_event_id'] . ") ";
6a488035
TO
375 $graphRows['value'][] = ($rows[$key]['civicrm_line_item_participant_count_count']);
376 }
377 }
378
379 if (($rows[$key]['civicrm_line_item_participant_count_count']) == 0) {
380 $countEvent = count($rows);
381 }
382
383 if ((!empty($rows)) && $countEvent != 1) {
384 $chartInfo = array(
385 'legend' => 'Participants Summary',
386 'xname' => 'Event',
387 'yname' => 'Total Participants',
388 );
389 if (!empty($graphRows)) {
390 foreach ($graphRows[$this->_interval] as $key => $val) {
391 $graph[$val] = $graphRows['value'][$key];
392 }
393 $chartInfo['values'] = $graph;
394 $chartInfo['tip'] = 'Participants : #val#';
395 $chartInfo['xLabelAngle'] = 20;
396
397 // build the chart.
398 CRM_Utils_OpenFlashChart::buildChart($chartInfo, $this->_params['charts']);
399 }
400 }
401 }
402 }
403
74cf4551 404 /**
ced9bfed
EM
405 * Alter display of rows.
406 *
407 * Iterate through the rows retrieved via SQL and make changes for display purposes,
408 * such as rendering contacts as links.
409 *
410 * @param array $rows
411 * Rows generated by SQL, with an array for each row.
74cf4551 412 */
00be9182 413 public function alterDisplay(&$rows) {
6a488035
TO
414
415 if (is_array($rows)) {
416 $eventType = CRM_Core_OptionGroup::values('event_type');
417
418 foreach ($rows as $rowNum => $row) {
419 if (array_key_exists('civicrm_event_title', $row)) {
420 if ($value = $row['civicrm_event_id']) {
421 CRM_Event_PseudoConstant::event($value, FALSE);
422 $url = CRM_Report_Utils_Report::getNextUrl('event/participantlist',
423 'reset=1&force=1&event_id_op=eq&event_id_value=' . $value,
424 $this->_absoluteUrl, $this->_id, $this->_drilldownReport
425 );
426 $rows[$rowNum]['civicrm_event_title_link'] = $url;
427 $rows[$rowNum]['civicrm_event_title_hover'] = ts("View Event Participants For this Event");
428 }
429 }
430
431 //handle event type
432 if (array_key_exists('civicrm_event_event_type_id', $row)) {
433 if ($value = $row['civicrm_event_event_type_id']) {
434 $rows[$rowNum]['civicrm_event_event_type_id'] = $eventType[$value];
435 }
436 }
437 }
438 }
439 }
96025800 440
6a488035 441}