Skip to content

Commit

Permalink
Convert setting credit_notes_prefix to be a 'real' setting.
Browse files Browse the repository at this point in the history
This is part of work to convert the non-standard contribution_invoice_settings array of settings into settings in line
with our standards
  • Loading branch information
eileenmcnaughton committed Jan 28, 2020
1 parent c2009ff commit 754051a
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 12 deletions.
10 changes: 3 additions & 7 deletions CRM/Admin/Form/Preferences/Contribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class CRM_Admin_Form_Preferences_Contribute extends CRM_Admin_Form_Preferences {
'deferred_revenue_enabled' => CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME,
'default_invoice_page' => CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME,
'invoicing' => CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME,
'credit_notes_prefix' => CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME,
];

/**
Expand All @@ -43,23 +44,18 @@ class CRM_Admin_Form_Preferences_Contribute extends CRM_Admin_Form_Preferences {

/**
* Build the form object.
*
* @throws \CRM_Core_Exception
*/
public function buildQuickForm() {
parent::buildQuickForm();
$config = CRM_Core_Config::singleton();
$this->invoiceSettings = [
'invoice_prefix' => [
'html_type' => 'text',
'title' => ts('Invoice Prefix'),
'weight' => 1,
'description' => ts('Enter prefix to be display on PDF for invoice'),
],
'credit_notes_prefix' => [
'html_type' => 'text',
'title' => ts('Credit Notes Prefix'),
'weight' => 2,
'description' => ts('Enter prefix to be display on PDF for credit notes.'),
],
'due_date' => [
'html_type' => 'text',
'title' => ts('Due Date'),
Expand Down
2 changes: 2 additions & 0 deletions CRM/Admin/Form/SettingTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,8 @@ protected function getQuickFormType($spec) {
* Get the defaults for all fields defined in the metadata.
*
* All others are pending conversion.
*
* @throws \CiviCRM_API3_Exception
*/
protected function setDefaultsForMetadataDefinedFields() {
CRM_Core_BAO_ConfigSetting::retrieve($this->_defaults);
Expand Down
5 changes: 3 additions & 2 deletions CRM/Contribute/BAO/Contribution.php
Original file line number Diff line number Diff line change
Expand Up @@ -4693,16 +4693,17 @@ public static function generateFromEmailAndName($input, $contribution) {
*
* @return string
* Credit Note Id.
*
* @throws \CiviCRM_API3_Exception
*/
public static function createCreditNoteId() {
$prefixValue = Civi::settings()->get('contribution_invoice_settings');

$creditNoteNum = CRM_Core_DAO::singleValueQuery("SELECT count(creditnote_id) as creditnote_number FROM civicrm_contribution WHERE creditnote_id IS NOT NULL");
$creditNoteId = NULL;

do {
$creditNoteNum++;
$creditNoteId = CRM_Utils_Array::value('credit_notes_prefix', $prefixValue) . "" . $creditNoteNum;
$creditNoteId = Civi::settings()->get('credit_notes_prefix') . '' . $creditNoteNum;
$result = civicrm_api3('Contribution', 'getcount', [
'sequential' => 1,
'creditnote_id' => $creditNoteId,
Expand Down
1 change: 1 addition & 0 deletions CRM/Upgrade/Incremental/php/FiveTwentyThree.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public function upgrade_5_23_alpha1($rev) {
$this->addTask('Remove Google + location option', 'removeGooglePlusOption');
$this->addTask('dev/mailing#59 Add in IMAP_XOAUTH2 protocol option for mailbox access', 'addXoauth2ProtocolOption');
$this->addTask('dev/translation#34 Fix contact-reference option for Postal Code', 'fixContactRefOptionPostalCode');
$this->addTask('Convert contribution_invoice_settings to be separate settings', 'updateContributeSettings');
}

/**
Expand Down
13 changes: 13 additions & 0 deletions Civi/Core/SettingsBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,19 @@ public function all() {
*/
public function get($key) {
$all = $this->all();
if ($key === 'contribution_invoice_settings') {
// Temporary support for using contribution_invoice_settings
// to retrieve the settings that used to be returned with it.
// Once they are all switched over we will add deprecation notices in here.
$convertedKeys = ['credit_notes_prefix'];
$return = $all[$key] ?? [];
foreach ($convertedKeys as $convertedKey) {
if (empty($return[$convertedKey]) && !empty($all[$convertedKey])) {
$return[$convertedKey] = $all[$convertedKey];
}
}
return $return;
}
return isset($all[$key]) ? $all[$key] : NULL;
}

Expand Down
1 change: 0 additions & 1 deletion api/v3/examples/Setting/GetFields.ex.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,6 @@ function setting_getfields_expectedresult() {
'type' => 'Array',
'default' => [
'invoice_prefix' => 'INV_',
'credit_notes_prefix' => 'CN_',
'due_date' => '10',
'due_date_period' => 'days',
'notes' => '',
Expand Down
18 changes: 17 additions & 1 deletion settings/Contribute.setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
'type' => 'Array',
'default' => [
'invoice_prefix' => 'INV_',
'credit_notes_prefix' => 'CN_',
'due_date' => '10',
'due_date_period' => 'days',
'notes' => '',
Expand All @@ -67,10 +66,27 @@
'title' => ts('Enable Tax and Invoicing'),
'is_domain' => 1,
'is_contact' => 0,
'weight' => 5,
'on_change' => [
'CRM_Invoicing_Utils::onToggle',
],
],
'credit_notes_prefix' => [
'group_name' => 'Contribute Preferences',
'group' => 'contribute',
'name' => 'credit_notes_prefix',
'html_type' => 'text',
'quick_form_type' => 'Element',
'add' => '5.23',
'type' => CRM_Utils_Type::T_STRING,
'title' => ts('Credit Notes Prefix'),
'weight' => 2,
'is_domain' => 1,
'is_contact' => 0,
'description' => ts('Prefix to be prepended to credit note ids'),
'default' => 'CN_',
'help_text' => ts('The credit note ID is generated when a contribution is set to Refunded, Cancelled or Chargeback. It is visible on invoices, if invoices are enabled'),
],
'acl_financial_type' => [
'group_name' => 'Contribute Preferences',
'group' => 'contribute',
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/CiviTest/CiviUnitTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3025,7 +3025,6 @@ protected function enableTaxAndInvoicing($params = []) {
[
'invoicing' => 1,
'invoice_prefix' => 'INV_',
'credit_notes_prefix' => 'CN_',
'due_date' => 10,
'due_date_period' => 'days',
'notes' => '',
Expand All @@ -3034,6 +3033,7 @@ protected function enableTaxAndInvoicing($params = []) {
'tax_display_settings' => 'Inclusive',
]
);
Civi::settings()->set('credit_notes_prefix', 'CN_');
return Civi::settings()->set('contribution_invoice_settings', $contributeSetting);
}

Expand Down

0 comments on commit 754051a

Please sign in to comment.