/**
  *  List all updaters which need to be processed
  *
  *  @return	array				Array of price global variable updaters
  */
 function listPendingUpdaters()
 {
     $sql = "SELECT rowid, type, description, parameters, fk_variable, update_interval, next_update, last_status";
     $sql .= " FROM " . MAIN_DB_PREFIX . $this->table_element;
     $sql .= " WHERE next_update < " . dol_now();
     dol_syslog(get_class($this) . "::processUpdaters");
     $resql = $this->db->query($sql);
     if ($resql) {
         $retarray = array();
         while ($record = $this->db->fetch_array($resql)) {
             $updater_obj = new PriceGlobalVariableUpdater($this->db);
             $updater_obj->id = $record["rowid"];
             $updater_obj->type = $record["type"];
             $updater_obj->description = $record["description"];
             $updater_obj->parameters = $record["parameters"];
             $updater_obj->fk_variable = $record["fk_variable"];
             $updater_obj->update_interval = $record["update_interval"];
             $updater_obj->next_update = $record["next_update"];
             $updater_obj->last_status = $record["last_status"];
             $updater_obj->checkParameters();
             $retarray[] = $updater_obj;
         }
         $this->db->free($resql);
         return $retarray;
     } else {
         $this->error = $this->db->error();
         return -1;
     }
 }
Esempio n. 2
0
$save = GETPOST('save', 'alpha');
$cancel = GETPOST('cancel', 'alpha');
$selection = GETPOST('selection', 'int');
// Security check
if (!$user->admin) {
    accessforbidden();
}
//Objects
$price_globals = new PriceGlobalVariable($db);
if ($action == 'edit_variable') {
    $res = $price_globals->fetch($selection);
    if ($res < 1) {
        setEventMessage($price_globals->error, 'errors');
    }
}
$price_updaters = new PriceGlobalVariableUpdater($db);
if ($action == 'edit_updater') {
    $res = $price_updaters->fetch($selection);
    if ($res < 1) {
        setEventMessage($price_updaters->error, 'errors');
    }
}
/*
 * Actions
 */
if (!empty($action) && empty($cancel)) {
    //Global variable actions
    if ($action == 'create_variable' || $action == 'edit_variable') {
        $price_globals->code = isset($_POST['code']) ? GETPOST('code', 'alpha') : $price_globals->code;
        $price_globals->description = isset($_POST['description']) ? GETPOST('description', 'alpha') : $price_globals->description;
        $price_globals->value = isset($_POST['value']) ? GETPOST('value', 'int') : $price_globals->value;
Esempio n. 3
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"];
 }