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