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);
127 global $user, $hookmanager, $extrafields;
129 $action =
'PARSEEXPRESSION';
130 if ($reshook = $hookmanager->executeHooks(
'doDynamiPrice', array(
131 'expression' => &$expression,
132 'product' => &$product,
134 ), $this, $action)) {
135 return $hookmanager->resArray[
'return'];
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,
158 if (! is_object($extrafields)) {
160 $extrafields->fetch_name_optionals_label($product->table_element);
163 $product->fetch_optionals();
164 if (is_array($extrafields->attributes[$product->table_element][
'label'])) {
165 foreach ($extrafields->attributes[$product->table_element][
'label'] as $key => $label) {
166 $values[
"extrafield_".$key] = $product->array_options[
'options_'.$key];
172 foreach ($price_updaters->listPendingUpdaters() as $entry) {
174 $entry->update_next_update(
dol_now() + ($entry->update_interval * 60), $user);
176 $res = $entry->process();
178 $entry->update_status($res < 1 ? $entry->error :
'', $user);
183 foreach ($price_globals->listGlobalVariables() as $entry) {
184 $values[
"global_".$entry->code] = $entry->value;
188 unset($values[
"supplier_id"]);
192 $em->suppress_errors =
true;
193 $this->error_expr =
null;
197 $expression = str_replace(
"\n", $this->separator_chr, $expression);
198 foreach ($values as $key => $value) {
199 if ($value ===
null && strpos($expression, $key) !==
false) {
200 $this->error_parser = array(24, $key);
203 $expression = str_replace($this->special_chr.$key.$this->special_chr, strval($value), $expression);
207 if (strpos($expression, $this->special_chr) !==
false) {
208 $data = explode($this->special_chr, $expression);
209 $variable = $this->special_chr.$data[1];
210 if (isset($data[2])) {
211 $variable .= $this->special_chr;
213 $this->error_parser = array(23, array($variable, $expression));
218 $expressions = explode($this->separator_chr, $expression);
219 $expressions = array_slice($expressions, 0, $this->limit);
220 foreach ($expressions as $expr) {
223 $last_result = $em->evaluate($expr);
224 $this->error_parser = $em->last_error_code;
225 if ($this->error_parser !==
null) {
226 $this->error_expr = $expr;
232 if (empty($vars[
"price"])) {
233 $vars[
"price"] = $last_result;
235 if (!isset($vars[
"price"])) {
236 $this->error_parser = array(21, $expression);
239 if ($vars[
"price"] < 0) {
240 $this->error_parser = array(22, $expression);
243 return $vars[
"price"];
257 $res = $price_expression->fetch($product->fk_price_expression);
259 $this->error_parser = array(19,
null);
265 $res = $productFournisseur->find_min_price_product_fournisseur($product->id, 0, 0);
267 $this->error_parser = array(25,
null);
269 } elseif ($res == 0) {
270 $supplier_min_price = 0;
271 $supplier_min_price_with_discount = 0;
273 $supplier_min_price = $productFournisseur->fourn_unitprice;
274 $supplier_min_price_with_discount = $productFournisseur->fourn_unitprice_with_discount;
278 $extra_values = array_merge($extra_values, array(
279 "supplier_min_price" => $supplier_min_price,
280 "supplier_min_price_with_discount" => $supplier_min_price_with_discount,
284 $result = $this->
parseExpression($product, $price_expression->expression, $extra_values);
285 if (empty($this->error_parser)) {
286 if ($result < $product->price_min) {
287 $result = $product->price_min;
304 $res = $price_expression->fetch($product_supplier->fk_supplier_price_expression);
306 $this->error_parser = array(19,
null);
311 $product_supplier->fetch($product_supplier->id,
'',
'',
'', 1);
314 $extra_values = array_merge($extra_values, array(
315 "supplier_quantity" => $product_supplier->fourn_qty,
316 "supplier_tva_tx" => $product_supplier->fourn_tva_tx,
320 return $this->
parseExpression($product_supplier, $price_expression->expression, $extra_values);
331 public function testExpression($product_id, $expression, $extra_values = array())
334 $product =
new Product($this->db);
335 $product->fetch($product_id,
'',
'', 1);
338 $extra_values = array_merge($extra_values, array(
339 "supplier_min_price" => 1,
340 "supplier_min_price_with_discount" => 2,
344 $extra_values = array_merge($extra_values, array(
345 "supplier_quantity" => 3,
346 "supplier_tva_tx" => 4,