Merge pull request #4822 from kurund/indentation-fixes
[civicrm-core.git] / CRM / Report / Form / Contribute / HouseholdSummary.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.6 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
33 * $Id$
34 *
35 */
36 class 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
44 protected $_summary = NULL;
45
46 /**
47 *
48 */
49 /**
50 *
51 */
52 public function __construct() {
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(
64 'civicrm_contact_household' => array(
65 'dao' => 'CRM_Contact_DAO_Contact',
66 'fields' => array(
67 'household_name' => array(
68 'title' => ts('Household Name'),
69 'required' => TRUE,
70 ),
71 'id' => array(
72 'no_display' => TRUE,
73 'required' => TRUE,
74 ),
75 'contact_type' => array(
76 'title' => ts('Contact Type'),
77 ),
78 'contact_sub_type' => array(
79 'title' => ts('Contact Subtype'),
80 ),
81 ),
82 'filters' => array(
83 'household_name' => array('title' => ts('Household Name')),
84 ),
85 'grouping' => 'household-fields',
86 ),
87 'civicrm_relationship' => array(
88 'dao' => 'CRM_Contact_DAO_Relationship',
89 'fields' => array(
90 'relationship_type_id' => array(
91 'title' => ts('Relationship Type'),
92 ),
93 ),
94 'filters' => array(
95 'relationship_type_id' => array(
96 'title' => ts('Relationship Type'),
97 'type' => CRM_Utils_Type::T_INT,
98 'operatorType' => CRM_Report_Form::OP_SELECT,
99 'options' => $this->relationTypes,
100 'default' => key($this->relationTypes),
101 ),
102 ),
103 'grouping' => 'household-fields',
104 ),
105 'civicrm_contact' => array(
106 'dao' => 'CRM_Contact_DAO_Contact',
107 'fields' => array(
108 'sort_name' => array(
109 'title' => ts('Contact Name'),
110 'required' => TRUE,
111 ),
112 'id' => array(
113 'no_display' => TRUE,
114 'required' => TRUE,
115 ),
116 ),
117 'grouping' => 'contact-fields',
118 ),
119 'civicrm_contribution' => array(
120 'dao' => 'CRM_Contribute_DAO_Contribution',
121 'fields' => array(
122 'total_amount' => array(
123 'title' => ts('Amount'),
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 ),
134 'check_number' => array(
135 'title' => ts('Check Number'),
136 ),
137 'currency' => array(
138 'required' => TRUE,
139 'no_display' => TRUE,
140 ),
141 'trxn_id' => NULL,
142 'receive_date' => array('default' => TRUE),
143 'receipt_date' => NULL,
144 ),
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',
150 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
151 'options' => CRM_Core_OptionGroup::values('currencies_enabled'),
152 'default' => NULL,
153 'type' => CRM_Utils_Type::T_STRING,
154 ),
155 'contribution_status_id' => array(
156 'title' => ts('Contribution Status'),
157 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
158 'options' => CRM_Contribute_PseudoConstant::contributionStatus(),
159 'default' => array(1),
160 ),
161 ),
162 'grouping' => 'contri-fields',
163 ),
164 'civicrm_address' => array(
165 'dao' => 'CRM_Core_DAO_Address',
166 'fields' => array(
167 'street_address' => NULL,
168 'city' => NULL,
169 'postal_code' => NULL,
170 'state_province_id' => array(
171 'title' => ts('State/Province'),
172 ),
173 'country_id' => array(
174 'title' => ts('Country'),
175 ),
176 ),
177 'grouping' => 'contact-fields',
178 ),
179 'civicrm_email' => array(
180 'dao' => 'CRM_Core_DAO_Email',
181 'fields' => array(
182 'email' => NULL
183 ),
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 );
193 $this->_columns['civicrm_contribution']['filters']['campaign_id'] = array(
194 'title' => ts('Campaign'),
195 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
196 'options' => $this->activeCampaigns,
197 );
198 }
199 $this->_currencyColumn = 'civicrm_contribution_currency';
200 parent::__construct();
201 }
202
203 public function preProcess() {
204 parent::preProcess();
205 }
206
207 public function select() {
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) {
212 if (!empty($field['required']) ||
213 !empty($this->_params['fields'][$fieldName])
214 ) {
215 if ($tableName == 'civicrm_address') {
216 $this->_addressField = TRUE;
217 }
218 elseif ($tableName == 'civicrm_email') {
219 $this->_emailField = TRUE;
220 }
221
222 if (!empty($field['statistics'])) {
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 {
231 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
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
243 public function from() {
244
245 $this->_from = NULL;
246 $this->_from = "
247 FROM civicrm_relationship {$this->_aliases['civicrm_relationship']}
248 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_contact_household']} ON
249 ({$this->_aliases['civicrm_contact_household']}.id = {$this->_aliases['civicrm_relationship']}.$this->householdContact AND {$this->_aliases['civicrm_contact_household']}.contact_type='Household')
250 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_contact']} ON
251 ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_relationship']}.$this->otherContact )
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) {
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
260 {$this->_aliases['civicrm_address']}.is_primary = 1\n ";
261 }
262 if ($this->_emailField) {
263 $this->_from .= "
264 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']} ON
265 {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id AND
266 {$this->_aliases['civicrm_email']}.is_primary = 1\n ";
267 }
268 }
269
270 public function where() {
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);
278 $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params);
279 $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params);
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') {
287 $clause =
288 "{$this->_aliases['civicrm_relationship']}.relationship_type_id=" .
289 $this->relationshipId;
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
321 public function groupBy() {
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
325 public function orderBy() {
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
329 /**
330 * @param $rows
331 *
332 * @return array
333 */
334 public function statistics(&$rows) {
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) {
340 if (
341 $value['title'] == 'Relationship Type' &&
342 isset($this->relationTypes[$this->relationshipId . '_' . $type])
343 ) {
344 $statistics['filters'][$id]['value'] = 'Is equal to ' .
345 $this->relationTypes[$this->relationshipId . '_' . $type];
346 }
347 }
348 return $statistics;
349 }
350
351 public function postProcess() {
352
353 $this->beginPostProcess();
354 $this->buildACLClause(array(
355 $this->_aliases['civicrm_contact'],
356 $this->_aliases['civicrm_contact_household']
357 ));
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 */
370 public function beginPostProcessCommon() {
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 }
382 }
383
384 public function validRelationships() {
385 $this->relationTypes = $relationTypes = array();
386
387 $params = array('contact_type_b' => 'Household', 'version' => 3);
388 $typesA = civicrm_api('relationship_type', 'get', $params);
389 if (empty($typesA['is_error'])) {
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);
395 $typesB = civicrm_api('relationship_type', 'get', $params);
396 if (empty($typesB['is_error'])) {
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
411 /**
412 * @param $rows
413 */
414 public function alterDisplay(&$rows) {
415 // custom code to alter rows
416 $type = substr($this->_params['relationship_type_id_value'], -3);
417
418 $entryFound = FALSE;
419 $flagHousehold = $flagContact = 0;
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']) {
425 $rows[$rowNum]['civicrm_relationship_relationship_type_id'] = $this->relationTypes[
426 $value . '_' . $type];
427 $entryFound = TRUE;
428 }
429 }
430
431 //remove duplicate Organization names
432 if (array_key_exists('civicrm_contact_household_household_name', $row) &&
433 $this->_outputMode != 'csv'
434 ) {
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
459 $rows[$rowNum]['civicrm_contact_household_household_name'] =
460 "<a href='$url' title='" .
461 ts('View contact summary for this househould') . "'>" . $value .
462 '</a>';
463 }
464 $entryFound = TRUE;
465 }
466 }
467
468 //remove duplicate Contact names and relationship type
469 if (array_key_exists('civicrm_contact_id', $row) &&
470 $this->_outputMode != 'csv'
471 ) {
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
533 if (!empty($row['civicrm_contribution_total_amount'])) {
534 $row['civicrm_contribution_total_amount'] = CRM_Utils_Money::format($row['civicrm_contribution_total_amount'], $row['civicrm_contribution_currency']);
535 }
536
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",
542 "reset=1&id=" . $row['civicrm_contribution_id'] . "&cid=" .
543 $row['civicrm_contact_id'] .
544 "&action=view&context=contribution&selectedChild=contribute",
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 }