Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[REF] Deprecate calls to createCreditNoteId #15492

Merged
merged 1 commit into from
Oct 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions CRM/Contribute/BAO/Contribution.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,17 +150,14 @@ public static function add(&$params, $ids = []) {

$contributionStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
//if contribution is created with cancelled or refunded status, add credit note id
if (!empty($params['contribution_status_id'])) {
// @todo - should we include Chargeback? If so use self::isContributionStatusNegative($params['contribution_status_id'])
if (($params['contribution_status_id'] == array_search('Refunded', $contributionStatus)
|| $params['contribution_status_id'] == array_search('Cancelled', $contributionStatus))
) {
if (empty($params['creditnote_id'])) {
$params['creditnote_id'] = self::createCreditNoteId();
}
// do the same for chargeback - this entered the code 'accidentally' but moving it to here
// as part of cleanup maintains consistency.
if (self::isContributionStatusNegative(CRM_Utils_Array::value('contribution_status_id', $params))) {
if (empty($params['creditnote_id'])) {
$params['creditnote_id'] = self::createCreditNoteId();
}
}
else {
if (empty($params['contribution_status_id'])) {
// Since the fee amount is expecting this (later on) ensure it is always set.
// It would only not be set for an update where it is unchanged.
$params['contribution_status_id'] = civicrm_api3('Contribution', 'getvalue', [
Expand Down Expand Up @@ -1126,6 +1123,8 @@ private static function updateFinancialAccountsOnContributionStatusChange(&$para
// @todo we should stop passing $params by reference - splitting this out would be a step towards that.
$params['trxnParams']['total_amount'] = -$params['total_amount'];
if (empty($params['contribution']->creditnote_id)) {
// This is always set in the Contribution::create function.
CRM_Core_Error::deprecatedFunctionWarning('Logic says this line is never reached & can be removed');
$creditNoteId = self::createCreditNoteId();
CRM_Core_DAO::setFieldValue('CRM_Contribute_DAO_Contribution', $params['contribution']->id, 'creditnote_id', $creditNoteId);
}
Expand All @@ -1141,6 +1140,8 @@ private static function updateFinancialAccountsOnContributionStatusChange(&$para
$params['trxnParams']['to_financial_account_id'] = $arAccountId;
$params['trxnParams']['total_amount'] = -$params['total_amount'];
if (empty($params['contribution']->creditnote_id)) {
// This is always set in the Contribution::create function.
CRM_Core_Error::deprecatedFunctionWarning('Logic says this line is never reached & can be removed');
$creditNoteId = self::createCreditNoteId();
CRM_Core_DAO::setFieldValue('CRM_Contribute_DAO_Contribution', $params['contribution']->id, 'creditnote_id', $creditNoteId);
}
Expand Down
1 change: 1 addition & 0 deletions CRM/Contribute/Form/Task/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ public static function printPDF($contribIDs, &$params, $contactIds) {

if ($contribution->contribution_status_id == $refundedStatusId || $contribution->contribution_status_id == $cancelledStatusId) {
if (is_null($contribution->creditnote_id)) {
CRM_Core_Error::deprecatedFunctionWarning('This it the wrong place to add a credit note id since the id is added when the status is changed in the Contribution::Create function- hopefully it is never hit');
$creditNoteId = CRM_Contribute_BAO_Contribution::createCreditNoteId();
CRM_Core_DAO::setFieldValue('CRM_Contribute_DAO_Contribution', $contribution->id, 'creditnote_id', $creditNoteId);
}
Expand Down
8 changes: 8 additions & 0 deletions tests/phpunit/api/v3/ContributionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1328,6 +1328,8 @@ public function testCreateUpdateContributionRefund() {
* CRM-17951 the contra account is a financial account with a relationship to a
* financial type. It is not always configured but should be reflected
* in the financial_trxn & financial_item table if it is.
*
* @throws \CRM_Core_Exception
*/
public function testCreateUpdateChargebackContributionDefaultAccount() {
$contribution = $this->callAPISuccess('Contribution', 'create', $this->_params);
Expand Down Expand Up @@ -1552,6 +1554,10 @@ public function testCreateUpdateContributionInValidStatusChange() {
* Function tests that financial records are added when Pending Contribution is Canceled.
*/
public function testCreateUpdateContributionCancelPending() {
// Enable & disable invoicing just to standardise the credit note id setting.
// Longer term we want to separate that setting from 'taxAndInvoicing'.
// and / or remove from core.
$this->enableTaxAndInvoicing();
$contribParams = [
'contact_id' => $this->_individualId,
'receive_date' => '2012-01-01',
Expand All @@ -1575,6 +1581,8 @@ public function testCreateUpdateContributionCancelPending() {
$contribution = $this->callAPISuccess('contribution', 'create', $newParams);
$this->_checkFinancialTrxn($contribution, 'cancelPending', NULL, $checkTrxnDate);
$this->_checkFinancialItem($contribution['id'], 'cancelPending');
$this->assertEquals('CN_1', $contribution['values'][$contribution['id']]['creditnote_id']);
$this->disableTaxAndInvoicing();
}

/**
Expand Down