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],
173 'title' => ts('On Hold'),
174 'type' => CRM_Utils_Type
::T_INT
,
175 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
176 'options' => ['' => ts('Any')] + CRM_Core_PseudoConstant
::emailOnHoldOptions(),
179 'grouping' => 'contact-fields',
181 'civicrm_financial_trxn' => [
182 'dao' => 'CRM_Financial_DAO_FinancialTrxn',
185 'title' => ts('Credit Card Type'),
186 'dbAlias' => 'GROUP_CONCAT(financial_trxn_civireport.card_type_id SEPARATOR ",")',
191 'title' => ts('Credit Card Type'),
192 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
193 'options' => CRM_Financial_DAO_FinancialTrxn
::buildOptions('card_type_id'),
195 'type' => CRM_Utils_Type
::T_STRING
,
201 // If we have a campaign, build out the relevant elements
202 $this->addCampaignFields('civicrm_contribution');
204 $this->_currencyColumn
= 'civicrm_contribution_currency';
205 parent
::__construct();
208 public function preProcess() {
209 parent
::preProcess();
212 public function select() {
213 // @todo remove this in favour of using parent function
214 $this->_columnHeaders
= $select = [];
215 foreach ($this->_columns
as $tableName => $table) {
216 if (array_key_exists('fields', $table)) {
217 foreach ($table['fields'] as $fieldName => $field) {
218 if (!empty($field['required']) ||
!empty($this->_params
['fields'][$fieldName])) {
220 if (!empty($field['statistics'])) {
221 foreach ($field['statistics'] as $stat => $label) {
222 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}_{$stat}";
223 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
224 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['type'] = $field['type'];
225 $this->_statFields
[] = "{$tableName}_{$fieldName}_{$stat}";
229 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
231 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = $field['title'] ??
NULL;
232 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = $field['type'] ??
NULL;
238 $this->_selectClauses
= $select;
239 $this->_select
= "SELECT " . implode(', ', $select) . " ";
242 public function from() {
246 FROM civicrm_relationship {$this->_aliases['civicrm_relationship']}
247 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_contact_organization']} ON
248 ({$this->_aliases['civicrm_contact_organization']}.id = {$this->_aliases['civicrm_relationship']}.$this->orgContact AND {$this->_aliases['civicrm_contact_organization']}.contact_type='Organization')
249 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_contact']} ON
250 ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_relationship']}.$this->otherContact )
252 INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']} ON
253 ({$this->_aliases['civicrm_contribution']}.contact_id = {$this->_aliases['civicrm_relationship']}.$this->otherContact ) AND {$this->_aliases['civicrm_contribution']}.is_test = 0 AND {$this->_aliases['civicrm_contribution']}.is_template = 0 ";
255 $this->joinAddressFromContact();
256 $this->joinEmailFromContact();
258 // for credit card type
259 $this->addFinancialTrxnFromClause();
262 public function where() {
264 foreach ($this->_columns
as $tableName => $table) {
265 if (array_key_exists('filters', $table)) {
266 foreach ($table['filters'] as $fieldName => $field) {
268 if (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
) {
269 $relative = $this->_params
["{$fieldName}_relative"] ??
NULL;
270 $from = $this->_params
["{$fieldName}_from"] ??
NULL;
271 $to = $this->_params
["{$fieldName}_to"] ??
NULL;
273 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
276 $op = $this->_params
["{$fieldName}_op"] ??
NULL;
278 if ($fieldName == 'relationship_type_id') {
279 $clause = "{$this->_aliases['civicrm_relationship']}.relationship_type_id=" . $this->relationshipId
;
282 $clause = $this->whereClause($field,
284 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
285 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
286 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
292 if (!empty($clause)) {
293 $clauses[] = $clause;
299 if (empty($clauses)) {
300 $this->_where
= "WHERE ( 1 )";
303 $this->_where
= "WHERE " . implode(' AND ', $clauses);
306 if ($this->_aclWhere
) {
307 $this->_where
.= " AND {$this->_aclWhere} ";
311 public function groupBy() {
313 "{$this->_aliases['civicrm_relationship']}.$this->orgContact",
314 "{$this->_aliases['civicrm_relationship']}.$this->otherContact",
315 "{$this->_aliases['civicrm_contribution']}.id",
316 "{$this->_aliases['civicrm_relationship']}.relationship_type_id",
318 $this->_groupBy
= CRM_Contact_BAO_Query
::getGroupByFromSelectColumns($this->_selectClauses
, $groupBy);
321 public function orderBy() {
322 $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";
330 public function statistics(&$rows) {
331 $statistics = parent
::statistics($rows);
333 //hack filter display for relationship type
334 $type = substr($this->_params
['relationship_type_id_value'], -3);
335 foreach ($statistics['filters'] as $id => $value) {
336 if ($value['title'] == 'Relationship Type' && !empty($id)) {
337 $statistics['filters'][$id]['value'] = 'Is equal to ' .
338 $this->relationTypes
[$this->relationshipId
. '_' . $type];
344 public function postProcess() {
345 $this->beginPostProcess();
346 $this->buildACLClause([$this->_aliases
['civicrm_contact'], $this->_aliases
['civicrm_contact_organization']]);
347 $sql = $this->buildQuery(TRUE);
349 $this->buildRows($sql, $rows);
350 $this->formatDisplay($rows);
351 $this->doTemplateAssignment($rows);
352 $this->endPostProcess($rows);
356 * Set variables to be accessed by API and form layer in processing.
358 public function beginPostProcessCommon() {
359 $getRelationship = $this->_params
['relationship_type_id_value'];
360 $type = substr($getRelationship, -3);
361 $this->relationshipId
= intval((substr($getRelationship, 0, strpos($getRelationship, '_'))));
362 if ($type == 'b_a') {
363 $this->orgContact
= 'contact_id_b';
364 $this->otherContact
= 'contact_id_a';
367 $this->orgContact
= 'contact_id_a';
368 $this->otherContact
= 'contact_id_b';
372 public function validRelationships() {
373 $this->relationTypes
= $relationTypes = [];
375 $params = ['contact_type_b' => 'Organization', 'version' => 3];
376 $typesA = civicrm_api('relationship_type', 'get', $params);
378 if (empty($typesA['is_error'])) {
379 foreach ($typesA['values'] as $rel) {
380 $relationTypes[$rel['id']][$rel['id'] . '_b_a'] = $rel['label_b_a'];
384 $params = ['contact_type_a' => 'Organization', 'version' => 3];
385 $typesB = civicrm_api('relationship_type', 'get', $params);
387 if (empty($typesB['is_error'])) {
388 foreach ($typesB['values'] as $rel) {
389 $relationTypes[$rel['id']][$rel['id'] . '_a_b'] = $rel['label_a_b'];
393 ksort($relationTypes);
394 foreach ($relationTypes as $relationship) {
395 foreach ($relationship as $index => $label) {
396 $this->relationTypes
[$index] = $label;
402 * Alter display of rows.
404 * Iterate through the rows retrieved via SQL and make changes for display purposes,
405 * such as rendering contacts as links.
408 * Rows generated by SQL, with an array for each row.
410 public function alterDisplay(&$rows) {
411 $type = substr($this->_params
['relationship_type_id_value'], -3);
414 $flagOrganization = $flagContact = 0;
416 foreach ($rows as $rowNum => $row) {
418 //replace retionship id by relationship name
419 if (array_key_exists('civicrm_relationship_relationship_type_id', $row)) {
420 if ($value = $row['civicrm_relationship_relationship_type_id']) {
421 $rows[$rowNum]['civicrm_relationship_relationship_type_id'] = $this->relationTypes
[$value . '_' . $type];
426 //remove duplicate Organization names
427 if (array_key_exists('civicrm_contact_organization_id', $row) && $this->_outputMode
!= 'csv') {
428 if ($value = $row['civicrm_contact_organization_id']) {
430 $previousOrganization = $value;
433 if ($previousOrganization == $value) {
434 $flagOrganization = 1;
435 $previousOrganization = $value;
438 $flagOrganization = 0;
439 $previousOrganization = $value;
443 if ($flagOrganization == 1) {
444 foreach ($row as $colName => $colVal) {
445 if (in_array($colName, $this->_noRepeats
)) {
446 unset($rows[$rowNum][$colName]);
454 // convert Organization display name to links
455 if (array_key_exists('civicrm_contact_organization_organization_name', $row) && !empty($rows[$rowNum]['civicrm_contact_organization_organization_name']) &&
456 array_key_exists('civicrm_contact_organization_id', $row)
458 $url = CRM_Utils_System
::url('civicrm/contact/view',
460 $rows[$rowNum]['civicrm_contact_organization_id'],
464 $rows[$rowNum]['civicrm_contact_organization_organization_name_link'] = $url;
465 $rows[$rowNum]['civicrm_contact_organization_organization_name_hover'] = ts('View contact summary for this organization.');
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']) {
472 $previousContact = $value;
475 if ($previousContact == $value) {
477 $previousContact = $value;
481 $previousContact = $value;
485 if ($flagContact == 1 && $flagOrganization == 1) {
486 $rows[$rowNum]['civicrm_contact_sort_name'] = "";
487 $rows[$rowNum]['civicrm_relationship_relationship_type_id'] = "";
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, 'label');
500 if (!empty($row['civicrm_financial_trxn_card_type_id'])) {
501 $rows[$rowNum]['civicrm_financial_trxn_card_type_id'] = $this->getLabels($row['civicrm_financial_trxn_card_type_id'], 'CRM_Financial_DAO_FinancialTrxn', 'card_type_id');
505 // convert Individual display name to links
506 if (array_key_exists('civicrm_contact_sort_name', $row) &&
507 $rows[$rowNum]['civicrm_contact_sort_name'] &&
508 array_key_exists('civicrm_contact_id', $row)
510 $url = CRM_Report_Utils_Report
::getNextUrl('contribute/detail',
511 'reset=1&force=1&id_op=eq&id_value=' .
512 $row['civicrm_contact_id'],
513 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
515 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
516 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts('View contribution details for this individual');
521 // Contribution amount links to view contribution
522 if (($value = CRM_Utils_Array
::value('civicrm_contribution_total_amount', $row)) &&
523 CRM_Core_Permission
::check('access CiviContribute')
525 $url = CRM_Utils_System
::url('civicrm/contact/view/contribution',
526 "reset=1&id=" . $row['civicrm_contribution_id'] . "&cid=" . $row['civicrm_contact_id'] .
527 "&action=view&context=contribution&selectedChild=contribute",
530 $rows[$rowNum]['civicrm_contribution_total_amount_link'] = $url;
531 $rows[$rowNum]['civicrm_contribution_total_amount_hover'] = ts('View this contribution.');
535 // convert campaign_id to campaign title
536 if (array_key_exists('civicrm_contribution_campaign_id', $row)) {
537 if ($value = $row['civicrm_contribution_campaign_id']) {
538 $rows[$rowNum]['civicrm_contribution_campaign_id'] = $this->campaigns
[$value];
543 $entryFound = $this->alterDisplayAddressFields($row, $rows, $rowNum, NULL, NULL) ?
TRUE : $entryFound;
544 // skip looking further in rows, if first row itself doesn't