From 186c09ada4a277fb91038f604455a46ed5d669dc Mon Sep 17 00:00:00 2001 From: Guanhuan Chen Date: Thu, 1 Oct 2015 13:17:39 +0100 Subject: [PATCH] Add credit note id if a contribution is created with cancelled/refunded status --- CRM/Contribute/BAO/Contribution.php | 38 +++++++++++++++++++++++----- CRM/Contribute/Form/Task/Invoice.php | 3 ++- 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/CRM/Contribute/BAO/Contribution.php b/CRM/Contribute/BAO/Contribution.php index f776098d07..fb47a665c5 100644 --- a/CRM/Contribute/BAO/Contribution.php +++ b/CRM/Contribute/BAO/Contribution.php @@ -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; } diff --git a/CRM/Contribute/Form/Task/Invoice.php b/CRM/Contribute/Form/Task/Invoice.php index 3bf9d91406..7c9b5dc658 100644 --- a/CRM/Contribute/Form/Task/Invoice.php +++ b/CRM/Contribute/Form/Task/Invoice.php @@ -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; -- 2.25.1