80 $langs->load(
"errors");
114 if (empty($this->error_parser)) {
115 return $langs->trans(
"ErrorPriceExpressionUnknown", 0);
117 list($code, $info) = $this->error_parser;
118 if (in_array($code, array(9, 14, 19, 20))) {
119 return $langs->trans(
"ErrorPriceExpression".$code);
120 } elseif (in_array($code, array(1, 2, 3, 4, 5, 8, 10, 11, 17, 21, 22))) {
121 return $langs->trans(
"ErrorPriceExpression".$code, (
string) $info);
122 } elseif (in_array($code, array(6, 23))) {
123 return $langs->trans(
"ErrorPriceExpression".$code, $info[0], $info[1]);
124 } elseif (in_array($code, array(7, 12, 13, 15, 16, 18))) {
125 return $langs->trans(
"ErrorPriceExpressionInternal", (
string) $code);
127 return $langs->trans(
"ErrorPriceExpressionUnknown", (
string) $code);
141 global $user, $hookmanager, $extrafields;
143 $action =
'PARSEEXPRESSION';
144 $reshook = $hookmanager->executeHooks(
'doDynamicPrice', array(
'expression' => &$expression,
'product' => &$product,
'values' => &$values), $this, $action);
146 return $hookmanager->resArray[
'return'];
149 $expression = trim($expression);
150 if (empty($expression)) {
151 $this->error_parser = array(20,
null);
156 $values = array_merge($values, array(
157 "tva_tx" => $product->tva_tx,
158 "localtax1_tx" => $product->localtax1_tx,
159 "localtax2_tx" => $product->localtax2_tx,
160 "weight" => $product->weight,
161 "length" => $product->length,
162 "surface" => $product->surface,
163 "price_min" => $product->price_min,
164 "cost_price" => $product->cost_price,
165 "pmp" => $product->pmp,
169 if (! is_object($extrafields)) {
171 $extrafields->fetch_name_optionals_label($product->table_element);
174 $product->fetch_optionals();
175 if (is_array($extrafields->attributes[$product->table_element][
'label'])) {
176 foreach ($extrafields->attributes[$product->table_element][
'label'] as $key => $label) {
177 $values[
"extrafield_".$key] = $product->array_options[
'options_'.$key];
183 foreach ($price_updaters->listPendingUpdaters() as $entry) {
185 $entry->update_next_update(
dol_now() + ($entry->update_interval * 60), $user);
187 $res = $entry->process();
189 $entry->update_status($res < 1 ? $entry->error :
'', $user);
194 foreach ($price_globals->listGlobalVariables() as $entry) {
195 $values[
"global_".$entry->code] = $entry->value;
199 unset($values[
"supplier_id"]);
203 $em->suppress_errors =
true;
204 $this->error_expr =
null;
208 $expression = str_replace(
"\n", $this->separator_chr, $expression);
209 foreach ($values as $key => $value) {
210 if ($value ===
null && strpos($expression, $key) !==
false) {
211 $this->error_parser = array(24, $key);
214 $expression = str_replace($this->special_chr.$key.$this->special_chr, strval($value), $expression);
218 if (strpos($expression, $this->special_chr) !==
false) {
219 $data = explode($this->special_chr, $expression);
220 $variable = $this->special_chr.$data[1];
221 if (isset($data[2])) {
222 $variable .= $this->special_chr;
224 $this->error_parser = array(23, array($variable, $expression));
229 $expressions = explode($this->separator_chr, $expression);
230 $expressions = array_slice($expressions, 0, $this->limit);
231 foreach ($expressions as $expr) {
234 $last_result = $em->evaluate($expr);
235 $this->error_parser = $em->last_error_code;
236 if ($this->error_parser !==
null) {
237 $this->error_expr = $expr;
243 if (empty($vars[
"price"])) {
244 $vars[
"price"] = $last_result;
246 if (!isset($vars[
"price"])) {
247 $this->error_parser = array(21, $expression);
250 if ($vars[
"price"] < 0) {
251 $this->error_parser = array(22, $expression);
254 return $vars[
"price"];
268 $res = $price_expression->fetch($product->fk_price_expression);
270 $this->error_parser = array(19,
null);
276 $res = $productFournisseur->find_min_price_product_fournisseur($product->id, 0, 0);
278 $this->error_parser = array(25,
null);
280 } elseif ($res == 0) {
281 $supplier_min_price = 0;
282 $supplier_min_price_with_discount = 0;
284 $supplier_min_price = $productFournisseur->fourn_unitprice;
285 $supplier_min_price_with_discount = $productFournisseur->fourn_unitprice_with_discount;
289 $extra_values = array_merge($extra_values, array(
290 "supplier_min_price" => $supplier_min_price,
291 "supplier_min_price_with_discount" => $supplier_min_price_with_discount,
295 $result = $this->
parseExpression($product, $price_expression->expression, $extra_values);
296 if (empty($this->error_parser)) {
297 if ($result < $product->price_min) {
298 $result = $product->price_min;
315 $res = $price_expression->fetch($product_supplier->fk_supplier_price_expression);
317 $this->error_parser = array(19,
null);
322 $product_supplier->fetch($product_supplier->id,
'',
'',
'', 1);
325 $extra_values = array_merge($extra_values, array(
326 "supplier_quantity" => $product_supplier->fourn_qty,
327 "supplier_tva_tx" => $product_supplier->fourn_tva_tx,
331 return $this->
parseExpression($product_supplier, $price_expression->expression, $extra_values);
342 public function testExpression($product_id, $expression, $extra_values = array())
345 $product =
new Product($this->db);
346 $product->fetch($product_id,
'',
'',
'1');
349 $extra_values = array_merge($extra_values, array(
350 "supplier_min_price" => 1,
351 "supplier_min_price_with_discount" => 2,
355 $extra_values = array_merge($extra_values, array(
356 "supplier_quantity" => 3,
357 "supplier_tva_tx" => 4,