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