Merge pull request #7796 from IonAgorria/fix-price-dynamic

Fix: price dynamic bugs
This commit is contained in:
Laurent Destailleur 2017-11-11 17:49:00 +01:00 committed by GitHub
commit 7e1e710d28
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 54 additions and 29 deletions

View File

@ -432,7 +432,7 @@ class ProductFournisseur extends Product
if (empty($ignore_expression) && !empty($this->fk_supplier_price_expression))
{
$priceparser = new PriceParser($this->db);
$price_result = $priceparser->parseProductSupplier($this->fk_product, $this->fk_supplier_price_expression, $this->fourn_qty, $this->fourn_tva_tx);
$price_result = $priceparser->parseProductSupplier($this);
if ($price_result >= 0) {
$this->fourn_price = $price_result;
//recalculation of unitprice, as probably the price changed...

View File

@ -155,6 +155,7 @@ ErrorPriceExpression19=Expression not found
ErrorPriceExpression20=Empty expression
ErrorPriceExpression21=Empty result '%s'
ErrorPriceExpression22=Negative result '%s'
ErrorPriceExpression23=Unknown or non set variable '%s' in %s
ErrorPriceExpressionInternal=Internal error '%s'
ErrorPriceExpressionUnknown=Unknown error '%s'
ErrorSrcAndTargetWarehouseMustDiffers=Source and target warehouses must differs

View File

@ -74,7 +74,7 @@ class PriceExpression
$this->db->begin();
dol_syslog(get_class($this)."::create", LOG_DEBUG);
dol_syslog(__METHOD__, LOG_DEBUG);
$resql=$this->db->query($sql);
if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
@ -121,11 +121,18 @@ class PriceExpression
*/
function fetch($id)
{
// Check parameters
if (empty($id))
{
$this->error='ErrorWrongParameters';
return -1;
}
$sql = "SELECT title, expression";
$sql.= " FROM ".MAIN_DB_PREFIX.$this->table_element;
$sql.= " WHERE rowid = ".$id;
dol_syslog(get_class($this)."::fetch");
dol_syslog(__METHOD__);
$resql=$this->db->query($sql);
if ($resql)
{
@ -160,7 +167,7 @@ class PriceExpression
$sql.= " FROM ".MAIN_DB_PREFIX.$this->table_element;
$sql.= " ORDER BY title";
dol_syslog(get_class($this)."::list_price_expression");
dol_syslog(__METHOD__, LOG_DEBUG);
$resql=$this->db->query($sql);
if ($resql)
{
@ -198,7 +205,7 @@ class PriceExpression
$sql.= " FROM ".MAIN_DB_PREFIX.$this->table_element;
$sql.= " WHERE title = '".$this->db->escape($title)."'";
dol_syslog(get_class($this)."::find_title");
dol_syslog(__METHOD__, LOG_DEBUG);
$resql=$this->db->query($sql);
if ($resql)
{
@ -243,7 +250,7 @@ class PriceExpression
$this->db->begin();
dol_syslog(get_class($this)."::update");
dol_syslog(__METHOD__);
$resql = $this->db->query($sql);
if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
@ -266,7 +273,7 @@ class PriceExpression
{
foreach($this->errors as $errmsg)
{
dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
$this->error.=($this->error?', '.$errmsg:$errmsg);
}
$this->db->rollback();
@ -314,7 +321,7 @@ class PriceExpression
$sql = "DELETE FROM ".MAIN_DB_PREFIX.$this->table_element;
$sql.= " WHERE rowid = ".$rowid;
dol_syslog(get_class($this)."::delete");
dol_syslog(__METHOD__);
$resql = $this->db->query($sql);
if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
}
@ -324,7 +331,7 @@ class PriceExpression
{
foreach($this->errors as $errmsg)
{
dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
$this->error.=($this->error?', '.$errmsg:$errmsg);
}
$this->db->rollback();

View File

@ -74,7 +74,7 @@ class PriceGlobalVariable
$this->db->begin();
dol_syslog(get_class($this)."::create", LOG_DEBUG);
dol_syslog(__METHOD__);
$resql=$this->db->query($sql);
if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
@ -125,7 +125,7 @@ class PriceGlobalVariable
$sql.= " FROM ".MAIN_DB_PREFIX.$this->table_element;
$sql.= " WHERE rowid = ".$id;
dol_syslog(get_class($this)."::fetch");
dol_syslog(__METHOD__);
$resql=$this->db->query($sql);
if ($resql)
{
@ -173,7 +173,7 @@ class PriceGlobalVariable
$this->db->begin();
dol_syslog(get_class($this)."::update");
dol_syslog(__METHOD__);
$resql = $this->db->query($sql);
if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
@ -196,7 +196,7 @@ class PriceGlobalVariable
{
foreach($this->errors as $errmsg)
{
dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
$this->error.=($this->error?', '.$errmsg:$errmsg);
}
$this->db->rollback();
@ -243,7 +243,7 @@ class PriceGlobalVariable
$sql = "DELETE FROM ".MAIN_DB_PREFIX.$this->table_element;
$sql.= " WHERE rowid = ".$rowid;
dol_syslog(get_class($this)."::delete");
dol_syslog(__METHOD__);
$resql = $this->db->query($sql);
if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
}
@ -253,7 +253,7 @@ class PriceGlobalVariable
{
foreach($this->errors as $errmsg)
{
dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
$this->error.=($this->error?', '.$errmsg:$errmsg);
}
$this->db->rollback();
@ -306,7 +306,7 @@ class PriceGlobalVariable
$sql.= " FROM ".MAIN_DB_PREFIX.$this->table_element;
$sql.= " ORDER BY code";
dol_syslog(get_class($this)."::listGlobalVariables");
dol_syslog(__METHOD__, LOG_DEBUG);
$resql=$this->db->query($sql);
if ($resql)
{

View File

@ -84,7 +84,7 @@ class PriceGlobalVariableUpdater
$this->db->begin();
dol_syslog(get_class($this)."::create", LOG_DEBUG);
dol_syslog(__METHOD__, LOG_DEBUG);
$resql=$this->db->query($sql);
if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
@ -135,7 +135,7 @@ class PriceGlobalVariableUpdater
$sql.= " FROM ".MAIN_DB_PREFIX.$this->table_element;
$sql.= " WHERE rowid = ".$id;
dol_syslog(get_class($this)."::fetch");
dol_syslog(__METHOD__);
$resql=$this->db->query($sql);
if ($resql)
{
@ -191,7 +191,7 @@ class PriceGlobalVariableUpdater
$this->db->begin();
dol_syslog(get_class($this)."::update");
dol_syslog(__METHOD__);
$resql = $this->db->query($sql);
if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
@ -260,7 +260,7 @@ class PriceGlobalVariableUpdater
$sql = "DELETE FROM ".MAIN_DB_PREFIX.$this->table_element;
$sql.= " WHERE rowid = ".$rowid;
dol_syslog(get_class($this)."::delete");
dol_syslog(__METHOD__);
$resql = $this->db->query($sql);
if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
}
@ -270,7 +270,7 @@ class PriceGlobalVariableUpdater
{
foreach($this->errors as $errmsg)
{
dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
$this->error.=($this->error?', '.$errmsg:$errmsg);
}
$this->db->rollback();
@ -346,7 +346,7 @@ class PriceGlobalVariableUpdater
$sql = "SELECT rowid, type, description, parameters, fk_variable, update_interval, next_update, last_status";
$sql.= " FROM ".MAIN_DB_PREFIX.$this->table_element;
dol_syslog(get_class($this)."::listUpdaters");
dol_syslog(__METHOD__, LOG_DEBUG);
$resql=$this->db->query($sql);
if ($resql)
{
@ -388,7 +388,7 @@ class PriceGlobalVariableUpdater
$sql.= " FROM ".MAIN_DB_PREFIX.$this->table_element;
$sql.= " WHERE next_update < ".dol_now();
dol_syslog(get_class($this)."::processUpdaters");
dol_syslog(__METHOD__, LOG_DEBUG);
$resql=$this->db->query($sql);
if ($resql)
{
@ -428,6 +428,7 @@ class PriceGlobalVariableUpdater
{
global $langs, $user;
$langs->load("errors");
dol_syslog(__METHOD__, LOG_DEBUG);
$this->error = null;
$this->checkParameters();
@ -551,7 +552,7 @@ class PriceGlobalVariableUpdater
$this->db->begin();
dol_syslog(get_class($this)."::update_next_update");
dol_syslog(__METHOD__);
$resql = $this->db->query($sql);
if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
@ -560,7 +561,7 @@ class PriceGlobalVariableUpdater
{
foreach($this->errors as $errmsg)
{
dol_syslog(get_class($this)."::update_next_update ".$errmsg, LOG_ERR);
dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
$this->error.=($this->error?', '.$errmsg:$errmsg);
}
$this->db->rollback();
@ -595,7 +596,7 @@ class PriceGlobalVariableUpdater
$this->db->begin();
dol_syslog(get_class($this)."::update_status");
dol_syslog(__METHOD__);
$resql = $this->db->query($sql);
if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
@ -604,7 +605,7 @@ class PriceGlobalVariableUpdater
{
foreach($this->errors as $errmsg)
{
dol_syslog(get_class($this)."::update_status ".$errmsg, LOG_ERR);
dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
$this->error.=($this->error?', '.$errmsg:$errmsg);
}
$this->db->rollback();

View File

@ -85,6 +85,7 @@ class PriceParser
-2 Args
6, wrong number of arguments (%s given, %s expected)
23, unknown or non set variable '%s' after %s
-internal errors
7, internal error
@ -178,19 +179,34 @@ class PriceParser
$values["global_".$entry->code] = $entry->value;
}
//Remove internal variables
unset($values["supplier_id"]);
//Prepare the lib, parameters and values
$em = new EvalMath();
$em->suppress_errors = true; //Don't print errors on page
$this->error_expr = null;
$last_result = null;
//Iterate over each expression splitted by $separator_chr
//Fill each variable in expression from values
$expression = str_replace("\n", $this->separator_chr, $expression);
foreach ($values as $key => $value)
{
if ($value === null) $value = "NULL";
$expression = str_replace($this->special_chr.$key.$this->special_chr, strval($value), $expression);
}
//Check if there is unfilled variable
if (strpos($expression, $this->special_chr) !== false)
{
$data = explode($this->special_chr, $expression);
$variable = $this->special_chr.$data[1];
if (isset($data[2])) $variable.= $this->special_chr;
$this->error_parser = array(23, array($variable, $expression));
return -6;
}
//Iterate over each expression splitted by $separator_chr
$expressions = explode($this->separator_chr, $expression);
$expressions = array_slice($expressions, 0, $this->limit);
foreach ($expressions as $expr) {
@ -241,7 +257,7 @@ class PriceParser
//Get the supplier min
$productFournisseur = new ProductFournisseur($this->db);
$supplier_min_price = $productFournisseur->find_min_price_product_fournisseur($product->id);
$supplier_min_price = $productFournisseur->find_min_price_product_fournisseur($product->id, 0, 0);
//Accessible values by expressions
$extra_values = array_merge($extra_values, array(