3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 class CRM_Report_Form_Contribute_OrganizationSummary
extends CRM_Report_Form
{
19 public $_drilldownReport = ['contribute/detail' => 'Link to Detail Report'];
21 protected $_summary = NULL;
24 * Organisation contact ie. 'contact_id_b' or 'contact_id_a'
27 protected $orgContact;
30 * Related Contact ie. 'contact_id_b' or 'contact_id_a'
33 protected $otherContact;
38 public function __construct() {
39 self
::validRelationships();
42 'civicrm_contact_organization' => [
43 'dao' => 'CRM_Contact_DAO_Contact',
45 'organization_name' => [
46 'title' => ts('Organization Name'),
55 'title' => ts('Contact Type'),
57 'contact_sub_type' => [
58 'title' => ts('Contact Subtype'),
62 'organization_name' => [
63 'title' => ts('Organization Name'),
67 'type' => CRM_Utils_Type
::T_BOOLEAN
,
70 'grouping' => 'organization-fields',
72 'civicrm_line_item' => [
73 'dao' => 'CRM_Price_DAO_LineItem',
75 'civicrm_relationship' => [
76 'dao' => 'CRM_Contact_DAO_Relationship',
78 'relationship_type_id' => [
79 'title' => ts('Relationship Type'),
83 'relationship_type_id' => [
84 'title' => ts('Relationship Type'),
85 'type' => CRM_Utils_Type
::T_INT
,
86 'operatorType' => CRM_Report_Form
::OP_SELECT
,
87 'options' => $this->relationTypes
,
88 'default' => key($this->relationTypes
),
91 'grouping' => 'organization-fields',
93 'civicrm_contact' => [
94 'dao' => 'CRM_Contact_DAO_Contact',
97 'title' => ts('Contact Name'),
101 'no_display' => TRUE,
105 'grouping' => 'contact-fields',
107 'civicrm_contribution' => [
108 'dao' => 'CRM_Contribute_DAO_Contribution',
111 'title' => ts('Amount'),
115 'no_display' => TRUE,
118 'contribution_status_id' => [
119 'title' => ts('Contribution Status'),
123 'title' => ts('Check Number'),
127 'no_display' => TRUE,
130 'receive_date' => ['default' => TRUE],
131 'receipt_date' => NULL,
134 'receive_date' => ['operatorType' => CRM_Report_Form
::OP_DATE
],
135 'receipt_date' => ['operatorType' => CRM_Report_Form
::OP_DATE
],
136 'total_amount' => ['title' => ts('Amount Between')],
138 'title' => ts('Currency'),
139 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
140 'options' => CRM_Core_OptionGroup
::values('currencies_enabled'),
142 'type' => CRM_Utils_Type
::T_STRING
,
144 'contribution_status_id' => [
145 'title' => ts('Contribution Status'),
146 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
147 'options' => CRM_Contribute_BAO_Contribution
::buildOptions('contribution_status_id', 'search'),
151 'grouping' => 'contri-fields',
153 'civicrm_address' => [
154 'dao' => 'CRM_Core_DAO_Address',
156 'street_address' => NULL,
158 'postal_code' => NULL,
159 'state_province_id' => [
160 'title' => ts('State/Province'),
163 'title' => ts('Country'),
166 'grouping' => 'contact-fields',
169 'dao' => 'CRM_Core_DAO_Email',
170 'fields' => ['email' => NULL],
171 'grouping' => 'contact-fields',
173 'civicrm_financial_trxn' => [
174 'dao' => 'CRM_Financial_DAO_FinancialTrxn',
177 'title' => ts('Credit Card Type'),
178 'dbAlias' => 'GROUP_CONCAT(financial_trxn_civireport.card_type_id SEPARATOR ",")',
183 'title' => ts('Credit Card Type'),
184 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
185 'options' => CRM_Financial_DAO_FinancialTrxn
::buildOptions('card_type_id'),
187 'type' => CRM_Utils_Type
::T_STRING
,
193 // If we have a campaign, build out the relevant elements
194 $this->addCampaignFields('civicrm_contribution');
196 $this->_currencyColumn
= 'civicrm_contribution_currency';
197 parent
::__construct();
200 public function preProcess() {
201 parent
::preProcess();
204 public function select() {
205 // @todo remove this in favour of using parent function
206 $this->_columnHeaders
= $select = [];
207 foreach ($this->_columns
as $tableName => $table) {
208 if (array_key_exists('fields', $table)) {
209 foreach ($table['fields'] as $fieldName => $field) {
210 if (!empty($field['required']) ||
!empty($this->_params
['fields'][$fieldName])) {
212 if (!empty($field['statistics'])) {
213 foreach ($field['statistics'] as $stat => $label) {
214 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}_{$stat}";
215 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
216 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['type'] = $field['type'];
217 $this->_statFields
[] = "{$tableName}_{$fieldName}_{$stat}";
221 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
223 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = $field['title'] ??
NULL;
224 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = $field['type'] ??
NULL;
230 $this->_selectClauses
= $select;
231 $this->_select
= "SELECT " . implode(', ', $select) . " ";
234 public function from() {
238 FROM civicrm_relationship {$this->_aliases['civicrm_relationship']}
239 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_contact_organization']} ON
240 ({$this->_aliases['civicrm_contact_organization']}.id = {$this->_aliases['civicrm_relationship']}.$this->orgContact AND {$this->_aliases['civicrm_contact_organization']}.contact_type='Organization')
241 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_contact']} ON
242 ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_relationship']}.$this->otherContact )
244 INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']} ON
245 ({$this->_aliases['civicrm_contribution']}.contact_id = {$this->_aliases['civicrm_relationship']}.$this->otherContact ) AND {$this->_aliases['civicrm_contribution']}.is_test = 0 ";
247 $this->joinAddressFromContact();
248 $this->joinEmailFromContact();
250 // for credit card type
251 $this->addFinancialTrxnFromClause();
254 public function where() {
256 foreach ($this->_columns
as $tableName => $table) {
257 if (array_key_exists('filters', $table)) {
258 foreach ($table['filters'] as $fieldName => $field) {
260 if (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
) {
261 $relative = $this->_params
["{$fieldName}_relative"] ??
NULL;
262 $from = $this->_params
["{$fieldName}_from"] ??
NULL;
263 $to = $this->_params
["{$fieldName}_to"] ??
NULL;
265 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
268 $op = $this->_params
["{$fieldName}_op"] ??
NULL;
270 if ($fieldName == 'relationship_type_id') {
271 $clause = "{$this->_aliases['civicrm_relationship']}.relationship_type_id=" . $this->relationshipId
;
274 $clause = $this->whereClause($field,
276 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
277 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
278 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
284 if (!empty($clause)) {
285 $clauses[] = $clause;
291 if (empty($clauses)) {
292 $this->_where
= "WHERE ( 1 )";
295 $this->_where
= "WHERE " . implode(' AND ', $clauses);
298 if ($this->_aclWhere
) {
299 $this->_where
.= " AND {$this->_aclWhere} ";
303 public function groupBy() {
305 "{$this->_aliases['civicrm_relationship']}.$this->orgContact",
306 "{$this->_aliases['civicrm_relationship']}.$this->otherContact",
307 "{$this->_aliases['civicrm_contribution']}.id",
308 "{$this->_aliases['civicrm_relationship']}.relationship_type_id",
310 $this->_groupBy
= CRM_Contact_BAO_Query
::getGroupByFromSelectColumns($this->_selectClauses
, $groupBy);
313 public function orderBy() {
314 $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";
322 public function statistics(&$rows) {
323 $statistics = parent
::statistics($rows);
325 //hack filter display for relationship type
326 $type = substr($this->_params
['relationship_type_id_value'], -3);
327 foreach ($statistics['filters'] as $id => $value) {
328 if ($value['title'] == 'Relationship Type' && !empty($id)) {
329 $statistics['filters'][$id]['value'] = 'Is equal to ' .
330 $this->relationTypes
[$this->relationshipId
. '_' . $type];
336 public function postProcess() {
337 $this->beginPostProcess();
338 $this->buildACLClause([$this->_aliases
['civicrm_contact'], $this->_aliases
['civicrm_contact_organization']]);
339 $sql = $this->buildQuery(TRUE);
341 $this->buildRows($sql, $rows);
342 $this->formatDisplay($rows);
343 $this->doTemplateAssignment($rows);
344 $this->endPostProcess($rows);
348 * Set variables to be accessed by API and form layer in processing.
350 public function beginPostProcessCommon() {
351 $getRelationship = $this->_params
['relationship_type_id_value'];
352 $type = substr($getRelationship, -3);
353 $this->relationshipId
= intval((substr($getRelationship, 0, strpos($getRelationship, '_'))));
354 if ($type == 'b_a') {
355 $this->orgContact
= 'contact_id_b';
356 $this->otherContact
= 'contact_id_a';
359 $this->orgContact
= 'contact_id_a';
360 $this->otherContact
= 'contact_id_b';
364 public function validRelationships() {
365 $this->relationTypes
= $relationTypes = [];
367 $params = ['contact_type_b' => 'Organization', 'version' => 3];
368 $typesA = civicrm_api('relationship_type', 'get', $params);
370 if (empty($typesA['is_error'])) {
371 foreach ($typesA['values'] as $rel) {
372 $relationTypes[$rel['id']][$rel['id'] . '_b_a'] = $rel['label_b_a'];
376 $params = ['contact_type_a' => 'Organization', 'version' => 3];
377 $typesB = civicrm_api('relationship_type', 'get', $params);
379 if (empty($typesB['is_error'])) {
380 foreach ($typesB['values'] as $rel) {
381 $relationTypes[$rel['id']][$rel['id'] . '_a_b'] = $rel['label_a_b'];
385 ksort($relationTypes);
386 foreach ($relationTypes as $relationship) {
387 foreach ($relationship as $index => $label) {
388 $this->relationTypes
[$index] = $label;
394 * Alter display of rows.
396 * Iterate through the rows retrieved via SQL and make changes for display purposes,
397 * such as rendering contacts as links.
400 * Rows generated by SQL, with an array for each row.
402 public function alterDisplay(&$rows) {
403 $type = substr($this->_params
['relationship_type_id_value'], -3);
406 $flagOrganization = $flagContact = 0;
408 foreach ($rows as $rowNum => $row) {
410 //replace retionship id by relationship name
411 if (array_key_exists('civicrm_relationship_relationship_type_id', $row)) {
412 if ($value = $row['civicrm_relationship_relationship_type_id']) {
413 $rows[$rowNum]['civicrm_relationship_relationship_type_id'] = $this->relationTypes
[$value . '_' . $type];
418 //remove duplicate Organization names
419 if (array_key_exists('civicrm_contact_organization_id', $row) && $this->_outputMode
!= 'csv') {
420 if ($value = $row['civicrm_contact_organization_id']) {
422 $previousOrganization = $value;
425 if ($previousOrganization == $value) {
426 $flagOrganization = 1;
427 $previousOrganization = $value;
430 $flagOrganization = 0;
431 $previousOrganization = $value;
435 if ($flagOrganization == 1) {
436 foreach ($row as $colName => $colVal) {
437 if (in_array($colName, $this->_noRepeats
)) {
438 unset($rows[$rowNum][$colName]);
446 // convert Organization display name to links
447 if (array_key_exists('civicrm_contact_organization_organization_name', $row) && !empty($rows[$rowNum]['civicrm_contact_organization_organization_name']) &&
448 array_key_exists('civicrm_contact_organization_id', $row)
450 $url = CRM_Utils_System
::url('civicrm/contact/view',
452 $rows[$rowNum]['civicrm_contact_organization_id'],
456 $rows[$rowNum]['civicrm_contact_organization_organization_name_link'] = $url;
457 $rows[$rowNum]['civicrm_contact_organization_organization_name_hover'] = ts('View contact summary for this organization.');
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']) {
464 $previousContact = $value;
467 if ($previousContact == $value) {
469 $previousContact = $value;
473 $previousContact = $value;
477 if ($flagContact == 1 && $flagOrganization == 1) {
478 $rows[$rowNum]['civicrm_contact_sort_name'] = "";
479 $rows[$rowNum]['civicrm_relationship_relationship_type_id'] = "";
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, 'label');
492 if (!empty($row['civicrm_financial_trxn_card_type_id'])) {
493 $rows[$rowNum]['civicrm_financial_trxn_card_type_id'] = $this->getLabels($row['civicrm_financial_trxn_card_type_id'], 'CRM_Financial_DAO_FinancialTrxn', 'card_type_id');
497 // convert Individual display name to links
498 if (array_key_exists('civicrm_contact_sort_name', $row) &&
499 $rows[$rowNum]['civicrm_contact_sort_name'] &&
500 array_key_exists('civicrm_contact_id', $row)
502 $url = CRM_Report_Utils_Report
::getNextUrl('contribute/detail',
503 'reset=1&force=1&id_op=eq&id_value=' .
504 $row['civicrm_contact_id'],
505 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
507 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
508 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts('View contribution details for this individual');
513 // Contribution amount links to view contribution
514 if (($value = CRM_Utils_Array
::value('civicrm_contribution_total_amount', $row)) &&
515 CRM_Core_Permission
::check('access CiviContribute')
517 $url = CRM_Utils_System
::url('civicrm/contact/view/contribution',
518 "reset=1&id=" . $row['civicrm_contribution_id'] . "&cid=" . $row['civicrm_contact_id'] .
519 "&action=view&context=contribution&selectedChild=contribute",
522 $rows[$rowNum]['civicrm_contribution_total_amount_link'] = $url;
523 $rows[$rowNum]['civicrm_contribution_total_amount_hover'] = ts('View this contribution.');
527 // convert campaign_id to campaign title
528 if (array_key_exists('civicrm_contribution_campaign_id', $row)) {
529 if ($value = $row['civicrm_contribution_campaign_id']) {
530 $rows[$rowNum]['civicrm_contribution_campaign_id'] = $this->campaigns
[$value];
535 $entryFound = $this->alterDisplayAddressFields($row, $rows, $rowNum, NULL, NULL) ?
TRUE : $entryFound;
536 // skip looking further in rows, if first row itself doesn't