diff --git a/CRM/Contribute/BAO/Contribution.php b/CRM/Contribute/BAO/Contribution.php index 437bbfc19042..31f5418ff917 100644 --- a/CRM/Contribute/BAO/Contribution.php +++ b/CRM/Contribute/BAO/Contribution.php @@ -131,7 +131,7 @@ public static function add(&$params, $ids = []) { if (!$contributionID) { CRM_Core_DAO::setCreateDefaults($params, self::getDefaults()); - if (empty($params['invoice_number'])) { + if (empty($params['invoice_number']) && CRM_Invoicing_Utils::isInvoicingEnabled()) { $nextContributionID = CRM_Core_DAO::singleValueQuery("SELECT COALESCE(MAX(id) + 1, 1) FROM civicrm_contribution"); $params['invoice_number'] = self::getInvoiceNumber($nextContributionID); } @@ -5059,21 +5059,13 @@ protected function addContributionPageValuesToValuesHeavyHandedly(&$values) { * * * @param string $name - * @param bool $checkInvoicing + * * @return string * */ - public static function checkContributeSettings($name = NULL, $checkInvoicing = FALSE) { + public static function checkContributeSettings($name) { $contributeSettings = Civi::settings()->get('contribution_invoice_settings'); - - if ($checkInvoicing && empty($contributeSettings['invoicing'])) { - return NULL; - } - - if ($name) { - return CRM_Utils_Array::value($name, $contributeSettings); - } - return $contributeSettings; + return CRM_Utils_Array::value($name, $contributeSettings); } /** @@ -5888,7 +5880,7 @@ public static function getContributionTokenValues($id, $messageToken) { * @return string */ public static function getInvoiceNumber($contributionID) { - if ($invoicePrefix = self::checkContributeSettings('invoice_prefix', TRUE)) { + if ($invoicePrefix = self::checkContributeSettings('invoice_prefix')) { return $invoicePrefix . $contributionID; } diff --git a/CRM/Contribute/Form/Contribution.php b/CRM/Contribute/Form/Contribution.php index 4bbaabac47d0..a47c573dd15e 100644 --- a/CRM/Contribute/Form/Contribution.php +++ b/CRM/Contribute/Form/Contribution.php @@ -1302,7 +1302,7 @@ public function testSubmit($params, $action, $creditCardMode = NULL) { ]); $this->_id = $params['id']; $this->_values = $existingContribution; - if (CRM_Contribute_BAO_Contribution::checkContributeSettings('invoicing')) { + if (CRM_Invoicing_Utils::isInvoicingEnabled()) { $this->_values['tax_amount'] = civicrm_api3('contribution', 'getvalue', [ 'id' => $params['id'], 'return' => 'tax_amount', diff --git a/CRM/Upgrade/Incremental/php/FourSeven.php b/CRM/Upgrade/Incremental/php/FourSeven.php index eee123e7ce7b..30724c0508a7 100644 --- a/CRM/Upgrade/Incremental/php/FourSeven.php +++ b/CRM/Upgrade/Incremental/php/FourSeven.php @@ -455,9 +455,10 @@ public function upgrade_4_7_28($rev) { $this->addTask(ts('Upgrade DB to %1: SQL', [1 => $rev]), 'runSql', $rev); $this->addTask('CRM-20572: Fix date fields in save search criteria of Contrib Sybunt custom search ', 'fixDateFieldsInSmartGroups'); // CRM-20868 : Update invoice_numbers (in batch) with value in [invoice prefix][contribution id] format - if ($invoicePrefix = CRM_Contribute_BAO_Contribution::checkContributeSettings('invoice_prefix', TRUE)) { + $contributionSettings = Civi::settings()->get('contribution_invoice_settings'); + if (!empty($contributionSettings['invoicing']) && !empty($contributionSettings['invoice_prefix'])) { list($minId, $maxId) = CRM_Core_DAO::executeQuery("SELECT coalesce(min(id),0), coalesce(max(id),0) - FROM civicrm_contribution ")->getDatabaseResult()->fetchRow(); + FROM civicrm_contribution ")->getDatabaseResult()->fetchRow(); for ($startId = $minId; $startId <= $maxId; $startId += self::BATCH_SIZE) { $endId = $startId + self::BATCH_SIZE - 1; $title = ts("Upgrade DB to %1: Update Contribution Invoice number (%2 => %3)", [ @@ -465,7 +466,7 @@ public function upgrade_4_7_28($rev) { 2 => $startId, 3 => $endId, ]); - $this->addTask($title, 'updateContributionInvoiceNumber', $startId, $endId, $invoicePrefix); + $this->addTask($title, 'updateContributionInvoiceNumber', $startId, $endId, $contributionSettings['invoice_prefix']); } } diff --git a/tests/phpunit/api/v3/ContributionTest.php b/tests/phpunit/api/v3/ContributionTest.php index 1e7180638687..1b8f437e887b 100644 --- a/tests/phpunit/api/v3/ContributionTest.php +++ b/tests/phpunit/api/v3/ContributionTest.php @@ -132,8 +132,7 @@ public function tearDown() { * @throws \CRM_Core_Exception */ public function testGetContribution() { - $contributionSettings = $this->enableTaxAndInvoicing(); - $invoice_prefix = CRM_Contribute_BAO_Contribution::checkContributeSettings('invoice_prefix', TRUE); + $this->enableTaxAndInvoicing(); $p = [ 'contact_id' => $this->_individualId, 'receive_date' => '2010-01-20', @@ -171,7 +170,7 @@ public function testGetContribution() { $this->assertEquals($contribution['net_amount'], 95.00); $this->assertEquals($contribution['trxn_id'], 23456); $this->assertEquals($contribution['invoice_id'], 78910); - $this->assertEquals($contribution['invoice_number'], $invoice_prefix . $contributions['id']); + $this->assertEquals($contribution['invoice_number'], 'INV_' . $contributions['id']); $this->assertEquals($contribution['contribution_source'], 'SSF'); $this->assertEquals($contribution['contribution_status'], 'Completed'); // Create a second contribution - we are testing that 'id' gets the right contribution id (not the contact id).