Merge pull request #851 from eileenmcnaughton/CRM-12686
[civicrm-core.git] / CRM / Report / Form / Pledge / Pbnp.php
CommitLineData
6a488035
TO
1<?php
2// $Id$
3
4/*
5 +--------------------------------------------------------------------+
6 | CiviCRM version 4.3 |
7 +--------------------------------------------------------------------+
8 | Copyright CiviCRM LLC (c) 2004-2013 |
9 +--------------------------------------------------------------------+
10 | This file is a part of CiviCRM. |
11 | |
12 | CiviCRM is free software; you can copy, modify, and distribute it |
13 | under the terms of the GNU Affero General Public License |
14 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
15 | |
16 | CiviCRM is distributed in the hope that it will be useful, but |
17 | WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
19 | See the GNU Affero General Public License for more details. |
20 | |
21 | You should have received a copy of the GNU Affero General Public |
22 | License and the CiviCRM Licensing Exception along |
23 | with this program; if not, contact CiviCRM LLC |
24 | at info[AT]civicrm[DOT]org. If you have questions about the |
25 | GNU Affero General Public License or the licensing of CiviCRM, |
26 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
27 +--------------------------------------------------------------------+
28*/
29
30/**
31 *
32 * @package CRM
33 * @copyright CiviCRM LLC (c) 2004-2013
34 * $Id$
35 *
36 */
37class CRM_Report_Form_Pledge_Pbnp extends CRM_Report_Form {
38 protected $_charts = array(
39 '' => 'Tabular',
40 'barChart' => 'Bar Chart',
41 'pieChart' => 'Pie Chart',
42 );
43 public $_drilldownReport = array('pledge/summary' => 'Link to Detail Report');
f16bf0fa 44
6a488035
TO
45 protected $_customGroupExtends = array(
46 'Pledge');
f16bf0fa 47
6a488035 48 function __construct() {
d62fab33
RN
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
6a488035
TO
59 $this->_columns = array(
60 'civicrm_contact' =>
61 array(
62 'dao' => 'CRM_Contact_DAO_Contact',
63 'fields' =>
64 array(
65 'sort_name' =>
66 array('title' => ts('Constituent Name'),
67 'required' => TRUE,
68 'no_repeat' => TRUE,
69 ),
70 'id' =>
71 array(
72 'no_display' => TRUE,
73 'required' => TRUE,
74 ),
75 ),
76 'grouping' => 'contact-fields',
77 ),
78 'civicrm_pledge' =>
79 array(
80 'dao' => 'CRM_Pledge_DAO_Pledge',
81 'fields' =>
82 array(
83 'pledge_create_date' =>
84 array('title' => ts('Pledge Made'),
85 'required' => TRUE,
86 ),
f3bc9ff5 87 'financial_type_id' =>
b914f4e8 88 array('title' => ts('Financial Type'),
6a488035
TO
89 'requried' => TRUE,
90 ),
91 'amount' =>
92 array('title' => ts('Amount'),
93 'required' => TRUE,
94 'type' => CRM_Utils_Type::T_MONEY,
95 ),
7a961f19 96 'currency' =>
97 array(
98 'required' => TRUE,
99 'no_display' => TRUE,
100 ),
6a488035
TO
101 'status_id' =>
102 array('title' => ts('Status'),
103 ),
104 ),
105 'filters' =>
106 array(
107 'pledge_create_date' =>
108 array(
f16bf0fa 109 'title' => ts('Pledge Made'),
6a488035 110 'operatorType' => CRM_Report_Form::OP_DATE,
7a961f19 111 ),
112 'currency' =>
113 array('title' => 'Currency',
f3bc9ff5 114 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
0637f1c4 115 'options' => CRM_Core_OptionGroup::values('currencies_enabled'),
d1b0931b 116 'default' => NULL,
f3bc9ff5 117 'type' => CRM_Utils_Type::T_STRING,
6a488035 118 ),
f3bc9ff5 119 'financial_type_id' =>
120 array( 'title' => ts('Financial Type'),
6a488035 121 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
f3bc9ff5 122 'options' => CRM_Contribute_PseudoConstant::financialType(),
6a488035
TO
123 ),
124 ),
125 'grouping' => 'pledge-fields',
126 ),
127 'civicrm_pledge_payment' =>
128 array(
129 'dao' => 'CRM_Pledge_DAO_PledgePayment',
130 'fields' =>
131 array(
132 'scheduled_date' =>
133 array('title' => ts('Next Payment Due'),
134 'type' => CRM_Utils_Type::T_DATE,
135 'required' => TRUE,
136 ),
137 ),
e8b1c881 138 'filters' =>
139 array(
140 'scheduled_date' =>
141 array(
142 'title' => ts('Next Payment Due'),
143 'operatorType' => CRM_Report_Form::OP_DATE,
144 ),
145 ),
6a488035
TO
146 'grouping' => 'pledge-fields',
147 ),
148 'civicrm_address' =>
149 array(
150 'dao' => 'CRM_Core_DAO_Address',
151 'fields' =>
152 array(
153 'street_address' => NULL,
154 'city' => NULL,
155 'postal_code' => NULL,
156 'state_province_id' =>
157 array('title' => ts('State/Province'),
158 ),
159 'country_id' =>
160 array('title' => ts('Country'),
161 'default' => TRUE,
162 ),
163 ),
164 'grouping' => 'contact-fields',
165 ),
166 'civicrm_email' =>
167 array(
168 'dao' => 'CRM_Core_DAO_Email',
169 'fields' =>
170 array('email' => NULL),
171 'grouping' => 'contact-fields',
172 ),
173 'civicrm_group' =>
174 array(
175 'dao' => 'CRM_Contact_DAO_Group',
176 'alias' => 'cgroup',
177 'filters' =>
178 array(
179 'gid' =>
180 array(
181 'name' => 'group_id',
182 'title' => ts('Group'),
183 'group' => TRUE,
184 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
185 'options' => CRM_Core_PseudoConstant::staticGroup(),
186 ),
187 ),
188 ),
189 );
190
d62fab33
RN
191 // If we have a campaign, build out the relevant elements
192 if ($campaignEnabled && !empty($this->activeCampaigns)) {
193 $this->_columns['civicrm_pledge']['fields']['campaign_id'] = array(
194 'title' => 'Campaign',
195 'default' => 'false',
196 );
197 $this->_columns['civicrm_pledge']['filters']['campaign_id'] = array('title' => ts('Campaign'),
198 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
199 'options' => $this->activeCampaigns,
200 );
201 }
202
6a488035 203 $this->_tagFilter = TRUE;
7a961f19 204 $this->_currencyColumn = 'civicrm_pledge_currency';
6a488035
TO
205 parent::__construct();
206 }
207
208 function preProcess() {
209 $this->assign('reportTitle', ts('Pledge But Not Paid Report'));
210 parent::preProcess();
211 }
212
213 function select() {
214 $select = array();
215 $this->_columnHeaders = array();
216 foreach ($this->_columns as $tableName => $table) {
217 if (array_key_exists('fields', $table)) {
218 foreach ($table['fields'] as $fieldName => $field) {
219 if (CRM_Utils_Array::value('required', $field) ||
220 CRM_Utils_Array::value($fieldName, $this->_params['fields'])
221 ) {
222 // to include optional columns address and email, only if checked
223 if ($tableName == 'civicrm_address') {
224 $this->_addressField = TRUE;
225 $this->_emailField = TRUE;
226 }
227 elseif ($tableName == 'civicrm_email') {
228 $this->_emailField = TRUE;
229 }
230 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
231 $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field);
232 $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array::value('title', $field);
233 }
234 }
235 }
236 }
237 $this->_select = "SELECT " . implode(', ', $select) . " ";
238 }
239
240 function from() {
241 $this->_from = NULL;
242
243 $allStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
244 $pendingStatus = array_search('Pending', $allStatus);
245 foreach (array(
246 'Pending', 'In Progress', 'Overdue') as $statusKey) {
247 if ($key = CRM_Utils_Array::key($statusKey, $allStatus)) {
248 $unpaidStatus[] = $key;
249 }
250 }
251
252 $statusIds = implode(', ', $unpaidStatus);
253
254 $this->_from = "
255 FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom}
f16bf0fa 256 INNER JOIN civicrm_pledge {$this->_aliases['civicrm_pledge']}
6a488035 257 ON ({$this->_aliases['civicrm_pledge']}.contact_id =
f16bf0fa 258 {$this->_aliases['civicrm_contact']}.id) AND
6a488035
TO
259 {$this->_aliases['civicrm_pledge']}.status_id IN ( {$statusIds} )
260 LEFT JOIN civicrm_pledge_payment {$this->_aliases['civicrm_pledge_payment']}
261 ON ({$this->_aliases['civicrm_pledge']}.id =
262 {$this->_aliases['civicrm_pledge_payment']}.pledge_id AND {$this->_aliases['civicrm_pledge_payment']}.status_id = {$pendingStatus} ) ";
263
264 // include address field if address column is to be included
265 if ($this->_addressField) {
266 $this->_from .= "
f16bf0fa 267 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
268 ON ({$this->_aliases['civicrm_contact']}.id =
6a488035
TO
269 {$this->_aliases['civicrm_address']}.contact_id) AND
270 {$this->_aliases['civicrm_address']}.is_primary = 1\n";
271 }
272
273 // include email field if email column is to be included
274 if ($this->_emailField) {
275 $this->_from .= "
f16bf0fa 276 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
277 ON ({$this->_aliases['civicrm_contact']}.id =
278 {$this->_aliases['civicrm_email']}.contact_id) AND
6a488035
TO
279 {$this->_aliases['civicrm_email']}.is_primary = 1\n";
280 }
281 }
282
283 function groupBy() {
284 $this->_groupBy = "
f16bf0fa 285 GROUP BY {$this->_aliases['civicrm_pledge']}.contact_id,
7a961f19 286 {$this->_aliases['civicrm_pledge']}.id,
287 {$this->_aliases['civicrm_pledge']}.currency";
6a488035
TO
288 }
289
290 function orderBy() {
291 $this->_orderBy = "ORDER BY {$this->_aliases['civicrm_contact']}.sort_name, {$this->_aliases['civicrm_pledge']}.contact_id, {$this->_aliases['civicrm_pledge']}.id";
292 }
293
294 function postProcess() {
295 // get the acl clauses built before we assemble the query
296 $this->buildACLClause($this->_aliases['civicrm_contact']);
297 parent::PostProcess();
298 }
299
300 function alterDisplay(&$rows) {
301 // custom code to alter rows
302 $entryFound = FALSE;
303 $checkList = array();
304 $display_flag = $prev_cid = $cid = 0;
305
306 foreach ($rows as $rowNum => $row) {
307 if (!empty($this->_noRepeats) && $this->_outputMode != 'csv') {
308 // don't repeat contact details if its same as the previous row
309 if (array_key_exists('civicrm_contact_id', $row)) {
310 if ($cid = $row['civicrm_contact_id']) {
311 if ($rowNum == 0) {
312 $prev_cid = $cid;
313 }
314 else {
315 if ($prev_cid == $cid) {
316 $display_flag = 1;
317 $prev_cid = $cid;
318 }
319 else {
320 $display_flag = 0;
321 $prev_cid = $cid;
322 }
323 }
324
325 if ($display_flag) {
326 foreach ($row as $colName => $colVal) {
327 if (in_array($colName, $this->_noRepeats)) {
328 unset($rows[$rowNum][$colName]);
329 }
330 }
331 }
332 $entryFound = TRUE;
333 }
334 }
335 }
336
b914f4e8
PN
337 //handle the Financial Type Ids
338 if (array_key_exists('civicrm_pledge_financial_type_id', $row)) {
339 if ($value = $row['civicrm_pledge_financial_type_id']) {
f16bf0fa 340 $rows[$rowNum]['civicrm_pledge_financial_type_id'] =
b914f4e8 341 CRM_Contribute_PseudoConstant::financialType($value, false);
6a488035
TO
342 }
343 $entryFound = TRUE;
344 }
345
346 //handle the Status Ids
347 if (array_key_exists('civicrm_pledge_status_id', $row)) {
348 if ($value = $row['civicrm_pledge_status_id']) {
349 $rows[$rowNum]['civicrm_pledge_status_id'] = CRM_Contribute_PseudoConstant::contributionStatus($value);
350 }
351 $entryFound = TRUE;
352 }
353
354 // handle state province
355 if (array_key_exists('civicrm_address_state_province_id', $row)) {
356 if ($value = $row['civicrm_address_state_province_id']) {
357 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant::stateProvinceAbbreviation($value, FALSE);
358 }
359 $entryFound = TRUE;
360 }
361
362 // handle country
363 if (array_key_exists('civicrm_address_country_id', $row)) {
364 if ($value = $row['civicrm_address_country_id']) {
365 $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant::country($value, FALSE);
366 }
367 $entryFound = TRUE;
368 }
369
370 // convert display name to links
371 if (array_key_exists('civicrm_contact_sort_name', $row) &&
372 array_key_exists('civicrm_contact_id', $row)
373 ) {
374 $url = CRM_Report_Utils_Report::getNextUrl('pledge/summary',
375 'reset=1&force=1&id_op=eq&id_value=' .
376 $row['civicrm_contact_id'],
377 $this->_absoluteUrl, $this->_id, $this->_drilldownReport
378 );
379 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
380 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Pledge Details for this contact");
381 $entryFound = TRUE;
382 }
383
d62fab33
RN
384 // If using campaigns, convert campaign_id to campaign title
385 if (array_key_exists('civicrm_pledge_campaign_id', $row)) {
386 if ($value = $row['civicrm_pledge_campaign_id']) {
387 $rows[$rowNum]['civicrm_pledge_campaign_id'] = $this->activeCampaigns[$value];
388 }
389 $entryFound = TRUE;
390 }
391
6a488035
TO
392 // skip looking further in rows, if first row itself doesn't
393 // have the column we need
394 if (!$entryFound) {
395 break;
396 }
397 }
398 }
399}
400