dolibarr  9.0.0
price_parser.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2015 Ion Agorria <ion@agorria.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
23 require_once DOL_DOCUMENT_ROOT.'/includes/evalmath/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';
29 
34 {
35  protected $db;
36  // Limit of expressions per price
37  public $limit = 100;
38  // The error that occurred when parsing price
39  public $error_parser;
40  // The expression that caused the error
41  public $error_expr;
42  //The special char
43  public $special_chr = "#";
44  //The separator char
45  public $separator_chr = ";";
46 
52  function __construct($db)
53  {
54  $this->db = $db;
55  }
56 
62  public function translatedError()
63  {
64  global $langs;
65  $langs->load("errors");
66  /*
67  -No arg
68  9, an unexpected error occured
69  14, division by zero
70  19, expression not found
71  20, empty expression
72 
73  -1 Arg
74  1, cannot assign to constant '%s'
75  2, cannot redefine built-in function '%s'
76  3, undefined variable '%s' in function definition
77  4, illegal character '%s'
78  5, unexpected '%s'
79  8, unexpected operator '%s'
80  10, operator '%s' lacks operand
81  11, expecting '%s'
82  17, undefined variable '%s'
83  21, empty result '%s'
84  22, negative result '%s'
85  24, variable '%s' exists but has no value
86 
87  -2 Args
88  6, wrong number of arguments (%s given, %s expected)
89  23, unknown or non set variable '%s' after %s
90 
91  -internal errors
92  7, internal error
93  12, internal error
94  13, internal error
95  15, internal error
96  16, internal error
97  18, internal error
98  */
99  if (empty($this->error_parser)) {
100  return $langs->trans("ErrorPriceExpressionUnknown", 0); //this is not supposed to happen
101  }
102  list($code, $info) = $this->error_parser;
103  if (in_array($code, array(9, 14, 19, 20))) //Errors which have 0 arg
104  {
105  return $langs->trans("ErrorPriceExpression".$code);
106  }
107  else if (in_array($code, array(1, 2, 3, 4, 5, 8, 10, 11, 17, 21, 22))) //Errors which have 1 arg
108  {
109  return $langs->trans("ErrorPriceExpression".$code, $info);
110  }
111  else if (in_array($code, array(6, 23))) //Errors which have 2 args
112  {
113  return $langs->trans("ErrorPriceExpression".$code, $info[0], $info[1]);
114  }
115  else if (in_array($code, array(7, 12, 13, 15, 16, 18))) //Internal errors
116  {
117  return $langs->trans("ErrorPriceExpressionInternal", $code);
118  }
119  else //Unknown errors
120  {
121  return $langs->trans("ErrorPriceExpressionUnknown", $code);
122  }
123  }
124 
133  public function parseExpression($product, $expression, $values)
134  {
135  global $user;
136 
137  //Check if empty
138  $expression = trim($expression);
139  if (empty($expression))
140  {
141  $this->error_parser = array(20, null);
142  return -2;
143  }
144 
145  //Accessible product values by expressions
146  $values = array_merge($values, array(
147  "tva_tx" => $product->tva_tx,
148  "localtax1_tx" => $product->localtax1_tx,
149  "localtax2_tx" => $product->localtax2_tx,
150  "weight" => $product->weight,
151  "length" => $product->length,
152  "surface" => $product->surface,
153  "price_min" => $product->price_min,
154  ));
155 
156  //Retrieve all extrafield for product and add it to values
157  $extrafields = new ExtraFields($this->db);
158  $extralabels = $extrafields->fetch_name_optionals_label('product', true);
159  $product->fetch_optionals();
160  foreach ($extrafields->attributes[$product->table_element]['label'] as $key=>$label)
161  {
162  $values["extrafield_".$key] = $product->array_options['options_'.$key];
163  }
164 
165  //Process any pending updaters
166  $price_updaters = new PriceGlobalVariableUpdater($this->db);
167  foreach ($price_updaters->listPendingUpdaters() as $entry) {
168  //Schedule the next update by adding current timestamp (secs) + interval (mins)
169  $entry->update_next_update(dol_now() + ($entry->update_interval * 60), $user);
170  //Do processing
171  $res = $entry->process();
172  //Store any error or clear status if OK
173  $entry->update_status($res < 1?$entry->error:'', $user);
174  }
175 
176  //Get all global values
177  $price_globals = new PriceGlobalVariable($this->db);
178  foreach ($price_globals->listGlobalVariables() as $entry)
179  {
180  $values["global_".$entry->code] = $entry->value;
181  }
182 
183  //Remove internal variables
184  unset($values["supplier_id"]);
185 
186  //Prepare the lib, parameters and values
187  $em = new EvalMath();
188  $em->suppress_errors = true; //Don't print errors on page
189  $this->error_expr = null;
190  $last_result = null;
191 
192  //Fill each variable in expression from values
193  $expression = str_replace("\n", $this->separator_chr, $expression);
194  foreach ($values as $key => $value)
195  {
196  if ($value === null && strpos($expression, $key) !== false) {
197  $this->error_parser = array(24, $key);
198  return -7;
199  }
200  $expression = str_replace($this->special_chr.$key.$this->special_chr, strval($value), $expression);
201  }
202 
203  //Check if there is unfilled variable
204  if (strpos($expression, $this->special_chr) !== false)
205  {
206  $data = explode($this->special_chr, $expression);
207  $variable = $this->special_chr.$data[1];
208  if (isset($data[2])) $variable.= $this->special_chr;
209  $this->error_parser = array(23, array($variable, $expression));
210  return -6;
211  }
212 
213  //Iterate over each expression splitted by $separator_chr
214  $expressions = explode($this->separator_chr, $expression);
215  $expressions = array_slice($expressions, 0, $this->limit);
216  foreach ($expressions as $expr) {
217  $expr = trim($expr);
218  if (!empty($expr))
219  {
220  $last_result = $em->evaluate($expr);
221  $this->error_parser = $em->last_error_code;
222  if ($this->error_parser !== null) { //$em->last_error_code is null if no error happened, so just check if error_parser is not null
223  $this->error_expr = $expr;
224  return -3;
225  }
226  }
227  }
228  $vars = $em->vars();
229  if (empty($vars["price"])) {
230  $vars["price"] = $last_result;
231  }
232  if (!isset($vars["price"]))
233  {
234  $this->error_parser = array(21, $expression);
235  return -4;
236  }
237  if ($vars["price"] < 0)
238  {
239  $this->error_parser = array(22, $expression);
240  return -5;
241  }
242  return $vars["price"];
243  }
244 
252  public function parseProduct($product, $extra_values = array())
253  {
254  //Get the expression from db
255  $price_expression = new PriceExpression($this->db);
256  $res = $price_expression->fetch($product->fk_price_expression);
257  if ($res < 1) {
258  $this->error_parser = array(19, null);
259  return -1;
260  }
261 
262  //Get the supplier min
263  $productFournisseur = new ProductFournisseur($this->db);
264  $supplier_min_price = $productFournisseur->find_min_price_product_fournisseur($product->id, 0, 0);
265 
266  //Accessible values by expressions
267  $extra_values = array_merge($extra_values, array(
268  "supplier_min_price" => $supplier_min_price,
269  ));
270 
271  //Parse the expression and return the price, if not error occurred check if price is higher than min
272  $result = $this->parseExpression($product, $price_expression->expression, $extra_values);
273  if (empty($this->error_parser)) {
274  if ($result < $product->price_min) {
275  $result = $product->price_min;
276  }
277  }
278  return $result;
279  }
280 
288  public function parseProductSupplier($product_supplier, $extra_values = array())
289  {
290  //Get the expression from db
291  $price_expression = new PriceExpression($this->db);
292  $res = $price_expression->fetch($product_supplier->fk_supplier_price_expression);
293  if ($res < 1)
294  {
295  $this->error_parser = array(19, null);
296  return -1;
297  }
298 
299  //Get the product data (use ignore_expression to avoid possible recursion)
300  $product_supplier->fetch($product_supplier->id, '', '', 1);
301 
302  //Accessible values by expressions
303  $extra_values = array_merge($extra_values, array(
304  "supplier_quantity" => $product_supplier->fourn_qty,
305  "supplier_tva_tx" => $product_supplier->fourn_tva_tx,
306  ));
307 
308  //Parse the expression and return the price
309  return $this->parseExpression($product_supplier, $price_expression->expression, $extra_values);
310  }
311 
320  public function testExpression($product_id, $expression, $extra_values = array())
321  {
322  //Get the product data
323  $product = new Product($this->db);
324  $product->fetch($product_id, '', '', 1);
325 
326  //Values for product expressions
327  $extra_values = array_merge($extra_values, array(
328  "supplier_min_price" => 1,
329  ));
330 
331  //Values for supplier product expressions
332  $extra_values = array_merge($extra_values, array(
333  "supplier_quantity" => 2,
334  "supplier_tva_tx" => 3,
335  ));
336  return $this->parseExpression($product, $expression, $extra_values);
337  }
338 }
Class to parse product price expressions.
Class to manage products or services.
parseProductSupplier($product_supplier, $extra_values=array())
Calculates supplier product price based on product supplier price and associated expression.
testExpression($product_id, $expression, $extra_values=array())
Tests string expression for validity.
translatedError()
Returns translated error.
parseProduct($product, $extra_values=array())
Calculates product price based on product id and associated expression.
parseExpression($product, $expression, $values)
Calculates price based on expression.
Class to manage standard extra fields.
Class for accesing price expression table.
dol_now($mode='gmt')
Return date for now.
Class for accesing price global variables table.
__construct($db)
Constructor.
Class for price global variable updaters table.
Class to manage predefined suppliers products.