3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 +--------------------------------------------------------------------+
30 * !!!!!!!!!!!!!!!!!!!!
31 * NB: this is named detail but behaves like a summary report.
32 * It is also accessed through the Pledge Summary link in the UI
33 * This should presumably be changed.
35 * !!!!!!!!!!!!!!!!!!!!
42 * @copyright CiviCRM LLC (c) 2004-2014
46 class CRM_Report_Form_Pledge_Detail
extends CRM_Report_Form
{
48 protected $_summary = NULL;
49 protected $_totalPaid = FALSE;
50 protected $_pledgeStatuses = array();
51 protected $_customGroupExtends = array(
62 function __construct() {
63 $this->_pledgeStatuses
= CRM_Contribute_PseudoConstant
::contributionStatus();
64 // Check if CiviCampaign is a) enabled and b) has active campaigns
65 $config = CRM_Core_Config
::singleton();
66 $campaignEnabled = in_array("CiviCampaign", $config->enableComponents
);
67 if ($campaignEnabled) {
68 $getCampaigns = CRM_Campaign_BAO_Campaign
::getPermissionedCampaigns(NULL, NULL, TRUE, FALSE, TRUE);
69 $this->activeCampaigns
= $getCampaigns['campaigns'];
70 asort($this->activeCampaigns
);
73 $this->_columns
= array(
76 'dao' => 'CRM_Contact_DAO_Contact',
80 array('title' => ts('Contact Name'),
88 array('title' => ts('Contact Name')),
90 array('no_display' => TRUE),
92 'grouping' => 'contact-fields',
96 'dao' => 'CRM_Core_DAO_Email',
100 array('no_repeat' => TRUE),
102 'grouping' => 'contact-fields',
106 'dao' => 'CRM_Pledge_DAO_Pledge',
111 'no_display' => TRUE,
116 'no_display' => TRUE,
120 array('title' => ts('Pledge Amount'),
122 'type' => CRM_Utils_Type
::T_MONEY
,
127 'no_display' => TRUE,
130 array('title' => ts('Frequency Unit'),
133 array('title' => ts('Installments'),
135 'pledge_create_date' =>
136 array('title' => ts('Pledge Made Date'),
139 array('title' => ts('Pledge Start Date'),
140 'type' => CRM_Utils_Type
::T_DATE
,
143 array('title' => ts('Pledge End Date'),
144 'type' => CRM_Utils_Type
::T_DATE
,
147 array('title' => ts('Pledge Status'),
153 'pledge_create_date' =>
155 'title' => 'Pledge Made Date',
156 'operatorType' => CRM_Report_Form
::OP_DATE
,
159 array('title' => ts('Pledged Amount'),
160 'operatorType' => CRM_Report_Form
::OP_INT
,
163 array('title' => 'Currency',
164 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
165 'options' => CRM_Core_OptionGroup
::values('currencies_enabled'),
167 'type' => CRM_Utils_Type
::T_STRING
,
171 'name' => 'status_id',
172 'title' => ts('Pledge Status'),
173 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
174 'options' => CRM_Core_OptionGroup
::values('contribution_status'),
179 'civicrm_pledge_payment' =>
181 'dao' => 'CRM_Pledge_DAO_PledgePayment',
186 'title' => ts('Total Amount Paid'),
187 'type' => CRM_Utils_Type
::T_MONEY
,
191 'title' => ts('Balance Due'),
193 'type' => CRM_Utils_Type
::T_MONEY
,
200 'dao' => 'CRM_Contact_DAO_Group',
206 'name' => 'group_id',
207 'title' => ts(' Group'),
208 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
210 'options' => CRM_Core_PseudoConstant
::group(),
215 +
$this->getAddressColumns(array('group_by' => FALSE))
216 +
$this->getPhoneColumns();
217 // If we have a campaign, build out the relevant elements
218 $this->_tagFilter
= TRUE;
219 if ($campaignEnabled && !empty($this->activeCampaigns
)) {
220 $this->_columns
['civicrm_pledge']['fields']['campaign_id'] = array(
221 'title' => 'Campaign',
222 'default' => 'false',
224 $this->_columns
['civicrm_pledge']['filters']['campaign_id'] = array('title' => ts('Campaign'),
225 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
226 'options' => $this->activeCampaigns
,
228 $this->_columns
['civicrm_pledge']['group_bys']['campaign_id'] = array('title' => ts('Campaign'));
232 $this->_tagFilter
= TRUE;
233 $this->_currencyColumn
= 'civicrm_pledge_currency';
234 parent
::__construct();
237 function preProcess() {
238 parent
::preProcess();
245 * If we are retrieving total paid we need to define the inclusion of pledge_payment
253 * @return bool|string
255 function selectClause(&$tableName, $tableKey, &$fieldName, &$field) {
256 if($fieldName == 'total_paid'){
257 $this->_totalPaid
= TRUE; // add pledge_payment join
258 $this->_columnHeaders
["{$tableName}_{$fieldName}"] = array(
259 'title' => $field['title'],
260 'type' => $field['type']
262 return "COALESCE(sum({$this->_aliases[$tableName]}.actual_amount), 0) as {$tableName}_{$fieldName}";
264 if($fieldName == 'balance_due'){
265 $cancelledStatus = array_search('Cancelled', $this->_pledgeStatuses
);
266 $completedStatus = array_search('Completed', $this->_pledgeStatuses
);
267 $this->_totalPaid
= TRUE; // add pledge_payment join
268 $this->_columnHeaders
["{$tableName}_{$fieldName}"] = $field['title'];
269 $this->_columnHeaders
["{$tableName}_{$fieldName}"] = array(
270 'title' => $field['title'],
271 'type' => $field['type']
273 return "IF({$this->_aliases['civicrm_pledge']}.status_id IN({$cancelledStatus}, $completedStatus), 0, COALESCE({$this->_aliases['civicrm_pledge']}.amount, 0) - COALESCE(sum({$this->_aliases[$tableName]}.actual_amount),0)) as {$tableName}_{$fieldName}";
280 if (empty($this->_groupBy
) && $this->_totalPaid
) {
281 $this->_groupBy
= " GROUP BY {$this->_aliases['civicrm_pledge']}.id, {$this->_aliases['civicrm_pledge']}.currency" ;
286 FROM civicrm_pledge {$this->_aliases['civicrm_pledge']}
287 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_contact']}
288 ON ({$this->_aliases['civicrm_contact']}.id =
289 {$this->_aliases['civicrm_pledge']}.contact_id )
292 if($this->_totalPaid
){
294 LEFT JOIN civicrm_pledge_payment {$this->_aliases['civicrm_pledge_payment']} ON
295 {$this->_aliases['civicrm_pledge']}.id = {$this->_aliases['civicrm_pledge_payment']}.pledge_id
296 AND {$this->_aliases['civicrm_pledge_payment']}.status_id = 1
300 $this->addPhoneFromClause();
301 $this->addAddressFromClause();
302 // include email field if email column is to be included
303 if ($this->_emailField
) {
305 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
306 ON ({$this->_aliases['civicrm_contact']}.id =
307 {$this->_aliases['civicrm_email']}.contact_id) AND
308 {$this->_aliases['civicrm_email']}.is_primary = 1\n";
317 function statistics(&$rows) {
318 $statistics = parent
::statistics($rows);
319 //regenerate the from field without extra left join on pledge payments
320 $this->_totalPaid
= FALSE;
322 $this->customDataFrom();
323 if (!$this->_having
) {
324 $totalAmount = $average = array();
327 SELECT COUNT({$this->_aliases['civicrm_pledge']}.amount ) as count,
328 SUM({$this->_aliases['civicrm_pledge']}.amount ) as amount,
329 ROUND(AVG({$this->_aliases['civicrm_pledge']}.amount), 2) as avg,
330 {$this->_aliases['civicrm_pledge']}.currency as currency
333 $group = "GROUP BY {$this->_aliases['civicrm_pledge']}.currency";
334 $sql = "{$select} {$this->_from} {$this->_where} {$group}";
335 $dao = CRM_Core_DAO
::executeQuery($sql);
336 $count = $index = $totalCount = 0;
337 // this will run once per currency
338 while($dao->fetch()) {
339 $totalAmount = CRM_Utils_Money
::format($dao->amount
, $dao->currency
);
340 $average = CRM_Utils_Money
::format($dao->avg
, $dao->currency
);
341 $count = $dao->count
;
342 $totalCount .= $count;
343 $statistics['counts']['amount' . $index] = array(
344 'title' => ts('Total Amount Pledged (') . $dao->currency
. ')',
345 'value' => $totalAmount,
346 'type' => CRM_Utils_Type
::T_STRING
,
348 $statistics['counts']['avg' . $index] = array(
349 'title' => ts('Average (') . $dao->currency
. ')',
351 'type' => CRM_Utils_Type
::T_STRING
,
353 $statistics['counts']['count' . $index] = array(
354 'title' => ts('Total No Pledges (') . $dao->currency
. ')',
356 'type' => CRM_Utils_Type
::T_INT
,
360 if($totalCount > $count) {
361 $statistics['counts']['count' . $index] = array(
362 'title' => ts('Total No Pledges'),
363 'value' => $totalCount,
364 'type' => CRM_Utils_Type
::T_INT
,
372 $this->_orderBy
= "ORDER BY {$this->_aliases['civicrm_contact']}.sort_name, {$this->_aliases['civicrm_contact']}.id";
377 foreach ($this->_columns
as $tableName => $table) {
378 if (array_key_exists('filters', $table)) {
379 foreach ($table['filters'] as $fieldName => $field) {
381 if (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
) {
382 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
383 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
384 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
386 if ($relative ||
$from ||
$to) {
387 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
391 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
393 $clause = $this->whereClause($field,
395 CRM_Utils_Array
::value("{$fieldName}_value",
398 CRM_Utils_Array
::value("{$fieldName}_min",
401 CRM_Utils_Array
::value("{$fieldName}_max",
408 if (!empty($clause)) {
409 $clauses[] = $clause;
414 if (empty($clauses)) {
415 $this->_where
= "WHERE ({$this->_aliases['civicrm_pledge']}.is_test=0 ) ";
418 $this->_where
= "WHERE ({$this->_aliases['civicrm_pledge']}.is_test=0 ) AND
419 " . implode(' AND ', $clauses);
422 if ($this->_aclWhere
) {
423 $this->_where
.= " AND {$this->_aclWhere} ";
427 function postProcess() {
429 $this->beginPostProcess();
431 // get the acl clauses built before we assemble the query
432 $this->buildACLClause($this->_aliases
['civicrm_contact']);
433 $sql = $this->buildQuery();
434 $rows = $payment = array();
436 $dao = CRM_Core_DAO
::executeQuery($sql);
438 // Set pager for the Main Query only which displays basic information
440 $this->assign('columnHeaders', $this->_columnHeaders
);
442 while ($dao->fetch()) {
443 $pledgeID = $dao->civicrm_pledge_id
;
444 foreach ($this->_columnHeaders
as $columnHeadersKey => $columnHeadersValue) {
446 if (property_exists($dao, $columnHeadersKey)) {
447 $display[$pledgeID][$columnHeadersKey] = $dao->$columnHeadersKey;
450 $pledgeIDArray[] = $pledgeID;
453 // Add Special headers
454 $this->_columnHeaders
['scheduled_date'] = array(
455 'type' => CRM_Utils_Type
::T_DATE
,
456 'title' => 'Next Payment Due',
458 $this->_columnHeaders
['scheduled_amount'] = array(
459 'type' => CRM_Utils_Type
::T_MONEY
,
460 'title' => 'Next Payment Amount',
462 $this->_columnHeaders
['status_id'] = NULL;
465 * this is purely about ordering the total paid & balance due fields off to the end
466 * of the table in case custom or address fields cause them to fall in the middle
467 * (arguably the pledge amount should be moved to after these fields too)
470 $tableHeaders = array(
471 'civicrm_pledge_payment_total_paid',
472 'civicrm_pledge_payment_balance_due'
475 foreach ($tableHeaders as $header) {
476 //per above, unset & reset them so they move to the end
477 if(isset($this->_columnHeaders
[$header])){
478 $headervalue = $this->_columnHeaders
[$header];
479 unset($this->_columnHeaders
[$header]);
480 $this->_columnHeaders
[$header] = $headervalue;
484 // To Display Payment Details of pledged amount
485 // for pledge payments In Progress
486 if (!empty($display)) {
488 SELECT min(payment.scheduled_date) as scheduled_date,
490 payment.scheduled_amount,
493 FROM civicrm_pledge_payment payment
494 LEFT JOIN civicrm_pledge pledge
495 ON pledge.id = payment.pledge_id
497 WHERE payment.status_id = 2
499 GROUP BY payment.pledge_id";
501 $daoPayment = CRM_Core_DAO
::executeQuery($sqlPayment);
503 while ($daoPayment->fetch()) {
504 foreach ($pledgeIDArray as $key => $val) {
505 if ($val == $daoPayment->pledge_id
) {
507 $display[$daoPayment->pledge_id
]['scheduled_date'] = $daoPayment->scheduled_date
;
509 $display[$daoPayment->pledge_id
]['scheduled_amount'] = $daoPayment->scheduled_amount
;
515 // Displaying entire data on the form
516 if (!empty($display)) {
517 foreach ($display as $key => $value) {
519 foreach ($this->_columnHeaders
as $columnKey => $columnValue) {
520 if (array_key_exists($columnKey, $value)) {
521 $row[$columnKey] = !empty($value[$columnKey]) ?
$value[$columnKey] : '';
528 unset($this->_columnHeaders
['status_id']);
529 unset($this->_columnHeaders
['civicrm_pledge_id']);
530 unset($this->_columnHeaders
['civicrm_pledge_contact_id']);
532 $this->formatDisplay($rows, FALSE);
533 $this->doTemplateAssignment($rows);
534 $this->endPostProcess($rows);
540 function alterDisplay(&$rows) {
541 // custom code to alter rows
543 $checkList = array();
544 $display_flag = $prev_cid = $cid = 0;
546 foreach ($rows as $rowNum => $row) {
547 if (!empty($this->_noRepeats
) && $this->_outputMode
!= 'csv') {
548 // don't repeat contact details if its same as the previous row
549 if (array_key_exists('civicrm_pledge_contact_id', $row)) {
550 if ($cid = $row['civicrm_pledge_contact_id']) {
555 if ($prev_cid == $cid) {
566 foreach ($row as $colName => $colVal) {
567 if (in_array($colName, $this->_noRepeats
)) {
568 unset($rows[$rowNum][$colName]);
577 // convert display name to links
578 if (array_key_exists('civicrm_contact_sort_name', $row) &&
579 array_key_exists('civicrm_pledge_contact_id', $row)
581 $url = CRM_Utils_System
::url("civicrm/contact/view",
582 'reset=1&cid=' . $row['civicrm_pledge_contact_id'],
585 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
586 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Contact Summary for this Contact.");
591 if (array_key_exists('civicrm_pledge_status_id', $row)) {
592 if ($value = $row['civicrm_pledge_status_id']) {
593 $rows[$rowNum]['civicrm_pledge_status_id'] = CRM_Contribute_PseudoConstant
::contributionStatus($value);
598 // If using campaigns, convert campaign_id to campaign title
599 if (array_key_exists('civicrm_pledge_campaign_id', $row)) {
600 if ($value = $row['civicrm_pledge_campaign_id']) {
601 $rows[$rowNum]['civicrm_pledge_campaign_id'] = $this->activeCampaigns
[$value];
606 $entryFound = $this->alterDisplayAddressFields($row, $rows, $rowNum, 'pledge/detail', 'List all pledge(s) for this ') ?
TRUE : $entryFound;
608 // skip looking further in rows, if first row itself doesn't
609 // have the column we need