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