VAT-572 Display Invoicing functionality based on admin globle setting under CiviContr...
[civicrm-core.git] / CRM / Price / Page / Field.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 Price Fields.
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_Field extends CRM_Core_Page {
45
96f50de2
CW
46 public $useLivePageJS = TRUE;
47
6a488035
TO
48 /**
49 * The price set group id of the field
50 *
51 * @var int
52 * @access protected
53 */
54 protected $_sid;
55
56 /**
57 * The action links that we need to display for the browse screen
58 *
59 * @var array
60 * @access private
61 */
62 private static $_actionLinks;
63
64 /**
65 * The price set is reserved or not
66 *
67 * @var boolean
68 * @access protected
69 */
70 protected $_isSetReserved = false;
71
72 /**
73 * Get the action links for this page.
74 *
75 * @param null
76 *
77 * @return array array of action links that we need to display for the browse screen
78 * @access public
79 */ function &actionLinks() {
80 if (!isset(self::$_actionLinks)) {
81 // helper variable for nicer formatting
82 $deleteExtra = ts('Are you sure you want to delete this price field?');
83 self::$_actionLinks = array(
84 CRM_Core_Action::UPDATE => array(
85 'name' => ts('Edit Price Field'),
86 'url' => 'civicrm/admin/price/field',
87 'qs' => 'action=update&reset=1&sid=%%sid%%&fid=%%fid%%',
88 'title' => ts('Edit Price'),
89 ),
90 CRM_Core_Action::PREVIEW => array(
91 'name' => ts('Preview Field'),
92 'url' => 'civicrm/admin/price/field',
93 'qs' => 'action=preview&reset=1&sid=%%sid%%&fid=%%fid%%',
94 'title' => ts('Preview Price'),
95 ),
96 CRM_Core_Action::DISABLE => array(
97 'name' => ts('Disable'),
4d17a233 98 'ref' => 'crm-enable-disable',
6a488035
TO
99 'title' => ts('Disable Price'),
100 ),
101 CRM_Core_Action::ENABLE => array(
102 'name' => ts('Enable'),
4d17a233 103 'ref' => 'crm-enable-disable',
6a488035
TO
104 'title' => ts('Enable Price'),
105 ),
106 CRM_Core_Action::DELETE => array(
107 'name' => ts('Delete'),
108 'url' => 'civicrm/admin/price/field',
109 'qs' => 'action=delete&reset=1&sid=%%sid%%&fid=%%fid%%',
110 'title' => ts('Delete Price'),
111 'extra' => 'onclick = "return confirm(\'' . $deleteExtra . '\');"',
112 ),
113 );
114 }
115 return self::$_actionLinks;
116 }
117
118 /**
119 * Browse all price set fields.
120 *
121 * @param null
122 *
123 * @return void
124 * @access public
125 */
126 function browse() {
127 $priceField = array();
9da8dc8c 128 $priceFieldBAO = new CRM_Price_BAO_PriceField();
6a488035
TO
129
130 // fkey is sid
131 $priceFieldBAO->price_set_id = $this->_sid;
132 $priceFieldBAO->orderBy('weight, label');
133 $priceFieldBAO->find();
44c8822b 134
03b412ae
PB
135 // display taxTerm for priceFields
136 $invoiceSettings = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME,'contribution_invoice_settings');
137 $taxTerm = CRM_Utils_Array::value('tax_term', $invoiceSettings);
138 $invoicing = CRM_Utils_Array::value('invoicing', $invoiceSettings);
a32709be
PB
139 $getTaxDetails = FALSE;
140 $taxRate = CRM_Core_PseudoConstant::getTaxRates();
6a488035
TO
141 while ($priceFieldBAO->fetch()) {
142 $priceField[$priceFieldBAO->id] = array();
143 CRM_Core_DAO::storeValues($priceFieldBAO, $priceField[$priceFieldBAO->id]);
144
145 // get price if it's a text field
146 if ($priceFieldBAO->html_type == 'Text') {
147 $optionValues = array();
148 $params = array('price_field_id' => $priceFieldBAO->id);
149
9da8dc8c 150 CRM_Price_BAO_PriceFieldValue::retrieve($params, $optionValues);
6a488035 151
a32709be 152 $financialTypeId = $optionValues['financial_type_id'];
6a488035 153 $priceField[$priceFieldBAO->id]['price'] = CRM_Utils_Array::value('amount', $optionValues);
03b412ae 154 if ($invoicing && isset($taxRate[$financialTypeId])) {
a32709be
PB
155 $priceField[$priceFieldBAO->id]['tax_rate'] = $taxRate[$financialTypeId];
156 $getTaxDetails = TRUE;
157 }
158 if (isset($priceField[$priceFieldBAO->id]['tax_rate'])) {
159 $taxAmount = CRM_Contribute_BAO_Contribution_Utils::calculateTaxAmount($priceField[$priceFieldBAO->id]['price'], $priceField[$priceFieldBAO->id]['tax_rate']);
160 $priceField[$priceFieldBAO->id]['tax_amount'] = $taxAmount['tax_amount'];
161 }
6a488035 162 }
44c8822b 163
6a488035
TO
164 $action = array_sum(array_keys($this->actionLinks()));
165
166 if ($this->_isSetReserved) {
167 $action -= CRM_Core_Action::UPDATE + CRM_Core_Action::DELETE + CRM_Core_Action::ENABLE + CRM_Core_Action::DISABLE;
168 }
169 else {
170 if ($priceFieldBAO->is_active) {
171 $action -= CRM_Core_Action::ENABLE;
172 }
173 else {
174 $action -= CRM_Core_Action::DISABLE;
175 }
176 }
177
178 if ($priceFieldBAO->active_on == '0000-00-00 00:00:00') {
179 $priceField[$priceFieldBAO->id]['active_on'] = '';
180 }
181
182 if ($priceFieldBAO->expire_on == '0000-00-00 00:00:00') {
183 $priceField[$priceFieldBAO->id]['expire_on'] = '';
184 }
185
186 // need to translate html types from the db
9da8dc8c 187 $htmlTypes = CRM_Price_BAO_PriceField::htmlTypes();
6a488035
TO
188 $priceField[$priceFieldBAO->id]['html_type'] = $htmlTypes[$priceField[$priceFieldBAO->id]['html_type']];
189 $priceField[$priceFieldBAO->id]['order'] = $priceField[$priceFieldBAO->id]['weight'];
87dab4a4
AH
190 $priceField[$priceFieldBAO->id]['action'] = CRM_Core_Action::formLink(
191 self::actionLinks(),
192 $action,
6a488035
TO
193 array(
194 'fid' => $priceFieldBAO->id,
195 'sid' => $this->_sid,
87dab4a4
AH
196 ),
197 ts('more'),
198 FALSE,
199 'priceField.row.actions',
200 'PriceField',
201 $priceFieldBAO->id
6a488035 202 );
03b412ae 203 $this->assign('taxTerm', $taxTerm);
a32709be 204 $this->assign('getTaxDetails', $getTaxDetails);
6a488035
TO
205 }
206
207 $returnURL = CRM_Utils_System::url('civicrm/admin/price/field', "reset=1&action=browse&sid={$this->_sid}");
208 $filter = "price_set_id = {$this->_sid}";
9da8dc8c 209 CRM_Utils_Weight::addOrder($priceField, 'CRM_Price_DAO_PriceField',
6a488035
TO
210 'id', $returnURL, $filter
211 );
212 $this->assign('priceField', $priceField);
213 }
214
215 /**
216 * edit price data.
217 *
218 * editing would involved modifying existing fields + adding data to new fields.
219 *
220 * @param string $action the action to be invoked
221
222 *
223 * @return void
224 * @access public
225 */
226 function edit($action) {
227 // create a simple controller for editing price data
228 $controller = new CRM_Core_Controller_Simple('CRM_Price_Form_Field', ts('Price Field'), $action);
229
230 // set the userContext stack
231 $session = CRM_Core_Session::singleton();
232 $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/price/field', 'reset=1&action=browse&sid=' . $this->_sid));
233
234 $controller->set('sid', $this->_sid);
235 $controller->setEmbedded(TRUE);
236 $controller->process();
237 $controller->run();
238 }
239
240 /**
241 * Run the page.
242 *
243 * This method is called after the page is created. It checks for the
244 * type of action and executes that action.
245 *
246 * @param null
247 *
248 * @return void
249 * @access public
250 */
251 function run() {
252
253 // get the group id
254 $this->_sid = CRM_Utils_Request::retrieve('sid', 'Positive',
255 $this
256 );
257 $fid = CRM_Utils_Request::retrieve('fid', 'Positive',
258 $this, FALSE, 0
259 );
260 $action = CRM_Utils_Request::retrieve('action', 'String',
261 // default to 'browse'
262 $this, FALSE, 'browse'
263 );
264
265 if ($this->_sid) {
9da8dc8c 266 $usedBy = CRM_Price_BAO_PriceSet::getUsedBy($this->_sid);
6a488035 267 $this->assign('usedBy', $usedBy);
9da8dc8c 268 $this->_isSetReserved= CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_sid, 'is_reserved');
6a488035 269 $this->assign('isReserved', $this->_isSetReserved);
44c8822b 270
9da8dc8c 271 CRM_Price_BAO_PriceSet::checkPermission($this->_sid);
6a488035
TO
272 $comps = array(
273 'Event' => 'civicrm_event',
274 'Contribution' => 'civicrm_contribution_page',
c34e4bb4 275 'EventTemplate' => 'civicrm_event_template'
6a488035
TO
276 );
277 $priceSetContexts = array();
278 foreach ($comps as $name => $table) {
279 if (array_key_exists($table, $usedBy)) {
280 $priceSetContexts[] = $name;
281 }
282 }
283 $this->assign('contexts', $priceSetContexts);
284 }
285
286 if ($action & (CRM_Core_Action::DELETE) && !$this->_isSetReserved) {
287 if (empty($usedBy)) {
288 // prompt to delete
289 $session = CRM_Core_Session::singleton();
290 $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/price/field', 'reset=1&action=browse&sid=' . $this->_sid));
291 $controller = new CRM_Core_Controller_Simple('CRM_Price_Form_DeleteField', 'Delete Price Field', '');
292 $controller->set('fid', $fid);
293 $controller->setEmbedded(TRUE);
294 $controller->process();
295 $controller->run();
296 }
297 else {
298 // add breadcrumb
299 $url = CRM_Utils_System::url('civicrm/admin/price/field', 'reset=1');
300 CRM_Utils_System::appendBreadCrumb(ts('Price'),
301 $url
302 );
9da8dc8c 303 $this->assign('usedPriceSetTitle', CRM_Price_BAO_PriceField::getTitle($fid));
6a488035
TO
304 }
305 }
306
307 if ($this->_sid) {
9da8dc8c 308 $groupTitle = CRM_Price_BAO_PriceSet::getTitle($this->_sid);
6a488035
TO
309 $this->assign('sid', $this->_sid);
310 $this->assign('groupTitle', $groupTitle);
311 CRM_Utils_System::setTitle(ts('%1 - Price Fields', array(1 => $groupTitle)));
312 }
313
314 // assign vars to templates
315 $this->assign('action', $action);
316
317 // what action to take ?
318 if ($action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD) && !$this->_isSetReserved) {
319 // no browse for edit/update/view
320 $this->edit($action);
321 }
322 elseif ($action & CRM_Core_Action::PREVIEW) {
323 $this->preview($fid);
324 }
325 else {
326 $this->browse();
327 }
328
329 // Call the parents run method
330 return parent::run();
331 }
332
333 /**
334 * Preview price field
335 *
77b97be7
EM
336 * @param $fid
337 *
338 * @internal param int $id price field id
6a488035
TO
339 *
340 * @return void
341 * @access public
342 */
343 function preview($fid) {
344 $controller = new CRM_Core_Controller_Simple('CRM_Price_Form_Preview', ts('Preview Form Field'), CRM_Core_Action::PREVIEW);
345 $session = CRM_Core_Session::singleton();
346 $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/price/field', 'reset=1&action=browse&sid=' . $this->_sid));
347 $controller->set('fieldId', $fid);
348 $controller->set('groupId', $this->_sid);
349 $controller->setEmbedded(TRUE);
350 $controller->process();
351 $controller->run();
352 }
353}
354