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