VAT-572 Display Invoicing functionality based on admin globle setting under CiviContr...
[civicrm-core.git] / CRM / Price / Page / Option.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
06b69b18 4 | CiviCRM version 4.5 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35
36/**
37 * Create a page for displaying Custom Options.
38 *
39 * Heart of this class is the run method which checks
40 * for action type and then displays the appropriate
41 * page.
42 *
43 */
44class CRM_Price_Page_Option extends CRM_Core_Page {
45
96f50de2
CW
46 public $useLivePageJS = TRUE;
47
6a488035
TO
48 /**
49 * The field id of the option
50 *
51 * @var int
52 * @access protected
53 */
54 protected $_fid;
55
56 /**
57 * The field id of the option
58 *
59 * @var int
60 * @access protected
61 */
62 protected $_sid;
63
64 /**
65 * The price set is reserved or not
66 *
67 * @var boolean
68 * @access protected
69 */
70 protected $_isSetReserved = false;
366fe2a3 71
6a488035
TO
72 /**
73 * The action links that we need to display for the browse screen
74 *
75 * @var array
76 * @access private
77 */
78 private static $_actionLinks;
79
80 /**
81 * Get the action links for this page.
82 *
83 * @param null
84 *
85 * @return array array of action links that we need to display for the browse screen
86 * @access public
87 */ function &actionLinks() {
88 if (!isset(self::$_actionLinks)) {
89 self::$_actionLinks = array(
90 CRM_Core_Action::UPDATE => array(
91 'name' => ts('Edit Option'),
92 'url' => 'civicrm/admin/price/field/option',
93 'qs' => 'reset=1&action=update&oid=%%oid%%&fid=%%fid%%&sid=%%sid%%',
94 'title' => ts('Edit Price Option'),
95 ),
96 CRM_Core_Action::VIEW => array(
97 'name' => ts('View'),
98 'url' => 'civicrm/admin/price/field/option',
99 'qs' => 'action=view&oid=%%oid%%',
100 'title' => ts('View Price Option'),
101 ),
102 CRM_Core_Action::DISABLE => array(
103 'name' => ts('Disable'),
4d17a233 104 'ref' => 'crm-enable-disable',
6a488035
TO
105 'title' => ts('Disable Price Option'),
106 ),
107 CRM_Core_Action::ENABLE => array(
108 'name' => ts('Enable'),
4d17a233 109 'ref' => 'crm-enable-disable',
6a488035
TO
110 'title' => ts('Enable Price Option'),
111 ),
112 CRM_Core_Action::DELETE => array(
113 'name' => ts('Delete'),
114 'url' => 'civicrm/admin/price/field/option',
115 'qs' => 'action=delete&oid=%%oid%%',
116 'title' => ts('Disable Price Option'),
117 ),
118 );
119 }
120 return self::$_actionLinks;
121 }
122
123 /**
124 * Browse all price fields.
125 *
126 * @param null
127 *
128 * @return void
129 * @access public
130 */
131 function browse() {
132 $customOption = array();
9da8dc8c 133 CRM_Price_BAO_PriceFieldValue::getValues($this->_fid, $customOption);
6a488035
TO
134 $config = CRM_Core_Config::singleton();
135 $financialType = CRM_Contribute_PseudoConstant::financialType();
a32709be 136 $taxRate = CRM_Core_PseudoConstant::getTaxRates();
03b412ae
PB
137 // display taxTerm for priceFields
138 $invoiceSettings = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME,'contribution_invoice_settings');
139 $taxTerm = CRM_Utils_Array::value('tax_term', $invoiceSettings);
140 $invoicing = CRM_Utils_Array::value('invoicing', $invoiceSettings);
a32709be 141 $getTaxDetails = FALSE;
6a488035
TO
142 foreach ($customOption as $id => $values) {
143 $action = array_sum(array_keys($this->actionLinks()));
a32709be
PB
144 // Adding the required fields in the array
145 if (isset($taxRate[$values['financial_type_id']])) {
146 $customOption[$id]['tax_rate'] = $taxRate[$values['financial_type_id']];
03b412ae 147 if ($invoicing && isset($customOption[$id]['tax_rate'])) {
a32709be
PB
148 $getTaxDetails = TRUE;
149 }
150 $taxAmount = CRM_Contribute_BAO_Contribution_Utils::calculateTaxAmount($customOption[$id]['amount'], $customOption[$id]['tax_rate']);
151 $customOption[$id]['tax_amount'] = $taxAmount['tax_amount'];
152 }
a7488080 153 if (!empty($values['financial_type_id'])){
6a488035
TO
154 $customOption[$id]['financial_type_id'] = $financialType[$values['financial_type_id']];
155 }
156 // update enable/disable links depending on price_field properties.
157 if ($this->_isSetReserved) {
158 $action -= CRM_Core_Action::UPDATE + CRM_Core_Action::DELETE + CRM_Core_Action::DISABLE + CRM_Core_Action::ENABLE;
159 }
160 else {
161 if ($values['is_active']) {
162 $action -= CRM_Core_Action::ENABLE;
163 }
164 else {
165 $action -= CRM_Core_Action::DISABLE;
166 }
167 }
a7488080 168 if (!empty($customOption[$id]['is_default'])) {
6a488035
TO
169 $customOption[$id]['is_default'] = '<img src="' . $config->resourceBase . 'i/check.gif" />';
170 }
171 else {
172 $customOption[$id]['is_default'] = '';
173 }
174 $customOption[$id]['order'] = $customOption[$id]['weight'];
175 $customOption[$id]['action'] = CRM_Core_Action::formLink(self::actionLinks(), $action,
176 array(
177 'oid' => $id,
178 'fid' => $this->_fid,
179 'sid' => $this->_sid,
87dab4a4
AH
180 ),
181 ts('more'),
182 FALSE,
183 'priceFieldValue.row.actions',
184 'PriceFieldValue',
185 $id
6a488035
TO
186 );
187 }
188 // Add order changing widget to selector
189 $returnURL = CRM_Utils_System::url('civicrm/admin/price/field/option', "action=browse&reset=1&fid={$this->_fid}&sid={$this->_sid}");
190 $filter = "price_field_id = {$this->_fid}";
9da8dc8c 191 CRM_Utils_Weight::addOrder($customOption, 'CRM_Price_DAO_PriceFieldValue',
6a488035
TO
192 'id', $returnURL, $filter
193 );
194
03b412ae 195 $this->assign('taxTerm', $taxTerm);
a32709be 196 $this->assign('getTaxDetails', $getTaxDetails);
6a488035
TO
197 $this->assign('customOption', $customOption);
198 $this->assign('sid', $this->_sid);
199 }
200
201 /**
202 * edit custom Option.
203 *
204 * editing would involved modifying existing fields + adding data to new fields.
205 *
206 * @param string $action the action to be invoked
207 *
208 * @return void
209 * @access public
210 */
211 function edit($action) {
212 $oid = CRM_Utils_Request::retrieve('oid', 'Positive',
213 $this, FALSE, 0
214 );
215 $params = array();
216 if ($oid) {
217 $params['oid'] = $oid;
9da8dc8c 218 $sid = CRM_Price_BAO_PriceSet::getSetId($params);
6a488035 219
9da8dc8c 220 $usedBy = CRM_Price_BAO_PriceSet::getUsedBy($sid);
6a488035
TO
221 }
222 // set the userContext stack
223 $session = CRM_Core_Session::singleton();
224
225 $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/price/field/option',
226 "reset=1&action=browse&fid={$this->_fid}&sid={$this->_sid}"
227 ));
228 $controller = new CRM_Core_Controller_Simple('CRM_Price_Form_Option', ts('Price Field Option'), $action);
229 $controller->set('fid', $this->_fid);
230 $controller->setEmbedded(TRUE);
231 $controller->process();
232 $controller->run();
233
234
235 $this->browse();
236
237 if ($action & CRM_Core_Action::DELETE) {
238 // add breadcrumb
239 $url = CRM_Utils_System::url('civicrm/admin/price/field/option', 'reset=1');
240 CRM_Utils_System::appendBreadCrumb(ts('Price Option'),
241 $url
242 );
9da8dc8c 243 $this->assign('usedPriceSetTitle', CRM_Price_BAO_PriceFieldValue::getOptionLabel($oid));
6a488035
TO
244 $this->assign('usedBy', $usedBy);
245 $comps = array(
246 "Event" => "civicrm_event",
247 "Contribution" => "civicrm_contribution_page",
248 );
249 $priceSetContexts = array();
250 foreach ($comps as $name => $table) {
251 if (array_key_exists($table, $usedBy)) {
252 $priceSetContexts[] = $name;
253 }
254 }
255 $this->assign('contexts', $priceSetContexts);
256 }
257 }
258
259 /**
260 * Run the page.
261 *
262 * This method is called after the page is created. It checks for the
263 * type of action and executes that action.
264 *
265 * @param null
266 *
267 * @return void
268 * @access public
269 */
270 function run() {
271
272 // get the field id
273 $this->_fid = CRM_Utils_Request::retrieve('fid', 'Positive',
274 $this, FALSE, 0
275 );
276 //get the price set id
277 if (!$this->_sid) {
278 $this->_sid = CRM_Utils_Request::retrieve('sid', 'Positive', $this);
279 }
280
281 if ($this->_sid) {
9da8dc8c 282 CRM_Price_BAO_PriceSet::checkPermission($this->_sid);
283 $this->_isSetReserved= CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_sid, 'is_reserved');
6a488035
TO
284 $this->assign('isReserved', $this->_isSetReserved);
285 }
286 //as url contain $sid so append breadcrumb dynamically.
287 $breadcrumb = array(array('title' => ts('Price Fields'),
288 'url' => CRM_Utils_System::url('civicrm/admin/price/field', 'reset=1&sid=' . $this->_sid),
289 ));
290 CRM_Utils_System::appendBreadCrumb($breadcrumb);
291
292 if ($this->_fid) {
9da8dc8c 293 $fieldTitle = CRM_Price_BAO_PriceField::getTitle($this->_fid);
6a488035
TO
294 $this->assign('fid', $this->_fid);
295 $this->assign('fieldTitle', $fieldTitle);
296 CRM_Utils_System::setTitle(ts('%1 - Price Options', array(1 => $fieldTitle)));
297
9da8dc8c 298 $htmlType = CRM_Core_DAO::getFieldValue('CRM_Price_BAO_PriceField', $this->_fid, 'html_type');
6a488035
TO
299 $this->assign('addMoreFields', TRUE);
300 //for text price field only single option present
301 if ($htmlType == 'Text') {
302 $this->assign('addMoreFields', FALSE);
303 }
304 }
305
306 // get the requested action
307 $action = CRM_Utils_Request::retrieve('action', 'String',
308 // default to 'browse'
309 $this, FALSE, 'browse'
310 );
311
312 // assign vars to templates
313 $this->assign('action', $action);
314
315 $oid = CRM_Utils_Request::retrieve('oid', 'Positive',
316 $this, FALSE, 0
317 );
318 // what action to take ?
319 if ($action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD |
320 CRM_Core_Action::VIEW | CRM_Core_Action::DELETE
321 ) && !$this->_isSetReserved) {
322 // no browse for edit/update/view
323 $this->edit($action);
324 }
325 else {
326 $this->browse();
327 }
328 // Call the parents run method
329 return parent::run();
330 }
331}
332