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