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
19 class CRM_Report_Form_Pledge_Summary
extends CRM_Report_Form
{
21 protected $_summary = NULL;
22 protected $_totalPaid = FALSE;
23 protected $_customGroupExtends = ['Pledge', 'Individual'];
24 protected $_customGroupGroupBy = TRUE;
27 * This report has not been optimised for group filtering.
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.
37 protected $groupFilterNotOptimised = TRUE;
42 public function __construct() {
44 'civicrm_contact' => [
45 'dao' => 'CRM_Contact_DAO_Contact',
48 'title' => ts('Contact Name'),
51 'postal_greeting_display' => ['title' => ts('Postal Greeting')],
57 'grouping' => 'contact-fields',
59 'id' => ['title' => ts('Contact ID')],
61 'title' => ts('Contact Name'),
66 'dao' => 'CRM_Core_DAO_Email',
70 'title' => ts('email'),
73 'grouping' => 'contact-fields',
76 'dao' => 'CRM_Pledge_DAO_Pledge',
82 'financial_type_id' => [
83 'title' => ts('Financial Type'),
90 'title' => ts('Pledge Amount'),
92 'type' => CRM_Utils_Type
::T_MONEY
,
94 'sum' => ts('Aggregate Amount Pledged'),
95 'count' => ts('Pledges'),
96 'avg' => ts('Average'),
100 'title' => ts('Frequency Unit'),
103 'title' => ts('Installments'),
105 'pledge_create_date' => [
106 'title' => ts('Pledge Made Date'),
109 'title' => ts('Pledge Start Date'),
110 'type' => CRM_Utils_Type
::T_DATE
,
113 'title' => ts('Pledge End Date'),
114 'type' => CRM_Utils_Type
::T_DATE
,
117 'title' => ts('Pledge Status'),
121 'pledge_create_date' => [
122 'title' => ts('Pledge Made Date'),
123 'operatorType' => CRM_Report_Form
::OP_DATE
,
126 'title' => ts('Pledged Amount'),
127 'operatorType' => CRM_Report_Form
::OP_INT
,
130 'title' => ts('Currency'),
131 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
132 'options' => CRM_Core_OptionGroup
::values('currencies_enabled'),
134 'type' => CRM_Utils_Type
::T_STRING
,
137 'name' => 'status_id',
138 'title' => ts('Pledge Status'),
139 'type' => CRM_Utils_Type
::T_INT
,
140 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
141 'options' => CRM_Core_OptionGroup
::values('pledge_status'),
143 'financial_type_id' => [
144 'title' => ts('Financial Type'),
145 'type' => CRM_Utils_Type
::T_INT
,
146 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
147 'options' => CRM_Contribute_PseudoConstant
::financialType(),
151 'pledge_create_date' => [
156 'frequency_unit' => [
157 'title' => ts('Frequency Unit'),
160 'title' => ts('Pledge Status'),
162 'financial_type_id' => [
163 'title' => ts('Financial Type'),
167 'civicrm_pledge_payment' => [
168 'dao' => 'CRM_Pledge_DAO_PledgePayment',
171 'title' => ts('Total Amount Paid'),
172 'type' => CRM_Utils_Type
::T_STRING
,
173 'dbAlias' => 'sum(pledge_payment_civireport.actual_amount)',
177 ] +
$this->addAddressFields();
179 $this->_groupFilter
= TRUE;
180 $this->_tagFilter
= TRUE;
181 $this->_currencyColumn
= 'civicrm_pledge_currency';
182 parent
::__construct();
185 public function preProcess() {
186 parent
::preProcess();
189 public function select() {
193 public function from() {
195 FROM civicrm_pledge {$this->_aliases['civicrm_pledge']}
196 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_contact']}
197 ON ({$this->_aliases['civicrm_contact']}.id =
198 {$this->_aliases['civicrm_pledge']}.contact_id )
201 $this->joinAddressFromContact();
202 $this->joinEmailFromContact();
204 if (!empty($this->_params
['fields']['total_paid'])) {
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
213 public function groupBy() {
214 $this->_groupBy
= "";
217 if (is_array($this->_params
['group_bys']) &&
218 !empty($this->_params
['group_bys'])
220 foreach ($this->_columns
as $tableName => $table) {
221 if (array_key_exists('group_bys', $table)) {
222 foreach ($table['group_bys'] as $fieldName => $field) {
223 if (!empty($this->_params
['group_bys'][$fieldName])) {
224 if (!empty($field['chart'])) {
225 $this->assign('chartSupported', TRUE);
228 if (!empty($table['group_bys'][$fieldName]['frequency']) &&
229 !empty($this->_params
['group_bys_freq'][$fieldName])
232 $append = "YEAR({$field['dbAlias']}),";
233 if (in_array(strtolower($this->_params
['group_bys_freq'][$fieldName]),
238 $this->_groupByArray
[] = "$append {$this->_params['group_bys_freq'][$fieldName]}({$field['dbAlias']})";
242 $this->_groupByArray
[] = $field['dbAlias'];
249 if (!empty($this->_statFields
) &&
250 (($append && count($this->_groupByArray
) <= 1) ||
(!$append)) &&
253 $this->_rollup
= " WITH ROLLUP";
255 $groupBy = $this->_groupByArray
;
256 $this->_groupBy
= "GROUP BY " . implode(', ', $this->_groupByArray
);
259 $groupBy = "{$this->_aliases['civicrm_contact']}.id";
260 $this->_groupBy
= "GROUP BY {$groupBy}";
262 $this->_select
= CRM_Contact_BAO_Query
::appendAnyValueToSelect($this->_selectClauses
, $groupBy);
263 $this->_groupBy
.= " {$this->_rollup}";
271 public function statistics(&$rows) {
272 $statistics = parent
::statistics($rows);
274 if (!$this->_having
) {
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
281 $sql = "{$select} {$this->_from} {$this->_where}";
283 $dao = CRM_Core_DAO
::executeQuery($sql);
286 $statistics['count']['amount'] = [
287 'value' => $dao->amount
,
288 'title' => ts('Total Pledged'),
289 'type' => CRM_Utils_Type
::T_MONEY
,
291 $statistics['count']['count '] = [
292 'value' => $dao->count
,
293 'title' => ts('Total No Pledges'),
295 $statistics['count']['avg '] = [
296 'value' => $dao->avg
,
297 'title' => ts('Average'),
298 'type' => CRM_Utils_Type
::T_MONEY
,
305 public function where() {
307 foreach ($this->_columns
as $tableName => $table) {
308 if (array_key_exists('filters', $table)) {
309 foreach ($table['filters'] as $fieldName => $field) {
311 if (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
) {
312 $relative = $this->_params
["{$fieldName}_relative"] ??
NULL;
313 $from = $this->_params
["{$fieldName}_from"] ??
NULL;
314 $to = $this->_params
["{$fieldName}_to"] ??
NULL;
316 if ($relative ||
$from ||
$to) {
317 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
321 $op = $this->_params
["{$fieldName}_op"] ??
NULL;
323 $clause = $this->whereClause($field,
325 CRM_Utils_Array
::value("{$fieldName}_value",
328 CRM_Utils_Array
::value("{$fieldName}_min",
331 CRM_Utils_Array
::value("{$fieldName}_max",
338 if (!empty($clause)) {
339 $clauses[] = $clause;
344 if (empty($clauses)) {
345 $this->_where
= "WHERE ({$this->_aliases['civicrm_pledge']}.is_test=0 ) ";
348 $this->_where
= "WHERE ({$this->_aliases['civicrm_pledge']}.is_test=0 ) AND
349 " . implode(' AND ', $clauses);
352 if ($this->_aclWhere
) {
353 $this->_where
.= " AND {$this->_aclWhere} ";
357 public function postProcess() {
358 parent
::postProcess();
362 * Alter display of rows.
364 * Iterate through the rows retrieved via SQL and make changes for display purposes,
365 * such as rendering contacts as links.
368 * Rows generated by SQL, with an array for each row.
370 public function alterDisplay(&$rows) {
373 $display_flag = $prev_cid = $cid = 0;
374 foreach ($rows as $rowNum => $row) {
376 // convert display name to links
377 if (array_key_exists('civicrm_contact_sort_name', $row) &&
378 array_key_exists('civicrm_contact_id', $row)
380 $url = CRM_Utils_System
::url("civicrm/contact/view",
381 'reset=1&cid=' . $row['civicrm_contact_id'],
384 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
385 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Contact Summary for this Contact.");
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);
397 if (array_key_exists('civicrm_pledge_status_id', $row)) {
398 if ($value = $row['civicrm_pledge_status_id']) {
399 $rows[$rowNum]['civicrm_pledge_status_id'] = CRM_Core_PseudoConstant
::getLabel('CRM_Pledge_BAO_Pledge', 'status_id', $value);
404 $entryFound = $this->alterDisplayAddressFields($row, $rows, $rowNum, 'pledge/summary', 'List all pledge(s) for this ') ?
TRUE : $entryFound;
406 // skip looking further in rows, if first row itself doesn't
407 // have the column we need