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