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)
) {
$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)
$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 {
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;
+ }
+
}
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;