Merge pull request #13001 from JKingsnorth/core-456
[civicrm-core.git] / CRM / Admin / Form / Preferences / Contribute.php
CommitLineData
d75f2f47 1<?php
c73475ea
WA
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
c73475ea 5 +--------------------------------------------------------------------+
8c9251b3 6 | Copyright CiviCRM LLC (c) 2004-2018 |
c73475ea
WA
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28/**
29 *
30 * @package CRM
8c9251b3 31 * @copyright CiviCRM LLC (c) 2004-2018
c73475ea 32 */
9d4da082
RK
33
34/**
ce064e4f 35 * This class generates form components for the display preferences.
9d4da082
RK
36 */
37class CRM_Admin_Form_Preferences_Contribute extends CRM_Admin_Form_Preferences {
e0d19757
WA
38 protected $_settings = array(
39 'cvv_backoffice_required' => CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME,
268a84f2 40 'update_contribution_on_membership_type_change' => CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME,
201b45e2 41 'acl_financial_type' => CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME,
4af9c91d 42 'always_post_to_accounts_receivable' => CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME,
201b45e2
PN
43 'deferred_revenue_enabled' => CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME,
44 'default_invoice_page' => CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME,
201b45e2 45 'invoicing' => CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME,
e0d19757 46 );
ce064e4f 47
1be9403d 48 /**
eceb18cc 49 * Process the form submission.
1be9403d 50 */
00be9182 51 public function preProcess() {
fff0f449 52 $config = CRM_Core_Config::singleton();
9d4da082
RK
53 CRM_Utils_System::setTitle(ts('CiviContribute Component Settings'));
54 $this->_varNames = array(
9d72cede 55 CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME => array(
353ffa53
TO
56 'invoice_prefix' => array(
57 'html_type' => 'text',
58 'title' => ts('Invoice Prefix'),
59 'weight' => 1,
60 'description' => ts('Enter prefix to be display on PDF for invoice'),
61 ),
62 'credit_notes_prefix' => array(
63 'html_type' => 'text',
64 'title' => ts('Credit Notes Prefix'),
65 'weight' => 2,
66 'description' => ts('Enter prefix to be display on PDF for credit notes.'),
67 ),
68 'due_date' => array(
69 'html_type' => 'text',
70 'title' => ts('Due Date'),
71 'weight' => 3,
72 ),
73 'due_date_period' => array(
74 'html_type' => 'select',
75 'title' => ts('For transmission'),
76 'weight' => 4,
77 'description' => ts('Select the interval for due date.'),
78 'option_values' => array(
79 'select' => ts('- select -'),
80 'days' => ts('Days'),
81 'months' => ts('Months'),
82 'years' => ts('Years'),
9d72cede 83 ),
353ffa53
TO
84 ),
85 'notes' => array(
86 'html_type' => 'wysiwyg',
87 'title' => ts('Notes or Standard Terms'),
88 'weight' => 5,
89 'description' => ts('Enter note or message to be displayed on PDF invoice or credit notes '),
90 'attributes' => array('rows' => 2, 'cols' => 40),
91 ),
92 'is_email_pdf' => array(
93 'html_type' => 'checkbox',
94 'title' => ts('Automatically email invoice when user purchases online'),
95 'weight' => 6,
96 ),
97 'tax_term' => array(
98 'html_type' => 'text',
99 'title' => ts('Tax Term'),
100 'weight' => 7,
101 ),
102 'tax_display_settings' => array(
103 'html_type' => 'select',
104 'title' => ts('Tax Display Settings'),
105 'weight' => 8,
106 'option_values' => array(
107 'Do_not_show' => ts('Do not show breakdown, only show total -i.e ' .
108 $config->defaultCurrencySymbol . '120.00'),
109 'Inclusive' => ts('Show [tax term] inclusive price - i.e. ' .
110 $config->defaultCurrencySymbol .
111 '120.00 (includes [tax term] of ' .
112 $config->defaultCurrencySymbol . '20.00)'),
113 'Exclusive' => ts('Show [tax term] exclusive price - i.e. ' .
114 $config->defaultCurrencySymbol . '100.00 + ' .
115 $config->defaultCurrencySymbol . '20.00 [tax term]'),
fff0f449 116 ),
117 ),
353ffa53 118 ),
fff0f449 119 );
9d4da082
RK
120 parent::preProcess();
121 }
d75f2f47 122
9d4da082 123 /**
eceb18cc 124 * Build the form object.
9d4da082 125 */
00be9182 126 public function buildQuickForm() {
201b45e2 127 $htmlFields = array();
e0d19757 128 foreach ($this->_settings as $setting => $group) {
0e700ee7 129 // @todo - remove this whole loop! The parent form does this - it's just because of the werid handling
130 // of $htmlFields for this form that needs to be unwound that we have it atm.
131 // The 'basicform' has been contaminated with processing $htlFields
132 // to cater to this form - probably due to the way invoicing settings were handled as
133 // an array not a bunch of keys.
e0d19757
WA
134 $settingMetaData = civicrm_api3('setting', 'getfields', array('name' => $setting));
135 $props = $settingMetaData['values'][$setting];
136 if (isset($props['quick_form_type'])) {
137 $add = 'add' . $props['quick_form_type'];
138 if ($add == 'addElement') {
201b45e2 139 if (in_array($props['html_type'], array('checkbox', 'textarea'))) {
201b45e2
PN
140 }
141 else {
142 if ($props['html_type'] == 'select') {
143 $functionName = CRM_Utils_Array::value('name', CRM_Utils_Array::value('pseudoconstant', $props));
144 if ($functionName) {
145 $props['option_values'] = array('' => ts('- select -')) + CRM_Contribute_PseudoConstant::$functionName();
146 }
147 }
201b45e2
PN
148 }
149 }
e0d19757 150 }
201b45e2 151 $htmlFields[$setting] = ts($props['description']);
e0d19757 152 }
201b45e2 153 $this->assign('htmlFields', $htmlFields);
9d4da082
RK
154 parent::buildQuickForm();
155 }
1be9403d 156
157 /**
c490a46a 158 * Set default values for the form.
1be9403d 159 *
ce064e4f 160 * default values are retrieved from the database
1be9403d 161 */
00be9182 162 public function setDefaultValues() {
aaffa79f 163 $defaults = Civi::settings()->get('contribution_invoice_settings');
e0d19757 164 //CRM-16691: Changes made related to settings of 'CVV'.
201b45e2 165 foreach (array('cvv_backoffice_required') as $setting) {
e0d19757
WA
166 $defaults[$setting] = civicrm_api3('setting', 'getvalue',
167 array(
168 'name' => $setting,
201b45e2 169 'group' => CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME,
e0d19757
WA
170 )
171 );
172 }
1be9403d 173 return $defaults;
174 }
175
176 /**
eceb18cc 177 * Process the form after the input has been submitted and validated.
1be9403d 178 */
179 public function postProcess() {
180 // store the submitted values in an array
181 $params = $this->controller->exportValues($this->_name);
fff0f449 182 unset($params['qfKey']);
183 unset($params['entryURL']);
08ef4ddd 184 Civi::settings()->set('contribution_invoice_settings', $params);
e0d683cf
PB
185
186 // to set default value for 'Invoices / Credit Notes' checkbox on display preferences
187 $values = CRM_Core_BAO_Setting::getItem("CiviCRM Preferences");
9148c277 188 $optionValues = CRM_Core_OptionGroup::values('user_dashboard_options', FALSE, FALSE, FALSE, NULL, 'name');
e0d683cf
PB
189 $setKey = array_search('Invoices / Credit Notes', $optionValues);
190
191 if (isset($params['invoicing'])) {
192 $value = array($setKey => $optionValues[$setKey]);
9d72cede
EM
193 $setInvoice = CRM_Core_DAO::VALUE_SEPARATOR .
194 implode(CRM_Core_DAO::VALUE_SEPARATOR, array_keys($value)) .
195 CRM_Core_DAO::VALUE_SEPARATOR;
08ef4ddd 196 Civi::settings()->set('user_dashboard_options', $values['user_dashboard_options'] . $setInvoice);
e0d683cf
PB
197 }
198 else {
199 $setting = explode(CRM_Core_DAO::VALUE_SEPARATOR, substr($values['user_dashboard_options'], 1, -1));
9d72cede 200 $invoiceKey = array_search($setKey, $setting);
3ff9e824 201 if ($invoiceKey !== FALSE) {
8077138d 202 unset($setting[$invoiceKey]);
203 }
9d72cede
EM
204 $settingName = CRM_Core_DAO::VALUE_SEPARATOR .
205 implode(CRM_Core_DAO::VALUE_SEPARATOR, array_values($setting)) .
206 CRM_Core_DAO::VALUE_SEPARATOR;
08ef4ddd 207 Civi::settings()->set('user_dashboard_options', $settingName);
e0d683cf 208 }
e0d19757 209 //CRM-16691: Changes made related to settings of 'CVV'.
201b45e2 210 $settings = array_intersect_key($params, array('cvv_backoffice_required' => 1));
e0d19757 211 $result = civicrm_api3('setting', 'create', $settings);
74a7714e 212 CRM_Core_Session::setStatus(ts('Your changes have been saved.'), ts('Changes Saved'), "success");
1be9403d 213 }
96025800 214
9d4da082 215}