From 1844808f5fffae828954677d7417112c3d5dd0a7 Mon Sep 17 00:00:00 2001 From: Guanhuan Chen Date: Thu, 1 Oct 2015 12:46:28 +0100 Subject: [PATCH] Refactor redundant code with reusable function --- CRM/Contribute/BAO/Contribution.php | 44 +++++++++++++++++++++------- CRM/Contribute/Form/Task/Invoice.php | 6 +--- 2 files changed, 34 insertions(+), 16 deletions(-) diff --git a/CRM/Contribute/BAO/Contribution.php b/CRM/Contribute/BAO/Contribution.php index bde21c2472..c8ee4df56b 100644 --- a/CRM/Contribute/BAO/Contribution.php +++ b/CRM/Contribute/BAO/Contribution.php @@ -3185,7 +3185,6 @@ WHERE contribution_id = %1 "; if ($context == 'changedStatus') { //get all the statuses $contributionStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); - $prefixValue = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME, 'contribution_invoice_settings'); if ($params['prevContribution']->contribution_status_id == array_search('Completed', $contributionStatus) && ($params['contribution']->contribution_status_id == array_search('Refunded', $contributionStatus) @@ -3193,11 +3192,7 @@ WHERE contribution_id = %1 "; ) { $params['trxnParams']['total_amount'] = -$params['total_amount']; if (is_null($params['contribution']->creditnote_id) || $params['contribution']->creditnote_id == "null") { - $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', $params['contribution']->id, 'creditnote_id', $creditNoteId); + self::createCreditNoteId($params['contribution']->id); } } elseif (($params['prevContribution']->contribution_status_id == array_search('Pending', $contributionStatus) @@ -3211,11 +3206,7 @@ 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") { - $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', $params['contribution']->id, 'creditnote_id', $creditNoteId); + self::createCreditNoteId($params['contribution']->id); } } else { @@ -4380,4 +4371,35 @@ LIMIT 1;"; return $contribution->composeMessageArray($input, $ids, $values, $recur, $returnMessageText); } + /** + * Generate credit note id with next avaible number + * + * @param Integer $contributionId + * Contribution Id. + * + * @return string + * Credit Note Id. + */ + public static function createCreditNoteId($contributionId) { + $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"; + $dao = CRM_Core_DAO::executeQuery($query); + $dao->fetch(); + $creditNoteNum = $dao->creditnote_number; + $creditNoteId = NULL; + + do { + $creditNoteNum++; + $creditNoteId = CRM_Utils_Array::value('credit_notes_prefix', $prefixValue) . "" . $creditNoteNum; + $result = civicrm_api3('Contribution', 'getcount', array( + 'sequential' => 1, + 'creditnote_id' => $creditNoteId, + )); + } 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 1fb1874ebf..88c68fa11a 100644 --- a/CRM/Contribute/Form/Task/Invoice.php +++ b/CRM/Contribute/Form/Task/Invoice.php @@ -303,11 +303,7 @@ 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)) { - $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); + $creditNoteId = CRM_Contribute_BAO_Contribution::createCreditNoteId($contribution->id); } else { $creditNoteId = $contribution->creditnote_id; -- 2.25.1