dolibarr  16.0.5
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) {
42 }
43 
44 //Objects
45 $price_globals = new PriceGlobalVariable($db);
46 if ($action == 'edit_variable') {
47  $res = $price_globals->fetch($selection);
48  if ($res < 1) {
49  setEventMessages($price_globals->error, $price_globals->errors, 'errors');
50  }
51 }
52 $price_updaters = new PriceGlobalVariableUpdater($db);
53 if ($action == 'edit_updater') {
54  $res = $price_updaters->fetch($selection);
55  if ($res < 1) {
56  setEventMessages($price_updaters->error, $price_updaters->errors, 'errors');
57  }
58 }
59 
60 
61 /*
62  * Actions
63  */
64 
65 if (!empty($action) && empty($cancel)) {
66  //Global variable actions
67  if ($action == 'create_variable' || $action == 'edit_variable') {
68  $price_globals->code = GETPOSTISSET('code') ?GETPOST('code', 'alpha') : $price_globals->code;
69  $price_globals->description = GETPOSTISSET('description') ?GETPOST('description', 'restricthtml') : $price_globals->description;
70  $price_globals->value = GETPOSTISSET('value') ?GETPOST('value', 'int') : $price_globals->value;
71  //Check if record already exists only when saving
72  if (!empty($save)) {
73  foreach ($price_globals->listGlobalVariables() as $entry) {
74  if ($price_globals->id != $entry->id && dol_strtolower($price_globals->code) == dol_strtolower($entry->code)) {
75  setEventMessages($langs->trans("ErrorRecordAlreadyExists"), null, 'errors');
76  $save = null;
77  }
78  }
79  }
80  }
81  if ($action == 'create_variable' && !empty($save)) {
82  $res = $price_globals->create($user);
83  if ($res > 0) {
84  $action = '';
85  } else {
86  setEventMessages($price_globals->error, $price_globals->errors, 'errors');
87  }
88  } elseif ($action == 'edit_variable' && !empty($save)) {
89  $res = $price_globals->update($user);
90  if ($res > 0) {
91  $action = '';
92  } else {
93  setEventMessages($price_globals->error, $price_globals->errors, 'errors');
94  }
95  } elseif ($action == 'delete_variable') {
96  $res = $price_globals->delete($selection, $user);
97  if ($res > 0) {
98  $action = '';
99  } else {
100  setEventMessages($price_globals->error, $price_globals->errors, 'errors');
101  }
102  }
103 
104  //Updaters actions
105  if ($action == 'create_updater' || $action == 'edit_updater') {
106  $price_updaters->type = GETPOSTISSET('type') ? GETPOST('type', 'int') : $price_updaters->type;
107  $price_updaters->description = GETPOSTISSET('description') ? GETPOST('description', 'restricthtml') : $price_updaters->description;
108  $price_updaters->parameters = GETPOSTISSET('parameters') ? GETPOST('parameters', 'alphanohtml') : $price_updaters->parameters;
109  $price_updaters->fk_variable = GETPOSTISSET('fk_variable') ? GETPOST('fk_variable', 'int') : $price_updaters->fk_variable;
110  $price_updaters->update_interval = GETPOSTISSET('update_interval') ? GETPOST('update_interval', 'int') : $price_updaters->update_interval;
111  }
112  if ($action == 'create_updater' && !empty($save)) {
113  //Verify if process() works
114  $res = $price_updaters->process();
115  if ($res > 0) {
116  $res = $price_updaters->create($user);
117  }
118  if ($res > 0) {
119  $action = '';
120  } else {
121  setEventMessages($price_updaters->error, $price_updaters->errors, 'errors');
122  }
123  } elseif ($action == 'edit_updater' && !empty($save)) {
124  //Verify if process() works
125  $res = $price_updaters->process();
126  if ($res > 0) {
127  $res = $price_updaters->update($user);
128  }
129  if ($res > 0) {
130  $action = '';
131  } else {
132  setEventMessages($price_updaters->error, $price_updaters->errors, 'errors');
133  }
134  } elseif ($action == 'delete_updater') {
135  $res = $price_updaters->delete($selection, $user);
136  if ($res > 0) {
137  $action = '';
138  } else {
139  setEventMessages($price_updaters->error, $price_updaters->errors, 'errors');
140  }
141  }
142 } elseif (!empty($cancel)) {
143  $action = '';
144 }
145 
146 
147 /*
148  * View
149  */
150 
151 $form = new Form($db);
152 
153 llxHeader("", "", $langs->trans("CardProduct".$product->type));
154 
155 $linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
156 print load_fiche_titre($langs->trans("DynamicPriceConfiguration"), $linkback, 'title_setup');
157 
158 print '<span class="opacitymedium">'.$langs->trans("DynamicPriceDesc").'</span><br>';
159 print '<br>';
160 
161 //Global variables table
162 if ($action != 'create_updater' && $action != 'edit_updater') {
163  print load_fiche_titre($langs->trans("GlobalVariables"), '', '');
164 
165  print '<table summary="listofattributes" class="noborder centpercent">';
166  print '<tr class="liste_titre">';
167  print '<td>'.$langs->trans("Variable").'</td>';
168  print '<td>'.$langs->trans("Description").'</td>';
169  print '<td>'.$langs->trans("Value").'</td>';
170  print '<td width="80">&nbsp;</td>'; //Space for buttons
171  print '</tr>';
172 
173  $arrayglobalvars = $price_globals->listGlobalVariables();
174  if (!empty($arrayglobalvars)) {
175  foreach ($arrayglobalvars as $i => $entry) {
176  print '<tr class="oddeven">';
177  print '<td>'.$entry->code.'</td>';
178  print '<td>'.$entry->description.'</td>';
179  print '<td>'.price($entry->value).'</td>';
180  print '<td class="right"><a class="editfielda paddingrightonly" href="'.$_SERVER["PHP_SELF"].'?action=edit_variable&token='.newToken().'&selection='.$entry->id.'">'.img_edit().'</a> &nbsp;';
181  print '<a href="'.$_SERVER["PHP_SELF"].'?action=delete_variable&token='.newToken().'&selection='.$entry->id.'">'.img_delete().'</a></td>';
182  print '</tr>';
183  }
184  } else {
185  print '<tr colspan="7"><td><span class="opacitymedium">';
186  print $langs->trans("None");
187  print '</span></td></tr>';
188  }
189  print '</table>';
190 
191  if (empty($action)) {
192  /*
193  * Action bar
194  */
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" class="minwidth100" 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" class="minwidth200" 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" class="minwidth100" value="'.(empty($price_globals->value) ? '' : $price_globals->value).'"></td>';
227  print '</tr>';
228  print '</table>';
229 
230  //Form Buttons
231  print $form->buttonsSaveCancel();
232 
233  print '</form>';
234 }
235 
236 // Updaters table
237 if ($action != 'create_variable' && $action != 'edit_variable') {
238  print load_fiche_titre($langs->trans("GlobalVariableUpdaters"), '', '');
239 
240  print '<table summary="listofattributes" class="noborder centpercent">';
241  print '<tr class="liste_titre">';
242  print '<td>'.$langs->trans("VariableToUpdate").'</td>';
243  print '<td>'.$langs->trans("Description").'</td>';
244  print '<td>'.$langs->trans("Type").'</td>';
245  print '<td>'.$langs->trans("Parameters").'</td>';
246  print '<td>'.$langs->trans("UpdateInterval").'</td>';
247  print '<td>'.$langs->trans("LastUpdated").'</td>';
248  print '<td width="80">&nbsp;</td>'; //Space for buttons
249  print '</tr>';
250 
251  $arraypriceupdaters = $price_updaters->listUpdaters();
252  if (!empty($arraypriceupdaters)) {
253  foreach ($arraypriceupdaters as $i => $entry) {
254  $code = "";
255  if ($entry->fk_variable > 0) {
256  $res = $price_globals->fetch($entry->fk_variable);
257  if ($res > 0) {
258  $code = $price_globals->code;
259  }
260  }
261  print '<tr>';
262  print '<td>'.$code.'</td>';
263  print '<td>'.$entry->description.'</td>';
264  print '<td>'.$langs->trans("GlobalVariableUpdaterType".$entry->type).'</td>';
265  print '<td style="max-width: 250px; word-wrap: break-word; white-space: pre-wrap;">'.$entry->parameters.'</td>';
266  print '<td>'.$entry->update_interval.'</td>';
267  print '<td>'.$entry->getLastUpdated().'</td>';
268  print '<td class="right"><a class="editfielda paddingrightonly" href="'.$_SERVER["PHP_SELF"].'?action=edit_updater&selection='.$entry->id.'">'.img_edit().'</a> &nbsp;';
269  print '<a href="'.$_SERVER["PHP_SELF"].'?action=delete_updater&token='.newToken().'&selection='.$entry->id.'">'.img_delete().'</a></td>';
270  print '</tr>';
271  }
272  } else {
273  print '<tr><td colspan="7" class="opacitymedium">';
274  print $langs->trans("None");
275  print '</td></tr>';
276  }
277  print '</table>';
278 
279  if (empty($action)) {
280  /*
281  * Action bar
282  */
283  print '<div class="tabsAction">';
284  print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?action=create_updater&token='.newToken().'">'.$langs->trans("AddUpdater").'</a>';
285  print '</div>';
286  }
287 }
288 
289 //Updater editor
290 if ($action == 'create_updater' || $action == 'edit_updater') {
291  //Form
292  print '<form id="updaterform" action="'.$_SERVER["PHP_SELF"].'" method="post">';
293  print '<input type="hidden" name="token" value="'.newToken().'">';
294  print '<input type="hidden" name="action" value="'.$action.'">';
295  print '<input type="hidden" name="selection" value="'.$selection.'">';
296 
297  //Table
298  print '<br><table summary="listofattributes" class="border centpercent">';
299  //Code
300  print '<tr>';
301  print '<td class="fieldrequired">'.$langs->trans("VariableToUpdate").'</td><td>';
302  $globals_list = array();
303  foreach ($price_globals->listGlobalVariables() as $entry) {
304  $globals_list[$entry->id] = $entry->code;
305  }
306  print $form->selectarray('fk_variable', $globals_list, (empty($price_updaters->fk_variable) ? 0 : $price_updaters->fk_variable));
307  print '</td></tr>';
308  //Description
309  print '<tr>';
310  print '<td>'.$langs->trans("Description").'</td>';
311  print '<td class="valeur"><input type="text" name="description" class="minwidth200" value="'.(empty($price_updaters->description) ? '' : $price_updaters->description).'"></td>';
312  print '</tr>';
313  //Type
314  print '<tr>';
315  print '<td class="fieldrequired">'.$langs->trans("Type").'</td><td>';
316  $type = empty($price_updaters->type) ? 0 : $price_updaters->type;
317  $type_list = array();
318  foreach ($price_updaters->types as $val) {
319  $type_list[$val] = $langs->trans("GlobalVariableUpdaterType".$val);
320  }
321  print $form->selectarray('type', $type_list, $type);
322  // This code submits form when type is changed
323  print '<script type="text/javascript">
324  jQuery(document).ready(run);
325  function run() {
326  jQuery("#type").change(on_change);
327  }
328  function on_change() {
329  jQuery("#updaterform").submit();
330  }
331  </script>';
332  print '</td></tr>';
333  //Parameters
334  print '<tr>';
335  $help = $langs->trans("GlobalVariableUpdaterHelp".$type).'<br><b>'.$langs->trans("GlobalVariableUpdaterHelpFormat".$type).'</b>';
336  print '<td class="fieldrequired">'.$form->textwithpicto($langs->trans("Parameters"), $help, 1).'</td><td>';
337  require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
338  $doleditor = new DolEditor('parameters', empty($price_updaters->parameters) ? '' : $price_updaters->parameters, '', 300, '', '', false, false, false, ROWS_8, '90%');
339  $doleditor->Create();
340  print '</td></tr>';
341  print '</tr>';
342  //Interval
343  print '<tr>';
344  print '<td class="fieldrequired">'.$langs->trans("UpdateInterval").'</td>';
345  print '<td class="valeur"><input type="text" name="update_interval" size="10" value="'.(empty($price_updaters->update_interval) ? '' : $price_updaters->update_interval).'"></td>';
346  print '</tr>';
347  print '</table>';
348 
349  //Form Buttons
350  print $form->buttonsSaveCancel();
351 
352  print '</form>';
353 }
354 
355 // End of page
356 llxFooter();
357 $db->close();
description
print *****$script_file(".$version.") pid cd cd cd description as description
Definition: email_expire_services_to_customers.php:83
llxFooter
llxFooter()
Empty footer.
Definition: wrapper.php:73
load_fiche_titre
load_fiche_titre($titre, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
Definition: functions.lib.php:5204
GETPOST
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
Definition: functions.lib.php:484
PriceGlobalVariableUpdater
Class for price global variable updaters table.
Definition: price_global_variable_updater.class.php:30
$form
if($cancel &&! $id) if($action=='add' &&! $cancel) if($action=='delete') if($id) $form
Actions.
Definition: card.php:142
img_edit
img_edit($titlealt='default', $float=0, $other='')
Show logo editer/modifier fiche.
Definition: functions.lib.php:4389
img_delete
img_delete($titlealt='default', $other='class="pictodelete"', $morecss='')
Show delete logo.
Definition: functions.lib.php:4429
code
print *****$script_file(".$version.") pid code
! Closing after partial payment: discount_vat, badcustomer or badsupplier, bankcharge,...
Definition: sync_members_ldap2dolibarr.php:60
newToken
newToken()
Return the value of token currently saved into session with name 'newtoken'.
Definition: functions.lib.php:10878
GETPOSTISSET
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname from a POST of a form.
Definition: functions.lib.php:386
Form
Class to manage generation of HTML components Only common components must be here.
Definition: html.form.class.php:52
setEventMessages
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='')
Set event messages in dol_events session object.
Definition: functions.lib.php:8137
accessforbidden
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 ...
Definition: security.lib.php:933
type
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition: repair.php:119
llxHeader
if(!defined('NOREQUIRESOC')) if(!defined('NOREQUIRETRAN')) if(!defined('NOCSRFCHECK')) if(!defined('NOTOKENRENEWAL')) if(!defined('NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined('NOREQUIREAJAX')) llxHeader()
Empty header.
Definition: wrapper.php:59
DolEditor
Class to manage a WYSIWYG editor.
Definition: doleditor.class.php:30
dol_strtolower
dol_strtolower($string, $encoding="UTF-8")
Convert a string to lower.
Definition: functions.lib.php:1525
PriceGlobalVariable
Class for accesing price global variables table.
Definition: price_global_variable.class.php:30