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