4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.5 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
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;
51 function __construct() {
52 $this->_columns
= array(
55 'dao' => 'CRM_Contact_DAO_Contact',
59 array('title' => ts('Contact Name'),
62 'postal_greeting_display' =>
63 array('title' => ts('Postal Greeting')),
70 'grouping' => 'contact-fields',
74 array('title' => ts('Contact ID')),
76 array('title' => ts('Contact Name'),
82 'dao' => 'CRM_Core_DAO_Email',
88 'title' => ts('email'),
91 'grouping' => 'contact-fields',
95 'dao' => 'CRM_Pledge_DAO_Pledge',
100 'no_display' => TRUE,
105 'no_display' => TRUE,
108 array('title' => ts('Pledge Amount'),
110 'type' => CRM_Utils_Type
::T_MONEY
,
112 array('sum' => ts('Aggregate Amount Pledged'),
113 'count' => ts('Pledges'),
114 'avg' => ts('Average'),
118 array('title' => ts('Frequency Unit'),
121 array('title' => ts('Installments'),
123 'pledge_create_date' =>
124 array('title' => ts('Pledge Made Date'),
127 array('title' => ts('Pledge Start Date'),
128 'type' => CRM_Utils_Type
::T_DATE
,
131 array('title' => ts('Pledge End Date'),
132 'type' => CRM_Utils_Type
::T_DATE
,
135 array('title' => ts('Pledge Status'),
140 'pledge_create_date' =>
142 'title' => 'Pledge Made Date',
143 'operatorType' => CRM_Report_Form
::OP_DATE
,
146 array('title' => ts('Pledged Amount'),
147 'operatorType' => CRM_Report_Form
::OP_INT
,
150 array('title' => 'Currency',
151 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
152 'options' => CRM_Core_OptionGroup
::values('currencies_enabled'),
154 'type' => CRM_Utils_Type
::T_STRING
,
158 'name' => 'status_id',
159 'title' => ts('Pledge Status'),
160 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
161 'options' => CRM_Core_OptionGroup
::values('contribution_status'),
166 'pledge_create_date' =>
173 array('title' => ts('Frequency Unit'),
176 array('title' => ts('Pledge Status'),
180 'civicrm_pledge_payment' =>
182 'dao' => 'CRM_Pledge_DAO_PledgePayment',
187 'title' => ts('Total Amount Paid'),
188 'type' => CRM_Utils_Type
::T_STRING
,
189 'dbAlias' => 'sum(pledge_payment_civireport.actual_amount)',
195 'dao' => 'CRM_Contact_DAO_Group',
201 'name' => 'group_id',
202 'title' => ts(' Group'),
203 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
205 'options' => CRM_Core_PseudoConstant
::group(),
209 ) +
$this->addAddressFields();
211 $this->_tagFilter
= TRUE;
212 $this->_currencyColumn
= 'civicrm_pledge_currency';
213 parent
::__construct();
216 function preProcess() {
217 parent
::preProcess();
226 FROM civicrm_pledge {$this->_aliases['civicrm_pledge']}
227 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_contact']}
228 ON ({$this->_aliases['civicrm_contact']}.id =
229 {$this->_aliases['civicrm_pledge']}.contact_id )
232 // include address field if address column is to be included
233 if ($this->_addressField
) {
235 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
236 ON ({$this->_aliases['civicrm_contact']}.id =
237 {$this->_aliases['civicrm_address']}.contact_id) AND
238 {$this->_aliases['civicrm_address']}.is_primary = 1\n";
241 // include email field if email column is to be included
242 if ($this->_emailField
) {
244 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
245 ON ({$this->_aliases['civicrm_contact']}.id =
246 {$this->_aliases['civicrm_email']}.contact_id) AND
247 {$this->_aliases['civicrm_email']}.is_primary = 1\n";
250 if (!empty($this->_params
['fields']['total_paid'])){
252 LEFT JOIN civicrm_pledge_payment {$this->_aliases['civicrm_pledge_payment']} ON
253 {$this->_aliases['civicrm_pledge']}.id = {$this->_aliases['civicrm_pledge_payment']}.pledge_id
254 AND {$this->_aliases['civicrm_pledge_payment']}.status_id = 1
260 $this->_groupBy
= "";
263 if (is_array($this->_params
['group_bys']) &&
264 !empty($this->_params
['group_bys'])
266 foreach ($this->_columns
as $tableName => $table) {
267 if (array_key_exists('group_bys', $table)) {
268 foreach ($table['group_bys'] as $fieldName => $field) {
269 if (!empty($this->_params
['group_bys'][$fieldName])) {
270 if (!empty($field['chart'])) {
271 $this->assign('chartSupported', TRUE);
274 if (!empty($table['group_bys'][$fieldName]['frequency']) && !empty($this->_params
['group_bys_freq'][$fieldName])) {
276 $append = "YEAR({$field['dbAlias']}),";
277 if (in_array(strtolower($this->_params
['group_bys_freq'][$fieldName]),
282 $this->_groupBy
[] = "$append {$this->_params['group_bys_freq'][$fieldName]}({$field['dbAlias']})";
286 $this->_groupBy
[] = $field['dbAlias'];
293 if (!empty($this->_statFields
) &&
294 (($append && count($this->_groupBy
) <= 1) ||
(!$append)) && !$this->_having
296 $this->_rollup
= " WITH ROLLUP";
298 $this->_groupBy
= "GROUP BY " . implode(', ', $this->_groupBy
) . " {$this->_rollup} ";
301 $this->_groupBy
= "GROUP BY {$this->_aliases['civicrm_contact']}.id";
310 function statistics(&$rows) {
311 $statistics = parent
::statistics($rows);
313 if (!$this->_having
) {
315 SELECT COUNT({$this->_aliases['civicrm_pledge']}.amount ) as count,
316 SUM({$this->_aliases['civicrm_pledge']}.amount ) as amount,
317 ROUND(AVG({$this->_aliases['civicrm_pledge']}.amount), 2) as avg
320 $sql = "{$select} {$this->_from} {$this->_where}";
322 $dao = CRM_Core_DAO
::executeQuery($sql);
325 $statistics['count']['amount'] = array(
326 'value' => $dao->amount
,
327 'title' => 'Total Pledged',
328 'type' => CRM_Utils_Type
::T_MONEY
,
330 $statistics['count']['count '] = array(
331 'value' => $dao->count
,
332 'title' => 'Total No Pledges',
334 $statistics['count']['avg '] = array(
335 'value' => $dao->avg
,
336 'title' => 'Average',
337 'type' => CRM_Utils_Type
::T_MONEY
,
346 foreach ($this->_columns
as $tableName => $table) {
347 if (array_key_exists('filters', $table)) {
348 foreach ($table['filters'] as $fieldName => $field) {
350 if (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
) {
351 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
352 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
353 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
355 if ($relative ||
$from ||
$to) {
356 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
360 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
362 $clause = $this->whereClause($field,
364 CRM_Utils_Array
::value("{$fieldName}_value",
367 CRM_Utils_Array
::value("{$fieldName}_min",
370 CRM_Utils_Array
::value("{$fieldName}_max",
377 if (!empty($clause)) {
378 $clauses[] = $clause;
383 if (empty($clauses)) {
384 $this->_where
= "WHERE ({$this->_aliases['civicrm_pledge']}.is_test=0 ) ";
387 $this->_where
= "WHERE ({$this->_aliases['civicrm_pledge']}.is_test=0 ) AND
388 " . implode(' AND ', $clauses);
391 if ($this->_aclWhere
) {
392 $this->_where
.= " AND {$this->_aclWhere} ";
396 function postProcess() {
397 parent
::postProcess();
403 function alterDisplay(&$rows) {
404 // custom code to alter rows
406 $checkList = array();
407 $display_flag = $prev_cid = $cid = 0;
408 crm_Core_error
::Debug('$rows', $rows);
409 foreach ($rows as $rowNum => $row) {
411 // convert display name to links
412 if (array_key_exists('civicrm_contact_sort_name', $row) &&
413 array_key_exists('civicrm_contact_id', $row)
415 $url = CRM_Utils_System
::url("civicrm/contact/view",
416 'reset=1&cid=' . $row['civicrm_contact_id'],
419 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
420 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Contact Summary for this Contact.");
425 if (array_key_exists('civicrm_pledge_status_id', $row)) {
426 if ($value = $row['civicrm_pledge_status_id']) {
427 $rows[$rowNum]['civicrm_pledge_status_id'] = CRM_Contribute_PseudoConstant
::contributionStatus($value);
432 $entryFound = $this->alterDisplayAddressFields($row, $rows, $rowNum, 'pledge/summary', 'List all pledge(s) for this ') ?
TRUE : $entryFound;
434 // skip looking further in rows, if first row itself doesn't
435 // have the column we need