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