23 require_once DOL_DOCUMENT_ROOT.
'/core/class/evalmath.class.php';
24 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.product.class.php';
25 require_once DOL_DOCUMENT_ROOT.
'/product/dynamic_price/class/price_expression.class.php';
26 require_once DOL_DOCUMENT_ROOT.
'/product/dynamic_price/class/price_global_variable.class.php';
27 require_once DOL_DOCUMENT_ROOT.
'/product/dynamic_price/class/price_global_variable_updater.class.php';
28 require_once DOL_DOCUMENT_ROOT.
'/core/class/extrafields.class.php';
43 public $special_chr =
"#";
45 public $separator_chr =
";";
65 $langs->load(
"errors");
99 if (empty($this->error_parser)) {
100 return $langs->trans(
"ErrorPriceExpressionUnknown", 0);
102 list($code, $info) = $this->error_parser;
103 if (in_array($code, array(9, 14, 19, 20))) {
104 return $langs->trans(
"ErrorPriceExpression".$code);
105 } elseif (in_array($code, array(1, 2, 3, 4, 5, 8, 10, 11, 17, 21, 22))) {
106 return $langs->trans(
"ErrorPriceExpression".$code, $info);
107 } elseif (in_array($code, array(6, 23))) {
108 return $langs->trans(
"ErrorPriceExpression".$code, $info[0], $info[1]);
109 } elseif (in_array($code, array(7, 12, 13, 15, 16, 18))) {
110 return $langs->trans(
"ErrorPriceExpressionInternal", $code);
113 return $langs->trans(
"ErrorPriceExpressionUnknown", $code);
129 $action =
'PARSEEXPRESSION';
130 if ($result = $hookmanager->executeHooks(
'doDynamiPrice', array(
131 'expression' =>$expression,
132 'product' => $product,
134 ), $this, $action)) {
138 $expression = trim($expression);
139 if (empty($expression)) {
140 $this->error_parser = array(20,
null);
145 $values = array_merge($values, array(
146 "tva_tx" => $product->tva_tx,
147 "localtax1_tx" => $product->localtax1_tx,
148 "localtax2_tx" => $product->localtax2_tx,
149 "weight" => $product->weight,
150 "length" => $product->length,
151 "surface" => $product->surface,
152 "price_min" => $product->price_min,
153 "cost_price" => $product->cost_price,
154 "pmp" => $product->pmp,
159 $product->fetch_optionals();
160 if (is_array($extrafields->attributes[$product->table_element][
'label'])) {
161 foreach ($extrafields->attributes[$product->table_element][
'label'] as $key => $label) {
162 $values[
"extrafield_".$key] = $product->array_options[
'options_'.$key];
168 foreach ($price_updaters->listPendingUpdaters() as $entry) {
170 $entry->update_next_update(
dol_now() + ($entry->update_interval * 60), $user);
172 $res = $entry->process();
174 $entry->update_status($res < 1 ? $entry->error :
'', $user);
179 foreach ($price_globals->listGlobalVariables() as $entry) {
180 $values[
"global_".$entry->code] = $entry->value;
184 unset($values[
"supplier_id"]);
188 $em->suppress_errors =
true;
189 $this->error_expr =
null;
193 $expression = str_replace(
"\n", $this->separator_chr, $expression);
194 foreach ($values as $key => $value) {
195 if ($value ===
null && strpos($expression, $key) !==
false) {
196 $this->error_parser = array(24, $key);
199 $expression = str_replace($this->special_chr.$key.$this->special_chr, strval($value), $expression);
203 if (strpos($expression, $this->special_chr) !==
false) {
204 $data = explode($this->special_chr, $expression);
205 $variable = $this->special_chr.$data[1];
206 if (isset($data[2])) {
207 $variable .= $this->special_chr;
209 $this->error_parser = array(23, array($variable, $expression));
214 $expressions = explode($this->separator_chr, $expression);
215 $expressions = array_slice($expressions, 0, $this->limit);
216 foreach ($expressions as $expr) {
219 $last_result = $em->evaluate($expr);
220 $this->error_parser = $em->last_error_code;
221 if ($this->error_parser !==
null) {
222 $this->error_expr = $expr;
228 if (empty($vars[
"price"])) {
229 $vars[
"price"] = $last_result;
231 if (!isset($vars[
"price"])) {
232 $this->error_parser = array(21, $expression);
235 if ($vars[
"price"] < 0) {
236 $this->error_parser = array(22, $expression);
239 return $vars[
"price"];
253 $res = $price_expression->fetch($product->fk_price_expression);
255 $this->error_parser = array(19,
null);
261 $res = $productFournisseur->find_min_price_product_fournisseur($product->id, 0, 0);
263 $this->error_parser = array(25,
null);
265 } elseif ($res == 0) {
266 $supplier_min_price = 0;
267 $supplier_min_price_with_discount = 0;
269 $supplier_min_price = $productFournisseur->fourn_unitprice;
270 $supplier_min_price_with_discount = $productFournisseur->fourn_unitprice_with_discount;
274 $extra_values = array_merge($extra_values, array(
275 "supplier_min_price" => $supplier_min_price,
276 "supplier_min_price_with_discount" => $supplier_min_price_with_discount,
280 $result = $this->
parseExpression($product, $price_expression->expression, $extra_values);
281 if (empty($this->error_parser)) {
282 if ($result < $product->price_min) {
283 $result = $product->price_min;
300 $res = $price_expression->fetch($product_supplier->fk_supplier_price_expression);
302 $this->error_parser = array(19,
null);
307 $product_supplier->fetch($product_supplier->id,
'',
'',
'', 1);
310 $extra_values = array_merge($extra_values, array(
311 "supplier_quantity" => $product_supplier->fourn_qty,
312 "supplier_tva_tx" => $product_supplier->fourn_tva_tx,
316 return $this->
parseExpression($product_supplier, $price_expression->expression, $extra_values);
327 public function testExpression($product_id, $expression, $extra_values = array())
331 $product->fetch($product_id,
'',
'', 1);
334 $extra_values = array_merge($extra_values, array(
335 "supplier_min_price" => 1,
336 "supplier_min_price_with_discount" => 2,
340 $extra_values = array_merge($extra_values, array(
341 "supplier_quantity" => 3,
342 "supplier_tva_tx" => 4,
Class for accesing price expression table.
Class for accesing price global variables table.
Class for price global variable updaters table.
Class to parse product price expressions.
parseExpression($product, $expression, $values)
Calculates price based on expression.
parseProduct($product, $extra_values=array())
Calculates product price based on product id and associated expression.
parseProductSupplier($product_supplier, $extra_values=array())
Calculates supplier product price based on product supplier price and associated expression.
__construct($db)
Constructor.
testExpression($product_id, $expression, $extra_values=array())
Tests string expression for validity.
translatedError()
Returns translated error.
Class to manage predefined suppliers products.
Class to manage products or services.
dol_now($mode='auto')
Return date for now.
$conf db
API class for accounts.