From 36bf52ef53fd89fadecade64c6aea39b84e67aa6 Mon Sep 17 00:00:00 2001 From: Deepak Srivastava Date: Wed, 26 Feb 2014 14:14:10 +0530 Subject: [PATCH] CRM-12467 - find contribution - soft credit filters, search, injecting columns dynamically based on filter. ---------------------------------------- * CRM-12467: Include Soft Credits in contribution search http://issues.civicrm.org/jira/browse/CRM-12467 --- CRM/Contribute/BAO/Query.php | 72 +++++++++- CRM/Contribute/Selector/Search.php | 123 +++++++++++------- .../CRM/Contribute/Form/Search/Common.tpl | 10 ++ templates/CRM/Contribute/Form/Selector.tpl | 13 +- 4 files changed, 168 insertions(+), 50 deletions(-) diff --git a/CRM/Contribute/BAO/Query.php b/CRM/Contribute/BAO/Query.php index 8738de29fe..e0d5c9d2c9 100644 --- a/CRM/Contribute/BAO/Query.php +++ b/CRM/Contribute/BAO/Query.php @@ -148,6 +148,20 @@ class CRM_Contribute_BAO_Query { $query->_tables['civicrm_contribution_soft_contact'] = 1; } + if (!empty($query->_returnProperties['soft_credit_amount'])) { + $query->_select['contribution_soft_credit_amount'] = "civicrm_contribution_soft.amount as contribution_soft_credit_amount"; + $query->_element['contribution_soft_credit_amount'] = 1; + $query->_tables['civicrm_contribution'] = 1; + $query->_tables['civicrm_contribution_soft'] = 1; + } + + if (!empty($query->_returnProperties['soft_credit_type'])) { + $query->_select['contribution_soft_credit_type'] = "contribution_softcredit_type.label as contribution_soft_credit_type"; + $query->_element['contribution_soft_credit_type'] = 1; + $query->_tables['civicrm_contribution'] = 1; + $query->_tables['contribution_softcredit_type'] = 1; + } + if (!empty($query->_returnProperties['soft_credit_email'])) { $query->_select['contribution_soft_credit_email'] = "soft_email.email as contribution_soft_credit_email"; $query->_element['contribution_soft_credit_email'] = 1; @@ -329,6 +343,18 @@ class CRM_Contribute_BAO_Query { $query->_tables['civicrm_contribution_soft'] = $query->_whereTables['civicrm_contribution_soft'] = 1; return; + case 'contribution_or_softcredits': + if ($value == 'only_scredits') { + $query->_where[$grouping][] = "civicrm_contribution_soft.id IS NOT NULL"; + $query->_qill[$grouping][] = ts('Contributions OR Soft Credits? - Soft Credits Only'); + $query->_tables['civicrm_contribution_soft'] = $query->_whereTables['civicrm_contribution_soft'] = 1; + } else if ($value == 'both') { + $query->_qill[$grouping][] = ts('Contributions OR Soft Credits? - Both'); + $query->_tables['civicrm_contribution_soft'] = $query->_whereTables['civicrm_contribution_soft'] = 1; + } + // default option: $value == 'only_contribs' + return; + case 'contribution_payment_instrument_id': case 'contribution_payment_instrument': $pi = $value; @@ -623,6 +649,14 @@ class CRM_Contribute_BAO_Query { AND option_group_contribution_status.id = contribution_status.option_group_id ) "; break; + case 'contribution_softcredit_type': + $from = " $side JOIN civicrm_option_group option_group_contribution_softcredit_type ON + (option_group_contribution_softcredit_type.name = 'soft_credit_type')"; + $from .= " $side JOIN civicrm_option_value contribution_softcredit_type ON + ( civicrm_contribution_soft.soft_credit_type_id = contribution_softcredit_type.value + AND option_group_contribution_softcredit_type.id = contribution_softcredit_type.option_group_id )"; + break; + case 'contribution_note': $from .= " $side JOIN civicrm_note ON ( civicrm_note.entity_table = 'civicrm_contribution' AND civicrm_contribution.id = civicrm_note.entity_id )"; @@ -671,7 +705,21 @@ class CRM_Contribute_BAO_Query { return $from; } - static function defaultReturnProperties($mode, $includeCustomFields = TRUE) { + static function isIncludeSoftCredits($queryParams = array()) { + foreach (array_keys($queryParams) as $id) { + if (empty($queryParams[$id][0])) { + continue; + } + if ($queryParams[$id][0] == 'contribution_or_softcredits' && + ($queryParams[$id][2] == 'only_scredits' || + $queryParams[$id][2] == 'both')) { + return TRUE; + } + } + return FALSE; + } + + static function defaultReturnProperties($mode, $includeCustomFields = TRUE, $includeSoftCredits = FALSE) { $properties = NULL; if ($mode & CRM_Contact_BAO_Query::MODE_CONTRIBUTE) { $properties = array( @@ -710,9 +758,17 @@ class CRM_Contribute_BAO_Query { 'amount_level' => 1, 'contribution_note' => 1, 'contribution_batch' => 1, - 'contribution_campaign_id' => 1 + 'contribution_campaign_id' => 1, ); - + if ($includeSoftCredits) { + $properties = array_merge( + $properties, array( + 'soft_credit_name' => 1, + 'soft_credit_amount' => 1, + 'soft_credit_type' => 1, + ) + ); + } if ($includeCustomFields) { // also get all the custom contribution properties $fields = CRM_Core_BAO_CustomField::getFieldsForImport('Contribution'); @@ -824,6 +880,16 @@ class CRM_Contribute_BAO_Query { // Add field for pcp display in roll search $form->addYesNo('contribution_pcp_display_in_roll', ts('Personal Campaign Page Honor Roll?'), TRUE); + // Soft credit related fields + $options = array( + 'only_scredits' => ts('Soft Credits Only'), + 'only_contribs' => ts('Contributions Only'), + 'both' => ts('Both'), + ); + $form->add('select', 'contribution_or_softcredits', ts('Contributions OR Soft Credits?'), $options, FALSE, array('class' => "crm-select2")); + $form->addSelect('soft_credit_type_id', + array('entity' => 'contribution_soft', 'multiple' => 'multiple', 'option_url' => NULL, 'placeholder' => ts('- any -'))); + // Add all the custom searchable fields $contribution = array('Contribution'); $groupDetails = CRM_Core_BAO_CustomGroup::getGroupDetail(NULL, TRUE, $contribution); diff --git a/CRM/Contribute/Selector/Search.php b/CRM/Contribute/Selector/Search.php index 5836363533..096c155eec 100644 --- a/CRM/Contribute/Selector/Search.php +++ b/CRM/Contribute/Selector/Search.php @@ -83,6 +83,9 @@ class CRM_Contribute_Selector_Search extends CRM_Core_Selector_Base implements C 'membership_id', 'currency', 'contribution_campaign_id', + 'contribution_soft_credit_name', + 'contribution_soft_credit_amount', + 'contribution_soft_credit_type', ); /** @@ -148,6 +151,8 @@ class CRM_Contribute_Selector_Search extends CRM_Core_Selector_Base implements C */ protected $_query; + protected $_includeSoftCredits = FALSE; + /** * Class constructor * @@ -182,9 +187,13 @@ class CRM_Contribute_Selector_Search extends CRM_Core_Selector_Base implements C // type of selector $this->_action = $action; - $this->_query = new CRM_Contact_BAO_Query($this->_queryParams, - CRM_Contribute_BAO_Query::defaultReturnProperties(CRM_Contact_BAO_Query::MODE_CONTRIBUTE, - FALSE + $this->_includeSoftCredits = CRM_Contribute_BAO_Query::isIncludeSoftCredits($this->_queryParams); + $this->_query = new CRM_Contact_BAO_Query( + $this->_queryParams, + CRM_Contribute_BAO_Query::defaultReturnProperties( + CRM_Contact_BAO_Query::MODE_CONTRIBUTE, + FALSE, + $this->_includeSoftCredits ), NULL, FALSE, FALSE, CRM_Contact_BAO_Query::MODE_CONTRIBUTE @@ -424,57 +433,81 @@ class CRM_Contribute_Selector_Search extends CRM_Core_Selector_Base implements C * @access public */ public function &getColumnHeaders($action = NULL, $output = NULL) { - if (!isset(self::$_columnHeaders)) { - self::$_columnHeaders = array( - array( - 'name' => ts('Amount'), - 'sort' => 'total_amount', - 'direction' => CRM_Utils_Sort::DONTCARE, - ), - array('name' => ts('Type'), - 'sort' => 'financial_type_id', - 'direction' => CRM_Utils_Sort::DONTCARE, - ), - array( - 'name' => ts('Source'), - 'sort' => 'contribution_source', - 'direction' => CRM_Utils_Sort::DONTCARE, - ), - array( - 'name' => ts('Received'), - 'sort' => 'receive_date', - 'direction' => CRM_Utils_Sort::DESCENDING, - ), - array( - 'name' => ts('Thank-you Sent'), - 'sort' => 'thankyou_date', - 'direction' => CRM_Utils_Sort::DONTCARE, - ), - array( - 'name' => ts('Status'), - 'sort' => 'contribution_status_id', - 'direction' => CRM_Utils_Sort::DONTCARE, - ), + self::$_columnHeaders = array( + array( + 'name' => ts('Amount'), + 'sort' => 'total_amount', + 'direction' => CRM_Utils_Sort::DONTCARE, + ), + array( + 'name' => ts('Type'), + 'sort' => 'financial_type_id', + 'direction' => CRM_Utils_Sort::DONTCARE, + ), + array( + 'name' => ts('Source'), + 'sort' => 'contribution_source', + 'direction' => CRM_Utils_Sort::DONTCARE, + ), + array( + 'name' => ts('Received'), + 'sort' => 'receive_date', + 'direction' => CRM_Utils_Sort::DESCENDING, + ), + array( + 'name' => ts('Thank-you Sent'), + 'sort' => 'thankyou_date', + 'direction' => CRM_Utils_Sort::DONTCARE, + ), + array( + 'name' => ts('Status'), + 'sort' => 'contribution_status_id', + 'direction' => CRM_Utils_Sort::DONTCARE, + ), + array( + 'name' => ts('Premium'), + 'sort' => 'product_name', + 'direction' => CRM_Utils_Sort::DONTCARE, + ), + ); + + if (!$this->_single) { + $pre = array( + array('desc' => ts('Contact Type')), array( - 'name' => ts('Premium'), - 'sort' => 'product_name', + 'name' => ts('Name'), + 'sort' => 'sort_name', 'direction' => CRM_Utils_Sort::DONTCARE, ), - array('desc' => ts('Actions')), ); - - if (!$this->_single) { - $pre = array( - array('desc' => ts('Contact Type')), + self::$_columnHeaders = array_merge($pre, self::$_columnHeaders); + } + if ($this->_includeSoftCredits) { + $softCreditColumns = + array( array( - 'name' => ts('Name'), - 'sort' => 'sort_name', + 'name' => ts('Soft Credit For'), + 'sort' => 'contribution_soft_credit_name', + 'direction' => CRM_Utils_Sort::DONTCARE, + ), + array( + 'name' => ts('Soft Credit Amount'), + 'sort' => 'contribution_soft_credit_amount', + 'direction' => CRM_Utils_Sort::DONTCARE, + ), + array( + 'name' => ts('Soft Credit Type'), + 'sort' => 'contribution_soft_credit_type', 'direction' => CRM_Utils_Sort::DONTCARE, ), ); - self::$_columnHeaders = array_merge($pre, self::$_columnHeaders); - } + self::$_columnHeaders = array_merge(self::$_columnHeaders, $softCreditColumns); } + self::$_columnHeaders = array_merge( + self::$_columnHeaders, array( + array('desc' => ts('Actions')) + ) + ); return self::$_columnHeaders; } diff --git a/templates/CRM/Contribute/Form/Search/Common.tpl b/templates/CRM/Contribute/Form/Search/Common.tpl index 2bee33855d..18a211ef17 100644 --- a/templates/CRM/Contribute/Form/Search/Common.tpl +++ b/templates/CRM/Contribute/Form/Search/Common.tpl @@ -127,6 +127,16 @@ {$form.contribution_pcp_display_in_roll.html} + + + {$form.contribution_or_softcredits.label}
+ {$form.contribution_or_softcredits.html} + + + {$form.soft_credit_type_id.label}
+ {$form.soft_credit_type_id.html|crmAddClass:twenty} + + diff --git a/templates/CRM/Contribute/Form/Selector.tpl b/templates/CRM/Contribute/Form/Selector.tpl index 6074f84d5d..21ac56abb3 100644 --- a/templates/CRM/Contribute/Form/Selector.tpl +++ b/templates/CRM/Contribute/Form/Selector.tpl @@ -34,6 +34,7 @@ {if !$single and $context eq 'Search' } {$form.toggleSelect.html} {/if} + {assign var="softCreditColumns" value=0} {foreach from=$columnHeaders item=header} {if $header.sort} @@ -41,11 +42,14 @@ {$sort->_response.$key.link} {else} {$header.name} - {/if} + {/if} + {if $header.name eq "Soft Credit For"} + {assign var='softCreditColumns' value=1} + {/if} {/foreach} - + {counter start=0 skip=1 print=false} {foreach from=$rows item=row} @@ -74,6 +78,11 @@ {/if} {$row.product_name} + {if $softCreditColumns} + {$row.contribution_soft_credit_name} + {$row.contribution_soft_credit_amount|crmMoney:$row.currency} + {$row.contribution_soft_credit_type} + {/if} {$row.action|replace:'xx':$row.contribution_id} {/foreach} -- 2.25.1