Relocation of expression files

This commit is contained in:
Ion Agorria 2015-03-14 16:48:56 +01:00
parent c197e00090
commit fc5899174b
3 changed files with 88 additions and 59 deletions

View File

@ -1,7 +1,7 @@
<?php <?php
/* Copyright (C) 2007-2012 Laurent Destailleur <eldy@users.sourceforge.net> /* Copyright (C) 2007-2012 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2014 Juanjo Menent <jmenent@2byte.es> * Copyright (C) 2014 Juanjo Menent <jmenent@2byte.es>
/* Copyright (C) 2014 Ion Agorria <ion@agorria.com> /* Copyright (C) 2015 Ion Agorria <ion@agorria.com>
* *
* This program is free software; you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@ -18,7 +18,7 @@
*/ */
/** /**
* \file htdocs/product/class/priceexpression.class.php * \file htdocs/product/expression/class/price_expression.class.php
* \ingroup product * \ingroup product
* \brief Class for accessing price expression table * \brief Class for accessing price expression table
*/ */
@ -35,6 +35,7 @@ class PriceExpression
var $id; var $id;
var $title; var $title;
var $expression; var $expression;
public $table_element = "c_price_expression";
/** /**
* Constructor * Constructor
@ -64,7 +65,7 @@ class PriceExpression
if (isset($this->expression)) $this->expression=trim($this->expression); if (isset($this->expression)) $this->expression=trim($this->expression);
// Insert request // Insert request
$sql = "INSERT INTO ".MAIN_DB_PREFIX."c_price_expression ("; $sql = "INSERT INTO ".MAIN_DB_PREFIX.$this->table_element." (";
$sql.= "title, expression"; $sql.= "title, expression";
$sql.= ") VALUES ("; $sql.= ") VALUES (";
$sql.= " ".(isset($this->title)?"'".$this->db->escape($this->title)."'":"''").","; $sql.= " ".(isset($this->title)?"'".$this->db->escape($this->title)."'":"''").",";
@ -121,7 +122,7 @@ class PriceExpression
function fetch($id) function fetch($id)
{ {
$sql = "SELECT title, expression"; $sql = "SELECT title, expression";
$sql.= " FROM ".MAIN_DB_PREFIX."c_price_expression"; $sql.= " FROM ".MAIN_DB_PREFIX.$this->table_element;
$sql.= " WHERE rowid = ".$id; $sql.= " WHERE rowid = ".$id;
dol_syslog(get_class($this)."::fetch"); dol_syslog(get_class($this)."::fetch");
@ -156,7 +157,7 @@ class PriceExpression
function list_price_expression() function list_price_expression()
{ {
$sql = "SELECT rowid, title, expression"; $sql = "SELECT rowid, title, expression";
$sql.= " FROM ".MAIN_DB_PREFIX."c_price_expression"; $sql.= " FROM ".MAIN_DB_PREFIX.$this->table_element;
$sql.= " ORDER BY title"; $sql.= " ORDER BY title";
dol_syslog(get_class($this)."::list_price_expression"); dol_syslog(get_class($this)."::list_price_expression");
@ -194,7 +195,7 @@ class PriceExpression
function find_title($title) function find_title($title)
{ {
$sql = "SELECT rowid"; $sql = "SELECT rowid";
$sql.= " FROM ".MAIN_DB_PREFIX."c_price_expression"; $sql.= " FROM ".MAIN_DB_PREFIX.$this->table_element;
$sql.= " WHERE title = '".$this->db->escape($title)."'"; $sql.= " WHERE title = '".$this->db->escape($title)."'";
dol_syslog(get_class($this)."::find_title"); dol_syslog(get_class($this)."::find_title");
@ -235,7 +236,7 @@ class PriceExpression
if (isset($this->expression)) $this->expression=trim($this->expression); if (isset($this->expression)) $this->expression=trim($this->expression);
// Update request // Update request
$sql = "UPDATE ".MAIN_DB_PREFIX."c_price_expression SET"; $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET";
$sql.= " title = ".(isset($this->title)?"'".$this->db->escape($this->title)."'":"''").","; $sql.= " title = ".(isset($this->title)?"'".$this->db->escape($this->title)."'":"''").",";
$sql.= " expression = ".(isset($this->expression)?"'".$this->db->escape($this->expression)."'":"''").""; $sql.= " expression = ".(isset($this->expression)?"'".$this->db->escape($this->expression)."'":"''")."";
$sql.= " WHERE rowid = ".$this->id; $sql.= " WHERE rowid = ".$this->id;
@ -309,7 +310,7 @@ class PriceExpression
if (! $error) if (! $error)
{ {
$sql = "DELETE FROM ".MAIN_DB_PREFIX."c_price_expression"; $sql = "DELETE FROM ".MAIN_DB_PREFIX.$this->table_element;
$sql.= " WHERE rowid = ".$rowid; $sql.= " WHERE rowid = ".$rowid;
dol_syslog(get_class($this)."::delete"); dol_syslog(get_class($this)."::delete");

View File

@ -1,5 +1,5 @@
<?php <?php
/* Copyright (C) 2014 Ion Agorria <ion@agorria.com> /* Copyright (C) 2015 Ion Agorria <ion@agorria.com>
* *
* This program is free software; you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@ -16,13 +16,15 @@
*/ */
/** /**
* \file htdocs/product/class/priceparser.class.php * \file htdocs/product/expression/class/price_parser.class.php
* \ingroup product * \ingroup product
* \brief File of class to calculate prices using expression * \brief File of class to calculate prices using expression
*/ */
require_once DOL_DOCUMENT_ROOT.'/includes/evalmath/evalmath.class.php'; require_once DOL_DOCUMENT_ROOT.'/includes/evalmath/evalmath.class.php';
require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.product.class.php'; require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.product.class.php';
require_once DOL_DOCUMENT_ROOT.'/product/class/priceexpression.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_global_variable_updater.class.php';
require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php'; require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
/** /**
@ -33,7 +35,7 @@ class PriceParser
protected $db; protected $db;
// Limit of expressions per price // Limit of expressions per price
public $limit = 100; public $limit = 100;
// The error that ocurred when parsing price // The error that occurred when parsing price
public $error; public $error;
// The expression that caused the error // The expression that caused the error
public $error_expr; public $error_expr;
@ -104,7 +106,7 @@ class PriceParser
{ {
return $langs->trans("ErrorPriceExpression".$code, $info); return $langs->trans("ErrorPriceExpression".$code, $info);
} }
else if (in_array($code, array(6))) //Errors which have 2 args else if (in_array($code, array(6, 23))) //Errors which have 2 args
{ {
return $langs->trans("ErrorPriceExpression".$code, $info[0], $info[1]); return $langs->trans("ErrorPriceExpression".$code, $info[0], $info[1]);
} }
@ -128,6 +130,7 @@ class PriceParser
*/ */
public function parseExpression($product, $expression, $values) public function parseExpression($product, $expression, $values)
{ {
global $user;
//Accessible product values by expressions //Accessible product values by expressions
$values = array_merge($values, array( $values = array_merge($values, array(
"tva_tx" => $product->tva_tx, "tva_tx" => $product->tva_tx,
@ -139,13 +142,31 @@ class PriceParser
"price_min" => $product->price_min, "price_min" => $product->price_min,
)); ));
//Retreive all extrafield for product and add it to values //Retrieve all extrafield for product and add it to values
$extrafields = new ExtraFields($this->db); $extrafields = new ExtraFields($this->db);
$extralabels = $extrafields->fetch_name_optionals_label('product', true); $extralabels = $extrafields->fetch_name_optionals_label('product', true);
$product->fetch_optionals($product->id, $extralabels); $product->fetch_optionals($product->id, $extralabels);
foreach ($extrafields->attribute_label as $key=>$label) foreach ($extrafields->attribute_label as $key=>$label)
{ {
$values['options_'.$key] = $product->array_options['options_'.$key]; $values["extrafield_".$key] = $product->array_options['options_'.$key];
}
//Process any pending updaters
$price_updaters = new PriceGlobalVariableUpdater($this->db);
foreach ($price_updaters->listPendingUpdaters() as $entry) {
//Schedule the next update by adding current timestamp (secs) + interval (mins)
$entry->update_next_update(dol_now() + ($entry->update_interval * 60), $user);
//Do processing
$res = $entry->process();
//Store any error or clear status if OK
$entry->update_status($res < 1?$entry->error:'', $user);
}
//Get all global values
$price_globals = new PriceGlobalVariable($this->db);
foreach ($price_globals->listGlobalVariables() as $entry)
{
$values["global_".$entry->code] = $entry->value;
} }
//Check if empty //Check if empty
@ -153,36 +174,32 @@ class PriceParser
if (empty($expression)) if (empty($expression))
{ {
$this->error = array(20, null); $this->error = array(20, null);
return -1; return -2;
} }
//Prepare the lib, parameters and values //Prepare the lib, parameters and values
$em = new EvalMath(); $em = new EvalMath();
$em->suppress_errors = true; //Don't print errors on page $em->suppress_errors = true; //Don't print errors on page
$this->error_expr = null; $this->error_expr = null;
$search = array(); $last_result = null;
$replace = array();
foreach ($values as $key => $value) {
if ($value !== null) {
$search[] = $this->special_chr.$key.$this->special_chr;
$replace[] = $value;
}
}
//Iterate over each expression splitted by $separator_chr //Iterate over each expression splitted by $separator_chr
$expression = str_replace("\n", $this->separator_chr, $expression); $expression = str_replace("\n", $this->separator_chr, $expression);
foreach ($values as $key => $value)
{
$expression = str_replace($this->special_chr.$key.$this->special_chr, "$value", $expression);
}
$expressions = explode($this->separator_chr, $expression); $expressions = explode($this->separator_chr, $expression);
$expressions = array_slice($expressions, 0, $limit); $expressions = array_slice($expressions, 0, $this->limit);
foreach ($expressions as $expr) { foreach ($expressions as $expr) {
$expr = trim($expr); $expr = trim($expr);
if (!empty($expr)) if (!empty($expr))
{ {
$expr = str_ireplace($search, $replace, $expr);
$last_result = $em->evaluate($expr); $last_result = $em->evaluate($expr);
$this->error = $em->last_error_code; $this->error = $em->last_error_code;
if ($this->error !== null) { //$em->last_error is null if no error happened, so just check if error is not null if ($this->error !== null) { //$em->last_error is null if no error happened, so just check if error is not null
$this->error_expr = $expr; $this->error_expr = $expr;
return -2; return -3;
} }
} }
} }
@ -190,15 +207,15 @@ class PriceParser
if (empty($vars["price"])) { if (empty($vars["price"])) {
$vars["price"] = $last_result; $vars["price"] = $last_result;
} }
if ($vars["price"] === null) if (!isset($vars["price"]))
{ {
$this->error = array(21, $expression); $this->error = array(21, $expression);
return -3; return -4;
} }
if ($vars["price"] < 0) if ($vars["price"] < 0)
{ {
$this->error = array(22, $expression); $this->error = array(22, $expression);
return -4; return -5;
} }
return $vars["price"]; return $vars["price"];
} }

View File

@ -16,16 +16,17 @@
*/ */
/** /**
* \file htdocs/product/expression.php * \file htdocs/product/expression/editor.php
* \ingroup product * \ingroup product
* \brief Page for editing expression * \brief Page for editing expression
*/ */
require '../main.inc.php'; require '../../main.inc.php';
require_once DOL_DOCUMENT_ROOT.'/core/lib/product.lib.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.'/fourn/class/fournisseur.product.class.php';
require_once DOL_DOCUMENT_ROOT.'/product/class/priceexpression.class.php'; require_once DOL_DOCUMENT_ROOT.'/product/dynamic_price/class/price_expression.class.php';
require_once DOL_DOCUMENT_ROOT.'/product/class/priceparser.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';
$langs->load("products"); $langs->load("products");
$langs->load("accountancy"); //"Back" translation is on this file $langs->load("accountancy"); //"Back" translation is on this file
@ -47,6 +48,7 @@ $product = new Product($db);
$product->fetch($id, ''); $product->fetch($id, '');
$price_expression = new PriceExpression($db); $price_expression = new PriceExpression($db);
$price_globals = new PriceGlobalVariable($db);
//Fetch expression data //Fetch expression data
if (empty($eid)) //This also disables fetch when eid == 0 if (empty($eid)) //This also disables fetch when eid == 0
@ -184,8 +186,17 @@ print '<tr><td class="fieldrequired">'.$langs->trans("Name").'</td><td>';
print '<input class="flat" name="expression_title" size="15" value="'.($price_expression->title?$price_expression->title:'').'">'; print '<input class="flat" name="expression_title" size="15" value="'.($price_expression->title?$price_expression->title:'').'">';
print '</td></tr>'; print '</td></tr>';
//Help text
$help_text = $langs->trans("PriceExpressionEditorHelp1");
$help_text.= '<br><br>'.$langs->trans("PriceExpressionEditorHelp2");
$help_text.= '<br><br>'.$langs->trans("PriceExpressionEditorHelp3");
$help_text.= '<br><br>'.$langs->trans("PriceExpressionEditorHelp4");
$help_text.= '<br><br>'.$langs->trans("PriceExpressionEditorHelp5");
foreach ($price_globals->listGlobalVariables() as $entry) {
$help_text.= '<br><b>#globals_'.$entry->code.'#</b> '.$entry->description.' = '.$entry->value;
}
//Price expression editor //Price expression editor
$help_text = $langs->trans("PriceExpressionEditorHelp1").'<br><br>'.$langs->trans("PriceExpressionEditorHelp2").'<br><br>'.$langs->trans("PriceExpressionEditorHelp3").'<br><br>'.$langs->trans("PriceExpressionEditorHelp4");
print '<tr><td class="fieldrequired">'.$form->textwithpicto($langs->trans("PriceExpressionEditor"),$help_text,1).'</td><td>'; print '<tr><td class="fieldrequired">'.$form->textwithpicto($langs->trans("PriceExpressionEditor"),$help_text,1).'</td><td>';
require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php'; require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
$doleditor=new DolEditor('expression',isset($price_expression->expression)?$price_expression->expression:'','',300,'','',false,false,false,4,80); $doleditor=new DolEditor('expression',isset($price_expression->expression)?$price_expression->expression:'','',300,'','',false,false,false,4,80);
@ -194,7 +205,7 @@ print '</td></tr>';
print '</table>'; print '</table>';
//Buttons //Buttons
print '<center>'; print '<div style="text-align: center;">';
print '<input type="submit" class="butAction" value="'.$langs->trans("Save").'">'; print '<input type="submit" class="butAction" value="'.$langs->trans("Save").'">';
print '<span id="back" class="butAction">'.$langs->trans("Back").'</span>'; print '<span id="back" class="butAction">'.$langs->trans("Back").'</span>';
if ($eid == 0) if ($eid == 0)
@ -205,7 +216,7 @@ else
{ {
print '<div class="inline-block divButAction"><a class="butActionDelete" href="'.$_SERVER["PHP_SELF"].'?id='.$id.'&amp;tab='.$tab.'&amp;eid='.$eid.'&amp;action=delete">'.$langs->trans("Delete").'</a></div>'; print '<div class="inline-block divButAction"><a class="butActionDelete" href="'.$_SERVER["PHP_SELF"].'?id='.$id.'&amp;tab='.$tab.'&amp;eid='.$eid.'&amp;action=delete">'.$langs->trans("Delete").'</a></div>';
} }
print '</center>'; print '</div>';
print '</form>'; print '</form>';
@ -217,7 +228,7 @@ print '<script type="text/javascript">
jQuery("#expression_selection").change(on_change); jQuery("#expression_selection").change(on_change);
} }
function on_click() { function on_click() {
window.location = "'.str_replace('expression.php', $tab.'.php', $_SERVER["PHP_SELF"]).'?id='.$id.($tab == 'price' ? '&action=edit_price' : '').'"; window.location = "'.str_replace('dynamic_price/editor.php', $tab.'.php', $_SERVER["PHP_SELF"]).'?id='.$id.($tab == 'price' ? '&action=edit_price' : '').'";
} }
function on_change() { function on_change() {
window.location = "'.$_SERVER["PHP_SELF"].'?id='.$id.'&tab='.$tab.'&eid=" + $("#expression_selection").attr("value"); window.location = "'.$_SERVER["PHP_SELF"].'?id='.$id.'&tab='.$tab.'&eid=" + $("#expression_selection").attr("value");