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