5 +--------------------------------------------------------------------+
6 | CiviCRM version 4.3 |
7 +--------------------------------------------------------------------+
8 | Copyright CiviCRM LLC (c) 2004-2013 |
9 +--------------------------------------------------------------------+
10 | This file is a part of CiviCRM. |
12 | CiviCRM is free software; you can copy, modify, and distribute it |
13 | under the terms of the GNU Affero General Public License |
14 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
16 | CiviCRM is distributed in the hope that it will be useful, but |
17 | WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
19 | See the GNU Affero General Public License for more details. |
21 | You should have received a copy of the GNU Affero General Public |
22 | License and the CiviCRM Licensing Exception along |
23 | with this program; if not, contact CiviCRM LLC |
24 | at info[AT]civicrm[DOT]org. If you have questions about the |
25 | GNU Affero General Public License or the licensing of CiviCRM, |
26 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
27 +--------------------------------------------------------------------+
33 * @copyright CiviCRM LLC (c) 2004-2013
37 class CRM_Report_Form_Event_Income
extends CRM_Report_Form_Event
{
38 CONST ROW_COUNT_LIMIT
= 2;
40 protected $_summary = NULL;
41 protected $_noFields = TRUE;
43 protected $_add2groupSupported = FALSE;
45 function __construct() {
47 $this->_columns
= array(
50 'dao' => 'CRM_Event_DAO_Event',
54 array('title' => ts('Event Title'),
55 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
56 'type' => CRM_Utils_Type
::T_INT
,
57 'options' => $this->getEventFilterOptions(),
63 parent
::__construct();
66 function preProcess() {
67 $this->_csvSupported
= FALSE;
71 function buildEventReport($eventIDs) {
73 $this->assign('events', $eventIDs);
75 $eventID = implode(',', $eventIDs);
77 $participantStatus = CRM_Event_PseudoConstant
::participantStatus(NULL, "is_counted = 1");
78 $participantRole = CRM_Event_PseudoConstant
::participantRole();
79 $paymentInstruments = CRM_Contribute_PseudoConstant
::paymentInstrument();
81 $rows = $eventSummary = $roleRows = $statusRows = $instrumentRows = $count = array();
83 $optionGroupDAO = new CRM_Core_DAO_OptionGroup();
84 $optionGroupDAO->name
= 'event_type';
85 $optionGroupId = NULL;
86 if ($optionGroupDAO->find(TRUE)) {
87 $optionGroupId = $optionGroupDAO->id
;
89 //show the income of active participant status (Counted = filter = 1)
90 $activeParticipantStatusIDArray = $activeParticipantStatusLabelArray = array();
91 foreach ($participantStatus as $id => $label) {
92 $activeParticipantStatusIDArray[] = $id;
93 $activeParticipantStatusLabelArray[] = $label;
95 $activeParticipantStatus = implode(',', $activeParticipantStatusIDArray);
96 $activeparticipnatStutusLabel = implode(', ', $activeParticipantStatusLabelArray);
97 $activeParticipantClause = " AND civicrm_participant.status_id IN ( $activeParticipantStatus ) ";
100 SELECT civicrm_event.id as event_id,
101 civicrm_event.title as event_title,
102 civicrm_event.max_participants as max_participants,
103 civicrm_event.start_date as start_date,
104 civicrm_event.end_date as end_date,
105 civicrm_option_value.label as event_type,
106 civicrm_participant.fee_currency as currency,
107 SUM(civicrm_participant.fee_amount) as total,
108 COUNT(civicrm_participant.id) as participant
111 LEFT JOIN civicrm_option_value
112 ON ( civicrm_event.event_type_id = civicrm_option_value.value AND
113 civicrm_option_value.option_group_id = {$optionGroupId} )
114 LEFT JOIN civicrm_participant ON ( civicrm_event.id = civicrm_participant.event_id
115 {$activeParticipantClause} AND civicrm_participant.is_test = 0 )
117 WHERE civicrm_event.id IN( {$eventID})
119 GROUP BY civicrm_event.id
122 $eventDAO = CRM_Core_DAO
::executeQuery($sql);
124 while ($eventDAO->fetch()) {
125 $eventSummary[$eventDAO->event_id
]['Title'] = $eventDAO->event_title
;
126 $eventSummary[$eventDAO->event_id
]['Max Participants'] = $eventDAO->max_participants
;
127 $eventSummary[$eventDAO->event_id
]['Start Date'] = CRM_Utils_Date
::customFormat($eventDAO->start_date
);
128 $eventSummary[$eventDAO->event_id
]['End Date'] = CRM_Utils_Date
::customFormat($eventDAO->end_date
);
129 $eventSummary[$eventDAO->event_id
]['Event Type'] = $eventDAO->event_type
;
130 $eventSummary[$eventDAO->event_id
]['Event Income'] = CRM_Utils_Money
::format($eventDAO->total
, $eventDAO->currency
);
131 $eventSummary[$eventDAO->event_id
]['Registered Participant'] = "{$eventDAO->participant} ({$activeparticipnatStutusLabel})";
132 $currency[$eventDAO->event_id
] = $eventDAO->currency
;
134 $this->assign_by_ref('summary', $eventSummary);
136 //Total Participant Registerd for the Event
137 $pariticipantCount = "
138 SELECT COUNT(civicrm_participant.id ) as count, civicrm_participant.event_id as event_id
140 FROM civicrm_participant
142 WHERE civicrm_participant.event_id IN( {$eventID}) AND
143 civicrm_participant.is_test = 0
144 {$activeParticipantClause}
145 GROUP BY civicrm_participant.event_id
148 $counteDAO = CRM_Core_DAO
::executeQuery($pariticipantCount);
149 while ($counteDAO->fetch()) {
150 $count[$counteDAO->event_id
] = $counteDAO->count
;
153 //Count the Participant by Role ID for Event
155 SELECT civicrm_participant.role_id as ROLEID,
156 COUNT( civicrm_participant.id ) as participant,
157 SUM(civicrm_participant.fee_amount) as amount,
158 civicrm_participant.event_id as event_id,
159 civicrm_participant.fee_currency as currency
160 FROM civicrm_participant
162 WHERE civicrm_participant.event_id IN ( {$eventID}) AND
163 civicrm_participant.is_test = 0
164 {$activeParticipantClause}
165 GROUP BY civicrm_participant.role_id, civicrm_participant.event_id
168 $roleDAO = CRM_Core_DAO
::executeQuery($role);
170 while ($roleDAO->fetch()) {
171 // fix for multiple role, CRM-6507
172 $roles = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $roleDAO->ROLEID
);
173 foreach ($roles as $roleId) {
174 if (!isset($roleRows[$roleDAO->event_id
][$participantRole[$roleId]])) {
175 $roleRows[$roleDAO->event_id
][$participantRole[$roleId]]['total'] = 0;
176 $roleRows[$roleDAO->event_id
][$participantRole[$roleId]]['round'] = 0;
177 $roleRows[$roleDAO->event_id
][$participantRole[$roleId]]['amount'] = 0;
179 $roleRows[$roleDAO->event_id
][$participantRole[$roleId]]['total'] +
= $roleDAO->participant
;
180 $roleRows[$roleDAO->event_id
][$participantRole[$roleId]]['amount'] +
= $roleDAO->amount
;
182 $roleRows[$roleDAO->event_id
][$participantRole[$roleId]]['amount'] = CRM_Utils_Money
::format($roleRows[$roleDAO->event_id
][$participantRole[$roleId]]['amount'], $currency[$roleDAO->event_id
]);
185 foreach ($roleRows as $eventId => $roleInfo) {
186 foreach ($participantRole as $roleName) {
187 if (isset($roleInfo[$roleName])) {
188 $roleRows[$eventId][$roleName]['round'] = round(($roleRows[$eventId][$roleName]['total'] / $count[$eventId]) * 100, 2);
193 $rows['Role'] = $roleRows;
195 //Count the Participant by status ID for Event
197 SELECT civicrm_participant.status_id as STATUSID,
198 COUNT( civicrm_participant.id ) as participant,
199 SUM(civicrm_participant.fee_amount) as amount,
200 civicrm_participant.event_id as event_id
202 FROM civicrm_participant
204 WHERE civicrm_participant.event_id IN ({$eventID}) AND
205 civicrm_participant.is_test = 0
206 {$activeParticipantClause}
207 GROUP BY civicrm_participant.status_id, civicrm_participant.event_id
210 $statusDAO = CRM_Core_DAO
::executeQuery($status);
212 while ($statusDAO->fetch()) {
213 $statusRows[$statusDAO->event_id
][$participantStatus[$statusDAO->STATUSID
]]['total'] = $statusDAO->participant
;
214 $statusRows[$statusDAO->event_id
][$participantStatus[$statusDAO->STATUSID
]]['round'] = round(($statusDAO->participant
/ $count[$statusDAO->event_id
]) * 100, 2);
215 $statusRows[$statusDAO->event_id
][$participantStatus[$statusDAO->STATUSID
]]['amount'] = CRM_Utils_Money
::format($statusDAO->amount
, $currency[$statusDAO->event_id
]);
218 $rows['Status'] = $statusRows;
220 //Count the Participant by payment instrument ID for Event
221 //e.g. Credit Card, Check,Cash etc
222 $paymentInstrument = "
223 SELECT c.payment_instrument_id as INSTRUMENT,
224 COUNT( c.id ) as participant,
225 SUM(civicrm_participant.fee_amount) as amount,
226 civicrm_participant.event_id as event_id
228 FROM civicrm_participant
229 LEFT JOIN civicrm_participant_payment pp ON(pp.participant_id = civicrm_participant.id )
230 LEFT JOIN civicrm_contribution c ON ( pp.contribution_id = c.id)
232 WHERE civicrm_participant.event_id IN ( {$eventID}) AND
233 civicrm_participant.is_test = 0
234 {$activeParticipantClause}
235 GROUP BY c.payment_instrument_id, civicrm_participant.event_id
238 $instrumentDAO = CRM_Core_DAO
::executeQuery($paymentInstrument);
240 while ($instrumentDAO->fetch()) {
241 //allow only if instrument is present in contribution table
242 if ($instrumentDAO->INSTRUMENT
) {
243 $instrumentRows[$instrumentDAO->event_id
][$paymentInstruments[$instrumentDAO->INSTRUMENT
]]['total'] = $instrumentDAO->participant
;
244 $instrumentRows[$instrumentDAO->event_id
][$paymentInstruments[$instrumentDAO->INSTRUMENT
]]['round'] = round(($instrumentDAO->participant
/ $count[$instrumentDAO->event_id
]) * 100, 2);
245 $instrumentRows[$instrumentDAO->event_id
][$paymentInstruments[$instrumentDAO->INSTRUMENT
]]['amount'] = $instrumentDAO->amount
;
248 $rows['Payment Method'] = $instrumentRows;
250 $this->assign_by_ref('rows', $rows);
251 if (!$this->_setVariable
) {
252 $this->_params
['id_value'] = NULL;
254 $this->assign('statistics', $this->statistics($eventIDs));
257 function statistics(&$eventIDs) {
258 $statistics = array();
259 $count = count($eventIDs);
260 $this->countStat($statistics, $count);
261 if ($this->_setVariable
) {
262 $this->filterStat($statistics);
268 function limit($rowCount = self
::ROW_COUNT_LIMIT
) {
269 parent
::limit($rowCount);
272 $pageId = $this->get(CRM_Utils_Pager
::PAGE_ID
);
274 //if pageId is greator than last page then display last page.
275 if ((($pageId * self
::ROW_COUNT_LIMIT
) - 1) > $this->_rowsFound
) {
276 $pageId = ceil((float)$this->_rowsFound
/ (float)self
::ROW_COUNT_LIMIT
);
277 $this->set(CRM_Utils_Pager
::PAGE_ID
, $pageId);
279 $this->_limit
= ($pageId - 1) * self
::ROW_COUNT_LIMIT
;
282 function setPager($rowCount = self
::ROW_COUNT_LIMIT
) {
284 'total' => $this->_rowsFound
,
285 'rowCount' => self
::ROW_COUNT_LIMIT
,
286 'status' => ts('Records %%StatusMessage%%'),
287 'buttonBottom' => 'PagerBottomButton',
288 'buttonTop' => 'PagerTopButton',
289 'pageID' => $this->get(CRM_Utils_Pager
::PAGE_ID
),
292 $pager = new CRM_Utils_Pager($params);
293 $this->assign_by_ref('pager', $pager);
296 function postProcess() {
297 $this->beginPostProcess();
298 $this->_setVariable
= TRUE;
300 $noSelection = FALSE;
301 if (empty($this->_params
['id_value'][0])) {
302 $this->_params
['id_value'] = array();
303 $this->_setVariable
= FALSE;
305 $events = CRM_Event_PseudoConstant
::event(NULL, NULL,
306 "is_template IS NULL OR is_template = 0"
308 if (empty($events)) {
311 foreach ($events as $key => $dnt) {
312 $this->_params
['id_value'][] = $key;
317 $this->_rowsFound
= count($this->_params
['id_value']);
319 //set pager and limit if output mode is html
320 if ($this->_outputMode
== 'html') {
324 $showEvents = array();
326 $numRows = $this->_limit
;
328 if (CRM_Utils_Array
::value('id_op', $this->_params
, 'in') == 'in' ||
$noSelection) {
329 while ($count < self
::ROW_COUNT_LIMIT
) {
330 if (!isset($this->_params
['id_value'][$numRows])) {
334 $showEvents[] = $this->_params
['id_value'][$numRows];
338 } elseif ($this->_params
['id_op'] == 'notin') {
339 $events = CRM_Event_PseudoConstant
::event(NULL, NULL,
340 "is_template IS NULL OR is_template = 0"
343 $showEvents = array_diff(array_keys($events), $this->_params
['id_value']);
346 $this->buildEventReport($showEvents);
350 $this->buildEventReport($this->_params
['id_value']);
353 parent
::endPostProcess();