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

CRM-20868, set invoice_number at Contribution::create not when invoice is generated #10681

Merged
merged 4 commits into from
Oct 31, 2017
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
27 changes: 26 additions & 1 deletion CRM/Contribute/BAO/Contribution.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ public static function add(&$params, $ids = array()) {
//set defaults in create mode
if (!$contributionID) {
CRM_Core_DAO::setCreateDefaults($params, self::getDefaults());

if (empty($params['invoice_number'])) {
$nextContributionID = CRM_Core_DAO::singleValueQuery("SELECT COALESCE(MAX(id) + 1, 1) FROM civicrm_contribution");
$params['invoice_number'] = self::getInvoiceNumber($nextContributionID);
}
}

$contributionStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
Expand Down Expand Up @@ -5105,12 +5110,17 @@ protected function addContributionPageValuesToValuesHeavyHandedly(&$values) {
*
*
* @param string $name
* @param bool $checkInvoicing
* @return string
*
*/
public static function checkContributeSettings($name = NULL) {
public static function checkContributeSettings($name = NULL, $checkInvoicing = FALSE) {
$contributeSettings = Civi::settings()->get('contribution_invoice_settings');

if ($checkInvoicing && !CRM_Utils_Array::value('invoicing', $contributeSettings)) {
return NULL;
}

if ($name) {
return CRM_Utils_Array::value($name, $contributeSettings);
}
Expand Down Expand Up @@ -5756,4 +5766,19 @@ public static function replaceContributionTokens(
return $contributionDetails;
}

/**
* Get invoice_number for contribution.
*
* @param int $contributionID
*
* @return string
*/
public static function getInvoiceNumber($contributionID) {
if ($invoicePrefix = self::checkContributeSettings('invoice_prefix', TRUE)) {
return $invoicePrefix . $contributionID;
}

return NULL;
}

}
1 change: 1 addition & 0 deletions CRM/Contribute/BAO/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -821,6 +821,7 @@ public static function defaultReturnProperties($mode, $includeCustomFields = TRU
'trxn_id' => 1,
// join
'invoice_id' => 1,
'invoice_number' => 1,
// added
'currency' => 1,
// to
Expand Down
10 changes: 5 additions & 5 deletions CRM/Contribute/Form/Task/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,9 @@ public static function printPDF($contribIDs, &$params, $contactIds) {
$creditNoteId = $contribution->creditnote_id;
}
}
$invoiceNumber = CRM_Utils_Array::value('invoice_prefix', $prefixValue) . "" . $contribution->id;
if (!$contribution->invoice_number) {
$contribution->invoice_number = CRM_Contribute_BAO_Contribution::getInvoiceNumber($contribution->id);
}

//to obtain due date for PDF invoice
$contributionReceiveDate = date('F j,Y', strtotime(date($input['receive_date'])));
Expand Down Expand Up @@ -425,7 +427,7 @@ public static function printPDF($contribIDs, &$params, $contactIds) {
'component' => $input['component'],
'id' => $contribution->id,
'source' => $source,
'invoice_number' => $invoiceNumber,
'invoice_number' => $contribution->invoice_number,
'invoice_id' => $contribution->invoice_id,
'resourceBase' => $config->userFrameworkResourceURL,
'defaultCurrency' => $config->defaultCurrency,
Expand Down Expand Up @@ -469,7 +471,7 @@ public static function printPDF($contribIDs, &$params, $contactIds) {
$tplParams['creditnote_id'] = $creditNoteId;
}

$pdfFileName = "{$invoiceNumber}.pdf";
$pdfFileName = $contribution->invoice_number . ".pdf";
$sendTemplateParams = array(
'groupName' => 'msg_tpl_workflow_contribution',
'valueName' => 'contribution_invoice_receipt',
Expand Down Expand Up @@ -556,8 +558,6 @@ public static function printPDF($contribIDs, &$params, $contactIds) {
$fileName = self::putFile($html, $pdfFileName);
self::addActivities($subject, $contribution->contact_id, $fileName, $params);
}

CRM_Core_DAO::setFieldValue('CRM_Contribute_DAO_Contribution', $contribution->id, 'invoice_number', $invoiceNumber);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

used function to set invoice number

$invoiceTemplate->clearTemplateVars();
}

Expand Down
35 changes: 35 additions & 0 deletions CRM/Upgrade/Incremental/php/FourSeven.php
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,20 @@ public function upgrade_4_7_27($rev) {
public function upgrade_4_7_28($rev) {
$this->addTask(ts('Upgrade DB to %1: SQL', array(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)) {
list($minId, $maxId) = CRM_Core_DAO::executeQuery("SELECT coalesce(min(id),0), coalesce(max(id),0)
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)", array(
1 => $rev,
2 => $startId,
3 => $endId,
));
$this->addTask($title, 'updateContributionInvoiceNumber', $startId, $endId, $invoicePrefix);
}
}
}

/*
Expand Down Expand Up @@ -601,6 +615,27 @@ public static function convertBackendToSettings($domainId, $config_backend) {
return $settings;
}

/**
* Update Invoice number for all completed contribution.
*
* @param \CRM_Queue_TaskContext $ctx
*
* @return bool
*/
public static function updateContributionInvoiceNumber(CRM_Queue_TaskContext $ctx, $startID, $endID, $invoicePrefix) {
CRM_Core_DAO::executeQuery("
UPDATE `civicrm_contribution` SET `invoice_number` = CONCAT(%1, `id`)
WHERE `id` BETWEEN (%2 AND %3) AND `invoice_number` IS NOT NULL ",
array(
1 => array($invoicePrefix, 'string'),
2 => array($startID, 'Integer'),
3 => array($endID, 'Integer'),
)
);

return TRUE;
}

/**
* Add Getting Started dashlet to dashboard
*
Expand Down
3 changes: 3 additions & 0 deletions tests/phpunit/api/v3/ContributionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ public function tearDown() {
* Test Get.
*/
public function testGetContribution() {
$contributionSettings = $this->enableTaxAndInvoicing();
$invoice_prefix = CRM_Contribute_BAO_Contribution::checkContributeSettings('invoice_prefix', TRUE);
$p = array(
'contact_id' => $this->_individualId,
'receive_date' => '2010-01-20',
Expand Down Expand Up @@ -165,6 +167,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['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