Add credit note id if a contribution is created with cancelled/refunded status
authorGuanhuan Chen <oooomic@gmail.com>
Thu, 1 Oct 2015 12:17:39 +0000 (13:17 +0100)
committerGuanhuan Chen <oooomic@gmail.com>
Thu, 1 Oct 2015 16:48:11 +0000 (17:48 +0100)
CRM/Contribute/BAO/Contribution.php
CRM/Contribute/Form/Task/Invoice.php

index f776098d0742cb559eebd3414a6416103b7b45f9..fb47a665c55a59aaaedbb68c0ee57e5546ffb114 100644 (file)
@@ -135,6 +135,19 @@ class CRM_Contribute_BAO_Contribution extends CRM_Contribute_DAO_Contribution {
       }
     }
 
+    //if contribution is created with cancelled or refunded status, add credit note id
+    if (!empty($params['contribution_status_id'])) {
+      $contributionStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
+
+      if (($params['contribution_status_id'] == array_search('Refunded', $contributionStatus)
+          || $params['contribution_status_id'] == array_search('Cancelled', $contributionStatus))
+      ) {
+        if (is_null($params['creditnote_id']) || $params['creditnote_id'] == "null") {
+          $params['creditnote_id'] = self::createCreditNoteId();
+        }
+      }
+    }
+
     //set defaults in create mode
     if (!$contributionID) {
       CRM_Core_DAO::setCreateDefaults($params, self::getDefaults());
@@ -312,6 +325,19 @@ class CRM_Contribute_BAO_Contribution extends CRM_Contribute_DAO_Contribution {
       }
     }
 
+    //if contribution is created with cancelled or refunded status, add credit note id
+    if (!empty($params['contribution_status_id'])) {
+      $contributionStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
+
+      if (($params['contribution_status_id'] == array_search('Refunded', $contributionStatus)
+          || $params['contribution_status_id'] == array_search('Cancelled', $contributionStatus))
+      ) {
+        if (is_null($params['creditnote_id']) || $params['creditnote_id'] == "null") {
+          $params['creditnote_id'] = self::createCreditNoteId();
+        }
+      }
+    }
+
     $transaction = new CRM_Core_Transaction();
 
     $contribution = self::add($params, $ids);
@@ -2996,7 +3022,8 @@ WHERE  contribution_id = %1 ";
       ) {
         $params['trxnParams']['total_amount'] = -$params['total_amount'];
         if (is_null($params['contribution']->creditnote_id) || $params['contribution']->creditnote_id == "null") {
-          self::createCreditNoteId($params['contribution']->id);
+          $creditNoteId = self::createCreditNoteId();
+          CRM_Core_DAO::setFieldValue('CRM_Contribute_DAO_Contribution', $params['contribution']->id, 'creditnote_id', $creditNoteId);
         }
       }
       elseif (($params['prevContribution']->contribution_status_id == array_search('Pending', $contributionStatus)
@@ -3010,7 +3037,8 @@ WHERE  contribution_id = %1 ";
           $params['trxnParams']['to_financial_account_id'] = $arAccountId;
           $params['trxnParams']['total_amount'] = -$params['total_amount'];
           if (is_null($params['contribution']->creditnote_id) || $params['contribution']->creditnote_id == "null") {
-            self::createCreditNoteId($params['contribution']->id);
+            $creditNoteId = self::createCreditNoteId();
+            CRM_Core_DAO::setFieldValue('CRM_Contribute_DAO_Contribution', $params['contribution']->id, 'creditnote_id', $creditNoteId);
           }
         }
         else {
@@ -3738,13 +3766,10 @@ WHERE con.id = {$contributionId}
   /**
    * Generate credit note id with next avaible number 
    *
-   * @param Integer $contributionId
-   *   Contribution Id.
-   *
    * @return string
    *   Credit Note Id.
    */
-  public static function createCreditNoteId($contributionId) {
+  public static function createCreditNoteId() {
     $prefixValue = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME, 'contribution_invoice_settings');
 
     $query = "select count(creditnote_id) as creditnote_number from civicrm_contribution";
@@ -3762,7 +3787,6 @@ WHERE con.id = {$contributionId}
       ));
     } while ($result > 0);
 
-    CRM_Core_DAO::setFieldValue('CRM_Contribute_DAO_Contribution', $contributionId, 'creditnote_id', $creditNoteId);
     return $creditNoteId;
   }
 
index 3bf9d91406afc90216adef51c63b19ed539c6c77..7c9b5dc658ee2e2a28582f76f96c810497c68f85 100644 (file)
@@ -313,7 +313,8 @@ class CRM_Contribute_Form_Task_Invoice extends CRM_Contribute_Form_Task {
 
       if ($contribution->contribution_status_id == $refundedStatusId || $contribution->contribution_status_id == $cancelledStatusId) {
         if (is_null($contribution->creditnote_id)) {
-          $creditNoteId = CRM_Contribute_BAO_Contribution::createCreditNoteId($contribution->id);
+          $creditNoteId = CRM_Contribute_BAO_Contribution::createCreditNoteId();
+          CRM_Core_DAO::setFieldValue('CRM_Contribute_DAO_Contribution', $contribution->id, 'creditnote_id', $creditNoteId);
         }
         else {
           $creditNoteId = $contribution->creditnote_id;