CRM-19427 - set deductible amount at price field option level
[civicrm-core.git] / CRM / Price / Form / Option.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
fa938177 6 | Copyright CiviCRM LLC (c) 2004-2016 |
6a488035
TO
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
fa938177 31 * @copyright CiviCRM LLC (c) 2004-2016
6a488035
TO
32 * $Id$
33 *
34 */
35
36/**
37 * form to process actions on the field aspect of Custom
38 */
39class CRM_Price_Form_Option extends CRM_Core_Form {
40
41 /**
fe482240 42 * The price field id saved to the session for an update.
6a488035
TO
43 *
44 * @var int
6a488035
TO
45 */
46 protected $_fid;
47
48 /**
100fef9d 49 * Option value id, used when editing the Option
6a488035
TO
50 *
51 * @var int
6a488035
TO
52 */
53 protected $_oid;
54
55 /**
fe482240 56 * Set variables up before form is built.
6a488035 57 *
6a488035 58 * @return void
6a488035
TO
59 */
60 public function preProcess() {
e5e8ab99 61 $this->setPageTitle(ts('Price Option'));
6a488035
TO
62 $this->_fid = CRM_Utils_Request::retrieve('fid', 'Positive',
63 $this
64 );
65 $this->_oid = CRM_Utils_Request::retrieve('oid', 'Positive',
66 $this
67 );
68 }
69
70 /**
c490a46a 71 * Set default values for the form. Note that in edit/view mode
6a488035
TO
72 * the default values are retrieved from the database
73 *
bed98343 74 * @return array|void array of default values
6a488035 75 */
00be9182 76 public function setDefaultValues() {
a4c1913d 77 if ($this->_action == CRM_Core_Action::DELETE) {
bed98343 78 return NULL;
a4c1913d 79 }
6a488035
TO
80 $defaults = array();
81
82 if (isset($this->_oid)) {
83 $params = array('id' => $this->_oid);
84
9da8dc8c 85 CRM_Price_BAO_PriceFieldValue::retrieve($params, $defaults);
6a488035
TO
86
87 // fix the display of the monetary value, CRM-4038
88 $defaults['value'] = CRM_Utils_Money::format(CRM_Utils_Array::value('value', $defaults), NULL, '%a');
89 }
90
91 $memberComponentId = CRM_Core_Component::getComponentID('CiviMember');
9da8dc8c 92 $extendComponentId = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_sid, 'extends', 'id');
6a488035
TO
93
94 if (!isset($defaults['membership_num_terms']) && $memberComponentId == $extendComponentId) {
95 $defaults['membership_num_terms'] = 1;
96 }
97 // set financial type used for price set to set default for new option
98 if (!$this->_oid) {
9da8dc8c 99 $defaults['financial_type_id'] = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_sid, 'financial_type_id', 'id');;
6a488035
TO
100 }
101 if (!isset($defaults['weight']) || !$defaults['weight']) {
102 $fieldValues = array('price_field_id' => $this->_fid);
9da8dc8c 103 $defaults['weight'] = CRM_Utils_Weight::getDefaultWeight('CRM_Price_DAO_PriceFieldValue', $fieldValues);
6a488035
TO
104 $defaults['is_active'] = 1;
105 }
106
107 return $defaults;
108 }
109
110 /**
fe482240 111 * Build the form object.
6a488035 112 *
6a488035 113 * @return void
6a488035
TO
114 */
115 public function buildQuickForm() {
e3ca0d21
E
116 if ($this->_action == CRM_Core_Action::UPDATE) {
117 $finTypeId = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $this->_oid, 'financial_type_id');
573fd305 118 CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes($financialTypes, CRM_Core_Action::UPDATE);
e3ca0d21
E
119 if (!array_key_exists($finTypeId, $financialTypes)) {
120 CRM_Core_Error::fatal(ts("You do not have permission to access this page"));
121 }
122 }
6a488035
TO
123 if ($this->_action == CRM_Core_Action::DELETE) {
124 $this->addButtons(array(
c5c263ca
AH
125 array(
126 'type' => 'next',
127 'name' => ts('Delete'),
128 ),
129 array(
130 'type' => 'cancel',
131 'name' => ts('Cancel'),
132 ),
133 ));
bed98343 134 return NULL;
6a488035
TO
135 }
136 else {
353ffa53 137 $attributes = CRM_Core_DAO::getAttribute('CRM_Price_DAO_PriceFieldValue');
6a488035
TO
138 // lets trim all the whitespace
139 $this->applyFilter('__ALL__', 'trim');
140
141 // hidden Option Id for validation use
142 $this->add('hidden', 'optionId', $this->_oid);
143
94166e28
CW
144 // Needed for i18n dialog
145 $this->assign('optionId', $this->_oid);
146
6a488035
TO
147 //hidden field ID for validation use
148 $this->add('hidden', 'fieldId', $this->_fid);
149
150 // label
151 $this->add('text', 'label', ts('Option Label'), NULL, TRUE);
152 $memberComponentId = CRM_Core_Component::getComponentID('CiviMember');
153 if ($this->_action == CRM_Core_Action::UPDATE) {
154 $this->_sid = CRM_Utils_Request::retrieve('sid', 'Positive', $this);
155 }
156 elseif ($this->_action == CRM_Core_Action::ADD ||
157 $this->_action == CRM_Core_Action::VIEW
158 ) {
9da8dc8c 159 $this->_sid = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField', $this->_fid, 'price_set_id', 'id');
6a488035 160 }
ba1dcfda 161 $this->isEvent = FALSE;
9da8dc8c 162 $extendComponentId = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_sid, 'extends', 'id');
6a488035
TO
163 $this->assign('showMember', FALSE);
164 if ($memberComponentId == $extendComponentId) {
165 $this->assign('showMember', TRUE);
166 $membershipTypes = CRM_Member_PseudoConstant::membershipType();
167 $this->add('select', 'membership_type_id', ts('Membership Type'), array(
c5c263ca
AH
168 '' => ' ',
169 ) + $membershipTypes, FALSE,
170 array('onClick' => "calculateRowValues( );"));
896bd22c 171 $this->add('text', 'membership_num_terms', ts('Number of Terms'), $attributes['membership_num_terms']);
6a488035
TO
172 }
173 else {
174 $allComponents = explode(CRM_Core_DAO::VALUE_SEPARATOR, $extendComponentId);
175 $eventComponentId = CRM_Core_Component::getComponentID('CiviEvent');
176 if (in_array($eventComponentId, $allComponents)) {
177 $this->isEvent = TRUE;
178 // count
bbb36eb8 179 $this->add('text', 'count', ts('Participant Count'));
6a488035 180 $this->addRule('count', ts('Please enter a valid Max Participants.'), 'positiveInteger');
366fe2a3 181
6a488035
TO
182 $this->add('text', 'max_value', ts('Max Participants'));
183 $this->addRule('max_value', ts('Please enter a valid Max Participants.'), 'positiveInteger');
184 }
366fe2a3 185
6a488035
TO
186 }
187 //Financial Type
188 $financialType = CRM_Financial_BAO_FinancialType::getIncomeFinancialType();
366fe2a3 189
6a488035
TO
190 if (count($financialType)) {
191 $this->assign('financialType', $financialType);
192 }
193 $this->add(
366fe2a3 194 'select',
195 'financial_type_id',
196 ts('Financial Type'),
6a488035 197 array('' => ts('- select -')) + $financialType,
ba1dcfda 198 TRUE
6a488035 199 );
366fe2a3 200
9da8dc8c 201 //CRM_Core_DAO::getFieldValue( 'CRM_Price_DAO_PriceField', $this->_fid, 'weight', 'id' );
6a488035
TO
202 // FIX ME: duplicate rule?
203 /*
e70a7fc0
TO
204 $this->addRule( 'label',
205 ts('Duplicate option label.'),
206 'optionExists',
207 array( 'CRM_Core_DAO_OptionValue', $this->_oid, $this->_ogId, 'label' ) );
208 */
6a488035 209
6a488035
TO
210 // value
211 $this->add('text', 'amount', ts('Option Amount'), NULL, TRUE);
212
213 // the above value is used directly by QF, so the value has to be have a rule
214 // please check with Lobo before u comment this
215 $this->registerRule('amount', 'callback', 'money', 'CRM_Utils_Rule');
216 $this->addRule('amount', ts('Please enter a monetary value for this field.'), 'money');
217
5afce5ad 218 $this->add('text', 'non_deductible_amount', ts('Non-deductible Amount'), NULL);
219 $this->registerRule('non_deductible_amount', 'callback', 'money', 'CRM_Utils_Rule');
220 $this->addRule('non_deductible_amount', ts('Please enter a monetary value for this field.'), 'money');
221
6a488035 222 $this->add('textarea', 'description', ts('Description'));
08b8b2d9 223 $this->add('textarea', 'help_pre', ts('Pre Option Help'));
224 $this->add('textarea', 'help_post', ts('Post Option Help'));
6a488035
TO
225
226 // weight
227 $this->add('text', 'weight', ts('Order'), NULL, TRUE);
228 $this->addRule('weight', ts('is a numeric field'), 'numeric');
229
230 // is active ?
231 $this->add('checkbox', 'is_active', ts('Active?'));
232
233 //is default
234 $this->add('checkbox', 'is_default', ts('Default'));
235
236 if ($this->_fid) {
237 //hide the default checkbox option for text field
9da8dc8c 238 $htmlType = CRM_Core_DAO::getFieldValue('CRM_Price_BAO_PriceField', $this->_fid, 'html_type');
6a488035
TO
239 $this->assign('hideDefaultOption', FALSE);
240 if ($htmlType == 'Text') {
241 $this->assign('hideDefaultOption', TRUE);
242 }
243 }
244 // add buttons
245 $this->addButtons(array(
c5c263ca
AH
246 array(
247 'type' => 'next',
248 'name' => ts('Save'),
249 ),
250 array(
251 'type' => 'cancel',
252 'name' => ts('Cancel'),
253 ),
254 ));
6a488035
TO
255
256 // if view mode pls freeze it with the done button.
257 if ($this->_action & CRM_Core_Action::VIEW) {
258 $this->freeze();
259 $this->addButtons(array(
c5c263ca
AH
260 array(
261 'type' => 'cancel',
262 'name' => ts('Done'),
263 'isDefault' => TRUE,
264 ),
265 ));
6a488035
TO
266 }
267 }
268
269 $this->addFormRule(array('CRM_Price_Form_Option', 'formRule'), $this);
270 }
271
272 /**
fe482240 273 * Global validation rules for the form.
6a488035 274 *
414c1420
TO
275 * @param array $fields
276 * Posted values of the form.
77b97be7
EM
277 *
278 * @param $files
c490a46a 279 * @param CRM_Core_Form $form
6a488035 280 *
a6c01b45
CW
281 * @return array
282 * if errors then list of errors to be posted back to the form,
6a488035 283 * true otherwise
6a488035 284 */
00be9182 285 public static function formRule($fields, $files, $form) {
6a488035 286 $errors = array();
8cc574cf 287 if (!empty($fields['count']) && !empty($fields['max_value']) &&
6a488035
TO
288 $fields['count'] > $fields['max_value']
289 ) {
290 $errors['count'] = ts('Participant count can not be greater than max participants.');
291 }
e68e1c9a 292 // CRM-16189
f7e2bf47
PN
293 try {
294 CRM_Financial_BAO_FinancialAccount::validateFinancialType($fields['financial_type_id'], $form->_fid, 'PriceField');
295 }
296 catch (CRM_Core_Exception $e) {
297 $errors['financial_type_id'] = $e->getMessage();
e68e1c9a 298 }
6a488035
TO
299 return empty($errors) ? TRUE : $errors;
300 }
301
302 /**
fe482240 303 * Process the form.
6a488035 304 *
6a488035 305 * @return void
6a488035
TO
306 */
307 public function postProcess() {
308 if ($this->_action == CRM_Core_Action::DELETE) {
309 $fieldValues = array('price_field_id' => $this->_fid);
353ffa53
TO
310 $wt = CRM_Utils_Weight::delWeight('CRM_Price_DAO_PriceFieldValue', $this->_oid, $fieldValues);
311 $label = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue',
6a488035
TO
312 $this->_oid,
313 'label', 'id'
314 );
315
9da8dc8c 316 if (CRM_Price_BAO_PriceFieldValue::del($this->_oid)) {
6a488035
TO
317 CRM_Core_Session::setStatus(ts('%1 option has been deleted.', array(1 => $label)), ts('Record Deleted'), 'success');
318 }
bed98343 319 return NULL;
6a488035
TO
320 }
321 else {
353ffa53
TO
322 $params = $ids = array();
323 $params = $this->controller->exportValues('Option');
9da8dc8c 324 $fieldLabel = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField', $this->_fid, 'label');
6a488035
TO
325
326 $params['amount'] = CRM_Utils_Rule::cleanMoney(trim($params['amount']));
327 $params['price_field_id'] = $this->_fid;
328 $params['is_default'] = CRM_Utils_Array::value('is_default', $params, FALSE);
ff33ddc8 329 $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE);
6a488035
TO
330 $ids = array();
331 if ($this->_oid) {
332 $ids['id'] = $this->_oid;
333 }
9da8dc8c 334 $optionValue = CRM_Price_BAO_PriceFieldValue::create($params, $ids);
6a488035
TO
335
336 CRM_Core_Session::setStatus(ts("The option '%1' has been saved.", array(1 => $params['label'])), ts('Value Saved'), 'success');
337 }
338 }
96025800 339
6a488035 340}