--- /tmp/dsg/dolibarr/htdocs/product/dynamic_price/class/github_price_expression.class.php +++ /tmp/dsg/dolibarr/htdocs/product/dynamic_price/class/client_price_expression.class.php @@ -29,337 +29,353 @@ */ class PriceExpression { - /** - * @var DoliDB Database handler. - */ - public $db; - - /** - * @var string Error code (or message) - */ - public $error = ''; - - /** - * @var string[] Error codes (or messages) - */ - public $errors = array(); - - /** - * @var int ID - */ - public $id; - - public $title; - public $expression; - - /** - * @var string Name of table without prefix where object is stored - */ - public $table_element = "c_price_expression"; - - /** - * Constructor - * - * @param DoliDb $db Database handler - */ - public function __construct($db) - { - $this->db = $db; - } - - - /** - * Create object into database - * - * @param User $user User that creates - * @param int $notrigger 0=launch triggers after, 1=disable triggers - * @return int <0 if KO, Id of created object if OK - */ - public function create($user, $notrigger = 0) - { - $error = 0; - - // Clean parameters - if (isset($this->title)) $this->title = trim($this->title); - if (isset($this->expression)) $this->expression = trim($this->expression); - - // Insert request - $sql = "INSERT INTO ".MAIN_DB_PREFIX.$this->table_element." ("; - $sql .= "title, expression"; - $sql .= ") VALUES ("; - $sql .= " ".(isset($this->title) ? "'".$this->db->escape($this->title)."'" : "''").","; - $sql .= " ".(isset($this->expression) ? "'".$this->db->escape($this->expression)."'" : "''"); - $sql .= ")"; - - $this->db->begin(); - - dol_syslog(__METHOD__, LOG_DEBUG); - $resql = $this->db->query($sql); - if (!$resql) { $error++; $this->errors[] = "Error ".$this->db->lasterror(); } - - if (!$error) - { - $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element); - - //if (! $notrigger) - //{ - // Uncomment this and change MYOBJECT to your own tag if you - // want this action calls a trigger. - - //// Call triggers - //$result=$this->call_trigger('MYOBJECT_CREATE',$user); - //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail} - //// End call triggers - //} - } - - // Commit or rollback - if ($error) - { - foreach ($this->errors as $errmsg) - { - dol_syslog(__METHOD__." ".$errmsg, LOG_ERR); - $this->error .= ($this->error ? ', '.$errmsg : $errmsg); - } - $this->db->rollback(); - return -1 * $error; - } else { - $this->db->commit(); - return $this->id; - } - } - - - /** - * Load object in memory from the database - * - * @param int $id Id object - * @return int < 0 if KO, 0 if OK but not found, > 0 if OK - */ - public function fetch($id) - { - // Check parameters - if (empty($id)) - { - $this->error = 'ErrorWrongParameters'; - return -1; - } - - $sql = "SELECT title, expression"; - $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element; - $sql .= " WHERE rowid = ".$id; - - dol_syslog(__METHOD__); - $resql = $this->db->query($sql); - if ($resql) - { - $obj = $this->db->fetch_object($resql); - if ($obj) - { - $this->id = $id; - $this->title = $obj->title; - $this->expression = $obj->expression; - return 1; - } else { - return 0; - } - } else { - $this->error = "Error ".$this->db->lasterror(); - return -1; - } - } - - // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps - /** - * List all price expressions - * - * @return array Array of price expressions - */ - public function list_price_expression() - { - // phpcs:enable - $sql = "SELECT rowid, title, expression"; - $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element; - $sql .= " ORDER BY title"; - - dol_syslog(__METHOD__, LOG_DEBUG); - $resql = $this->db->query($sql); - if ($resql) - { - $retarray = array(); - - while ($record = $this->db->fetch_array($resql)) - { - $price_expression_obj = new PriceExpression($this->db); - $price_expression_obj->id = $record["rowid"]; - $price_expression_obj->title = $record["title"]; - $price_expression_obj->expression = $record["expression"]; - $retarray[] = $price_expression_obj; - } - - $this->db->free($resql); - return $retarray; - } else { - $this->error = $this->db->error(); - return -1; - } - } - - - // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps - /** - * Returns any existing rowid with specified title - * - * @param String $title Title of expression - * @return int < 0 if KO, 0 if OK but not found, > 0 rowid - */ - public function find_title($title) - { - // phpcs:enable - $sql = "SELECT rowid"; - $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element; - $sql .= " WHERE title = '".$this->db->escape($title)."'"; - - dol_syslog(__METHOD__, LOG_DEBUG); - $resql = $this->db->query($sql); - if ($resql) - { - $obj = $this->db->fetch_object($resql); - if ($obj) - { - return (int) $obj->rowid; - } else { - return 0; - } - } else { - $this->error = "Error ".$this->db->lasterror(); - return -1; - } - } - - - /** - * Update object into database - * - * @param User $user User that modifies - * @param int $notrigger 0=launch triggers after, 1=disable triggers - * @return int <0 if KO, >0 if OK - */ - public function update($user = 0, $notrigger = 0) - { - $error = 0; - - // Clean parameters - if (isset($this->title)) $this->title = trim($this->title); - if (isset($this->expression)) $this->expression = trim($this->expression); - - // Update request - $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET"; - $sql .= " title = ".(isset($this->title) ? "'".$this->db->escape($this->title)."'" : "''").","; - $sql .= " expression = ".(isset($this->expression) ? "'".$this->db->escape($this->expression)."'" : "''").""; - $sql .= " WHERE rowid = ".$this->id; - - $this->db->begin(); - - dol_syslog(__METHOD__); - $resql = $this->db->query($sql); - if (!$resql) { $error++; $this->errors[] = "Error ".$this->db->lasterror(); } - - // if (! $error) - // { - // if (! $notrigger) - // { - // // Uncomment this and change MYOBJECT to your own tag if you - // // want this action calls a trigger. - - // //// Call triggers - // //$result=$this->call_trigger('MYOBJECT_MODIFY',$user); - // //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail} - // //// End call triggers - // } - // } - - // Commit or rollback - if ($error) - { - foreach ($this->errors as $errmsg) - { - dol_syslog(__METHOD__." ".$errmsg, LOG_ERR); - $this->error .= ($this->error ? ', '.$errmsg : $errmsg); - } - $this->db->rollback(); - return -1 * $error; - } else { - $this->db->commit(); - return 1; - } - } - - - /** - * Delete object in database - * - * @param User $user User that deletes - * @param int $notrigger 0=launch triggers after, 1=disable triggers - * @return int <0 if KO, >0 if OK - */ - public function delete(User $user, $notrigger = 0) - { - $error = 0; - - $rowid = $this->id; - - $this->db->begin(); - - //if (! $error) - //{ - // if (! $notrigger) - // { - // Uncomment this and change MYOBJECT to your own tag if you - // want this action calls a trigger. - - //// Call triggers - //$result=$this->call_trigger('MYOBJECT_DELETE',$user); - //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail} - //// End call triggers - // } - //} - - if (!$error) - { - $sql = "DELETE FROM ".MAIN_DB_PREFIX.$this->table_element; - $sql .= " WHERE rowid = ".$rowid; - - dol_syslog(__METHOD__); - $resql = $this->db->query($sql); - if (!$resql) { $error++; $this->errors[] = "Error ".$this->db->lasterror(); } - } - - // Commit or rollback - if ($error) - { - foreach ($this->errors as $errmsg) - { - dol_syslog(__METHOD__." ".$errmsg, LOG_ERR); - $this->error .= ($this->error ? ', '.$errmsg : $errmsg); - } - $this->db->rollback(); - return -1 * $error; - } else { - $this->db->commit(); - return 1; - } - } - - /** - * Initialise object with example values - * Id must be 0 if object instance is a specimen - * - * @return void - */ - public function initAsSpecimen() - { - $this->id = 0; - $this->expression = ''; - } + /** + * @var DoliDB Database handler. + */ + public $db; + + /** + * @var string Error code (or message) + */ + public $error = ''; + + /** + * @var string[] Error codes (or messages) + */ + public $errors = array(); + + /** + * @var int ID + */ + public $id; + + public $title; + public $expression; + + /** + * @var string Name of table without prefix where object is stored + */ + public $table_element = "c_price_expression"; + + /** + * Constructor + * + * @param DoliDb $db Database handler + */ + public function __construct($db) + { + $this->db = $db; + } + + + /** + * Create object into database + * + * @param User $user User that creates + * @param int $notrigger 0=launch triggers after, 1=disable triggers + * @return int <0 if KO, Id of created object if OK + */ + public function create($user, $notrigger = 0) + { + $error = 0; + + // Clean parameters + if (isset($this->title)) $this->title = trim($this->title); + if (isset($this->expression)) $this->expression = trim($this->expression); + + // Insert request + $sql = "INSERT INTO ".MAIN_DB_PREFIX.$this->table_element." ("; + $sql .= "title, expression"; + $sql .= ") VALUES ("; + $sql .= " ".(isset($this->title) ? "'".$this->db->escape($this->title)."'" : "''").","; + $sql .= " ".(isset($this->expression) ? "'".$this->db->escape($this->expression)."'" : "''"); + $sql .= ")"; + + $this->db->begin(); + + dol_syslog(__METHOD__, LOG_DEBUG); + $resql = $this->db->query($sql); + if (!$resql) { $error++; $this->errors[] = "Error ".$this->db->lasterror(); } + + if (!$error) + { + $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element); + + //if (! $notrigger) + //{ + // Uncomment this and change MYOBJECT to your own tag if you + // want this action calls a trigger. + + //// Call triggers + //$result=$this->call_trigger('MYOBJECT_CREATE',$user); + //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail} + //// End call triggers + //} + } + + // Commit or rollback + if ($error) + { + foreach ($this->errors as $errmsg) + { + dol_syslog(__METHOD__." ".$errmsg, LOG_ERR); + $this->error .= ($this->error ? ', '.$errmsg : $errmsg); + } + $this->db->rollback(); + return -1 * $error; + } + else + { + $this->db->commit(); + return $this->id; + } + } + + + /** + * Load object in memory from the database + * + * @param int $id Id object + * @return int < 0 if KO, 0 if OK but not found, > 0 if OK + */ + public function fetch($id) + { + // Check parameters + if (empty($id)) + { + $this->error = 'ErrorWrongParameters'; + return -1; + } + + $sql = "SELECT title, expression"; + $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element; + $sql .= " WHERE rowid = ".$id; + + dol_syslog(__METHOD__); + $resql = $this->db->query($sql); + if ($resql) + { + $obj = $this->db->fetch_object($resql); + if ($obj) + { + $this->id = $id; + $this->title = $obj->title; + $this->expression = $obj->expression; + return 1; + } + else + { + return 0; + } + } + else + { + $this->error = "Error ".$this->db->lasterror(); + return -1; + } + } + + // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps + /** + * List all price expressions + * + * @return array Array of price expressions + */ + public function list_price_expression() + { + // phpcs:enable + $sql = "SELECT rowid, title, expression"; + $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element; + $sql .= " ORDER BY title"; + + dol_syslog(__METHOD__, LOG_DEBUG); + $resql = $this->db->query($sql); + if ($resql) + { + $retarray = array(); + + while ($record = $this->db->fetch_array($resql)) + { + $price_expression_obj = new PriceExpression($this->db); + $price_expression_obj->id = $record["rowid"]; + $price_expression_obj->title = $record["title"]; + $price_expression_obj->expression = $record["expression"]; + $retarray[] = $price_expression_obj; + } + + $this->db->free($resql); + return $retarray; + } + else + { + $this->error = $this->db->error(); + return -1; + } + } + + + // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps + /** + * Returns any existing rowid with specified title + * + * @param String $title Title of expression + * @return int < 0 if KO, 0 if OK but not found, > 0 rowid + */ + public function find_title($title) + { + // phpcs:enable + $sql = "SELECT rowid"; + $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element; + $sql .= " WHERE title = '".$this->db->escape($title)."'"; + + dol_syslog(__METHOD__, LOG_DEBUG); + $resql = $this->db->query($sql); + if ($resql) + { + $obj = $this->db->fetch_object($resql); + if ($obj) + { + return (int) $obj->rowid; + } + else + { + return 0; + } + } + else + { + $this->error = "Error ".$this->db->lasterror(); + return -1; + } + } + + + /** + * Update object into database + * + * @param User $user User that modifies + * @param int $notrigger 0=launch triggers after, 1=disable triggers + * @return int <0 if KO, >0 if OK + */ + public function update($user = 0, $notrigger = 0) + { + $error = 0; + + // Clean parameters + if (isset($this->title)) $this->title = trim($this->title); + if (isset($this->expression)) $this->expression = trim($this->expression); + + // Update request + $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET"; + $sql .= " title = ".(isset($this->title) ? "'".$this->db->escape($this->title)."'" : "''").","; + $sql .= " expression = ".(isset($this->expression) ? "'".$this->db->escape($this->expression)."'" : "''").""; + $sql .= " WHERE rowid = ".$this->id; + + $this->db->begin(); + + dol_syslog(__METHOD__); + $resql = $this->db->query($sql); + if (!$resql) { $error++; $this->errors[] = "Error ".$this->db->lasterror(); } + + // if (! $error) + // { + // if (! $notrigger) + // { + // // Uncomment this and change MYOBJECT to your own tag if you + // // want this action calls a trigger. + + // //// Call triggers + // //$result=$this->call_trigger('MYOBJECT_MODIFY',$user); + // //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail} + // //// End call triggers + // } + // } + + // Commit or rollback + if ($error) + { + foreach ($this->errors as $errmsg) + { + dol_syslog(__METHOD__." ".$errmsg, LOG_ERR); + $this->error .= ($this->error ? ', '.$errmsg : $errmsg); + } + $this->db->rollback(); + return -1 * $error; + } + else + { + $this->db->commit(); + return 1; + } + } + + + /** + * Delete object in database + * + * @param User $user User that deletes + * @param int $notrigger 0=launch triggers after, 1=disable triggers + * @return int <0 if KO, >0 if OK + */ + public function delete(User $user, $notrigger = 0) + { + $error = 0; + + $rowid = $this->id; + + $this->db->begin(); + + //if (! $error) + //{ + // if (! $notrigger) + // { + // Uncomment this and change MYOBJECT to your own tag if you + // want this action calls a trigger. + + //// Call triggers + //$result=$this->call_trigger('MYOBJECT_DELETE',$user); + //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail} + //// End call triggers + // } + //} + + if (!$error) + { + $sql = "DELETE FROM ".MAIN_DB_PREFIX.$this->table_element; + $sql .= " WHERE rowid = ".$rowid; + + dol_syslog(__METHOD__); + $resql = $this->db->query($sql); + if (!$resql) { $error++; $this->errors[] = "Error ".$this->db->lasterror(); } + } + + // Commit or rollback + if ($error) + { + foreach ($this->errors as $errmsg) + { + dol_syslog(__METHOD__." ".$errmsg, LOG_ERR); + $this->error .= ($this->error ? ', '.$errmsg : $errmsg); + } + $this->db->rollback(); + return -1 * $error; + } + else + { + $this->db->commit(); + return 1; + } + } + + /** + * Initialise object with example values + * Id must be 0 if object instance is a specimen + * + * @return void + */ + public function initAsSpecimen() + { + $this->id = 0; + $this->expression = ''; + } } --- /tmp/dsg/dolibarr/htdocs/product/dynamic_price/class/github_price_global_variable.class.php +++ /tmp/dsg/dolibarr/htdocs/product/dynamic_price/class/client_price_global_variable.class.php @@ -29,319 +29,331 @@ */ class PriceGlobalVariable { - /** - * @var DoliDB Database handler. - */ - public $db; - - /** - * @var string Error code (or message) - */ - public $error = ''; - - /** - * @var string[] Error codes (or messages) - */ - public $errors = array(); - - /** - * @var int ID - */ - public $id; - - public $code; - - /** - * @var string description - */ - public $description; - - public $value; - - /** - * @var string Name of table without prefix where object is stored - */ - public $table_element = "c_price_global_variable"; - - /** - * Constructor - * - * @param DoliDb $db Database handler - */ - public function __construct($db) - { - $this->db = $db; - } - - - /** - * Create object into database - * - * @param User $user User that creates - * @param int $notrigger 0=launch triggers after, 1=disable triggers - * @return int <0 if KO, Id of created object if OK - */ - public function create($user, $notrigger = 0) - { - $error = 0; - - $this->checkParameters(); - - // Insert request - $sql = "INSERT INTO ".MAIN_DB_PREFIX.$this->table_element." ("; - $sql .= "code, description, value"; - $sql .= ") VALUES ("; - $sql .= " ".(isset($this->code) ? "'".$this->db->escape($this->code)."'" : "''").","; - $sql .= " ".(isset($this->description) ? "'".$this->db->escape($this->description)."'" : "''").","; - $sql .= " ".$this->value; - $sql .= ")"; - - $this->db->begin(); - - dol_syslog(__METHOD__); - $resql = $this->db->query($sql); - if (!$resql) { $error++; $this->errors[] = "Error ".$this->db->lasterror(); } - - if (!$error) - { - $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element); - - if (!$notrigger) - { - // Uncomment this and change MYOBJECT to your own tag if you - // want this action calls a trigger. - - //// Call triggers - //$result=$this->call_trigger('MYOBJECT_CREATE',$user); - //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail} - //// End call triggers - } - } - - // Commit or rollback - if ($error) - { - foreach ($this->errors as $errmsg) - { - dol_syslog(__METHOD__." ".$errmsg, LOG_ERR); - $this->error .= ($this->error ? ', '.$errmsg : $errmsg); - } - $this->db->rollback(); - return -1 * $error; - } else { - $this->db->commit(); - return $this->id; - } - } - - - /** - * Load object in memory from the database - * - * @param int $id Id object - * @return int < 0 if KO, 0 if OK but not found, > 0 if OK - */ - public function fetch($id) - { - $sql = "SELECT code, description, value"; - $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element; - $sql .= " WHERE rowid = ".$id; - - dol_syslog(__METHOD__); - $resql = $this->db->query($sql); - if ($resql) - { - $obj = $this->db->fetch_object($resql); - if ($obj) - { - $this->id = $id; - $this->code = $obj->code; - $this->description = $obj->description; - $this->value = $obj->value; - $this->checkParameters(); - return 1; - } else { - return 0; - } - } else { - $this->error = "Error ".$this->db->lasterror(); - return -1; - } - } - - /** - * Update object into database - * - * @param User $user User that modifies - * @param int $notrigger 0=launch triggers after, 1=disable triggers - * @return int <0 if KO, >0 if OK - */ - public function update($user = 0, $notrigger = 0) - { - $error = 0; - - $this->checkParameters(); - - // Update request - $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET"; - $sql .= " code = ".(isset($this->code) ? "'".$this->db->escape($this->code)."'" : "''").","; - $sql .= " description = ".(isset($this->description) ? "'".$this->db->escape($this->description)."'" : "''").","; - $sql .= " value = ".$this->value; - $sql .= " WHERE rowid = ".$this->id; - - $this->db->begin(); - - dol_syslog(__METHOD__); - $resql = $this->db->query($sql); - if (!$resql) { $error++; $this->errors[] = "Error ".$this->db->lasterror(); } - - // if (! $error) - // { - // if (! $notrigger) - // { - // // Uncomment this and change MYOBJECT to your own tag if you - // // want this action calls a trigger. - - // //// Call triggers - // //$result=$this->call_trigger('MYOBJECT_MODIFY',$user); - // //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail} - // //// End call triggers - // } - // } - - // Commit or rollback - if ($error) - { - foreach ($this->errors as $errmsg) - { - dol_syslog(__METHOD__." ".$errmsg, LOG_ERR); - $this->error .= ($this->error ? ', '.$errmsg : $errmsg); - } - $this->db->rollback(); - return -1 * $error; - } else { - $this->db->commit(); - return 1; - } - } - - - /** - * Delete object in database - * - * @param int $rowid Row id of global variable - * @param User $user User that deletes - * @param int $notrigger 0=launch triggers after, 1=disable triggers - * @return int <0 if KO, >0 if OK - */ - public function delete($rowid, $user, $notrigger = 0) - { - $error = 0; - - $this->db->begin(); - - if (!$error) - { - if (!$notrigger) - { - // Uncomment this and change MYOBJECT to your own tag if you - // want this action calls a trigger. - - //// Call triggers - //$result=$this->call_trigger('MYOBJECT_DELETE',$user); - //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail} - //// End call triggers - } - } - - if (!$error) - { - $sql = "DELETE FROM ".MAIN_DB_PREFIX.$this->table_element; - $sql .= " WHERE rowid = ".$rowid; - - dol_syslog(__METHOD__); - $resql = $this->db->query($sql); - if (!$resql) { $error++; $this->errors[] = "Error ".$this->db->lasterror(); } - } - - // Commit or rollback - if ($error) - { - foreach ($this->errors as $errmsg) - { - dol_syslog(__METHOD__." ".$errmsg, LOG_ERR); - $this->error .= ($this->error ? ', '.$errmsg : $errmsg); - } - $this->db->rollback(); - return -1 * $error; - } else { - $this->db->commit(); - return 1; - } - } - - /** - * Initialise object with example values - * Id must be 0 if object instance is a specimen - * - * @return void - */ - public function initAsSpecimen() - { - $this->id = 0; - $this->code = ''; - $this->description = ''; - $this->value = ''; - } - - /** - * Checks if all parameters are in order - * - * @return void - */ - public function checkParameters() - { - // Clean parameters - if (isset($this->code)) $this->code = trim($this->code); - if (isset($this->description)) $this->description = trim($this->description); - - // Check parameters - if (empty($this->value) || !is_numeric($this->value)) $this->value = 0; - } - - /** - * List all price global variables - * - * @return array Array of price global variables - */ - public function listGlobalVariables() - { - $sql = "SELECT rowid, code, description, value"; - $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element; - $sql .= " ORDER BY code"; - - dol_syslog(__METHOD__, LOG_DEBUG); - $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; - } - } + /** + * @var DoliDB Database handler. + */ + public $db; + + /** + * @var string Error code (or message) + */ + public $error = ''; + + /** + * @var string[] Error codes (or messages) + */ + public $errors = array(); + + /** + * @var int ID + */ + public $id; + + public $code; + + /** + * @var string description + */ + public $description; + + public $value; + + /** + * @var string Name of table without prefix where object is stored + */ + public $table_element = "c_price_global_variable"; + + /** + * Constructor + * + * @param DoliDb $db Database handler + */ + public function __construct($db) + { + $this->db = $db; + } + + + /** + * Create object into database + * + * @param User $user User that creates + * @param int $notrigger 0=launch triggers after, 1=disable triggers + * @return int <0 if KO, Id of created object if OK + */ + public function create($user, $notrigger = 0) + { + $error = 0; + + $this->checkParameters(); + + // Insert request + $sql = "INSERT INTO ".MAIN_DB_PREFIX.$this->table_element." ("; + $sql .= "code, description, value"; + $sql .= ") VALUES ("; + $sql .= " ".(isset($this->code) ? "'".$this->db->escape($this->code)."'" : "''").","; + $sql .= " ".(isset($this->description) ? "'".$this->db->escape($this->description)."'" : "''").","; + $sql .= " ".$this->value; + $sql .= ")"; + + $this->db->begin(); + + dol_syslog(__METHOD__); + $resql = $this->db->query($sql); + if (!$resql) { $error++; $this->errors[] = "Error ".$this->db->lasterror(); } + + if (!$error) + { + $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element); + + if (!$notrigger) + { + // Uncomment this and change MYOBJECT to your own tag if you + // want this action calls a trigger. + + //// Call triggers + //$result=$this->call_trigger('MYOBJECT_CREATE',$user); + //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail} + //// End call triggers + } + } + + // Commit or rollback + if ($error) + { + foreach ($this->errors as $errmsg) + { + dol_syslog(__METHOD__." ".$errmsg, LOG_ERR); + $this->error .= ($this->error ? ', '.$errmsg : $errmsg); + } + $this->db->rollback(); + return -1 * $error; + } + else + { + $this->db->commit(); + return $this->id; + } + } + + + /** + * Load object in memory from the database + * + * @param int $id Id object + * @return int < 0 if KO, 0 if OK but not found, > 0 if OK + */ + public function fetch($id) + { + $sql = "SELECT code, description, value"; + $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element; + $sql .= " WHERE rowid = ".$id; + + dol_syslog(__METHOD__); + $resql = $this->db->query($sql); + if ($resql) + { + $obj = $this->db->fetch_object($resql); + if ($obj) + { + $this->id = $id; + $this->code = $obj->code; + $this->description = $obj->description; + $this->value = $obj->value; + $this->checkParameters(); + return 1; + } + else + { + return 0; + } + } + else + { + $this->error = "Error ".$this->db->lasterror(); + return -1; + } + } + + /** + * Update object into database + * + * @param User $user User that modifies + * @param int $notrigger 0=launch triggers after, 1=disable triggers + * @return int <0 if KO, >0 if OK + */ + public function update($user = 0, $notrigger = 0) + { + $error = 0; + + $this->checkParameters(); + + // Update request + $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET"; + $sql .= " code = ".(isset($this->code) ? "'".$this->db->escape($this->code)."'" : "''").","; + $sql .= " description = ".(isset($this->description) ? "'".$this->db->escape($this->description)."'" : "''").","; + $sql .= " value = ".$this->value; + $sql .= " WHERE rowid = ".$this->id; + + $this->db->begin(); + + dol_syslog(__METHOD__); + $resql = $this->db->query($sql); + if (!$resql) { $error++; $this->errors[] = "Error ".$this->db->lasterror(); } + + // if (! $error) + // { + // if (! $notrigger) + // { + // // Uncomment this and change MYOBJECT to your own tag if you + // // want this action calls a trigger. + + // //// Call triggers + // //$result=$this->call_trigger('MYOBJECT_MODIFY',$user); + // //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail} + // //// End call triggers + // } + // } + + // Commit or rollback + if ($error) + { + foreach ($this->errors as $errmsg) + { + dol_syslog(__METHOD__." ".$errmsg, LOG_ERR); + $this->error .= ($this->error ? ', '.$errmsg : $errmsg); + } + $this->db->rollback(); + return -1 * $error; + } + else + { + $this->db->commit(); + return 1; + } + } + + + /** + * Delete object in database + * + * @param int $rowid Row id of global variable + * @param User $user User that deletes + * @param int $notrigger 0=launch triggers after, 1=disable triggers + * @return int <0 if KO, >0 if OK + */ + public function delete($rowid, $user, $notrigger = 0) + { + $error = 0; + + $this->db->begin(); + + if (!$error) + { + if (!$notrigger) + { + // Uncomment this and change MYOBJECT to your own tag if you + // want this action calls a trigger. + + //// Call triggers + //$result=$this->call_trigger('MYOBJECT_DELETE',$user); + //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail} + //// End call triggers + } + } + + if (!$error) + { + $sql = "DELETE FROM ".MAIN_DB_PREFIX.$this->table_element; + $sql .= " WHERE rowid = ".$rowid; + + dol_syslog(__METHOD__); + $resql = $this->db->query($sql); + if (!$resql) { $error++; $this->errors[] = "Error ".$this->db->lasterror(); } + } + + // Commit or rollback + if ($error) + { + foreach ($this->errors as $errmsg) + { + dol_syslog(__METHOD__." ".$errmsg, LOG_ERR); + $this->error .= ($this->error ? ', '.$errmsg : $errmsg); + } + $this->db->rollback(); + return -1 * $error; + } + else + { + $this->db->commit(); + return 1; + } + } + + /** + * Initialise object with example values + * Id must be 0 if object instance is a specimen + * + * @return void + */ + public function initAsSpecimen() + { + $this->id = 0; + $this->code = ''; + $this->description = ''; + $this->value = ''; + } + + /** + * Checks if all parameters are in order + * + * @return void + */ + public function checkParameters() + { + // Clean parameters + if (isset($this->code)) $this->code = trim($this->code); + if (isset($this->description)) $this->description = trim($this->description); + + // Check parameters + if (empty($this->value) || !is_numeric($this->value)) $this->value = 0; + } + + /** + * List all price global variables + * + * @return array Array of price global variables + */ + public function listGlobalVariables() + { + $sql = "SELECT rowid, code, description, value"; + $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element; + $sql .= " ORDER BY code"; + + dol_syslog(__METHOD__, LOG_DEBUG); + $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; + } + } } --- /tmp/dsg/dolibarr/htdocs/product/dynamic_price/class/github_price_global_variable_updater.class.php +++ /tmp/dsg/dolibarr/htdocs/product/dynamic_price/class/client_price_global_variable_updater.class.php @@ -29,608 +29,626 @@ */ class PriceGlobalVariableUpdater { - /** - * @var DoliDB Database handler. - */ - public $db; - - /** - * @var string Error code (or message) - */ - public $error = ''; - - /** - * @var string[] Error codes (or messages) - */ - public $errors = array(); - - public $types = array(0, 1); //!< Updater types - public $update_min = 5; //!< Minimal update rate - - /** - * @var int ID - */ - public $id; - - public $type; - - /** - * @var string description - */ - public $description; - - public $parameters; - - /** - * @var int ID - */ - public $fk_variable; - - public $update_interval; //!< Interval in mins - public $next_update; //!< Next update timestamp - public $last_status; - - /** - * @var string Name of table without prefix where object is stored - */ - public $table_element = "c_price_global_variable_updater"; - - /** - * Constructor - * - * @param DoliDb $db Database handler - */ - public function __construct($db) - { - $this->db = $db; - } - - - /** - * Create object into database - * - * @param User $user User that creates - * @param int $notrigger 0=launch triggers after, 1=disable triggers - * @return int <0 if KO, Id of created object if OK - */ - public function create($user, $notrigger = 0) - { - $error = 0; - - $this->checkParameters(); - - // Insert request - $sql = "INSERT INTO ".MAIN_DB_PREFIX.$this->table_element." ("; - $sql .= "type, description, parameters, fk_variable, update_interval, next_update, last_status"; - $sql .= ") VALUES ("; - $sql .= " ".$this->type.","; - $sql .= " ".(isset($this->description) ? "'".$this->db->escape($this->description)."'" : "''").","; - $sql .= " ".(isset($this->parameters) ? "'".$this->db->escape($this->parameters)."'" : "''").","; - $sql .= " ".$this->fk_variable.","; - $sql .= " ".$this->update_interval.","; - $sql .= " ".$this->next_update.","; - $sql .= " ".(isset($this->last_status) ? "'".$this->db->escape($this->last_status)."'" : "''"); - $sql .= ")"; - - $this->db->begin(); - - dol_syslog(__METHOD__, LOG_DEBUG); - $resql = $this->db->query($sql); - if (!$resql) { $error++; $this->errors[] = "Error ".$this->db->lasterror(); } - - if (!$error) - { - $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element); - - if (!$notrigger) - { - // Uncomment this and change MYOBJECT to your own tag if you - // want this action calls a trigger. - - //// Call triggers - //$result=$this->call_trigger('MYOBJECT_CREATE',$user); - //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail} - //// End call triggers - } - } - - // Commit or rollback - if ($error) - { - foreach ($this->errors as $errmsg) - { - dol_syslog(__METHOD__." ".$errmsg, LOG_ERR); - $this->error .= ($this->error ? ', '.$errmsg : $errmsg); - } - $this->db->rollback(); - return -1 * $error; - } else { - $this->db->commit(); - return $this->id; - } - } - - - /** - * Load object in memory from the database - * - * @param int $id Id object - * @return int < 0 if KO, 0 if OK but not found, > 0 if OK - */ - public function fetch($id) - { - $sql = "SELECT type, description, parameters, fk_variable, update_interval, next_update, last_status"; - $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element; - $sql .= " WHERE rowid = ".$id; - - dol_syslog(__METHOD__); - $resql = $this->db->query($sql); - if ($resql) - { - $obj = $this->db->fetch_object($resql); - if ($obj) - { - $this->id = $id; - $this->type = $obj->type; - $this->description = $obj->description; - $this->parameters = $obj->parameters; - $this->fk_variable = $obj->fk_variable; - $this->update_interval = $obj->update_interval; - $this->next_update = $obj->next_update; - $this->last_status = $obj->last_status; - $this->checkParameters(); - return 1; - } else { - return 0; - } - } else { - $this->error = "Error ".$this->db->lasterror(); - return -1; - } - } - - /** - * Update object into database - * - * @param User $user User that modifies - * @param int $notrigger 0=launch triggers after, 1=disable triggers - * @return int <0 if KO, >0 if OK - */ - public function update($user = 0, $notrigger = 0) - { - $error = 0; - - $this->checkParameters(); - - // Update request - $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET"; - $sql .= " type = ".$this->type.","; - $sql .= " description = ".(isset($this->description) ? "'".$this->db->escape($this->description)."'" : "''").","; - $sql .= " parameters = ".(isset($this->parameters) ? "'".$this->db->escape($this->parameters)."'" : "''").","; - $sql .= " fk_variable = ".$this->fk_variable.","; - $sql .= " update_interval = ".$this->update_interval.","; - $sql .= " next_update = ".$this->next_update.","; - $sql .= " last_status = ".(isset($this->last_status) ? "'".$this->db->escape($this->last_status)."'" : "''"); - $sql .= " WHERE rowid = ".$this->id; - - $this->db->begin(); - - dol_syslog(__METHOD__); - $resql = $this->db->query($sql); - if (!$resql) { $error++; $this->errors[] = "Error ".$this->db->lasterror(); } - - // if (! $error) - // { - // if (! $notrigger) - // { - // // Uncomment this and change MYOBJECT to your own tag if you - // // want this action calls a trigger. - - // //// Call triggers - // //$result=$this->call_trigger('MYOBJECT_MODIFY',$user); - // //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail} - // //// End call triggers - // } - // } - - // Commit or rollback - if ($error) - { - foreach ($this->errors as $errmsg) - { - dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR); - $this->error .= ($this->error ? ', '.$errmsg : $errmsg); - } - $this->db->rollback(); - return -1 * $error; - } else { - $this->db->commit(); - return 1; - } - } - - /** - * Delete object in database - * - * @param int $rowid Row id of global variable - * @param User $user User that deletes - * @param int $notrigger 0=launch triggers after, 1=disable triggers - * @return int <0 if KO, >0 if OK - */ - public function delete($rowid, $user, $notrigger = 0) - { - $error = 0; - - $this->db->begin(); - - //if (! $error) - //{ - // if (! $notrigger) - // { - // Uncomment this and change MYOBJECT to your own tag if you - // want this action calls a trigger. - - //// Call triggers - //$result=$this->call_trigger('MYOBJECT_DELETE',$user); - //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail} - //// End call triggers - // } - //} - - if (!$error) - { - $sql = "DELETE FROM ".MAIN_DB_PREFIX.$this->table_element; - $sql .= " WHERE rowid = ".$rowid; - - dol_syslog(__METHOD__); - $resql = $this->db->query($sql); - if (!$resql) { $error++; $this->errors[] = "Error ".$this->db->lasterror(); } - } - - // Commit or rollback - if ($error) - { - foreach ($this->errors as $errmsg) - { - dol_syslog(__METHOD__." ".$errmsg, LOG_ERR); - $this->error .= ($this->error ? ', '.$errmsg : $errmsg); - } - $this->db->rollback(); - return -1 * $error; - } else { - $this->db->commit(); - return 1; - } - } - - /** - * Initialise object with example values - * Id must be 0 if object instance is a specimen - * - * @return void - */ - public function initAsSpecimen() - { - $this->id = 0; - $this->type = 0; - $this->description = ''; - $this->parameters = ''; - $this->fk_variable = 0; - $this->update_interval = 0; - $this->next_update = 0; - $this->last_status = ''; - } - - /** - * Returns the last updated time in string html format, returns "never" if its less than 1 - * - * @return string - */ - public function getLastUpdated() - { - global $langs; - $last = $this->next_update - ($this->update_interval * 60); - if ($last < 1) { - return $langs->trans("Never"); - } - $status = empty($this->last_status) ? $langs->trans("CorrectlyUpdated") : $this->last_status; - return $status.'
'.dol_print_date($last, '%d/%m/%Y %H:%M:%S'); - } - - /** - * Checks if all parameters are in order - * - * @return void - */ - public function checkParameters() - { - // Clean parameters - if (isset($this->description)) $this->description = trim($this->description); - if (isset($this->parameters)) $this->parameters = trim($this->parameters); - else $this->parameters = ""; - if (isset($this->last_status)) $this->last_status = trim($this->last_status); - - // Check parameters - if (empty($this->type) || !is_numeric($this->type) || !in_array($this->type, $this->types)) $this->type = 0; - if (empty($this->update_interval) || !is_numeric($this->update_interval) || $this->update_interval < 1) $this->update_interval = $this->update_min; - if (empty($this->next_update) || !is_numeric($this->next_update) || $this->next_update < 0) $this->next_update = 0; - } - - /** - * List all price global variables - * - * @return array Array of price global variable updaters - */ - public function listUpdaters() - { - $sql = "SELECT rowid, type, description, parameters, fk_variable, update_interval, next_update, last_status"; - $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element; - - dol_syslog(__METHOD__, LOG_DEBUG); - $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; - } - } - - /** - * List all updaters which need to be processed - * - * @return array Array of price global variable updaters - */ - public 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(__METHOD__, LOG_DEBUG); - $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; - } - } - - /** - * Handles the processing of this updater - * - * @return int <0 if KO, 0 if OK but no global variable found, >0 if OK - */ - public function process() - { - global $langs, $user; - $langs->load("errors"); - dol_syslog(__METHOD__, LOG_DEBUG); - - $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) { - // Call JSON request - include_once DOL_DOCUMENT_ROOT.'/core/lib/geturl.lib.php'; - $tmpresult = getURLContent($url); - $code = $tmpresult['http_code']; - $result = $tmpresult['content']; - - if (!isset($result)) { - $this->error = $langs->trans("ErrorGlobalVariableUpdater0", "empty response"); - return -1; - } - if ($code !== 200) { - $this->error = $langs->trans("ErrorGlobalVariableUpdater0", $code.' '.$tmpresult['curl_error_msg']); - 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; - } - - // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps - /** - * Update next_update into database - * - * @param string $next_update Next update to write - * @param User $user User that modifies - * @param int $notrigger 0=launch triggers after, 1=disable triggers - * @return int <0 if KO, >0 if OK - */ - public function update_next_update($next_update, $user = 0, $notrigger = 0) - { - // phpcs:enable - $error = 0; - - $this->next_update = $next_update; - $this->checkParameters(); - - // Update request - $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET"; - $sql .= " next_update = ".$this->next_update; - $sql .= " WHERE rowid = ".$this->id; - - $this->db->begin(); - - dol_syslog(__METHOD__); - $resql = $this->db->query($sql); - if (!$resql) { $error++; $this->errors[] = "Error ".$this->db->lasterror(); } - - // Commit or rollback - if ($error) - { - foreach ($this->errors as $errmsg) - { - dol_syslog(__METHOD__." ".$errmsg, LOG_ERR); - $this->error .= ($this->error ? ', '.$errmsg : $errmsg); - } - $this->db->rollback(); - return -1 * $error; - } else { - $this->db->commit(); - return 1; - } - } - - // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps - /** - * Update last_status into database - * - * @param string $last_status Status to write - * @param User $user User that modifies - * @param int $notrigger 0=launch triggers after, 1=disable triggers - * @return int <0 if KO, >0 if OK - */ - public function update_status($last_status, $user = 0, $notrigger = 0) - { - // phpcs:enable - $error = 0; - - $this->last_status = $last_status; - $this->checkParameters(); - - // Update request - $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET"; - $sql .= " last_status = ".(isset($this->last_status) ? "'".$this->db->escape($this->last_status)."'" : "''"); - $sql .= " WHERE rowid = ".$this->id; - - $this->db->begin(); - - dol_syslog(__METHOD__); - $resql = $this->db->query($sql); - if (!$resql) { $error++; $this->errors[] = "Error ".$this->db->lasterror(); } - - // Commit or rollback - if ($error) - { - foreach ($this->errors as $errmsg) - { - dol_syslog(__METHOD__." ".$errmsg, LOG_ERR); - $this->error .= ($this->error ? ', '.$errmsg : $errmsg); - } - $this->db->rollback(); - return -1 * $error; - } else { - $this->db->commit(); - return 1; - } - } + /** + * @var DoliDB Database handler. + */ + public $db; + + /** + * @var string Error code (or message) + */ + public $error = ''; + + /** + * @var string[] Error codes (or messages) + */ + public $errors = array(); + + public $types = array(0, 1); //!< Updater types + public $update_min = 5; //!< Minimal update rate + + /** + * @var int ID + */ + public $id; + + public $type; + + /** + * @var string description + */ + public $description; + + public $parameters; + + /** + * @var int ID + */ + public $fk_variable; + + public $update_interval; //!< Interval in mins + public $next_update; //!< Next update timestamp + public $last_status; + + /** + * @var string Name of table without prefix where object is stored + */ + public $table_element = "c_price_global_variable_updater"; + + /** + * Constructor + * + * @param DoliDb $db Database handler + */ + public function __construct($db) + { + $this->db = $db; + } + + + /** + * Create object into database + * + * @param User $user User that creates + * @param int $notrigger 0=launch triggers after, 1=disable triggers + * @return int <0 if KO, Id of created object if OK + */ + public function create($user, $notrigger = 0) + { + $error = 0; + + $this->checkParameters(); + + // Insert request + $sql = "INSERT INTO ".MAIN_DB_PREFIX.$this->table_element." ("; + $sql .= "type, description, parameters, fk_variable, update_interval, next_update, last_status"; + $sql .= ") VALUES ("; + $sql .= " ".$this->type.","; + $sql .= " ".(isset($this->description) ? "'".$this->db->escape($this->description)."'" : "''").","; + $sql .= " ".(isset($this->parameters) ? "'".$this->db->escape($this->parameters)."'" : "''").","; + $sql .= " ".$this->fk_variable.","; + $sql .= " ".$this->update_interval.","; + $sql .= " ".$this->next_update.","; + $sql .= " ".(isset($this->last_status) ? "'".$this->db->escape($this->last_status)."'" : "''"); + $sql .= ")"; + + $this->db->begin(); + + dol_syslog(__METHOD__, LOG_DEBUG); + $resql = $this->db->query($sql); + if (!$resql) { $error++; $this->errors[] = "Error ".$this->db->lasterror(); } + + if (!$error) + { + $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element); + + if (!$notrigger) + { + // Uncomment this and change MYOBJECT to your own tag if you + // want this action calls a trigger. + + //// Call triggers + //$result=$this->call_trigger('MYOBJECT_CREATE',$user); + //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail} + //// End call triggers + } + } + + // Commit or rollback + if ($error) + { + foreach ($this->errors as $errmsg) + { + dol_syslog(__METHOD__." ".$errmsg, LOG_ERR); + $this->error .= ($this->error ? ', '.$errmsg : $errmsg); + } + $this->db->rollback(); + return -1 * $error; + } + else + { + $this->db->commit(); + return $this->id; + } + } + + + /** + * Load object in memory from the database + * + * @param int $id Id object + * @return int < 0 if KO, 0 if OK but not found, > 0 if OK + */ + public function fetch($id) + { + $sql = "SELECT type, description, parameters, fk_variable, update_interval, next_update, last_status"; + $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element; + $sql .= " WHERE rowid = ".$id; + + dol_syslog(__METHOD__); + $resql = $this->db->query($sql); + if ($resql) + { + $obj = $this->db->fetch_object($resql); + if ($obj) + { + $this->id = $id; + $this->type = $obj->type; + $this->description = $obj->description; + $this->parameters = $obj->parameters; + $this->fk_variable = $obj->fk_variable; + $this->update_interval = $obj->update_interval; + $this->next_update = $obj->next_update; + $this->last_status = $obj->last_status; + $this->checkParameters(); + return 1; + } + else + { + return 0; + } + } + else + { + $this->error = "Error ".$this->db->lasterror(); + return -1; + } + } + + /** + * Update object into database + * + * @param User $user User that modifies + * @param int $notrigger 0=launch triggers after, 1=disable triggers + * @return int <0 if KO, >0 if OK + */ + public function update($user = 0, $notrigger = 0) + { + $error = 0; + + $this->checkParameters(); + + // Update request + $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET"; + $sql .= " type = ".$this->type.","; + $sql .= " description = ".(isset($this->description) ? "'".$this->db->escape($this->description)."'" : "''").","; + $sql .= " parameters = ".(isset($this->parameters) ? "'".$this->db->escape($this->parameters)."'" : "''").","; + $sql .= " fk_variable = ".$this->fk_variable.","; + $sql .= " update_interval = ".$this->update_interval.","; + $sql .= " next_update = ".$this->next_update.","; + $sql .= " last_status = ".(isset($this->last_status) ? "'".$this->db->escape($this->last_status)."'" : "''"); + $sql .= " WHERE rowid = ".$this->id; + + $this->db->begin(); + + dol_syslog(__METHOD__); + $resql = $this->db->query($sql); + if (!$resql) { $error++; $this->errors[] = "Error ".$this->db->lasterror(); } + + // if (! $error) + // { + // if (! $notrigger) + // { + // // Uncomment this and change MYOBJECT to your own tag if you + // // want this action calls a trigger. + + // //// Call triggers + // //$result=$this->call_trigger('MYOBJECT_MODIFY',$user); + // //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail} + // //// End call triggers + // } + // } + + // Commit or rollback + if ($error) + { + foreach ($this->errors as $errmsg) + { + dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR); + $this->error .= ($this->error ? ', '.$errmsg : $errmsg); + } + $this->db->rollback(); + return -1 * $error; + } + else + { + $this->db->commit(); + return 1; + } + } + + /** + * Delete object in database + * + * @param int $rowid Row id of global variable + * @param User $user User that deletes + * @param int $notrigger 0=launch triggers after, 1=disable triggers + * @return int <0 if KO, >0 if OK + */ + public function delete($rowid, $user, $notrigger = 0) + { + $error = 0; + + $this->db->begin(); + + //if (! $error) + //{ + // if (! $notrigger) + // { + // Uncomment this and change MYOBJECT to your own tag if you + // want this action calls a trigger. + + //// Call triggers + //$result=$this->call_trigger('MYOBJECT_DELETE',$user); + //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail} + //// End call triggers + // } + //} + + if (!$error) + { + $sql = "DELETE FROM ".MAIN_DB_PREFIX.$this->table_element; + $sql .= " WHERE rowid = ".$rowid; + + dol_syslog(__METHOD__); + $resql = $this->db->query($sql); + if (!$resql) { $error++; $this->errors[] = "Error ".$this->db->lasterror(); } + } + + // Commit or rollback + if ($error) + { + foreach ($this->errors as $errmsg) + { + dol_syslog(__METHOD__." ".$errmsg, LOG_ERR); + $this->error .= ($this->error ? ', '.$errmsg : $errmsg); + } + $this->db->rollback(); + return -1 * $error; + } + else + { + $this->db->commit(); + return 1; + } + } + + /** + * Initialise object with example values + * Id must be 0 if object instance is a specimen + * + * @return void + */ + public function initAsSpecimen() + { + $this->id = 0; + $this->type = 0; + $this->description = ''; + $this->parameters = ''; + $this->fk_variable = 0; + $this->update_interval = 0; + $this->next_update = 0; + $this->last_status = ''; + } + + /** + * Returns the last updated time in string html format, returns "never" if its less than 1 + * + * @return string + */ + public function getLastUpdated() + { + global $langs; + $last = $this->next_update - ($this->update_interval * 60); + if ($last < 1) { + return $langs->trans("Never"); + } + $status = empty($this->last_status) ? $langs->trans("CorrectlyUpdated") : $this->last_status; + return $status.'
'.dol_print_date($last, '%d/%m/%Y %H:%M:%S'); + } + + /** + * Checks if all parameters are in order + * + * @return void + */ + public function checkParameters() + { + // Clean parameters + if (isset($this->description)) $this->description = trim($this->description); + if (isset($this->parameters)) $this->parameters = trim($this->parameters); + else $this->parameters = ""; + if (isset($this->last_status)) $this->last_status = trim($this->last_status); + + // Check parameters + if (empty($this->type) || !is_numeric($this->type) || !in_array($this->type, $this->types)) $this->type = 0; + if (empty($this->update_interval) || !is_numeric($this->update_interval) || $this->update_interval < 1) $this->update_interval = $this->update_min; + if (empty($this->next_update) || !is_numeric($this->next_update) || $this->next_update < 0) $this->next_update = 0; + } + + /** + * List all price global variables + * + * @return array Array of price global variable updaters + */ + public function listUpdaters() + { + $sql = "SELECT rowid, type, description, parameters, fk_variable, update_interval, next_update, last_status"; + $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element; + + dol_syslog(__METHOD__, LOG_DEBUG); + $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; + } + } + + /** + * List all updaters which need to be processed + * + * @return array Array of price global variable updaters + */ + public 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(__METHOD__, LOG_DEBUG); + $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; + } + } + + /** + * Handles the processing of this updater + * + * @return int <0 if KO, 0 if OK but no global variable found, >0 if OK + */ + public function process() + { + global $langs, $user; + $langs->load("errors"); + dol_syslog(__METHOD__, LOG_DEBUG); + + $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) { + // Call JSON request + include_once DOL_DOCUMENT_ROOT.'/core/lib/geturl.lib.php'; + $tmpresult = getURLContent($url); + $code = $tmpresult['http_code']; + $result = $tmpresult['content']; + + if (!isset($result)) { + $this->error = $langs->trans("ErrorGlobalVariableUpdater0", "empty response"); + return -1; + } + if ($code !== 200) { + $this->error = $langs->trans("ErrorGlobalVariableUpdater0", $code.' '.$tmpresult['curl_error_msg']); + 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; + } + + // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps + /** + * Update next_update into database + * + * @param string $next_update Next update to write + * @param User $user User that modifies + * @param int $notrigger 0=launch triggers after, 1=disable triggers + * @return int <0 if KO, >0 if OK + */ + public function update_next_update($next_update, $user = 0, $notrigger = 0) + { + // phpcs:enable + $error = 0; + + $this->next_update = $next_update; + $this->checkParameters(); + + // Update request + $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET"; + $sql .= " next_update = ".$this->next_update; + $sql .= " WHERE rowid = ".$this->id; + + $this->db->begin(); + + dol_syslog(__METHOD__); + $resql = $this->db->query($sql); + if (!$resql) { $error++; $this->errors[] = "Error ".$this->db->lasterror(); } + + // Commit or rollback + if ($error) + { + foreach ($this->errors as $errmsg) + { + dol_syslog(__METHOD__." ".$errmsg, LOG_ERR); + $this->error .= ($this->error ? ', '.$errmsg : $errmsg); + } + $this->db->rollback(); + return -1 * $error; + } + else + { + $this->db->commit(); + return 1; + } + } + + // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps + /** + * Update last_status into database + * + * @param string $last_status Status to write + * @param User $user User that modifies + * @param int $notrigger 0=launch triggers after, 1=disable triggers + * @return int <0 if KO, >0 if OK + */ + public function update_status($last_status, $user = 0, $notrigger = 0) + { + // phpcs:enable + $error = 0; + + $this->last_status = $last_status; + $this->checkParameters(); + + // Update request + $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET"; + $sql .= " last_status = ".(isset($this->last_status) ? "'".$this->db->escape($this->last_status)."'" : "''"); + $sql .= " WHERE rowid = ".$this->id; + + $this->db->begin(); + + dol_syslog(__METHOD__); + $resql = $this->db->query($sql); + if (!$resql) { $error++; $this->errors[] = "Error ".$this->db->lasterror(); } + + // Commit or rollback + if ($error) + { + foreach ($this->errors as $errmsg) + { + dol_syslog(__METHOD__." ".$errmsg, LOG_ERR); + $this->error .= ($this->error ? ', '.$errmsg : $errmsg); + } + $this->db->rollback(); + return -1 * $error; + } + else + { + $this->db->commit(); + return 1; + } + } } --- /tmp/dsg/dolibarr/htdocs/product/dynamic_price/class/github_price_parser.class.php +++ /tmp/dsg/dolibarr/htdocs/product/dynamic_price/class/client_price_parser.class.php @@ -32,38 +32,38 @@ */ class PriceParser { - protected $db; - // Limit of expressions per price - public $limit = 100; - // The error that occurred when parsing price - public $error_parser; - // The expression that caused the error - public $error_expr; - //The special char - public $special_chr = "#"; - //The separator char - public $separator_chr = ";"; - - /** - * Constructor - * - * @param DoliDB $db Database handler - */ - public function __construct($db) - { - $this->db = $db; - } - - /** - * Returns translated error - * - * @return string Translated error - */ - public function translatedError() - { - global $langs; - $langs->load("errors"); - /* + protected $db; + // Limit of expressions per price + public $limit = 100; + // The error that occurred when parsing price + public $error_parser; + // The expression that caused the error + public $error_expr; + //The special char + public $special_chr = "#"; + //The separator char + public $separator_chr = ";"; + + /** + * Constructor + * + * @param DoliDB $db Database handler + */ + public function __construct($db) + { + $this->db = $db; + } + + /** + * Returns translated error + * + * @return string Translated error + */ + public function translatedError() + { + global $langs; + $langs->load("errors"); + /* -No arg 9, an unexpected error occured 14, division by zero @@ -96,72 +96,76 @@ 16, internal error 18, internal error */ - if (empty($this->error_parser)) { - return $langs->trans("ErrorPriceExpressionUnknown", 0); //this is not supposed to happen - } - list($code, $info) = $this->error_parser; - if (in_array($code, array(9, 14, 19, 20))) //Errors which have 0 arg - { - return $langs->trans("ErrorPriceExpression".$code); - } elseif (in_array($code, array(1, 2, 3, 4, 5, 8, 10, 11, 17, 21, 22))) //Errors which have 1 arg - { - return $langs->trans("ErrorPriceExpression".$code, $info); - } elseif (in_array($code, array(6, 23))) //Errors which have 2 args - { - return $langs->trans("ErrorPriceExpression".$code, $info[0], $info[1]); - } elseif (in_array($code, array(7, 12, 13, 15, 16, 18))) //Internal errors - { - return $langs->trans("ErrorPriceExpressionInternal", $code); - } else //Unknown errors - { - return $langs->trans("ErrorPriceExpressionUnknown", $code); - } - } - - /** - * 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; - global $hookmanager; - $action = 'PARSEEXPRESSION'; - if ($result = $hookmanager->executeHooks('doDynamiPrice', array( + if (empty($this->error_parser)) { + return $langs->trans("ErrorPriceExpressionUnknown", 0); //this is not supposed to happen + } + list($code, $info) = $this->error_parser; + if (in_array($code, array(9, 14, 19, 20))) //Errors which have 0 arg + { + return $langs->trans("ErrorPriceExpression".$code); + } + elseif (in_array($code, array(1, 2, 3, 4, 5, 8, 10, 11, 17, 21, 22))) //Errors which have 1 arg + { + return $langs->trans("ErrorPriceExpression".$code, $info); + } + elseif (in_array($code, array(6, 23))) //Errors which have 2 args + { + return $langs->trans("ErrorPriceExpression".$code, $info[0], $info[1]); + } + elseif (in_array($code, array(7, 12, 13, 15, 16, 18))) //Internal errors + { + return $langs->trans("ErrorPriceExpressionInternal", $code); + } + else //Unknown errors + { + return $langs->trans("ErrorPriceExpressionUnknown", $code); + } + } + + /** + * 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; + global $hookmanager; + $action = 'PARSEEXPRESSION'; + if ($result = $hookmanager->executeHooks('doDynamiPrice', array( 'expression' =>$expression, 'product' => $product, 'values' => $values - ), $this, $action)) { + ), $this, $action)) { return $result; - } - //Check if empty - $expression = trim($expression); - if (empty($expression)) - { - $this->error_parser = array(20, null); - return -2; - } - - //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); - $extrafields->fetch_name_optionals_label('product', true); - $product->fetch_optionals(); - if (is_array($extrafields->attributes[$product->table_element]['label'])) + } + //Check if empty + $expression = trim($expression); + if (empty($expression)) + { + $this->error_parser = array(20, null); + return -2; + } + + //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); + $extrafields->fetch_name_optionals_label('product', true); + $product->fetch_optionals(); + if (is_array($extrafields->attributes[$product->table_element]['label'])) { foreach ($extrafields->attributes[$product->table_element]['label'] as $key=>$label) { @@ -169,102 +173,102 @@ } } - //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; - } - - //Remove internal variables - unset($values["supplier_id"]); - - //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; - - //Fill each variable in expression from values - $expression = str_replace("\n", $this->separator_chr, $expression); - foreach ($values as $key => $value) - { - if ($value === null && strpos($expression, $key) !== false) { - $this->error_parser = array(24, $key); - return -7; - } - $expression = str_replace($this->special_chr.$key.$this->special_chr, strval($value), $expression); - } - - //Check if there is unfilled variable - if (strpos($expression, $this->special_chr) !== false) - { - $data = explode($this->special_chr, $expression); - $variable = $this->special_chr.$data[1]; - if (isset($data[2])) $variable .= $this->special_chr; - $this->error_parser = array(23, array($variable, $expression)); - return -6; - } - - //Iterate over each expression splitted by $separator_chr - $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_parser = $em->last_error_code; - if ($this->error_parser !== null) { //$em->last_error_code is null if no error happened, so just check if error_parser 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_parser = array(21, $expression); - return -4; - } - if ($vars["price"] < 0) - { - $this->error_parser = array(22, $expression); - return -5; - } - return $vars["price"]; - } - - /** - * Calculates product price based on product id and associated expression - * - * @param Product $product The Product object to get information - * @param array $extra_values Any aditional values for expression - * @return int > 0 if OK, < 1 if KO - */ - public function parseProduct($product, $extra_values = array()) - { - //Get the expression from db - $price_expression = new PriceExpression($this->db); - $res = $price_expression->fetch($product->fk_price_expression); - if ($res < 1) { - $this->error_parser = array(19, null); - return -1; - } + //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; + } + + //Remove internal variables + unset($values["supplier_id"]); + + //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; + + //Fill each variable in expression from values + $expression = str_replace("\n", $this->separator_chr, $expression); + foreach ($values as $key => $value) + { + if ($value === null && strpos($expression, $key) !== false) { + $this->error_parser = array(24, $key); + return -7; + } + $expression = str_replace($this->special_chr.$key.$this->special_chr, strval($value), $expression); + } + + //Check if there is unfilled variable + if (strpos($expression, $this->special_chr) !== false) + { + $data = explode($this->special_chr, $expression); + $variable = $this->special_chr.$data[1]; + if (isset($data[2])) $variable .= $this->special_chr; + $this->error_parser = array(23, array($variable, $expression)); + return -6; + } + + //Iterate over each expression splitted by $separator_chr + $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_parser = $em->last_error_code; + if ($this->error_parser !== null) { //$em->last_error_code is null if no error happened, so just check if error_parser 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_parser = array(21, $expression); + return -4; + } + if ($vars["price"] < 0) + { + $this->error_parser = array(22, $expression); + return -5; + } + return $vars["price"]; + } + + /** + * Calculates product price based on product id and associated expression + * + * @param Product $product The Product object to get information + * @param array $extra_values Any aditional values for expression + * @return int > 0 if OK, < 1 if KO + */ + public function parseProduct($product, $extra_values = array()) + { + //Get the expression from db + $price_expression = new PriceExpression($this->db); + $res = $price_expression->fetch($product->fk_price_expression); + if ($res < 1) { + $this->error_parser = array(19, null); + return -1; + } //Get the supplier min price $productFournisseur = new ProductFournisseur($this->db); @@ -272,13 +276,13 @@ if ($res < 0) { $this->error_parser = array(25, null); return -1; - } elseif ($res == 0) { + } elseif ($res == 0) { $supplier_min_price = 0; } else { $supplier_min_price = $productFournisseur->fourn_unitprice; } - //Accessible values by expressions + //Accessible values by expressions $extra_values = array_merge($extra_values, array( "supplier_min_price" => $supplier_min_price, )); @@ -293,61 +297,61 @@ return $result; } - /** - * Calculates supplier product price based on product supplier price and associated expression - * - * @param ProductFournisseur $product_supplier The Product supplier object to get information - * @param array $extra_values Any aditional values for expression - * @return int > 0 if OK, < 1 if KO - */ - public function parseProductSupplier($product_supplier, $extra_values = array()) - { - //Get the expression from db - $price_expression = new PriceExpression($this->db); - $res = $price_expression->fetch($product_supplier->fk_supplier_price_expression); - if ($res < 1) - { - $this->error_parser = array(19, null); - return -1; - } - - //Get the product data (use ignore_expression to avoid possible recursion) - $product_supplier->fetch($product_supplier->id, '', '', '', 1); - - //Accessible values by expressions - $extra_values = array_merge($extra_values, array( - "supplier_quantity" => $product_supplier->fourn_qty, - "supplier_tva_tx" => $product_supplier->fourn_tva_tx, - )); - - //Parse the expression and return the price - return $this->parseExpression($product_supplier, $price_expression->expression, $extra_values); - } - - /** - * Tests string expression for validity - * - * @param int $product_id The Product id to get information - * @param string $expression The expression to parse - * @param array $extra_values Any aditional values for expression - * @return int > 0 if OK, < 1 if KO - */ - public function testExpression($product_id, $expression, $extra_values = array()) - { - //Get the product data - $product = new Product($this->db); - $product->fetch($product_id, '', '', 1); - - //Values for product expressions - $extra_values = array_merge($extra_values, array( - "supplier_min_price" => 1, - )); - - //Values for supplier product expressions - $extra_values = array_merge($extra_values, array( - "supplier_quantity" => 2, - "supplier_tva_tx" => 3, - )); - return $this->parseExpression($product, $expression, $extra_values); - } + /** + * Calculates supplier product price based on product supplier price and associated expression + * + * @param ProductFournisseur $product_supplier The Product supplier object to get information + * @param array $extra_values Any aditional values for expression + * @return int > 0 if OK, < 1 if KO + */ + public function parseProductSupplier($product_supplier, $extra_values = array()) + { + //Get the expression from db + $price_expression = new PriceExpression($this->db); + $res = $price_expression->fetch($product_supplier->fk_supplier_price_expression); + if ($res < 1) + { + $this->error_parser = array(19, null); + return -1; + } + + //Get the product data (use ignore_expression to avoid possible recursion) + $product_supplier->fetch($product_supplier->id, '', '', '', 1); + + //Accessible values by expressions + $extra_values = array_merge($extra_values, array( + "supplier_quantity" => $product_supplier->fourn_qty, + "supplier_tva_tx" => $product_supplier->fourn_tva_tx, + )); + + //Parse the expression and return the price + return $this->parseExpression($product_supplier, $price_expression->expression, $extra_values); + } + + /** + * Tests string expression for validity + * + * @param int $product_id The Product id to get information + * @param string $expression The expression to parse + * @param array $extra_values Any aditional values for expression + * @return int > 0 if OK, < 1 if KO + */ + public function testExpression($product_id, $expression, $extra_values = array()) + { + //Get the product data + $product = new Product($this->db); + $product->fetch($product_id, '', '', 1); + + //Values for product expressions + $extra_values = array_merge($extra_values, array( + "supplier_min_price" => 1, + )); + + //Values for supplier product expressions + $extra_values = array_merge($extra_values, array( + "supplier_quantity" => 2, + "supplier_tva_tx" => 3, + )); + return $this->parseExpression($product, $expression, $extra_values); + } }