3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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-2016
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;
41 protected $_addressField = FALSE;
42 protected $_emailField = FALSE;
45 * This report has not been optimised for group filtering.
47 * The functionality for group filtering has been improved but not
48 * all reports have been adjusted to take care of it. This report has not
49 * and will run an inefficient query until fixed.
55 protected $groupFilterNotOptimised = TRUE;
60 public function __construct() {
61 $this->_columns
= array(
62 'civicrm_contact' => array(
63 'dao' => 'CRM_Contact_DAO_Contact',
66 'title' => ts('Contact Name'),
69 'postal_greeting_display' => array('title' => ts('Postal Greeting')),
75 'grouping' => 'contact-fields',
77 'id' => array('title' => ts('Contact ID')),
79 'title' => ts('Contact Name'),
83 'civicrm_email' => array(
84 'dao' => 'CRM_Core_DAO_Email',
88 'title' => ts('email'),
91 'grouping' => 'contact-fields',
93 'civicrm_pledge' => array(
94 'dao' => 'CRM_Pledge_DAO_Pledge',
100 'financial_type_id' => array(
101 'title' => ts('Financial Type'),
105 'no_display' => TRUE,
108 'title' => ts('Pledge Amount'),
110 'type' => CRM_Utils_Type
::T_MONEY
,
111 'statistics' => array(
112 'sum' => ts('Aggregate Amount Pledged'),
113 'count' => ts('Pledges'),
114 'avg' => ts('Average'),
117 'frequency_unit' => array(
118 'title' => ts('Frequency Unit'),
120 'installments' => array(
121 'title' => ts('Installments'),
123 'pledge_create_date' => array(
124 'title' => ts('Pledge Made Date'),
126 'start_date' => array(
127 'title' => ts('Pledge Start Date'),
128 'type' => CRM_Utils_Type
::T_DATE
,
131 'title' => ts('Pledge End Date'),
132 'type' => CRM_Utils_Type
::T_DATE
,
134 'status_id' => array(
135 'title' => ts('Pledge Status'),
139 'pledge_create_date' => array(
140 'title' => ts('Pledge Made Date'),
141 'operatorType' => CRM_Report_Form
::OP_DATE
,
143 'pledge_amount' => array(
144 'title' => ts('Pledged Amount'),
145 'operatorType' => CRM_Report_Form
::OP_INT
,
148 'title' => ts('Currency'),
149 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
150 'options' => CRM_Core_OptionGroup
::values('currencies_enabled'),
152 'type' => CRM_Utils_Type
::T_STRING
,
155 'name' => 'status_id',
156 'title' => ts('Pledge Status'),
157 'type' => CRM_Utils_Type
::T_INT
,
158 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
159 'options' => CRM_Core_OptionGroup
::values('contribution_status'),
161 'financial_type_id' => array(
162 'title' => ts('Financial Type'),
163 'type' => CRM_Utils_Type
::T_INT
,
164 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
165 'options' => CRM_Contribute_PseudoConstant
::financialType(),
168 'group_bys' => array(
169 'pledge_create_date' => array(
174 'frequency_unit' => array(
175 'title' => ts('Frequency Unit'),
177 'status_id' => array(
178 'title' => ts('Pledge Status'),
180 'financial_type_id' => array(
181 'title' => ts('Financial Type'),
185 'civicrm_pledge_payment' => array(
186 'dao' => 'CRM_Pledge_DAO_PledgePayment',
188 'total_paid' => array(
189 'title' => ts('Total Amount Paid'),
190 'type' => CRM_Utils_Type
::T_STRING
,
191 'dbAlias' => 'sum(pledge_payment_civireport.actual_amount)',
195 ) +
$this->addAddressFields();
197 $this->_groupFilter
= TRUE;
198 $this->_tagFilter
= TRUE;
199 $this->_currencyColumn
= 'civicrm_pledge_currency';
200 parent
::__construct();
203 public function preProcess() {
204 parent
::preProcess();
207 public function select() {
211 public function from() {
213 FROM civicrm_pledge {$this->_aliases['civicrm_pledge']}
214 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_contact']}
215 ON ({$this->_aliases['civicrm_contact']}.id =
216 {$this->_aliases['civicrm_pledge']}.contact_id )
219 // include address field if address column is to be included
220 if ($this->_addressField
) {
222 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
223 ON ({$this->_aliases['civicrm_contact']}.id =
224 {$this->_aliases['civicrm_address']}.contact_id) AND
225 {$this->_aliases['civicrm_address']}.is_primary = 1\n";
228 // include email field if email column is to be included
229 if ($this->_emailField
) {
231 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
232 ON ({$this->_aliases['civicrm_contact']}.id =
233 {$this->_aliases['civicrm_email']}.contact_id) AND
234 {$this->_aliases['civicrm_email']}.is_primary = 1\n";
237 if (!empty($this->_params
['fields']['total_paid'])) {
239 LEFT JOIN civicrm_pledge_payment {$this->_aliases['civicrm_pledge_payment']} ON
240 {$this->_aliases['civicrm_pledge']}.id = {$this->_aliases['civicrm_pledge_payment']}.pledge_id
241 AND {$this->_aliases['civicrm_pledge_payment']}.status_id = 1
246 public function groupBy() {
247 $this->_groupBy
= "";
250 if (is_array($this->_params
['group_bys']) &&
251 !empty($this->_params
['group_bys'])
253 foreach ($this->_columns
as $tableName => $table) {
254 if (array_key_exists('group_bys', $table)) {
255 foreach ($table['group_bys'] as $fieldName => $field) {
256 if (!empty($this->_params
['group_bys'][$fieldName])) {
257 if (!empty($field['chart'])) {
258 $this->assign('chartSupported', TRUE);
261 if (!empty($table['group_bys'][$fieldName]['frequency']) &&
262 !empty($this->_params
['group_bys_freq'][$fieldName])
265 $append = "YEAR({$field['dbAlias']}),";
266 if (in_array(strtolower($this->_params
['group_bys_freq'][$fieldName]),
271 $this->_groupBy
[] = "$append {$this->_params['group_bys_freq'][$fieldName]}({$field['dbAlias']})";
275 $this->_groupBy
[] = $field['dbAlias'];
282 if (!empty($this->_statFields
) &&
283 (($append && count($this->_groupBy
) <= 1) ||
(!$append)) &&
286 $this->_rollup
= " WITH ROLLUP";
288 $groupBy = $this->_groupBy
;
289 $this->_groupBy
= "GROUP BY " . implode(', ', $this->_groupBy
);
292 $groupBy = "{$this->_aliases['civicrm_contact']}.id";
293 $this->_groupBy
= "GROUP BY {$groupBy}";
295 $this->_select
= CRM_Contact_BAO_Query
::appendAnyValueToSelect($this->_selectClauses
, $groupBy);
296 $this->_groupBy
.= " {$this->_rollup}";
304 public function statistics(&$rows) {
305 $statistics = parent
::statistics($rows);
307 if (!$this->_having
) {
309 SELECT COUNT({$this->_aliases['civicrm_pledge']}.amount ) as count,
310 SUM({$this->_aliases['civicrm_pledge']}.amount ) as amount,
311 ROUND(AVG({$this->_aliases['civicrm_pledge']}.amount), 2) as avg
314 $sql = "{$select} {$this->_from} {$this->_where}";
316 $dao = CRM_Core_DAO
::executeQuery($sql);
319 $statistics['count']['amount'] = array(
320 'value' => $dao->amount
,
321 'title' => ts('Total Pledged'),
322 'type' => CRM_Utils_Type
::T_MONEY
,
324 $statistics['count']['count '] = array(
325 'value' => $dao->count
,
326 'title' => ts('Total No Pledges'),
328 $statistics['count']['avg '] = array(
329 'value' => $dao->avg
,
330 'title' => ts('Average'),
331 'type' => CRM_Utils_Type
::T_MONEY
,
338 public function where() {
340 foreach ($this->_columns
as $tableName => $table) {
341 if (array_key_exists('filters', $table)) {
342 foreach ($table['filters'] as $fieldName => $field) {
344 if (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
) {
345 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
346 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
347 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
349 if ($relative ||
$from ||
$to) {
350 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
354 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
356 $clause = $this->whereClause($field,
358 CRM_Utils_Array
::value("{$fieldName}_value",
361 CRM_Utils_Array
::value("{$fieldName}_min",
364 CRM_Utils_Array
::value("{$fieldName}_max",
371 if (!empty($clause)) {
372 $clauses[] = $clause;
377 if (empty($clauses)) {
378 $this->_where
= "WHERE ({$this->_aliases['civicrm_pledge']}.is_test=0 ) ";
381 $this->_where
= "WHERE ({$this->_aliases['civicrm_pledge']}.is_test=0 ) AND
382 " . implode(' AND ', $clauses);
385 if ($this->_aclWhere
) {
386 $this->_where
.= " AND {$this->_aclWhere} ";
390 public function postProcess() {
391 parent
::postProcess();
395 * Alter display of rows.
397 * Iterate through the rows retrieved via SQL and make changes for display purposes,
398 * such as rendering contacts as links.
401 * Rows generated by SQL, with an array for each row.
403 public function alterDisplay(&$rows) {
405 $checkList = array();
406 $display_flag = $prev_cid = $cid = 0;
407 foreach ($rows as $rowNum => $row) {
409 // convert display name to links
410 if (array_key_exists('civicrm_contact_sort_name', $row) &&
411 array_key_exists('civicrm_contact_id', $row)
413 $url = CRM_Utils_System
::url("civicrm/contact/view",
414 'reset=1&cid=' . $row['civicrm_contact_id'],
417 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
418 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Contact Summary for this Contact.");
422 if (array_key_exists('civicrm_pledge_financial_type_id', $row)) {
423 if ($value = $row['civicrm_pledge_financial_type_id']) {
424 $rows[$rowNum]['civicrm_pledge_financial_type_id'] = CRM_Contribute_PseudoConstant
::financialType($value, FALSE);
430 if (array_key_exists('civicrm_pledge_status_id', $row)) {
431 if ($value = $row['civicrm_pledge_status_id']) {
432 $rows[$rowNum]['civicrm_pledge_status_id'] = CRM_Contribute_PseudoConstant
::contributionStatus($value);
437 $entryFound = $this->alterDisplayAddressFields($row, $rows, $rowNum, 'pledge/summary', 'List all pledge(s) for this ') ?
TRUE : $entryFound;
439 // skip looking further in rows, if first row itself doesn't
440 // have the column we need