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