3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2015
35 class CRM_Report_Form_Event_Income
extends CRM_Report_Form_Event
{
36 const ROW_COUNT_LIMIT
= 2;
38 protected $_summary = NULL;
39 protected $_noFields = TRUE;
41 protected $_add2groupSupported = FALSE;
46 public function __construct() {
48 $this->_columns
= array(
49 'civicrm_event' => array(
50 'dao' => 'CRM_Event_DAO_Event',
53 'title' => ts('Event'),
54 'operatorType' => CRM_Report_Form
::OP_ENTITYREF
,
55 'type' => CRM_Utils_Type
::T_INT
,
56 'attributes' => array('select' => array('minimumInputLength' => 0)),
62 parent
::__construct();
65 public function preProcess() {
66 $this->_csvSupported
= FALSE;
73 * @param array $eventIDs
75 public function buildEventReport($eventIDs) {
77 $this->assign('events', $eventIDs);
79 $eventID = implode(',', $eventIDs);
81 $participantStatus = CRM_Event_PseudoConstant
::participantStatus(NULL, "is_counted = 1");
82 $participantRole = CRM_Event_PseudoConstant
::participantRole();
83 $paymentInstruments = CRM_Contribute_PseudoConstant
::paymentInstrument();
85 $rows = $eventSummary = $roleRows = $statusRows = $instrumentRows = $count = array();
87 $optionGroupDAO = new CRM_Core_DAO_OptionGroup();
88 $optionGroupDAO->name
= 'event_type';
89 $optionGroupId = NULL;
90 if ($optionGroupDAO->find(TRUE)) {
91 $optionGroupId = $optionGroupDAO->id
;
93 //show the income of active participant status (Counted = filter = 1)
94 $activeParticipantStatusIDArray = $activeParticipantStatusLabelArray = array();
95 foreach ($participantStatus as $id => $label) {
96 $activeParticipantStatusIDArray[] = $id;
97 $activeParticipantStatusLabelArray[] = $label;
99 $activeParticipantStatus = implode(',', $activeParticipantStatusIDArray);
100 $activeparticipnatStutusLabel = implode(', ', $activeParticipantStatusLabelArray);
101 $activeParticipantClause = " AND civicrm_participant.status_id IN ( $activeParticipantStatus ) ";
104 SELECT civicrm_event.id as event_id,
105 civicrm_event.title as event_title,
106 civicrm_event.max_participants as max_participants,
107 civicrm_event.start_date as start_date,
108 civicrm_event.end_date as end_date,
109 civicrm_option_value.label as event_type,
110 civicrm_participant.fee_currency as currency,
111 SUM(civicrm_participant.fee_amount) as total,
112 COUNT(civicrm_participant.id) as participant
115 LEFT JOIN civicrm_option_value
116 ON ( civicrm_event.event_type_id = civicrm_option_value.value AND
117 civicrm_option_value.option_group_id = {$optionGroupId} )
118 LEFT JOIN civicrm_participant ON ( civicrm_event.id = civicrm_participant.event_id
119 {$activeParticipantClause} AND civicrm_participant.is_test = 0 )
121 WHERE civicrm_event.id IN( {$eventID})
123 GROUP BY civicrm_event.id
126 $eventDAO = CRM_Core_DAO
::executeQuery($sql);
128 while ($eventDAO->fetch()) {
129 $eventSummary[$eventDAO->event_id
]['Title'] = $eventDAO->event_title
;
130 $eventSummary[$eventDAO->event_id
]['Max Participants'] = $eventDAO->max_participants
;
131 $eventSummary[$eventDAO->event_id
]['Start Date'] = CRM_Utils_Date
::customFormat($eventDAO->start_date
);
132 $eventSummary[$eventDAO->event_id
]['End Date'] = CRM_Utils_Date
::customFormat($eventDAO->end_date
);
133 $eventSummary[$eventDAO->event_id
]['Event Type'] = $eventDAO->event_type
;
134 $eventSummary[$eventDAO->event_id
]['Event Income'] = CRM_Utils_Money
::format($eventDAO->total
, $eventDAO->currency
);
135 $eventSummary[$eventDAO->event_id
]['Registered Participant'] = "{$eventDAO->participant} ({$activeparticipnatStutusLabel})";
136 $currency[$eventDAO->event_id
] = $eventDAO->currency
;
138 $this->assign_by_ref('summary', $eventSummary);
140 //Total Participant Registerd for the Event
141 $pariticipantCount = "
142 SELECT COUNT(civicrm_participant.id ) as count, civicrm_participant.event_id as event_id
144 FROM civicrm_participant
146 WHERE civicrm_participant.event_id IN( {$eventID}) AND
147 civicrm_participant.is_test = 0
148 {$activeParticipantClause}
149 GROUP BY civicrm_participant.event_id
152 $counteDAO = CRM_Core_DAO
::executeQuery($pariticipantCount);
153 while ($counteDAO->fetch()) {
154 $count[$counteDAO->event_id
] = $counteDAO->count
;
157 // Count the Participant by Role ID for Event.
159 SELECT civicrm_participant.role_id as ROLEID,
160 COUNT( civicrm_participant.id ) as participant,
161 SUM(civicrm_participant.fee_amount) as amount,
162 civicrm_participant.event_id as event_id,
163 civicrm_participant.fee_currency as currency
164 FROM civicrm_participant
166 WHERE civicrm_participant.event_id IN ( {$eventID}) AND
167 civicrm_participant.is_test = 0
168 {$activeParticipantClause}
169 GROUP BY civicrm_participant.role_id, civicrm_participant.event_id
172 $roleDAO = CRM_Core_DAO
::executeQuery($role);
174 while ($roleDAO->fetch()) {
175 // fix for multiple role, CRM-6507
176 $roles = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $roleDAO->ROLEID
);
177 foreach ($roles as $roleId) {
178 if (!isset($roleRows[$roleDAO->event_id
][$participantRole[$roleId]])) {
179 $roleRows[$roleDAO->event_id
][$participantRole[$roleId]]['total'] = 0;
180 $roleRows[$roleDAO->event_id
][$participantRole[$roleId]]['round'] = 0;
181 $roleRows[$roleDAO->event_id
][$participantRole[$roleId]]['amount'] = 0;
183 $roleRows[$roleDAO->event_id
][$participantRole[$roleId]]['total'] +
= $roleDAO->participant
;
184 $roleRows[$roleDAO->event_id
][$participantRole[$roleId]]['amount'] +
= $roleDAO->amount
;
188 foreach ($roleRows as $eventId => $roleInfo) {
189 foreach ($participantRole as $roleName) {
190 if (isset($roleInfo[$roleName])) {
191 $roleRows[$eventId][$roleName]['round'] = round(($roleRows[$eventId][$roleName]['total'] / $count[$eventId]) * 100, 2);
193 if (!empty($roleRows[$eventId][$roleName])) {
194 $roleRows[$eventId][$roleName]['amount'] = CRM_Utils_Money
::format($roleRows[$eventId][$roleName]['amount'], $currency[$eventId]);
199 $rows['Role'] = $roleRows;
201 // Count the Participant by status ID for Event.
203 SELECT civicrm_participant.status_id as STATUSID,
204 COUNT( civicrm_participant.id ) as participant,
205 SUM(civicrm_participant.fee_amount) as amount,
206 civicrm_participant.event_id as event_id
208 FROM civicrm_participant
210 WHERE civicrm_participant.event_id IN ({$eventID}) AND
211 civicrm_participant.is_test = 0
212 {$activeParticipantClause}
213 GROUP BY civicrm_participant.status_id, civicrm_participant.event_id
216 $statusDAO = CRM_Core_DAO
::executeQuery($status);
218 while ($statusDAO->fetch()) {
219 $statusRows[$statusDAO->event_id
][$participantStatus[$statusDAO->STATUSID
]]['total'] = $statusDAO->participant
;
220 $statusRows[$statusDAO->event_id
][$participantStatus[$statusDAO->STATUSID
]]['round'] = round(($statusDAO->participant
/ $count[$statusDAO->event_id
]) * 100, 2);
221 $statusRows[$statusDAO->event_id
][$participantStatus[$statusDAO->STATUSID
]]['amount'] = CRM_Utils_Money
::format($statusDAO->amount
, $currency[$statusDAO->event_id
]);
224 $rows['Status'] = $statusRows;
226 //Count the Participant by payment instrument ID for Event
227 //e.g. Credit Card, Check,Cash etc
228 $paymentInstrument = "
229 SELECT c.payment_instrument_id as INSTRUMENT,
230 COUNT( civicrm_participant.id ) as participant,
231 SUM( civicrm_participant.fee_amount ) as amount,
232 civicrm_participant.event_id as event_id
234 FROM civicrm_participant,
235 civicrm_participant_payment pp
236 LEFT JOIN civicrm_contribution c ON ( pp.contribution_id = c.id)
238 WHERE civicrm_participant.event_id IN ( {$eventID} )
239 AND civicrm_participant.is_test = 0
240 {$activeParticipantClause}
241 AND ((pp.participant_id = civicrm_participant.id )
242 OR (pp.participant_id = civicrm_participant.registered_by_id ))
243 GROUP BY c.payment_instrument_id, civicrm_participant.event_id
246 $instrumentDAO = CRM_Core_DAO
::executeQuery($paymentInstrument);
248 while ($instrumentDAO->fetch()) {
249 //allow only if instrument is present in contribution table
250 if ($instrumentDAO->INSTRUMENT
) {
251 $instrumentRows[$instrumentDAO->event_id
][$paymentInstruments[$instrumentDAO->INSTRUMENT
]]['total'] = $instrumentDAO->participant
;
252 $instrumentRows[$instrumentDAO->event_id
][$paymentInstruments[$instrumentDAO->INSTRUMENT
]]['round'] = round(($instrumentDAO->participant
/ $count[$instrumentDAO->event_id
]) * 100, 2);
253 $instrumentRows[$instrumentDAO->event_id
][$paymentInstruments[$instrumentDAO->INSTRUMENT
]]['amount'] = CRM_Utils_Money
::format($instrumentDAO->amount
, $currency[$instrumentDAO->event_id
]);
256 $rows['Payment Method'] = $instrumentRows;
258 $this->assign_by_ref('rows', $rows);
259 if (!$this->_setVariable
) {
260 $this->_params
['id_value'] = NULL;
262 $this->assign('statistics', $this->statistics($eventIDs));
270 public function statistics(&$eventIDs) {
271 $statistics = array();
272 $count = count($eventIDs);
273 $this->countStat($statistics, $count);
274 if ($this->_setVariable
) {
275 $this->filterStat($statistics);
284 public function limit($rowCount = self
::ROW_COUNT_LIMIT
) {
285 parent
::limit($rowCount);
288 $pageId = $this->get(CRM_Utils_Pager
::PAGE_ID
);
290 //if pageId is greater than last page then display last page.
291 if ((($pageId * self
::ROW_COUNT_LIMIT
) - 1) > $this->_rowsFound
) {
292 $pageId = ceil((float) $this->_rowsFound
/ (float) self
::ROW_COUNT_LIMIT
);
293 $this->set(CRM_Utils_Pager
::PAGE_ID
, $pageId);
295 $this->_limit
= ($pageId - 1) * self
::ROW_COUNT_LIMIT
;
299 * @param int $rowCount
301 public function setPager($rowCount = self
::ROW_COUNT_LIMIT
) {
303 'total' => $this->_rowsFound
,
304 'rowCount' => self
::ROW_COUNT_LIMIT
,
305 'status' => ts('Records %%StatusMessage%%'),
306 'buttonBottom' => 'PagerBottomButton',
307 'buttonTop' => 'PagerTopButton',
308 'pageID' => $this->get(CRM_Utils_Pager
::PAGE_ID
),
311 $pager = new CRM_Utils_Pager($params);
312 $this->assign_by_ref('pager', $pager);
316 * Form post process function.
320 public function postProcess() {
321 $this->beginPostProcess();
322 $this->_setVariable
= TRUE;
324 $noSelection = FALSE;
325 if (empty($this->_params
['id_value'])) {
326 $this->_params
['id_value'] = array();
327 $this->_setVariable
= FALSE;
329 $events = CRM_Event_PseudoConstant
::event(NULL, NULL,
332 if (empty($events)) {
335 foreach ($events as $key => $dnt) {
336 $this->_params
['id_value'][] = $key;
340 elseif (!is_array($this->_params
['id_value'])) {
341 $this->_params
['id_value'] = explode(',', $this->_params
['id_value']);
344 $this->_rowsFound
= count($this->_params
['id_value']);
346 //set pager and limit if output mode is html
347 if ($this->_outputMode
== 'html') {
351 $showEvents = array();
353 $numRows = $this->_limit
;
355 if (CRM_Utils_Array
::value('id_op', $this->_params
, 'in') == 'in' ||
$noSelection) {
356 while ($count < self
::ROW_COUNT_LIMIT
) {
357 if (!isset($this->_params
['id_value'][$numRows])) {
361 $showEvents[] = $this->_params
['id_value'][$numRows];
366 elseif ($this->_params
['id_op'] == 'notin') {
367 $events = CRM_Event_PseudoConstant
::event(NULL, NULL,
371 $showEvents = array_diff(array_keys($events), $this->_params
['id_value']);
374 $this->buildEventReport($showEvents);
378 $this->buildEventReport($this->_params
['id_value']);
381 parent
::endPostProcess();