Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
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 | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
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'); | |
f16bf0fa | 42 | |
6a488035 | 43 | protected $_customGroupExtends = array( |
9d72cede EM |
44 | 'Pledge' |
45 | ); | |
f16bf0fa | 46 | |
74cf4551 EM |
47 | /** |
48 | * | |
49 | */ | |
50 | /** | |
51 | * | |
52 | */ | |
00be9182 | 53 | public function __construct() { |
d62fab33 RN |
54 | |
55 | // Check if CiviCampaign is a) enabled and b) has active campaigns | |
56 | $config = CRM_Core_Config::singleton(); | |
57 | $campaignEnabled = in_array("CiviCampaign", $config->enableComponents); | |
58 | if ($campaignEnabled) { | |
59 | $getCampaigns = CRM_Campaign_BAO_Campaign::getPermissionedCampaigns(NULL, NULL, TRUE, FALSE, TRUE); | |
60 | $this->activeCampaigns = $getCampaigns['campaigns']; | |
61 | asort($this->activeCampaigns); | |
62 | } | |
63 | ||
6a488035 | 64 | $this->_columns = array( |
9d72cede | 65 | 'civicrm_contact' => array( |
6a488035 | 66 | 'dao' => 'CRM_Contact_DAO_Contact', |
9d72cede EM |
67 | 'fields' => array( |
68 | 'sort_name' => array( | |
69 | 'title' => ts('Constituent Name'), | |
6a488035 TO |
70 | 'required' => TRUE, |
71 | 'no_repeat' => TRUE, | |
72 | ), | |
9d72cede | 73 | 'id' => array( |
6a488035 TO |
74 | 'no_display' => TRUE, |
75 | 'required' => TRUE, | |
76 | ), | |
77 | ), | |
78 | 'grouping' => 'contact-fields', | |
79 | ), | |
9d72cede | 80 | 'civicrm_pledge' => array( |
6a488035 | 81 | 'dao' => 'CRM_Pledge_DAO_Pledge', |
9d72cede EM |
82 | 'fields' => array( |
83 | 'pledge_create_date' => array( | |
84 | 'title' => ts('Pledge Made'), | |
6a488035 TO |
85 | 'required' => TRUE, |
86 | ), | |
9d72cede EM |
87 | 'financial_type_id' => array( |
88 | 'title' => ts('Financial Type'), | |
6a488035 TO |
89 | 'requried' => TRUE, |
90 | ), | |
9d72cede EM |
91 | 'amount' => array( |
92 | 'title' => ts('Amount'), | |
6a488035 TO |
93 | 'required' => TRUE, |
94 | 'type' => CRM_Utils_Type::T_MONEY, | |
95 | ), | |
9d72cede | 96 | 'currency' => array( |
7a961f19 | 97 | 'required' => TRUE, |
98 | 'no_display' => TRUE, | |
99 | ), | |
9d72cede EM |
100 | 'status_id' => array( |
101 | 'title' => ts('Status'), | |
6a488035 TO |
102 | ), |
103 | ), | |
9d72cede EM |
104 | 'filters' => array( |
105 | 'pledge_create_date' => array( | |
f16bf0fa | 106 | 'title' => ts('Pledge Made'), |
6a488035 | 107 | 'operatorType' => CRM_Report_Form::OP_DATE, |
7a961f19 | 108 | ), |
9d72cede EM |
109 | 'currency' => array( |
110 | 'title' => 'Currency', | |
f3bc9ff5 | 111 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
0637f1c4 | 112 | 'options' => CRM_Core_OptionGroup::values('currencies_enabled'), |
d1b0931b | 113 | 'default' => NULL, |
f3bc9ff5 | 114 | 'type' => CRM_Utils_Type::T_STRING, |
6a488035 | 115 | ), |
9d72cede EM |
116 | 'financial_type_id' => array( |
117 | 'title' => ts('Financial Type'), | |
6a488035 | 118 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
9d72cede | 119 | 'options' => CRM_Contribute_PseudoConstant::financialType(), |
6a488035 TO |
120 | ), |
121 | ), | |
122 | 'grouping' => 'pledge-fields', | |
123 | ), | |
9d72cede | 124 | 'civicrm_pledge_payment' => array( |
6a488035 | 125 | 'dao' => 'CRM_Pledge_DAO_PledgePayment', |
9d72cede | 126 | 'fields' => array( |
688ab7ab E |
127 | 'scheduled_date' => array( |
128 | 'title' => ts('Next Payment Due'), | |
6a488035 TO |
129 | 'type' => CRM_Utils_Type::T_DATE, |
130 | 'required' => TRUE, | |
131 | ), | |
132 | ), | |
9d72cede EM |
133 | 'filters' => array( |
134 | 'scheduled_date' => array( | |
e8b1c881 | 135 | 'title' => ts('Next Payment Due'), |
136 | 'operatorType' => CRM_Report_Form::OP_DATE, | |
688ab7ab | 137 | 'type' => CRM_Utils_Type::T_DATE, |
e8b1c881 | 138 | ), |
139 | ), | |
6a488035 TO |
140 | 'grouping' => 'pledge-fields', |
141 | ), | |
9d72cede | 142 | 'civicrm_address' => array( |
6a488035 | 143 | 'dao' => 'CRM_Core_DAO_Address', |
9d72cede | 144 | 'fields' => array( |
6a488035 TO |
145 | 'street_address' => NULL, |
146 | 'city' => NULL, | |
147 | 'postal_code' => NULL, | |
9d72cede EM |
148 | 'state_province_id' => array( |
149 | 'title' => ts('State/Province'), | |
6a488035 | 150 | ), |
9d72cede EM |
151 | 'country_id' => array( |
152 | 'title' => ts('Country'), | |
6a488035 TO |
153 | 'default' => TRUE, |
154 | ), | |
155 | ), | |
156 | 'grouping' => 'contact-fields', | |
157 | ), | |
9d72cede | 158 | 'civicrm_email' => array( |
6a488035 | 159 | 'dao' => 'CRM_Core_DAO_Email', |
9d72cede | 160 | 'fields' => array('email' => NULL), |
6a488035 TO |
161 | 'grouping' => 'contact-fields', |
162 | ), | |
9d72cede | 163 | 'civicrm_group' => array( |
6a488035 TO |
164 | 'dao' => 'CRM_Contact_DAO_Group', |
165 | 'alias' => 'cgroup', | |
9d72cede EM |
166 | 'filters' => array( |
167 | 'gid' => array( | |
6a488035 TO |
168 | 'name' => 'group_id', |
169 | 'title' => ts('Group'), | |
170 | 'group' => TRUE, | |
171 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
172 | 'options' => CRM_Core_PseudoConstant::staticGroup(), | |
173 | ), | |
174 | ), | |
175 | ), | |
176 | ); | |
177 | ||
d62fab33 RN |
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', | |
f813f78e | 183 | ); |
9d72cede EM |
184 | $this->_columns['civicrm_pledge']['filters']['campaign_id'] = array( |
185 | 'title' => ts('Campaign'), | |
d62fab33 RN |
186 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
187 | 'options' => $this->activeCampaigns, | |
f813f78e | 188 | ); |
d62fab33 RN |
189 | } |
190 | ||
6a488035 | 191 | $this->_tagFilter = TRUE; |
7a961f19 | 192 | $this->_currencyColumn = 'civicrm_pledge_currency'; |
6a488035 TO |
193 | parent::__construct(); |
194 | } | |
195 | ||
00be9182 | 196 | public function preProcess() { |
fec1b4d0 | 197 | $this->assign('reportTitle', ts('Pledged but not Paid Report')); |
6a488035 TO |
198 | parent::preProcess(); |
199 | } | |
200 | ||
00be9182 | 201 | public function select() { |
6a488035 TO |
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) { | |
9d72cede EM |
207 | if (!empty($field['required']) || |
208 | !empty($this->_params['fields'][$fieldName]) | |
209 | ) { | |
6a488035 TO |
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 | ||
00be9182 | 228 | public function from() { |
6a488035 TO |
229 | $this->_from = NULL; |
230 | ||
231 | $allStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); | |
232 | $pendingStatus = array_search('Pending', $allStatus); | |
233 | foreach (array( | |
9d72cede EM |
234 | 'Pending', |
235 | 'In Progress', | |
236 | 'Overdue' | |
237 | ) as $statusKey) { | |
6a488035 TO |
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} | |
f16bf0fa | 247 | INNER JOIN civicrm_pledge {$this->_aliases['civicrm_pledge']} |
6a488035 | 248 | ON ({$this->_aliases['civicrm_pledge']}.contact_id = |
f16bf0fa | 249 | {$this->_aliases['civicrm_contact']}.id) AND |
6a488035 TO |
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 .= " | |
f16bf0fa | 258 | LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']} |
259 | ON ({$this->_aliases['civicrm_contact']}.id = | |
6a488035 TO |
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 .= " | |
f16bf0fa | 267 | LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']} |
268 | ON ({$this->_aliases['civicrm_contact']}.id = | |
269 | {$this->_aliases['civicrm_email']}.contact_id) AND | |
6a488035 TO |
270 | {$this->_aliases['civicrm_email']}.is_primary = 1\n"; |
271 | } | |
272 | } | |
273 | ||
00be9182 | 274 | public function groupBy() { |
6a488035 | 275 | $this->_groupBy = " |
f16bf0fa | 276 | GROUP BY {$this->_aliases['civicrm_pledge']}.contact_id, |
7a961f19 | 277 | {$this->_aliases['civicrm_pledge']}.id, |
278 | {$this->_aliases['civicrm_pledge']}.currency"; | |
6a488035 TO |
279 | } |
280 | ||
00be9182 | 281 | public function orderBy() { |
6a488035 TO |
282 | $this->_orderBy = "ORDER BY {$this->_aliases['civicrm_contact']}.sort_name, {$this->_aliases['civicrm_pledge']}.contact_id, {$this->_aliases['civicrm_pledge']}.id"; |
283 | } | |
284 | ||
00be9182 | 285 | public function postProcess() { |
6a488035 TO |
286 | // get the acl clauses built before we assemble the query |
287 | $this->buildACLClause($this->_aliases['civicrm_contact']); | |
288 | parent::PostProcess(); | |
289 | } | |
290 | ||
74cf4551 EM |
291 | /** |
292 | * @param $rows | |
293 | */ | |
00be9182 | 294 | public function alterDisplay(&$rows) { |
6a488035 | 295 | // custom code to alter rows |
9d72cede EM |
296 | $entryFound = FALSE; |
297 | $checkList = array(); | |
6a488035 TO |
298 | $display_flag = $prev_cid = $cid = 0; |
299 | ||
300 | foreach ($rows as $rowNum => $row) { | |
301 | if (!empty($this->_noRepeats) && $this->_outputMode != 'csv') { | |
302 | // don't repeat contact details if its same as the previous row | |
303 | if (array_key_exists('civicrm_contact_id', $row)) { | |
304 | if ($cid = $row['civicrm_contact_id']) { | |
305 | if ($rowNum == 0) { | |
306 | $prev_cid = $cid; | |
307 | } | |
308 | else { | |
309 | if ($prev_cid == $cid) { | |
310 | $display_flag = 1; | |
311 | $prev_cid = $cid; | |
312 | } | |
313 | else { | |
314 | $display_flag = 0; | |
315 | $prev_cid = $cid; | |
316 | } | |
317 | } | |
318 | ||
319 | if ($display_flag) { | |
320 | foreach ($row as $colName => $colVal) { | |
321 | if (in_array($colName, $this->_noRepeats)) { | |
322 | unset($rows[$rowNum][$colName]); | |
323 | } | |
324 | } | |
325 | } | |
326 | $entryFound = TRUE; | |
327 | } | |
328 | } | |
329 | } | |
330 | ||
b914f4e8 PN |
331 | //handle the Financial Type Ids |
332 | if (array_key_exists('civicrm_pledge_financial_type_id', $row)) { | |
333 | if ($value = $row['civicrm_pledge_financial_type_id']) { | |
f16bf0fa | 334 | $rows[$rowNum]['civicrm_pledge_financial_type_id'] = |
9d72cede | 335 | CRM_Contribute_PseudoConstant::financialType($value, FALSE); |
6a488035 TO |
336 | } |
337 | $entryFound = TRUE; | |
338 | } | |
339 | ||
340 | //handle the Status Ids | |
341 | if (array_key_exists('civicrm_pledge_status_id', $row)) { | |
342 | if ($value = $row['civicrm_pledge_status_id']) { | |
343 | $rows[$rowNum]['civicrm_pledge_status_id'] = CRM_Contribute_PseudoConstant::contributionStatus($value); | |
344 | } | |
345 | $entryFound = TRUE; | |
346 | } | |
347 | ||
348 | // handle state province | |
349 | if (array_key_exists('civicrm_address_state_province_id', $row)) { | |
350 | if ($value = $row['civicrm_address_state_province_id']) { | |
351 | $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant::stateProvinceAbbreviation($value, FALSE); | |
352 | } | |
353 | $entryFound = TRUE; | |
354 | } | |
355 | ||
356 | // handle country | |
357 | if (array_key_exists('civicrm_address_country_id', $row)) { | |
358 | if ($value = $row['civicrm_address_country_id']) { | |
359 | $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant::country($value, FALSE); | |
360 | } | |
361 | $entryFound = TRUE; | |
362 | } | |
363 | ||
364 | // convert display name to links | |
365 | if (array_key_exists('civicrm_contact_sort_name', $row) && | |
366 | array_key_exists('civicrm_contact_id', $row) | |
367 | ) { | |
368 | $url = CRM_Report_Utils_Report::getNextUrl('pledge/summary', | |
369 | 'reset=1&force=1&id_op=eq&id_value=' . | |
370 | $row['civicrm_contact_id'], | |
371 | $this->_absoluteUrl, $this->_id, $this->_drilldownReport | |
372 | ); | |
373 | $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url; | |
374 | $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Pledge Details for this contact"); | |
375 | $entryFound = TRUE; | |
376 | } | |
377 | ||
d62fab33 RN |
378 | // If using campaigns, convert campaign_id to campaign title |
379 | if (array_key_exists('civicrm_pledge_campaign_id', $row)) { | |
380 | if ($value = $row['civicrm_pledge_campaign_id']) { | |
381 | $rows[$rowNum]['civicrm_pledge_campaign_id'] = $this->activeCampaigns[$value]; | |
382 | } | |
383 | $entryFound = TRUE; | |
384 | } | |
385 | ||
6a488035 TO |
386 | // skip looking further in rows, if first row itself doesn't |
387 | // have the column we need | |
388 | if (!$entryFound) { | |
389 | break; | |
390 | } | |
391 | } | |
392 | } | |
393 | } |