dynamic_prices.php 13 KB

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