Merge pull request #8612 from nishant-bhorodia/CRM-18688-cancel-ipn-signal-cancels...
[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-2016 |
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-2016
32 * $Id$
33 *
34 */
35 class 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');
42
43 protected $_customGroupExtends = array(
44 'Pledge',
45 );
46
47 /**
48 */
49 /**
50 */
51 public function __construct() {
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
62 $this->_columns = array(
63 'civicrm_contact' => array(
64 'dao' => 'CRM_Contact_DAO_Contact',
65 'fields' => array(
66 'sort_name' => array(
67 'title' => ts('Constituent Name'),
68 'required' => TRUE,
69 'no_repeat' => TRUE,
70 ),
71 'id' => array(
72 'no_display' => TRUE,
73 'required' => TRUE,
74 ),
75 ),
76 'grouping' => 'contact-fields',
77 ),
78 'civicrm_pledge' => array(
79 'dao' => 'CRM_Pledge_DAO_Pledge',
80 'fields' => array(
81 'pledge_create_date' => array(
82 'title' => ts('Pledge Made'),
83 'required' => TRUE,
84 ),
85 'financial_type_id' => array(
86 'title' => ts('Financial Type'),
87 'required' => TRUE,
88 ),
89 'frequency_unit' => array(
90 'title' => ts('Frequency Unit'),
91 ),
92 'installments' => array(
93 'title' => ts('Installments'),
94 ),
95 'amount' => array(
96 'title' => ts('Amount'),
97 'required' => TRUE,
98 'type' => CRM_Utils_Type::T_MONEY,
99 ),
100 'currency' => array(
101 'required' => TRUE,
102 'no_display' => TRUE,
103 ),
104 'status_id' => array(
105 'title' => ts('Status'),
106 ),
107 ),
108 'filters' => array(
109 'pledge_create_date' => array(
110 'title' => ts('Pledge Made'),
111 'operatorType' => CRM_Report_Form::OP_DATE,
112 ),
113 'pledge_amount' => array(
114 'title' => ts('Pledged Amount'),
115 'operatorType' => CRM_Report_Form::OP_INT,
116 ),
117 'currency' => array(
118 'title' => 'Currency',
119 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
120 'options' => CRM_Core_OptionGroup::values('currencies_enabled'),
121 'default' => NULL,
122 'type' => CRM_Utils_Type::T_STRING,
123 ),
124 'financial_type_id' => array(
125 'title' => ts('Financial Type'),
126 'type' => CRM_Utils_Type::T_INT,
127 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
128 'options' => CRM_Contribute_PseudoConstant::financialType(),
129 ),
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 ),
141 ),
142 'grouping' => 'pledge-fields',
143 ),
144 'civicrm_pledge_payment' => array(
145 'dao' => 'CRM_Pledge_DAO_PledgePayment',
146 'fields' => array(
147 'scheduled_date' => array(
148 'title' => ts('Next Payment Due'),
149 'type' => CRM_Utils_Type::T_DATE,
150 'required' => TRUE,
151 ),
152 'scheduled_amount' => array(
153 'type' => CRM_Utils_Type::T_MONEY,
154 'title' => 'Next Payment Amount',
155 ),
156 ),
157 'filters' => array(
158 'scheduled_date' => array(
159 'title' => ts('Next Payment Due'),
160 'operatorType' => CRM_Report_Form::OP_DATE,
161 'type' => CRM_Utils_Type::T_DATE,
162 ),
163 ),
164 'grouping' => 'pledge-fields',
165 ),
166 'civicrm_address' => array(
167 'dao' => 'CRM_Core_DAO_Address',
168 'fields' => array(
169 'street_address' => NULL,
170 'city' => NULL,
171 'postal_code' => NULL,
172 'state_province_id' => array(
173 'title' => ts('State/Province'),
174 ),
175 'country_id' => array(
176 'title' => ts('Country'),
177 'default' => TRUE,
178 ),
179 ),
180 'grouping' => 'contact-fields',
181 ),
182 'civicrm_email' => array(
183 'dao' => 'CRM_Core_DAO_Email',
184 'fields' => array('email' => NULL),
185 'grouping' => 'contact-fields',
186 ),
187 'civicrm_group' => array(
188 'dao' => 'CRM_Contact_DAO_Group',
189 'alias' => 'cgroup',
190 'filters' => array(
191 'gid' => array(
192 'name' => 'group_id',
193 'title' => ts('Group'),
194 'type' => CRM_Utils_Type::T_INT,
195 'group' => TRUE,
196 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
197 'options' => CRM_Core_PseudoConstant::staticGroup(),
198 ),
199 ),
200 ),
201 );
202
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(
206 'title' => 'Campaign',
207 'default' => 'false',
208 );
209 $this->_columns['civicrm_pledge']['filters']['campaign_id'] = array(
210 'title' => ts('Campaign'),
211 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
212 'options' => $this->activeCampaigns,
213 'type' => CRM_Utils_Type::T_INT,
214 );
215 }
216
217 $this->_tagFilter = TRUE;
218 $this->_currencyColumn = 'civicrm_pledge_currency';
219 parent::__construct();
220 }
221
222 public function preProcess() {
223 $this->assign('reportTitle', ts('Pledged but not Paid Report'));
224 parent::preProcess();
225 }
226
227 public function select() {
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) {
233 if (!empty($field['required']) ||
234 !empty($this->_params['fields'][$fieldName])
235 ) {
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 }
251 $this->_select = "SELECT " . implode(', ', $select) . " ";
252 }
253
254 public function from() {
255 $this->_from = NULL;
256
257 $allStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
258 $pendingStatus = array_search('Pending', $allStatus);
259 foreach (array(
260 'Pending',
261 'In Progress',
262 'Overdue',
263 ) as $statusKey) {
264 if ($key = CRM_Utils_Array::key($statusKey, $allStatus)) {
265 $unpaidStatus[] = $key;
266 }
267 }
268
269 $statusIds = implode(', ', $unpaidStatus);
270
271 $this->_from = "
272 FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom}
273 INNER JOIN civicrm_pledge {$this->_aliases['civicrm_pledge']}
274 ON ({$this->_aliases['civicrm_pledge']}.contact_id =
275 {$this->_aliases['civicrm_contact']}.id) AND
276 {$this->_aliases['civicrm_pledge']}.status_id IN ( {$statusIds} )\n";
277
278 // Note that the derived query protects us from providing inaccurate data in the edge case where pledge
279 // payments have been edited such that they are not in id order. This might be better as a temp table.
280 $this->_from .= "LEFT JOIN (SELECT * FROM civicrm_pledge_payment ORDER BY scheduled_date) as {$this->_aliases['civicrm_pledge_payment']}
281 ON ({$this->_aliases['civicrm_pledge']}.id =
282 {$this->_aliases['civicrm_pledge_payment']}.pledge_id AND {$this->_aliases['civicrm_pledge_payment']}.status_id = {$pendingStatus} ) ";
283
284 // include address field if address column is to be included
285 if ($this->_addressField) {
286 $this->_from .= "
287 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
288 ON ({$this->_aliases['civicrm_contact']}.id =
289 {$this->_aliases['civicrm_address']}.contact_id) AND
290 {$this->_aliases['civicrm_address']}.is_primary = 1\n";
291 }
292
293 // include email field if email column is to be included
294 if ($this->_emailField) {
295 $this->_from .= "
296 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
297 ON ({$this->_aliases['civicrm_contact']}.id =
298 {$this->_aliases['civicrm_email']}.contact_id) AND
299 {$this->_aliases['civicrm_email']}.is_primary = 1\n";
300 }
301 }
302
303 public function groupBy() {
304 $this->_groupBy = "
305 GROUP BY {$this->_aliases['civicrm_pledge']}.contact_id,
306 {$this->_aliases['civicrm_pledge']}.id,
307 {$this->_aliases['civicrm_pledge']}.currency";
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 }