4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.4 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2013 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
32 * @copyright CiviCRM LLC (c) 2004-2013
36 class CRM_Report_Form_Pledge_Summary
extends CRM_Report_Form
{
38 protected $_summary = NULL;
39 protected $_totalPaid = FALSE;
40 protected $_customGroupExtends = array('Pledge', 'Individual');
41 protected $_customGroupGroupBy = TRUE;
42 protected $_addressField = FALSE;
43 protected $_emailField = FALSE;
45 function __construct() {
46 $this->_columns
= array(
49 'dao' => 'CRM_Contact_DAO_Contact',
53 array('title' => ts('Contact Name'),
56 'postal_greeting_display' =>
57 array('title' => ts('Postal Greeting')),
64 'grouping' => 'contact-fields',
68 array('title' => ts('Contact ID')),
70 array('title' => ts('Contact Name'),
76 'dao' => 'CRM_Core_DAO_Email',
82 'title' => ts('email'),
85 'grouping' => 'contact-fields',
89 'dao' => 'CRM_Pledge_DAO_Pledge',
102 array('title' => ts('Pledge Amount'),
104 'type' => CRM_Utils_Type
::T_MONEY
,
106 array('sum' => ts('Aggregate Amount Pledged'),
107 'count' => ts('Pledges'),
108 'avg' => ts('Average'),
112 array('title' => ts('Frequency Unit'),
115 array('title' => ts('Installments'),
117 'pledge_create_date' =>
118 array('title' => ts('Pledge Made Date'),
121 array('title' => ts('Pledge Start Date'),
122 'type' => CRM_Utils_Type
::T_DATE
,
125 array('title' => ts('Pledge End Date'),
126 'type' => CRM_Utils_Type
::T_DATE
,
129 array('title' => ts('Pledge Status'),
134 'pledge_create_date' =>
136 'title' => 'Pledge Made Date',
137 'operatorType' => CRM_Report_Form
::OP_DATE
,
140 array('title' => ts('Pledged Amount'),
141 'operatorType' => CRM_Report_Form
::OP_INT
,
144 array('title' => 'Currency',
145 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
146 'options' => CRM_Core_OptionGroup
::values('currencies_enabled'),
148 'type' => CRM_Utils_Type
::T_STRING
,
152 'name' => 'status_id',
153 'title' => ts('Pledge Status'),
154 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
155 'options' => CRM_Core_OptionGroup
::values('contribution_status'),
160 'pledge_create_date' =>
167 array('title' => ts('Frequency Unit'),
170 array('title' => ts('Pledge Status'),
174 'civicrm_pledge_payment' =>
176 'dao' => 'CRM_Pledge_DAO_PledgePayment',
181 'title' => ts('Total Amount Paid'),
182 'type' => CRM_Utils_Type
::T_STRING
,
183 'dbAlias' => 'sum(pledge_payment_civireport.actual_amount)',
189 'dao' => 'CRM_Contact_DAO_Group',
195 'name' => 'group_id',
196 'title' => ts(' Group'),
197 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
199 'options' => CRM_Core_PseudoConstant
::group(),
203 ) +
$this->addAddressFields();
205 $this->_tagFilter
= TRUE;
206 $this->_currencyColumn
= 'civicrm_pledge_currency';
207 parent
::__construct();
210 function preProcess() {
211 parent
::preProcess();
220 FROM civicrm_pledge {$this->_aliases['civicrm_pledge']}
221 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_contact']}
222 ON ({$this->_aliases['civicrm_contact']}.id =
223 {$this->_aliases['civicrm_pledge']}.contact_id )
226 // include address field if address column is to be included
227 if ($this->_addressField
) {
229 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
230 ON ({$this->_aliases['civicrm_contact']}.id =
231 {$this->_aliases['civicrm_address']}.contact_id) AND
232 {$this->_aliases['civicrm_address']}.is_primary = 1\n";
235 // include email field if email column is to be included
236 if ($this->_emailField
) {
238 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
239 ON ({$this->_aliases['civicrm_contact']}.id =
240 {$this->_aliases['civicrm_email']}.contact_id) AND
241 {$this->_aliases['civicrm_email']}.is_primary = 1\n";
244 if (!empty($this->_params
['fields']['total_paid'])){
246 LEFT JOIN civicrm_pledge_payment {$this->_aliases['civicrm_pledge_payment']} ON
247 {$this->_aliases['civicrm_pledge']}.id = {$this->_aliases['civicrm_pledge_payment']}.pledge_id
248 AND {$this->_aliases['civicrm_pledge_payment']}.status_id = 1
254 $this->_groupBy
= "";
257 if (is_array($this->_params
['group_bys']) &&
258 !empty($this->_params
['group_bys'])
260 foreach ($this->_columns
as $tableName => $table) {
261 if (array_key_exists('group_bys', $table)) {
262 foreach ($table['group_bys'] as $fieldName => $field) {
263 if (!empty($this->_params
['group_bys'][$fieldName])) {
264 if (!empty($field['chart'])) {
265 $this->assign('chartSupported', TRUE);
268 if (!empty($table['group_bys'][$fieldName]['frequency']) && !empty($this->_params
['group_bys_freq'][$fieldName])) {
270 $append = "YEAR({$field['dbAlias']}),";
271 if (in_array(strtolower($this->_params
['group_bys_freq'][$fieldName]),
276 $this->_groupBy
[] = "$append {$this->_params['group_bys_freq'][$fieldName]}({$field['dbAlias']})";
280 $this->_groupBy
[] = $field['dbAlias'];
287 if (!empty($this->_statFields
) &&
288 (($append && count($this->_groupBy
) <= 1) ||
(!$append)) && !$this->_having
290 $this->_rollup
= " WITH ROLLUP";
292 $this->_groupBy
= "GROUP BY " . implode(', ', $this->_groupBy
) . " {$this->_rollup} ";
295 $this->_groupBy
= "GROUP BY {$this->_aliases['civicrm_contact']}.id";
299 function statistics(&$rows) {
300 $statistics = parent
::statistics($rows);
302 if (!$this->_having
) {
304 SELECT COUNT({$this->_aliases['civicrm_pledge']}.amount ) as count,
305 SUM({$this->_aliases['civicrm_pledge']}.amount ) as amount,
306 ROUND(AVG({$this->_aliases['civicrm_pledge']}.amount), 2) as avg
309 $sql = "{$select} {$this->_from} {$this->_where}";
311 $dao = CRM_Core_DAO
::executeQuery($sql);
314 $statistics['count']['amount'] = array(
315 'value' => $dao->amount
,
316 'title' => 'Total Pledged',
317 'type' => CRM_Utils_Type
::T_MONEY
,
319 $statistics['count']['count '] = array(
320 'value' => $dao->count
,
321 'title' => 'Total No Pledges',
323 $statistics['count']['avg '] = array(
324 'value' => $dao->avg
,
325 'title' => 'Average',
326 'type' => CRM_Utils_Type
::T_MONEY
,
335 foreach ($this->_columns
as $tableName => $table) {
336 if (array_key_exists('filters', $table)) {
337 foreach ($table['filters'] as $fieldName => $field) {
339 if (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
) {
340 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
341 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
342 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
344 if ($relative ||
$from ||
$to) {
345 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
349 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
351 $clause = $this->whereClause($field,
353 CRM_Utils_Array
::value("{$fieldName}_value",
356 CRM_Utils_Array
::value("{$fieldName}_min",
359 CRM_Utils_Array
::value("{$fieldName}_max",
366 if (!empty($clause)) {
367 $clauses[] = $clause;
372 if (empty($clauses)) {
373 $this->_where
= "WHERE ({$this->_aliases['civicrm_pledge']}.is_test=0 ) ";
376 $this->_where
= "WHERE ({$this->_aliases['civicrm_pledge']}.is_test=0 ) AND
377 " . implode(' AND ', $clauses);
380 if ($this->_aclWhere
) {
381 $this->_where
.= " AND {$this->_aclWhere} ";
385 function postProcess() {
386 parent
::postProcess();
389 function alterDisplay(&$rows) {
390 // custom code to alter rows
392 $checkList = array();
393 $display_flag = $prev_cid = $cid = 0;
394 crm_Core_error
::Debug('$rows', $rows);
395 foreach ($rows as $rowNum => $row) {
397 // convert display name to links
398 if (array_key_exists('civicrm_contact_sort_name', $row) &&
399 array_key_exists('civicrm_contact_id', $row)
401 $url = CRM_Utils_System
::url("civicrm/contact/view",
402 'reset=1&cid=' . $row['civicrm_contact_id'],
405 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
406 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Contact Summary for this Contact.");
411 if (array_key_exists('civicrm_pledge_status_id', $row)) {
412 if ($value = $row['civicrm_pledge_status_id']) {
413 $rows[$rowNum]['civicrm_pledge_status_id'] = CRM_Contribute_PseudoConstant
::contributionStatus($value);
418 $entryFound = $this->alterDisplayAddressFields($row, $rows, $rowNum, 'pledge/summary', 'List all pledge(s) for this ') ?
TRUE : $entryFound;
420 // skip looking further in rows, if first row itself doesn't
421 // have the column we need