dolibarr 21.0.0-beta
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-2024 Frédéric France <frederic.france@free.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
41// Load translation files required by the page
42$langs->load("products");
43
44$id = GETPOSTINT('id');
45$action = GETPOST('action', 'aZ09');
46$save = GETPOST('save', 'alpha');
47$cancel = GETPOST('cancel', 'alpha');
48$selection = GETPOSTINT('selection');
49
50// Security check
51if (!$user->admin) {
53}
54
55//Objects
56$price_globals = new PriceGlobalVariable($db);
57if ($action == 'edit_variable') {
58 $res = $price_globals->fetch($selection);
59 if ($res < 1) {
60 setEventMessages($price_globals->error, $price_globals->errors, 'errors');
61 }
62}
63$price_updaters = new PriceGlobalVariableUpdater($db);
64if ($action == 'edit_updater') {
65 $res = $price_updaters->fetch($selection);
66 if ($res < 1) {
67 setEventMessages($price_updaters->error, $price_updaters->errors, 'errors');
68 }
69}
70
71
72/*
73 * Actions
74 */
75
76if (!empty($action) && empty($cancel)) {
77 //Global variable actions
78 if ($action == 'create_variable' || $action == 'edit_variable') {
79 $price_globals->code = GETPOSTISSET('code') ? GETPOST('code', 'alpha') : $price_globals->code;
80 $price_globals->description = GETPOSTISSET('description') ? GETPOST('description', 'restricthtml') : $price_globals->description;
81 $price_globals->value = GETPOSTISSET('value') ? GETPOSTINT('value') : $price_globals->value;
82 //Check if record already exists only when saving
83 if (!empty($save)) {
84 foreach ($price_globals->listGlobalVariables() as $entry) {
85 if ($price_globals->id != $entry->id && dol_strtolower($price_globals->code) == dol_strtolower($entry->code)) {
86 setEventMessages($langs->trans("ErrorRecordAlreadyExists"), null, 'errors');
87 $save = null;
88 }
89 }
90 }
91 }
92 if ($action == 'create_variable' && !empty($save)) {
93 $res = $price_globals->create($user);
94 if ($res > 0) {
95 $action = '';
96 } else {
97 setEventMessages($price_globals->error, $price_globals->errors, 'errors');
98 }
99 } elseif ($action == 'edit_variable' && !empty($save)) {
100 $res = $price_globals->update($user);
101 if ($res > 0) {
102 $action = '';
103 } else {
104 setEventMessages($price_globals->error, $price_globals->errors, 'errors');
105 }
106 } elseif ($action == 'delete_variable') {
107 $res = $price_globals->delete($selection, $user);
108 if ($res > 0) {
109 $action = '';
110 } else {
111 setEventMessages($price_globals->error, $price_globals->errors, 'errors');
112 }
113 }
114
115 //Updaters actions
116 if ($action == 'create_updater' || $action == 'edit_updater') {
117 $price_updaters->type = GETPOSTISSET('type') ? GETPOSTINT('type') : $price_updaters->type;
118 $price_updaters->description = GETPOSTISSET('description') ? GETPOST('description', 'restricthtml') : $price_updaters->description;
119 $price_updaters->parameters = GETPOSTISSET('parameters') ? GETPOST('parameters', 'alphanohtml') : $price_updaters->parameters;
120 $price_updaters->fk_variable = GETPOSTISSET('fk_variable') ? GETPOSTINT('fk_variable') : $price_updaters->fk_variable;
121 $price_updaters->update_interval = GETPOSTISSET('update_interval') ? GETPOSTINT('update_interval') : $price_updaters->update_interval;
122 }
123 if ($action == 'create_updater' && !empty($save)) {
124 //Verify if process() works
125 $res = $price_updaters->process();
126 if ($res > 0) {
127 $res = $price_updaters->create($user);
128 }
129 if ($res > 0) {
130 $action = '';
131 } else {
132 setEventMessages($price_updaters->error, $price_updaters->errors, 'errors');
133 }
134 } elseif ($action == 'edit_updater' && !empty($save)) {
135 //Verify if process() works
136 $res = $price_updaters->process();
137 if ($res > 0) {
138 $res = $price_updaters->update($user);
139 }
140 if ($res > 0) {
141 $action = '';
142 } else {
143 setEventMessages($price_updaters->error, $price_updaters->errors, 'errors');
144 }
145 } elseif ($action == 'delete_updater') {
146 $res = $price_updaters->delete($selection, $user);
147 if ($res > 0) {
148 $action = '';
149 } else {
150 setEventMessages($price_updaters->error, $price_updaters->errors, 'errors');
151 }
152 }
153} elseif (!empty($cancel)) {
154 $action = '';
155}
156
157
158/*
159 * View
160 */
161
162$form = new Form($db);
163
164llxHeader("", "", $langs->trans("DynamicPrice"), '', 0, 0, '', '', '', 'mod-product page-admin_dynamic_prices');
165
166$linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
167print load_fiche_titre($langs->trans("DynamicPriceConfiguration"), $linkback, 'title_setup');
168
169print '<span class="opacitymedium">'.$langs->trans("DynamicPriceDesc").'</span><br>';
170print '<br>';
171
172//Global variables table
173if ($action != 'create_updater' && $action != 'edit_updater') {
174 print load_fiche_titre($langs->trans("GlobalVariables"), '', '');
175
176 print '<table summary="listofattributes" class="noborder centpercent">';
177 print '<tr class="liste_titre">';
178 print '<td>'.$langs->trans("Variable").'</td>';
179 print '<td>'.$langs->trans("Description").'</td>';
180 print '<td>'.$langs->trans("Value").'</td>';
181 print '<td width="80">&nbsp;</td>'; //Space for buttons
182 print '</tr>';
183
184 $arrayglobalvars = $price_globals->listGlobalVariables();
185 if (!empty($arrayglobalvars)) {
186 foreach ($arrayglobalvars as $i => $entry) {
187 print '<tr class="oddeven">';
188 print '<td>'.$entry->code.'</td>';
189 print '<td>'.$entry->description.'</td>';
190 print '<td>'.price($entry->value).'</td>';
191 print '<td class="right"><a class="editfielda paddingrightonly" href="'.$_SERVER["PHP_SELF"].'?action=edit_variable&token='.newToken().'&selection='.$entry->id.'">'.img_edit().'</a> &nbsp;';
192 print '<a href="'.$_SERVER["PHP_SELF"].'?action=delete_variable&token='.newToken().'&selection='.$entry->id.'">'.img_delete().'</a></td>';
193 print '</tr>';
194 }
195 } else {
196 print '<tr><td colspan="4"><span class="opacitymedium">';
197 print $langs->trans("None");
198 print '</span></td></tr>';
199 }
200 print '</table>';
201
202 if (empty($action)) {
203 /*
204 * Action bar
205 */
206 print '<div class="tabsAction">';
207 print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?action=create_variable">'.$langs->trans("AddVariable").'</a>';
208 print '</div>';
209 //Separator is only need for updaters table is showed after buttons
210 print '<br><br>';
211 }
212}
213
214//Global variable editor
215if ($action == 'create_variable' || $action == 'edit_variable') {
216 //Form
217 print '<form action="'.$_SERVER["PHP_SELF"].'" method="post">';
218 print '<input type="hidden" name="token" value="'.newToken().'">';
219 print '<input type="hidden" name="action" value="'.$action.'">';
220 print '<input type="hidden" name="selection" value="'.$selection.'">';
221
222 //Table
223 print '<br><table summary="listofattributes" class="border centpercent">';
224 //Code
225 print '<tr>';
226 print '<td class="fieldrequired">'.$langs->trans("Variable").'</td>';
227 print '<td class="valeur"><input type="text" name="code" class="minwidth100" value="'.(empty($price_globals->code) ? '' : $price_globals->code).'"></td>';
228 print '</tr>';
229 //Description
230 print '<tr>';
231 print '<td>'.$langs->trans("Description").'</td>';
232 print '<td class="valeur"><input type="text" name="description" class="minwidth200" value="'.(empty($price_globals->description) ? '' : $price_globals->description).'"></td>';
233 print '</tr>';
234 //Value
235 print '<tr>';
236 print '<td class="fieldrequired">'.$langs->trans("Value").'</td>';
237 print '<td class="valeur"><input type="text" name="value" class="minwidth100" value="'.(empty($price_globals->value) ? '' : $price_globals->value).'"></td>';
238 print '</tr>';
239 print '</table>';
240
241 //Form Buttons
242 print $form->buttonsSaveCancel();
243
244 print '</form>';
245}
246
247// Updaters table
248if ($action != 'create_variable' && $action != 'edit_variable') {
249 print load_fiche_titre($langs->trans("GlobalVariableUpdaters"), '', '');
250
251 print '<table summary="listofattributes" class="noborder centpercent">';
252 print '<tr class="liste_titre">';
253 print '<td>'.$langs->trans("VariableToUpdate").'</td>';
254 print '<td>'.$langs->trans("Description").'</td>';
255 print '<td>'.$langs->trans("Type").'</td>';
256 print '<td>'.$langs->trans("Parameters").'</td>';
257 print '<td>'.$langs->trans("UpdateInterval").'</td>';
258 print '<td>'.$langs->trans("LastUpdated").'</td>';
259 print '<td width="80">&nbsp;</td>'; //Space for buttons
260 print '</tr>';
261
262 $arraypriceupdaters = $price_updaters->listUpdaters();
263 if (!empty($arraypriceupdaters) && is_array($arraypriceupdaters)) {
264 foreach ($arraypriceupdaters as $i => $entry) {
265 $code = "";
266 if ($entry->fk_variable > 0) {
267 $res = $price_globals->fetch($entry->fk_variable);
268 if ($res > 0) {
269 $code = $price_globals->code;
270 }
271 }
272 print '<tr>';
273 print '<td>'.$code.'</td>';
274 print '<td>'.$entry->description.'</td>';
275 print '<td>'.$langs->trans("GlobalVariableUpdaterType".$entry->type).'</td>';
276 print '<td style="max-width: 250px; word-wrap: break-word; white-space: pre-wrap;">'.$entry->parameters.'</td>';
277 print '<td>'.$entry->update_interval.'</td>';
278 print '<td>'.$entry->getLastUpdated().'</td>';
279 print '<td class="right"><a class="editfielda paddingrightonly" href="'.$_SERVER["PHP_SELF"].'?action=edit_updater&selection='.$entry->id.'">'.img_edit().'</a> &nbsp;';
280 print '<a href="'.$_SERVER["PHP_SELF"].'?action=delete_updater&token='.newToken().'&selection='.$entry->id.'">'.img_delete().'</a></td>';
281 print '</tr>';
282 }
283 } else {
284 print '<tr><td colspan="7"><span class="opacitymedium">';
285 print $langs->trans("None");
286 print '</span></td></tr>';
287 }
288 print '</table>';
289
290 if (empty($action)) {
291 /*
292 * Action bar
293 */
294 print '<div class="tabsAction">';
295 print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?action=create_updater&token='.newToken().'">'.$langs->trans("AddUpdater").'</a>';
296 print '</div>';
297 }
298}
299
300//Updater editor
301if ($action == 'create_updater' || $action == 'edit_updater') {
302 //Form
303 print '<form id="updaterform" action="'.$_SERVER["PHP_SELF"].'" method="post">';
304 print '<input type="hidden" name="token" value="'.newToken().'">';
305 print '<input type="hidden" name="action" value="'.$action.'">';
306 print '<input type="hidden" name="selection" value="'.$selection.'">';
307
308 //Table
309 print '<br><table summary="listofattributes" class="border centpercent">';
310 //Code
311 print '<tr>';
312 print '<td class="fieldrequired">'.$langs->trans("VariableToUpdate").'</td><td>';
313 $globals_list = array();
314 foreach ($price_globals->listGlobalVariables() as $entry) {
315 $globals_list[$entry->id] = $entry->code;
316 }
317 print $form->selectarray('fk_variable', $globals_list, (empty($price_updaters->fk_variable) ? 0 : $price_updaters->fk_variable));
318 print '</td></tr>';
319 //Description
320 print '<tr>';
321 print '<td>'.$langs->trans("Description").'</td>';
322 print '<td class="valeur"><input type="text" name="description" class="minwidth200" value="'.(empty($price_updaters->description) ? '' : $price_updaters->description).'"></td>';
323 print '</tr>';
324 //Type
325 print '<tr>';
326 print '<td class="fieldrequired">'.$langs->trans("Type").'</td><td>';
327 $type = empty($price_updaters->type) ? 0 : $price_updaters->type;
328 $type_list = array();
329 foreach ($price_updaters->types as $val) {
330 $type_list[$val] = $langs->trans("GlobalVariableUpdaterType".$val);
331 }
332 print $form->selectarray('type', $type_list, $type);
333 // This code submits form when type is changed
334 print '<script type="text/javascript">
335 jQuery(document).ready(run);
336 function run() {
337 jQuery("#type").change(on_change);
338 }
339 function on_change() {
340 jQuery("#updaterform").submit();
341 }
342 </script>';
343 print '</td></tr>';
344 //Parameters
345 print '<tr>';
346 $help = $langs->trans("GlobalVariableUpdaterHelp".$type).'<br><b>'.$langs->trans("GlobalVariableUpdaterHelpFormat".$type).'</b>';
347 print '<td class="fieldrequired">'.$form->textwithpicto($langs->trans("Parameters"), $help, 1).'</td><td>';
348 require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
349 $doleditor = new DolEditor('parameters', empty($price_updaters->parameters) ? '' : $price_updaters->parameters, '', 300, '', '', false, false, false, ROWS_8, '90%');
350 $doleditor->Create();
351 print '</td></tr>';
352 print '</tr>';
353 //Interval
354 print '<tr>';
355 print '<td class="fieldrequired">'.$langs->trans("UpdateInterval").'</td>';
356 print '<td class="valeur"><input type="text" name="update_interval" size="10" value="'.(empty($price_updaters->update_interval) ? '' : $price_updaters->update_interval).'"></td>';
357 print '</tr>';
358 print '</table>';
359
360 //Form Buttons
361 print $form->buttonsSaveCancel();
362
363 print '</form>';
364}
365
366// End of page
367llxFooter();
368$db->close();
$id
Definition account.php:48
llxFooter($comment='', $zone='private', $disabledoutputofmessages=0)
Empty footer.
Definition wrapper.php:87
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:71
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.
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:149
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.