Esempio n. 1
0
 /**
  *	Calculates price based on expression
  *
  *	@param	Product	$product    	The Product object to get information
  *	@param	String 	$expression     The expression to parse
  *	@param	array  	$values     	Strings to replaces
  *  @return int 					> 0 if OK, < 1 if KO
  */
 public function parseExpression($product, $expression, $values)
 {
     global $user;
     //Accessible product values by expressions
     $values = array_merge($values, array("tva_tx" => $product->tva_tx, "localtax1_tx" => $product->localtax1_tx, "localtax2_tx" => $product->localtax2_tx, "weight" => $product->weight, "length" => $product->length, "surface" => $product->surface, "price_min" => $product->price_min));
     //Retrieve all extrafield for product and add it to values
     $extrafields = new ExtraFields($this->db);
     $extralabels = $extrafields->fetch_name_optionals_label('product', true);
     $product->fetch_optionals($product->id, $extralabels);
     foreach ($extrafields->attribute_label as $key => $label) {
         $values["extrafield_" . $key] = $product->array_options['options_' . $key];
     }
     //Process any pending updaters
     $price_updaters = new PriceGlobalVariableUpdater($this->db);
     foreach ($price_updaters->listPendingUpdaters() as $entry) {
         //Schedule the next update by adding current timestamp (secs) + interval (mins)
         $entry->update_next_update(dol_now() + $entry->update_interval * 60, $user);
         //Do processing
         $res = $entry->process();
         //Store any error or clear status if OK
         $entry->update_status($res < 1 ? $entry->error : '', $user);
     }
     //Get all global values
     $price_globals = new PriceGlobalVariable($this->db);
     foreach ($price_globals->listGlobalVariables() as $entry) {
         $values["global_" . $entry->code] = $entry->value;
     }
     //Check if empty
     $expression = trim($expression);
     if (empty($expression)) {
         $this->error = array(20, null);
         return -2;
     }
     //Prepare the lib, parameters and values
     $em = new EvalMath();
     $em->suppress_errors = true;
     //Don't print errors on page
     $this->error_expr = null;
     $last_result = null;
     //Iterate over each expression splitted by $separator_chr
     $expression = str_replace("\n", $this->separator_chr, $expression);
     foreach ($values as $key => $value) {
         $expression = str_replace($this->special_chr . $key . $this->special_chr, "{$value}", $expression);
     }
     $expressions = explode($this->separator_chr, $expression);
     $expressions = array_slice($expressions, 0, $this->limit);
     foreach ($expressions as $expr) {
         $expr = trim($expr);
         if (!empty($expr)) {
             $last_result = $em->evaluate($expr);
             $this->error = $em->last_error_code;
             if ($this->error !== null) {
                 //$em->last_error is null if no error happened, so just check if error is not null
                 $this->error_expr = $expr;
                 return -3;
             }
         }
     }
     $vars = $em->vars();
     if (empty($vars["price"])) {
         $vars["price"] = $last_result;
     }
     if (!isset($vars["price"])) {
         $this->error = array(21, $expression);
         return -4;
     }
     if ($vars["price"] < 0) {
         $this->error = array(22, $expression);
         return -5;
     }
     return $vars["price"];
 }