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