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;
48 public function __construct() {
49 $this->_columns
= array(
50 'civicrm_contact' => array(
51 'dao' => 'CRM_Contact_DAO_Contact',
54 'title' => ts('Contact Name'),
57 'postal_greeting_display' => array('title' => ts('Postal Greeting')),
63 'grouping' => 'contact-fields',
65 'id' => array('title' => ts('Contact ID')),
67 'title' => ts('Contact Name'),
71 'civicrm_email' => array(
72 'dao' => 'CRM_Core_DAO_Email',
76 'title' => ts('email'),
79 'grouping' => 'contact-fields',
81 'civicrm_pledge' => array(
82 'dao' => 'CRM_Pledge_DAO_Pledge',
88 'financial_type_id' => array(
89 'title' => ts('Financial Type'),
96 'title' => ts('Pledge Amount'),
98 'type' => CRM_Utils_Type
::T_MONEY
,
99 'statistics' => array(
100 'sum' => ts('Aggregate Amount Pledged'),
101 'count' => ts('Pledges'),
102 'avg' => ts('Average'),
105 'frequency_unit' => array(
106 'title' => ts('Frequency Unit'),
108 'installments' => array(
109 'title' => ts('Installments'),
111 'pledge_create_date' => array(
112 'title' => ts('Pledge Made Date'),
114 'start_date' => array(
115 'title' => ts('Pledge Start Date'),
116 'type' => CRM_Utils_Type
::T_DATE
,
119 'title' => ts('Pledge End Date'),
120 'type' => CRM_Utils_Type
::T_DATE
,
122 'status_id' => array(
123 'title' => ts('Pledge Status'),
127 'pledge_create_date' => array(
128 'title' => 'Pledge Made Date',
129 'operatorType' => CRM_Report_Form
::OP_DATE
,
131 'pledge_amount' => array(
132 'title' => ts('Pledged Amount'),
133 'operatorType' => CRM_Report_Form
::OP_INT
,
136 'title' => 'Currency',
137 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
138 'options' => CRM_Core_OptionGroup
::values('currencies_enabled'),
140 'type' => CRM_Utils_Type
::T_STRING
,
143 'name' => 'status_id',
144 'title' => ts('Pledge Status'),
145 'type' => CRM_Utils_Type
::T_INT
,
146 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
147 'options' => CRM_Core_OptionGroup
::values('contribution_status'),
149 'financial_type_id' => array(
150 'title' => ts('Financial Type'),
151 'type' => CRM_Utils_Type
::T_INT
,
152 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
153 'options' => CRM_Contribute_PseudoConstant
::financialType(),
156 'group_bys' => array(
157 'pledge_create_date' => array(
162 'frequency_unit' => array(
163 'title' => ts('Frequency Unit'),
165 'status_id' => array(
166 'title' => ts('Pledge Status'),
168 'financial_type_id' => array(
169 'title' => ts('Financial Type'),
173 'civicrm_pledge_payment' => array(
174 'dao' => 'CRM_Pledge_DAO_PledgePayment',
176 'total_paid' => array(
177 'title' => ts('Total Amount Paid'),
178 'type' => CRM_Utils_Type
::T_STRING
,
179 'dbAlias' => 'sum(pledge_payment_civireport.actual_amount)',
183 ) +
$this->addAddressFields();
185 $this->_groupFilter
= TRUE;
186 $this->_tagFilter
= TRUE;
187 $this->_currencyColumn
= 'civicrm_pledge_currency';
188 parent
::__construct();
191 public function preProcess() {
192 parent
::preProcess();
195 public function select() {
199 public function from() {
201 FROM civicrm_pledge {$this->_aliases['civicrm_pledge']}
202 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_contact']}
203 ON ({$this->_aliases['civicrm_contact']}.id =
204 {$this->_aliases['civicrm_pledge']}.contact_id )
207 // include address field if address column is to be included
208 if ($this->_addressField
) {
210 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
211 ON ({$this->_aliases['civicrm_contact']}.id =
212 {$this->_aliases['civicrm_address']}.contact_id) AND
213 {$this->_aliases['civicrm_address']}.is_primary = 1\n";
216 // include email field if email column is to be included
217 if ($this->_emailField
) {
219 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
220 ON ({$this->_aliases['civicrm_contact']}.id =
221 {$this->_aliases['civicrm_email']}.contact_id) AND
222 {$this->_aliases['civicrm_email']}.is_primary = 1\n";
225 if (!empty($this->_params
['fields']['total_paid'])) {
227 LEFT JOIN civicrm_pledge_payment {$this->_aliases['civicrm_pledge_payment']} ON
228 {$this->_aliases['civicrm_pledge']}.id = {$this->_aliases['civicrm_pledge_payment']}.pledge_id
229 AND {$this->_aliases['civicrm_pledge_payment']}.status_id = 1
234 public function groupBy() {
235 $this->_groupBy
= "";
238 if (is_array($this->_params
['group_bys']) &&
239 !empty($this->_params
['group_bys'])
241 foreach ($this->_columns
as $tableName => $table) {
242 if (array_key_exists('group_bys', $table)) {
243 foreach ($table['group_bys'] as $fieldName => $field) {
244 if (!empty($this->_params
['group_bys'][$fieldName])) {
245 if (!empty($field['chart'])) {
246 $this->assign('chartSupported', TRUE);
249 if (!empty($table['group_bys'][$fieldName]['frequency']) &&
250 !empty($this->_params
['group_bys_freq'][$fieldName])
253 $append = "YEAR({$field['dbAlias']}),";
254 if (in_array(strtolower($this->_params
['group_bys_freq'][$fieldName]),
259 $this->_groupBy
[] = "$append {$this->_params['group_bys_freq'][$fieldName]}({$field['dbAlias']})";
263 $this->_groupBy
[] = $field['dbAlias'];
270 if (!empty($this->_statFields
) &&
271 (($append && count($this->_groupBy
) <= 1) ||
(!$append)) &&
274 $this->_rollup
= " WITH ROLLUP";
276 $groupBy = $this->_groupBy
;
277 $this->_groupBy
= "GROUP BY " . implode(', ', $this->_groupBy
);
280 $groupBy = "{$this->_aliases['civicrm_contact']}.id";
281 $this->_groupBy
= "GROUP BY {$groupBy}";
283 $this->appendSelect($this->_selectClauses
, $groupBy);
284 $this->_groupBy
.= " {$this->_rollup}";
292 public function statistics(&$rows) {
293 $statistics = parent
::statistics($rows);
295 if (!$this->_having
) {
297 SELECT COUNT({$this->_aliases['civicrm_pledge']}.amount ) as count,
298 SUM({$this->_aliases['civicrm_pledge']}.amount ) as amount,
299 ROUND(AVG({$this->_aliases['civicrm_pledge']}.amount), 2) as avg
302 $sql = "{$select} {$this->_from} {$this->_where}";
304 $dao = CRM_Core_DAO
::executeQuery($sql);
307 $statistics['count']['amount'] = array(
308 'value' => $dao->amount
,
309 'title' => 'Total Pledged',
310 'type' => CRM_Utils_Type
::T_MONEY
,
312 $statistics['count']['count '] = array(
313 'value' => $dao->count
,
314 'title' => 'Total No Pledges',
316 $statistics['count']['avg '] = array(
317 'value' => $dao->avg
,
318 'title' => 'Average',
319 'type' => CRM_Utils_Type
::T_MONEY
,
326 public function where() {
328 foreach ($this->_columns
as $tableName => $table) {
329 if (array_key_exists('filters', $table)) {
330 foreach ($table['filters'] as $fieldName => $field) {
332 if (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
) {
333 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
334 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
335 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
337 if ($relative ||
$from ||
$to) {
338 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
342 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
344 $clause = $this->whereClause($field,
346 CRM_Utils_Array
::value("{$fieldName}_value",
349 CRM_Utils_Array
::value("{$fieldName}_min",
352 CRM_Utils_Array
::value("{$fieldName}_max",
359 if (!empty($clause)) {
360 $clauses[] = $clause;
365 if (empty($clauses)) {
366 $this->_where
= "WHERE ({$this->_aliases['civicrm_pledge']}.is_test=0 ) ";
369 $this->_where
= "WHERE ({$this->_aliases['civicrm_pledge']}.is_test=0 ) AND
370 " . implode(' AND ', $clauses);
373 if ($this->_aclWhere
) {
374 $this->_where
.= " AND {$this->_aclWhere} ";
378 public function postProcess() {
379 parent
::postProcess();
383 * Alter display of rows.
385 * Iterate through the rows retrieved via SQL and make changes for display purposes,
386 * such as rendering contacts as links.
389 * Rows generated by SQL, with an array for each row.
391 public function alterDisplay(&$rows) {
393 $checkList = array();
394 $display_flag = $prev_cid = $cid = 0;
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.");
410 if (array_key_exists('civicrm_pledge_financial_type_id', $row)) {
411 if ($value = $row['civicrm_pledge_financial_type_id']) {
412 $rows[$rowNum]['civicrm_pledge_financial_type_id'] = CRM_Contribute_PseudoConstant
::financialType($value, FALSE);
418 if (array_key_exists('civicrm_pledge_status_id', $row)) {
419 if ($value = $row['civicrm_pledge_status_id']) {
420 $rows[$rowNum]['civicrm_pledge_status_id'] = CRM_Contribute_PseudoConstant
::contributionStatus($value);
425 $entryFound = $this->alterDisplayAddressFields($row, $rows, $rowNum, 'pledge/summary', 'List all pledge(s) for this ') ?
TRUE : $entryFound;
427 // skip looking further in rows, if first row itself doesn't
428 // have the column we need