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