be9e362c6e6b3f5d039a2bc6cf477c7d9586e84b
[civicrm-core.git] / CRM / Report / Form / Pledge / Pbnp.php
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 */
37 class 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');
44
45 protected $_customGroupExtends = array(
46 'Pledge');
47
48 function __construct() {
49 $this->_columns = array(
50 'civicrm_contact' =>
51 array(
52 'dao' => 'CRM_Contact_DAO_Contact',
53 'fields' =>
54 array(
55 'sort_name' =>
56 array('title' => ts('Constituent Name'),
57 'required' => TRUE,
58 'no_repeat' => TRUE,
59 ),
60 'id' =>
61 array(
62 'no_display' => TRUE,
63 'required' => TRUE,
64 ),
65 ),
66 'grouping' => 'contact-fields',
67 ),
68 'civicrm_pledge' =>
69 array(
70 'dao' => 'CRM_Pledge_DAO_Pledge',
71 'fields' =>
72 array(
73 'pledge_create_date' =>
74 array('title' => ts('Pledge Made'),
75 'required' => TRUE,
76 ),
77 'financial_type_id' =>
78 array('title' => ts('Financial Type'),
79 'requried' => TRUE,
80 ),
81 'amount' =>
82 array('title' => ts('Amount'),
83 'required' => TRUE,
84 'type' => CRM_Utils_Type::T_MONEY,
85 ),
86 'currency' =>
87 array(
88 'required' => TRUE,
89 'no_display' => TRUE,
90 ),
91 'status_id' =>
92 array('title' => ts('Status'),
93 ),
94 ),
95 'filters' =>
96 array(
97 'pledge_create_date' =>
98 array(
99 'title' => ts('Pledge Made'),
100 'operatorType' => CRM_Report_Form::OP_DATE,
101 ),
102 'currency' =>
103 array('title' => 'Currency',
104 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
105 'options' => CRM_Core_OptionGroup::values('currencies_enabled'),
106 'default' => NULL,
107 'type' => CRM_Utils_Type::T_STRING,
108 ),
109 'financial_type_id' =>
110 array( 'title' => ts('Financial Type'),
111 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
112 'options' => CRM_Contribute_PseudoConstant::financialType(),
113 ),
114 ),
115 'grouping' => 'pledge-fields',
116 ),
117 'civicrm_pledge_payment' =>
118 array(
119 'dao' => 'CRM_Pledge_DAO_PledgePayment',
120 'fields' =>
121 array(
122 'scheduled_date' =>
123 array('title' => ts('Next Payment Due'),
124 'type' => CRM_Utils_Type::T_DATE,
125 'required' => TRUE,
126 ),
127 ),
128 'filters' =>
129 array(
130 'scheduled_date' =>
131 array(
132 'title' => ts('Next Payment Due'),
133 'operatorType' => CRM_Report_Form::OP_DATE,
134 ),
135 ),
136 'grouping' => 'pledge-fields',
137 ),
138 'civicrm_address' =>
139 array(
140 'dao' => 'CRM_Core_DAO_Address',
141 'fields' =>
142 array(
143 'street_address' => NULL,
144 'city' => NULL,
145 'postal_code' => NULL,
146 'state_province_id' =>
147 array('title' => ts('State/Province'),
148 ),
149 'country_id' =>
150 array('title' => ts('Country'),
151 'default' => TRUE,
152 ),
153 ),
154 'grouping' => 'contact-fields',
155 ),
156 'civicrm_email' =>
157 array(
158 'dao' => 'CRM_Core_DAO_Email',
159 'fields' =>
160 array('email' => NULL),
161 'grouping' => 'contact-fields',
162 ),
163 'civicrm_group' =>
164 array(
165 'dao' => 'CRM_Contact_DAO_Group',
166 'alias' => 'cgroup',
167 'filters' =>
168 array(
169 'gid' =>
170 array(
171 'name' => 'group_id',
172 'title' => ts('Group'),
173 'group' => TRUE,
174 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
175 'options' => CRM_Core_PseudoConstant::staticGroup(),
176 ),
177 ),
178 ),
179 );
180
181 $this->_tagFilter = TRUE;
182 $this->_currencyColumn = 'civicrm_pledge_currency';
183 parent::__construct();
184 }
185
186 function preProcess() {
187 $this->assign('reportTitle', ts('Pledge But Not Paid Report'));
188 parent::preProcess();
189 }
190
191 function select() {
192 $select = array();
193 $this->_columnHeaders = array();
194 foreach ($this->_columns as $tableName => $table) {
195 if (array_key_exists('fields', $table)) {
196 foreach ($table['fields'] as $fieldName => $field) {
197 if (CRM_Utils_Array::value('required', $field) ||
198 CRM_Utils_Array::value($fieldName, $this->_params['fields'])
199 ) {
200 // to include optional columns address and email, only if checked
201 if ($tableName == 'civicrm_address') {
202 $this->_addressField = TRUE;
203 $this->_emailField = TRUE;
204 }
205 elseif ($tableName == 'civicrm_email') {
206 $this->_emailField = TRUE;
207 }
208 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
209 $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field);
210 $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array::value('title', $field);
211 }
212 }
213 }
214 }
215 $this->_select = "SELECT " . implode(', ', $select) . " ";
216 }
217
218 function from() {
219 $this->_from = NULL;
220
221 $allStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
222 $pendingStatus = array_search('Pending', $allStatus);
223 foreach (array(
224 'Pending', 'In Progress', 'Overdue') as $statusKey) {
225 if ($key = CRM_Utils_Array::key($statusKey, $allStatus)) {
226 $unpaidStatus[] = $key;
227 }
228 }
229
230 $statusIds = implode(', ', $unpaidStatus);
231
232 $this->_from = "
233 FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom}
234 INNER JOIN civicrm_pledge {$this->_aliases['civicrm_pledge']}
235 ON ({$this->_aliases['civicrm_pledge']}.contact_id =
236 {$this->_aliases['civicrm_contact']}.id) AND
237 {$this->_aliases['civicrm_pledge']}.status_id IN ( {$statusIds} )
238 LEFT JOIN civicrm_pledge_payment {$this->_aliases['civicrm_pledge_payment']}
239 ON ({$this->_aliases['civicrm_pledge']}.id =
240 {$this->_aliases['civicrm_pledge_payment']}.pledge_id AND {$this->_aliases['civicrm_pledge_payment']}.status_id = {$pendingStatus} ) ";
241
242 // include address field if address column is to be included
243 if ($this->_addressField) {
244 $this->_from .= "
245 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
246 ON ({$this->_aliases['civicrm_contact']}.id =
247 {$this->_aliases['civicrm_address']}.contact_id) AND
248 {$this->_aliases['civicrm_address']}.is_primary = 1\n";
249 }
250
251 // include email field if email column is to be included
252 if ($this->_emailField) {
253 $this->_from .= "
254 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
255 ON ({$this->_aliases['civicrm_contact']}.id =
256 {$this->_aliases['civicrm_email']}.contact_id) AND
257 {$this->_aliases['civicrm_email']}.is_primary = 1\n";
258 }
259 }
260
261 function groupBy() {
262 $this->_groupBy = "
263 GROUP BY {$this->_aliases['civicrm_pledge']}.contact_id,
264 {$this->_aliases['civicrm_pledge']}.id,
265 {$this->_aliases['civicrm_pledge']}.currency";
266 }
267
268 function orderBy() {
269 $this->_orderBy = "ORDER BY {$this->_aliases['civicrm_contact']}.sort_name, {$this->_aliases['civicrm_pledge']}.contact_id, {$this->_aliases['civicrm_pledge']}.id";
270 }
271
272 function postProcess() {
273 // get the acl clauses built before we assemble the query
274 $this->buildACLClause($this->_aliases['civicrm_contact']);
275 parent::PostProcess();
276 }
277
278 function alterDisplay(&$rows) {
279 // custom code to alter rows
280 $entryFound = FALSE;
281 $checkList = array();
282 $display_flag = $prev_cid = $cid = 0;
283
284 foreach ($rows as $rowNum => $row) {
285 if (!empty($this->_noRepeats) && $this->_outputMode != 'csv') {
286 // don't repeat contact details if its same as the previous row
287 if (array_key_exists('civicrm_contact_id', $row)) {
288 if ($cid = $row['civicrm_contact_id']) {
289 if ($rowNum == 0) {
290 $prev_cid = $cid;
291 }
292 else {
293 if ($prev_cid == $cid) {
294 $display_flag = 1;
295 $prev_cid = $cid;
296 }
297 else {
298 $display_flag = 0;
299 $prev_cid = $cid;
300 }
301 }
302
303 if ($display_flag) {
304 foreach ($row as $colName => $colVal) {
305 if (in_array($colName, $this->_noRepeats)) {
306 unset($rows[$rowNum][$colName]);
307 }
308 }
309 }
310 $entryFound = TRUE;
311 }
312 }
313 }
314
315 //handle the Financial Type Ids
316 if (array_key_exists('civicrm_pledge_financial_type_id', $row)) {
317 if ($value = $row['civicrm_pledge_financial_type_id']) {
318 $rows[$rowNum]['civicrm_pledge_financial_type_id'] =
319 CRM_Contribute_PseudoConstant::financialType($value, false);
320 }
321 $entryFound = TRUE;
322 }
323
324 //handle the Status Ids
325 if (array_key_exists('civicrm_pledge_status_id', $row)) {
326 if ($value = $row['civicrm_pledge_status_id']) {
327 $rows[$rowNum]['civicrm_pledge_status_id'] = CRM_Contribute_PseudoConstant::contributionStatus($value);
328 }
329 $entryFound = TRUE;
330 }
331
332 // handle state province
333 if (array_key_exists('civicrm_address_state_province_id', $row)) {
334 if ($value = $row['civicrm_address_state_province_id']) {
335 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant::stateProvinceAbbreviation($value, FALSE);
336 }
337 $entryFound = TRUE;
338 }
339
340 // handle country
341 if (array_key_exists('civicrm_address_country_id', $row)) {
342 if ($value = $row['civicrm_address_country_id']) {
343 $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant::country($value, FALSE);
344 }
345 $entryFound = TRUE;
346 }
347
348 // convert display name to links
349 if (array_key_exists('civicrm_contact_sort_name', $row) &&
350 array_key_exists('civicrm_contact_id', $row)
351 ) {
352 $url = CRM_Report_Utils_Report::getNextUrl('pledge/summary',
353 'reset=1&force=1&id_op=eq&id_value=' .
354 $row['civicrm_contact_id'],
355 $this->_absoluteUrl, $this->_id, $this->_drilldownReport
356 );
357 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
358 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Pledge Details for this contact");
359 $entryFound = TRUE;
360 }
361
362 // skip looking further in rows, if first row itself doesn't
363 // have the column we need
364 if (!$entryFound) {
365 break;
366 }
367 }
368 }
369 }
370