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