*
* 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/dynamic_price/editor.php
* \ingroup product
* \brief Page for editing expression
*/
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_expression.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_parser.class.php';
// Load translation files required by the page
$langs->loadLangs(array('products', 'accountancy')); //"Back" translation is on this accountancy file
$id = GETPOST('id', 'int');
$eid = GETPOST('eid', 'int');
$action = GETPOST('action', 'alpha');
$title = GETPOST('expression_title', 'alpha');
$expression = GETPOST('expression');
$tab = GETPOST('tab', 'alpha');
$tab = (!empty($tab)) ? $tab : 'card';
$tab = strtolower($tab);
// Security check
$result=restrictedArea($user,'produit|service&fournisseur',$id,'product&product','','','rowid');
//Initialize objects
$product = new Product($db);
$product->fetch($id, '');
$price_expression = new PriceExpression($db);
$price_globals = new PriceGlobalVariable($db);
//Fetch expression data
if (empty($eid)) //This also disables fetch when eid == 0
{
$eid = 0;
}
else if ($action != 'delete')
{
$price_expression->fetch($eid);
}
/*
* Actions
*/
if ($action == 'add')
{
if ($eid == 0)
{
$result = $price_expression->find_title($title);
if ($result == 0) //No existing entry found with title, ok
{
//Check the expression validity by parsing it
$priceparser = new PriceParser($db);
$price_result = $priceparser->testExpression($id, $expression);
if ($price_result < 0) { //Expression is not valid
setEventMessages($priceparser->translatedError(), null, 'errors');
}
else
{
$price_expression->title = $title;
$price_expression->expression = $expression;
$result = $price_expression->create($user);
if ($result > 0) //created successfully, set the eid to newly created entry
{
$eid = $price_expression->id;
setEventMessages($langs->trans("RecordSaved"), null, 'mesgs');
}
else
{
setEventMessages("add: ".$price_expression->error, $price_expression->errors, 'errors');
}
}
}
else if ($result < 0)
{
setEventMessages("add find: ".$price_expression->error, $price_expression->errors, 'errors');
}
else
{
setEventMessages($langs->trans("ErrorRecordAlreadyExists"), null, 'errors');
}
}
}
if ($action == 'update')
{
if ($eid != 0)
{
$result = $price_expression->find_title($title);
if ($result == 0 || $result == $eid) //No existing entry found with title or existing one is the current one, ok
{
//Check the expression validity by parsing it
$priceparser = new PriceParser($db);
$price_result = $priceparser->testExpression($id, $expression);
if ($price_result < 0) { //Expression is not valid
setEventMessages($priceparser->translatedError(), null, 'errors');
}
else
{
$price_expression->id = $eid;
$price_expression->title = $title;
$price_expression->expression = $expression;
$result = $price_expression->update($user);
if ($result < 0)
{
setEventMessages("update: ".$price_expression->error, $price_expression->errors, 'errors');
}
else
{
setEventMessages($langs->trans("RecordSaved"), null, 'mesgs');
}
}
}
else if ($result < 0)
{
setEventMessages("update find: ".$price_expression->error, $price_expression->errors, 'errors');
}
else
{
setEventMessages($langs->trans("ErrorRecordAlreadyExists"), null, 'errors');
}
}
}
if ($action == 'delete')
{
if ($eid != 0)
{
$price_expression->fetch($eid);
$result = $price_expression->delete($user);
if ($result < 0)
{
setEventMessages("delete: ".$price_expression->error, $price_expression->errors, 'errors');
}
$eid = 0;
}
}
/*
* View
*/
$form = new Form($db);
llxHeader("","",$langs->trans("CardProduct".$product->type));
print load_fiche_titre($langs->trans("PriceExpressionEditor"));
//Form/Table
print '
';
// This code reloads the page depending of selected option, goes to page selected by tab when back is pressed
print '';
// End of page
llxFooter();
$db->close();