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