price_parser.class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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 <https://www.gnu.org/licenses/>.
  16. */
  17. /**
  18. * \file htdocs/product/dynamic_price/class/price_parser.class.php
  19. * \ingroup product
  20. * \brief File of class to calculate prices using expression
  21. */
  22. require_once DOL_DOCUMENT_ROOT.'/core/class/evalmath.class.php';
  23. require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.product.class.php';
  24. require_once DOL_DOCUMENT_ROOT.'/product/dynamic_price/class/price_expression.class.php';
  25. require_once DOL_DOCUMENT_ROOT.'/product/dynamic_price/class/price_global_variable.class.php';
  26. require_once DOL_DOCUMENT_ROOT.'/product/dynamic_price/class/price_global_variable_updater.class.php';
  27. require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
  28. /**
  29. * Class to parse product price expressions
  30. */
  31. class PriceParser
  32. {
  33. protected $db;
  34. // Limit of expressions per price
  35. public $limit = 100;
  36. // The error that occurred when parsing price
  37. public $error_parser;
  38. // The expression that caused the error
  39. public $error_expr;
  40. //The special char
  41. public $special_chr = "#";
  42. //The separator char
  43. public $separator_chr = ";";
  44. /**
  45. * Constructor
  46. *
  47. * @param DoliDB $db Database handler
  48. */
  49. public function __construct($db)
  50. {
  51. $this->db = $db;
  52. }
  53. /**
  54. * Returns translated error
  55. *
  56. * @return string Translated error
  57. */
  58. public function translatedError()
  59. {
  60. global $langs;
  61. $langs->load("errors");
  62. /*
  63. -No arg
  64. 9, an unexpected error occured
  65. 14, division by zero
  66. 19, expression not found
  67. 20, empty expression
  68. -1 Arg
  69. 1, cannot assign to constant '%s'
  70. 2, cannot redefine built-in function '%s'
  71. 3, undefined variable '%s' in function definition
  72. 4, illegal character '%s'
  73. 5, unexpected '%s'
  74. 8, unexpected operator '%s'
  75. 10, operator '%s' lacks operand
  76. 11, expecting '%s'
  77. 17, undefined variable '%s'
  78. 21, empty result '%s'
  79. 22, negative result '%s'
  80. 24, variable '%s' exists but has no value
  81. -2 Args
  82. 6, wrong number of arguments (%s given, %s expected)
  83. 23, unknown or non set variable '%s' after %s
  84. -internal errors
  85. 7, internal error
  86. 12, internal error
  87. 13, internal error
  88. 15, internal error
  89. 16, internal error
  90. 18, internal error
  91. */
  92. if (empty($this->error_parser)) {
  93. return $langs->trans("ErrorPriceExpressionUnknown", 0); //this is not supposed to happen
  94. }
  95. list($code, $info) = $this->error_parser;
  96. if (in_array($code, array(9, 14, 19, 20))) { //Errors which have 0 arg
  97. return $langs->trans("ErrorPriceExpression".$code);
  98. } elseif (in_array($code, array(1, 2, 3, 4, 5, 8, 10, 11, 17, 21, 22))) { //Errors which have 1 arg
  99. return $langs->trans("ErrorPriceExpression".$code, $info);
  100. } elseif (in_array($code, array(6, 23))) { //Errors which have 2 args
  101. return $langs->trans("ErrorPriceExpression".$code, $info[0], $info[1]);
  102. } elseif (in_array($code, array(7, 12, 13, 15, 16, 18))) { //Internal errors
  103. return $langs->trans("ErrorPriceExpressionInternal", $code);
  104. } else //Unknown errors
  105. {
  106. return $langs->trans("ErrorPriceExpressionUnknown", $code);
  107. }
  108. }
  109. /**
  110. * Calculates price based on expression
  111. *
  112. * @param Product $product The Product object to get information
  113. * @param String $expression The expression to parse
  114. * @param array $values Strings to replaces
  115. * @return int > 0 if OK, < 1 if KO
  116. */
  117. public function parseExpression($product, $expression, $values)
  118. {
  119. global $user, $hookmanager, $extrafields;
  120. $action = 'PARSEEXPRESSION';
  121. if ($reshook = $hookmanager->executeHooks('doDynamiPrice', array(
  122. 'expression' => &$expression,
  123. 'product' => &$product,
  124. 'values' => &$values
  125. ), $this, $action)) {
  126. return $hookmanager->resArray['return'];
  127. }
  128. //Check if empty
  129. $expression = trim($expression);
  130. if (empty($expression)) {
  131. $this->error_parser = array(20, null);
  132. return -2;
  133. }
  134. //Accessible product values by expressions
  135. $values = array_merge($values, array(
  136. "tva_tx" => $product->tva_tx,
  137. "localtax1_tx" => $product->localtax1_tx,
  138. "localtax2_tx" => $product->localtax2_tx,
  139. "weight" => $product->weight,
  140. "length" => $product->length,
  141. "surface" => $product->surface,
  142. "price_min" => $product->price_min,
  143. "cost_price" => $product->cost_price,
  144. "pmp" => $product->pmp,
  145. ));
  146. // Retrieve all extrafields if not already not know (should not happen)
  147. if (! is_object($extrafields)) {
  148. $extrafields = new ExtraFields($this->db);
  149. $extrafields->fetch_name_optionals_label();
  150. }
  151. $product->fetch_optionals();
  152. if (is_array($extrafields->attributes[$product->table_element]['label'])) {
  153. foreach ($extrafields->attributes[$product->table_element]['label'] as $key => $label) {
  154. $values["extrafield_".$key] = $product->array_options['options_'.$key];
  155. }
  156. }
  157. //Process any pending updaters
  158. $price_updaters = new PriceGlobalVariableUpdater($this->db);
  159. foreach ($price_updaters->listPendingUpdaters() as $entry) {
  160. //Schedule the next update by adding current timestamp (secs) + interval (mins)
  161. $entry->update_next_update(dol_now() + ($entry->update_interval * 60), $user);
  162. //Do processing
  163. $res = $entry->process();
  164. //Store any error or clear status if OK
  165. $entry->update_status($res < 1 ? $entry->error : '', $user);
  166. }
  167. //Get all global values
  168. $price_globals = new PriceGlobalVariable($this->db);
  169. foreach ($price_globals->listGlobalVariables() as $entry) {
  170. $values["global_".$entry->code] = $entry->value;
  171. }
  172. //Remove internal variables
  173. unset($values["supplier_id"]);
  174. //Prepare the lib, parameters and values
  175. $em = new EvalMath();
  176. $em->suppress_errors = true; //Don't print errors on page
  177. $this->error_expr = null;
  178. $last_result = null;
  179. //Fill each variable in expression from values
  180. $expression = str_replace("\n", $this->separator_chr, $expression);
  181. foreach ($values as $key => $value) {
  182. if ($value === null && strpos($expression, $key) !== false) {
  183. $this->error_parser = array(24, $key);
  184. return -7;
  185. }
  186. $expression = str_replace($this->special_chr.$key.$this->special_chr, strval($value), $expression);
  187. }
  188. //Check if there is unfilled variable
  189. if (strpos($expression, $this->special_chr) !== false) {
  190. $data = explode($this->special_chr, $expression);
  191. $variable = $this->special_chr.$data[1];
  192. if (isset($data[2])) {
  193. $variable .= $this->special_chr;
  194. }
  195. $this->error_parser = array(23, array($variable, $expression));
  196. return -6;
  197. }
  198. //Iterate over each expression splitted by $separator_chr
  199. $expressions = explode($this->separator_chr, $expression);
  200. $expressions = array_slice($expressions, 0, $this->limit);
  201. foreach ($expressions as $expr) {
  202. $expr = trim($expr);
  203. if (!empty($expr)) {
  204. $last_result = $em->evaluate($expr);
  205. $this->error_parser = $em->last_error_code;
  206. if ($this->error_parser !== null) { //$em->last_error_code is null if no error happened, so just check if error_parser is not null
  207. $this->error_expr = $expr;
  208. return -3;
  209. }
  210. }
  211. }
  212. $vars = $em->vars();
  213. if (empty($vars["price"])) {
  214. $vars["price"] = $last_result;
  215. }
  216. if (!isset($vars["price"])) {
  217. $this->error_parser = array(21, $expression);
  218. return -4;
  219. }
  220. if ($vars["price"] < 0) {
  221. $this->error_parser = array(22, $expression);
  222. return -5;
  223. }
  224. return $vars["price"];
  225. }
  226. /**
  227. * Calculates product price based on product id and associated expression
  228. *
  229. * @param Product $product The Product object to get information
  230. * @param array $extra_values Any aditional values for expression
  231. * @return int > 0 if OK, < 1 if KO
  232. */
  233. public function parseProduct($product, $extra_values = array())
  234. {
  235. //Get the expression from db
  236. $price_expression = new PriceExpression($this->db);
  237. $res = $price_expression->fetch($product->fk_price_expression);
  238. if ($res < 1) {
  239. $this->error_parser = array(19, null);
  240. return -1;
  241. }
  242. //Get the supplier min price
  243. $productFournisseur = new ProductFournisseur($this->db);
  244. $res = $productFournisseur->find_min_price_product_fournisseur($product->id, 0, 0);
  245. if ($res < 0) {
  246. $this->error_parser = array(25, null);
  247. return -1;
  248. } elseif ($res == 0) {
  249. $supplier_min_price = 0;
  250. $supplier_min_price_with_discount = 0;
  251. } else {
  252. $supplier_min_price = $productFournisseur->fourn_unitprice;
  253. $supplier_min_price_with_discount = $productFournisseur->fourn_unitprice_with_discount;
  254. }
  255. //Accessible values by expressions
  256. $extra_values = array_merge($extra_values, array(
  257. "supplier_min_price" => $supplier_min_price,
  258. "supplier_min_price_with_discount" => $supplier_min_price_with_discount,
  259. ));
  260. //Parse the expression and return the price, if not error occurred check if price is higher than min
  261. $result = $this->parseExpression($product, $price_expression->expression, $extra_values);
  262. if (empty($this->error_parser)) {
  263. if ($result < $product->price_min) {
  264. $result = $product->price_min;
  265. }
  266. }
  267. return $result;
  268. }
  269. /**
  270. * Calculates supplier product price based on product supplier price and associated expression
  271. *
  272. * @param ProductFournisseur $product_supplier The Product supplier object to get information
  273. * @param array $extra_values Any aditional values for expression
  274. * @return int > 0 if OK, < 1 if KO
  275. */
  276. public function parseProductSupplier($product_supplier, $extra_values = array())
  277. {
  278. //Get the expression from db
  279. $price_expression = new PriceExpression($this->db);
  280. $res = $price_expression->fetch($product_supplier->fk_supplier_price_expression);
  281. if ($res < 1) {
  282. $this->error_parser = array(19, null);
  283. return -1;
  284. }
  285. //Get the product data (use ignore_expression to avoid possible recursion)
  286. $product_supplier->fetch($product_supplier->id, '', '', '', 1);
  287. //Accessible values by expressions
  288. $extra_values = array_merge($extra_values, array(
  289. "supplier_quantity" => $product_supplier->fourn_qty,
  290. "supplier_tva_tx" => $product_supplier->fourn_tva_tx,
  291. ));
  292. //Parse the expression and return the price
  293. return $this->parseExpression($product_supplier, $price_expression->expression, $extra_values);
  294. }
  295. /**
  296. * Tests string expression for validity
  297. *
  298. * @param int $product_id The Product id to get information
  299. * @param string $expression The expression to parse
  300. * @param array $extra_values Any aditional values for expression
  301. * @return int > 0 if OK, < 1 if KO
  302. */
  303. public function testExpression($product_id, $expression, $extra_values = array())
  304. {
  305. //Get the product data
  306. $product = new Product($this->db);
  307. $product->fetch($product_id, '', '', 1);
  308. //Values for product expressions
  309. $extra_values = array_merge($extra_values, array(
  310. "supplier_min_price" => 1,
  311. "supplier_min_price_with_discount" => 2,
  312. ));
  313. //Values for supplier product expressions
  314. $extra_values = array_merge($extra_values, array(
  315. "supplier_quantity" => 3,
  316. "supplier_tva_tx" => 4,
  317. ));
  318. return $this->parseExpression($product, $expression, $extra_values);
  319. }
  320. }