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