-- CRM-13250 Added Contact Type and Contact SubType columns in report
[civicrm-core.git] / CRM / Report / Form / Contribute / HouseholdSummary.php
CommitLineData
6a488035 1<?php
6a488035
TO
2
3/*
4 +--------------------------------------------------------------------+
232624b1 5 | CiviCRM version 4.4 |
6a488035
TO
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2013 |
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
32 * @copyright CiviCRM LLC (c) 2004-2013
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) {
223 if (CRM_Utils_Array::value('required', $field) ||
224 CRM_Utils_Array::value($fieldName, $this->_params['fields'])
225 ) {
226 if ($tableName == 'civicrm_address') {
227 $this->_addressField = TRUE;
228 }
229 elseif ($tableName == 'civicrm_email') {
230 $this->_emailField = TRUE;
231 }
232
233 if (CRM_Utils_Array::value('statistics', $field)) {
234 foreach ($field['statistics'] as $stat => $label) {
235 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}_{$stat}";
236 $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
237 $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['type'] = $field['type'];
238 $this->_statFields[] = "{$tableName}_{$fieldName}_{$stat}";
239 }
240 }
241 else {
242 $select[] = "{$table['alias']}.{$fieldName} as {$tableName}_{$fieldName}";
243
244 $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array::value('title', $field);
245 $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field);
246 }
247 }
248 }
249 }
250 }
251 $this->_select = "SELECT " . implode(', ', $select) . " ";
252 }
253
254 function from() {
255
256 $this->_from = NULL;
257 $this->_from = "
39eb89f4
DL
258 FROM civicrm_relationship {$this->_aliases['civicrm_relationship']}
259 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_contact_household']} ON
6a488035 260 ({$this->_aliases['civicrm_contact_household']}.id = {$this->_aliases['civicrm_relationship']}.$this->householdContact AND {$this->_aliases['civicrm_contact_household']}.contact_type='Household')
39eb89f4
DL
261 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_contact']} ON
262 ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_relationship']}.$this->otherContact )
6a488035
TO
263 {$this->_aclFrom}
264 INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']} ON
265 ({$this->_aliases['civicrm_contribution']}.contact_id = {$this->_aliases['civicrm_relationship']}.$this->otherContact ) AND {$this->_aliases['civicrm_contribution']}.is_test = 0 ";
266
267 if ($this->_addressField) {
39eb89f4
DL
268 $this->_from .= "
269 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']} ON
270 {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_address']}.contact_id AND
6a488035
TO
271 {$this->_aliases['civicrm_address']}.is_primary = 1\n ";
272 }
273 if ($this->_emailField) {
274 $this->_from .= "
39eb89f4
DL
275 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']} ON
276 {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id AND
6a488035
TO
277 {$this->_aliases['civicrm_email']}.is_primary = 1\n ";
278 }
279 }
280
281 function where() {
282 $clauses = array();
283 foreach ($this->_columns as $tableName => $table) {
284 if (array_key_exists('filters', $table)) {
285 foreach ($table['filters'] as $fieldName => $field) {
286 $clause = NULL;
287 if (CRM_Utils_Array::value('type', $field) & CRM_Utils_Type::T_DATE) {
288 $relative = CRM_Utils_Array::value("{$fieldName}_relative", $this->_params);
289 $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params);
290 $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params);
291
292 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
293 }
294 else {
295 $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params);
296 if ($op) {
297 if ($fieldName == 'relationship_type_id') {
298 $clause = "{$this->_aliases['civicrm_relationship']}.relationship_type_id=" . $this->relationshipId;
299 }
300 else {
301 $clause = $this->whereClause($field,
302 $op,
303 CRM_Utils_Array::value("{$fieldName}_value", $this->_params),
304 CRM_Utils_Array::value("{$fieldName}_min", $this->_params),
305 CRM_Utils_Array::value("{$fieldName}_max", $this->_params)
306 );
307 }
308 }
309 }
310
311 if (!empty($clause)) {
312 $clauses[] = $clause;
313 }
314 }
315 }
316 }
317
318 if (empty($clauses)) {
319 $this->_where = "WHERE ( 1 )";
320 }
321 else {
322 $this->_where = "WHERE " . implode(' AND ', $clauses);
323 }
324
325 if ($this->_aclWhere) {
326 $this->_where .= " AND {$this->_aclWhere} ";
327 }
328 }
329
330 function groupBy() {
331 $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 ";
332 }
333
334 function orderBy() {
335 $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";
336 }
337
338 function statistics(&$rows) {
339 $statistics = parent::statistics($rows);
340
341 //hack filter display for relationship type
342 $type = substr($this->_params['relationship_type_id_value'], -3);
343 foreach ($statistics['filters'] as $id => $value) {
39eb89f4
DL
344 if (
345 $value['title'] == 'Relationship Type' &&
346 isset($this->relationTypes[$this->relationshipId . '_' . $type])
347 ) {
6a488035
TO
348 $statistics['filters'][$id]['value'] = 'Is equal to ' . $this->relationTypes[$this->relationshipId . '_' . $type];
349 }
350 }
351 return $statistics;
352 }
353
354 function postProcess() {
355
356 $this->beginPostProcess();
357 $getRelationship = $this->_params['relationship_type_id_value'];
358 $type = substr($getRelationship, -3);
359 $this->relationshipId = intval((substr($getRelationship, 0, strpos($getRelationship, '_'))));
360 if ($type == 'b_a') {
361 $this->householdContact = 'contact_id_b';
362 $this->otherContact = 'contact_id_a';
363 }
364 else {
365 $this->householdContact = 'contact_id_a';
366 $this->otherContact = 'contact_id_b';
367 }
368 $this->buildACLClause(array($this->_aliases['civicrm_contact'], $this->_aliases['civicrm_contact_household']));
369 $sql = $this->buildQuery(TRUE);
370 $rows = array();
371
372 $this->buildRows($sql, $rows);
373 $this->formatDisplay($rows);
374 $this->doTemplateAssignment($rows);
375 $this->endPostProcess($rows);
376 }
377
378 function validRelationships() {
379 $this->relationTypes = $relationTypes = array();
380
381 $params = array('contact_type_b' => 'Household', 'version' => 3);
39eb89f4 382 $typesA = civicrm_api('relationship_type', 'get', $params);
6a488035
TO
383 if (!CRM_Utils_Array::value('is_error', $typesA)) {
384 foreach ($typesA['values'] as $rel) {
385 $relationTypes[$rel['id']][$rel['id'] . '_b_a'] = $rel['label_b_a'];
386 }
387 }
388 $params = array('contact_type_a' => 'Household', 'version' => 3);
39eb89f4 389 $typesB = civicrm_api('relationship_type', 'get', $params);
6a488035
TO
390 if (!CRM_Utils_Array::value('is_error', $typesB)) {
391 foreach ($typesB['values'] as $rel) {
392 $relationTypes[$rel['id']][$rel['id'] . '_a_b'] = $rel['label_a_b'];
393 //$this->relationTypes[$rel['id'].'_a_b'] = $rel['label_a_b'];
394 }
395 }
396
397 ksort($relationTypes);
398 foreach ($relationTypes as $relationship) {
399 foreach ($relationship as $index => $label) {
400 $this->relationTypes[$index] = $label;
401 }
402 }
403 }
404
405 function alterDisplay(&$rows) {
406 // custom code to alter rows
407 $type = substr($this->_params['relationship_type_id_value'], -3);
408
409 $entryFound = FALSE;
410 $flagHousehold = $flagContact = 0;
6a488035
TO
411 foreach ($rows as $rowNum => $row) {
412
413 //replace retionship id by relationship name
414 if (array_key_exists('civicrm_relationship_relationship_type_id', $row)) {
415 if ($value = $row['civicrm_relationship_relationship_type_id']) {
416 $rows[$rowNum]['civicrm_relationship_relationship_type_id'] = $this->relationTypes[$value . '_' . $type];
417 $entryFound = TRUE;
418 }
419 }
420
421 //remove duplicate Organization names
422 if (array_key_exists('civicrm_contact_household_household_name', $row) && $this->_outputMode != 'csv') {
423 if ($value = $row['civicrm_contact_household_household_name']) {
424 if ($rowNum == 0) {
425 $priviousHousehold = $value;
426 }
427 else {
428 if ($priviousHousehold == $value) {
429 $flagHousehold = 1;
430 $priviousHousehold = $value;
431 }
432 else {
433 $flagHousehold = 0;
434 $priviousHousehold = $value;
435 }
436 }
437
438 if ($flagHousehold == 1) {
439 $rows[$rowNum]['civicrm_contact_household_household_name'] = "";
440 }
441 else {
442 $url = CRM_Utils_System::url('civicrm/contact/view',
443 'reset=1&cid=' . $rows[$rowNum]['civicrm_contact_household_id'],
444 $this->_absoluteUrl
445 );
446
447 $rows[$rowNum]['civicrm_contact_household_household_name'] = "<a href='$url' title='" . ts('View contact summary for this househould') . "'>" . $value . '</a>';
448 }
449 $entryFound = TRUE;
450 }
451 }
452
453 //remove duplicate Contact names and relationship type
454 if (array_key_exists('civicrm_contact_id', $row) && $this->_outputMode != 'csv') {
455 if ($value = $row['civicrm_contact_id']) {
456 if ($rowNum == 0) {
457 $priviousContact = $value;
458 }
459 else {
460 if ($priviousContact == $value) {
461 $flagContact = 1;
462 $priviousContact = $value;
463 }
464 else {
465 $flagContact = 0;
466 $priviousContact = $value;
467 }
468 }
469
470 if ($flagContact == 1 && $flagHousehold == 1) {
471 $rows[$rowNum]['civicrm_contact_sort_name'] = "";
472 $rows[$rowNum]['civicrm_relationship_relationship_type_id'] = "";
473 }
474
475 $entryFound = TRUE;
476 }
477 }
478
479 if (array_key_exists('civicrm_contribution_contribution_status_id', $row)) {
480 if ($value = $row['civicrm_contribution_contribution_status_id']) {
481 $rows[$rowNum]['civicrm_contribution_contribution_status_id'] = CRM_Contribute_PseudoConstant::contributionStatus($value);
482 }
483 }
484
485 // handle state province
486 if (array_key_exists('civicrm_address_state_province_id', $row)) {
487 if ($value = $row['civicrm_address_state_province_id']) {
488 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant::stateProvinceAbbreviation($value, FALSE);
489 }
490 $entryFound = TRUE;
491 }
492
493 // handle country
494 if (array_key_exists('civicrm_address_country_id', $row)) {
495 if ($value = $row['civicrm_address_country_id']) {
496 $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant::country($value, FALSE);
497 }
498 $entryFound = TRUE;
499 }
500
501 // convert display name to links
502 if (array_key_exists('civicrm_contact_sort_name', $row) &&
503 $rows[$rowNum]['civicrm_contact_sort_name'] &&
504 array_key_exists('civicrm_contact_id', $row)
505 ) {
506 $url = CRM_Report_Utils_Report::getNextUrl('contribute/detail',
507 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id'],
508 $this->_absoluteUrl, $this->_id, $this->_drilldownReport
509 );
510 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
511 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts('View contribution details for this individual');
512
513 $entryFound = TRUE;
514 }
515
eb3b4c04 516 if (CRM_Utils_Array::value('civicrm_contribution_total_amount', $row)) {
517 $row['civicrm_contribution_total_amount'] = CRM_Utils_Money::format($row['civicrm_contribution_total_amount'], $row['civicrm_contribution_currency']);
518 }
519
6a488035
TO
520 // Contribution amount links to view contribution
521 if (($value = CRM_Utils_Array::value('civicrm_contribution_total_amount', $row)) &&
522 CRM_Core_Permission::check('access CiviContribute')
523 ) {
524 $url = CRM_Utils_System::url("civicrm/contact/view/contribution",
525 "reset=1&id=" . $row['civicrm_contribution_id'] . "&cid=" . $row['civicrm_contact_id'] . "&action=view&context=contribution&selectedChild=contribute",
526 $this->_absoluteUrl
527 );
528 $rows[$rowNum]['civicrm_contribution_total_amount_link'] = $url;
529 $rows[$rowNum]['civicrm_contribution_total_amount_hover'] = ts("View this contribution.");
530 $entryFound = TRUE;
531 }
532
533 // convert campaign_id to campaign title
534 if (array_key_exists('civicrm_contribution_campaign_id', $row)) {
535 if ($value = $row['civicrm_contribution_campaign_id']) {
536 $rows[$rowNum]['civicrm_contribution_campaign_id'] = $this->activeCampaigns[$value];
537 $entryFound = TRUE;
538 }
539 }
540
541 // skip looking further in rows, if first row itself doesn't
542 if (!$entryFound) {
543 break;
544 }
545 $lastKey = $rowNum;
546 }
547 }
548}
549