From 451ce63f7a15a7a85bd5e716a10e0651b8b66a2d Mon Sep 17 00:00:00 2001
From: Pradeep Nayak <pradpnayak@gmail.com>
Date: Thu, 11 Aug 2016 12:46:41 +0530
Subject: [PATCH] CRM-16189, Added add payment link on contribution search

----------------------------------------
* CRM-16189: Improve support for Accrual Method bookkeeping
  https://issues.civicrm.org/jira/browse/CRM-16189
---
 CRM/Contribute/Selector/Search.php | 26 ++++++++++++++++++++++++--
 CRM/Event/BAO/Query.php            | 11 +++++++++++
 2 files changed, 35 insertions(+), 2 deletions(-)

diff --git a/CRM/Contribute/Selector/Search.php b/CRM/Contribute/Selector/Search.php
index 91cec687be25..dbfb829bc364 100644
--- a/CRM/Contribute/Selector/Search.php
+++ b/CRM/Contribute/Selector/Search.php
@@ -82,6 +82,7 @@ class CRM_Contribute_Selector_Search extends CRM_Core_Selector_Base implements C
     'contribution_soft_credit_contact_id',
     'contribution_soft_credit_amount',
     'contribution_soft_credit_type',
+    'participant_contribution_id',
   );
 
   /**
@@ -183,11 +184,12 @@ public function __construct(
 
     // type of selector
     $this->_action = $action;
-
+    $returnProperties = CRM_Contribute_BAO_Query::selectorReturnProperties();
+    $returnProperties['participant_contribution_id'] = 1;
     $this->_includeSoftCredits = CRM_Contribute_BAO_Query::isSoftCreditOptionEnabled($this->_queryParams);
     $this->_query = new CRM_Contact_BAO_Query(
       $this->_queryParams,
-      CRM_Contribute_BAO_Query::selectorReturnProperties(),
+      $returnProperties,
       NULL, FALSE, FALSE,
       CRM_Contact_BAO_Query::MODE_CONTRIBUTE
     );
@@ -433,7 +435,27 @@ public function &getRows($action, $offset, $rowCount, $sort, $output = NULL) {
         'id' => $result->contribution_id,
         'cid' => $result->contact_id,
         'cxt' => $this->_context,
+        'pid' => $result->participant_id,
       );
+      if (!empty($row['participant_contribution_id'])) {
+        if ($row['contribution_status_name'] == 'Partially paid') {
+          $links[CRM_Core_Action::ADD] = array(
+            'name' => ts('Record Payment'),
+            'url' => 'civicrm/payment',
+            'qs' => 'reset=1&id=%%pid%%&cid=%%cid%%&action=add&component=event',
+            'title' => ts('Record Payment'),
+          );
+        }
+
+        if ($row['contribution_status_name'] == 'Pending refund') {
+          $links[CRM_Core_Action::ADD] = array(
+            'name' => ts('Record Refund'),
+            'url' => 'civicrm/payment',
+            'qs' => 'reset=1&id=%%pid%%&cid=%%cid%%&action=add&component=event',
+            'title' => ts('Record Refund'),
+          );
+        }
+      }
 
       $row['action'] = CRM_Core_Action::formLink(
         $links,
diff --git a/CRM/Event/BAO/Query.php b/CRM/Event/BAO/Query.php
index 7bce9747f77b..e4e3e257a14c 100644
--- a/CRM/Event/BAO/Query.php
+++ b/CRM/Event/BAO/Query.php
@@ -90,6 +90,13 @@ public static function select(&$query) {
         $query->_element['participant_fee_amount'] = 1;
       }
 
+      if (!empty($query->_returnProperties['participant_contribution_id'])) {
+        $query->_select['participant_contribution_id'] = "civicrm_participant_payment.participant_id as participant_contribution_id";
+        $query->_element['participant_contribution_id'] = 1;
+        $query->_tables['civicrm_contribution_participant_payment'] = 1;
+        $query->_whereTables['civicrm_contribution_participant_payment'] = 1;
+      }
+
       //add fee currency
       if (!empty($query->_returnProperties['participant_fee_currency'])) {
         $query->_select['participant_fee_currency'] = "civicrm_participant.fee_currency as participant_fee_currency";
@@ -479,6 +486,10 @@ public static function from($name, $mode, $side) {
         $from = " LEFT JOIN civicrm_participant ON civicrm_participant.contact_id = contact_a.id ";
         break;
 
+      case 'civicrm_contribution_participant_payment':
+        $from = " $side JOIN civicrm_participant_payment ON civicrm_contribution.id = civicrm_participant_payment.contribution_id ";
+        break;
+
       case 'civicrm_event':
         //CRM-17121
         $from = " LEFT JOIN civicrm_event ON civicrm_participant.event_id = civicrm_event.id ";