*
* 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/expression_globals.php
* \ingroup product
* \brief Page for configuring dynamic prices
*/
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';
$langs->load("products");
$id = GETPOST('id', 'int');
$action = GETPOST('action', 'alpha');
$save = GETPOST('save', 'alpha');
$cancel = GETPOST('cancel', 'alpha');
$selection = GETPOST('selection', 'int');
// Security check
if (!$user->admin) accessforbidden();
//Objects
$price_globals = new PriceGlobalVariable($db);
if ($action == 'edit_variable') {
$res = $price_globals->fetch($selection);
if ($res < 1) {
setEventMessage($price_globals->error, 'errors');
}
}
$price_updaters = new PriceGlobalVariableUpdater($db);
if ($action == 'edit_updater') {
$res = $price_updaters->fetch($selection);
if ($res < 1) {
setEventMessage($price_updaters->error, 'errors');
}
}
/*
* Actions
*/
if (!empty($action) && empty($cancel)) {
//Global variable actions
if ($action == 'create_variable' || $action == 'edit_variable') {
$price_globals->code = isset($_POST['code'])?GETPOST('code', 'alpha'):$price_globals->code;
$price_globals->description = isset($_POST['description'])?GETPOST('description', 'alpha'):$price_globals->description;
$price_globals->value = isset($_POST['value'])?GETPOST('value', 'int'):$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)) {
setEventMessage($langs->trans("ErrorRecordAlreadyExists"), 'errors');
$save = null;
}
}
}
}
if ($action == 'create_variable' && !empty($save)) {
$res = $price_globals->create($user);
if ($res > 0) {
$action = '';
} else {
setEventMessage($price_globals->error, 'errors');
}
} elseif ($action == 'edit_variable' && !empty($save)) {
$res = $price_globals->update($user);
if ($res > 0) {
$action = '';
} else {
setEventMessage($price_globals->error, 'errors');
}
} elseif ($action == 'delete_variable') {
$res = $price_globals->delete($selection, $user);
if ($res > 0) {
$action = '';
} else {
setEventMessage($price_globals->error, 'errors');
}
}
//Updaters actions
if ($action == 'create_updater' || $action == 'edit_updater') {
$price_updaters->type = isset($_POST['type'])?GETPOST('type', 'int'):$price_updaters->type;
$price_updaters->description = isset($_POST['description'])?GETPOST('description', 'alpha'):$price_updaters->description;
$price_updaters->parameters = isset($_POST['parameters'])?GETPOST('parameters'):$price_updaters->parameters;
$price_updaters->fk_variable = isset($_POST['fk_variable'])?GETPOST('fk_variable', 'int'):$price_updaters->fk_variable;
$price_updaters->update_interval = isset($_POST['update_interval'])?GETPOST('update_interval', 'int'):$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 {
setEventMessage($price_updaters->error, '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 {
setEventMessage($price_updaters->error, 'errors');
}
} elseif ($action == 'delete_updater') {
$res = $price_updaters->delete($selection, $user);
if ($res > 0) {
$action = '';
} else {
setEventMessage($price_updaters->error, 'errors');
}
}
} elseif (!empty($cancel)) {
$action = '';
}
/*
* View
*/
//Header
llxHeader("","",$langs->trans("CardProduct".$product->type));
print_fiche_titre($langs->trans("DynamicPriceConfiguration"));
$form = new Form($db);
//Global variables table
if ($action != 'create_updater' && $action != 'edit_updater') {
print $langs->trans("GlobalVariables");
print '
';
print '';
print '| '.$langs->trans("Code").' | ';
print ''.$langs->trans("Description").' | ';
print ''.$langs->trans("Value").' | ';
print ' | '; //Space for buttons
print '
';
$var=True;
foreach ($price_globals->listGlobalVariables() as $i=>$entry) {
$var = !$var;
print '';
print '| '.$entry->code.' | ';
print ''.$entry->description.' | ';
print ''.price($entry->value).' | ';
print 'id.'">'.img_edit().' ';
print 'id.'">'.img_delete().' | ';
print '
';
}
print '
';
}
//Global variable editor
if ($action == 'create_variable' || $action == 'edit_variable') {
//Form
print '';
} else {
//Action Buttons
print '';
//Separator is only need for updaters table is showed after buttons
print '
';
}
//Updaters table
if ($action != 'create_variable' && $action != 'edit_variable') {
print $langs->trans("GlobalVariableUpdaters");
print '';
print '';
print '| '.$langs->trans("Code").' | ';
print ''.$langs->trans("Description").' | ';
print ''.$langs->trans("Type").' | ';
print ''.$langs->trans("Parameters").' | ';
print ''.$langs->trans("UpdateInterval").' | ';
print ''.$langs->trans("LastUpdated").' | ';
print ' | '; //Space for buttons
print '
';
$var=True;
foreach ($price_updaters->listUpdaters() as $i=>$entry) {
$code = "";
if ($entry->fk_variable > 0) {
$res = $price_globals->fetch($entry->fk_variable);
if ($res > 0) {
$code = $price_globals->code;
}
}
$var = !$var;
print '';
print '| '.$code.' | ';
print ''.$entry->description.' | ';
print ''.$langs->trans("GlobalVariableUpdaterType".$entry->type).' | ';
print ''.$entry->parameters.' | ';
print ''.$entry->update_interval.' | ';
print ''.$entry->getLastUpdated().' | ';
print 'id.'">'.img_edit().' ';
print 'id.'">'.img_delete().' | ';
print '
';
}
print '
';
}
//Updater editor
if ($action == 'create_updater' || $action == 'edit_updater') {
//Form
print '';
} else {
//Action Buttons
print '';
}
llxFooter();
$db->close();