dolibarr  13.0.2
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 require '../../main.inc.php';
25 require_once DOL_DOCUMENT_ROOT.'/core/lib/product.lib.php';
26 require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.product.class.php';
27 require_once DOL_DOCUMENT_ROOT.'/product/dynamic_price/class/price_global_variable.class.php';
28 require_once DOL_DOCUMENT_ROOT.'/product/dynamic_price/class/price_global_variable_updater.class.php';
29 
30 // Load translation files required by the page
31 $langs->load("products");
32 
33 $id = GETPOST('id', 'int');
34 $action = GETPOST('action', 'aZ09');
35 $save = GETPOST('save', 'alpha');
36 $cancel = GETPOST('cancel', 'alpha');
37 $selection = GETPOST('selection', 'int');
38 
39 // Security check
40 if (!$user->admin) accessforbidden();
41 
42 //Objects
43 $price_globals = new PriceGlobalVariable($db);
44 if ($action == 'edit_variable') {
45  $res = $price_globals->fetch($selection);
46  if ($res < 1) {
47  setEventMessages($price_globals->error, $price_globals->errors, 'errors');
48  }
49 }
50 $price_updaters = new PriceGlobalVariableUpdater($db);
51 if ($action == 'edit_updater') {
52  $res = $price_updaters->fetch($selection);
53  if ($res < 1) {
54  setEventMessages($price_updaters->error, $price_updaters->errors, 'errors');
55  }
56 }
57 
58 
59 /*
60  * Actions
61  */
62 
63 if (!empty($action) && empty($cancel)) {
64  //Global variable actions
65  if ($action == 'create_variable' || $action == 'edit_variable') {
66  $price_globals->code = GETPOSTISSET('code') ?GETPOST('code', 'alpha') : $price_globals->code;
67  $price_globals->description = GETPOSTISSET('description') ?GETPOST('description', 'restricthtml') : $price_globals->description;
68  $price_globals->value = GETPOSTISSET('value') ?GETPOST('value', 'int') : $price_globals->value;
69  //Check if record already exists only when saving
70  if (!empty($save)) {
71  foreach ($price_globals->listGlobalVariables() as $entry) {
72  if ($price_globals->id != $entry->id && dol_strtolower($price_globals->code) == dol_strtolower($entry->code)) {
73  setEventMessages($langs->trans("ErrorRecordAlreadyExists"), null, 'errors');
74  $save = null;
75  }
76  }
77  }
78  }
79  if ($action == 'create_variable' && !empty($save)) {
80  $res = $price_globals->create($user);
81  if ($res > 0) {
82  $action = '';
83  } else {
84  setEventMessages($price_globals->error, $price_globals->errors, 'errors');
85  }
86  } elseif ($action == 'edit_variable' && !empty($save)) {
87  $res = $price_globals->update($user);
88  if ($res > 0) {
89  $action = '';
90  } else {
91  setEventMessages($price_globals->error, $price_globals->errors, 'errors');
92  }
93  } elseif ($action == 'delete_variable') {
94  $res = $price_globals->delete($selection, $user);
95  if ($res > 0) {
96  $action = '';
97  } else {
98  setEventMessages($price_globals->error, $price_globals->errors, 'errors');
99  }
100  }
101 
102  //Updaters actions
103  if ($action == 'create_updater' || $action == 'edit_updater') {
104  $price_updaters->type = GETPOSTISSET('type') ? GETPOST('type', 'int') : $price_updaters->type;
105  $price_updaters->description = GETPOSTISSET('description') ? GETPOST('description', 'restricthtml') : $price_updaters->description;
106  $price_updaters->parameters = GETPOSTISSET('parameters') ? GETPOST('parameters', 'alphanohtml') : $price_updaters->parameters;
107  $price_updaters->fk_variable = GETPOSTISSET('fk_variable') ? GETPOST('fk_variable', 'int') : $price_updaters->fk_variable;
108  $price_updaters->update_interval = GETPOSTISSET('update_interval') ? GETPOST('update_interval', 'int') : $price_updaters->update_interval;
109  }
110  if ($action == 'create_updater' && !empty($save)) {
111  //Verify if process() works
112  $res = $price_updaters->process();
113  if ($res > 0) {
114  $res = $price_updaters->create($user);
115  }
116  if ($res > 0) {
117  $action = '';
118  } else {
119  setEventMessages($price_updaters->error, $price_updaters->errors, 'errors');
120  }
121  } elseif ($action == 'edit_updater' && !empty($save)) {
122  //Verify if process() works
123  $res = $price_updaters->process();
124  if ($res > 0) {
125  $res = $price_updaters->update($user);
126  }
127  if ($res > 0) {
128  $action = '';
129  } else {
130  setEventMessages($price_updaters->error, $price_updaters->errors, 'errors');
131  }
132  } elseif ($action == 'delete_updater') {
133  $res = $price_updaters->delete($selection, $user);
134  if ($res > 0) {
135  $action = '';
136  } else {
137  setEventMessages($price_updaters->error, $price_updaters->errors, 'errors');
138  }
139  }
140 } elseif (!empty($cancel)) {
141  $action = '';
142 }
143 
144 
145 /*
146  * View
147  */
148 
149 $form = new Form($db);
150 
151 llxHeader("", "", $langs->trans("CardProduct".$product->type));
152 
153 $linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
154 print load_fiche_titre($langs->trans("DynamicPriceConfiguration"), $linkback, 'title_setup');
155 
156 print '<span class="opacitymedium">'.$langs->trans("DynamicPriceDesc").'</span><br>';
157 print '<br>';
158 
159 //Global variables table
160 if ($action != 'create_updater' && $action != 'edit_updater')
161 {
162  print load_fiche_titre($langs->trans("GlobalVariables"), '', '');
163 
164  print '<table summary="listofattributes" class="noborder centpercent">';
165  print '<tr class="liste_titre">';
166  print '<td>'.$langs->trans("Variable").'</td>';
167  print '<td>'.$langs->trans("Description").'</td>';
168  print '<td>'.$langs->trans("Value").'</td>';
169  print '<td width="80">&nbsp;</td>'; //Space for buttons
170  print '</tr>';
171 
172  $arrayglobalvars = $price_globals->listGlobalVariables();
173  if (!empty($arrayglobalvars))
174  {
175  foreach ($arrayglobalvars as $i=>$entry) {
176  $var = !$var;
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 class="opacitymedium">';
187  print $langs->trans("None");
188  print '</td></tr>';
189  }
190  print '</table>';
191 
192  if (empty($action))
193  {
194  //Action Buttons
195  print '<div class="tabsAction">';
196  print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?action=create_variable">'.$langs->trans("AddVariable").'</a>';
197  print '</div>';
198  //Separator is only need for updaters table is showed after buttons
199  print '<br><br>';
200  }
201 }
202 
203 //Global variable editor
204 if ($action == 'create_variable' || $action == 'edit_variable') {
205  //Form
206  print '<form action="'.$_SERVER["PHP_SELF"].'" method="post">';
207  print '<input type="hidden" name="token" value="'.newToken().'">';
208  print '<input type="hidden" name="action" value="'.$action.'">';
209  print '<input type="hidden" name="selection" value="'.$selection.'">';
210 
211  //Table
212  print '<br><table summary="listofattributes" class="border centpercent">';
213  //Code
214  print '<tr>';
215  print '<td class="fieldrequired">'.$langs->trans("Variable").'</td>';
216  print '<td class="valeur"><input type="text" name="code" size="20" value="'.(empty($price_globals->code) ? '' : $price_globals->code).'"></td>';
217  print '</tr>';
218  //Description
219  print '<tr>';
220  print '<td>'.$langs->trans("Description").'</td>';
221  print '<td class="valeur"><input type="text" name="description" size="50" value="'.(empty($price_globals->description) ? '' : $price_globals->description).'"></td>';
222  print '</tr>';
223  //Value
224  print '<tr>';
225  print '<td class="fieldrequired">'.$langs->trans("Value").'</td>';
226  print '<td class="valeur"><input type="text" name="value" size="10" value="'.(empty($price_globals->value) ? '' : $price_globals->value).'"></td>';
227  print '</tr>';
228  print '</table>';
229 
230  //Form Buttons
231  print '<br><div class="center">';
232  print '<input type="submit" class="button button-save" name="save" value="'.$langs->trans("Save").'"> &nbsp;';
233  print '<input type="submit" class="button button-cancel" name="cancel" id="cancel" value="'.$langs->trans("Cancel").'">';
234  print '</div>';
235  print '</form>';
236 }
237 
238 // Updaters table
239 if ($action != 'create_variable' && $action != 'edit_variable')
240 {
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))
256  {
257  foreach ($arraypriceupdaters as $i=>$entry) {
258  $code = "";
259  if ($entry->fk_variable > 0) {
260  $res = $price_globals->fetch($entry->fk_variable);
261  if ($res > 0) {
262  $code = $price_globals->code;
263  }
264  }
265  print '<tr>';
266  print '<td>'.$code.'</td>';
267  print '<td>'.$entry->description.'</td>';
268  print '<td>'.$langs->trans("GlobalVariableUpdaterType".$entry->type).'</td>';
269  print '<td style="max-width: 250px; word-wrap: break-word; white-space: pre-wrap;">'.$entry->parameters.'</td>';
270  print '<td>'.$entry->update_interval.'</td>';
271  print '<td>'.$entry->getLastUpdated().'</td>';
272  print '<td class="right"><a class="editfielda paddingrightonly" href="'.$_SERVER["PHP_SELF"].'?action=edit_updater&selection='.$entry->id.'">'.img_edit().'</a> &nbsp;';
273  print '<a href="'.$_SERVER["PHP_SELF"].'?action=delete_updater&token='.newToken().'&selection='.$entry->id.'">'.img_delete().'</a></td>';
274  print '</tr>';
275  }
276  } else {
277  print '<tr><td colspan="7" class="opacitymedium">';
278  print $langs->trans("None");
279  print '</td></tr>';
280  }
281  print '</table>';
282 
283  if (empty($action))
284  {
285  //Action Buttons
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
293 if ($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" size="50" 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 '<br><div class="center">';
354  print '<input type="submit" class="button button-save" name="save" value="'.$langs->trans("Save").'"> &nbsp;';
355  print '<input type="submit" class="button button-cancel" name="cancel" id="cancel" value="'.$langs->trans("Cancel").'">';
356  print '</div>';
357  print '</form>';
358 }
359 
360 // End of page
361 llxFooter();
362 $db->close();
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 editer/modifier fiche.
if(!empty($arrayfields['country.code_iso']['checked'])) print_liste_field_titre($arrayfields['country.code_iso']['label'] country if(!empty($arrayfields['typent.code']['checked'])) print_liste_field_titre($arrayfields['typent.code']['label'] typent code
Definition: list.php:566
</td > param sortfield sortorder printFieldListOption< tdclass="liste_titremaxwidthsearchright"></td ></tr >< trclass="liste_titre">< inputtype="checkbox"onClick="toggle(this)"/> Ref p ref Label p label Duration p duration center DesiredStock p desiredstock right StockLimitShort p seuil_stock_alerte right stock_physique right stock_real_warehouse right Ordered right StockToBuy right SupplierRef right param sortfield sortorder printFieldListTitle warehouseinternal SELECT description FROM product_lang WHERE qty< br > qty qty qty StockTooLow StockTooLow help help help< trclass="oddeven">< td >< inputtype="checkbox"class="check"name="choose'.$i.'"></td >< tdclass="nowrap"> stock</td >< td >< inputtype="hidden"name="desc'.$i.'"value="'.dol_escape_htmltag($objp-> description
Only used if Module[ID]Desc translation string is not found.
Definition: replenish.php:750
llxHeader()
Empty header.
Definition: wrapper.php:45
setEventMessages($mesg, $mesgs, $style= 'mesgs', $messagekey= '')
Set event messages in dol_events session object.
Class to manage generation of HTML components Only common components must be here.
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname.
load_fiche_titre($titre, $morehtmlright= '', $picto= 'generic', $pictoisfullpath=0, $id= '', $morecssontable= '', $morehtmlcenter= '')
Load a title with picto.
accessforbidden($message= '', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program Calling this function terminate execution ...
print
Draft customers invoices.
Definition: index.php:89
Class for accesing price global variables table.
newToken()
Return the value of token currently saved into session with name &#39;newtoken&#39;.
Class to manage a WYSIWYG editor.
llxFooter()
Empty footer.
Definition: wrapper.php:59
img_delete($titlealt= 'default', $other= 'class="pictodelete"', $morecss= '')
Show delete logo.
Class for price global variable updaters table.
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition: repair.php:105
dol_strtolower($string, $encoding="UTF-8")
Convert a string to lower.