Merge pull request #2763 from colemanw/master
[civicrm-core.git] / CRM / Report / Form / Contribute / HouseholdSummary.php
CommitLineData
6a488035 1<?php
6a488035
TO
2
3/*
4 +--------------------------------------------------------------------+
06b69b18 5 | CiviCRM version 4.5 |
6a488035 6 +--------------------------------------------------------------------+
06b69b18 7 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
19 | |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
27*/
28
29/**
30 *
31 * @package CRM
06b69b18 32 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
33 * $Id$
34 *
35 */
36class CRM_Report_Form_Contribute_HouseholdSummary extends CRM_Report_Form {
37
38 protected $_addressField = FALSE;
39
40 protected $_emailField = FALSE;
41
42 public $_drilldownReport = array('contribute/detail' => 'Link to Detail Report');
43
eb3b4c04 44 protected $_summary = NULL;
2f4c2f5d 45
eb3b4c04 46 function __construct() {
6a488035
TO
47 self::validRelationships();
48
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_household' =>
59 array(
60 'dao' => 'CRM_Contact_DAO_Contact',
61 'fields' =>
62 array(
63 'household_name' =>
64 array('title' => ts('Household Name'),
65 'required' => TRUE,
66 ),
67 'id' =>
68 array(
69 'no_display' => TRUE,
70 'required' => TRUE,
71 ),
30f85891
RN
72 'contact_type' =>
73 array(
74 'title' => ts('Contact Type'),
75 ),
76 'contact_sub_type' =>
77 array(
78 'title' => ts('Contact SubType'),
79 ),
6a488035
TO
80 ),
81 'filters' =>
82 array(
83 'household_name' =>
84 array('title' => ts('Household Name')),
85 ),
86 'grouping' => 'household-fields',
87 ),
88 'civicrm_relationship' =>
89 array(
90 'dao' => 'CRM_Contact_DAO_Relationship',
91 'fields' =>
92 array(
93 'relationship_type_id' =>
94 array('title' => ts('Relationship Type'),
95 ),
96 ),
97 'filters' =>
98 array(
99 'relationship_type_id' =>
100 array(
101 'title' => ts('Relationship Type'),
102 'type' => CRM_Utils_Type::T_INT,
103 'operatorType' => CRM_Report_Form::OP_SELECT,
104 'options' => $this->relationTypes,
105 'default' => array(1),
106 ),
107 ),
108 'grouping' => 'household-fields',
109 ),
110 'civicrm_contact' =>
111 array(
112 'dao' => 'CRM_Contact_DAO_Contact',
113 'fields' =>
114 array(
115 'sort_name' =>
116 array('title' => ts('Contact Name'),
117 'required' => TRUE,
118 ),
119 'id' =>
120 array(
121 'no_display' => TRUE,
122 'required' => TRUE,
123 ),
124 ),
125 'grouping' => 'contact-fields',
126 ),
127 'civicrm_contribution' =>
128 array(
129 'dao' => 'CRM_Contribute_DAO_Contribution',
130 'fields' =>
131 array('total_amount' => array('title' => ts('Amount'),
132 'required' => TRUE,
133 ),
134 'id' => array(
135 'no_display' => TRUE,
136 'required' => TRUE,
137 ),
138 'contribution_status_id' => array(
139 'title' => 'Contribution Status',
140 'default' => TRUE,
141 ),
f5aace12
DG
142 'check_number' => array('title' => ts('Check Number'),
143 ),
eb3b4c04 144 'currency' => array(
145 'required' => TRUE,
146 'no_display' => TRUE,
147 ),
6a488035
TO
148 'trxn_id' => NULL,
149 'receive_date' => array('default' => TRUE),
150 'receipt_date' => NULL,
151 ),
152 'filters' =>
153 array(
154 'receive_date' =>
155 array('operatorType' => CRM_Report_Form::OP_DATE),
156 'total_amount' =>
157 array('title' => ts('Amount Between')),
eb3b4c04 158 'currency' =>
159 array('title' => 'Currency',
160 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
161 'options' => CRM_Core_OptionGroup::values('currencies_enabled'),
d1b0931b 162 'default' => NULL,
eb3b4c04 163 'type' => CRM_Utils_Type::T_STRING,
164 ),
6a488035
TO
165 'contribution_status_id' =>
166 array('title' => ts('Contribution Status'),
167 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
168 'options' => CRM_Contribute_PseudoConstant::contributionStatus(),
169 'default' => array(1),
170 ),
171 ),
172 'grouping' => 'contri-fields',
173 ),
174 'civicrm_address' =>
175 array(
176 'dao' => 'CRM_Core_DAO_Address',
177 'fields' =>
178 array(
179 'street_address' => NULL,
180 'city' => NULL,
181 'postal_code' => NULL,
182 'state_province_id' =>
183 array('title' => ts('State/Province'),
184 ),
185 'country_id' =>
186 array('title' => ts('Country'),
187 ),
188 ),
189 'grouping' => 'contact-fields',
190 ),
191 'civicrm_email' =>
192 array(
193 'dao' => 'CRM_Core_DAO_Email',
194 'fields' =>
195 array('email' => NULL),
196 'grouping' => 'contact-fields',
197 ),
198 );
199
200 if ($campaignEnabled && !empty($this->activeCampaigns)) {
201 $this->_columns['civicrm_contribution']['fields']['campaign_id'] = array(
202 'title' => 'Campaign',
203 'default' => 'false',
204 );
205 $this->_columns['civicrm_contribution']['filters']['campaign_id'] = array('title' => ts('Campaign'),
206 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
207 'options' => $this->activeCampaigns,
208 );
209 }
eb3b4c04 210 $this->_currencyColumn = 'civicrm_contribution_currency';
6a488035
TO
211 parent::__construct();
212 }
213
214 function preProcess() {
215 parent::preProcess();
216 }
217
218 function select() {
219 $this->_columnHeaders = $select = array();
220 foreach ($this->_columns as $tableName => $table) {
221 if (array_key_exists('fields', $table)) {
222 foreach ($table['fields'] as $fieldName => $field) {
8cc574cf 223 if (!empty($field['required']) || !empty($this->_params['fields'][$fieldName])) {
6a488035
TO
224 if ($tableName == 'civicrm_address') {
225 $this->_addressField = TRUE;
226 }
227 elseif ($tableName == 'civicrm_email') {
228 $this->_emailField = TRUE;
229 }
230
a7488080 231 if (!empty($field['statistics'])) {
6a488035
TO
232 foreach ($field['statistics'] as $stat => $label) {
233 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}_{$stat}";
234 $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
235 $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['type'] = $field['type'];
236 $this->_statFields[] = "{$tableName}_{$fieldName}_{$stat}";
237 }
238 }
239 else {
240 $select[] = "{$table['alias']}.{$fieldName} as {$tableName}_{$fieldName}";
241
242 $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array::value('title', $field);
243 $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field);
244 }
245 }
246 }
247 }
248 }
249 $this->_select = "SELECT " . implode(', ', $select) . " ";
250 }
251
252 function from() {
253
254 $this->_from = NULL;
255 $this->_from = "
39eb89f4
DL
256 FROM civicrm_relationship {$this->_aliases['civicrm_relationship']}
257 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_contact_household']} ON
6a488035 258 ({$this->_aliases['civicrm_contact_household']}.id = {$this->_aliases['civicrm_relationship']}.$this->householdContact AND {$this->_aliases['civicrm_contact_household']}.contact_type='Household')
39eb89f4
DL
259 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_contact']} ON
260 ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_relationship']}.$this->otherContact )
6a488035
TO
261 {$this->_aclFrom}
262 INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']} ON
263 ({$this->_aliases['civicrm_contribution']}.contact_id = {$this->_aliases['civicrm_relationship']}.$this->otherContact ) AND {$this->_aliases['civicrm_contribution']}.is_test = 0 ";
264
265 if ($this->_addressField) {
39eb89f4
DL
266 $this->_from .= "
267 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']} ON
268 {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_address']}.contact_id AND
6a488035
TO
269 {$this->_aliases['civicrm_address']}.is_primary = 1\n ";
270 }
271 if ($this->_emailField) {
272 $this->_from .= "
39eb89f4
DL
273 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']} ON
274 {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id AND
6a488035
TO
275 {$this->_aliases['civicrm_email']}.is_primary = 1\n ";
276 }
277 }
278
279 function where() {
280 $clauses = array();
281 foreach ($this->_columns as $tableName => $table) {
282 if (array_key_exists('filters', $table)) {
283 foreach ($table['filters'] as $fieldName => $field) {
284 $clause = NULL;
285 if (CRM_Utils_Array::value('type', $field) & CRM_Utils_Type::T_DATE) {
286 $relative = CRM_Utils_Array::value("{$fieldName}_relative", $this->_params);
287 $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params);
288 $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params);
289
290 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
291 }
292 else {
293 $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params);
294 if ($op) {
295 if ($fieldName == 'relationship_type_id') {
296 $clause = "{$this->_aliases['civicrm_relationship']}.relationship_type_id=" . $this->relationshipId;
297 }
298 else {
299 $clause = $this->whereClause($field,
300 $op,
301 CRM_Utils_Array::value("{$fieldName}_value", $this->_params),
302 CRM_Utils_Array::value("{$fieldName}_min", $this->_params),
303 CRM_Utils_Array::value("{$fieldName}_max", $this->_params)
304 );
305 }
306 }
307 }
308
309 if (!empty($clause)) {
310 $clauses[] = $clause;
311 }
312 }
313 }
314 }
315
316 if (empty($clauses)) {
317 $this->_where = "WHERE ( 1 )";
318 }
319 else {
320 $this->_where = "WHERE " . implode(' AND ', $clauses);
321 }
322
323 if ($this->_aclWhere) {
324 $this->_where .= " AND {$this->_aclWhere} ";
325 }
326 }
327
328 function groupBy() {
329 $this->_groupBy = " GROUP BY {$this->_aliases['civicrm_relationship']}.$this->householdContact, {$this->_aliases['civicrm_relationship']}.$this->otherContact , {$this->_aliases['civicrm_contribution']}.id, {$this->_aliases['civicrm_relationship']}.relationship_type_id ";
330 }
331
332 function orderBy() {
333 $this->_orderBy = " ORDER BY {$this->_aliases['civicrm_contact_household']}.household_name, {$this->_aliases['civicrm_relationship']}.$this->householdContact, {$this->_aliases['civicrm_contact']}.sort_name, {$this->_aliases['civicrm_relationship']}.$this->otherContact";
334 }
335
336 function statistics(&$rows) {
337 $statistics = parent::statistics($rows);
338
339 //hack filter display for relationship type
340 $type = substr($this->_params['relationship_type_id_value'], -3);
341 foreach ($statistics['filters'] as $id => $value) {
39eb89f4
DL
342 if (
343 $value['title'] == 'Relationship Type' &&
344 isset($this->relationTypes[$this->relationshipId . '_' . $type])
345 ) {
6a488035
TO
346 $statistics['filters'][$id]['value'] = 'Is equal to ' . $this->relationTypes[$this->relationshipId . '_' . $type];
347 }
348 }
349 return $statistics;
350 }
351
352 function postProcess() {
353
354 $this->beginPostProcess();
355 $getRelationship = $this->_params['relationship_type_id_value'];
356 $type = substr($getRelationship, -3);
357 $this->relationshipId = intval((substr($getRelationship, 0, strpos($getRelationship, '_'))));
358 if ($type == 'b_a') {
359 $this->householdContact = 'contact_id_b';
360 $this->otherContact = 'contact_id_a';
361 }
362 else {
363 $this->householdContact = 'contact_id_a';
364 $this->otherContact = 'contact_id_b';
365 }
366 $this->buildACLClause(array($this->_aliases['civicrm_contact'], $this->_aliases['civicrm_contact_household']));
367 $sql = $this->buildQuery(TRUE);
368 $rows = array();
369
370 $this->buildRows($sql, $rows);
371 $this->formatDisplay($rows);
372 $this->doTemplateAssignment($rows);
373 $this->endPostProcess($rows);
374 }
375
376 function validRelationships() {
377 $this->relationTypes = $relationTypes = array();
378
379 $params = array('contact_type_b' => 'Household', 'version' => 3);
39eb89f4 380 $typesA = civicrm_api('relationship_type', 'get', $params);
a7488080 381 if (empty($typesA['is_error'])) {
6a488035
TO
382 foreach ($typesA['values'] as $rel) {
383 $relationTypes[$rel['id']][$rel['id'] . '_b_a'] = $rel['label_b_a'];
384 }
385 }
386 $params = array('contact_type_a' => 'Household', 'version' => 3);
39eb89f4 387 $typesB = civicrm_api('relationship_type', 'get', $params);
a7488080 388 if (empty($typesB['is_error'])) {
6a488035
TO
389 foreach ($typesB['values'] as $rel) {
390 $relationTypes[$rel['id']][$rel['id'] . '_a_b'] = $rel['label_a_b'];
391 //$this->relationTypes[$rel['id'].'_a_b'] = $rel['label_a_b'];
392 }
393 }
394
395 ksort($relationTypes);
396 foreach ($relationTypes as $relationship) {
397 foreach ($relationship as $index => $label) {
398 $this->relationTypes[$index] = $label;
399 }
400 }
401 }
402
403 function alterDisplay(&$rows) {
404 // custom code to alter rows
405 $type = substr($this->_params['relationship_type_id_value'], -3);
406
407 $entryFound = FALSE;
408 $flagHousehold = $flagContact = 0;
6a488035
TO
409 foreach ($rows as $rowNum => $row) {
410
411 //replace retionship id by relationship name
412 if (array_key_exists('civicrm_relationship_relationship_type_id', $row)) {
413 if ($value = $row['civicrm_relationship_relationship_type_id']) {
414 $rows[$rowNum]['civicrm_relationship_relationship_type_id'] = $this->relationTypes[$value . '_' . $type];
415 $entryFound = TRUE;
416 }
417 }
418
419 //remove duplicate Organization names
420 if (array_key_exists('civicrm_contact_household_household_name', $row) && $this->_outputMode != 'csv') {
421 if ($value = $row['civicrm_contact_household_household_name']) {
422 if ($rowNum == 0) {
423 $priviousHousehold = $value;
424 }
425 else {
426 if ($priviousHousehold == $value) {
427 $flagHousehold = 1;
428 $priviousHousehold = $value;
429 }
430 else {
431 $flagHousehold = 0;
432 $priviousHousehold = $value;
433 }
434 }
435
436 if ($flagHousehold == 1) {
437 $rows[$rowNum]['civicrm_contact_household_household_name'] = "";
438 }
439 else {
440 $url = CRM_Utils_System::url('civicrm/contact/view',
441 'reset=1&cid=' . $rows[$rowNum]['civicrm_contact_household_id'],
442 $this->_absoluteUrl
443 );
444
445 $rows[$rowNum]['civicrm_contact_household_household_name'] = "<a href='$url' title='" . ts('View contact summary for this househould') . "'>" . $value . '</a>';
446 }
447 $entryFound = TRUE;
448 }
449 }
450
451 //remove duplicate Contact names and relationship type
452 if (array_key_exists('civicrm_contact_id', $row) && $this->_outputMode != 'csv') {
453 if ($value = $row['civicrm_contact_id']) {
454 if ($rowNum == 0) {
455 $priviousContact = $value;
456 }
457 else {
458 if ($priviousContact == $value) {
459 $flagContact = 1;
460 $priviousContact = $value;
461 }
462 else {
463 $flagContact = 0;
464 $priviousContact = $value;
465 }
466 }
467
468 if ($flagContact == 1 && $flagHousehold == 1) {
469 $rows[$rowNum]['civicrm_contact_sort_name'] = "";
470 $rows[$rowNum]['civicrm_relationship_relationship_type_id'] = "";
471 }
472
473 $entryFound = TRUE;
474 }
475 }
476
477 if (array_key_exists('civicrm_contribution_contribution_status_id', $row)) {
478 if ($value = $row['civicrm_contribution_contribution_status_id']) {
479 $rows[$rowNum]['civicrm_contribution_contribution_status_id'] = CRM_Contribute_PseudoConstant::contributionStatus($value);
480 }
481 }
482
483 // handle state province
484 if (array_key_exists('civicrm_address_state_province_id', $row)) {
485 if ($value = $row['civicrm_address_state_province_id']) {
486 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant::stateProvinceAbbreviation($value, FALSE);
487 }
488 $entryFound = TRUE;
489 }
490
491 // handle country
492 if (array_key_exists('civicrm_address_country_id', $row)) {
493 if ($value = $row['civicrm_address_country_id']) {
494 $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant::country($value, FALSE);
495 }
496 $entryFound = TRUE;
497 }
498
499 // convert display name to links
500 if (array_key_exists('civicrm_contact_sort_name', $row) &&
501 $rows[$rowNum]['civicrm_contact_sort_name'] &&
502 array_key_exists('civicrm_contact_id', $row)
503 ) {
504 $url = CRM_Report_Utils_Report::getNextUrl('contribute/detail',
505 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id'],
506 $this->_absoluteUrl, $this->_id, $this->_drilldownReport
507 );
508 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
509 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts('View contribution details for this individual');
510
511 $entryFound = TRUE;
512 }
513
a7488080 514 if (!empty($row['civicrm_contribution_total_amount'])) {
eb3b4c04 515 $row['civicrm_contribution_total_amount'] = CRM_Utils_Money::format($row['civicrm_contribution_total_amount'], $row['civicrm_contribution_currency']);
516 }
517
6a488035
TO
518 // Contribution amount links to view contribution
519 if (($value = CRM_Utils_Array::value('civicrm_contribution_total_amount', $row)) &&
520 CRM_Core_Permission::check('access CiviContribute')
521 ) {
522 $url = CRM_Utils_System::url("civicrm/contact/view/contribution",
523 "reset=1&id=" . $row['civicrm_contribution_id'] . "&cid=" . $row['civicrm_contact_id'] . "&action=view&context=contribution&selectedChild=contribute",
524 $this->_absoluteUrl
525 );
526 $rows[$rowNum]['civicrm_contribution_total_amount_link'] = $url;
527 $rows[$rowNum]['civicrm_contribution_total_amount_hover'] = ts("View this contribution.");
528 $entryFound = TRUE;
529 }
530
531 // convert campaign_id to campaign title
532 if (array_key_exists('civicrm_contribution_campaign_id', $row)) {
533 if ($value = $row['civicrm_contribution_campaign_id']) {
534 $rows[$rowNum]['civicrm_contribution_campaign_id'] = $this->activeCampaigns[$value];
535 $entryFound = TRUE;
536 }
537 }
538
539 // skip looking further in rows, if first row itself doesn't
540 if (!$entryFound) {
541 break;
542 }
543 $lastKey = $rowNum;
544 }
545 }
546}
547