3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2018
33 class CRM_Report_Form_Contribute_OrganizationSummary
extends CRM_Report_Form
{
35 protected $_addressField = FALSE;
37 protected $_emailField = FALSE;
39 public $_drilldownReport = array('contribute/detail' => 'Link to Detail Report');
41 protected $_summary = NULL;
44 * Organisation contact ie. 'contact_id_b' or 'contact_id_a'
47 protected $orgContact;
50 * Related Contact ie. 'contact_id_b' or 'contact_id_a'
53 protected $otherContact;
58 public function __construct() {
59 self
::validRelationships();
60 $config = CRM_Core_Config
::singleton();
61 $campaignEnabled = in_array('CiviCampaign', $config->enableComponents
);
62 if ($campaignEnabled) {
63 $getCampaigns = CRM_Campaign_BAO_Campaign
::getPermissionedCampaigns(NULL, NULL, TRUE, FALSE, TRUE);
64 $this->activeCampaigns
= $getCampaigns['campaigns'];
65 asort($this->activeCampaigns
);
68 $this->_columns
= array(
69 'civicrm_contact_organization' => array(
70 'dao' => 'CRM_Contact_DAO_Contact',
72 'organization_name' => array(
73 'title' => ts('Organization Name'),
81 'contact_type' => array(
82 'title' => ts('Contact Type'),
84 'contact_sub_type' => array(
85 'title' => ts('Contact Subtype'),
89 'organization_name' => array(
90 'title' => ts('Organization Name'),
92 'is_deleted' => array(
94 'type' => CRM_Utils_Type
::T_BOOLEAN
,
97 'grouping' => 'organization-fields',
99 'civicrm_line_item' => array(
100 'dao' => 'CRM_Price_DAO_LineItem',
102 'civicrm_relationship' => array(
103 'dao' => 'CRM_Contact_DAO_Relationship',
105 'relationship_type_id' => array(
106 'title' => ts('Relationship Type'),
110 'relationship_type_id' => array(
111 'title' => ts('Relationship Type'),
112 'type' => CRM_Utils_Type
::T_INT
,
113 'operatorType' => CRM_Report_Form
::OP_SELECT
,
114 'options' => $this->relationTypes
,
115 'default' => key($this->relationTypes
),
118 'grouping' => 'organization-fields',
120 'civicrm_contact' => array(
121 'dao' => 'CRM_Contact_DAO_Contact',
123 'sort_name' => array(
124 'title' => ts('Contact Name'),
128 'no_display' => TRUE,
132 'grouping' => 'contact-fields',
134 'civicrm_contribution' => array(
135 'dao' => 'CRM_Contribute_DAO_Contribution',
137 'total_amount' => array(
138 'title' => ts('Amount'),
142 'no_display' => TRUE,
145 'contribution_status_id' => array(
146 'title' => ts('Contribution Status'),
149 'check_number' => array(
150 'title' => ts('Check Number'),
154 'no_display' => TRUE,
157 'receive_date' => array('default' => TRUE),
158 'receipt_date' => NULL,
161 'receive_date' => array('operatorType' => CRM_Report_Form
::OP_DATE
),
162 'total_amount' => array('title' => ts('Amount Between')),
164 'title' => ts('Currency'),
165 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
166 'options' => CRM_Core_OptionGroup
::values('currencies_enabled'),
168 'type' => CRM_Utils_Type
::T_STRING
,
170 'contribution_status_id' => array(
171 'title' => ts('Contribution Status'),
172 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
173 'options' => CRM_Contribute_PseudoConstant
::contributionStatus(),
174 'default' => array(1),
177 'grouping' => 'contri-fields',
179 'civicrm_address' => array(
180 'dao' => 'CRM_Core_DAO_Address',
182 'street_address' => NULL,
184 'postal_code' => NULL,
185 'state_province_id' => array(
186 'title' => ts('State/Province'),
188 'country_id' => array(
189 'title' => ts('Country'),
192 'grouping' => 'contact-fields',
194 'civicrm_email' => array(
195 'dao' => 'CRM_Core_DAO_Email',
196 'fields' => array('email' => NULL),
197 'grouping' => 'contact-fields',
199 'civicrm_financial_trxn' => array(
200 'dao' => 'CRM_Financial_DAO_FinancialTrxn',
202 'card_type_id' => array(
203 'title' => ts('Credit Card Type'),
204 'dbAlias' => 'GROUP_CONCAT(financial_trxn_civireport.card_type_id SEPARATOR ",")',
208 'card_type_id' => array(
209 'title' => ts('Credit Card Type'),
210 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
211 'options' => CRM_Financial_DAO_FinancialTrxn
::buildOptions('card_type_id'),
213 'type' => CRM_Utils_Type
::T_STRING
,
219 if ($campaignEnabled && !empty($this->activeCampaigns
)) {
220 $this->_columns
['civicrm_contribution']['fields']['campaign_id'] = array(
221 'title' => ts('Campaign'),
222 'default' => 'false',
224 $this->_columns
['civicrm_contribution']['filters']['campaign_id'] = array(
225 'title' => ts('Campaign'),
226 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
227 'options' => $this->activeCampaigns
,
228 'type' => CRM_Utils_Type
::T_INT
,
232 $this->_currencyColumn
= 'civicrm_contribution_currency';
233 parent
::__construct();
236 public function preProcess() {
237 parent
::preProcess();
240 public function select() {
241 $this->_columnHeaders
= $select = array();
242 foreach ($this->_columns
as $tableName => $table) {
243 if (array_key_exists('fields', $table)) {
244 foreach ($table['fields'] as $fieldName => $field) {
245 if (!empty($field['required']) ||
!empty($this->_params
['fields'][$fieldName])) {
246 if ($tableName == 'civicrm_address') {
247 $this->_addressField
= TRUE;
249 elseif ($tableName == 'civicrm_email') {
250 $this->_emailField
= TRUE;
253 if (!empty($field['statistics'])) {
254 foreach ($field['statistics'] as $stat => $label) {
255 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}_{$stat}";
256 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
257 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['type'] = $field['type'];
258 $this->_statFields
[] = "{$tableName}_{$fieldName}_{$stat}";
262 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
264 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array
::value('title', $field);
265 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
271 $this->_selectClauses
= $select;
272 $this->_select
= "SELECT " . implode(', ', $select) . " ";
275 public function from() {
279 FROM civicrm_relationship {$this->_aliases['civicrm_relationship']}
280 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_contact_organization']} ON
281 ({$this->_aliases['civicrm_contact_organization']}.id = {$this->_aliases['civicrm_relationship']}.$this->orgContact AND {$this->_aliases['civicrm_contact_organization']}.contact_type='Organization')
282 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_contact']} ON
283 ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_relationship']}.$this->otherContact )
285 INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']} ON
286 ({$this->_aliases['civicrm_contribution']}.contact_id = {$this->_aliases['civicrm_relationship']}.$this->otherContact ) AND {$this->_aliases['civicrm_contribution']}.is_test = 0 ";
288 if ($this->_addressField
) {
290 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']} ON
291 {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_address']}.contact_id AND
292 {$this->_aliases['civicrm_address']}.is_primary = 1\n ";
294 if ($this->_emailField
) {
296 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']} ON
297 {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id AND
298 {$this->_aliases['civicrm_email']}.is_primary = 1\n ";
301 // for credit card type
302 $this->addFinancialTrxnFromClause();
305 public function where() {
307 foreach ($this->_columns
as $tableName => $table) {
308 if (array_key_exists('filters', $table)) {
309 foreach ($table['filters'] as $fieldName => $field) {
311 if (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
) {
312 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
313 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
314 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
316 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
319 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
321 if ($fieldName == 'relationship_type_id') {
322 $clause = "{$this->_aliases['civicrm_relationship']}.relationship_type_id=" . $this->relationshipId
;
325 $clause = $this->whereClause($field,
327 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
328 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
329 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
335 if (!empty($clause)) {
336 $clauses[] = $clause;
342 if (empty($clauses)) {
343 $this->_where
= "WHERE ( 1 )";
346 $this->_where
= "WHERE " . implode(' AND ', $clauses);
349 if ($this->_aclWhere
) {
350 $this->_where
.= " AND {$this->_aclWhere} ";
354 public function groupBy() {
356 "{$this->_aliases['civicrm_relationship']}.$this->orgContact",
357 "{$this->_aliases['civicrm_relationship']}.$this->otherContact",
358 "{$this->_aliases['civicrm_contribution']}.id",
359 "{$this->_aliases['civicrm_relationship']}.relationship_type_id",
361 $this->_groupBy
= CRM_Contact_BAO_Query
::getGroupByFromSelectColumns($this->_selectClauses
, $groupBy);
364 public function orderBy() {
365 $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";
373 public function statistics(&$rows) {
374 $statistics = parent
::statistics($rows);
376 //hack filter display for relationship type
377 $type = substr($this->_params
['relationship_type_id_value'], -3);
378 foreach ($statistics['filters'] as $id => $value) {
379 if ($value['title'] == 'Relationship Type' && !empty($id)) {
380 $statistics['filters'][$id]['value'] = 'Is equal to ' .
381 $this->relationTypes
[$this->relationshipId
. '_' . $type];
387 public function postProcess() {
388 $this->beginPostProcess();
389 $this->buildACLClause(array($this->_aliases
['civicrm_contact'], $this->_aliases
['civicrm_contact_organization']));
390 $sql = $this->buildQuery(TRUE);
392 $this->buildRows($sql, $rows);
393 $this->formatDisplay($rows);
394 $this->doTemplateAssignment($rows);
395 $this->endPostProcess($rows);
399 * Set variables to be accessed by API and form layer in processing.
401 public function beginPostProcessCommon() {
402 $getRelationship = $this->_params
['relationship_type_id_value'];
403 $type = substr($getRelationship, -3);
404 $this->relationshipId
= intval((substr($getRelationship, 0, strpos($getRelationship, '_'))));
405 if ($type == 'b_a') {
406 $this->orgContact
= 'contact_id_b';
407 $this->otherContact
= 'contact_id_a';
410 $this->orgContact
= 'contact_id_a';
411 $this->otherContact
= 'contact_id_b';
415 public function validRelationships() {
416 $this->relationTypes
= $relationTypes = array();
418 $params = array('contact_type_b' => 'Organization', 'version' => 3);
419 $typesA = civicrm_api('relationship_type', 'get', $params);
421 if (empty($typesA['is_error'])) {
422 foreach ($typesA['values'] as $rel) {
423 $relationTypes[$rel['id']][$rel['id'] . '_b_a'] = $rel['label_b_a'];
427 $params = array('contact_type_a' => 'Organization', 'version' => 3);
428 $typesB = civicrm_api('relationship_type', 'get', $params);
430 if (empty($typesB['is_error'])) {
431 foreach ($typesB['values'] as $rel) {
432 $relationTypes[$rel['id']][$rel['id'] . '_a_b'] = $rel['label_a_b'];
436 ksort($relationTypes);
437 foreach ($relationTypes as $relationship) {
438 foreach ($relationship as $index => $label) {
439 $this->relationTypes
[$index] = $label;
445 * Alter display of rows.
447 * Iterate through the rows retrieved via SQL and make changes for display purposes,
448 * such as rendering contacts as links.
451 * Rows generated by SQL, with an array for each row.
453 public function alterDisplay(&$rows) {
454 $type = substr($this->_params
['relationship_type_id_value'], -3);
457 $flagOrganization = $flagContact = 0;
459 foreach ($rows as $rowNum => $row) {
461 //replace retionship id by relationship name
462 if (array_key_exists('civicrm_relationship_relationship_type_id', $row)) {
463 if ($value = $row['civicrm_relationship_relationship_type_id']) {
464 $rows[$rowNum]['civicrm_relationship_relationship_type_id'] = $this->relationTypes
[$value . '_' . $type];
469 //remove duplicate Organization names
470 if (array_key_exists('civicrm_contact_organization_id', $row) && $this->_outputMode
!= 'csv') {
471 if ($value = $row['civicrm_contact_organization_id']) {
473 $previousOrganization = $value;
476 if ($previousOrganization == $value) {
477 $flagOrganization = 1;
478 $previousOrganization = $value;
481 $flagOrganization = 0;
482 $previousOrganization = $value;
486 if ($flagOrganization == 1) {
487 foreach ($row as $colName => $colVal) {
488 if (in_array($colName, $this->_noRepeats
)) {
489 unset($rows[$rowNum][$colName]);
497 // convert Organization display name to links
498 if (array_key_exists('civicrm_contact_organization_organization_name', $row) && !empty($rows[$rowNum]['civicrm_contact_organization_organization_name']) &&
499 array_key_exists('civicrm_contact_organization_id', $row)
501 $url = CRM_Utils_System
::url('civicrm/contact/view',
503 $rows[$rowNum]['civicrm_contact_organization_id'],
507 $rows[$rowNum]['civicrm_contact_organization_organization_name_link'] = $url;
508 $rows[$rowNum]['civicrm_contact_organization_organization_name_hover'] = ts('View contact summary for this organization.');
511 //remove duplicate Contact names and relationship type
512 if (array_key_exists('civicrm_contact_id', $row) && $this->_outputMode
!= 'csv') {
513 if ($value = $row['civicrm_contact_id']) {
515 $previousContact = $value;
518 if ($previousContact == $value) {
520 $previousContact = $value;
524 $previousContact = $value;
528 if ($flagContact == 1 && $flagOrganization == 1) {
529 $rows[$rowNum]['civicrm_contact_sort_name'] = "";
530 $rows[$rowNum]['civicrm_relationship_relationship_type_id'] = "";
537 if (array_key_exists('civicrm_contribution_contribution_status_id', $row)) {
538 if ($value = $row['civicrm_contribution_contribution_status_id']) {
539 $rows[$rowNum]['civicrm_contribution_contribution_status_id'] = CRM_Contribute_PseudoConstant
::contributionStatus($value);
543 // handle state province
544 if (array_key_exists('civicrm_address_state_province_id', $row)) {
545 if ($value = $row['civicrm_address_state_province_id']) {
546 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant
::stateProvinceAbbreviation($value, FALSE);
552 if (array_key_exists('civicrm_address_country_id', $row)) {
553 if ($value = $row['civicrm_address_country_id']) {
554 $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant
::country($value, FALSE);
559 if (!empty($row['civicrm_financial_trxn_card_type_id'])) {
560 $rows[$rowNum]['civicrm_financial_trxn_card_type_id'] = $this->getLabels($row['civicrm_financial_trxn_card_type_id'], 'CRM_Financial_DAO_FinancialTrxn', 'card_type_id');
564 // convert Individual display name to links
565 if (array_key_exists('civicrm_contact_sort_name', $row) &&
566 $rows[$rowNum]['civicrm_contact_sort_name'] &&
567 array_key_exists('civicrm_contact_id', $row)
569 $url = CRM_Report_Utils_Report
::getNextUrl('contribute/detail',
570 'reset=1&force=1&id_op=eq&id_value=' .
571 $row['civicrm_contact_id'],
572 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
574 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
575 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts('View contribution details for this individual');
580 // Contribution amount links to view contribution
581 if (($value = CRM_Utils_Array
::value('civicrm_contribution_total_amount', $row)) &&
582 CRM_Core_Permission
::check('access CiviContribute')
584 $url = CRM_Utils_System
::url('civicrm/contact/view/contribution',
585 "reset=1&id=" . $row['civicrm_contribution_id'] . "&cid=" . $row['civicrm_contact_id'] .
586 "&action=view&context=contribution&selectedChild=contribute",
589 $rows[$rowNum]['civicrm_contribution_total_amount_link'] = $url;
590 $rows[$rowNum]['civicrm_contribution_total_amount_hover'] = ts('View this contribution.');
594 // convert campaign_id to campaign title
595 if (array_key_exists('civicrm_contribution_campaign_id', $row)) {
596 if ($value = $row['civicrm_contribution_campaign_id']) {
597 $rows[$rowNum]['civicrm_contribution_campaign_id'] = $this->activeCampaigns
[$value];
602 // skip looking further in rows, if first row itself doesn't