Fix credit note sequence issue
authorGuanhuan Chen <oooomic@gmail.com>
Tue, 29 Sep 2015 15:59:41 +0000 (16:59 +0100)
committermonishdeb <monish.deb@webaccessglobal.com>
Mon, 5 Oct 2015 20:31:02 +0000 (02:01 +0530)
CRM/Contribute/Form/Task/Invoice.php

index 074fdf3c3a48d031b21931d15dce35413aa4325c..1fb1874ebf82f34c8346e92e7a25eebd25fa66e6 100644 (file)
@@ -302,7 +302,16 @@ class CRM_Contribute_Form_Task_Invoice extends CRM_Contribute_Form_Task {
       }
 
       if ($contribution->contribution_status_id == $refundedStatusId || $contribution->contribution_status_id == $cancelledStatusId) {
-        $creditNoteId = CRM_Utils_Array::value('credit_notes_prefix', $prefixValue) . "" . $contribution->id;
+        if (is_null($contribution->creditnote_id)) {
+          $query = "select count(creditnote_id) as creditnote_number from civicrm_contribution";
+          $dao = CRM_Core_DAO::executeQuery($query);
+          $dao->fetch();
+          $creditNoteId = CRM_Utils_Array::value('credit_notes_prefix', $prefixValue) . "" . ($dao->creditnote_number + 1);
+          CRM_Core_DAO::setFieldValue('CRM_Contribute_DAO_Contribution', $contribution->id, 'creditnote_id', $creditNoteId);
+        }
+        else {
+          $creditNoteId = $contribution->creditnote_id;
+        }
       }
       $invoiceId = CRM_Utils_Array::value('invoice_prefix', $prefixValue) . "" . $contribution->id;
 
@@ -534,9 +543,6 @@ class CRM_Contribute_Form_Task_Invoice extends CRM_Contribute_Form_Task {
       }
 
       CRM_Core_DAO::setFieldValue('CRM_Contribute_DAO_Contribution', $contribution->id, 'invoice_id', $invoiceId);
-      if ($contribution->contribution_status_id == $refundedStatusId || $contribution->contribution_status_id == $cancelledStatusId) {
-        CRM_Core_DAO::setFieldValue('CRM_Contribute_DAO_Contribution', $contribution->id, 'creditnote_id', $creditNoteId);
-      }
       $invoiceTemplate->clearTemplateVars();
     }