Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | class CRM_Contact_Form_Search_Custom_EventAggregate extends CRM_Contact_Form_Search_Custom_Base implements CRM_Contact_Form_Search_Interface { | |
36 | ||
4e54c348 PJ |
37 | protected $_formValues; |
38 | public $_permissionedComponent; | |
39 | ||
86538308 EM |
40 | /** |
41 | * @param $formValues | |
42 | */ | |
4e54c348 | 43 | function __construct(&$formValues) { |
6a488035 | 44 | $this->_formValues = $formValues; |
4e54c348 | 45 | $this->_permissionedComponent = array('CiviContribute', 'CiviEvent'); |
6a488035 TO |
46 | |
47 | /** | |
48 | * Define the columns for search result rows | |
49 | */ | |
50 | $this->_columns = array( | |
51 | ts('Event') => 'event_name', | |
52 | ts('Type') => 'event_type', | |
53 | ts('Number of<br />Participant') => 'participant_count', | |
54 | ts('Total Payment') => 'payment_amount', | |
55 | ts('Fee') => 'fee', | |
56 | ts('Net Payment') => 'net_payment', | |
57 | ts('Participant') => 'participant', | |
58 | ); | |
59 | } | |
60 | ||
86538308 EM |
61 | /** |
62 | * @param $form | |
63 | */ | |
6a488035 TO |
64 | function buildForm(&$form) { |
65 | ||
66 | /** | |
67 | * You can define a custom title for the search form | |
68 | */ | |
69 | $this->setTitle('Find Totals for Events'); | |
70 | ||
71 | /** | |
72 | * Define the search form fields here | |
73 | */ | |
74 | ||
75 | $form->addElement('checkbox', 'paid_online', ts('Only show Credit Card Payments')); | |
76 | ||
77 | $form->addElement('checkbox', 'show_payees', ts('Show payees')); | |
78 | ||
79 | $event_type = CRM_Core_OptionGroup::values('event_type', FALSE); | |
80 | foreach ($event_type as $eventId => $eventName) { | |
81 | $form->addElement('checkbox', "event_type_id[$eventId]", 'Event Type', $eventName); | |
82 | } | |
c2be40dc | 83 | $events = CRM_Event_BAO_Event::getEvents(1); |
6a488035 TO |
84 | $form->add('select', 'event_id', ts('Event Name'), array('' => ts('- select -')) + $events); |
85 | ||
86 | $form->addDate('start_date', ts('Payments Date From'), FALSE, array('formatType' => 'custom')); | |
87 | $form->addDate('end_date', ts('...through'), FALSE, array('formatType' => 'custom')); | |
88 | ||
89 | /** | |
90 | * If you are using the sample template, this array tells the template fields to render | |
91 | * for the search form. | |
92 | */ | |
93 | $form->assign('elements', array('paid_online', 'start_date', 'end_date', 'show_payees', 'event_type_id', 'event_id')); | |
94 | } | |
95 | ||
96 | /** | |
97 | * Define the smarty template used to layout the search form and results listings. | |
98 | */ | |
99 | function templateFile() { | |
100 | return 'CRM/Contact/Form/Search/Custom/EventDetails.tpl'; | |
101 | } | |
102 | ||
103 | /** | |
104 | * Construct the search query | |
105 | */ | |
106 | function all($offset = 0, $rowcount = 0, $sort = NULL, | |
107 | $includeContactIDs = FALSE, $justIDs = FALSE | |
108 | ) { | |
109 | // SELECT clause must include contact_id as an alias for civicrm_contact.id if you are going to use "tasks" like export etc. | |
110 | $select = "civicrm_participant.event_id as event_id, | |
111 | COUNT(civicrm_participant.id) as participant_count, | |
112 | GROUP_CONCAT(DISTINCT(civicrm_event.title)) as event_name, | |
113 | civicrm_event.event_type_id as event_type_id, | |
114 | civicrm_option_value.label as event_type, | |
115 | IF(civicrm_contribution.payment_instrument_id <>0 , 'Yes', 'No') as payment_instrument_id, | |
116 | SUM(civicrm_contribution.total_amount) as payment_amount, | |
117 | format(sum(if(civicrm_contribution.payment_instrument_id <>0,(civicrm_contribution.total_amount *.034) +.45,0)),2) as fee, | |
118 | format(sum(civicrm_contribution.total_amount - (if(civicrm_contribution.payment_instrument_id <>0,(civicrm_contribution.total_amount *.034) +.45,0))),2) as net_payment"; | |
119 | ||
120 | $from = $this->from(); | |
121 | ||
122 | $onLine = CRM_Utils_Array::value('paid_online', | |
123 | $this->_formValues | |
124 | ); | |
125 | if ($onLine) { | |
126 | $from .= " | |
127 | inner join civicrm_entity_financial_trxn | |
128 | on (civicrm_entity_financial_trxn.entity_id = civicrm_participant_payment.contribution_id and civicrm_entity_financial_trxn.entity_table='civicrm_contribution')"; | |
129 | } | |
130 | ||
131 | $showPayees = CRM_Utils_Array::value('show_payees', | |
132 | $this->_formValues | |
133 | ); | |
134 | if ($showPayees) { | |
135 | $select .= ", GROUP_CONCAT(DISTINCT(civicrm_contact.display_name)) as participant "; | |
136 | $from .= " inner join civicrm_contact | |
137 | on civicrm_contact.id = civicrm_participant.contact_id"; | |
138 | } | |
139 | else { | |
140 | unset($this->_columns['Participant']); | |
141 | } | |
142 | ||
143 | $where = $this->where(); | |
144 | ||
145 | $groupBy = "event_id"; | |
146 | if (!empty($this->_formValues['event_type_id'])) { | |
147 | $groupBy = "event_type_id"; | |
148 | } | |
149 | ||
150 | $sql = " | |
151 | SELECT $select | |
152 | FROM $from | |
153 | WHERE $where | |
154 | GROUP BY $groupBy | |
155 | "; | |
156 | // Define ORDER BY for query in $sort, with default value | |
157 | if (!empty($sort)) { | |
158 | if (is_string($sort)) { | |
159 | $sql .= " ORDER BY $sort "; | |
160 | } | |
161 | else { | |
162 | $sql .= " ORDER BY " . trim($sort->orderBy()); | |
163 | } | |
164 | } | |
165 | else { | |
166 | $sql .= "ORDER BY event_name desc"; | |
167 | } | |
168 | ||
169 | if ($rowcount > 0 && $offset >= 0) { | |
bf00d1b6 | 170 | $offset = CRM_Utils_Type::escape($offset, 'Int'); |
dd3a4117 | 171 | $rowcount = CRM_Utils_Type::escape($rowcount, 'Int'); |
6a488035 TO |
172 | $sql .= " LIMIT $offset, $rowcount "; |
173 | } | |
174 | ||
175 | // Uncomment the next line to see the actual SQL generated: | |
176 | //CRM_Core_Error::debug('sql',$sql); exit(); | |
177 | return $sql; | |
178 | } | |
179 | ||
86538308 EM |
180 | /** |
181 | * @return string | |
182 | */ | |
6a488035 TO |
183 | function from() { |
184 | return " | |
185 | civicrm_participant_payment | |
186 | left join civicrm_participant | |
187 | on civicrm_participant_payment.participant_id=civicrm_participant.id | |
188 | ||
189 | left join civicrm_event on | |
190 | civicrm_participant.event_id = civicrm_event.id | |
191 | ||
192 | left join civicrm_contribution | |
193 | on civicrm_contribution.id = civicrm_participant_payment.contribution_id | |
194 | ||
195 | left join civicrm_option_value on | |
196 | ( civicrm_option_value.value = civicrm_event.event_type_id AND civicrm_option_value.option_group_id = 14)"; | |
197 | } | |
198 | ||
199 | /* | |
200 | * WHERE clause is an array built from any required JOINS plus conditional filters based on search criteria field values | |
201 | * | |
202 | */ | |
86538308 EM |
203 | /** |
204 | * @param bool $includeContactIDs | |
205 | * | |
206 | * @return string | |
207 | */ | |
6a488035 TO |
208 | function where($includeContactIDs = FALSE) { |
209 | $clauses = array(); | |
210 | ||
211 | $clauses[] = "civicrm_participant.status_id in ( 1 )"; | |
212 | $clauses[] = "civicrm_contribution.is_test = 0"; | |
213 | $onLine = CRM_Utils_Array::value('paid_online', | |
214 | $this->_formValues | |
215 | ); | |
216 | if ($onLine) { | |
217 | $clauses[] = "civicrm_contribution.payment_instrument_id <> 0"; | |
218 | } | |
219 | ||
220 | $startDate = CRM_Utils_Date::processDate($this->_formValues['start_date']); | |
221 | if ($startDate) { | |
222 | $clauses[] = "civicrm_contribution.receive_date >= $startDate"; | |
223 | } | |
224 | ||
225 | $endDate = CRM_Utils_Date::processDate($this->_formValues['end_date']); | |
226 | if ($endDate) { | |
227 | $clauses[] = "civicrm_contribution.receive_date <= {$endDate}235959"; | |
228 | } | |
229 | ||
230 | if (!empty($this->_formValues['event_id'])) { | |
231 | $clauses[] = "civicrm_event.id = {$this->_formValues['event_id']}"; | |
232 | } | |
233 | ||
234 | if ($includeContactIDs) { | |
235 | $contactIDs = array(); | |
236 | foreach ($this->_formValues as $id => $value) { | |
237 | if ($value && | |
238 | substr($id, 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX | |
239 | ) { | |
240 | $contactIDs[] = substr($id, CRM_Core_Form::CB_PREFIX_LEN); | |
241 | } | |
242 | } | |
243 | ||
244 | if (!empty($contactIDs)) { | |
245 | $contactIDs = implode(', ', $contactIDs); | |
246 | $clauses[] = "contact.id IN ( $contactIDs )"; | |
247 | } | |
248 | } | |
249 | ||
250 | if (!empty($this->_formValues['event_type_id'])) { | |
251 | $event_type_ids = implode(',', array_keys($this->_formValues['event_type_id'])); | |
252 | $clauses[] = "civicrm_event.event_type_id IN ( $event_type_ids )"; | |
253 | } | |
254 | return implode(' AND ', $clauses); | |
255 | } | |
256 | ||
257 | ||
258 | /* This function does a query to get totals for some of the search result columns and returns a totals array. */ | |
86538308 EM |
259 | /** |
260 | * @return array | |
261 | */ | |
6a488035 TO |
262 | function summary() { |
263 | $totalSelect = " | |
264 | SUM(civicrm_contribution.total_amount) as payment_amount,COUNT(civicrm_participant.id) as participant_count, | |
265 | format(sum(if(civicrm_contribution.payment_instrument_id <>0,(civicrm_contribution.total_amount *.034) +.45,0)),2) as fee, | |
266 | format(sum(civicrm_contribution.total_amount - (if(civicrm_contribution.payment_instrument_id <>0,(civicrm_contribution.total_amount *.034) +.45,0))),2) as net_payment"; | |
267 | ||
268 | $from = $this->from(); | |
269 | ||
270 | $onLine = CRM_Utils_Array::value('paid_online', | |
271 | $this->_formValues | |
272 | ); | |
273 | if ($onLine) { | |
274 | $from .= " | |
275 | inner join civicrm_entity_financial_trxn | |
276 | on (civicrm_entity_financial_trxn.entity_id = civicrm_participant_payment.contribution_id and civicrm_entity_financial_trxn.entity_table='civicrm_contribution')"; | |
277 | } | |
278 | ||
279 | ||
280 | $where = $this->where(); | |
281 | ||
282 | $sql = " | |
283 | SELECT $totalSelect | |
284 | FROM $from | |
285 | WHERE $where | |
286 | "; | |
287 | ||
288 | //CRM_Core_Error::debug('sql',$sql); | |
289 | $dao = CRM_Core_DAO::executeQuery($sql, | |
290 | CRM_Core_DAO::$_nullArray | |
291 | ); | |
292 | $totals = array(); | |
293 | while ($dao->fetch()) { | |
294 | $totals['payment_amount'] = $dao->payment_amount; | |
295 | $totals['fee'] = $dao->fee; | |
296 | $totals['net_payment'] = $dao->net_payment; | |
297 | $totals['participant_count'] = $dao->participant_count; | |
298 | } | |
299 | return $totals; | |
300 | } | |
301 | ||
302 | /* | |
303 | * Functions below generally don't need to be modified | |
304 | */ | |
305 | function count() { | |
306 | $sql = $this->all(); | |
307 | ||
308 | $dao = CRM_Core_DAO::executeQuery($sql, | |
309 | CRM_Core_DAO::$_nullArray | |
310 | ); | |
311 | return $dao->N; | |
312 | } | |
313 | ||
86538308 EM |
314 | /** |
315 | * @param int $offset | |
316 | * @param int $rowcount | |
317 | * @param null $sort | |
318 | * | |
319 | * @return string | |
320 | */ | |
6a488035 TO |
321 | function contactIDs($offset = 0, $rowcount = 0, $sort = NULL) { |
322 | return $this->all($offset, $rowcount, $sort); | |
323 | } | |
324 | ||
86538308 EM |
325 | /** |
326 | * @return array | |
327 | */ | |
6a488035 TO |
328 | function &columns() { |
329 | return $this->_columns; | |
330 | } | |
331 | ||
86538308 EM |
332 | /** |
333 | * @param $title | |
334 | */ | |
6a488035 TO |
335 | function setTitle($title) { |
336 | if ($title) { | |
337 | CRM_Utils_System::setTitle($title); | |
338 | } | |
339 | else { | |
340 | CRM_Utils_System::setTitle(ts('Search')); | |
341 | } | |
342 | } | |
343 | } | |
344 |