From 4add5adbbc1e038c3168dfbe46758002584c7420 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 c8ee4df56b..f11cd5bbf2 100644 --- a/CRM/Contribute/BAO/Contribution.php +++ b/CRM/Contribute/BAO/Contribution.php @@ -133,6 +133,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()); @@ -389,6 +402,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); @@ -3192,7 +3218,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) @@ -3206,7 +3233,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 { @@ -4374,13 +4402,10 @@ LIMIT 1;"; /** * 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"; @@ -4398,7 +4423,6 @@ LIMIT 1;"; )); } 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 88c68fa11a..f9d1ad0e76 100644 --- a/CRM/Contribute/Form/Task/Invoice.php +++ b/CRM/Contribute/Form/Task/Invoice.php @@ -303,7 +303,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