Merge pull request #13001 from JKingsnorth/core-456
[civicrm-core.git] / CRM / Admin / Form / Preferences / Contribute.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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
31 * @copyright CiviCRM LLC (c) 2004-2018
32 */
33
34 /**
35 * This class generates form components for the display preferences.
36 */
37 class CRM_Admin_Form_Preferences_Contribute extends CRM_Admin_Form_Preferences {
38 protected $_settings = array(
39 'cvv_backoffice_required' => CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME,
40 'update_contribution_on_membership_type_change' => CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME,
41 'acl_financial_type' => CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME,
42 'always_post_to_accounts_receivable' => CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME,
43 'deferred_revenue_enabled' => CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME,
44 'default_invoice_page' => CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME,
45 'invoicing' => CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME,
46 );
47
48 /**
49 * Process the form submission.
50 */
51 public function preProcess() {
52 $config = CRM_Core_Config::singleton();
53 CRM_Utils_System::setTitle(ts('CiviContribute Component Settings'));
54 $this->_varNames = array(
55 CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME => array(
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'),
83 ),
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]'),
116 ),
117 ),
118 ),
119 );
120 parent::preProcess();
121 }
122
123 /**
124 * Build the form object.
125 */
126 public function buildQuickForm() {
127 $htmlFields = array();
128 foreach ($this->_settings as $setting => $group) {
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.
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') {
139 if (in_array($props['html_type'], array('checkbox', 'textarea'))) {
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 }
148 }
149 }
150 }
151 $htmlFields[$setting] = ts($props['description']);
152 }
153 $this->assign('htmlFields', $htmlFields);
154 parent::buildQuickForm();
155 }
156
157 /**
158 * Set default values for the form.
159 *
160 * default values are retrieved from the database
161 */
162 public function setDefaultValues() {
163 $defaults = Civi::settings()->get('contribution_invoice_settings');
164 //CRM-16691: Changes made related to settings of 'CVV'.
165 foreach (array('cvv_backoffice_required') as $setting) {
166 $defaults[$setting] = civicrm_api3('setting', 'getvalue',
167 array(
168 'name' => $setting,
169 'group' => CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME,
170 )
171 );
172 }
173 return $defaults;
174 }
175
176 /**
177 * Process the form after the input has been submitted and validated.
178 */
179 public function postProcess() {
180 // store the submitted values in an array
181 $params = $this->controller->exportValues($this->_name);
182 unset($params['qfKey']);
183 unset($params['entryURL']);
184 Civi::settings()->set('contribution_invoice_settings', $params);
185
186 // to set default value for 'Invoices / Credit Notes' checkbox on display preferences
187 $values = CRM_Core_BAO_Setting::getItem("CiviCRM Preferences");
188 $optionValues = CRM_Core_OptionGroup::values('user_dashboard_options', FALSE, FALSE, FALSE, NULL, 'name');
189 $setKey = array_search('Invoices / Credit Notes', $optionValues);
190
191 if (isset($params['invoicing'])) {
192 $value = array($setKey => $optionValues[$setKey]);
193 $setInvoice = CRM_Core_DAO::VALUE_SEPARATOR .
194 implode(CRM_Core_DAO::VALUE_SEPARATOR, array_keys($value)) .
195 CRM_Core_DAO::VALUE_SEPARATOR;
196 Civi::settings()->set('user_dashboard_options', $values['user_dashboard_options'] . $setInvoice);
197 }
198 else {
199 $setting = explode(CRM_Core_DAO::VALUE_SEPARATOR, substr($values['user_dashboard_options'], 1, -1));
200 $invoiceKey = array_search($setKey, $setting);
201 if ($invoiceKey !== FALSE) {
202 unset($setting[$invoiceKey]);
203 }
204 $settingName = CRM_Core_DAO::VALUE_SEPARATOR .
205 implode(CRM_Core_DAO::VALUE_SEPARATOR, array_values($setting)) .
206 CRM_Core_DAO::VALUE_SEPARATOR;
207 Civi::settings()->set('user_dashboard_options', $settingName);
208 }
209 //CRM-16691: Changes made related to settings of 'CVV'.
210 $settings = array_intersect_key($params, array('cvv_backoffice_required' => 1));
211 $result = civicrm_api3('setting', 'create', $settings);
212 CRM_Core_Session::setStatus(ts('Your changes have been saved.'), ts('Changes Saved'), "success");
213 }
214
215 }