Skip to content

Commit

Permalink
[REF] spare a query
Browse files Browse the repository at this point in the history
getInvoiceNumber returns NULL if the 'invoicing' setting is disabled, by checking slightly earlier
we can skip a query if it is not

Upgrade fix: Copy code to upgrade script rather than call a function.

The function will change as we fix the setting but we want the upgrade function to work off the old setting

Test fix: Use hard coded invoicePrefix

We know what it is - if it were not set right then the test would not pick it up because it is comparing
based on the assumption it is - using the string is more reliable
  • Loading branch information
eileenmcnaughton committed Jan 29, 2020
1 parent 4d5b93c commit 5d288dc
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 20 deletions.
18 changes: 5 additions & 13 deletions CRM/Contribute/BAO/Contribution.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion CRM/Contribute/Form/Contribution.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
7 changes: 4 additions & 3 deletions CRM/Upgrade/Incremental/php/FourSeven.php
Original file line number Diff line number Diff line change
Expand Up @@ -455,17 +455,18 @@ 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)", [
1 => $rev,
2 => $startId,
3 => $endId,
]);
$this->addTask($title, 'updateContributionInvoiceNumber', $startId, $endId, $invoicePrefix);
$this->addTask($title, 'updateContributionInvoiceNumber', $startId, $endId, $contributionSettings['invoice_prefix']);
}
}

Expand Down
5 changes: 2 additions & 3 deletions tests/phpunit/api/v3/ContributionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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).
Expand Down

0 comments on commit 5d288dc

Please sign in to comment.