3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 class CRM_Report_Form_Pledge_Summary
extends CRM_Report_Form
{
19 protected $_summary = NULL;
20 protected $_totalPaid = FALSE;
21 protected $_customGroupExtends = ['Pledge', 'Individual'];
22 protected $_customGroupGroupBy = TRUE;
25 * This report has not been optimised for group filtering.
27 * The functionality for group filtering has been improved but not
28 * all reports have been adjusted to take care of it. This report has not
29 * and will run an inefficient query until fixed.
32 * @see https://issues.civicrm.org/jira/browse/CRM-19170
34 protected $groupFilterNotOptimised = TRUE;
39 public function __construct() {
41 'civicrm_contact' => [
42 'dao' => 'CRM_Contact_DAO_Contact',
45 'title' => ts('Contact Name'),
48 'postal_greeting_display' => ['title' => ts('Postal Greeting')],
54 'grouping' => 'contact-fields',
56 'id' => ['title' => ts('Contact ID')],
58 'title' => ts('Contact Name'),
63 'dao' => 'CRM_Core_DAO_Email',
67 'title' => ts('email'),
70 'grouping' => 'contact-fields',
73 'dao' => 'CRM_Pledge_DAO_Pledge',
79 'financial_type_id' => [
80 'title' => ts('Financial Type'),
87 'title' => ts('Pledge Amount'),
89 'type' => CRM_Utils_Type
::T_MONEY
,
91 'sum' => ts('Aggregate Amount Pledged'),
92 'count' => ts('Pledges'),
93 'avg' => ts('Average'),
97 'title' => ts('Frequency Unit'),
100 'title' => ts('Installments'),
102 'pledge_create_date' => [
103 'title' => ts('Pledge Made Date'),
106 'title' => ts('Pledge Start Date'),
107 'type' => CRM_Utils_Type
::T_DATE
,
110 'title' => ts('Pledge End Date'),
111 'type' => CRM_Utils_Type
::T_DATE
,
114 'title' => ts('Pledge Status'),
118 'pledge_create_date' => [
119 'title' => ts('Pledge Made Date'),
120 'operatorType' => CRM_Report_Form
::OP_DATE
,
123 'title' => ts('Pledged Amount'),
124 'operatorType' => CRM_Report_Form
::OP_INT
,
127 'title' => ts('Currency'),
128 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
129 'options' => CRM_Core_OptionGroup
::values('currencies_enabled'),
131 'type' => CRM_Utils_Type
::T_STRING
,
134 'name' => 'status_id',
135 'title' => ts('Pledge Status'),
136 'type' => CRM_Utils_Type
::T_INT
,
137 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
138 'options' => CRM_Core_OptionGroup
::values('pledge_status'),
140 'financial_type_id' => [
141 'title' => ts('Financial Type'),
142 'type' => CRM_Utils_Type
::T_INT
,
143 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
144 'options' => CRM_Contribute_PseudoConstant
::financialType(),
148 'pledge_create_date' => [
153 'frequency_unit' => [
154 'title' => ts('Frequency Unit'),
157 'title' => ts('Pledge Status'),
159 'financial_type_id' => [
160 'title' => ts('Financial Type'),
164 'civicrm_pledge_payment' => [
165 'dao' => 'CRM_Pledge_DAO_PledgePayment',
168 'title' => ts('Total Amount Paid'),
169 'type' => CRM_Utils_Type
::T_STRING
,
170 'dbAlias' => 'sum(pledge_payment_civireport.actual_amount)',
174 ] +
$this->addAddressFields();
176 $this->_groupFilter
= TRUE;
177 $this->_tagFilter
= TRUE;
178 $this->_currencyColumn
= 'civicrm_pledge_currency';
179 parent
::__construct();
182 public function preProcess() {
183 parent
::preProcess();
186 public function select() {
190 public function from() {
192 FROM civicrm_pledge {$this->_aliases['civicrm_pledge']}
193 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_contact']}
194 ON ({$this->_aliases['civicrm_contact']}.id =
195 {$this->_aliases['civicrm_pledge']}.contact_id )
198 $this->joinAddressFromContact();
199 $this->joinEmailFromContact();
201 if (!empty($this->_params
['fields']['total_paid'])) {
203 LEFT JOIN civicrm_pledge_payment {$this->_aliases['civicrm_pledge_payment']} ON
204 {$this->_aliases['civicrm_pledge']}.id = {$this->_aliases['civicrm_pledge_payment']}.pledge_id
205 AND {$this->_aliases['civicrm_pledge_payment']}.status_id = 1
210 public function groupBy() {
211 $this->_groupBy
= "";
214 if (is_array($this->_params
['group_bys']) &&
215 !empty($this->_params
['group_bys'])
217 foreach ($this->_columns
as $tableName => $table) {
218 if (array_key_exists('group_bys', $table)) {
219 foreach ($table['group_bys'] as $fieldName => $field) {
220 if (!empty($this->_params
['group_bys'][$fieldName])) {
221 if (!empty($field['chart'])) {
222 $this->assign('chartSupported', TRUE);
225 if (!empty($table['group_bys'][$fieldName]['frequency']) &&
226 !empty($this->_params
['group_bys_freq'][$fieldName])
229 $append = "YEAR({$field['dbAlias']}),";
230 if (in_array(strtolower($this->_params
['group_bys_freq'][$fieldName]),
235 $this->_groupByArray
[] = "$append {$this->_params['group_bys_freq'][$fieldName]}({$field['dbAlias']})";
239 $this->_groupByArray
[] = $field['dbAlias'];
246 if (!empty($this->_statFields
) &&
247 (($append && count($this->_groupByArray
) <= 1) ||
(!$append)) &&
250 $this->_rollup
= " WITH ROLLUP";
252 $groupBy = $this->_groupByArray
;
253 $this->_groupBy
= "GROUP BY " . implode(', ', $this->_groupByArray
);
256 $groupBy = "{$this->_aliases['civicrm_contact']}.id";
257 $this->_groupBy
= "GROUP BY {$groupBy}";
259 $this->_select
= CRM_Contact_BAO_Query
::appendAnyValueToSelect($this->_selectClauses
, $groupBy);
260 $this->_groupBy
.= " {$this->_rollup}";
268 public function statistics(&$rows) {
269 $statistics = parent
::statistics($rows);
271 if (!$this->_having
) {
273 SELECT COUNT({$this->_aliases['civicrm_pledge']}.amount ) as count,
274 SUM({$this->_aliases['civicrm_pledge']}.amount ) as amount,
275 ROUND(AVG({$this->_aliases['civicrm_pledge']}.amount), 2) as avg
278 $sql = "{$select} {$this->_from} {$this->_where}";
280 $dao = CRM_Core_DAO
::executeQuery($sql);
283 $statistics['count']['amount'] = [
284 'value' => $dao->amount
,
285 'title' => ts('Total Pledged'),
286 'type' => CRM_Utils_Type
::T_MONEY
,
288 $statistics['count']['count '] = [
289 'value' => $dao->count
,
290 'title' => ts('Total No Pledges'),
292 $statistics['count']['avg '] = [
293 'value' => $dao->avg
,
294 'title' => ts('Average'),
295 'type' => CRM_Utils_Type
::T_MONEY
,
302 public function where() {
304 foreach ($this->_columns
as $tableName => $table) {
305 if (array_key_exists('filters', $table)) {
306 foreach ($table['filters'] as $fieldName => $field) {
308 if (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
) {
309 $relative = $this->_params
["{$fieldName}_relative"] ??
NULL;
310 $from = $this->_params
["{$fieldName}_from"] ??
NULL;
311 $to = $this->_params
["{$fieldName}_to"] ??
NULL;
313 if ($relative ||
$from ||
$to) {
314 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
318 $op = $this->_params
["{$fieldName}_op"] ??
NULL;
320 $clause = $this->whereClause($field,
322 CRM_Utils_Array
::value("{$fieldName}_value",
325 CRM_Utils_Array
::value("{$fieldName}_min",
328 CRM_Utils_Array
::value("{$fieldName}_max",
335 if (!empty($clause)) {
336 $clauses[] = $clause;
341 if (empty($clauses)) {
342 $this->_where
= "WHERE ({$this->_aliases['civicrm_pledge']}.is_test=0 ) ";
345 $this->_where
= "WHERE ({$this->_aliases['civicrm_pledge']}.is_test=0 ) AND
346 " . implode(' AND ', $clauses);
349 if ($this->_aclWhere
) {
350 $this->_where
.= " AND {$this->_aclWhere} ";
354 public function postProcess() {
355 parent
::postProcess();
359 * Alter display of rows.
361 * Iterate through the rows retrieved via SQL and make changes for display purposes,
362 * such as rendering contacts as links.
365 * Rows generated by SQL, with an array for each row.
367 public function alterDisplay(&$rows) {
370 $display_flag = $prev_cid = $cid = 0;
371 foreach ($rows as $rowNum => $row) {
373 // convert display name to links
374 if (array_key_exists('civicrm_contact_sort_name', $row) &&
375 array_key_exists('civicrm_contact_id', $row)
377 $url = CRM_Utils_System
::url("civicrm/contact/view",
378 'reset=1&cid=' . $row['civicrm_contact_id'],
381 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
382 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Contact Summary for this Contact.");
386 if (array_key_exists('civicrm_pledge_financial_type_id', $row)) {
387 if ($value = $row['civicrm_pledge_financial_type_id']) {
388 $rows[$rowNum]['civicrm_pledge_financial_type_id'] = CRM_Contribute_PseudoConstant
::financialType($value, FALSE);
394 if (array_key_exists('civicrm_pledge_status_id', $row)) {
395 if ($value = $row['civicrm_pledge_status_id']) {
396 $rows[$rowNum]['civicrm_pledge_status_id'] = CRM_Core_PseudoConstant
::getLabel('CRM_Pledge_BAO_Pledge', 'status_id', $value);
401 $entryFound = $this->alterDisplayAddressFields($row, $rows, $rowNum, 'pledge/summary', 'List all pledge(s) for this ') ?
TRUE : $entryFound;
403 // skip looking further in rows, if first row itself doesn't
404 // have the column we need