jenkins-bot has submitted this change and it was merged.

Change subject: CRM-17751 add refund trxn to forms
......................................................................


CRM-17751 add refund trxn to forms

This has been submitted to core (4.7) in 
https://github.com/civicrm/civicrm-core/pull/7501

This commit adds the refund transaction id to the contribution edit form. There 
are some minor
tidyups that were also in the upstream PR.

Bug: T116317

Change-Id: I8f4ad17cb2d7f9e33d853bbfc13d36fe5bacd572
---
M CRM/Contribute/BAO/Contribution.php
M CRM/Contribute/Form/Contribution.php
M CRM/Contribute/Form/ContributionView.php
M CRM/Core/BAO/FinancialTrxn.php
M templates/CRM/Contribute/Form/Contribution.tpl
M templates/CRM/Contribute/Form/ContributionView.tpl
6 files changed, 104 insertions(+), 6 deletions(-)

Approvals:
  XenoRyet: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/CRM/Contribute/BAO/Contribution.php 
b/CRM/Contribute/BAO/Contribution.php
index 8d99534..3018c8d 100644
--- a/CRM/Contribute/BAO/Contribution.php
+++ b/CRM/Contribute/BAO/Contribution.php
@@ -262,6 +262,32 @@
   }
 
   /**
+   * Get the values and resolve the most common mappings.
+   *
+   * Since contribution status is resolved in almost every function that calls 
getValues it makes
+   * sense to have an extra function to resolve it rather than repeat the code.
+   *
+   * Think carefully before adding more mappings to be resolved as there could 
be performance implications
+   * if this function starts to be called from more iterative functions.
+   *
+   * @param array $params
+   *   Input parameters to find object.
+   *
+   * @return array
+   *   Array of the found contribution.
+   * @throws CRM_Core_Exception
+   */
+  public static function getValuesWithMappings($params) {
+    $values = $ids = array();
+    $contribution = self::getValues($params, $values, $ids);
+    if (is_null($contribution)) {
+      throw new CRM_Core_Exception('No contribution found');
+    }
+    $values['contribution_status'] = 
CRM_Core_PseudoConstant::getName('CRM_Contribute_BAO_Contribution', 
'contribution_status_id', $values['contribution_status_id']);
+    return $values;
+  }
+
+  /**
    * Calculate net_amount & fee_amount if they are not set.
    *
    * Net amount should be total - fee.
@@ -2845,6 +2871,7 @@
       $params['trxnParams'] = $trxnParams;
 
       if (!empty($params['prevContribution'])) {
+        $updated = FALSE;
         $params['trxnParams']['total_amount'] = $trxnParams['total_amount'] = 
$params['total_amount'] = $params['prevContribution']->total_amount;
         $params['trxnParams']['fee_amount'] = 
$params['prevContribution']->fee_amount;
         $params['trxnParams']['net_amount'] = 
$params['prevContribution']->net_amount;
@@ -2890,6 +2917,7 @@
             $params['total_amount'] = $params['trxnParams']['total_amount'] = 
$trxnParams['total_amount'];
             self::updateFinancialAccounts($params);
             $params['trxnParams']['to_financial_account_id'] = 
$trxnParams['to_financial_account_id'];
+            $updated = TRUE;
           }
         }
 
@@ -2906,6 +2934,7 @@
         ) {
           //Update Financial Records
           self::updateFinancialAccounts($params, 'changedStatus');
+          $updated = TRUE;
         }
 
         // change Payment Instrument for a Completed contribution
@@ -2927,6 +2956,7 @@
               self::updateFinancialAccounts($params, 
'changePaymentInstrument');
               $params['total_amount'] = $params['trxnParams']['total_amount'] 
= $trxnParams['total_amount'];
               self::updateFinancialAccounts($params, 
'changePaymentInstrument');
+              $updated = TRUE;
             }
           }
           elseif 
((!CRM_Utils_System::isNull($params['contribution']->payment_instrument_id) ||
@@ -2937,6 +2967,7 @@
             self::updateFinancialAccounts($params, 'changePaymentInstrument');
             $params['total_amount'] = $params['trxnParams']['total_amount'] = 
$trxnParams['total_amount'];
             self::updateFinancialAccounts($params, 'changePaymentInstrument');
+            $updated = TRUE;
           }
           elseif 
(!CRM_Utils_System::isNull($params['contribution']->check_number) &&
             $params['contribution']->check_number != 
$params['prevContribution']->check_number
@@ -2949,6 +2980,7 @@
             $params['trxnParams']['check_number'] = 
$params['contribution']->check_number;
             $params['total_amount'] = $params['trxnParams']['total_amount'] = 
$trxnParams['total_amount'];
             self::updateFinancialAccounts($params, 'changePaymentInstrument');
+            $updated = TRUE;
           }
         }
 
@@ -2963,6 +2995,22 @@
           //Update Financial Records
           $params['trxnParams']['from_financial_account_id'] = NULL;
           self::updateFinancialAccounts($params, 'changedAmount');
+          $updated = TRUE;
+        }
+
+        if (!$updated) {
+          // Looks like we might have a data correction update.
+          // This would be a case where a transaction id has been entered but 
it is incorrect &
+          // the person goes back in & fixes it, as opposed to a new 
transaction.
+          // Currently the UI doesn't support multiple refunds against a 
single transaction & we are only supporting
+          // the data fix scenario.
+          // CRM-17751.
+          if (isset($params['refund_trxn_id'])) {
+            $refundIDs = 
CRM_Core_BAO_FinancialTrxn::getRefundTransactionIDs($params['id']);
+            if ($refundIDs['trxn_id'] != $params['refund_trxn_id']) {
+              civicrm_api3('FinancialTrxn', 'create', array('id' => 
$refundIDs['financialTrxnId'], 'trxn_id' => $params['refund_trxn_id']));
+            }
+          }
         }
       }
 
diff --git a/CRM/Contribute/Form/Contribution.php 
b/CRM/Contribute/Form/Contribution.php
index 110950c..8e846d3 100644
--- a/CRM/Contribute/Form/Contribution.php
+++ b/CRM/Contribute/Form/Contribution.php
@@ -360,6 +360,7 @@
     CRM_Contribute_Form_SoftCredit::setDefaultValues($defaults, $this);
 
     if ($this->_mode) {
+      // @todo - remove this function as the parent does it too.
       $config = CRM_Core_Config::singleton();
       // Set default country from config if no country set.
       if (empty($defaults["billing_country_id-{$this->_bltID}"])) {
@@ -446,7 +447,12 @@
       $this->assign('is_pay_later', TRUE);
     }
     $this->assign('contribution_status_id', 
CRM_Utils_Array::value('contribution_status_id', $defaults));
-
+    if (CRM_Utils_Array::value('contribution_status_id', $defaults) == 
CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 
'contribution_status_id', 'Refunded')) {
+      $defaults['refund_trxn_id'] = 
CRM_Core_BAO_FinancialTrxn::getRefundTransactionTrxnID($this->_id);
+    }
+    else {
+      $defaults['refund_trxn_id'] = isset($defaults['trxn_id']) ? 
$defaults['trxn_id'] : NULL;
+    }
     $dates = array(
       'receive_date',
       'receipt_date',
@@ -764,6 +770,8 @@
     if ($buildRecurBlock) {
       $recurJs = array('onChange' => "buildRecurBlock( this.value ); return 
false;");
     }
+
+    $this->add('text', 'refund_trxn_id', ts('Transaction ID for the refund 
payment'));
     $element = $this->add('select',
       'payment_processor_id',
       ts('Payment Processor'),
@@ -1216,6 +1224,7 @@
         'cancel_reason',
         'source',
         'check_number',
+        'refund_trxn_id',
       );
       foreach ($fields as $f) {
         $params[$f] = CRM_Utils_Array::value($f, $formValues);
diff --git a/CRM/Contribute/Form/ContributionView.php 
b/CRM/Contribute/Form/ContributionView.php
index bc424d3..47f405c 100644
--- a/CRM/Contribute/Form/ContributionView.php
+++ b/CRM/Contribute/Form/ContributionView.php
@@ -46,13 +46,15 @@
    */
   public function preProcess() {
     $id = $this->get('id');
-    $values = $ids = array();
     $params = array('id' => $id);
     $context = CRM_Utils_Request::retrieve('context', 'String', $this);
     $this->assign('context', $context);
 
-    CRM_Contribute_BAO_Contribution::getValues($params, $values, $ids);
+    $values = CRM_Contribute_BAO_Contribution::getValuesWithMappings($params);
+
     CRM_Contribute_BAO_Contribution::resolveDefaults($values);
+    // @todo - I believe this cancelledStatus is unused - if someone reaches 
the same conclusion
+    // by grepping then the next few lines can go.
     $cancelledStatus = TRUE;
     $status = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
     if (CRM_Utils_Array::value('contribution_status_id', $values) == 
array_search('Cancelled', $status)) {
@@ -153,6 +155,9 @@
       $campaigns = CRM_Campaign_BAO_Campaign::getCampaigns($campaignId);
       $values['campaign'] = $campaigns[$campaignId];
     }
+    if ($values['contribution_status'] == 'Refunded') {
+      $this->assign('refund_trxn_id', 
CRM_Core_BAO_FinancialTrxn::getRefundTransactionTrxnID($id));
+    }
 
     // assign values to the template
     $this->assign($values);
diff --git a/CRM/Core/BAO/FinancialTrxn.php b/CRM/Core/BAO/FinancialTrxn.php
index f6cfc85..2375d4a 100644
--- a/CRM/Core/BAO/FinancialTrxn.php
+++ b/CRM/Core/BAO/FinancialTrxn.php
@@ -144,16 +144,18 @@
    * NOTE: This should be moved to separate BAO for EntityFinancialTrxn when 
we start adding more code for that object.
    *
    * @param $entity_id
-   *   Id of the entity usually the contactID.
+   *   Id of the entity usually the contributionID.
    * @param string $orderBy
    *   To get single trxn id for a entity table i.e last or first.
    * @param bool $newTrxn
+   * @param string $whereClause
+   *   Additional where parameters
    *
    * @return array
    *   array of category id's the contact belongs to.
    *
    */
-  public static function getFinancialTrxnId($entity_id, $orderBy = 'ASC', 
$newTrxn = FALSE) {
+  public static function getFinancialTrxnId($entity_id, $orderBy = 'ASC', 
$newTrxn = FALSE, $whereClause = '') {
     $ids = array('entityFinancialTrxnId' => NULL, 'financialTrxnId' => NULL);
 
     $condition = "";
@@ -165,7 +167,7 @@
       $orderBy = CRM_Utils_Type::escape($orderBy, 'String');
     }
 
-    $query = "SELECT ceft.id, ceft.financial_trxn_id FROM 
`civicrm_financial_trxn` cft
+    $query = "SELECT ceft.id, ceft.financial_trxn_id, cft.trxn_id FROM 
`civicrm_financial_trxn` cft
 LEFT JOIN civicrm_entity_financial_trxn ceft
 ON ceft.financial_trxn_id = cft.id AND ceft.entity_table = 
'civicrm_contribution'
 LEFT JOIN civicrm_entity_financial_trxn ceft1
@@ -173,6 +175,7 @@
 LEFT JOIN civicrm_financial_item cfi ON ceft1.entity_table = 
'civicrm_financial_item' and cfi.id = ceft1.entity_id
 WHERE ceft.entity_id = %1 AND (cfi.entity_table <> 'civicrm_financial_trxn' or 
cfi.entity_table is NULL)
 {$condition}
+{$whereClause}
 ORDER BY cft.id {$orderBy}
 LIMIT 1;";
 
@@ -181,11 +184,34 @@
     if ($dao->fetch()) {
       $ids['entityFinancialTrxnId'] = $dao->id;
       $ids['financialTrxnId'] = $dao->financial_trxn_id;
+      $ids['trxn_id'] = $dao->trxn_id;
     }
     return $ids;
   }
 
   /**
+   * Get the transaction id for the (latest) refund associated with a 
contribution.
+   *
+   * @param int $contributionID
+   * @return string
+   */
+  public static function getRefundTransactionTrxnID($contributionID) {
+    $ids = self::getRefundTransactionIDs($contributionID);
+    return isset($ids['trxn_id']) ? $ids['trxn_id'] : NULL;
+  }
+
+  /**
+   * Get the transaction id for the (latest) refund associated with a 
contribution.
+   *
+   * @param int $contributionID
+   * @return string
+   */
+  public static function getRefundTransactionIDs($contributionID) {
+    $refundStatusID = 
CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 
'contribution_status_id', 'Refunded');
+    return self::getFinancialTrxnId($contributionID, 'DESC', FALSE, " AND 
cft.status_id = $refundStatusID");
+  }
+
+  /**
    * Given an entity_id and entity_table, check for corresponding 
entity_financial_trxn and financial_trxn record.
    * @todo This should be moved to separate BAO for EntityFinancialTrxn when 
we start adding more code for that object.
    *
diff --git a/templates/CRM/Contribute/Form/Contribution.tpl 
b/templates/CRM/Contribute/Form/Contribution.tpl
index b74152b..7ee7bd8 100644
--- a/templates/CRM/Contribute/Form/Contribution.tpl
+++ b/templates/CRM/Contribute/Form/Contribution.tpl
@@ -211,6 +211,10 @@
               <td class="label" style="vertical-align: 
top;">{$form.cancel_reason.label}</td>
               <td>{$form.cancel_reason.html}</td>
             </tr>
+            <tr id="refundTrxnID">
+              <td class="label" style="vertical-align: 
top;">{$form.refund_trxn_id.label}</td>
+              <td>{$form.refund_trxn_id.html}</td>
+            </tr>
           </table>
         </fieldset>
         </td>
diff --git a/templates/CRM/Contribute/Form/ContributionView.tpl 
b/templates/CRM/Contribute/Form/ContributionView.tpl
index d220fc6..337d57b 100644
--- a/templates/CRM/Contribute/Form/ContributionView.tpl
+++ b/templates/CRM/Contribute/Form/ContributionView.tpl
@@ -147,6 +147,12 @@
         <td>{$cancel_reason}</td>
       </tr>
     {/if}
+    {if $refund_trxn_id}
+      <tr>
+        <td class="label">{ts}Refund Transaction ID{/ts}</td>
+        <td>{$refund_trxn_id}</td>
+      </tr>
+    {/if}
   {/if}
   <tr>
     <td class="label">{ts}Paid By{/ts}</td>

-- 
To view, visit https://gerrit.wikimedia.org/r/261601
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I8f4ad17cb2d7f9e33d853bbfc13d36fe5bacd572
Gerrit-PatchSet: 5
Gerrit-Project: wikimedia/fundraising/crm/civicrm
Gerrit-Branch: master
Gerrit-Owner: Eileen <[email protected]>
Gerrit-Reviewer: Awight <[email protected]>
Gerrit-Reviewer: Eileen <[email protected]>
Gerrit-Reviewer: Ejegg <[email protected]>
Gerrit-Reviewer: XenoRyet <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to