Merge pull request #13967 from eileenmcnaughton/activity_token
[civicrm-core.git] / CRM / Report / Form / Pledge / Pbnp.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
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
6b83d5bd 31 * @copyright CiviCRM LLC (c) 2004-2019
6a488035
TO
32 */
33class CRM_Report_Form_Pledge_Pbnp extends CRM_Report_Form {
34 protected $_charts = array(
35 '' => 'Tabular',
36 'barChart' => 'Bar Chart',
37 'pieChart' => 'Pie Chart',
38 );
39 public $_drilldownReport = array('pledge/summary' => 'Link to Detail Report');
f16bf0fa 40
6a488035 41 protected $_customGroupExtends = array(
21dfd5f5 42 'Pledge',
9d72cede 43 );
f16bf0fa 44
74cf4551 45 /**
73b448bf 46 * Class constructor.
74cf4551 47 */
00be9182 48 public function __construct() {
6a488035 49 $this->_columns = array(
9d72cede 50 'civicrm_contact' => array(
6a488035 51 'dao' => 'CRM_Contact_DAO_Contact',
9d72cede
EM
52 'fields' => array(
53 'sort_name' => array(
54 'title' => ts('Constituent Name'),
6a488035
TO
55 'required' => TRUE,
56 'no_repeat' => TRUE,
57 ),
9d72cede 58 'id' => array(
6a488035
TO
59 'no_display' => TRUE,
60 'required' => TRUE,
61 ),
62 ),
63 'grouping' => 'contact-fields',
64 ),
9d72cede 65 'civicrm_pledge' => array(
6a488035 66 'dao' => 'CRM_Pledge_DAO_Pledge',
9d72cede
EM
67 'fields' => array(
68 'pledge_create_date' => array(
69 'title' => ts('Pledge Made'),
6a488035
TO
70 'required' => TRUE,
71 ),
9d72cede
EM
72 'financial_type_id' => array(
73 'title' => ts('Financial Type'),
80c17053
E
74 'required' => TRUE,
75 ),
76 'frequency_unit' => array(
77 'title' => ts('Frequency Unit'),
78 ),
79 'installments' => array(
80 'title' => ts('Installments'),
6a488035 81 ),
9d72cede
EM
82 'amount' => array(
83 'title' => ts('Amount'),
6a488035
TO
84 'required' => TRUE,
85 'type' => CRM_Utils_Type::T_MONEY,
86 ),
9d72cede 87 'currency' => array(
7a961f19 88 'required' => TRUE,
89 'no_display' => TRUE,
90 ),
9d72cede
EM
91 'status_id' => array(
92 'title' => ts('Status'),
6a488035
TO
93 ),
94 ),
9d72cede
EM
95 'filters' => array(
96 'pledge_create_date' => array(
f16bf0fa 97 'title' => ts('Pledge Made'),
6a488035 98 'operatorType' => CRM_Report_Form::OP_DATE,
7a961f19 99 ),
80c17053
E
100 'pledge_amount' => array(
101 'title' => ts('Pledged Amount'),
102 'operatorType' => CRM_Report_Form::OP_INT,
103 ),
9d72cede 104 'currency' => array(
ccc29f8e 105 'title' => ts('Currency'),
f3bc9ff5 106 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
0637f1c4 107 'options' => CRM_Core_OptionGroup::values('currencies_enabled'),
d1b0931b 108 'default' => NULL,
f3bc9ff5 109 'type' => CRM_Utils_Type::T_STRING,
6a488035 110 ),
9d72cede
EM
111 'financial_type_id' => array(
112 'title' => ts('Financial Type'),
8ee006e7 113 'type' => CRM_Utils_Type::T_INT,
6a488035 114 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
9d72cede 115 'options' => CRM_Contribute_PseudoConstant::financialType(),
6a488035 116 ),
80c17053
E
117 'pledge_status_id' => array(
118 'name' => 'status_id',
119 'title' => ts('Pledge Status'),
120 'type' => CRM_Utils_Type::T_INT,
121 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
01dac399 122 'options' => CRM_Core_OptionGroup::values('pledge_status'),
80c17053
E
123 ),
124 'installments' => array(
125 'title' => ts('Installments'),
126 'type' => CRM_Utils_Type::T_INT,
127 ),
6a488035
TO
128 ),
129 'grouping' => 'pledge-fields',
130 ),
9d72cede 131 'civicrm_pledge_payment' => array(
6a488035 132 'dao' => 'CRM_Pledge_DAO_PledgePayment',
9d72cede 133 'fields' => array(
688ab7ab
E
134 'scheduled_date' => array(
135 'title' => ts('Next Payment Due'),
6a488035
TO
136 'type' => CRM_Utils_Type::T_DATE,
137 'required' => TRUE,
138 ),
36aff7b7
E
139 'scheduled_amount' => array(
140 'type' => CRM_Utils_Type::T_MONEY,
ccc29f8e 141 'title' => ts('Next Payment Amount'),
36aff7b7 142 ),
6a488035 143 ),
9d72cede
EM
144 'filters' => array(
145 'scheduled_date' => array(
e8b1c881 146 'title' => ts('Next Payment Due'),
147 'operatorType' => CRM_Report_Form::OP_DATE,
688ab7ab 148 'type' => CRM_Utils_Type::T_DATE,
e8b1c881 149 ),
150 ),
6a488035
TO
151 'grouping' => 'pledge-fields',
152 ),
9d72cede 153 'civicrm_address' => array(
6a488035 154 'dao' => 'CRM_Core_DAO_Address',
9d72cede 155 'fields' => array(
6a488035
TO
156 'street_address' => NULL,
157 'city' => NULL,
158 'postal_code' => NULL,
9d72cede
EM
159 'state_province_id' => array(
160 'title' => ts('State/Province'),
6a488035 161 ),
9d72cede
EM
162 'country_id' => array(
163 'title' => ts('Country'),
6a488035
TO
164 'default' => TRUE,
165 ),
166 ),
167 'grouping' => 'contact-fields',
168 ),
9d72cede 169 'civicrm_email' => array(
6a488035 170 'dao' => 'CRM_Core_DAO_Email',
9d72cede 171 'fields' => array('email' => NULL),
6a488035
TO
172 'grouping' => 'contact-fields',
173 ),
6a488035
TO
174 );
175
d62fab33 176 // If we have a campaign, build out the relevant elements
bbb5f671 177 $this->addCampaignFields('civicrm_pledge');
d62fab33 178
321b8a10 179 $this->_groupFilter = TRUE;
6a488035 180 $this->_tagFilter = TRUE;
7a961f19 181 $this->_currencyColumn = 'civicrm_pledge_currency';
6a488035
TO
182 parent::__construct();
183 }
184
00be9182 185 public function preProcess() {
fec1b4d0 186 $this->assign('reportTitle', ts('Pledged but not Paid Report'));
6a488035
TO
187 parent::preProcess();
188 }
189
00be9182 190 public function select() {
6a488035
TO
191 $select = array();
192 $this->_columnHeaders = array();
193 foreach ($this->_columns as $tableName => $table) {
194 if (array_key_exists('fields', $table)) {
195 foreach ($table['fields'] as $fieldName => $field) {
9d72cede
EM
196 if (!empty($field['required']) ||
197 !empty($this->_params['fields'][$fieldName])
198 ) {
6a488035
TO
199 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
200 $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field);
201 $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array::value('title', $field);
202 }
203 }
204 }
205 }
d1641c51 206 $this->_selectClauses = $select;
6a488035
TO
207 $this->_select = "SELECT " . implode(', ', $select) . " ";
208 }
209
00be9182 210 public function from() {
6a488035
TO
211 $this->_from = NULL;
212
213 $allStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
214 $pendingStatus = array_search('Pending', $allStatus);
215 foreach (array(
9d72cede
EM
216 'Pending',
217 'In Progress',
21dfd5f5 218 'Overdue',
9d72cede 219 ) as $statusKey) {
6a488035
TO
220 if ($key = CRM_Utils_Array::key($statusKey, $allStatus)) {
221 $unpaidStatus[] = $key;
222 }
223 }
224
225 $statusIds = implode(', ', $unpaidStatus);
226
227 $this->_from = "
228 FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom}
f16bf0fa 229 INNER JOIN civicrm_pledge {$this->_aliases['civicrm_pledge']}
6a488035 230 ON ({$this->_aliases['civicrm_pledge']}.contact_id =
f16bf0fa 231 {$this->_aliases['civicrm_contact']}.id) AND
c52be1fe 232 {$this->_aliases['civicrm_pledge']}.status_id IN ( {$statusIds} )\n";
36aff7b7
E
233
234 // Note that the derived query protects us from providing inaccurate data in the edge case where pledge
235 // payments have been edited such that they are not in id order. This might be better as a temp table.
c52be1fe 236 $this->_from .= "LEFT JOIN (SELECT * FROM civicrm_pledge_payment ORDER BY scheduled_date) as {$this->_aliases['civicrm_pledge_payment']}
6a488035
TO
237 ON ({$this->_aliases['civicrm_pledge']}.id =
238 {$this->_aliases['civicrm_pledge_payment']}.pledge_id AND {$this->_aliases['civicrm_pledge_payment']}.status_id = {$pendingStatus} ) ";
239
cd2426ba 240 $this->joinAddressFromContact();
241 $this->joinEmailFromContact();
6a488035 242
6a488035
TO
243 }
244
00be9182 245 public function groupBy() {
d1641c51 246 $groupBy = array(
247 "{$this->_aliases['civicrm_pledge']}.contact_id",
248 "{$this->_aliases['civicrm_pledge']}.id",
249 "{$this->_aliases['civicrm_pledge']}.currency",
250 );
b708c08d 251 $this->_groupBy = CRM_Contact_BAO_Query::getGroupByFromSelectColumns($this->_selectClauses, $groupBy);
6a488035
TO
252 }
253
00be9182 254 public function orderBy() {
6a488035
TO
255 $this->_orderBy = "ORDER BY {$this->_aliases['civicrm_contact']}.sort_name, {$this->_aliases['civicrm_pledge']}.contact_id, {$this->_aliases['civicrm_pledge']}.id";
256 }
257
00be9182 258 public function postProcess() {
6a488035
TO
259 // get the acl clauses built before we assemble the query
260 $this->buildACLClause($this->_aliases['civicrm_contact']);
261 parent::PostProcess();
262 }
263
74cf4551 264 /**
ced9bfed
EM
265 * Alter display of rows.
266 *
267 * Iterate through the rows retrieved via SQL and make changes for display purposes,
268 * such as rendering contacts as links.
269 *
270 * @param array $rows
74cf4551 271 */
00be9182 272 public function alterDisplay(&$rows) {
9d72cede
EM
273 $entryFound = FALSE;
274 $checkList = array();
6a488035
TO
275 $display_flag = $prev_cid = $cid = 0;
276
277 foreach ($rows as $rowNum => $row) {
278 if (!empty($this->_noRepeats) && $this->_outputMode != 'csv') {
279 // don't repeat contact details if its same as the previous row
280 if (array_key_exists('civicrm_contact_id', $row)) {
281 if ($cid = $row['civicrm_contact_id']) {
282 if ($rowNum == 0) {
283 $prev_cid = $cid;
284 }
285 else {
286 if ($prev_cid == $cid) {
287 $display_flag = 1;
288 $prev_cid = $cid;
289 }
290 else {
291 $display_flag = 0;
292 $prev_cid = $cid;
293 }
294 }
295
296 if ($display_flag) {
297 foreach ($row as $colName => $colVal) {
298 if (in_array($colName, $this->_noRepeats)) {
299 unset($rows[$rowNum][$colName]);
300 }
301 }
302 }
303 $entryFound = TRUE;
304 }
305 }
306 }
307
b914f4e8
PN
308 //handle the Financial Type Ids
309 if (array_key_exists('civicrm_pledge_financial_type_id', $row)) {
310 if ($value = $row['civicrm_pledge_financial_type_id']) {
6c552737 311 $rows[$rowNum]['civicrm_pledge_financial_type_id'] = CRM_Contribute_PseudoConstant::financialType($value, FALSE);
6a488035
TO
312 }
313 $entryFound = TRUE;
314 }
315
316 //handle the Status Ids
317 if (array_key_exists('civicrm_pledge_status_id', $row)) {
318 if ($value = $row['civicrm_pledge_status_id']) {
01dac399 319 $rows[$rowNum]['civicrm_pledge_status_id'] = CRM_Core_PseudoConstant::getLabel('CRM_Pledge_BAO_Pledge', 'status_id', $value);
6a488035
TO
320 }
321 $entryFound = TRUE;
322 }
323
6a488035
TO
324 // convert display name to links
325 if (array_key_exists('civicrm_contact_sort_name', $row) &&
326 array_key_exists('civicrm_contact_id', $row)
327 ) {
328 $url = CRM_Report_Utils_Report::getNextUrl('pledge/summary',
329 'reset=1&force=1&id_op=eq&id_value=' .
330 $row['civicrm_contact_id'],
331 $this->_absoluteUrl, $this->_id, $this->_drilldownReport
332 );
333 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
334 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Pledge Details for this contact");
335 $entryFound = TRUE;
336 }
337
d62fab33
RN
338 // If using campaigns, convert campaign_id to campaign title
339 if (array_key_exists('civicrm_pledge_campaign_id', $row)) {
340 if ($value = $row['civicrm_pledge_campaign_id']) {
bbb5f671 341 $rows[$rowNum]['civicrm_pledge_campaign_id'] = $this->campaigns[$value];
d62fab33
RN
342 }
343 $entryFound = TRUE;
344 }
345
96d30001 346 $entryFound = $this->alterDisplayAddressFields($row, $rows, $rowNum, NULL, NULL) ? TRUE : $entryFound;
6a488035
TO
347 // skip looking further in rows, if first row itself doesn't
348 // have the column we need
349 if (!$entryFound) {
350 break;
351 }
352 }
353 }
96025800 354
6a488035 355}