require_once DOL_DOCUMENT_ROOT . '/core/lib/product.lib.php'; require_once DOL_DOCUMENT_ROOT . '/fourn/class/fournisseur.product.class.php'; require_once DOL_DOCUMENT_ROOT . '/product/dynamic_price/class/price_global_variable.class.php'; require_once DOL_DOCUMENT_ROOT . '/product/dynamic_price/class/price_global_variable_updater.class.php'; $langs->load("products"); $id = GETPOST('id', 'int'); $action = GETPOST('action', 'alpha'); $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
/** * List all price global variables * * @return array Array of price global variables */ function listGlobalVariables() { $sql = "SELECT rowid, code, description, value"; $sql .= " FROM " . MAIN_DB_PREFIX . $this->table_element; $sql .= " ORDER BY code"; dol_syslog(get_class($this) . "::listGlobalVariables"); $resql = $this->db->query($sql); if ($resql) { $retarray = array(); while ($record = $this->db->fetch_array($resql)) { $variable_obj = new PriceGlobalVariable($this->db); $variable_obj->id = $record["rowid"]; $variable_obj->code = $record["code"]; $variable_obj->description = $record["description"]; $variable_obj->value = $record["value"]; $variable_obj->checkParameters(); $retarray[] = $variable_obj; } $this->db->free($resql); return $retarray; } else { $this->error = $this->db->error(); return -1; } }
/** * Handles the processing of this updater * * @return int <0 if KO, 0 if OK but no global variable found, >0 if OK */ function process() { global $langs, $user; $langs->load("errors"); $this->error = null; $this->checkParameters(); //Try to load the target global variable and abort if fails if ($this->fk_variable < 1) { $this->error = $langs->trans("ErrorGlobalVariableUpdater5"); return 0; } $price_globals = new PriceGlobalVariable($this->db); $res = $price_globals->fetch($this->fk_variable); if ($res < 1) { $this->error = $langs->trans("ErrorGlobalVariableUpdater5"); return 0; } //Process depending of type if ($this->type == 0 || $this->type == 1) { //Get and check if required parameters are present $parameters = json_decode($this->parameters, true); if (!isset($parameters)) { $this->error = $langs->trans("ErrorGlobalVariableUpdater1", $this->parameters); return -1; } $url = $parameters['URL']; if (!isset($url)) { $this->error = $langs->trans("ErrorGlobalVariableUpdater2", 'URL'); return -1; } $value = $parameters['VALUE']; if (!isset($value)) { $this->error = $langs->trans("ErrorGlobalVariableUpdater2", 'VALUE'); return -1; } $result = ""; if ($this->type == 0) { //CURL client $handle = curl_init(); curl_setopt_array($handle, array(CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 5, CURLOPT_POST => false, CURLOPT_HEADER => false)); $result = curl_exec($handle); $code = curl_getinfo($handle, CURLINFO_HTTP_CODE); if (!isset($result)) { $this->error = $langs->trans("ErrorGlobalVariableUpdater0", "empty response"); return -1; } if ($code !== 200) { $this->error = $langs->trans("ErrorGlobalVariableUpdater0", $code); return -1; } //Decode returned response $result = json_decode($result, true); } elseif ($this->type == 1) { $ns = $parameters['NS']; if (!isset($ns)) { $this->error = $langs->trans("ErrorGlobalVariableUpdater2", 'NS'); return -1; } $method = $parameters['METHOD']; if (!isset($method)) { $this->error = $langs->trans("ErrorGlobalVariableUpdater2", 'METHOD'); return -1; } $data = $parameters['DATA']; if (!isset($data)) { $this->error = $langs->trans("ErrorGlobalVariableUpdater2", 'DATA'); return -1; } //SOAP client require_once NUSOAP_PATH . '/nusoap.php'; $soap_client = new nusoap_client($url); $soap_client->soap_defencoding = 'UTF-8'; $soap_client->decodeUTF8(false); $result = $soap_client->call($method, $data, $ns, ''); //Check if result is a error if ($result === false) { $this->error = $langs->trans("ErrorGlobalVariableUpdater4", $soap_client->error_str); return -1; } } //Explode value and walk for each key in value array to get the relevant key $value = explode(',', $value); foreach ($value as $key) { $result = $result[$key]; } if (!isset($result)) { $this->error = $langs->trans("ErrorGlobalVariableUpdater3"); return -1; } //Save data to global and update it $price_globals->value = $result; $price_globals->update($user); } return 1; }
//"Back" translation is on this file $id = GETPOST('id', 'int'); $eid = GETPOST('eid', 'int'); $action = GETPOST('action', 'alpha'); $title = GETPOST('expression_title', 'alpha'); $expression = GETPOST('expression'); $tab = GETPOST('tab', 'alpha'); $tab = !empty($tab) ? $tab : 'card'; $tab = strtolower($tab); // Security check $result = restrictedArea($user, 'produit|service&fournisseur', $id, 'product&product', '', '', 'rowid'); //Initialize objects $product = new Product($db); $product->fetch($id, ''); $price_expression = new PriceExpression($db); $price_globals = new PriceGlobalVariable($db); //Fetch expression data if (empty($eid)) { $eid = 0; } else { if ($action != 'delete') { $price_expression->fetch($eid); } } /* * Actions */ if ($action == 'add') { if ($eid == 0) { $result = $price_expression->find_title($title); if ($result == 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"]; }