dolibarr 21.0.0-alpha
dynamic_prices.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2015 Ion Agorria <ion@agorria.com>
3 * Copyright (C) 2023 Frédéric France <frederic.france@netlogic.fr>
4 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
26// Load Dolibarr environment
27require '../../main.inc.php';
28require_once DOL_DOCUMENT_ROOT.'/core/lib/product.lib.php';
29require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.product.class.php';
30require_once DOL_DOCUMENT_ROOT.'/product/dynamic_price/class/price_global_variable.class.php';
31require_once DOL_DOCUMENT_ROOT.'/product/dynamic_price/class/price_global_variable_updater.class.php';
32
33// Load translation files required by the page
34$langs->load("products");
35
36$id = GETPOSTINT('id');
37$action = GETPOST('action', 'aZ09');
38$save = GETPOST('save', 'alpha');
39$cancel = GETPOST('cancel', 'alpha');
40$selection = GETPOSTINT('selection');
41
42// Security check
43if (!$user->admin) {
45}
46
47//Objects
48$price_globals = new PriceGlobalVariable($db);
49if ($action == 'edit_variable') {
50 $res = $price_globals->fetch($selection);
51 if ($res < 1) {
52 setEventMessages($price_globals->error, $price_globals->errors, 'errors');
53 }
54}
55$price_updaters = new PriceGlobalVariableUpdater($db);
56if ($action == 'edit_updater') {
57 $res = $price_updaters->fetch($selection);
58 if ($res < 1) {
59 setEventMessages($price_updaters->error, $price_updaters->errors, 'errors');
60 }
61}
62
63
64/*
65 * Actions
66 */
67
68if (!empty($action) && empty($cancel)) {
69 //Global variable actions
70 if ($action == 'create_variable' || $action == 'edit_variable') {
71 $price_globals->code = GETPOSTISSET('code') ? GETPOST('code', 'alpha') : $price_globals->code;
72 $price_globals->description = GETPOSTISSET('description') ? GETPOST('description', 'restricthtml') : $price_globals->description;
73 $price_globals->value = GETPOSTISSET('value') ? GETPOSTINT('value') : $price_globals->value;
74 //Check if record already exists only when saving
75 if (!empty($save)) {
76 foreach ($price_globals->listGlobalVariables() as $entry) {
77 if ($price_globals->id != $entry->id && dol_strtolower($price_globals->code) == dol_strtolower($entry->code)) {
78 setEventMessages($langs->trans("ErrorRecordAlreadyExists"), null, 'errors');
79 $save = null;
80 }
81 }
82 }
83 }
84 if ($action == 'create_variable' && !empty($save)) {
85 $res = $price_globals->create($user);
86 if ($res > 0) {
87 $action = '';
88 } else {
89 setEventMessages($price_globals->error, $price_globals->errors, 'errors');
90 }
91 } elseif ($action == 'edit_variable' && !empty($save)) {
92 $res = $price_globals->update($user);
93 if ($res > 0) {
94 $action = '';
95 } else {
96 setEventMessages($price_globals->error, $price_globals->errors, 'errors');
97 }
98 } elseif ($action == 'delete_variable') {
99 $res = $price_globals->delete($selection, $user);
100 if ($res > 0) {
101 $action = '';
102 } else {
103 setEventMessages($price_globals->error, $price_globals->errors, 'errors');
104 }
105 }
106
107 //Updaters actions
108 if ($action == 'create_updater' || $action == 'edit_updater') {
109 $price_updaters->type = GETPOSTISSET('type') ? GETPOSTINT('type') : $price_updaters->type;
110 $price_updaters->description = GETPOSTISSET('description') ? GETPOST('description', 'restricthtml') : $price_updaters->description;
111 $price_updaters->parameters = GETPOSTISSET('parameters') ? GETPOST('parameters', 'alphanohtml') : $price_updaters->parameters;
112 $price_updaters->fk_variable = GETPOSTISSET('fk_variable') ? GETPOSTINT('fk_variable') : $price_updaters->fk_variable;
113 $price_updaters->update_interval = GETPOSTISSET('update_interval') ? GETPOSTINT('update_interval') : $price_updaters->update_interval;
114 }
115 if ($action == 'create_updater' && !empty($save)) {
116 //Verify if process() works
117 $res = $price_updaters->process();
118 if ($res > 0) {
119 $res = $price_updaters->create($user);
120 }
121 if ($res > 0) {
122 $action = '';
123 } else {
124 setEventMessages($price_updaters->error, $price_updaters->errors, 'errors');
125 }
126 } elseif ($action == 'edit_updater' && !empty($save)) {
127 //Verify if process() works
128 $res = $price_updaters->process();
129 if ($res > 0) {
130 $res = $price_updaters->update($user);
131 }
132 if ($res > 0) {
133 $action = '';
134 } else {
135 setEventMessages($price_updaters->error, $price_updaters->errors, 'errors');
136 }
137 } elseif ($action == 'delete_updater') {
138 $res = $price_updaters->delete($selection, $user);
139 if ($res > 0) {
140 $action = '';
141 } else {
142 setEventMessages($price_updaters->error, $price_updaters->errors, 'errors');
143 }
144 }
145} elseif (!empty($cancel)) {
146 $action = '';
147}
148
149
150/*
151 * View
152 */
153
154$form = new Form($db);
155
156llxHeader("", "", $langs->trans("DynamicPrice"), '', 0, 0, '', '', '', 'mod-product page-admin_dynamic_prices');
157
158$linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
159print load_fiche_titre($langs->trans("DynamicPriceConfiguration"), $linkback, 'title_setup');
160
161print '<span class="opacitymedium">'.$langs->trans("DynamicPriceDesc").'</span><br>';
162print '<br>';
163
164//Global variables table
165if ($action != 'create_updater' && $action != 'edit_updater') {
166 print load_fiche_titre($langs->trans("GlobalVariables"), '', '');
167
168 print '<table summary="listofattributes" class="noborder centpercent">';
169 print '<tr class="liste_titre">';
170 print '<td>'.$langs->trans("Variable").'</td>';
171 print '<td>'.$langs->trans("Description").'</td>';
172 print '<td>'.$langs->trans("Value").'</td>';
173 print '<td width="80">&nbsp;</td>'; //Space for buttons
174 print '</tr>';
175
176 $arrayglobalvars = $price_globals->listGlobalVariables();
177 if (!empty($arrayglobalvars)) {
178 foreach ($arrayglobalvars as $i => $entry) {
179 print '<tr class="oddeven">';
180 print '<td>'.$entry->code.'</td>';
181 print '<td>'.$entry->description.'</td>';
182 print '<td>'.price($entry->value).'</td>';
183 print '<td class="right"><a class="editfielda paddingrightonly" href="'.$_SERVER["PHP_SELF"].'?action=edit_variable&token='.newToken().'&selection='.$entry->id.'">'.img_edit().'</a> &nbsp;';
184 print '<a href="'.$_SERVER["PHP_SELF"].'?action=delete_variable&token='.newToken().'&selection='.$entry->id.'">'.img_delete().'</a></td>';
185 print '</tr>';
186 }
187 } else {
188 print '<tr><td colspan="4"><span class="opacitymedium">';
189 print $langs->trans("None");
190 print '</span></td></tr>';
191 }
192 print '</table>';
193
194 if (empty($action)) {
195 /*
196 * Action bar
197 */
198 print '<div class="tabsAction">';
199 print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?action=create_variable">'.$langs->trans("AddVariable").'</a>';
200 print '</div>';
201 //Separator is only need for updaters table is showed after buttons
202 print '<br><br>';
203 }
204}
205
206//Global variable editor
207if ($action == 'create_variable' || $action == 'edit_variable') {
208 //Form
209 print '<form action="'.$_SERVER["PHP_SELF"].'" method="post">';
210 print '<input type="hidden" name="token" value="'.newToken().'">';
211 print '<input type="hidden" name="action" value="'.$action.'">';
212 print '<input type="hidden" name="selection" value="'.$selection.'">';
213
214 //Table
215 print '<br><table summary="listofattributes" class="border centpercent">';
216 //Code
217 print '<tr>';
218 print '<td class="fieldrequired">'.$langs->trans("Variable").'</td>';
219 print '<td class="valeur"><input type="text" name="code" class="minwidth100" value="'.(empty($price_globals->code) ? '' : $price_globals->code).'"></td>';
220 print '</tr>';
221 //Description
222 print '<tr>';
223 print '<td>'.$langs->trans("Description").'</td>';
224 print '<td class="valeur"><input type="text" name="description" class="minwidth200" value="'.(empty($price_globals->description) ? '' : $price_globals->description).'"></td>';
225 print '</tr>';
226 //Value
227 print '<tr>';
228 print '<td class="fieldrequired">'.$langs->trans("Value").'</td>';
229 print '<td class="valeur"><input type="text" name="value" class="minwidth100" value="'.(empty($price_globals->value) ? '' : $price_globals->value).'"></td>';
230 print '</tr>';
231 print '</table>';
232
233 //Form Buttons
234 print $form->buttonsSaveCancel();
235
236 print '</form>';
237}
238
239// Updaters table
240if ($action != 'create_variable' && $action != 'edit_variable') {
241 print load_fiche_titre($langs->trans("GlobalVariableUpdaters"), '', '');
242
243 print '<table summary="listofattributes" class="noborder centpercent">';
244 print '<tr class="liste_titre">';
245 print '<td>'.$langs->trans("VariableToUpdate").'</td>';
246 print '<td>'.$langs->trans("Description").'</td>';
247 print '<td>'.$langs->trans("Type").'</td>';
248 print '<td>'.$langs->trans("Parameters").'</td>';
249 print '<td>'.$langs->trans("UpdateInterval").'</td>';
250 print '<td>'.$langs->trans("LastUpdated").'</td>';
251 print '<td width="80">&nbsp;</td>'; //Space for buttons
252 print '</tr>';
253
254 $arraypriceupdaters = $price_updaters->listUpdaters();
255 if (!empty($arraypriceupdaters) && is_array($arraypriceupdaters)) {
256 foreach ($arraypriceupdaters as $i => $entry) {
257 $code = "";
258 if ($entry->fk_variable > 0) {
259 $res = $price_globals->fetch($entry->fk_variable);
260 if ($res > 0) {
261 $code = $price_globals->code;
262 }
263 }
264 print '<tr>';
265 print '<td>'.$code.'</td>';
266 print '<td>'.$entry->description.'</td>';
267 print '<td>'.$langs->trans("GlobalVariableUpdaterType".$entry->type).'</td>';
268 print '<td style="max-width: 250px; word-wrap: break-word; white-space: pre-wrap;">'.$entry->parameters.'</td>';
269 print '<td>'.$entry->update_interval.'</td>';
270 print '<td>'.$entry->getLastUpdated().'</td>';
271 print '<td class="right"><a class="editfielda paddingrightonly" href="'.$_SERVER["PHP_SELF"].'?action=edit_updater&selection='.$entry->id.'">'.img_edit().'</a> &nbsp;';
272 print '<a href="'.$_SERVER["PHP_SELF"].'?action=delete_updater&token='.newToken().'&selection='.$entry->id.'">'.img_delete().'</a></td>';
273 print '</tr>';
274 }
275 } else {
276 print '<tr><td colspan="7"><span class="opacitymedium">';
277 print $langs->trans("None");
278 print '</span></td></tr>';
279 }
280 print '</table>';
281
282 if (empty($action)) {
283 /*
284 * Action bar
285 */
286 print '<div class="tabsAction">';
287 print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?action=create_updater&token='.newToken().'">'.$langs->trans("AddUpdater").'</a>';
288 print '</div>';
289 }
290}
291
292//Updater editor
293if ($action == 'create_updater' || $action == 'edit_updater') {
294 //Form
295 print '<form id="updaterform" action="'.$_SERVER["PHP_SELF"].'" method="post">';
296 print '<input type="hidden" name="token" value="'.newToken().'">';
297 print '<input type="hidden" name="action" value="'.$action.'">';
298 print '<input type="hidden" name="selection" value="'.$selection.'">';
299
300 //Table
301 print '<br><table summary="listofattributes" class="border centpercent">';
302 //Code
303 print '<tr>';
304 print '<td class="fieldrequired">'.$langs->trans("VariableToUpdate").'</td><td>';
305 $globals_list = array();
306 foreach ($price_globals->listGlobalVariables() as $entry) {
307 $globals_list[$entry->id] = $entry->code;
308 }
309 print $form->selectarray('fk_variable', $globals_list, (empty($price_updaters->fk_variable) ? 0 : $price_updaters->fk_variable));
310 print '</td></tr>';
311 //Description
312 print '<tr>';
313 print '<td>'.$langs->trans("Description").'</td>';
314 print '<td class="valeur"><input type="text" name="description" class="minwidth200" value="'.(empty($price_updaters->description) ? '' : $price_updaters->description).'"></td>';
315 print '</tr>';
316 //Type
317 print '<tr>';
318 print '<td class="fieldrequired">'.$langs->trans("Type").'</td><td>';
319 $type = empty($price_updaters->type) ? 0 : $price_updaters->type;
320 $type_list = array();
321 foreach ($price_updaters->types as $val) {
322 $type_list[$val] = $langs->trans("GlobalVariableUpdaterType".$val);
323 }
324 print $form->selectarray('type', $type_list, $type);
325 // This code submits form when type is changed
326 print '<script type="text/javascript">
327 jQuery(document).ready(run);
328 function run() {
329 jQuery("#type").change(on_change);
330 }
331 function on_change() {
332 jQuery("#updaterform").submit();
333 }
334 </script>';
335 print '</td></tr>';
336 //Parameters
337 print '<tr>';
338 $help = $langs->trans("GlobalVariableUpdaterHelp".$type).'<br><b>'.$langs->trans("GlobalVariableUpdaterHelpFormat".$type).'</b>';
339 print '<td class="fieldrequired">'.$form->textwithpicto($langs->trans("Parameters"), $help, 1).'</td><td>';
340 require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
341 $doleditor = new DolEditor('parameters', empty($price_updaters->parameters) ? '' : $price_updaters->parameters, '', 300, '', '', false, false, false, ROWS_8, '90%');
342 $doleditor->Create();
343 print '</td></tr>';
344 print '</tr>';
345 //Interval
346 print '<tr>';
347 print '<td class="fieldrequired">'.$langs->trans("UpdateInterval").'</td>';
348 print '<td class="valeur"><input type="text" name="update_interval" size="10" value="'.(empty($price_updaters->update_interval) ? '' : $price_updaters->update_interval).'"></td>';
349 print '</tr>';
350 print '</table>';
351
352 //Form Buttons
353 print $form->buttonsSaveCancel();
354
355 print '</form>';
356}
357
358// End of page
359llxFooter();
360$db->close();
$id
Definition account.php:39
if(!defined('NOREQUIRESOC')) if(!defined( 'NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined( 'NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined( 'NOREQUIREAJAX')) llxHeader($head='', $title='', $help_url='', $target='', $disablejs=0, $disablehead=0, $arrayofjs='', $arrayofcss='', $morequerystring='', $morecssonbody='', $replacemainareaby='', $disablenofollow=0, $disablenoindex=0)
Empty header.
Definition wrapper.php:70
Class to manage a WYSIWYG editor.
Class to manage generation of HTML components Only common components must be here.
Class for accessing price global variables table.
Class for price global variable updaters table.
llxFooter()
Footer empty.
Definition document.php:107
print $script_file $mode $langs defaultlang(is_numeric($duration_value) ? " delay=". $duration_value :"").(is_numeric($duration_value2) ? " after cd cd cd description as description
Only used if Module[ID]Desc translation string is not found.
load_fiche_titre($title, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0, $attop=0)
Set event messages in dol_events session object.
dol_strtolower($string, $encoding="UTF-8")
Convert a string to lower.
img_delete($titlealt='default', $other='class="pictodelete"', $morecss='')
Show delete logo.
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
newToken()
Return the value of token currently saved into session with name 'newtoken'.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
img_edit($titlealt='default', $float=0, $other='')
Show logo edit/modify fiche.
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition repair.php:137
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.