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