From dfe9afbf299d72602c2daa512f1f5879894cee4a Mon Sep 17 00:00:00 2001 From: jitendrapurohit Date: Tue, 26 Jul 2016 16:21:34 +0530 Subject: [PATCH] CRM-19121 - add DT for soft credit listings --- CRM/Contribute/BAO/ContributionSoft.php | 96 +++++++++-- CRM/Contribute/Page/AJAX.php | 64 +++++++ CRM/Contribute/xml/Menu/Contribute.xml | 5 + .../CRM/Contribute/Page/ContributionSoft.tpl | 51 +++--- .../phpunit/CRM/Contribute/Page/AjaxTest.php | 160 ++++++++++++++++++ 5 files changed, 341 insertions(+), 35 deletions(-) create mode 100644 CRM/Contribute/Page/AJAX.php create mode 100644 tests/phpunit/CRM/Contribute/Page/AjaxTest.php diff --git a/CRM/Contribute/BAO/ContributionSoft.php b/CRM/Contribute/BAO/ContributionSoft.php index b9f8044e8e..645934d3c0 100644 --- a/CRM/Contribute/BAO/ContributionSoft.php +++ b/CRM/Contribute/BAO/ContributionSoft.php @@ -386,6 +386,41 @@ class CRM_Contribute_BAO_ContributionSoft extends CRM_Contribute_DAO_Contributio return $id; } + /** + * Wrapper for ajax soft contribution selector. + * + * @param array $params + * Associated array for params. + * + * @return array + * Associated array of soft contributions + */ + public static function getSoftContributionSelector($params) { + $isTest = 0; + if (!empty($params['isTest'])) { + $isTest = $params['isTest']; + } + // Format the params. + $params['offset'] = ($params['page'] - 1) * $params['rp']; + $params['rowCount'] = $params['rp']; + $params['sort'] = CRM_Utils_Array::value('sortBy', $params); + $contactId = $params['cid']; + + $filter = NULL; + if ($params['context'] == 'membership' && !empty($params['entityID']) && $contactId) { + $filter = " AND cc.id IN (SELECT contribution_id FROM civicrm_membership_payment WHERE membership_id = {$params['entityID']})"; + } + + $softCreditList = self::getSoftContributionList($contactId, $filter, $isTest, $params); + + $softCreditListDT = array(); + $softCreditListDT['data'] = array_values($softCreditList); + $softCreditListDT['recordsTotal'] = $params['total']; + $softCreditListDT['recordsFiltered'] = $params['total']; + + return $softCreditListDT; + } + /** * Function to retrieve the list of soft contributions for given contact. * @@ -394,25 +429,48 @@ class CRM_Contribute_BAO_ContributionSoft extends CRM_Contribute_DAO_Contributio * @param string $filter * @param int $isTest * Additional filter criteria, later used in where clause. + * @param array $dTParams * * @return array */ - public static function getSoftContributionList($contact_id, $filter = NULL, $isTest = 0) { + public static function getSoftContributionList($contact_id, $filter = NULL, $isTest = 0, &$dTParams = NULL) { + $config = CRM_Core_Config::singleton(); + $links = array( + CRM_Core_Action::VIEW => array( + 'name' => ts('View'), + 'url' => 'civicrm/contact/view/contribution', + 'qs' => 'reset=1&id=%%contributionid%%&cid=%%contactId%%&action=view&context=contribution&selectedChild=contribute', + 'title' => ts('View related contribution'), + ), + ); + $orderBy = 'cc.receive_date DESC'; + if (!empty($dTParams['sort'])) { + $orderBy = $dTParams['sort']; + } + $limit = ''; + if (!empty($dTParams['rowCount']) && $dTParams['rowCount'] > 0) { + $limit = " LIMIT {$dTParams['offset']}, {$dTParams['rowCount']} "; + } + $softOgId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', 'soft_credit_type', 'id', 'name'); + $statusOgId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', 'contribution_status', 'id', 'name'); + $query = ' - SELECT ccs.id, ccs.amount as amount, + SELECT SQL_CALC_FOUND_ROWS ccs.id, ccs.amount as amount, ccs.contribution_id, ccs.pcp_id, ccs.pcp_display_in_roll, ccs.pcp_roll_nickname, ccs.pcp_personal_note, ccs.soft_credit_type_id, + sov.label as sct_label, cc.receive_date, cc.contact_id as contributor_id, cc.contribution_status_id as contribution_status_id, + cov.label as contribution_status, cp.title as pcp_title, cc.currency, - contact.display_name, - cct.name as contributionType + contact.display_name as contributor_name, + cct.name as financial_type FROM civicrm_contribution_soft ccs LEFT JOIN civicrm_contribution cc ON ccs.contribution_id = cc.id @@ -421,6 +479,8 @@ class CRM_Contribute_BAO_ContributionSoft extends CRM_Contribute_DAO_Contributio LEFT JOIN civicrm_contact contact ON ccs.contribution_id = cc.id AND cc.contact_id = contact.id LEFT JOIN civicrm_financial_type cct ON cc.financial_type_id = cct.id + LEFT JOIN civicrm_option_value sov ON sov.option_group_id = %3 AND ccs.soft_credit_type_id = sov.value + LEFT JOIN civicrm_option_value cov ON cov.option_group_id = %4 AND cc.contribution_status_id = cov.value '; $where = " @@ -429,30 +489,42 @@ class CRM_Contribute_BAO_ContributionSoft extends CRM_Contribute_DAO_Contributio $where .= $filter; } - $query .= "{$where} ORDER BY cc.receive_date DESC"; + $query .= "{$where} ORDER BY {$orderBy} {$limit}"; $params = array( 1 => array($contact_id, 'Integer'), 2 => array($isTest, 'Integer'), + 3 => array($softOgId, 'Integer'), + 4 => array($statusOgId, 'Integer'), ); $cs = CRM_Core_DAO::executeQuery($query, $params); - $contributionStatus = CRM_Contribute_PseudoConstant::contributionStatus(); + + $dTParams['total'] = CRM_Core_DAO::singleValueQuery('SELECT FOUND_ROWS()'); $result = array(); while ($cs->fetch()) { $result[$cs->id]['amount'] = $cs->amount; $result[$cs->id]['currency'] = $cs->currency; $result[$cs->id]['contributor_id'] = $cs->contributor_id; $result[$cs->id]['contribution_id'] = $cs->contribution_id; - $result[$cs->id]['contributor_name'] = $cs->display_name; - $result[$cs->id]['financial_type'] = $cs->contributionType; - $result[$cs->id]['receive_date'] = $cs->receive_date; + $result[$cs->id]['contributor_name'] = CRM_Utils_System::href( + $cs->contributor_name, + 'civicrm/contact/view', + "reset=1&cid={$cs->contributor_id}" + ); + $result[$cs->id]['financial_type'] = $cs->financial_type; + $result[$cs->id]['receive_date'] = CRM_Utils_Date::customFormat($cs->receive_date, $config->dateformatDatetime); $result[$cs->id]['pcp_id'] = $cs->pcp_id; - $result[$cs->id]['pcp_title'] = $cs->pcp_title; + $result[$cs->id]['pcp_title'] = ($cs->pcp_title) ? $cs->pcp_title : 'n/a'; $result[$cs->id]['pcp_display_in_roll'] = $cs->pcp_display_in_roll; $result[$cs->id]['pcp_roll_nickname'] = $cs->pcp_roll_nickname; $result[$cs->id]['pcp_personal_note'] = $cs->pcp_personal_note; - $result[$cs->id]['contribution_status'] = CRM_Utils_Array::value($cs->contribution_status_id, $contributionStatus); - $result[$cs->id]['sct_label'] = CRM_Core_PseudoConstant::getLabel('CRM_Contribute_BAO_ContributionSoft', 'soft_credit_type_id', $cs->soft_credit_type_id); + $result[$cs->id]['contribution_status'] = $cs->contribution_status; + $result[$cs->id]['sct_label'] = $cs->sct_label; + $replace = array( + 'contributionid' => $cs->contribution_id, + 'contactId' => $cs->contributor_id, + ); + $result[$cs->id]['links'] = CRM_Core_Action::formLink($links, NULL, $replace); if ($isTest) { $result[$cs->id]['contribution_status'] = $result[$cs->id]['contribution_status'] . '
(test)'; diff --git a/CRM/Contribute/Page/AJAX.php b/CRM/Contribute/Page/AJAX.php new file mode 100644 index 0000000000..9f670969a8 --- /dev/null +++ b/CRM/Contribute/Page/AJAX.php @@ -0,0 +1,64 @@ + 'Integer', + 'context' => 'String', + ); + $optionalParameters = array( + 'entityID' => 'Integer', + 'isTest' => 'Integer', + ); + + $params = CRM_Core_Page_AJAX::defaultSortAndPagerParams(); + $params += CRM_Core_Page_AJAX::validateParams($requiredParameters, $optionalParameters); + + $softCreditList = CRM_Contribute_BAO_ContributionSoft::getSoftContributionSelector($params); + + if (!empty($_GET['is_unit_test'])) { + return $softCreditList; + } + + CRM_Utils_JSON::output($softCreditList); + } + +} diff --git a/CRM/Contribute/xml/Menu/Contribute.xml b/CRM/Contribute/xml/Menu/Contribute.xml index d1dc1cf134..c303bcf26f 100644 --- a/CRM/Contribute/xml/Menu/Contribute.xml +++ b/CRM/Contribute/xml/Menu/Contribute.xml @@ -327,4 +327,9 @@ 640 CiviContribute + + civicrm/ajax/softcontributionlist + CRM_Contribute_Page_AJAX::getSoftContributionRows + access CiviCRM + diff --git a/templates/CRM/Contribute/Page/ContributionSoft.tpl b/templates/CRM/Contribute/Page/ContributionSoft.tpl index a8798b3156..3aa7f21533 100644 --- a/templates/CRM/Contribute/Page/ContributionSoft.tpl +++ b/templates/CRM/Contribute/Page/ContributionSoft.tpl @@ -40,30 +40,35 @@

{/if} - - - - - - - - - - - +
{ts}Contributor{/ts}{ts}Amount{/ts}{ts}Type{/ts}{ts}Financial Type{/ts}{ts}Received{/ts}{ts}Status{/ts}{ts}Personal Campaign Page?{/ts}
+ + + + + + + + + + - {foreach from=$softCreditRows item=row} - - - - - - - - - - - {/foreach} +
{ts}Contributor{/ts}{ts}Amount{/ts}{ts}Type{/ts}{ts}Financial Type{/ts}{ts}Received{/ts}{ts}Status{/ts}{ts}Personal Campaign Page?{/ts} 
{$row.contributor_name}{$row.amount|crmMoney:$row.currency}{$row.sct_label}{$row.financial_type}{$row.receive_date|truncate:10:''|crmDate}{$row.contribution_status}{if $row.pcp_id}{$row.pcp_title}{else}{ts}(n/a){/ts}{/if}{ts}View{/ts}
{/strip} {/if} + +{if !empty($membership_id) && $context eq 'membership'} + {assign var="entityID" value=$membership_id} +{/if} + +{literal} + +{/literal} diff --git a/tests/phpunit/CRM/Contribute/Page/AjaxTest.php b/tests/phpunit/CRM/Contribute/Page/AjaxTest.php new file mode 100644 index 0000000000..601c6a2484 --- /dev/null +++ b/tests/phpunit/CRM/Contribute/Page/AjaxTest.php @@ -0,0 +1,160 @@ +_fields = array('amount', 'sct_label'); + + $this->_params = array( + 'page' => 1, + 'rp' => 50, + 'offset' => 0, + 'rowCount' => 50, + 'sort' => NULL, + 'is_unit_test' => TRUE, + ); + $softContactParams = array( + 'first_name' => 'soft', + 'last_name' => 'Contact', + ); + $this->_softContactId = $this->individualCreate($softContactParams); + + //create three sample contacts + foreach (array(0, 1, 2) as $seq) { + $this->_primaryContacts[] = $this->individualCreate(array(), $seq); + } + } + + /** + * Test retrieve Soft Contribution through AJAX + */ + public function testGetSoftContributionSelector() { + $softTypes = array(3, 2, 5); + $amounts = array('100', '600', '150'); + + // create sample soft contribution for contact + foreach ($this->_primaryContacts as $seq => $contactId) { + $this->callAPISuccess('Contribution', 'create', array( + 'contact_id' => $contactId, + 'receive_date' => date('Ymd'), + 'total_amount' => $amounts[$seq], + 'financial_type_id' => 1, + 'non_deductible_amount' => '10', + 'contribution_status_id' => 1, + 'soft_credit' => array( + '1' => array( + 'contact_id' => $this->_softContactId, + 'amount' => $amounts[$seq], + 'soft_credit_type_id' => $softTypes[$seq], + ), + ), + )); + } + + $_GET = array_merge($this->_params, + array( + 'cid' => $this->_softContactId, + 'context' => 'contribution', + ) + ); + $softCreditList = CRM_Contribute_Page_AJAX::getSoftContributionRows(); + + foreach ($this->_fields as $columnName) { + $_GET['columns'][] = array( + 'data' => $columnName, + ); + } + // get the results in descending order + $_GET['order'] = array( + '0' => array( + 'column' => 0, + 'dir' => 'desc', + ), + ); + $amountSortedList = CRM_Contribute_Page_AJAX::getSoftContributionRows(); + + $this->assertEquals(3, $softCreditList['recordsTotal']); + $this->assertEquals(3, $amountSortedList['recordsTotal']); + rsort($amounts); + foreach ($amounts as $key => $amount) { + $this->assertEquals($amount, $amountSortedList['data'][$key]['amount']); + } + + // sort with soft credit types + $_GET['order'][0]['column'] = 1; + foreach ($softTypes as $id) { + $softLabels[] = CRM_Core_PseudoConstant::getLabel('CRM_Contribute_BAO_ContributionSoft', 'soft_credit_type_id', $id); + } + rsort($softLabels); + $softTypeSortedList = CRM_Contribute_Page_AJAX::getSoftContributionRows(); + foreach ($softLabels as $key => $labels) { + $this->assertEquals($labels, $softTypeSortedList['data'][$key]['sct_label']); + } + } + + /** + * Test retrieve Soft Contribution For Membership + */ + public function testGetSoftContributionForMembership() { + //Check soft credit for membership + $memParams = array( + 'contribution_contact_id' => $this->_primaryContacts[0], + 'contact_id' => $this->_softContactId, + 'contribution_status_id' => 1, + 'financial_type_id' => 2, + 'status_id' => 1, + 'total_amount' => 100, + 'soft_credit' => array( + 'soft_credit_type_id' => 11, + 'contact_id' => $this->_softContactId, + ), + ); + $_GET = array_merge($this->_params, + array( + 'cid' => $this->_softContactId, + 'context' => 'membership', + 'entityID' => $this->contactMembershipCreate($memParams), + ) + ); + + $softCreditList = CRM_Contribute_Page_AJAX::getSoftContributionRows(); + $this->assertEquals(1, $softCreditList['recordsTotal']); + $this->assertEquals('Gift', $softCreditList['data'][0]['sct_label']); + $this->assertEquals('100.00', $softCreditList['data'][0]['amount']); + $this->assertEquals('Member Dues', $softCreditList['data'][0]['financial_type']); + } + +} -- 2.25.1