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