Merge pull request #11736 from jitendrapurohit/CRM-21815
[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 $settingMetaData = civicrm_api3('setting', 'getfields', array('name' => $setting));
130 $props = $settingMetaData['values'][$setting];
131 if (isset($props['quick_form_type'])) {
132 $add = 'add' . $props['quick_form_type'];
133 if ($add == 'addElement') {
134 if (in_array($props['html_type'], array('checkbox', 'textarea'))) {
135 $this->add($props['html_type'],
136 $setting,
137 $props['title']
138 );
139 }
140 else {
141 if ($props['html_type'] == 'select') {
142 $functionName = CRM_Utils_Array::value('name', CRM_Utils_Array::value('pseudoconstant', $props));
143 if ($functionName) {
144 $props['option_values'] = array('' => ts('- select -')) + CRM_Contribute_PseudoConstant::$functionName();
145 }
146 }
147 $this->$add(
148 $props['html_type'],
149 $setting,
150 ts($props['title']),
151 CRM_Utils_Array::value($props['html_type'] == 'select' ? 'option_values' : 'html_attributes', $props, array()),
152 $props['html_type'] == 'select' ? CRM_Utils_Array::value('html_attributes', $props) : NULL
153 );
154 }
155 }
156 elseif ($add == 'addMonthDay') {
157 $this->add('date', $setting, ts($props['title']), CRM_Core_SelectValues::date(NULL, 'M d'));
158 }
159 elseif ($add == 'addDate') {
160 $this->addDate($setting, ts($props['title']), FALSE, array('formatType' => $props['type']));
161 }
162 else {
163 $this->$add($setting, ts($props['title']));
164 }
165 }
166 $htmlFields[$setting] = ts($props['description']);
167 }
168 $this->assign('htmlFields', $htmlFields);
169 parent::buildQuickForm();
170 }
171
172 /**
173 * Set default values for the form.
174 *
175 * default values are retrieved from the database
176 */
177 public function setDefaultValues() {
178 $defaults = Civi::settings()->get('contribution_invoice_settings');
179 //CRM-16691: Changes made related to settings of 'CVV'.
180 foreach (array('cvv_backoffice_required') as $setting) {
181 $defaults[$setting] = civicrm_api3('setting', 'getvalue',
182 array(
183 'name' => $setting,
184 'group' => CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME,
185 )
186 );
187 }
188 return $defaults;
189 }
190
191 /**
192 * Process the form after the input has been submitted and validated.
193 */
194 public function postProcess() {
195 // store the submitted values in an array
196 $params = $this->controller->exportValues($this->_name);
197 unset($params['qfKey']);
198 unset($params['entryURL']);
199 Civi::settings()->set('contribution_invoice_settings', $params);
200 Civi::settings()->set('update_contribution_on_membership_type_change',
201 CRM_Utils_Array::value('update_contribution_on_membership_type_change', $params)
202 );
203
204 // to set default value for 'Invoices / Credit Notes' checkbox on display preferences
205 $values = CRM_Core_BAO_Setting::getItem("CiviCRM Preferences");
206 $optionValues = CRM_Core_OptionGroup::values('user_dashboard_options', FALSE, FALSE, FALSE, NULL, 'name');
207 $setKey = array_search('Invoices / Credit Notes', $optionValues);
208
209 if (isset($params['invoicing'])) {
210 $value = array($setKey => $optionValues[$setKey]);
211 $setInvoice = CRM_Core_DAO::VALUE_SEPARATOR .
212 implode(CRM_Core_DAO::VALUE_SEPARATOR, array_keys($value)) .
213 CRM_Core_DAO::VALUE_SEPARATOR;
214 Civi::settings()->set('user_dashboard_options', $values['user_dashboard_options'] . $setInvoice);
215 }
216 else {
217 $setting = explode(CRM_Core_DAO::VALUE_SEPARATOR, substr($values['user_dashboard_options'], 1, -1));
218 $invoiceKey = array_search($setKey, $setting);
219 if ($invoiceKey !== FALSE) {
220 unset($setting[$invoiceKey]);
221 }
222 $settingName = CRM_Core_DAO::VALUE_SEPARATOR .
223 implode(CRM_Core_DAO::VALUE_SEPARATOR, array_values($setting)) .
224 CRM_Core_DAO::VALUE_SEPARATOR;
225 Civi::settings()->set('user_dashboard_options', $settingName);
226 }
227 //CRM-16691: Changes made related to settings of 'CVV'.
228 $settings = array_intersect_key($params, array('cvv_backoffice_required' => 1));
229 $result = civicrm_api3('setting', 'create', $settings);
230 CRM_Core_Session::setStatus(ts('Your changes have been saved.'), ts('Changes Saved'), "success");
231 }
232
233 }