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