Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
6b83d5bd | 31 | * @copyright CiviCRM LLC (c) 2004-2019 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | class CRM_Report_Form_Pledge_Summary extends CRM_Report_Form { | |
36 | ||
37 | protected $_summary = NULL; | |
38 | protected $_totalPaid = FALSE; | |
39 | protected $_customGroupExtends = array('Pledge', 'Individual'); | |
40 | protected $_customGroupGroupBy = TRUE; | |
6a488035 | 41 | |
74cf4551 | 42 | /** |
1728e9a0 | 43 | * This report has not been optimised for group filtering. |
44 | * | |
45 | * The functionality for group filtering has been improved but not | |
46 | * all reports have been adjusted to take care of it. This report has not | |
47 | * and will run an inefficient query until fixed. | |
48 | * | |
49 | * CRM-19170 | |
50 | * | |
51 | * @var bool | |
74cf4551 | 52 | */ |
1728e9a0 | 53 | protected $groupFilterNotOptimised = TRUE; |
54 | ||
74cf4551 | 55 | /** |
73b448bf | 56 | * Class constructor. |
74cf4551 | 57 | */ |
00be9182 | 58 | public function __construct() { |
6a488035 | 59 | $this->_columns = array( |
c301f76e | 60 | 'civicrm_contact' => array( |
61 | 'dao' => 'CRM_Contact_DAO_Contact', | |
62 | 'fields' => array( | |
63 | 'sort_name' => array( | |
64 | 'title' => ts('Contact Name'), | |
65 | 'no_repeat' => TRUE, | |
6a488035 | 66 | ), |
c301f76e | 67 | 'postal_greeting_display' => array('title' => ts('Postal Greeting')), |
68 | 'id' => array( | |
69 | 'no_display' => TRUE, | |
70 | 'required' => TRUE, | |
6a488035 TO |
71 | ), |
72 | ), | |
c301f76e | 73 | 'grouping' => 'contact-fields', |
74 | 'group_bys' => array( | |
75 | 'id' => array('title' => ts('Contact ID')), | |
76 | 'sort_name' => array( | |
77 | 'title' => ts('Contact Name'), | |
6a488035 TO |
78 | ), |
79 | ), | |
c301f76e | 80 | ), |
81 | 'civicrm_email' => array( | |
82 | 'dao' => 'CRM_Core_DAO_Email', | |
83 | 'fields' => array( | |
84 | 'email' => array( | |
85 | 'no_repeat' => TRUE, | |
86 | 'title' => ts('email'), | |
6a488035 | 87 | ), |
c301f76e | 88 | ), |
89 | 'grouping' => 'contact-fields', | |
90 | ), | |
91 | 'civicrm_pledge' => array( | |
92 | 'dao' => 'CRM_Pledge_DAO_Pledge', | |
93 | 'fields' => array( | |
94 | 'id' => array( | |
95 | 'no_display' => TRUE, | |
96 | 'required' => FALSE, | |
6a488035 | 97 | ), |
9e335308 | 98 | 'financial_type_id' => array( |
99 | 'title' => ts('Financial Type'), | |
100 | ), | |
c301f76e | 101 | 'currency' => array( |
102 | 'required' => TRUE, | |
103 | 'no_display' => TRUE, | |
104 | ), | |
105 | 'amount' => array( | |
106 | 'title' => ts('Pledge Amount'), | |
107 | 'required' => TRUE, | |
108 | 'type' => CRM_Utils_Type::T_MONEY, | |
109 | 'statistics' => array( | |
110 | 'sum' => ts('Aggregate Amount Pledged'), | |
111 | 'count' => ts('Pledges'), | |
112 | 'avg' => ts('Average'), | |
9d72cede | 113 | ), |
6a488035 | 114 | ), |
c301f76e | 115 | 'frequency_unit' => array( |
116 | 'title' => ts('Frequency Unit'), | |
117 | ), | |
118 | 'installments' => array( | |
119 | 'title' => ts('Installments'), | |
120 | ), | |
121 | 'pledge_create_date' => array( | |
122 | 'title' => ts('Pledge Made Date'), | |
123 | ), | |
124 | 'start_date' => array( | |
125 | 'title' => ts('Pledge Start Date'), | |
126 | 'type' => CRM_Utils_Type::T_DATE, | |
127 | ), | |
128 | 'end_date' => array( | |
129 | 'title' => ts('Pledge End Date'), | |
130 | 'type' => CRM_Utils_Type::T_DATE, | |
131 | ), | |
132 | 'status_id' => array( | |
133 | 'title' => ts('Pledge Status'), | |
134 | ), | |
6a488035 | 135 | ), |
c301f76e | 136 | 'filters' => array( |
137 | 'pledge_create_date' => array( | |
ccc29f8e | 138 | 'title' => ts('Pledge Made Date'), |
c301f76e | 139 | 'operatorType' => CRM_Report_Form::OP_DATE, |
140 | ), | |
141 | 'pledge_amount' => array( | |
142 | 'title' => ts('Pledged Amount'), | |
143 | 'operatorType' => CRM_Report_Form::OP_INT, | |
144 | ), | |
145 | 'currency' => array( | |
ccc29f8e | 146 | 'title' => ts('Currency'), |
c301f76e | 147 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
148 | 'options' => CRM_Core_OptionGroup::values('currencies_enabled'), | |
149 | 'default' => NULL, | |
150 | 'type' => CRM_Utils_Type::T_STRING, | |
151 | ), | |
152 | 'sid' => array( | |
153 | 'name' => 'status_id', | |
154 | 'title' => ts('Pledge Status'), | |
525ae77a | 155 | 'type' => CRM_Utils_Type::T_INT, |
c301f76e | 156 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
01dac399 | 157 | 'options' => CRM_Core_OptionGroup::values('pledge_status'), |
c301f76e | 158 | ), |
9e335308 | 159 | 'financial_type_id' => array( |
160 | 'title' => ts('Financial Type'), | |
525ae77a | 161 | 'type' => CRM_Utils_Type::T_INT, |
9e335308 | 162 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
163 | 'options' => CRM_Contribute_PseudoConstant::financialType(), | |
164 | ), | |
c301f76e | 165 | ), |
166 | 'group_bys' => array( | |
167 | 'pledge_create_date' => array( | |
168 | 'frequency' => TRUE, | |
169 | 'default' => TRUE, | |
170 | 'chart' => TRUE, | |
171 | ), | |
172 | 'frequency_unit' => array( | |
173 | 'title' => ts('Frequency Unit'), | |
174 | ), | |
175 | 'status_id' => array( | |
176 | 'title' => ts('Pledge Status'), | |
177 | ), | |
a00ead4d BC |
178 | 'financial_type_id' => array( |
179 | 'title' => ts('Financial Type'), | |
180 | ), | |
c301f76e | 181 | ), |
182 | ), | |
183 | 'civicrm_pledge_payment' => array( | |
184 | 'dao' => 'CRM_Pledge_DAO_PledgePayment', | |
185 | 'fields' => array( | |
186 | 'total_paid' => array( | |
187 | 'title' => ts('Total Amount Paid'), | |
188 | 'type' => CRM_Utils_Type::T_STRING, | |
189 | 'dbAlias' => 'sum(pledge_payment_civireport.actual_amount)', | |
9d72cede | 190 | ), |
6a488035 | 191 | ), |
c301f76e | 192 | ), |
193 | ) + $this->addAddressFields(); | |
6a488035 | 194 | |
16e2e80c | 195 | $this->_groupFilter = TRUE; |
6a488035 | 196 | $this->_tagFilter = TRUE; |
9bf1940a | 197 | $this->_currencyColumn = 'civicrm_pledge_currency'; |
6a488035 TO |
198 | parent::__construct(); |
199 | } | |
200 | ||
00be9182 | 201 | public function preProcess() { |
6a488035 TO |
202 | parent::preProcess(); |
203 | } | |
204 | ||
00be9182 | 205 | public function select() { |
6a488035 TO |
206 | parent::select(); |
207 | } | |
208 | ||
00be9182 | 209 | public function from() { |
6a488035 TO |
210 | $this->_from = " |
211 | FROM civicrm_pledge {$this->_aliases['civicrm_pledge']} | |
2f4c2f5d | 212 | LEFT JOIN civicrm_contact {$this->_aliases['civicrm_contact']} |
213 | ON ({$this->_aliases['civicrm_contact']}.id = | |
6a488035 TO |
214 | {$this->_aliases['civicrm_pledge']}.contact_id ) |
215 | {$this->_aclFrom} "; | |
216 | ||
cd2426ba | 217 | $this->joinAddressFromContact(); |
218 | $this->joinEmailFromContact(); | |
6a488035 | 219 | |
9d72cede | 220 | if (!empty($this->_params['fields']['total_paid'])) { |
6a488035 TO |
221 | $this->_from .= " |
222 | LEFT JOIN civicrm_pledge_payment {$this->_aliases['civicrm_pledge_payment']} ON | |
223 | {$this->_aliases['civicrm_pledge']}.id = {$this->_aliases['civicrm_pledge_payment']}.pledge_id | |
224 | AND {$this->_aliases['civicrm_pledge_payment']}.status_id = 1 | |
225 | "; | |
226 | } | |
227 | } | |
228 | ||
00be9182 | 229 | public function groupBy() { |
47c0ee09 | 230 | $this->_groupBy = ""; |
6a488035 TO |
231 | $append = FALSE; |
232 | ||
233 | if (is_array($this->_params['group_bys']) && | |
234 | !empty($this->_params['group_bys']) | |
235 | ) { | |
236 | foreach ($this->_columns as $tableName => $table) { | |
237 | if (array_key_exists('group_bys', $table)) { | |
238 | foreach ($table['group_bys'] as $fieldName => $field) { | |
a7488080 CW |
239 | if (!empty($this->_params['group_bys'][$fieldName])) { |
240 | if (!empty($field['chart'])) { | |
6a488035 TO |
241 | $this->assign('chartSupported', TRUE); |
242 | } | |
243 | ||
9d72cede EM |
244 | if (!empty($table['group_bys'][$fieldName]['frequency']) && |
245 | !empty($this->_params['group_bys_freq'][$fieldName]) | |
246 | ) { | |
6a488035 TO |
247 | |
248 | $append = "YEAR({$field['dbAlias']}),"; | |
249 | if (in_array(strtolower($this->_params['group_bys_freq'][$fieldName]), | |
9d72cede EM |
250 | array('year') |
251 | )) { | |
6a488035 TO |
252 | $append = ''; |
253 | } | |
47c0ee09 | 254 | $this->_groupByArray[] = "$append {$this->_params['group_bys_freq'][$fieldName]}({$field['dbAlias']})"; |
6a488035 TO |
255 | $append = TRUE; |
256 | } | |
257 | else { | |
47c0ee09 | 258 | $this->_groupByArray[] = $field['dbAlias']; |
6a488035 TO |
259 | } |
260 | } | |
261 | } | |
262 | } | |
263 | } | |
264 | ||
265 | if (!empty($this->_statFields) && | |
47c0ee09 | 266 | (($append && count($this->_groupByArray) <= 1) || (!$append)) && |
9d72cede | 267 | !$this->_having |
6a488035 TO |
268 | ) { |
269 | $this->_rollup = " WITH ROLLUP"; | |
270 | } | |
47c0ee09 SL |
271 | $groupBy = $this->_groupByArray; |
272 | $this->_groupBy = "GROUP BY " . implode(', ', $this->_groupByArray); | |
6a488035 TO |
273 | } |
274 | else { | |
d1641c51 | 275 | $groupBy = "{$this->_aliases['civicrm_contact']}.id"; |
276 | $this->_groupBy = "GROUP BY {$groupBy}"; | |
6a488035 | 277 | } |
36d2f4d5 | 278 | $this->_select = CRM_Contact_BAO_Query::appendAnyValueToSelect($this->_selectClauses, $groupBy); |
d1641c51 | 279 | $this->_groupBy .= " {$this->_rollup}"; |
6a488035 TO |
280 | } |
281 | ||
74cf4551 EM |
282 | /** |
283 | * @param $rows | |
284 | * | |
285 | * @return array | |
286 | */ | |
00be9182 | 287 | public function statistics(&$rows) { |
6a488035 TO |
288 | $statistics = parent::statistics($rows); |
289 | ||
290 | if (!$this->_having) { | |
291 | $select = " | |
292 | SELECT COUNT({$this->_aliases['civicrm_pledge']}.amount ) as count, | |
293 | SUM({$this->_aliases['civicrm_pledge']}.amount ) as amount, | |
294 | ROUND(AVG({$this->_aliases['civicrm_pledge']}.amount), 2) as avg | |
295 | "; | |
296 | ||
297 | $sql = "{$select} {$this->_from} {$this->_where}"; | |
9bf1940a | 298 | |
6a488035 TO |
299 | $dao = CRM_Core_DAO::executeQuery($sql); |
300 | ||
301 | if ($dao->fetch()) { | |
302 | $statistics['count']['amount'] = array( | |
303 | 'value' => $dao->amount, | |
ccc29f8e | 304 | 'title' => ts('Total Pledged'), |
6a488035 TO |
305 | 'type' => CRM_Utils_Type::T_MONEY, |
306 | ); | |
307 | $statistics['count']['count '] = array( | |
308 | 'value' => $dao->count, | |
ccc29f8e | 309 | 'title' => ts('Total No Pledges'), |
6a488035 TO |
310 | ); |
311 | $statistics['count']['avg '] = array( | |
312 | 'value' => $dao->avg, | |
ccc29f8e | 313 | 'title' => ts('Average'), |
6a488035 TO |
314 | 'type' => CRM_Utils_Type::T_MONEY, |
315 | ); | |
316 | } | |
317 | } | |
318 | return $statistics; | |
319 | } | |
320 | ||
00be9182 | 321 | public function where() { |
6a488035 TO |
322 | $clauses = array(); |
323 | foreach ($this->_columns as $tableName => $table) { | |
324 | if (array_key_exists('filters', $table)) { | |
325 | foreach ($table['filters'] as $fieldName => $field) { | |
326 | $clause = NULL; | |
327 | if (CRM_Utils_Array::value('type', $field) & CRM_Utils_Type::T_DATE) { | |
328 | $relative = CRM_Utils_Array::value("{$fieldName}_relative", $this->_params); | |
9d72cede EM |
329 | $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params); |
330 | $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params); | |
6a488035 TO |
331 | |
332 | if ($relative || $from || $to) { | |
333 | $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']); | |
334 | } | |
335 | } | |
336 | else { | |
337 | $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params); | |
338 | if ($op) { | |
339 | $clause = $this->whereClause($field, | |
340 | $op, | |
341 | CRM_Utils_Array::value("{$fieldName}_value", | |
342 | $this->_params | |
343 | ), | |
344 | CRM_Utils_Array::value("{$fieldName}_min", | |
345 | $this->_params | |
346 | ), | |
347 | CRM_Utils_Array::value("{$fieldName}_max", | |
348 | $this->_params | |
349 | ) | |
350 | ); | |
351 | } | |
352 | } | |
353 | ||
354 | if (!empty($clause)) { | |
355 | $clauses[] = $clause; | |
356 | } | |
357 | } | |
358 | } | |
359 | } | |
360 | if (empty($clauses)) { | |
361 | $this->_where = "WHERE ({$this->_aliases['civicrm_pledge']}.is_test=0 ) "; | |
362 | } | |
363 | else { | |
2f4c2f5d | 364 | $this->_where = "WHERE ({$this->_aliases['civicrm_pledge']}.is_test=0 ) AND |
6a488035 TO |
365 | " . implode(' AND ', $clauses); |
366 | } | |
367 | ||
368 | if ($this->_aclWhere) { | |
369 | $this->_where .= " AND {$this->_aclWhere} "; | |
370 | } | |
371 | } | |
372 | ||
00be9182 | 373 | public function postProcess() { |
6a488035 TO |
374 | parent::postProcess(); |
375 | } | |
376 | ||
74cf4551 | 377 | /** |
ced9bfed EM |
378 | * Alter display of rows. |
379 | * | |
380 | * Iterate through the rows retrieved via SQL and make changes for display purposes, | |
381 | * such as rendering contacts as links. | |
382 | * | |
383 | * @param array $rows | |
384 | * Rows generated by SQL, with an array for each row. | |
74cf4551 | 385 | */ |
00be9182 | 386 | public function alterDisplay(&$rows) { |
9d72cede EM |
387 | $entryFound = FALSE; |
388 | $checkList = array(); | |
6a488035 | 389 | $display_flag = $prev_cid = $cid = 0; |
6a488035 TO |
390 | foreach ($rows as $rowNum => $row) { |
391 | ||
392 | // convert display name to links | |
393 | if (array_key_exists('civicrm_contact_sort_name', $row) && | |
394 | array_key_exists('civicrm_contact_id', $row) | |
395 | ) { | |
396 | $url = CRM_Utils_System::url("civicrm/contact/view", | |
397 | 'reset=1&cid=' . $row['civicrm_contact_id'], | |
398 | $this->_absoluteUrl | |
399 | ); | |
400 | $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url; | |
401 | $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Contact Summary for this Contact."); | |
402 | $entryFound = TRUE; | |
403 | } | |
404 | ||
9e335308 | 405 | if (array_key_exists('civicrm_pledge_financial_type_id', $row)) { |
406 | if ($value = $row['civicrm_pledge_financial_type_id']) { | |
407 | $rows[$rowNum]['civicrm_pledge_financial_type_id'] = CRM_Contribute_PseudoConstant::financialType($value, FALSE); | |
408 | } | |
409 | $entryFound = TRUE; | |
410 | } | |
411 | ||
6a488035 TO |
412 | //handle status id |
413 | if (array_key_exists('civicrm_pledge_status_id', $row)) { | |
414 | if ($value = $row['civicrm_pledge_status_id']) { | |
01dac399 | 415 | $rows[$rowNum]['civicrm_pledge_status_id'] = CRM_Core_PseudoConstant::getLabel('CRM_Pledge_BAO_Pledge', 'status_id', $value); |
6a488035 TO |
416 | } |
417 | $entryFound = TRUE; | |
418 | } | |
419 | ||
420 | $entryFound = $this->alterDisplayAddressFields($row, $rows, $rowNum, 'pledge/summary', 'List all pledge(s) for this ') ? TRUE : $entryFound; | |
421 | ||
422 | // skip looking further in rows, if first row itself doesn't | |
423 | // have the column we need | |
424 | if (!$entryFound) { | |
425 | break; | |
426 | } | |
427 | } | |
428 | } | |
96025800 | 429 | |
6a488035 | 430 | } |