Merge pull request #3814 from magnolia61/CRM-13595-Event-ID-in-Participant-Export
[civicrm-core.git] / CRM / Report / Form / Event / Income.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 and the CiviCRM Licensing Exception. |
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 and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
27*/
28
29/**
30 *
31 * @package CRM
06b69b18 32 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
33 * $Id$
34 *
35 */
36class CRM_Report_Form_Event_Income extends CRM_Report_Form_Event {
37 CONST ROW_COUNT_LIMIT = 2;
38
39 protected $_summary = NULL;
40 protected $_noFields = TRUE;
a5611c8e
DL
41
42 protected $_add2groupSupported = FALSE;
43
74cf4551
EM
44 /**
45 *
46 */
47 /**
48 *
49 */
6a488035
TO
50 function __construct() {
51
52 $this->_columns = array(
53 'civicrm_event' =>
54 array(
55 'dao' => 'CRM_Event_DAO_Event',
56 'filters' =>
57 array(
58 'id' =>
59 array('title' => ts('Event Title'),
60 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
61 'type' => CRM_Utils_Type::T_INT,
62 'options' => $this->getEventFilterOptions(),
63 ),
64 ),
65 ),
66 );
a5611c8e 67
6a488035
TO
68 parent::__construct();
69 }
70
71 function preProcess() {
72 $this->_csvSupported = FALSE;
73 parent::preProcess();
74 }
75
74cf4551
EM
76 /**
77 * @param $eventIDs
78 */
6a488035
TO
79 function buildEventReport($eventIDs) {
80
81 $this->assign('events', $eventIDs);
82
83 $eventID = implode(',', $eventIDs);
84
85 $participantStatus = CRM_Event_PseudoConstant::participantStatus(NULL, "is_counted = 1");
86 $participantRole = CRM_Event_PseudoConstant::participantRole();
87 $paymentInstruments = CRM_Contribute_PseudoConstant::paymentInstrument();
88
89 $rows = $eventSummary = $roleRows = $statusRows = $instrumentRows = $count = array();
90
91 $optionGroupDAO = new CRM_Core_DAO_OptionGroup();
92 $optionGroupDAO->name = 'event_type';
93 $optionGroupId = NULL;
94 if ($optionGroupDAO->find(TRUE)) {
95 $optionGroupId = $optionGroupDAO->id;
96 }
97 //show the income of active participant status (Counted = filter = 1)
98 $activeParticipantStatusIDArray = $activeParticipantStatusLabelArray = array();
99 foreach ($participantStatus as $id => $label) {
100 $activeParticipantStatusIDArray[] = $id;
101 $activeParticipantStatusLabelArray[] = $label;
102 }
103 $activeParticipantStatus = implode(',', $activeParticipantStatusIDArray);
104 $activeparticipnatStutusLabel = implode(', ', $activeParticipantStatusLabelArray);
105 $activeParticipantClause = " AND civicrm_participant.status_id IN ( $activeParticipantStatus ) ";
106
107 $sql = "
108 SELECT civicrm_event.id as event_id,
109 civicrm_event.title as event_title,
a5611c8e 110 civicrm_event.max_participants as max_participants,
6a488035 111 civicrm_event.start_date as start_date,
a5611c8e
DL
112 civicrm_event.end_date as end_date,
113 civicrm_option_value.label as event_type,
33207fc5 114 civicrm_participant.fee_currency as currency,
6a488035
TO
115 SUM(civicrm_participant.fee_amount) as total,
116 COUNT(civicrm_participant.id) as participant
117
118 FROM civicrm_event
a5611c8e 119 LEFT JOIN civicrm_option_value
6a488035
TO
120 ON ( civicrm_event.event_type_id = civicrm_option_value.value AND
121 civicrm_option_value.option_group_id = {$optionGroupId} )
a5611c8e 122 LEFT JOIN civicrm_participant ON ( civicrm_event.id = civicrm_participant.event_id
6a488035
TO
123 {$activeParticipantClause} AND civicrm_participant.is_test = 0 )
124
a5611c8e
DL
125 WHERE civicrm_event.id IN( {$eventID})
126
6a488035
TO
127 GROUP BY civicrm_event.id
128 ";
33207fc5 129
6a488035 130 $eventDAO = CRM_Core_DAO::executeQuery($sql);
7baf6261 131 $currency = array();
6a488035
TO
132 while ($eventDAO->fetch()) {
133 $eventSummary[$eventDAO->event_id]['Title'] = $eventDAO->event_title;
134 $eventSummary[$eventDAO->event_id]['Max Participants'] = $eventDAO->max_participants;
135 $eventSummary[$eventDAO->event_id]['Start Date'] = CRM_Utils_Date::customFormat($eventDAO->start_date);
136 $eventSummary[$eventDAO->event_id]['End Date'] = CRM_Utils_Date::customFormat($eventDAO->end_date);
137 $eventSummary[$eventDAO->event_id]['Event Type'] = $eventDAO->event_type;
33207fc5 138 $eventSummary[$eventDAO->event_id]['Event Income'] = CRM_Utils_Money::format($eventDAO->total, $eventDAO->currency);
6a488035 139 $eventSummary[$eventDAO->event_id]['Registered Participant'] = "{$eventDAO->participant} ({$activeparticipnatStutusLabel})";
7baf6261 140 $currency[$eventDAO->event_id] = $eventDAO->currency;
6a488035
TO
141 }
142 $this->assign_by_ref('summary', $eventSummary);
143
144 //Total Participant Registerd for the Event
145 $pariticipantCount = "
146 SELECT COUNT(civicrm_participant.id ) as count, civicrm_participant.event_id as event_id
147
148 FROM civicrm_participant
149
a5611c8e
DL
150 WHERE civicrm_participant.event_id IN( {$eventID}) AND
151 civicrm_participant.is_test = 0
6a488035
TO
152 {$activeParticipantClause}
153 GROUP BY civicrm_participant.event_id
154 ";
155
156 $counteDAO = CRM_Core_DAO::executeQuery($pariticipantCount);
157 while ($counteDAO->fetch()) {
158 $count[$counteDAO->event_id] = $counteDAO->count;
159 }
160
161 //Count the Participant by Role ID for Event
162 $role = "
a5611c8e
DL
163 SELECT civicrm_participant.role_id as ROLEID,
164 COUNT( civicrm_participant.id ) as participant,
6a488035 165 SUM(civicrm_participant.fee_amount) as amount,
7baf6261 166 civicrm_participant.event_id as event_id,
167 civicrm_participant.fee_currency as currency
6a488035
TO
168 FROM civicrm_participant
169
170 WHERE civicrm_participant.event_id IN ( {$eventID}) AND
a5611c8e 171 civicrm_participant.is_test = 0
6a488035
TO
172 {$activeParticipantClause}
173 GROUP BY civicrm_participant.role_id, civicrm_participant.event_id
174 ";
175
176 $roleDAO = CRM_Core_DAO::executeQuery($role);
177
178 while ($roleDAO->fetch()) {
179 // fix for multiple role, CRM-6507
180 $roles = explode(CRM_Core_DAO::VALUE_SEPARATOR, $roleDAO->ROLEID);
181 foreach ($roles as $roleId) {
182 if (!isset($roleRows[$roleDAO->event_id][$participantRole[$roleId]])) {
183 $roleRows[$roleDAO->event_id][$participantRole[$roleId]]['total'] = 0;
184 $roleRows[$roleDAO->event_id][$participantRole[$roleId]]['round'] = 0;
185 $roleRows[$roleDAO->event_id][$participantRole[$roleId]]['amount'] = 0;
186 }
187 $roleRows[$roleDAO->event_id][$participantRole[$roleId]]['total'] += $roleDAO->participant;
188 $roleRows[$roleDAO->event_id][$participantRole[$roleId]]['amount'] += $roleDAO->amount;
189 }
7baf6261 190 $roleRows[$roleDAO->event_id][$participantRole[$roleId]]['amount'] = CRM_Utils_Money::format($roleRows[$roleDAO->event_id][$participantRole[$roleId]]['amount'], $currency[$roleDAO->event_id]);
6a488035
TO
191 }
192
193 foreach ($roleRows as $eventId => $roleInfo) {
194 foreach ($participantRole as $roleName) {
195 if (isset($roleInfo[$roleName])) {
196 $roleRows[$eventId][$roleName]['round'] = round(($roleRows[$eventId][$roleName]['total'] / $count[$eventId]) * 100, 2);
197 }
198 }
199 }
2f4c2f5d 200
6a488035
TO
201 $rows['Role'] = $roleRows;
202
203 //Count the Participant by status ID for Event
204 $status = "
a5611c8e
DL
205 SELECT civicrm_participant.status_id as STATUSID,
206 COUNT( civicrm_participant.id ) as participant,
6a488035
TO
207 SUM(civicrm_participant.fee_amount) as amount,
208 civicrm_participant.event_id as event_id
209
210 FROM civicrm_participant
211
212 WHERE civicrm_participant.event_id IN ({$eventID}) AND
a5611c8e 213 civicrm_participant.is_test = 0
6a488035
TO
214 {$activeParticipantClause}
215 GROUP BY civicrm_participant.status_id, civicrm_participant.event_id
216 ";
217
218 $statusDAO = CRM_Core_DAO::executeQuery($status);
219
220 while ($statusDAO->fetch()) {
221 $statusRows[$statusDAO->event_id][$participantStatus[$statusDAO->STATUSID]]['total'] = $statusDAO->participant;
222 $statusRows[$statusDAO->event_id][$participantStatus[$statusDAO->STATUSID]]['round'] = round(($statusDAO->participant / $count[$statusDAO->event_id]) * 100, 2);
7baf6261 223 $statusRows[$statusDAO->event_id][$participantStatus[$statusDAO->STATUSID]]['amount'] = CRM_Utils_Money::format($statusDAO->amount, $currency[$statusDAO->event_id]);
6a488035
TO
224 }
225
226 $rows['Status'] = $statusRows;
227
228 //Count the Participant by payment instrument ID for Event
229 //e.g. Credit Card, Check,Cash etc
230 $paymentInstrument = "
a5611c8e 231 SELECT c.payment_instrument_id as INSTRUMENT,
9341d544
DL
232 COUNT( civicrm_participant.id ) as participant,
233 SUM( civicrm_participant.fee_amount ) as amount,
6a488035
TO
234 civicrm_participant.event_id as event_id
235
9341d544
DL
236 FROM civicrm_participant,
237 civicrm_participant_payment pp
6a488035
TO
238 LEFT JOIN civicrm_contribution c ON ( pp.contribution_id = c.id)
239
9341d544
DL
240 WHERE civicrm_participant.event_id IN ( {$eventID} )
241 AND civicrm_participant.is_test = 0
6a488035 242 {$activeParticipantClause}
9341d544
DL
243 AND ((pp.participant_id = civicrm_participant.id )
244 OR (pp.participant_id = civicrm_participant.registered_by_id ))
6a488035
TO
245 GROUP BY c.payment_instrument_id, civicrm_participant.event_id
246 ";
247
248 $instrumentDAO = CRM_Core_DAO::executeQuery($paymentInstrument);
249
250 while ($instrumentDAO->fetch()) {
251 //allow only if instrument is present in contribution table
252 if ($instrumentDAO->INSTRUMENT) {
253 $instrumentRows[$instrumentDAO->event_id][$paymentInstruments[$instrumentDAO->INSTRUMENT]]['total'] = $instrumentDAO->participant;
254 $instrumentRows[$instrumentDAO->event_id][$paymentInstruments[$instrumentDAO->INSTRUMENT]]['round'] = round(($instrumentDAO->participant / $count[$instrumentDAO->event_id]) * 100, 2);
9341d544 255 $instrumentRows[$instrumentDAO->event_id][$paymentInstruments[$instrumentDAO->INSTRUMENT]]['amount'] = CRM_Utils_Money::format($instrumentDAO->amount, $currency[$instrumentDAO->event_id]);
6a488035
TO
256 }
257 }
258 $rows['Payment Method'] = $instrumentRows;
259
260 $this->assign_by_ref('rows', $rows);
261 if (!$this->_setVariable) {
262 $this->_params['id_value'] = NULL;
263 }
264 $this->assign('statistics', $this->statistics($eventIDs));
265 }
266
74cf4551
EM
267 /**
268 * @param $eventIDs
269 *
270 * @return array
271 */
6a488035
TO
272 function statistics(&$eventIDs) {
273 $statistics = array();
274 $count = count($eventIDs);
275 $this->countStat($statistics, $count);
276 if ($this->_setVariable) {
277 $this->filterStat($statistics);
278 }
279
280 return $statistics;
281 }
282
74cf4551
EM
283 /**
284 * @param int $rowCount
285 */
6a488035
TO
286 function limit($rowCount = self::ROW_COUNT_LIMIT) {
287 parent::limit($rowCount);
288
289 //modify limit
290 $pageId = $this->get(CRM_Utils_Pager::PAGE_ID);
291
292 //if pageId is greator than last page then display last page.
293 if ((($pageId * self::ROW_COUNT_LIMIT) - 1) > $this->_rowsFound) {
294 $pageId = ceil((float)$this->_rowsFound / (float)self::ROW_COUNT_LIMIT);
295 $this->set(CRM_Utils_Pager::PAGE_ID, $pageId);
296 }
297 $this->_limit = ($pageId - 1) * self::ROW_COUNT_LIMIT;
298 }
299
74cf4551
EM
300 /**
301 * @param int $rowCount
302 */
a5611c8e 303 function setPager($rowCount = self::ROW_COUNT_LIMIT) {
6a488035
TO
304 $params = array(
305 'total' => $this->_rowsFound,
306 'rowCount' => self::ROW_COUNT_LIMIT,
307 'status' => ts('Records %%StatusMessage%%'),
308 'buttonBottom' => 'PagerBottomButton',
309 'buttonTop' => 'PagerTopButton',
310 'pageID' => $this->get(CRM_Utils_Pager::PAGE_ID),
311 );
312
313 $pager = new CRM_Utils_Pager($params);
314 $this->assign_by_ref('pager', $pager);
315 }
316
317 function postProcess() {
318 $this->beginPostProcess();
319 $this->_setVariable = TRUE;
320
321 $noSelection = FALSE;
322 if (empty($this->_params['id_value'][0])) {
323 $this->_params['id_value'] = array();
324 $this->_setVariable = FALSE;
325
326 $events = CRM_Event_PseudoConstant::event(NULL, NULL,
99f13d8d 327 "is_template = 0"
6a488035
TO
328 );
329 if (empty($events)) {
330 return FALSE;
331 }
332 foreach ($events as $key => $dnt) {
333 $this->_params['id_value'][] = $key;
334 }
335 $noSelection = TRUE;
a5611c8e
DL
336 }
337
6a488035
TO
338 $this->_rowsFound = count($this->_params['id_value']);
339
340 //set pager and limit if output mode is html
341 if ($this->_outputMode == 'html') {
342 $this->limit();
343 $this->setPager();
a5611c8e 344
6a488035
TO
345 $showEvents = array();
346 $count = 0;
347 $numRows = $this->_limit;
348
349 if (CRM_Utils_Array::value('id_op', $this->_params, 'in') == 'in' || $noSelection) {
350 while ($count < self::ROW_COUNT_LIMIT) {
351 if (!isset($this->_params['id_value'][$numRows])) {
352 break;
353 }
a5611c8e 354
6a488035
TO
355 $showEvents[] = $this->_params['id_value'][$numRows];
356 $count++;
357 $numRows++;
358 }
359 } elseif ($this->_params['id_op'] == 'notin') {
360 $events = CRM_Event_PseudoConstant::event(NULL, NULL,
99f13d8d 361 "is_template = 0"
6a488035 362 );
a5611c8e 363
6a488035
TO
364 $showEvents = array_diff(array_keys($events), $this->_params['id_value']);
365 }
366
367 $this->buildEventReport($showEvents);
a5611c8e 368
6a488035
TO
369 }
370 else {
371 $this->buildEventReport($this->_params['id_value']);
372 }
373
374 parent::endPostProcess();
375 }
376}
377