From 9cad3ff4174de4bcad980d5bac6038735340e582 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Wed, 3 May 2017 17:15:28 -0400 Subject: [PATCH] CRM-20387 - Add invoice_number field for human readable contribution invoice --- CRM/Contribute/BAO/Query.php | 3 +- CRM/Contribute/DAO/Contribution.php | 30 +- CRM/Contribute/Form/Task/Invoice.php | 15 +- .../civicrm_msg_template.tpl | 16 + .../contribution_invoice_receipt_html.tpl | 458 ++++++++++++++++++ CRM/Upgrade/Incremental/php/FourSeven.php | 14 + CRM/Upgrade/Incremental/sql/4.7.22.mysql.tpl | 5 + api/v3/examples/Contribution/Create.php | 1 + .../CRM/Contribute/Form/ContributionView.tpl | 9 +- .../CRM/Contribute/Form/Search/Common.tpl | 4 +- tests/phpunit/api/v3/ContributionTest.php | 8 + xml/schema/Contribute/Contribution.xml | 17 +- .../contribution_invoice_receipt_html.tpl | 2 +- 13 files changed, 566 insertions(+), 16 deletions(-) create mode 100644 CRM/Upgrade/4.7.22.msg_template/civicrm_msg_template.tpl create mode 100644 CRM/Upgrade/4.7.22.msg_template/message_templates/contribution_invoice_receipt_html.tpl diff --git a/CRM/Contribute/BAO/Query.php b/CRM/Contribute/BAO/Query.php index 185f38cfe7..e402e7c213 100644 --- a/CRM/Contribute/BAO/Query.php +++ b/CRM/Contribute/BAO/Query.php @@ -232,6 +232,7 @@ class CRM_Contribute_BAO_Query extends CRM_Core_BAO_Query { CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes($financialTypes); $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_contribution.$name", 'IN', array_keys($financialTypes), 'String'); case 'invoice_id': + case 'invoice_number': case 'payment_instrument_id': case 'contribution_payment_instrument_id': case 'contribution_page_id': @@ -943,7 +944,7 @@ class CRM_Contribute_BAO_Query extends CRM_Core_BAO_Query { // Add field for transaction ID search $form->addElement('text', 'contribution_trxn_id', ts("Transaction ID")); - $form->addElement('text', 'invoice_id', ts("Invoice ID")); + $form->addElement('text', 'invoice_number', ts("Invoice Number")); $form->addElement('text', 'contribution_check_number', ts('Check Number')); // Add field for pcp display in roll search diff --git a/CRM/Contribute/DAO/Contribution.php b/CRM/Contribute/DAO/Contribution.php index e0285ac385..6744a69fa9 100644 --- a/CRM/Contribute/DAO/Contribution.php +++ b/CRM/Contribute/DAO/Contribution.php @@ -30,7 +30,7 @@ * * Generated from xml/schema/CRM/Contribute/Contribution.xml * DO NOT EDIT. Generated by CRM_Core_CodeGen - * (GenCodeChecksum:59c3615cae90b0580f7d77aa8343b21f) + * (GenCodeChecksum:45a20d00d01766a61687cbac5cef1482) */ require_once 'CRM/Core/DAO.php'; require_once 'CRM/Utils/Type.php'; @@ -122,6 +122,12 @@ class CRM_Contribute_DAO_Contribution extends CRM_Core_DAO { * @var string */ public $invoice_id; + /** + * Human readable invoice number + * + * @var string + */ + public $invoice_number; /** * 3 character string, value from config setting or input via user. * @@ -487,7 +493,7 @@ class CRM_Contribute_DAO_Contribution extends CRM_Core_DAO { 'invoice_id' => array( 'name' => 'invoice_id', 'type' => CRM_Utils_Type::T_STRING, - 'title' => ts('Invoice ID') , + 'title' => ts('Invoice Reference') , 'description' => 'unique invoice id, system generated or passed in', 'maxlength' => 255, 'size' => CRM_Utils_Type::HUGE, @@ -504,6 +510,26 @@ class CRM_Contribute_DAO_Contribution extends CRM_Core_DAO { 'type' => 'Text', ) , ) , + 'invoice_number' => array( + 'name' => 'invoice_number', + 'type' => CRM_Utils_Type::T_STRING, + 'title' => ts('Invoice Number') , + 'description' => 'Human readable invoice number', + 'maxlength' => 255, + 'size' => CRM_Utils_Type::HUGE, + 'import' => true, + 'where' => 'civicrm_contribution.invoice_number', + 'headerPattern' => '/invoice(.?number)?/i', + 'dataPattern' => '', + 'export' => true, + 'table_name' => 'civicrm_contribution', + 'entity' => 'Contribution', + 'bao' => 'CRM_Contribute_BAO_Contribution', + 'localizable' => 0, + 'html' => array( + 'type' => 'Text', + ) , + ) , 'currency' => array( 'name' => 'currency', 'type' => CRM_Utils_Type::T_STRING, diff --git a/CRM/Contribute/Form/Task/Invoice.php b/CRM/Contribute/Form/Task/Invoice.php index 5500804b71..121bf65ec0 100644 --- a/CRM/Contribute/Form/Task/Invoice.php +++ b/CRM/Contribute/Form/Task/Invoice.php @@ -312,7 +312,7 @@ class CRM_Contribute_Form_Task_Invoice extends CRM_Contribute_Form_Task { $creditNoteId = $contribution->creditnote_id; } } - $invoiceId = CRM_Utils_Array::value('invoice_prefix', $prefixValue) . "" . $contribution->id; + $invoiceNumber = CRM_Utils_Array::value('invoice_prefix', $prefixValue) . "" . $contribution->id; //to obtain due date for PDF invoice $contributionReceiveDate = date('F j,Y', strtotime(date($input['receive_date']))); @@ -425,7 +425,8 @@ class CRM_Contribute_Form_Task_Invoice extends CRM_Contribute_Form_Task { 'component' => $input['component'], 'id' => $contribution->id, 'source' => $source, - 'invoice_id' => $invoiceId, + 'invoice_number' => $invoiceNumber, + 'invoice_id' => $contribution->invoice_id, 'resourceBase' => $config->userFrameworkResourceURL, 'defaultCurrency' => $config->defaultCurrency, 'amount' => $contribution->total_amount, @@ -468,7 +469,7 @@ class CRM_Contribute_Form_Task_Invoice extends CRM_Contribute_Form_Task { $tplParams['creditnote_id'] = $creditNoteId; } - $pdfFileName = "{$invoiceId}.pdf"; + $pdfFileName = "{$invoiceNumber}.pdf"; $sendTemplateParams = array( 'groupName' => 'msg_tpl_workflow_contribution', 'valueName' => 'contribution_invoice_receipt', @@ -536,7 +537,7 @@ class CRM_Contribute_Form_Task_Invoice extends CRM_Contribute_Form_Task { list($sent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams); // functions call for adding activity with attachment - $pdfFileName = "{$invoiceId}.pdf"; + $pdfFileName = "{$invoiceNumber}.pdf"; $fileName = self::putFile($html, $pdfFileName); self::addActivities($subject, $contribution->contact_id, $fileName, $params); } @@ -551,12 +552,12 @@ class CRM_Contribute_Form_Task_Invoice extends CRM_Contribute_Form_Task { list($sent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams); // functions call for adding activity with attachment - $pdfFileName = "{$invoiceId}.pdf"; + $pdfFileName = "{$invoiceNumber}.pdf"; $fileName = self::putFile($html, $pdfFileName); self::addActivities($subject, $contribution->contact_id, $fileName, $params); } - CRM_Core_DAO::setFieldValue('CRM_Contribute_DAO_Contribution', $contribution->id, 'invoice_id', $invoiceId); + CRM_Core_DAO::setFieldValue('CRM_Contribute_DAO_Contribution', $contribution->id, 'invoice_number', $invoiceNumber); $invoiceTemplate->clearTemplateVars(); } @@ -565,7 +566,7 @@ class CRM_Contribute_Form_Task_Invoice extends CRM_Contribute_Form_Task { return $html; } else { - $pdfFileName = "{$invoiceId}.pdf"; + $pdfFileName = "{$invoiceNumber}.pdf"; CRM_Utils_PDF_Utils::html2pdf($messageInvoice, $pdfFileName, FALSE, array( 'margin_top' => 10, 'margin_left' => 65, diff --git a/CRM/Upgrade/4.7.22.msg_template/civicrm_msg_template.tpl b/CRM/Upgrade/4.7.22.msg_template/civicrm_msg_template.tpl new file mode 100644 index 0000000000..60cb7dfcde --- /dev/null +++ b/CRM/Upgrade/4.7.22.msg_template/civicrm_msg_template.tpl @@ -0,0 +1,16 @@ +{php} + $dir = SMARTY_DIR . '/../../CRM/Upgrade/4.7.22.msg_template/message_templates'; + $templates = array(); + foreach (preg_grep('/\.tpl$/', scandir($dir)) as $filename) { + $parts = explode('_', basename($filename, '.tpl')); + $templates[] = array('type' => array_pop($parts), 'name' => implode('_', $parts), 'filename' => "$dir/$filename"); + } + $this->assign('templates', $templates); +{/php} + +{foreach from=$templates item=tpl} + {fetch assign=content file=$tpl.filename} + SELECT @workflow_id := MAX(id) FROM civicrm_option_value WHERE name = '{$tpl.name}'; + SELECT @content := msg_{$tpl.type} FROM civicrm_msg_template WHERE workflow_id = @workflow_id AND is_reserved = 1 LIMIT 1; + UPDATE civicrm_msg_template SET msg_{$tpl.type} = '{$content|escape:"quotes"}' WHERE workflow_id = @workflow_id AND (is_reserved = 1 OR (is_default = 1 AND msg_{$tpl.type} = @content)); +{/foreach} diff --git a/CRM/Upgrade/4.7.22.msg_template/message_templates/contribution_invoice_receipt_html.tpl b/CRM/Upgrade/4.7.22.msg_template/message_templates/contribution_invoice_receipt_html.tpl new file mode 100644 index 0000000000..75d7bf006c --- /dev/null +++ b/CRM/Upgrade/4.7.22.msg_template/message_templates/contribution_invoice_receipt_html.tpl @@ -0,0 +1,458 @@ + + + + + + + + + + + +
+
+ + + + + + + + + {if $organization_name} + + {else} + + {/if} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
{ts}INVOICE{/ts}{ts}Invoice Date:{/ts}{$domain_organization}
{$display_name} ({$organization_name}){$display_name}{$invoice_date} + + {if $domain_street_address }{$domain_street_address}{/if} + {if $domain_supplemental_address_1 }{$domain_supplemental_address_1}{/if} + +
{$street_address} {$supplemental_address_1}{ts}Invoice Number:{/ts} + + {if $domain_supplemental_address_2 }{$domain_supplemental_address_2}{/if} + {if $domain_state }{$domain_state}{/if} + +
{$supplemental_address_2} {$stateProvinceAbbreviation}{$invoice_number} + + {if $domain_city}{$domain_city}{/if} + {if $domain_postal_code }{$domain_postal_code}{/if} + +
{$city} {$postal_code}{ts}Reference:{/ts} {if $domain_country}{$domain_country}{/if}
{$source} {if $domain_phone}{$domain_phone}{/if}
{if $domain_email}{$domain_email}{/if}
+ + + + +
+ {* FIXME: style this table so that it looks like the text version (justification, etc.) *} + + + + + + + + {foreach from=$lineItem item=value key=priceset name=taxpricevalue} + {if $smarty.foreach.taxpricevalue.index eq 0} + + + + {else} + + + + {/if} + + + + + {if $value.tax_amount != ''} + + {else} + + {/if} + + + {/foreach} + + + + + + + {foreach from = $dataArray item = value key = priceset} + + + {if $priceset} + + + {elseif $priceset == 0} + + + + {/if} + {/foreach} + + + + + + + + + + + {if $is_pay_later == 0} + + + + + + + + + + + + + + + + {/if} +


+ + + + {if $contribution_status_id == $pendingStatusId && $is_pay_later == 1} + + + + + {/if} +
{ts}Description{/ts}{ts}Quantity{/ts}{ts}Unit Price{/ts}{$taxTerm} {ts 1=$defaultCurrency}Amount %1{/ts}


+ {if $value.html_type eq 'Text'} + {$value.label} + {else} + {$value.field_title} - {$value.label} + {/if} + {if $value.description} +
{$value.description|truncate:30:"..."}
+ {/if} +
+
{$value.qty} {$value.unit_price|crmMoney:$currency} {$value.tax_rate}%{ts 1=$taxTerm}No %1{/ts}{$value.subTotal|crmMoney:$currency}

{ts}Sub Total{/ts} {$subTotal|crmMoney:$currency}
{ts 1=$taxTerm 2=$priceset}TOTAL %1 %2%{/ts}{$value|crmMoney:$currency} {ts 1=$taxTerm}TOTAL NO %1{/ts}{$value|crmMoney:$currency}

{ts 1=$defaultCurrency}TOTAL %1{/ts}{$amount|crmMoney:$currency}
+ {if $contribution_status_id == $refundedStatusId} + {ts}LESS Amount Credited{/ts} + {else} + {ts}LESS Amount Paid{/ts} + {/if} + + {$amountPaid|crmMoney:$currency}

{ts}AMOUNT DUE:{/ts} {$amountDue|crmMoney:$currency}
{ts 1=$dueDate}DUE DATE: %1{/ts}
+
+ {if $contribution_status_id == $pendingStatusId && $is_pay_later == 1} + + + + +
+ + + + + +
{ts}PAYMENT ADVICE{/ts}

{ts}To: {/ts}
+ {$domain_organization}
+ {$domain_street_address} {$domain_supplemental_address_1}
+ {$domain_supplemental_address_2} {$domain_state}
+ {$domain_city} {$domain_postal_code}
+ {$domain_country}
+ {$domain_phone}
+ {$domain_email}
+


{$notes} +
+ + + + + + + + + + + + + {if $is_pay_later == 1} + + + + + + {else} + + + + + + {/if} + + + + + + + + +
{ts}Customer: {/ts}{$display_name}
{ts}Invoice Number: {/ts}{$invoice_id}

{ts}Amount Due:{/ts}{$amount|crmMoney:$currency}
{ts}Amount Due: {/ts}{$amountDue|crmMoney:$currency}
{ts}Due Date: {/ts}{$dueDate}

+
+ {/if} + + {if $contribution_status_id == $refundedStatusId || $contribution_status_id == $cancelledStatusId} + + + + +
+
+ + + + + + + + + + {if $organization_name} + + {else} + + {/if} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
{ts}CREDIT NOTE{/ts}{ts}Date:{/ts}{$domain_organization}
{$display_name} ({$organization_name}){$display_name}{$invoice_date} + + {if $domain_street_address }{$domain_street_address}{/if} + {if $domain_supplemental_address_1 }{$domain_supplemental_address_1}{/if} + +
{$street_address} {$supplemental_address_1}{ts}Credit Note Number:{/ts} + + {if $domain_supplemental_address_2 }{$domain_supplemental_address_2}{/if} + {if $domain_state }{$domain_state}{/if} + +
{$supplemental_address_2} {$stateProvinceAbbreviation}{$creditnote_id} + + {if $domain_city}{$domain_city}{/if} + {if $domain_postal_code }{$domain_postal_code}{/if} + +
{$city} {$postal_code}{ts}Reference:{/ts} + + {if $domain_country}{$domain_country}{/if} + +
{$source} + + {if $domain_phone}{$domain_phone}{/if} + +
+ + {if $domain_email}{$domain_email}{/if} + +
+ + + + + +
+ {* FIXME: style this table so that it looks like the text version (justification, etc.) *} + + + + + + + + {foreach from=$lineItem item=value key=priceset name=pricevalue} + {if $smarty.foreach.pricevalue.index eq 0} + + {else} + + {/if} + + + + + {if $value.tax_amount != ''} + + {else} + + {/if} + + + {/foreach} + + + + + + + {foreach from = $dataArray item = value key = priceset} + + + {if $priceset} + + + {elseif $priceset == 0} + + + + {/if} + {/foreach} + + + + + + + + + + {if $is_pay_later == 0} + + + + + + + + + + + + + + + + {/if} +


+ + + + + + + +
{ts}Description{/ts}{ts}Quantity{/ts}{ts}Unit Price{/ts}{$taxTerm} {ts 1=$defaultCurrency}Amount %1{/ts}


+ + {if $value.html_type eq 'Text'} + {$value.label} + {else} + {$value.field_title} - {$value.label} + {/if} + {if $value.description} +
{$value.description|truncate:30:"..."}
+ {/if} +
+
{$value.qty} {$value.unit_price|crmMoney:$currency} {$value.tax_rate}%{ts 1=$taxTerm}No %1{/ts}{$value.subTotal|crmMoney:$currency}

{ts}Sub Total{/ts} {$subTotal|crmMoney:$currency}
{ts 1=$taxTerm 2=$priceset}TOTAL %1 %2%{/ts}{$value|crmMoney:$currency} {ts 1=$taxTerm}TOTAL NO %1{/ts}{$value|crmMoney:$currency}

{ts 1=$defaultCurrency}TOTAL %1{/ts}{$amount|crmMoney:$currency}
{ts}LESS Credit to invoice(s){/ts}{$amount|crmMoney:$currency}

{ts}REMAINING CREDIT{/ts}{$amountDue|crmMoney:$currency}
+
+ + + + +
+ + + + + + +
{ts}CREDIT ADVICE{/ts}

{ts}Please do not pay on this advice. Deduct the amount of this Credit Note from your next payment to us{/ts}

+ + + + + + + + + + + + + + + + + +
{ts}Customer:{/ts} {$display_name}
{ts}Credit Note#:{/ts} {$creditnote_id}

{ts}Credit Amount:{/ts}{$amount|crmMoney:$currency}
+
+ {/if} +
+ + diff --git a/CRM/Upgrade/Incremental/php/FourSeven.php b/CRM/Upgrade/Incremental/php/FourSeven.php index aea404fd53..f0e601ae77 100644 --- a/CRM/Upgrade/Incremental/php/FourSeven.php +++ b/CRM/Upgrade/Incremental/php/FourSeven.php @@ -122,6 +122,9 @@ class CRM_Upgrade_Incremental_php_FourSeven extends CRM_Upgrade_Incremental_Base $postUpgradeMessage .= '

civicrm_sms_provider ' . ts('has now had a domain id column added. As there is more than 1 domains in this install you need to manually set the domain id for the providers in this install') . '

'; } } + if ($rev == '4.7.22') { + $postUpgradeMessage .= '

' . ts('Default version of the following System Workflow Message Templates have been modified: If you have modified these templates, please review the new default versions and implement updates as needed to your copies (Administer > Communications > Message Templates > System Workflow Messages).'); + } } /** @@ -360,6 +363,17 @@ class CRM_Upgrade_Incremental_php_FourSeven extends CRM_Upgrade_Incremental_Base 'civicrm_mail_settings', 'activity_status', "varchar (255) DEFAULT NULL COMMENT 'Name of status to use when creating email to activity.'"); } + /** + * Upgrade function. + * + * @param string $rev + */ + public function upgrade_4_7_22($rev) { + $this->addTask('CRM-20387 - Add invoice_number column to civicrm_contribution', 'addColumn', + 'civicrm_contribution', 'invoice_number', "varchar(255) COMMENT 'Human readable invoice number' DEFAULT NULL"); + $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev); + } + /* * Important! All upgrade functions MUST add a 'runSql' task. * Uncomment and use the following template for a new upgrade version diff --git a/CRM/Upgrade/Incremental/sql/4.7.22.mysql.tpl b/CRM/Upgrade/Incremental/sql/4.7.22.mysql.tpl index c4fca4dc84..071a9a6430 100644 --- a/CRM/Upgrade/Incremental/sql/4.7.22.mysql.tpl +++ b/CRM/Upgrade/Incremental/sql/4.7.22.mysql.tpl @@ -1 +1,6 @@ {* file to handle db changes in 4.7.22 during upgrade *} + +{include file='../CRM/Upgrade/4.7.22.msg_template/civicrm_msg_template.tpl'} + +-- CRM-20387 +UPDATE `civicrm_contribution` SET `invoice_number` = `invoice_id` WHERE `invoice_id` LIKE CONCAT('%', `id`); diff --git a/api/v3/examples/Contribution/Create.php b/api/v3/examples/Contribution/Create.php index 9b49970393..a1ffbf661b 100644 --- a/api/v3/examples/Contribution/Create.php +++ b/api/v3/examples/Contribution/Create.php @@ -83,6 +83,7 @@ function contribution_create_expectedresult() { 'tax_amount' => '', 'revenue_recognition_date' => '', 'contribution_type_id' => '1', + 'invoice_number' => '', ), ), ); diff --git a/templates/CRM/Contribute/Form/ContributionView.tpl b/templates/CRM/Contribute/Form/ContributionView.tpl index 4252279c83..33eee999e2 100644 --- a/templates/CRM/Contribute/Form/ContributionView.tpl +++ b/templates/CRM/Contribute/Form/ContributionView.tpl @@ -217,9 +217,16 @@ {/if} + {if $invoice_number} + + {ts}Invoice Number{/ts} + {$invoice_number}  + + {/if} + {if $invoice_id} - {ts}Invoice ID{/ts} + {ts}Invoice Reference{/ts} {$invoice_id}  {/if} diff --git a/templates/CRM/Contribute/Form/Search/Common.tpl b/templates/CRM/Contribute/Form/Search/Common.tpl index 2e601e47a0..9b94d9df54 100644 --- a/templates/CRM/Contribute/Form/Search/Common.tpl +++ b/templates/CRM/Contribute/Form/Search/Common.tpl @@ -71,8 +71,8 @@ - {$form.invoice_id.label}
- {$form.invoice_id.html} + {$form.invoice_number.label}
+ {$form.invoice_number.html} diff --git a/tests/phpunit/api/v3/ContributionTest.php b/tests/phpunit/api/v3/ContributionTest.php index 9dc657d2df..3317805d18 100644 --- a/tests/phpunit/api/v3/ContributionTest.php +++ b/tests/phpunit/api/v3/ContributionTest.php @@ -247,6 +247,14 @@ class api_v3_ContributionTest extends CiviUnitTestCase { $params['campaign_id'] = $this->campaignCreate(); $contributionID = $this->contributionCreate($params); + + // update contribution with invoice number + $params = array_merge($params, array( + 'id' => $contributionID, + 'invoice_number' => CRM_Utils_Array::value('invoice_prefix', Civi::settings()->get('contribution_invoice_settings')) . "" . $contributionID, + )); + $contributionID = $this->contributionCreate($params); + $contribution = $this->callAPISuccessGetSingle('Contribution', array('id' => $contributionID)); $this->assertEquals('bouncer', $contribution['check_number']); $this->assertEquals('bouncer', $contribution['contribution_check_number']); diff --git a/xml/schema/Contribute/Contribution.xml b/xml/schema/Contribute/Contribution.xml index ad9260293b..edcb3befa4 100644 --- a/xml/schema/Contribute/Contribution.xml +++ b/xml/schema/Contribute/Contribution.xml @@ -226,7 +226,7 @@ invoice_id - Invoice ID + Invoice Reference varchar 255 true @@ -236,7 +236,20 @@ Text 1.3 - + + + invoice_number + Invoice Number + varchar + 255 + true + /invoice(.?number)?/i + Human readable invoice number + + Text + + 4.7 + currency varchar diff --git a/xml/templates/message_templates/contribution_invoice_receipt_html.tpl b/xml/templates/message_templates/contribution_invoice_receipt_html.tpl index 0b5633f53b..75d7bf006c 100644 --- a/xml/templates/message_templates/contribution_invoice_receipt_html.tpl +++ b/xml/templates/message_templates/contribution_invoice_receipt_html.tpl @@ -47,7 +47,7 @@ {$supplemental_address_2} {$stateProvinceAbbreviation} - {$invoice_id} + {$invoice_number} {if $domain_city}{$domain_city}{/if} -- 2.25.1