Merge pull request #7779 from jitendrapurohit/CRM-17993
[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 '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 );
189 }
190
191 $this->_tagFilter = TRUE;
192 $this->_currencyColumn = 'civicrm_pledge_currency';
193 parent::__construct();
194 }
195
196 public function preProcess() {
197 $this->assign('reportTitle', ts('Pledged but not Paid Report'));
198 parent::preProcess();
199 }
200
201 public function select() {
202 $select = array();
203 $this->_columnHeaders = array();
204 foreach ($this->_columns as $tableName => $table) {
205 if (array_key_exists('fields', $table)) {
206 foreach ($table['fields'] as $fieldName => $field) {
207 if (!empty($field['required']) ||
208 !empty($this->_params['fields'][$fieldName])
209 ) {
210 // to include optional columns address and email, only if checked
211 if ($tableName == 'civicrm_address') {
212 $this->_addressField = TRUE;
213 $this->_emailField = TRUE;
214 }
215 elseif ($tableName == 'civicrm_email') {
216 $this->_emailField = TRUE;
217 }
218 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
219 $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field);
220 $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array::value('title', $field);
221 }
222 }
223 }
224 }
225 $this->_select = "SELECT " . implode(', ', $select) . " ";
226 }
227
228 public function from() {
229 $this->_from = NULL;
230
231 $allStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
232 $pendingStatus = array_search('Pending', $allStatus);
233 foreach (array(
234 'Pending',
235 'In Progress',
236 'Overdue',
237 ) as $statusKey) {
238 if ($key = CRM_Utils_Array::key($statusKey, $allStatus)) {
239 $unpaidStatus[] = $key;
240 }
241 }
242
243 $statusIds = implode(', ', $unpaidStatus);
244
245 $this->_from = "
246 FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom}
247 INNER JOIN civicrm_pledge {$this->_aliases['civicrm_pledge']}
248 ON ({$this->_aliases['civicrm_pledge']}.contact_id =
249 {$this->_aliases['civicrm_contact']}.id) AND
250 {$this->_aliases['civicrm_pledge']}.status_id IN ( {$statusIds} )
251 LEFT JOIN civicrm_pledge_payment {$this->_aliases['civicrm_pledge_payment']}
252 ON ({$this->_aliases['civicrm_pledge']}.id =
253 {$this->_aliases['civicrm_pledge_payment']}.pledge_id AND {$this->_aliases['civicrm_pledge_payment']}.status_id = {$pendingStatus} ) ";
254
255 // include address field if address column is to be included
256 if ($this->_addressField) {
257 $this->_from .= "
258 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
259 ON ({$this->_aliases['civicrm_contact']}.id =
260 {$this->_aliases['civicrm_address']}.contact_id) AND
261 {$this->_aliases['civicrm_address']}.is_primary = 1\n";
262 }
263
264 // include email field if email column is to be included
265 if ($this->_emailField) {
266 $this->_from .= "
267 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
268 ON ({$this->_aliases['civicrm_contact']}.id =
269 {$this->_aliases['civicrm_email']}.contact_id) AND
270 {$this->_aliases['civicrm_email']}.is_primary = 1\n";
271 }
272 }
273
274 public function groupBy() {
275 $this->_groupBy = "
276 GROUP BY {$this->_aliases['civicrm_pledge']}.contact_id,
277 {$this->_aliases['civicrm_pledge']}.id,
278 {$this->_aliases['civicrm_pledge']}.currency";
279 }
280
281 public function orderBy() {
282 $this->_orderBy = "ORDER BY {$this->_aliases['civicrm_contact']}.sort_name, {$this->_aliases['civicrm_pledge']}.contact_id, {$this->_aliases['civicrm_pledge']}.id";
283 }
284
285 public function postProcess() {
286 // get the acl clauses built before we assemble the query
287 $this->buildACLClause($this->_aliases['civicrm_contact']);
288 parent::PostProcess();
289 }
290
291 /**
292 * Alter display of rows.
293 *
294 * Iterate through the rows retrieved via SQL and make changes for display purposes,
295 * such as rendering contacts as links.
296 *
297 * @param array $rows
298 */
299 public function alterDisplay(&$rows) {
300 $entryFound = FALSE;
301 $checkList = array();
302 $display_flag = $prev_cid = $cid = 0;
303
304 foreach ($rows as $rowNum => $row) {
305 if (!empty($this->_noRepeats) && $this->_outputMode != 'csv') {
306 // don't repeat contact details if its same as the previous row
307 if (array_key_exists('civicrm_contact_id', $row)) {
308 if ($cid = $row['civicrm_contact_id']) {
309 if ($rowNum == 0) {
310 $prev_cid = $cid;
311 }
312 else {
313 if ($prev_cid == $cid) {
314 $display_flag = 1;
315 $prev_cid = $cid;
316 }
317 else {
318 $display_flag = 0;
319 $prev_cid = $cid;
320 }
321 }
322
323 if ($display_flag) {
324 foreach ($row as $colName => $colVal) {
325 if (in_array($colName, $this->_noRepeats)) {
326 unset($rows[$rowNum][$colName]);
327 }
328 }
329 }
330 $entryFound = TRUE;
331 }
332 }
333 }
334
335 //handle the Financial Type Ids
336 if (array_key_exists('civicrm_pledge_financial_type_id', $row)) {
337 if ($value = $row['civicrm_pledge_financial_type_id']) {
338 $rows[$rowNum]['civicrm_pledge_financial_type_id'] = CRM_Contribute_PseudoConstant::financialType($value, FALSE);
339 }
340 $entryFound = TRUE;
341 }
342
343 //handle the Status Ids
344 if (array_key_exists('civicrm_pledge_status_id', $row)) {
345 if ($value = $row['civicrm_pledge_status_id']) {
346 $rows[$rowNum]['civicrm_pledge_status_id'] = CRM_Contribute_PseudoConstant::contributionStatus($value);
347 }
348 $entryFound = TRUE;
349 }
350
351 // handle state province
352 if (array_key_exists('civicrm_address_state_province_id', $row)) {
353 if ($value = $row['civicrm_address_state_province_id']) {
354 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant::stateProvinceAbbreviation($value, FALSE);
355 }
356 $entryFound = TRUE;
357 }
358
359 // handle country
360 if (array_key_exists('civicrm_address_country_id', $row)) {
361 if ($value = $row['civicrm_address_country_id']) {
362 $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant::country($value, FALSE);
363 }
364 $entryFound = TRUE;
365 }
366
367 // convert display name to links
368 if (array_key_exists('civicrm_contact_sort_name', $row) &&
369 array_key_exists('civicrm_contact_id', $row)
370 ) {
371 $url = CRM_Report_Utils_Report::getNextUrl('pledge/summary',
372 'reset=1&force=1&id_op=eq&id_value=' .
373 $row['civicrm_contact_id'],
374 $this->_absoluteUrl, $this->_id, $this->_drilldownReport
375 );
376 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
377 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Pledge Details for this contact");
378 $entryFound = TRUE;
379 }
380
381 // If using campaigns, convert campaign_id to campaign title
382 if (array_key_exists('civicrm_pledge_campaign_id', $row)) {
383 if ($value = $row['civicrm_pledge_campaign_id']) {
384 $rows[$rowNum]['civicrm_pledge_campaign_id'] = $this->activeCampaigns[$value];
385 }
386 $entryFound = TRUE;
387 }
388
389 // skip looking further in rows, if first row itself doesn't
390 // have the column we need
391 if (!$entryFound) {
392 break;
393 }
394 }
395 }
396
397 }