3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2019
35 class CRM_Report_Form_Pledge_Summary
extends CRM_Report_Form
{
37 protected $_summary = NULL;
38 protected $_totalPaid = FALSE;
39 protected $_customGroupExtends = array('Pledge', 'Individual');
40 protected $_customGroupGroupBy = TRUE;
43 * This report has not been optimised for group filtering.
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.
53 protected $groupFilterNotOptimised = TRUE;
58 public function __construct() {
59 $this->_columns
= array(
60 'civicrm_contact' => array(
61 'dao' => 'CRM_Contact_DAO_Contact',
64 'title' => ts('Contact Name'),
67 'postal_greeting_display' => array('title' => ts('Postal Greeting')),
73 'grouping' => 'contact-fields',
75 'id' => array('title' => ts('Contact ID')),
77 'title' => ts('Contact Name'),
81 'civicrm_email' => array(
82 'dao' => 'CRM_Core_DAO_Email',
86 'title' => ts('email'),
89 'grouping' => 'contact-fields',
91 'civicrm_pledge' => array(
92 'dao' => 'CRM_Pledge_DAO_Pledge',
98 'financial_type_id' => array(
99 'title' => ts('Financial Type'),
103 'no_display' => TRUE,
106 'title' => ts('Pledge Amount'),
108 'type' => CRM_Utils_Type
::T_MONEY
,
109 'statistics' => array(
110 'sum' => ts('Aggregate Amount Pledged'),
111 'count' => ts('Pledges'),
112 'avg' => ts('Average'),
115 'frequency_unit' => array(
116 'title' => ts('Frequency Unit'),
118 'installments' => array(
119 'title' => ts('Installments'),
121 'pledge_create_date' => array(
122 'title' => ts('Pledge Made Date'),
124 'start_date' => array(
125 'title' => ts('Pledge Start Date'),
126 'type' => CRM_Utils_Type
::T_DATE
,
129 'title' => ts('Pledge End Date'),
130 'type' => CRM_Utils_Type
::T_DATE
,
132 'status_id' => array(
133 'title' => ts('Pledge Status'),
137 'pledge_create_date' => array(
138 'title' => ts('Pledge Made Date'),
139 'operatorType' => CRM_Report_Form
::OP_DATE
,
141 'pledge_amount' => array(
142 'title' => ts('Pledged Amount'),
143 'operatorType' => CRM_Report_Form
::OP_INT
,
146 'title' => ts('Currency'),
147 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
148 'options' => CRM_Core_OptionGroup
::values('currencies_enabled'),
150 'type' => CRM_Utils_Type
::T_STRING
,
153 'name' => 'status_id',
154 'title' => ts('Pledge Status'),
155 'type' => CRM_Utils_Type
::T_INT
,
156 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
157 'options' => CRM_Core_OptionGroup
::values('pledge_status'),
159 'financial_type_id' => array(
160 'title' => ts('Financial Type'),
161 'type' => CRM_Utils_Type
::T_INT
,
162 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
163 'options' => CRM_Contribute_PseudoConstant
::financialType(),
166 'group_bys' => array(
167 'pledge_create_date' => array(
172 'frequency_unit' => array(
173 'title' => ts('Frequency Unit'),
175 'status_id' => array(
176 'title' => ts('Pledge Status'),
178 'financial_type_id' => array(
179 'title' => ts('Financial Type'),
183 'civicrm_pledge_payment' => array(
184 'dao' => 'CRM_Pledge_DAO_PledgePayment',
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)',
193 ) +
$this->addAddressFields();
195 $this->_groupFilter
= TRUE;
196 $this->_tagFilter
= TRUE;
197 $this->_currencyColumn
= 'civicrm_pledge_currency';
198 parent
::__construct();
201 public function preProcess() {
202 parent
::preProcess();
205 public function select() {
209 public function from() {
211 FROM civicrm_pledge {$this->_aliases['civicrm_pledge']}
212 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_contact']}
213 ON ({$this->_aliases['civicrm_contact']}.id =
214 {$this->_aliases['civicrm_pledge']}.contact_id )
217 $this->joinAddressFromContact();
218 $this->joinEmailFromContact();
220 if (!empty($this->_params
['fields']['total_paid'])) {
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
229 public function groupBy() {
230 $this->_groupBy
= "";
233 if (is_array($this->_params
['group_bys']) &&
234 !empty($this->_params
['group_bys'])
236 foreach ($this->_columns
as $tableName => $table) {
237 if (array_key_exists('group_bys', $table)) {
238 foreach ($table['group_bys'] as $fieldName => $field) {
239 if (!empty($this->_params
['group_bys'][$fieldName])) {
240 if (!empty($field['chart'])) {
241 $this->assign('chartSupported', TRUE);
244 if (!empty($table['group_bys'][$fieldName]['frequency']) &&
245 !empty($this->_params
['group_bys_freq'][$fieldName])
248 $append = "YEAR({$field['dbAlias']}),";
249 if (in_array(strtolower($this->_params
['group_bys_freq'][$fieldName]),
254 $this->_groupByArray
[] = "$append {$this->_params['group_bys_freq'][$fieldName]}({$field['dbAlias']})";
258 $this->_groupByArray
[] = $field['dbAlias'];
265 if (!empty($this->_statFields
) &&
266 (($append && count($this->_groupByArray
) <= 1) ||
(!$append)) &&
269 $this->_rollup
= " WITH ROLLUP";
271 $groupBy = $this->_groupByArray
;
272 $this->_groupBy
= "GROUP BY " . implode(', ', $this->_groupByArray
);
275 $groupBy = "{$this->_aliases['civicrm_contact']}.id";
276 $this->_groupBy
= "GROUP BY {$groupBy}";
278 $this->_select
= CRM_Contact_BAO_Query
::appendAnyValueToSelect($this->_selectClauses
, $groupBy);
279 $this->_groupBy
.= " {$this->_rollup}";
287 public function statistics(&$rows) {
288 $statistics = parent
::statistics($rows);
290 if (!$this->_having
) {
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
297 $sql = "{$select} {$this->_from} {$this->_where}";
299 $dao = CRM_Core_DAO
::executeQuery($sql);
302 $statistics['count']['amount'] = array(
303 'value' => $dao->amount
,
304 'title' => ts('Total Pledged'),
305 'type' => CRM_Utils_Type
::T_MONEY
,
307 $statistics['count']['count '] = array(
308 'value' => $dao->count
,
309 'title' => ts('Total No Pledges'),
311 $statistics['count']['avg '] = array(
312 'value' => $dao->avg
,
313 'title' => ts('Average'),
314 'type' => CRM_Utils_Type
::T_MONEY
,
321 public function where() {
323 foreach ($this->_columns
as $tableName => $table) {
324 if (array_key_exists('filters', $table)) {
325 foreach ($table['filters'] as $fieldName => $field) {
327 if (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
) {
328 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
329 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
330 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
332 if ($relative ||
$from ||
$to) {
333 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
337 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
339 $clause = $this->whereClause($field,
341 CRM_Utils_Array
::value("{$fieldName}_value",
344 CRM_Utils_Array
::value("{$fieldName}_min",
347 CRM_Utils_Array
::value("{$fieldName}_max",
354 if (!empty($clause)) {
355 $clauses[] = $clause;
360 if (empty($clauses)) {
361 $this->_where
= "WHERE ({$this->_aliases['civicrm_pledge']}.is_test=0 ) ";
364 $this->_where
= "WHERE ({$this->_aliases['civicrm_pledge']}.is_test=0 ) AND
365 " . implode(' AND ', $clauses);
368 if ($this->_aclWhere
) {
369 $this->_where
.= " AND {$this->_aclWhere} ";
373 public function postProcess() {
374 parent
::postProcess();
378 * Alter display of rows.
380 * Iterate through the rows retrieved via SQL and make changes for display purposes,
381 * such as rendering contacts as links.
384 * Rows generated by SQL, with an array for each row.
386 public function alterDisplay(&$rows) {
388 $checkList = array();
389 $display_flag = $prev_cid = $cid = 0;
390 foreach ($rows as $rowNum => $row) {
392 // convert display name to links
393 if (array_key_exists('civicrm_contact_sort_name', $row) &&
394 array_key_exists('civicrm_contact_id', $row)
396 $url = CRM_Utils_System
::url("civicrm/contact/view",
397 'reset=1&cid=' . $row['civicrm_contact_id'],
400 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
401 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Contact Summary for this Contact.");
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);
413 if (array_key_exists('civicrm_pledge_status_id', $row)) {
414 if ($value = $row['civicrm_pledge_status_id']) {
415 $rows[$rowNum]['civicrm_pledge_status_id'] = CRM_Core_PseudoConstant
::getLabel('CRM_Pledge_BAO_Pledge', 'status_id', $value);
420 $entryFound = $this->alterDisplayAddressFields($row, $rows, $rowNum, 'pledge/summary', 'List all pledge(s) for this ') ?
TRUE : $entryFound;
422 // skip looking further in rows, if first row itself doesn't
423 // have the column we need