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