Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
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 | */ | |
00be9182 | 43 | public 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 | 61 | /** |
c490a46a | 62 | * @param CRM_Core_Form $form |
86538308 | 63 | */ |
00be9182 | 64 | public function buildForm(&$form) { |
6a488035 TO |
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 | */ | |
00be9182 | 99 | public function templateFile() { |
6a488035 TO |
100 | return 'CRM/Contact/Form/Search/Custom/EventDetails.tpl'; |
101 | } | |
102 | ||
103 | /** | |
104 | * Construct the search query | |
105 | */ | |
51ccfbbe TO |
106 | function all( |
107 | $offset = 0, $rowcount = 0, $sort = NULL, | |
6a488035 TO |
108 | $includeContactIDs = FALSE, $justIDs = FALSE |
109 | ) { | |
110 | // SELECT clause must include contact_id as an alias for civicrm_contact.id if you are going to use "tasks" like export etc. | |
111 | $select = "civicrm_participant.event_id as event_id, | |
112 | COUNT(civicrm_participant.id) as participant_count, | |
113 | GROUP_CONCAT(DISTINCT(civicrm_event.title)) as event_name, | |
114 | civicrm_event.event_type_id as event_type_id, | |
115 | civicrm_option_value.label as event_type, | |
116 | IF(civicrm_contribution.payment_instrument_id <>0 , 'Yes', 'No') as payment_instrument_id, | |
117 | SUM(civicrm_contribution.total_amount) as payment_amount, | |
118 | format(sum(if(civicrm_contribution.payment_instrument_id <>0,(civicrm_contribution.total_amount *.034) +.45,0)),2) as fee, | |
119 | format(sum(civicrm_contribution.total_amount - (if(civicrm_contribution.payment_instrument_id <>0,(civicrm_contribution.total_amount *.034) +.45,0))),2) as net_payment"; | |
120 | ||
121 | $from = $this->from(); | |
122 | ||
123 | $onLine = CRM_Utils_Array::value('paid_online', | |
124 | $this->_formValues | |
125 | ); | |
126 | if ($onLine) { | |
127 | $from .= " | |
128 | inner join civicrm_entity_financial_trxn | |
129 | on (civicrm_entity_financial_trxn.entity_id = civicrm_participant_payment.contribution_id and civicrm_entity_financial_trxn.entity_table='civicrm_contribution')"; | |
130 | } | |
131 | ||
132 | $showPayees = CRM_Utils_Array::value('show_payees', | |
133 | $this->_formValues | |
134 | ); | |
135 | if ($showPayees) { | |
136 | $select .= ", GROUP_CONCAT(DISTINCT(civicrm_contact.display_name)) as participant "; | |
137 | $from .= " inner join civicrm_contact | |
138 | on civicrm_contact.id = civicrm_participant.contact_id"; | |
139 | } | |
140 | else { | |
141 | unset($this->_columns['Participant']); | |
142 | } | |
143 | ||
144 | $where = $this->where(); | |
145 | ||
146 | $groupBy = "event_id"; | |
147 | if (!empty($this->_formValues['event_type_id'])) { | |
148 | $groupBy = "event_type_id"; | |
149 | } | |
150 | ||
151 | $sql = " | |
152 | SELECT $select | |
153 | FROM $from | |
154 | WHERE $where | |
155 | GROUP BY $groupBy | |
156 | "; | |
157 | // Define ORDER BY for query in $sort, with default value | |
158 | if (!empty($sort)) { | |
159 | if (is_string($sort)) { | |
160 | $sql .= " ORDER BY $sort "; | |
161 | } | |
162 | else { | |
163 | $sql .= " ORDER BY " . trim($sort->orderBy()); | |
164 | } | |
165 | } | |
166 | else { | |
167 | $sql .= "ORDER BY event_name desc"; | |
168 | } | |
169 | ||
170 | if ($rowcount > 0 && $offset >= 0) { | |
bf00d1b6 | 171 | $offset = CRM_Utils_Type::escape($offset, 'Int'); |
dd3a4117 | 172 | $rowcount = CRM_Utils_Type::escape($rowcount, 'Int'); |
6a488035 TO |
173 | $sql .= " LIMIT $offset, $rowcount "; |
174 | } | |
6a488035 TO |
175 | return $sql; |
176 | } | |
177 | ||
86538308 EM |
178 | /** |
179 | * @return string | |
180 | */ | |
00be9182 | 181 | public function from() { |
6a488035 TO |
182 | return " |
183 | civicrm_participant_payment | |
184 | left join civicrm_participant | |
185 | on civicrm_participant_payment.participant_id=civicrm_participant.id | |
186 | ||
187 | left join civicrm_event on | |
188 | civicrm_participant.event_id = civicrm_event.id | |
189 | ||
190 | left join civicrm_contribution | |
191 | on civicrm_contribution.id = civicrm_participant_payment.contribution_id | |
192 | ||
193 | left join civicrm_option_value on | |
194 | ( civicrm_option_value.value = civicrm_event.event_type_id AND civicrm_option_value.option_group_id = 14)"; | |
195 | } | |
196 | ||
86538308 | 197 | /** |
c490a46a CW |
198 | * WHERE clause is an array built from any required JOINS plus conditional filters based on search criteria field values |
199 | * | |
86538308 EM |
200 | * @param bool $includeContactIDs |
201 | * | |
202 | * @return string | |
203 | */ | |
00be9182 | 204 | public function where($includeContactIDs = FALSE) { |
6a488035 TO |
205 | $clauses = array(); |
206 | ||
207 | $clauses[] = "civicrm_participant.status_id in ( 1 )"; | |
208 | $clauses[] = "civicrm_contribution.is_test = 0"; | |
209 | $onLine = CRM_Utils_Array::value('paid_online', | |
210 | $this->_formValues | |
211 | ); | |
212 | if ($onLine) { | |
213 | $clauses[] = "civicrm_contribution.payment_instrument_id <> 0"; | |
214 | } | |
215 | ||
216 | $startDate = CRM_Utils_Date::processDate($this->_formValues['start_date']); | |
217 | if ($startDate) { | |
218 | $clauses[] = "civicrm_contribution.receive_date >= $startDate"; | |
219 | } | |
220 | ||
221 | $endDate = CRM_Utils_Date::processDate($this->_formValues['end_date']); | |
222 | if ($endDate) { | |
223 | $clauses[] = "civicrm_contribution.receive_date <= {$endDate}235959"; | |
224 | } | |
225 | ||
226 | if (!empty($this->_formValues['event_id'])) { | |
227 | $clauses[] = "civicrm_event.id = {$this->_formValues['event_id']}"; | |
228 | } | |
229 | ||
230 | if ($includeContactIDs) { | |
231 | $contactIDs = array(); | |
232 | foreach ($this->_formValues as $id => $value) { | |
233 | if ($value && | |
234 | substr($id, 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX | |
235 | ) { | |
236 | $contactIDs[] = substr($id, CRM_Core_Form::CB_PREFIX_LEN); | |
237 | } | |
238 | } | |
239 | ||
240 | if (!empty($contactIDs)) { | |
241 | $contactIDs = implode(', ', $contactIDs); | |
242 | $clauses[] = "contact.id IN ( $contactIDs )"; | |
243 | } | |
244 | } | |
245 | ||
246 | if (!empty($this->_formValues['event_type_id'])) { | |
247 | $event_type_ids = implode(',', array_keys($this->_formValues['event_type_id'])); | |
248 | $clauses[] = "civicrm_event.event_type_id IN ( $event_type_ids )"; | |
249 | } | |
250 | return implode(' AND ', $clauses); | |
251 | } | |
252 | ||
253 | ||
254 | /* This function does a query to get totals for some of the search result columns and returns a totals array. */ | |
86538308 EM |
255 | /** |
256 | * @return array | |
257 | */ | |
00be9182 | 258 | public function summary() { |
6a488035 TO |
259 | $totalSelect = " |
260 | SUM(civicrm_contribution.total_amount) as payment_amount,COUNT(civicrm_participant.id) as participant_count, | |
261 | format(sum(if(civicrm_contribution.payment_instrument_id <>0,(civicrm_contribution.total_amount *.034) +.45,0)),2) as fee, | |
262 | format(sum(civicrm_contribution.total_amount - (if(civicrm_contribution.payment_instrument_id <>0,(civicrm_contribution.total_amount *.034) +.45,0))),2) as net_payment"; | |
263 | ||
264 | $from = $this->from(); | |
265 | ||
266 | $onLine = CRM_Utils_Array::value('paid_online', | |
267 | $this->_formValues | |
268 | ); | |
269 | if ($onLine) { | |
270 | $from .= " | |
271 | inner join civicrm_entity_financial_trxn | |
272 | on (civicrm_entity_financial_trxn.entity_id = civicrm_participant_payment.contribution_id and civicrm_entity_financial_trxn.entity_table='civicrm_contribution')"; | |
273 | } | |
274 | ||
6a488035 TO |
275 | $where = $this->where(); |
276 | ||
277 | $sql = " | |
278 | SELECT $totalSelect | |
279 | FROM $from | |
280 | WHERE $where | |
281 | "; | |
282 | ||
6a488035 TO |
283 | $dao = CRM_Core_DAO::executeQuery($sql, |
284 | CRM_Core_DAO::$_nullArray | |
285 | ); | |
286 | $totals = array(); | |
287 | while ($dao->fetch()) { | |
288 | $totals['payment_amount'] = $dao->payment_amount; | |
289 | $totals['fee'] = $dao->fee; | |
290 | $totals['net_payment'] = $dao->net_payment; | |
291 | $totals['participant_count'] = $dao->participant_count; | |
292 | } | |
293 | return $totals; | |
294 | } | |
295 | ||
296 | /* | |
c490a46a CW |
297 | * Functions below generally don't need to be modified |
298 | */ | |
00be9182 | 299 | public function count() { |
6a488035 TO |
300 | $sql = $this->all(); |
301 | ||
302 | $dao = CRM_Core_DAO::executeQuery($sql, | |
303 | CRM_Core_DAO::$_nullArray | |
304 | ); | |
305 | return $dao->N; | |
306 | } | |
307 | ||
86538308 EM |
308 | /** |
309 | * @param int $offset | |
310 | * @param int $rowcount | |
311 | * @param null $sort | |
312 | * | |
313 | * @return string | |
314 | */ | |
00be9182 | 315 | public function contactIDs($offset = 0, $rowcount = 0, $sort = NULL) { |
6a488035 TO |
316 | return $this->all($offset, $rowcount, $sort); |
317 | } | |
318 | ||
86538308 EM |
319 | /** |
320 | * @return array | |
321 | */ | |
00be9182 | 322 | public function &columns() { |
6a488035 TO |
323 | return $this->_columns; |
324 | } | |
325 | ||
86538308 EM |
326 | /** |
327 | * @param $title | |
328 | */ | |
00be9182 | 329 | public function setTitle($title) { |
6a488035 TO |
330 | if ($title) { |
331 | CRM_Utils_System::setTitle($title); | |
332 | } | |
333 | else { | |
334 | CRM_Utils_System::setTitle(ts('Search')); | |
335 | } | |
336 | } | |
337 | } |