CRM-19175 fix add to group when viewing contribution detail without soft credits.
[civicrm-core.git] / CRM / Report / Form / Pledge / Pbnp.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
fa938177 6 | Copyright CiviCRM LLC (c) 2004-2016 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
fa938177 31 * @copyright CiviCRM LLC (c) 2004-2016
6a488035
TO
32 * $Id$
33 *
34 */
35class CRM_Report_Form_Pledge_Pbnp extends CRM_Report_Form {
36 protected $_charts = array(
37 '' => 'Tabular',
38 'barChart' => 'Bar Chart',
39 'pieChart' => 'Pie Chart',
40 );
41 public $_drilldownReport = array('pledge/summary' => 'Link to Detail Report');
f16bf0fa 42
6a488035 43 protected $_customGroupExtends = array(
21dfd5f5 44 'Pledge',
9d72cede 45 );
f16bf0fa 46
74cf4551 47 /**
74cf4551
EM
48 */
49 /**
74cf4551 50 */
00be9182 51 public function __construct() {
d62fab33
RN
52
53 // Check if CiviCampaign is a) enabled and b) has active campaigns
54 $config = CRM_Core_Config::singleton();
55 $campaignEnabled = in_array("CiviCampaign", $config->enableComponents);
56 if ($campaignEnabled) {
57 $getCampaigns = CRM_Campaign_BAO_Campaign::getPermissionedCampaigns(NULL, NULL, TRUE, FALSE, TRUE);
58 $this->activeCampaigns = $getCampaigns['campaigns'];
59 asort($this->activeCampaigns);
60 }
61
6a488035 62 $this->_columns = array(
9d72cede 63 'civicrm_contact' => array(
6a488035 64 'dao' => 'CRM_Contact_DAO_Contact',
9d72cede
EM
65 'fields' => array(
66 'sort_name' => array(
67 'title' => ts('Constituent Name'),
6a488035
TO
68 'required' => TRUE,
69 'no_repeat' => TRUE,
70 ),
9d72cede 71 'id' => array(
6a488035
TO
72 'no_display' => TRUE,
73 'required' => TRUE,
74 ),
75 ),
76 'grouping' => 'contact-fields',
77 ),
9d72cede 78 'civicrm_pledge' => array(
6a488035 79 'dao' => 'CRM_Pledge_DAO_Pledge',
9d72cede
EM
80 'fields' => array(
81 'pledge_create_date' => array(
82 'title' => ts('Pledge Made'),
6a488035
TO
83 'required' => TRUE,
84 ),
9d72cede
EM
85 'financial_type_id' => array(
86 'title' => ts('Financial Type'),
80c17053
E
87 'required' => TRUE,
88 ),
89 'frequency_unit' => array(
90 'title' => ts('Frequency Unit'),
91 ),
92 'installments' => array(
93 'title' => ts('Installments'),
6a488035 94 ),
9d72cede
EM
95 'amount' => array(
96 'title' => ts('Amount'),
6a488035
TO
97 'required' => TRUE,
98 'type' => CRM_Utils_Type::T_MONEY,
99 ),
9d72cede 100 'currency' => array(
7a961f19 101 'required' => TRUE,
102 'no_display' => TRUE,
103 ),
9d72cede
EM
104 'status_id' => array(
105 'title' => ts('Status'),
6a488035
TO
106 ),
107 ),
9d72cede
EM
108 'filters' => array(
109 'pledge_create_date' => array(
f16bf0fa 110 'title' => ts('Pledge Made'),
6a488035 111 'operatorType' => CRM_Report_Form::OP_DATE,
7a961f19 112 ),
80c17053
E
113 'pledge_amount' => array(
114 'title' => ts('Pledged Amount'),
115 'operatorType' => CRM_Report_Form::OP_INT,
116 ),
9d72cede 117 'currency' => array(
ccc29f8e 118 'title' => ts('Currency'),
f3bc9ff5 119 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
0637f1c4 120 'options' => CRM_Core_OptionGroup::values('currencies_enabled'),
d1b0931b 121 'default' => NULL,
f3bc9ff5 122 'type' => CRM_Utils_Type::T_STRING,
6a488035 123 ),
9d72cede
EM
124 'financial_type_id' => array(
125 'title' => ts('Financial Type'),
8ee006e7 126 'type' => CRM_Utils_Type::T_INT,
6a488035 127 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
9d72cede 128 'options' => CRM_Contribute_PseudoConstant::financialType(),
6a488035 129 ),
80c17053
E
130 'pledge_status_id' => array(
131 'name' => 'status_id',
132 'title' => ts('Pledge Status'),
133 'type' => CRM_Utils_Type::T_INT,
134 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
135 'options' => CRM_Core_OptionGroup::values('contribution_status'),
136 ),
137 'installments' => array(
138 'title' => ts('Installments'),
139 'type' => CRM_Utils_Type::T_INT,
140 ),
6a488035
TO
141 ),
142 'grouping' => 'pledge-fields',
143 ),
9d72cede 144 'civicrm_pledge_payment' => array(
6a488035 145 'dao' => 'CRM_Pledge_DAO_PledgePayment',
9d72cede 146 'fields' => array(
688ab7ab
E
147 'scheduled_date' => array(
148 'title' => ts('Next Payment Due'),
6a488035
TO
149 'type' => CRM_Utils_Type::T_DATE,
150 'required' => TRUE,
151 ),
36aff7b7
E
152 'scheduled_amount' => array(
153 'type' => CRM_Utils_Type::T_MONEY,
ccc29f8e 154 'title' => ts('Next Payment Amount'),
36aff7b7 155 ),
6a488035 156 ),
9d72cede
EM
157 'filters' => array(
158 'scheduled_date' => array(
e8b1c881 159 'title' => ts('Next Payment Due'),
160 'operatorType' => CRM_Report_Form::OP_DATE,
688ab7ab 161 'type' => CRM_Utils_Type::T_DATE,
e8b1c881 162 ),
163 ),
6a488035
TO
164 'grouping' => 'pledge-fields',
165 ),
9d72cede 166 'civicrm_address' => array(
6a488035 167 'dao' => 'CRM_Core_DAO_Address',
9d72cede 168 'fields' => array(
6a488035
TO
169 'street_address' => NULL,
170 'city' => NULL,
171 'postal_code' => NULL,
9d72cede
EM
172 'state_province_id' => array(
173 'title' => ts('State/Province'),
6a488035 174 ),
9d72cede
EM
175 'country_id' => array(
176 'title' => ts('Country'),
6a488035
TO
177 'default' => TRUE,
178 ),
179 ),
180 'grouping' => 'contact-fields',
181 ),
9d72cede 182 'civicrm_email' => array(
6a488035 183 'dao' => 'CRM_Core_DAO_Email',
9d72cede 184 'fields' => array('email' => NULL),
6a488035
TO
185 'grouping' => 'contact-fields',
186 ),
9d72cede 187 'civicrm_group' => array(
6a488035
TO
188 'dao' => 'CRM_Contact_DAO_Group',
189 'alias' => 'cgroup',
9d72cede
EM
190 'filters' => array(
191 'gid' => array(
6a488035
TO
192 'name' => 'group_id',
193 'title' => ts('Group'),
8ee006e7 194 'type' => CRM_Utils_Type::T_INT,
6a488035
TO
195 'group' => TRUE,
196 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
197 'options' => CRM_Core_PseudoConstant::staticGroup(),
198 ),
199 ),
200 ),
201 );
202
d62fab33
RN
203 // If we have a campaign, build out the relevant elements
204 if ($campaignEnabled && !empty($this->activeCampaigns)) {
205 $this->_columns['civicrm_pledge']['fields']['campaign_id'] = array(
ccc29f8e 206 'title' => ts('Campaign'),
d62fab33 207 'default' => 'false',
f813f78e 208 );
9d72cede
EM
209 $this->_columns['civicrm_pledge']['filters']['campaign_id'] = array(
210 'title' => ts('Campaign'),
d62fab33
RN
211 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
212 'options' => $this->activeCampaigns,
942b1d01 213 'type' => CRM_Utils_Type::T_INT,
f813f78e 214 );
d62fab33
RN
215 }
216
6a488035 217 $this->_tagFilter = TRUE;
7a961f19 218 $this->_currencyColumn = 'civicrm_pledge_currency';
6a488035
TO
219 parent::__construct();
220 }
221
00be9182 222 public function preProcess() {
fec1b4d0 223 $this->assign('reportTitle', ts('Pledged but not Paid Report'));
6a488035
TO
224 parent::preProcess();
225 }
226
00be9182 227 public function select() {
6a488035
TO
228 $select = array();
229 $this->_columnHeaders = array();
230 foreach ($this->_columns as $tableName => $table) {
231 if (array_key_exists('fields', $table)) {
232 foreach ($table['fields'] as $fieldName => $field) {
9d72cede
EM
233 if (!empty($field['required']) ||
234 !empty($this->_params['fields'][$fieldName])
235 ) {
6a488035
TO
236 // to include optional columns address and email, only if checked
237 if ($tableName == 'civicrm_address') {
238 $this->_addressField = TRUE;
239 $this->_emailField = TRUE;
240 }
241 elseif ($tableName == 'civicrm_email') {
242 $this->_emailField = TRUE;
243 }
244 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
245 $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field);
246 $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array::value('title', $field);
247 }
248 }
249 }
250 }
d1641c51 251 $this->_selectClauses = $select;
6a488035
TO
252 $this->_select = "SELECT " . implode(', ', $select) . " ";
253 }
254
00be9182 255 public function from() {
6a488035
TO
256 $this->_from = NULL;
257
258 $allStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
259 $pendingStatus = array_search('Pending', $allStatus);
260 foreach (array(
9d72cede
EM
261 'Pending',
262 'In Progress',
21dfd5f5 263 'Overdue',
9d72cede 264 ) as $statusKey) {
6a488035
TO
265 if ($key = CRM_Utils_Array::key($statusKey, $allStatus)) {
266 $unpaidStatus[] = $key;
267 }
268 }
269
270 $statusIds = implode(', ', $unpaidStatus);
271
272 $this->_from = "
273 FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom}
f16bf0fa 274 INNER JOIN civicrm_pledge {$this->_aliases['civicrm_pledge']}
6a488035 275 ON ({$this->_aliases['civicrm_pledge']}.contact_id =
f16bf0fa 276 {$this->_aliases['civicrm_contact']}.id) AND
c52be1fe 277 {$this->_aliases['civicrm_pledge']}.status_id IN ( {$statusIds} )\n";
36aff7b7
E
278
279 // Note that the derived query protects us from providing inaccurate data in the edge case where pledge
280 // payments have been edited such that they are not in id order. This might be better as a temp table.
c52be1fe 281 $this->_from .= "LEFT JOIN (SELECT * FROM civicrm_pledge_payment ORDER BY scheduled_date) as {$this->_aliases['civicrm_pledge_payment']}
6a488035
TO
282 ON ({$this->_aliases['civicrm_pledge']}.id =
283 {$this->_aliases['civicrm_pledge_payment']}.pledge_id AND {$this->_aliases['civicrm_pledge_payment']}.status_id = {$pendingStatus} ) ";
284
285 // include address field if address column is to be included
286 if ($this->_addressField) {
287 $this->_from .= "
f16bf0fa 288 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
289 ON ({$this->_aliases['civicrm_contact']}.id =
6a488035
TO
290 {$this->_aliases['civicrm_address']}.contact_id) AND
291 {$this->_aliases['civicrm_address']}.is_primary = 1\n";
292 }
293
294 // include email field if email column is to be included
295 if ($this->_emailField) {
296 $this->_from .= "
f16bf0fa 297 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
298 ON ({$this->_aliases['civicrm_contact']}.id =
299 {$this->_aliases['civicrm_email']}.contact_id) AND
6a488035
TO
300 {$this->_aliases['civicrm_email']}.is_primary = 1\n";
301 }
302 }
303
00be9182 304 public function groupBy() {
d1641c51 305 $groupBy = array(
306 "{$this->_aliases['civicrm_pledge']}.contact_id",
307 "{$this->_aliases['civicrm_pledge']}.id",
308 "{$this->_aliases['civicrm_pledge']}.currency",
309 );
b708c08d 310 $this->_groupBy = CRM_Contact_BAO_Query::getGroupByFromSelectColumns($this->_selectClauses, $groupBy);
6a488035
TO
311 }
312
00be9182 313 public function orderBy() {
6a488035
TO
314 $this->_orderBy = "ORDER BY {$this->_aliases['civicrm_contact']}.sort_name, {$this->_aliases['civicrm_pledge']}.contact_id, {$this->_aliases['civicrm_pledge']}.id";
315 }
316
00be9182 317 public function postProcess() {
6a488035
TO
318 // get the acl clauses built before we assemble the query
319 $this->buildACLClause($this->_aliases['civicrm_contact']);
320 parent::PostProcess();
321 }
322
74cf4551 323 /**
ced9bfed
EM
324 * Alter display of rows.
325 *
326 * Iterate through the rows retrieved via SQL and make changes for display purposes,
327 * such as rendering contacts as links.
328 *
329 * @param array $rows
74cf4551 330 */
00be9182 331 public function alterDisplay(&$rows) {
9d72cede
EM
332 $entryFound = FALSE;
333 $checkList = array();
6a488035
TO
334 $display_flag = $prev_cid = $cid = 0;
335
336 foreach ($rows as $rowNum => $row) {
337 if (!empty($this->_noRepeats) && $this->_outputMode != 'csv') {
338 // don't repeat contact details if its same as the previous row
339 if (array_key_exists('civicrm_contact_id', $row)) {
340 if ($cid = $row['civicrm_contact_id']) {
341 if ($rowNum == 0) {
342 $prev_cid = $cid;
343 }
344 else {
345 if ($prev_cid == $cid) {
346 $display_flag = 1;
347 $prev_cid = $cid;
348 }
349 else {
350 $display_flag = 0;
351 $prev_cid = $cid;
352 }
353 }
354
355 if ($display_flag) {
356 foreach ($row as $colName => $colVal) {
357 if (in_array($colName, $this->_noRepeats)) {
358 unset($rows[$rowNum][$colName]);
359 }
360 }
361 }
362 $entryFound = TRUE;
363 }
364 }
365 }
366
b914f4e8
PN
367 //handle the Financial Type Ids
368 if (array_key_exists('civicrm_pledge_financial_type_id', $row)) {
369 if ($value = $row['civicrm_pledge_financial_type_id']) {
6c552737 370 $rows[$rowNum]['civicrm_pledge_financial_type_id'] = CRM_Contribute_PseudoConstant::financialType($value, FALSE);
6a488035
TO
371 }
372 $entryFound = TRUE;
373 }
374
375 //handle the Status Ids
376 if (array_key_exists('civicrm_pledge_status_id', $row)) {
377 if ($value = $row['civicrm_pledge_status_id']) {
378 $rows[$rowNum]['civicrm_pledge_status_id'] = CRM_Contribute_PseudoConstant::contributionStatus($value);
379 }
380 $entryFound = TRUE;
381 }
382
383 // handle state province
384 if (array_key_exists('civicrm_address_state_province_id', $row)) {
385 if ($value = $row['civicrm_address_state_province_id']) {
386 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant::stateProvinceAbbreviation($value, FALSE);
387 }
388 $entryFound = TRUE;
389 }
390
391 // handle country
392 if (array_key_exists('civicrm_address_country_id', $row)) {
393 if ($value = $row['civicrm_address_country_id']) {
394 $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant::country($value, FALSE);
395 }
396 $entryFound = TRUE;
397 }
398
399 // convert display name to links
400 if (array_key_exists('civicrm_contact_sort_name', $row) &&
401 array_key_exists('civicrm_contact_id', $row)
402 ) {
403 $url = CRM_Report_Utils_Report::getNextUrl('pledge/summary',
404 'reset=1&force=1&id_op=eq&id_value=' .
405 $row['civicrm_contact_id'],
406 $this->_absoluteUrl, $this->_id, $this->_drilldownReport
407 );
408 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
409 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Pledge Details for this contact");
410 $entryFound = TRUE;
411 }
412
d62fab33
RN
413 // If using campaigns, convert campaign_id to campaign title
414 if (array_key_exists('civicrm_pledge_campaign_id', $row)) {
415 if ($value = $row['civicrm_pledge_campaign_id']) {
416 $rows[$rowNum]['civicrm_pledge_campaign_id'] = $this->activeCampaigns[$value];
417 }
418 $entryFound = TRUE;
419 }
420
6a488035
TO
421 // skip looking further in rows, if first row itself doesn't
422 // have the column we need
423 if (!$entryFound) {
424 break;
425 }
426 }
427 }
96025800 428
6a488035 429}