CRM-19121 - add DT for soft credit listings
authorjitendrapurohit <jitendra.purohit@webaccessglobal.com>
Tue, 26 Jul 2016 10:51:34 +0000 (16:21 +0530)
committerjitendrapurohit <jitendra.purohit@webaccessglobal.com>
Wed, 24 Aug 2016 11:54:51 +0000 (17:24 +0530)
CRM/Contribute/BAO/ContributionSoft.php
CRM/Contribute/Page/AJAX.php [new file with mode: 0644]
CRM/Contribute/xml/Menu/Contribute.xml
templates/CRM/Contribute/Page/ContributionSoft.tpl
tests/phpunit/CRM/Contribute/Page/AjaxTest.php [new file with mode: 0644]

index b9f8044e8e0211cb67e84f4cb2c2c4b838fe6d1d..645934d3c09095df42ae75d5d33d9d1918d37b81 100644 (file)
@@ -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'] . '<br /> (test)';
diff --git a/CRM/Contribute/Page/AJAX.php b/CRM/Contribute/Page/AJAX.php
new file mode 100644 (file)
index 0000000..9f67096
--- /dev/null
@@ -0,0 +1,64 @@
+<?php
+/*
+ +--------------------------------------------------------------------+
+ | CiviCRM version 4.7                                                |
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC (c) 2004-2016                                |
+ +--------------------------------------------------------------------+
+ | This file is a part of CiviCRM.                                    |
+ |                                                                    |
+ | CiviCRM is free software; you can copy, modify, and distribute it  |
+ | under the terms of the GNU Affero General Public License           |
+ | Version 3, 19 November 2007 and the CiviCRM Licensing Exception.   |
+ |                                                                    |
+ | CiviCRM is distributed in the hope that it will be useful, but     |
+ | WITHOUT ANY WARRANTY; without even the implied warranty of         |
+ | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |
+ | See the GNU Affero General Public License for more details.        |
+ |                                                                    |
+ | You should have received a copy of the GNU Affero General Public   |
+ | License and the CiviCRM Licensing Exception along                  |
+ | with this program; if not, contact CiviCRM LLC                     |
+ | at info[AT]civicrm[DOT]org. If you have questions about the        |
+ | GNU Affero General Public License or the licensing of CiviCRM,     |
+ | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
+ +--------------------------------------------------------------------+
+ */
+
+/**
+ *
+ * @package CRM
+ * @copyright CiviCRM LLC (c) 2004-2016
+ *
+ */
+
+/**
+ * This class contains all the function that are called using AJAX (jQuery)
+ */
+class CRM_Contribute_Page_AJAX {
+  /**
+   * Get Soft credit to list in DT
+   */
+  public static function getSoftContributionRows() {
+    $requiredParameters = array(
+      'cid' => '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);
+  }
+
+}
index d1dc1cf134fd1670b2212f9b3a3276719bb994ce..c303bcf26f2806feb4a51fa4d4c69248a2faa72b 100644 (file)
     <weight>640</weight>
     <component>CiviContribute</component>
   </item>
+  <item>
+    <path>civicrm/ajax/softcontributionlist</path>
+    <page_callback>CRM_Contribute_Page_AJAX::getSoftContributionRows</page_callback>
+    <access_arguments>access CiviCRM</access_arguments>
+  </item>
 </menu>
index a8798b3156903c37df8a94a0972839dfdf5d086a..3aa7f21533e257865dd13fe8dd39eeaa12ee018c 100644 (file)
     </table>
     <p></p>
 {/if}
-
-<table class="selector row-highlight">
-    <tr class="columnheader">
-        <th scope="col">{ts}Contributor{/ts}</th>
-        <th scope="col">{ts}Amount{/ts}</th>
-        <th scope="col">{ts}Type{/ts}</th>
-        <th scope="col">{ts}Financial Type{/ts}</th>
-        <th scope="col" class="sorting_desc">{ts}Received{/ts}</th>
-        <th scope="col">{ts}Status{/ts}</th>
-        <th scope="col">{ts}Personal Campaign Page?{/ts}</th>
-        <th></th>
+<table class="crm-softcredit-selector crm-ajax-table">
+  <thead>
+    <tr>
+      <th data-data="contributor_name">{ts}Contributor{/ts}</th>
+      <th data-data="amount">{ts}Amount{/ts}</th>
+      <th data-data="sct_label">{ts}Type{/ts}</th>
+      <th data-data="financial_type">{ts}Financial Type{/ts}</th>
+      <th data-data="receive_date" class="sorting_desc">{ts}Received{/ts}</th>
+      <th data-data="contribution_status">{ts}Status{/ts}</th>
+      <th data-data="pcp_title">{ts}Personal Campaign Page?{/ts}</th>
+      <th data-data="links" data-orderable="false">&nbsp;</th>
     </tr>
-    {foreach from=$softCreditRows item=row}
-        <tr id='rowid{$row.id}' class="{cycle values="odd-row,even-row"}">
-            <td><a href="{crmURL p="civicrm/contact/view" q="reset=1&cid=`$row.contributor_id`"}" id="view_contact" title="{ts}View contributor contact record{/ts}">{$row.contributor_name}</a></td>
-            <td>{$row.amount|crmMoney:$row.currency}</td>
-            <td>{$row.sct_label}</td>
-            <td>{$row.financial_type}</td>
-            <td>{$row.receive_date|truncate:10:''|crmDate}</td>
-            <td>{$row.contribution_status}</td>
-            <td>{if $row.pcp_id}<a href="{crmURL p="civicrm/pcp/info" q="reset=1&id=`$row.pcp_id`"}" title="{ts}View Personal Campaign Page{/ts}">{$row.pcp_title}</a>{else}{ts}(n/a){/ts}{/if}</td>
-            <td><a href="{crmURL p="civicrm/contact/view/contribution" q="reset=1&id=`$row.contribution_id`&cid=`$contactId`&action=view&context=contribution&selectedChild=contribute"}" title="{ts}View related contribution{/ts}">{ts}View{/ts}</a></td>
-        </tr>
-    {/foreach}
+  </thead>
 </table>
 {/strip}
 {/if}
+
+{if !empty($membership_id) && $context eq 'membership'}
+  {assign var="entityID" value=$membership_id}
+{/if}
+
+{literal}
+<script type="text/javascript">
+  (function($) {
+    CRM.$('table.crm-softcredit-selector').data({
+      "ajax": {
+        "url": {/literal}'{crmURL p="civicrm/ajax/softcontributionlist" h=0 q="snippet=4&cid=`$contactId`&context=`$context`&entityID=`$entityID`&isTest=`$isTest`"}'{literal},
+      }
+    });
+  })(CRM.$);
+</script>
+{/literal}
diff --git a/tests/phpunit/CRM/Contribute/Page/AjaxTest.php b/tests/phpunit/CRM/Contribute/Page/AjaxTest.php
new file mode 100644 (file)
index 0000000..601c6a2
--- /dev/null
@@ -0,0 +1,160 @@
+<?php
+/*
+ +--------------------------------------------------------------------+
+ | CiviCRM version 4.7                                                |
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC (c) 2004-2016                                |
+ +--------------------------------------------------------------------+
+ | This file is a part of CiviCRM.                                    |
+ |                                                                    |
+ | CiviCRM is free software; you can copy, modify, and distribute it  |
+ | under the terms of the GNU Affero General Public License           |
+ | Version 3, 19 November 2007 and the CiviCRM Licensing Exception.   |
+ |                                                                    |
+ | CiviCRM is distributed in the hope that it will be useful, but     |
+ | WITHOUT ANY WARRANTY; without even the implied warranty of         |
+ | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |
+ | See the GNU Affero General Public License for more details.        |
+ |                                                                    |
+ | You should have received a copy of the GNU Affero General Public   |
+ | License and the CiviCRM Licensing Exception along                  |
+ | with this program; if not, contact CiviCRM LLC                     |
+ | at info[AT]civicrm[DOT]org. If you have questions about the        |
+ | GNU Affero General Public License or the licensing of CiviCRM,     |
+ | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
+ +--------------------------------------------------------------------+
+ */
+
+/**
+ * Class CRM_Contribute_Page_AjaxTest
+ * @group headless
+ */
+class CRM_Contribute_Page_AjaxTest extends CiviUnitTestCase {
+
+  protected $_params = array();
+
+  public function setUp() {
+    parent::setUp();
+
+    $this->_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']);
+  }
+
+}