Merge pull request #4401 from eileenmcnaughton/CRM-14730
[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
74cf4551
EM
46 /**
47 *
48 */
49 /**
50 *
51 */
eb3b4c04 52 function __construct() {
6a488035
TO
53 self::validRelationships();
54
55 $config = CRM_Core_Config::singleton();
56 $campaignEnabled = in_array("CiviCampaign", $config->enableComponents);
57 if ($campaignEnabled) {
58 $getCampaigns = CRM_Campaign_BAO_Campaign::getPermissionedCampaigns(NULL, NULL, TRUE, FALSE, TRUE);
59 $this->activeCampaigns = $getCampaigns['campaigns'];
60 asort($this->activeCampaigns);
61 }
62
63 $this->_columns = array(
a92f4449 64 'civicrm_contact_household' => array(
6a488035 65 'dao' => 'CRM_Contact_DAO_Contact',
a92f4449 66 'fields' => array(
6a488035 67 'household_name' =>
a92f4449
EM
68 array(
69 'title' => ts('Household Name'),
70 'required' => TRUE,
71 ),
72 'id' => array(
6a488035
TO
73 'no_display' => TRUE,
74 'required' => TRUE,
75 ),
a92f4449 76 'contact_type' => array(
30f85891
RN
77 'title' => ts('Contact Type'),
78 ),
a92f4449 79 'contact_sub_type' => array(
30f85891
RN
80 'title' => ts('Contact SubType'),
81 ),
6a488035 82 ),
a92f4449
EM
83 'filters' => array(
84 'household_name' => array('title' => ts('Household Name')),
6a488035
TO
85 ),
86 'grouping' => 'household-fields',
87 ),
a92f4449 88 'civicrm_relationship' => array(
6a488035 89 'dao' => 'CRM_Contact_DAO_Relationship',
a92f4449
EM
90 'fields' => array(
91 'relationship_type_id' => array(
92 'title' => ts('Relationship Type'),
6a488035
TO
93 ),
94 ),
a92f4449
EM
95 'filters' => array(
96 'relationship_type_id' => array(
6a488035
TO
97 'title' => ts('Relationship Type'),
98 'type' => CRM_Utils_Type::T_INT,
99 'operatorType' => CRM_Report_Form::OP_SELECT,
100 'options' => $this->relationTypes,
101 'default' => array(1),
102 ),
103 ),
104 'grouping' => 'household-fields',
105 ),
a92f4449 106 'civicrm_contact' => array(
6a488035 107 'dao' => 'CRM_Contact_DAO_Contact',
a92f4449
EM
108 'fields' => array(
109 'sort_name' => array(
110 'title' => ts('Contact Name'),
6a488035
TO
111 'required' => TRUE,
112 ),
a92f4449 113 'id' => array(
6a488035
TO
114 'no_display' => TRUE,
115 'required' => TRUE,
116 ),
117 ),
118 'grouping' => 'contact-fields',
119 ),
a92f4449 120 'civicrm_contribution' => array(
6a488035 121 'dao' => 'CRM_Contribute_DAO_Contribution',
a92f4449
EM
122 'fields' => array(
123 'total_amount' => array(
124 'title' => ts('Amount'),
6a488035
TO
125 'required' => TRUE,
126 ),
127 'id' => array(
128 'no_display' => TRUE,
129 'required' => TRUE,
130 ),
131 'contribution_status_id' => array(
132 'title' => 'Contribution Status',
133 'default' => TRUE,
134 ),
a92f4449
EM
135 'check_number' => array(
136 'title' => ts('Check Number'),
f5aace12 137 ),
eb3b4c04 138 'currency' => array(
139 'required' => TRUE,
140 'no_display' => TRUE,
141 ),
6a488035
TO
142 'trxn_id' => NULL,
143 'receive_date' => array('default' => TRUE),
144 'receipt_date' => NULL,
145 ),
a92f4449
EM
146 'filters' => array(
147 'receive_date' => array('operatorType' => CRM_Report_Form::OP_DATE),
148 'total_amount' => array('title' => ts('Amount Between')),
149 'currency' => array(
150 'title' => 'Currency',
eb3b4c04 151 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
152 'options' => CRM_Core_OptionGroup::values('currencies_enabled'),
d1b0931b 153 'default' => NULL,
eb3b4c04 154 'type' => CRM_Utils_Type::T_STRING,
155 ),
a92f4449
EM
156 'contribution_status_id' => array(
157 'title' => ts('Contribution Status'),
6a488035
TO
158 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
159 'options' => CRM_Contribute_PseudoConstant::contributionStatus(),
160 'default' => array(1),
161 ),
162 ),
163 'grouping' => 'contri-fields',
164 ),
a92f4449 165 'civicrm_address' => array(
6a488035 166 'dao' => 'CRM_Core_DAO_Address',
a92f4449 167 'fields' => array(
6a488035
TO
168 'street_address' => NULL,
169 'city' => NULL,
170 'postal_code' => NULL,
a92f4449
EM
171 'state_province_id' => array(
172 'title' => ts('State/Province'),
6a488035 173 ),
a92f4449
EM
174 'country_id' => array(
175 'title' => ts('Country'),
6a488035
TO
176 ),
177 ),
178 'grouping' => 'contact-fields',
179 ),
a92f4449 180 'civicrm_email' => array(
6a488035 181 'dao' => 'CRM_Core_DAO_Email',
a92f4449
EM
182 'fields' => array(
183 'email' => NULL
184 ),
6a488035
TO
185 'grouping' => 'contact-fields',
186 ),
187 );
188
189 if ($campaignEnabled && !empty($this->activeCampaigns)) {
190 $this->_columns['civicrm_contribution']['fields']['campaign_id'] = array(
191 'title' => 'Campaign',
192 'default' => 'false',
193 );
a92f4449
EM
194 $this->_columns['civicrm_contribution']['filters']['campaign_id'] = array(
195 'title' => ts('Campaign'),
6a488035
TO
196 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
197 'options' => $this->activeCampaigns,
198 );
199 }
eb3b4c04 200 $this->_currencyColumn = 'civicrm_contribution_currency';
6a488035
TO
201 parent::__construct();
202 }
203
204 function preProcess() {
205 parent::preProcess();
206 }
207
208 function select() {
209 $this->_columnHeaders = $select = array();
210 foreach ($this->_columns as $tableName => $table) {
211 if (array_key_exists('fields', $table)) {
212 foreach ($table['fields'] as $fieldName => $field) {
8cc574cf 213 if (!empty($field['required']) || !empty($this->_params['fields'][$fieldName])) {
6a488035
TO
214 if ($tableName == 'civicrm_address') {
215 $this->_addressField = TRUE;
216 }
217 elseif ($tableName == 'civicrm_email') {
218 $this->_emailField = TRUE;
219 }
220
a7488080 221 if (!empty($field['statistics'])) {
6a488035
TO
222 foreach ($field['statistics'] as $stat => $label) {
223 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}_{$stat}";
224 $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
225 $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['type'] = $field['type'];
226 $this->_statFields[] = "{$tableName}_{$fieldName}_{$stat}";
227 }
228 }
229 else {
22a17535 230 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
6a488035
TO
231
232 $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array::value('title', $field);
233 $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field);
234 }
235 }
236 }
237 }
238 }
239 $this->_select = "SELECT " . implode(', ', $select) . " ";
240 }
241
242 function from() {
243
244 $this->_from = NULL;
245 $this->_from = "
39eb89f4
DL
246 FROM civicrm_relationship {$this->_aliases['civicrm_relationship']}
247 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_contact_household']} ON
6a488035 248 ({$this->_aliases['civicrm_contact_household']}.id = {$this->_aliases['civicrm_relationship']}.$this->householdContact AND {$this->_aliases['civicrm_contact_household']}.contact_type='Household')
39eb89f4
DL
249 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_contact']} ON
250 ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_relationship']}.$this->otherContact )
6a488035
TO
251 {$this->_aclFrom}
252 INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']} ON
253 ({$this->_aliases['civicrm_contribution']}.contact_id = {$this->_aliases['civicrm_relationship']}.$this->otherContact ) AND {$this->_aliases['civicrm_contribution']}.is_test = 0 ";
254
255 if ($this->_addressField) {
39eb89f4
DL
256 $this->_from .= "
257 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']} ON
258 {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_address']}.contact_id AND
6a488035
TO
259 {$this->_aliases['civicrm_address']}.is_primary = 1\n ";
260 }
261 if ($this->_emailField) {
262 $this->_from .= "
39eb89f4
DL
263 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']} ON
264 {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id AND
6a488035
TO
265 {$this->_aliases['civicrm_email']}.is_primary = 1\n ";
266 }
267 }
268
269 function where() {
270 $clauses = array();
271 foreach ($this->_columns as $tableName => $table) {
272 if (array_key_exists('filters', $table)) {
273 foreach ($table['filters'] as $fieldName => $field) {
274 $clause = NULL;
275 if (CRM_Utils_Array::value('type', $field) & CRM_Utils_Type::T_DATE) {
276 $relative = CRM_Utils_Array::value("{$fieldName}_relative", $this->_params);
a92f4449
EM
277 $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params);
278 $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params);
6a488035
TO
279
280 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
281 }
282 else {
283 $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params);
284 if ($op) {
285 if ($fieldName == 'relationship_type_id') {
286 $clause = "{$this->_aliases['civicrm_relationship']}.relationship_type_id=" . $this->relationshipId;
287 }
288 else {
289 $clause = $this->whereClause($field,
290 $op,
291 CRM_Utils_Array::value("{$fieldName}_value", $this->_params),
292 CRM_Utils_Array::value("{$fieldName}_min", $this->_params),
293 CRM_Utils_Array::value("{$fieldName}_max", $this->_params)
294 );
295 }
296 }
297 }
298
299 if (!empty($clause)) {
300 $clauses[] = $clause;
301 }
302 }
303 }
304 }
305
306 if (empty($clauses)) {
307 $this->_where = "WHERE ( 1 )";
308 }
309 else {
310 $this->_where = "WHERE " . implode(' AND ', $clauses);
311 }
312
313 if ($this->_aclWhere) {
314 $this->_where .= " AND {$this->_aclWhere} ";
315 }
316 }
317
318 function groupBy() {
319 $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 ";
320 }
321
322 function orderBy() {
323 $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";
324 }
325
74cf4551
EM
326 /**
327 * @param $rows
328 *
329 * @return array
330 */
6a488035
TO
331 function statistics(&$rows) {
332 $statistics = parent::statistics($rows);
333
334 //hack filter display for relationship type
335 $type = substr($this->_params['relationship_type_id_value'], -3);
336 foreach ($statistics['filters'] as $id => $value) {
39eb89f4
DL
337 if (
338 $value['title'] == 'Relationship Type' &&
339 isset($this->relationTypes[$this->relationshipId . '_' . $type])
340 ) {
a92f4449
EM
341 $statistics['filters'][$id]['value'] = 'Is equal to ' .
342 $this->relationTypes[$this->relationshipId . '_' . $type];
6a488035
TO
343 }
344 }
345 return $statistics;
346 }
347
348 function postProcess() {
349
350 $this->beginPostProcess();
351 $getRelationship = $this->_params['relationship_type_id_value'];
352 $type = substr($getRelationship, -3);
353 $this->relationshipId = intval((substr($getRelationship, 0, strpos($getRelationship, '_'))));
354 if ($type == 'b_a') {
355 $this->householdContact = 'contact_id_b';
356 $this->otherContact = 'contact_id_a';
357 }
358 else {
359 $this->householdContact = 'contact_id_a';
360 $this->otherContact = 'contact_id_b';
361 }
362 $this->buildACLClause(array($this->_aliases['civicrm_contact'], $this->_aliases['civicrm_contact_household']));
363 $sql = $this->buildQuery(TRUE);
364 $rows = array();
365
366 $this->buildRows($sql, $rows);
367 $this->formatDisplay($rows);
368 $this->doTemplateAssignment($rows);
369 $this->endPostProcess($rows);
370 }
371
372 function validRelationships() {
373 $this->relationTypes = $relationTypes = array();
374
375 $params = array('contact_type_b' => 'Household', 'version' => 3);
39eb89f4 376 $typesA = civicrm_api('relationship_type', 'get', $params);
a7488080 377 if (empty($typesA['is_error'])) {
6a488035
TO
378 foreach ($typesA['values'] as $rel) {
379 $relationTypes[$rel['id']][$rel['id'] . '_b_a'] = $rel['label_b_a'];
380 }
381 }
382 $params = array('contact_type_a' => 'Household', 'version' => 3);
39eb89f4 383 $typesB = civicrm_api('relationship_type', 'get', $params);
a7488080 384 if (empty($typesB['is_error'])) {
6a488035
TO
385 foreach ($typesB['values'] as $rel) {
386 $relationTypes[$rel['id']][$rel['id'] . '_a_b'] = $rel['label_a_b'];
387 //$this->relationTypes[$rel['id'].'_a_b'] = $rel['label_a_b'];
388 }
389 }
390
391 ksort($relationTypes);
392 foreach ($relationTypes as $relationship) {
393 foreach ($relationship as $index => $label) {
394 $this->relationTypes[$index] = $label;
395 }
396 }
397 }
398
74cf4551
EM
399 /**
400 * @param $rows
401 */
6a488035
TO
402 function alterDisplay(&$rows) {
403 // custom code to alter rows
404 $type = substr($this->_params['relationship_type_id_value'], -3);
405
406 $entryFound = FALSE;
407 $flagHousehold = $flagContact = 0;
6a488035
TO
408 foreach ($rows as $rowNum => $row) {
409
410 //replace retionship id by relationship name
411 if (array_key_exists('civicrm_relationship_relationship_type_id', $row)) {
412 if ($value = $row['civicrm_relationship_relationship_type_id']) {
413 $rows[$rowNum]['civicrm_relationship_relationship_type_id'] = $this->relationTypes[$value . '_' . $type];
414 $entryFound = TRUE;
415 }
416 }
417
418 //remove duplicate Organization names
419 if (array_key_exists('civicrm_contact_household_household_name', $row) && $this->_outputMode != 'csv') {
420 if ($value = $row['civicrm_contact_household_household_name']) {
421 if ($rowNum == 0) {
422 $priviousHousehold = $value;
423 }
424 else {
425 if ($priviousHousehold == $value) {
426 $flagHousehold = 1;
427 $priviousHousehold = $value;
428 }
429 else {
430 $flagHousehold = 0;
431 $priviousHousehold = $value;
432 }
433 }
434
435 if ($flagHousehold == 1) {
436 $rows[$rowNum]['civicrm_contact_household_household_name'] = "";
437 }
438 else {
439 $url = CRM_Utils_System::url('civicrm/contact/view',
440 'reset=1&cid=' . $rows[$rowNum]['civicrm_contact_household_id'],
441 $this->_absoluteUrl
442 );
443
a92f4449
EM
444 $rows[$rowNum]['civicrm_contact_household_household_name'] =
445 "<a href='$url' title='" . ts('View contact summary for this househould') . "'>" . $value . '</a>';
6a488035
TO
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",
a92f4449
EM
523 "reset=1&id=" . $row['civicrm_contribution_id'] . "&cid=" . $row['civicrm_contact_id'] .
524 "&action=view&context=contribution&selectedChild=contribute",
6a488035
TO
525 $this->_absoluteUrl
526 );
527 $rows[$rowNum]['civicrm_contribution_total_amount_link'] = $url;
528 $rows[$rowNum]['civicrm_contribution_total_amount_hover'] = ts("View this contribution.");
529 $entryFound = TRUE;
530 }
531
532 // convert campaign_id to campaign title
533 if (array_key_exists('civicrm_contribution_campaign_id', $row)) {
534 if ($value = $row['civicrm_contribution_campaign_id']) {
535 $rows[$rowNum]['civicrm_contribution_campaign_id'] = $this->activeCampaigns[$value];
536 $entryFound = TRUE;
537 }
538 }
539
540 // skip looking further in rows, if first row itself doesn't
541 if (!$entryFound) {
542 break;
543 }
544 $lastKey = $rowNum;
545 }
546 }
547}
548