dynamic_prices.php 13 KB

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