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