* Copyright (C) 2023-2024 Frédéric France * Copyright (C) 2024 MDW * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ /** * \file htdocs/product/admin/dynamic_prices.php * \ingroup product * \brief Page for configuring dynamic prices */ // Load Dolibarr environment require '../../main.inc.php'; require_once DOL_DOCUMENT_ROOT.'/core/lib/product.lib.php'; require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.product.class.php'; require_once DOL_DOCUMENT_ROOT.'/product/dynamic_price/class/price_global_variable.class.php'; require_once DOL_DOCUMENT_ROOT.'/product/dynamic_price/class/price_global_variable_updater.class.php'; /** * @var Conf $conf * @var DoliDB $db * @var HookManager $hookmanager * @var Translate $langs * @var User $user */ // Load translation files required by the page $langs->load("products"); $id = GETPOSTINT('id'); $action = GETPOST('action', 'aZ09'); $save = GETPOST('save', 'alpha'); $cancel = GETPOST('cancel', 'alpha'); $selection = GETPOSTINT('selection'); // Security check if (!$user->admin) { accessforbidden(); } //Objects $price_globals = new PriceGlobalVariable($db); if ($action == 'edit_variable') { $res = $price_globals->fetch($selection); if ($res < 1) { setEventMessages($price_globals->error, $price_globals->errors, 'errors'); } } $price_updaters = new PriceGlobalVariableUpdater($db); if ($action == 'edit_updater') { $res = $price_updaters->fetch($selection); if ($res < 1) { setEventMessages($price_updaters->error, $price_updaters->errors, 'errors'); } } /* * Actions */ if (!empty($action) && empty($cancel)) { //Global variable actions if ($action == 'create_variable' || $action == 'edit_variable') { $price_globals->code = GETPOSTISSET('code') ? GETPOST('code', 'alpha') : $price_globals->code; $price_globals->description = GETPOSTISSET('description') ? GETPOST('description', 'restricthtml') : $price_globals->description; $price_globals->value = GETPOSTISSET('value') ? GETPOSTINT('value') : $price_globals->value; //Check if record already exists only when saving if (!empty($save)) { foreach ($price_globals->listGlobalVariables() as $entry) { if ($price_globals->id != $entry->id && dol_strtolower($price_globals->code) == dol_strtolower($entry->code)) { setEventMessages($langs->trans("ErrorRecordAlreadyExists"), null, 'errors'); $save = null; } } } } if ($action == 'create_variable' && !empty($save)) { $res = $price_globals->create($user); if ($res > 0) { $action = ''; } else { setEventMessages($price_globals->error, $price_globals->errors, 'errors'); } } elseif ($action == 'edit_variable' && !empty($save)) { $res = $price_globals->update($user); if ($res > 0) { $action = ''; } else { setEventMessages($price_globals->error, $price_globals->errors, 'errors'); } } elseif ($action == 'delete_variable') { $res = $price_globals->delete($selection, $user); if ($res > 0) { $action = ''; } else { setEventMessages($price_globals->error, $price_globals->errors, 'errors'); } } //Updaters actions if ($action == 'create_updater' || $action == 'edit_updater') { $price_updaters->type = GETPOSTISSET('type') ? GETPOSTINT('type') : $price_updaters->type; $price_updaters->description = GETPOSTISSET('description') ? GETPOST('description', 'restricthtml') : $price_updaters->description; $price_updaters->parameters = GETPOSTISSET('parameters') ? GETPOST('parameters', 'alphanohtml') : $price_updaters->parameters; $price_updaters->fk_variable = GETPOSTISSET('fk_variable') ? GETPOSTINT('fk_variable') : $price_updaters->fk_variable; $price_updaters->update_interval = GETPOSTISSET('update_interval') ? GETPOSTINT('update_interval') : $price_updaters->update_interval; } if ($action == 'create_updater' && !empty($save)) { //Verify if process() works $res = $price_updaters->process(); if ($res > 0) { $res = $price_updaters->create($user); } if ($res > 0) { $action = ''; } else { setEventMessages($price_updaters->error, $price_updaters->errors, 'errors'); } } elseif ($action == 'edit_updater' && !empty($save)) { //Verify if process() works $res = $price_updaters->process(); if ($res > 0) { $res = $price_updaters->update($user); } if ($res > 0) { $action = ''; } else { setEventMessages($price_updaters->error, $price_updaters->errors, 'errors'); } } elseif ($action == 'delete_updater') { $res = $price_updaters->delete($selection, $user); if ($res > 0) { $action = ''; } else { setEventMessages($price_updaters->error, $price_updaters->errors, 'errors'); } } } elseif (!empty($cancel)) { $action = ''; } /* * View */ $form = new Form($db); llxHeader("", "", $langs->trans("DynamicPrice"), '', 0, 0, '', '', '', 'mod-product page-admin_dynamic_prices'); $linkback = ''.$langs->trans("BackToModuleList").''; print load_fiche_titre($langs->trans("DynamicPriceConfiguration"), $linkback, 'title_setup'); print ''.$langs->trans("DynamicPriceDesc").'
'; print '
'; //Global variables table if ($action != 'create_updater' && $action != 'edit_updater') { print load_fiche_titre($langs->trans("GlobalVariables"), '', ''); print ''; print ''; print ''; print ''; print ''; print ''; //Space for buttons print ''; $arrayglobalvars = $price_globals->listGlobalVariables(); if (!empty($arrayglobalvars)) { foreach ($arrayglobalvars as $i => $entry) { print ''; print ''; print ''; print ''; print ''; print ''; } } else { print ''; } print '
'.$langs->trans("Variable").''.$langs->trans("Description").''.$langs->trans("Value").' 
'.$entry->code.''.$entry->description.''.price($entry->value).'id.'">'.img_edit().'  '; print 'id.'">'.img_delete().'
'; print $langs->trans("None"); print '
'; if (empty($action)) { /* * Action bar */ print ''; //Separator is only need for updaters table is showed after buttons print '

'; } } //Global variable editor if ($action == 'create_variable' || $action == 'edit_variable') { //Form print '
'; print ''; print ''; print ''; //Table print '
'; //Code print ''; print ''; print ''; print ''; //Description print ''; print ''; print ''; print ''; //Value print ''; print ''; print ''; print ''; print '
'.$langs->trans("Variable").'
'.$langs->trans("Description").'
'.$langs->trans("Value").'
'; //Form Buttons print $form->buttonsSaveCancel(); print '
'; } // Updaters table if ($action != 'create_variable' && $action != 'edit_variable') { print load_fiche_titre($langs->trans("GlobalVariableUpdaters"), '', ''); print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; //Space for buttons print ''; $arraypriceupdaters = $price_updaters->listUpdaters(); if (!empty($arraypriceupdaters) && is_array($arraypriceupdaters)) { foreach ($arraypriceupdaters as $i => $entry) { $code = ""; if ($entry->fk_variable > 0) { $res = $price_globals->fetch($entry->fk_variable); if ($res > 0) { $code = $price_globals->code; } } print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; } } else { print ''; } print '
'.$langs->trans("VariableToUpdate").''.$langs->trans("Description").''.$langs->trans("Type").''.$langs->trans("Parameters").''.$langs->trans("UpdateInterval").''.$langs->trans("LastUpdated").' 
'.$code.''.$entry->description.''.$langs->trans("GlobalVariableUpdaterType".$entry->type).''.$entry->parameters.''.$entry->update_interval.''.$entry->getLastUpdated().'id.'">'.img_edit().'  '; print 'id.'">'.img_delete().'
'; print $langs->trans("None"); print '
'; if (empty($action)) { /* * Action bar */ print ''; } } //Updater editor if ($action == 'create_updater' || $action == 'edit_updater') { //Form print '
'; print ''; print ''; print ''; //Table print '
'; //Code print ''; print ''; //Description print ''; print ''; print ''; print ''; //Type print ''; print ''; //Parameters print ''; $help = $langs->trans("GlobalVariableUpdaterHelp".$type).'
'.$langs->trans("GlobalVariableUpdaterHelpFormat".$type).''; print ''; print ''; //Interval print ''; print ''; print ''; print ''; print '
'.$langs->trans("VariableToUpdate").''; $globals_list = array(); foreach ($price_globals->listGlobalVariables() as $entry) { $globals_list[$entry->id] = $entry->code; } print $form->selectarray('fk_variable', $globals_list, (empty($price_updaters->fk_variable) ? 0 : $price_updaters->fk_variable)); print '
'.$langs->trans("Description").'
'.$langs->trans("Type").''; $type = empty($price_updaters->type) ? 0 : $price_updaters->type; $type_list = array(); foreach ($price_updaters->types as $val) { $type_list[$val] = $langs->trans("GlobalVariableUpdaterType".$val); } print $form->selectarray('type', $type_list, $type); // This code submits form when type is changed print ''; print '
'.$form->textwithpicto($langs->trans("Parameters"), $help, 1).''; require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php'; $doleditor = new DolEditor('parameters', empty($price_updaters->parameters) ? '' : $price_updaters->parameters, '', 300, '', '', false, false, false, ROWS_8, '90%'); $doleditor->Create(); print '
'.$langs->trans("UpdateInterval").'
'; //Form Buttons print $form->buttonsSaveCancel(); print '
'; } // End of page llxFooter(); $db->close();