From 99efbd3464170c8e72f550338548bb76ee4984a2 Mon Sep 17 00:00:00 2001 From: Ion Agorria Date: Sun, 14 Feb 2016 14:26:04 +0100 Subject: [PATCH 1/5] Check if dynamic price is enabled --- htdocs/core/boxes/box_produits.php | 4 ++-- htdocs/core/boxes/box_produits_alerte_stock.php | 4 ++-- htdocs/core/class/html.form.class.php | 6 +++--- htdocs/fourn/class/fournisseur.product.class.php | 5 +++-- htdocs/product/class/product.class.php | 7 ++++--- htdocs/product/fournisseurs.php | 2 +- htdocs/product/index.php | 3 ++- 7 files changed, 17 insertions(+), 14 deletions(-) diff --git a/htdocs/core/boxes/box_produits.php b/htdocs/core/boxes/box_produits.php index 042526224a2..c8ef1b5f558 100644 --- a/htdocs/core/boxes/box_produits.php +++ b/htdocs/core/boxes/box_produits.php @@ -116,11 +116,11 @@ class box_produits extends ModeleBoxes 'text' => $objp->label, ); - if (empty($objp->fk_price_expression)) { + if (empty($conf->dynamicprices->enabled) || empty($objp->fk_price_expression)) { $price_base_type=$langs->trans($objp->price_base_type); $price=($objp->price_base_type == 'HT')?price($objp->price):$price=price($objp->price_ttc); } - else //Parse the dinamic price + else //Parse the dynamic price { $productstatic->fetch($objp->rowid, '', '', 1); $priceparser = new PriceParser($this->db); diff --git a/htdocs/core/boxes/box_produits_alerte_stock.php b/htdocs/core/boxes/box_produits_alerte_stock.php index 66f1cbbfe3b..5a718b79696 100644 --- a/htdocs/core/boxes/box_produits_alerte_stock.php +++ b/htdocs/core/boxes/box_produits_alerte_stock.php @@ -123,12 +123,12 @@ class box_produits_alerte_stock extends ModeleBoxes 'text' => $objp->label, ); - if (empty($objp->fk_price_expression)) + if (empty($conf->dynamicprices->enabled) || empty($objp->fk_price_expression)) { $price_base_type=$langs->trans($objp->price_base_type); $price=($objp->price_base_type == 'HT')?price($objp->price):$price=price($objp->price_ttc); } - else //Parse the dinamic price + else //Parse the dynamic price { $productstatic->fetch($objp->rowid, '', '', 1); $priceparser = new PriceParser($this->db); diff --git a/htdocs/core/class/html.form.class.php b/htdocs/core/class/html.form.class.php index 668855672a5..a71f161fe37 100644 --- a/htdocs/core/class/html.form.class.php +++ b/htdocs/core/class/html.form.class.php @@ -1790,7 +1790,7 @@ class Form } else { - if (!empty($objp->fk_price_expression)) { + if (!empty($conf->dynamicprices->enabled) && !empty($objp->fk_price_expression)) { $price_product = new Product($this->db); $price_product->fetch($objp->rowid, '', '', 1); $priceparser = new PriceParser($this->db); @@ -2168,7 +2168,7 @@ class Form { $outqty=$objp->quantity; $outdiscount=$objp->remise_percent; - if (!empty($objp->fk_supplier_price_expression)) { + if (!empty($conf->dynamicprices->enabled) && !empty($objp->fk_supplier_price_expression)) { $priceparser = new PriceParser($this->db); $price_result = $priceparser->parseProductSupplier($objp->fk_product, $objp->fk_supplier_price_expression, $objp->quantity, $objp->tva_tx); if ($price_result >= 0) { @@ -2307,7 +2307,7 @@ class Form } $opt.= '>'.$objp->name.' - '.$objp->ref_fourn.' - '; - if (!empty($objp->fk_supplier_price_expression)) { + if (!empty($conf->dynamicprices->enabled) && !empty($objp->fk_supplier_price_expression)) { $priceparser = new PriceParser($this->db); $price_result = $priceparser->parseProductSupplier($objp->fk_product, $objp->fk_supplier_price_expression, $objp->quantity, $objp->tva_tx); if ($price_result >= 0) { diff --git a/htdocs/fourn/class/fournisseur.product.class.php b/htdocs/fourn/class/fournisseur.product.class.php index 41da86cd9bf..ed0dcb96680 100644 --- a/htdocs/fourn/class/fournisseur.product.class.php +++ b/htdocs/fourn/class/fournisseur.product.class.php @@ -355,6 +355,7 @@ class ProductFournisseur extends Product */ function fetch_product_fournisseur_price($rowid, $ignore_expression = 0) { + global $conf; $sql = "SELECT pfp.rowid, pfp.price, pfp.quantity, pfp.unitprice, pfp.remise_percent, pfp.remise, pfp.tva_tx, pfp.fk_availability,"; $sql.= " pfp.fk_soc, pfp.ref_fourn, pfp.fk_product, pfp.charges, pfp.unitcharges, pfp.fk_supplier_price_expression, pfp.delivery_time_days"; // , pfp.recuperableonly as fourn_tva_npr"; FIXME this field not exist in llx_product_fournisseur_price $sql.= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price as pfp"; @@ -473,7 +474,7 @@ class ProductFournisseur extends Product $prodfourn->fourn_tva_npr = $record["info_bits"]; $prodfourn->fk_supplier_price_expression = $record["fk_supplier_price_expression"]; - if (!empty($prodfourn->fk_supplier_price_expression)) { + if (!empty($conf->dynamicprices->enabled) && !empty($prodfourn->fk_supplier_price_expression)) { $priceparser = new PriceParser($this->db); $price_result = $priceparser->parseProductSupplier($prodid, $prodfourn->fk_supplier_price_expression, $prodfourn->fourn_qty, $prodfourn->fourn_tva_tx); if ($price_result >= 0) { @@ -572,7 +573,7 @@ class ProductFournisseur extends Product { $fourn_price = $record["price"]; $fourn_unitprice = $record["unitprice"]; - if (!empty($record["fk_supplier_price_expression"])) { + if (!empty($conf->dynamicprices->enabled) && !empty($record["fk_supplier_price_expression"])) { $priceparser = new PriceParser($this->db); $price_result = $priceparser->parseProductSupplier($prodid, $record["fk_supplier_price_expression"], $record["quantity"], $record["tva_tx"]); if ($price_result >= 0) { diff --git a/htdocs/product/class/product.class.php b/htdocs/product/class/product.class.php index 9ccb295406a..2891b2e1cf9 100644 --- a/htdocs/product/class/product.class.php +++ b/htdocs/product/class/product.class.php @@ -1278,6 +1278,7 @@ class Product extends CommonObject */ function get_buyprice($prodfournprice,$qty,$product_id=0,$fourn_ref=0) { + global $conf; $result = 0; // We do select by searching with qty and prodfournprice @@ -1294,7 +1295,7 @@ class Product extends CommonObject $obj = $this->db->fetch_object($resql); if ($obj && $obj->quantity > 0) // If found { - if (!empty($obj->fk_supplier_price_expression)) + if (!empty($conf->dynamicprices->enabled) && !empty($obj->fk_supplier_price_expression)) { require_once DOL_DOCUMENT_ROOT.'/product/dynamic_price/class/price_parser.class.php'; $priceparser = new PriceParser($this->db); @@ -1329,7 +1330,7 @@ class Product extends CommonObject $obj = $this->db->fetch_object($resql); if ($obj && $obj->quantity > 0) // If found { - if (!empty($obj->fk_supplier_price_expression)) + if (!empty($conf->dynamicprices->enabled) && !empty($obj->fk_supplier_price_expression)) { require_once DOL_DOCUMENT_ROOT.'/product/dynamic_price/class/price_parser.class.php'; $priceparser = new PriceParser($this->db); @@ -1785,7 +1786,7 @@ class Product extends CommonObject } } - if (!empty($this->fk_price_expression) && empty($ignore_expression)) + if (!empty($conf->dynamicprices->enabled) && !empty($this->fk_price_expression) && empty($ignore_expression)) { require_once DOL_DOCUMENT_ROOT.'/product/dynamic_price/class/price_parser.class.php'; $priceparser = new PriceParser($this->db); diff --git a/htdocs/product/fournisseurs.php b/htdocs/product/fournisseurs.php index 0fe2769803d..bd4d60b5370 100644 --- a/htdocs/product/fournisseurs.php +++ b/htdocs/product/fournisseurs.php @@ -225,7 +225,7 @@ if (empty($reshook)) } else { - if ($price_expression !== '') + if (!empty($conf->dynamicprices->enabled) && $price_expression !== '') { //Check the expression validity by parsing it $priceparser = new PriceParser($db); diff --git a/htdocs/product/index.php b/htdocs/product/index.php index ccba0f887eb..2fa1ec06fb3 100644 --- a/htdocs/product/index.php +++ b/htdocs/product/index.php @@ -307,7 +307,8 @@ if ($result) // Sell price if (empty($conf->global->PRODUIT_MULTIPRICES)) { - if (!empty($objp->fk_price_expression)) { + if (!empty($conf->dynamicprices->enabled) && !empty($objp->fk_price_expression)) + { $product = new Product($db); $product->fetch($objp->rowid); $priceparser = new PriceParser($db); From b1568a7319e7fd3b6fb49b41b9a6be6d261ae14b Mon Sep 17 00:00:00 2001 From: Ion Agorria Date: Sun, 14 Feb 2016 14:46:58 +0100 Subject: [PATCH 2/5] Better Add buttons in dynamicprice module --- htdocs/langs/en_US/products.lang | 2 ++ htdocs/product/admin/dynamic_prices.php | 23 ++++++++++++++++++----- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/htdocs/langs/en_US/products.lang b/htdocs/langs/en_US/products.lang index 3dfabdc124d..bf223e0e33e 100644 --- a/htdocs/langs/en_US/products.lang +++ b/htdocs/langs/en_US/products.lang @@ -296,6 +296,8 @@ ComposedProduct=Sub-product MinSupplierPrice=Minimum supplier price DynamicPriceConfiguration=Dynamic price configuration DynamicPriceDesc=On product card, with this module enabled, you should be able to set mathematic functions to calculate Customer or Supplier prices. Such function can use all mathematic operators, some constants and variables. You can set here the variables you want to be able and if the variable need an automatic update, the external URL to use to ask Dolibarr to update automaticaly the value. +AddVariable=Add Variable +AddUpdater=Add Updater GlobalVariables=Global variables VariableToUpdate=Variable to update GlobalVariableUpdaters=Global variable updaters diff --git a/htdocs/product/admin/dynamic_prices.php b/htdocs/product/admin/dynamic_prices.php index 84ec54433cd..7c9a46d8972 100644 --- a/htdocs/product/admin/dynamic_prices.php +++ b/htdocs/product/admin/dynamic_prices.php @@ -173,6 +173,16 @@ if ($action != 'create_updater' && $action != 'edit_updater') { print ''; } print ''; + + if (empty($action)) + { + //Action Buttons + print '
'; + print ''.$langs->trans("AddVariable").''; + print '
'; + //Separator is only need for updaters table is showed after buttons + print '

'; + } } //Global variable editor @@ -253,6 +263,14 @@ if ($action != 'create_variable' && $action != 'edit_variable') { print ''; } print ''; + + if (empty($action)) + { + //Action Buttons + print '
'; + print ''.$langs->trans("AddUpdater").''; + print '
'; + } } //Updater editor @@ -321,11 +339,6 @@ if ($action == 'create_updater' || $action == 'edit_updater') { print ''; print ''; print ''; -} else { - //Action Buttons - print '
'; - print ''.$langs->trans("Add").''; - print '
'; } llxFooter(); From 6bfc3f446f0106f6f8ae19349e76c40681cc4f57 Mon Sep 17 00:00:00 2001 From: Ion Agorria Date: Sun, 14 Feb 2016 14:50:10 +0100 Subject: [PATCH 3/5] Use setSupplierPriceExpression instead of product setPriceExpression --- htdocs/product/fournisseurs.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/htdocs/product/fournisseurs.php b/htdocs/product/fournisseurs.php index bd4d60b5370..535420d3243 100644 --- a/htdocs/product/fournisseurs.php +++ b/htdocs/product/fournisseurs.php @@ -235,8 +235,10 @@ if (empty($reshook)) setEventMessages($priceparser->translatedError(), null, 'errors'); } } - if (! $error && ! empty($conf->dynamicprices->enabled)) { - $ret=$object->setPriceExpression($price_expression); + if (! $error && ! empty($conf->dynamicprices->enabled)) + { + //Set the price expression for this supplier price + $ret=$object->setSupplierPriceExpression($price_expression); if ($ret < 0) { $error++; From 89657d043945dc24870d1aed482462aa16e12377 Mon Sep 17 00:00:00 2001 From: Ion Agorria Date: Sun, 14 Feb 2016 17:28:31 +0100 Subject: [PATCH 4/5] Changed Price Parser functions and added testExpression --- htdocs/core/class/html.form.class.php | 18 ++- .../fourn/class/fournisseur.product.class.php | 10 +- htdocs/product/class/product.class.php | 20 ++- .../class/price_parser.class.php | 139 +++++++++--------- htdocs/product/dynamic_price/editor.php | 6 +- htdocs/product/fournisseurs.php | 10 +- 6 files changed, 118 insertions(+), 85 deletions(-) diff --git a/htdocs/core/class/html.form.class.php b/htdocs/core/class/html.form.class.php index a71f161fe37..07ea1fc6edf 100644 --- a/htdocs/core/class/html.form.class.php +++ b/htdocs/core/class/html.form.class.php @@ -2084,7 +2084,7 @@ class Form $sql = "SELECT p.rowid, p.label, p.ref, p.price, p.duration,"; $sql.= " pfp.ref_fourn, pfp.rowid as idprodfournprice, pfp.price as fprice, pfp.quantity, pfp.remise_percent, pfp.remise, pfp.unitprice,"; - $sql.= " pfp.fk_supplier_price_expression, pfp.fk_product, pfp.tva_tx, s.nom as name"; + $sql.= " pfp.fk_supplier_price_expression, pfp.fk_product, pfp.tva_tx, pfp.fk_soc, s.nom as name"; $sql.= " FROM ".MAIN_DB_PREFIX."product as p"; $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."product_fournisseur_price as pfp ON p.rowid = pfp.fk_product"; if ($socid) $sql.= " AND pfp.fk_soc = ".$socid; @@ -2169,8 +2169,14 @@ class Form $outqty=$objp->quantity; $outdiscount=$objp->remise_percent; if (!empty($conf->dynamicprices->enabled) && !empty($objp->fk_supplier_price_expression)) { + $prod_supplier = new ProductFournisseur($this->db); + $prod_supplier->product_fourn_price_id = $objp->idprodfournprice; + $prod_supplier->id = $objp->fk_product; + $prod_supplier->fourn_qty = $objp->quantity; + $prod_supplier->fourn_tva_tx = $objp->tva_tx; + $prod_supplier->fk_supplier_price_expression = $objp->fk_supplier_price_expression; $priceparser = new PriceParser($this->db); - $price_result = $priceparser->parseProductSupplier($objp->fk_product, $objp->fk_supplier_price_expression, $objp->quantity, $objp->tva_tx); + $price_result = $priceparser->parseProductSupplier($prod_supplier); if ($price_result >= 0) { $objp->fprice = $price_result; if ($objp->quantity >= 1) @@ -2308,8 +2314,14 @@ class Form $opt.= '>'.$objp->name.' - '.$objp->ref_fourn.' - '; if (!empty($conf->dynamicprices->enabled) && !empty($objp->fk_supplier_price_expression)) { + $prod_supplier = new ProductFournisseur($this->db); + $prod_supplier->product_fourn_price_id = $objp->idprodfournprice; + $prod_supplier->id = $productid; + $prod_supplier->fourn_qty = $objp->quantity; + $prod_supplier->fourn_tva_tx = $objp->tva_tx; + $prod_supplier->fk_supplier_price_expression = $objp->fk_supplier_price_expression; $priceparser = new PriceParser($this->db); - $price_result = $priceparser->parseProductSupplier($objp->fk_product, $objp->fk_supplier_price_expression, $objp->quantity, $objp->tva_tx); + $price_result = $priceparser->parseProductSupplier($prod_supplier); if ($price_result >= 0) { $objp->fprice = $price_result; if ($objp->quantity >= 1) diff --git a/htdocs/fourn/class/fournisseur.product.class.php b/htdocs/fourn/class/fournisseur.product.class.php index ed0dcb96680..0bf0492b411 100644 --- a/htdocs/fourn/class/fournisseur.product.class.php +++ b/htdocs/fourn/class/fournisseur.product.class.php @@ -476,7 +476,7 @@ class ProductFournisseur extends Product if (!empty($conf->dynamicprices->enabled) && !empty($prodfourn->fk_supplier_price_expression)) { $priceparser = new PriceParser($this->db); - $price_result = $priceparser->parseProductSupplier($prodid, $prodfourn->fk_supplier_price_expression, $prodfourn->fourn_qty, $prodfourn->fourn_tva_tx); + $price_result = $priceparser->parseProductSupplier($prodfourn); if ($price_result >= 0) { $prodfourn->fourn_price = $price_result; $prodfourn->fourn_unitprice = null; //force recalculation of unitprice, as probably the price changed... @@ -574,8 +574,14 @@ class ProductFournisseur extends Product $fourn_price = $record["price"]; $fourn_unitprice = $record["unitprice"]; if (!empty($conf->dynamicprices->enabled) && !empty($record["fk_supplier_price_expression"])) { + $prod_supplier = new ProductFournisseur($this->db); + $prod_supplier->product_fourn_price_id = $record["product_fourn_price_id"]; + $prod_supplier->id = $prodid; + $prod_supplier->fourn_qty = $record["quantity"]; + $prod_supplier->fourn_tva_tx = $record["tva_tx"]; + $prod_supplier->fk_supplier_price_expression = $record["fk_supplier_price_expression"]; $priceparser = new PriceParser($this->db); - $price_result = $priceparser->parseProductSupplier($prodid, $record["fk_supplier_price_expression"], $record["quantity"], $record["tva_tx"]); + $price_result = $priceparser->parseProductSupplier($prod_supplier); if ($price_result >= 0) { $fourn_price = price2num($price_result,'MU'); if ($record["quantity"] != 0) diff --git a/htdocs/product/class/product.class.php b/htdocs/product/class/product.class.php index 2891b2e1cf9..721f3c38759 100644 --- a/htdocs/product/class/product.class.php +++ b/htdocs/product/class/product.class.php @@ -1298,8 +1298,14 @@ class Product extends CommonObject if (!empty($conf->dynamicprices->enabled) && !empty($obj->fk_supplier_price_expression)) { require_once DOL_DOCUMENT_ROOT.'/product/dynamic_price/class/price_parser.class.php'; - $priceparser = new PriceParser($this->db); - $price_result = $priceparser->parseProductSupplier($obj->fk_product, $obj->fk_supplier_price_expression, $obj->quantity, $obj->tva_tx); + $prod_supplier = new ProductFournisseur($this->db); + $prod_supplier->product_fourn_price_id = $obj->rowid; + $prod_supplier->id = $obj->fk_product; + $prod_supplier->fourn_qty = $obj->quantity; + $prod_supplier->fourn_tva_tx = $obj->tva_tx; + $prod_supplier->fk_supplier_price_expression = $obj->fk_supplier_price_expression; + $priceparser = new PriceParser($this->db); + $price_result = $priceparser->parseProductSupplier($prod_supplier); if ($price_result >= 0) { $obj->price = $price_result; } @@ -1333,8 +1339,14 @@ class Product extends CommonObject if (!empty($conf->dynamicprices->enabled) && !empty($obj->fk_supplier_price_expression)) { require_once DOL_DOCUMENT_ROOT.'/product/dynamic_price/class/price_parser.class.php'; - $priceparser = new PriceParser($this->db); - $price_result = $priceparser->parseProductSupplier($obj->fk_product, $obj->fk_supplier_price_expression, $obj->quantity, $obj->tva_tx); + $prod_supplier = new ProductFournisseur($this->db); + $prod_supplier->product_fourn_price_id = $obj->rowid; + $prod_supplier->id = $obj->fk_product; + $prod_supplier->fourn_qty = $obj->quantity; + $prod_supplier->fourn_tva_tx = $obj->tva_tx; + $prod_supplier->fk_supplier_price_expression = $obj->fk_supplier_price_expression; + $priceparser = new PriceParser($this->db); + $price_result = $priceparser->parseProductSupplier($prod_supplier); if ($result >= 0) { $obj->price = $price_result; } diff --git a/htdocs/product/dynamic_price/class/price_parser.class.php b/htdocs/product/dynamic_price/class/price_parser.class.php index 4a7bdb7acf6..eb1ef4e4345 100644 --- a/htdocs/product/dynamic_price/class/price_parser.class.php +++ b/htdocs/product/dynamic_price/class/price_parser.class.php @@ -131,6 +131,15 @@ class PriceParser public function parseExpression($product, $expression, $values) { global $user; + + //Check if empty + $expression = trim($expression); + if (empty($expression)) + { + $this->error = array(20, null); + return -2; + } + //Accessible product values by expressions $values = array_merge($values, array( "tva_tx" => $product->tva_tx, @@ -169,14 +178,6 @@ class PriceParser $values["global_".$entry->code] = $entry->value; } - //Check if empty - $expression = trim($expression); - if (empty($expression)) - { - $this->error = array(20, null); - return -2; - } - //Prepare the lib, parameters and values $em = new EvalMath(); $em->suppress_errors = true; //Don't print errors on page @@ -221,40 +222,11 @@ class PriceParser } /** - * Calculates product price based on product id and string expression - * - * @param Product $product The Product object to get information - * @param string $expression The expression to parse - * @param array $extra_values Any aditional values for expression - * @return int > 0 if OK, < 1 if KO - */ - public function parseProductExpression($product, $expression, $extra_values = array()) - { - //Get the supplier min - $productFournisseur = new ProductFournisseur($this->db); - $supplier_min_price = $productFournisseur->find_min_price_product_fournisseur($product->id); - - //Accessible values by expressions - $extra_values = array_merge($extra_values, array( - "supplier_min_price" => $supplier_min_price, - )); - - //Parse the expression and return the price, if not error occurred check if price is higher than min - $result = $this->parseExpression($product, $expression, $extra_values); - if (empty($this->error)) { - if ($result < $product->price_min) { - $result = $product->price_min; - } - } - return $result; - } - - /** - * Calculates product price based on product id and expression id + * Calculates product price based on product id and associated expression * * @param Product $product The Product object to get information * @param array $extra_values Any aditional values for expression - * @return int > 0 if OK, < 1 if KO + * @return int > 0 if OK, < 1 if KO */ public function parseProduct($product, $extra_values = array()) { @@ -266,55 +238,80 @@ class PriceParser return -1; } - //Parse the expression and return the price - return $this->parseProductExpression($product, $price_expression->expression, $extra_values); - } - - /** - * Calculates supplier product price based on product id and string expression - * - * @param int $product_id The Product id to get information - * @param string $expression The expression to parse - * @param int $quantity Supplier Min quantity - * @param int $tva_tx Supplier VAT rate - * @param array $extra_values Any aditional values for expression - * @return int > 0 if OK, < 1 if KO - */ - public function parseProductSupplierExpression($product_id, $expression, $quantity = null, $tva_tx = null, $extra_values = array()) - { - //Get the product data - $product = new ProductFournisseur($this->db); - $product->fetch($product_id, '', '', 1); + //Get the supplier min + $productFournisseur = new ProductFournisseur($this->db); + $supplier_min_price = $productFournisseur->find_min_price_product_fournisseur($product->id); //Accessible values by expressions $extra_values = array_merge($extra_values, array( - "supplier_quantity" => $quantity, - "supplier_tva_tx" => $tva_tx, + "supplier_min_price" => $supplier_min_price, )); - return $this->parseExpression($product, $expression, $extra_values); + + //Parse the expression and return the price, if not error occurred check if price is higher than min + $result = $this->parseExpression($product, $price_expression->expression, $extra_values); + if (empty($this->error)) { + if ($result < $product->price_min) { + $result = $product->price_min; + } + } + return $result; } /** - * Calculates supplier product price based on product id and expression id + * Calculates supplier product price based on product supplier price and associated expression * - * @param int $product_id The Product id to get information - * @param int $expression_id The expression to parse - * @param int $quantity Min quantity - * @param int $tva_tx VAT rate - * @param array $extra_values Any aditional values for expression + * @param ProductFournisseur $product_supplier The Product supplier object to get information + * @param array $extra_values Any aditional values for expression * @return int > 0 if OK, < 1 if KO */ - public function parseProductSupplier($product_id, $expression_id, $quantity = null, $tva_tx = null, $extra_values = array()) + public function parseProductSupplier($product_supplier, $extra_values = array()) { //Get the expression from db $price_expression = new PriceExpression($this->db); - $res = $price_expression->fetch($expression_id); - if ($res < 1) { + $res = $price_expression->fetch($product_supplier->fk_supplier_price_expression); + if ($res < 1) + { $this->error = array(19, null); return -1; } + //Get the product data (use ignore_expression to avoid possible recursion) + $product_supplier->fetch($product_supplier->id, '', '', 1); + + //Accessible values by expressions + $extra_values = array_merge($extra_values, array( + "supplier_quantity" => $product_supplier->fourn_qty, + "supplier_tva_tx" => $product_supplier->fourn_tva_tx, + )); + //Parse the expression and return the price - return $this->parseProductSupplierExpression($product_id, $price_expression->expression, $quantity, $tva_tx, $extra_values); + return $this->parseExpression($product_supplier, $price_expression->expression, $extra_values); + } + + /** + * Tests string expression for validity + * + * @param int $product_id The Product id to get information + * @param string $expression The expression to parse + * @param array $extra_values Any aditional values for expression + * @return int > 0 if OK, < 1 if KO + */ + public function testExpression($product_id, $expression, $extra_values = array()) + { + //Get the product data + $product = new Product($this->db); + $product->fetch($product_id, '', '', 1); + + //Values for product expressions + $extra_values = array_merge($extra_values, array( + "supplier_min_price" => 1, + )); + + //Values for supplier product expressions + $extra_values = array_merge($extra_values, array( + "supplier_quantity" => 2, + "supplier_tva_tx" => 3, + )); + return $this->parseExpression($product, $expression, $extra_values); } } \ No newline at end of file diff --git a/htdocs/product/dynamic_price/editor.php b/htdocs/product/dynamic_price/editor.php index d7d059ae10d..9b20371ea5b 100644 --- a/htdocs/product/dynamic_price/editor.php +++ b/htdocs/product/dynamic_price/editor.php @@ -16,7 +16,7 @@ */ /** - * \file htdocs/product/expression/editor.php + * \file htdocs/product/dynamic_price/editor.php * \ingroup product * \brief Page for editing expression */ @@ -73,7 +73,7 @@ if ($action == 'add') { //Check the expression validity by parsing it $priceparser = new PriceParser($db); - $price_result = $priceparser->parseProductSupplierExpression($id, $expression, 0, 0); + $price_result = $priceparser->testExpression($id, $expression); if ($price_result < 0) { //Expression is not valid setEventMessages($priceparser->translatedError(), null, 'errors'); } @@ -113,7 +113,7 @@ if ($action == 'update') { //Check the expression validity by parsing it $priceparser = new PriceParser($db); - $price_result = $priceparser->parseProductSupplierExpression($id, $expression, 0, 0); + $price_result = $priceparser->testExpression($id, $expression); if ($price_result < 0) { //Expression is not valid setEventMessages($priceparser->translatedError(), null, 'errors'); } diff --git a/htdocs/product/fournisseurs.php b/htdocs/product/fournisseurs.php index 535420d3243..db6adb52648 100644 --- a/htdocs/product/fournisseurs.php +++ b/htdocs/product/fournisseurs.php @@ -228,8 +228,14 @@ if (empty($reshook)) if (!empty($conf->dynamicprices->enabled) && $price_expression !== '') { //Check the expression validity by parsing it - $priceparser = new PriceParser($db); - $price_result = $priceparser->parseProductSupplier($id, $price_expression, $quantity, $tva_tx); + $prod_supplier = new ProductFournisseur($this->db); + $prod_supplier->id = $prodid; + $prod_supplier->fourn_qty = $quantity; + $prod_supplier->fourn_tva_tx = $tva_tx; + $prod_supplier->fourn_id = $id_fourn; + $prod_supplier->fk_supplier_price_expression = $price_expression; + $priceparser = new PriceParser($db); + $price_result = $priceparser->parseProductSupplier($prod_supplier); if ($price_result < 0) { //Expression is not valid $error++; setEventMessages($priceparser->translatedError(), null, 'errors'); From 0e2be880c5591b49e18622479b68871a8a3aa024 Mon Sep 17 00:00:00 2001 From: Ion Agorria Date: Sun, 14 Feb 2016 19:05:43 +0100 Subject: [PATCH 5/5] Convert null to string --- htdocs/product/dynamic_price/class/price_parser.class.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/htdocs/product/dynamic_price/class/price_parser.class.php b/htdocs/product/dynamic_price/class/price_parser.class.php index eb1ef4e4345..0f4ff5a0dbb 100644 --- a/htdocs/product/dynamic_price/class/price_parser.class.php +++ b/htdocs/product/dynamic_price/class/price_parser.class.php @@ -188,7 +188,8 @@ class PriceParser $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); + if ($value === null) $value = "NULL"; + $expression = str_replace($this->special_chr.$key.$this->special_chr, strval($value), $expression); } $expressions = explode($this->separator_chr, $expression); $expressions = array_slice($expressions, 0, $this->limit);