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