Fix missing transaction on add_commercial method.

This commit is contained in:
Laurent Destailleur 2019-03-15 19:34:04 +01:00
parent c72cd7e750
commit 5027a36dfb

View File

@ -1939,40 +1939,68 @@ class Societe extends CommonObject
*
* @param User $user Object user
* @param int $commid Id of user
* @return int <0 if KO, >0 if OK
* @return int <=0 if KO, >0 if OK
*/
public function add_commercial(User $user, $commid)
{
// phpcs:enable
// phpcs:enable
$error=0;
if ($this->id > 0 && $commid > 0)
{
$sql = "DELETE FROM ".MAIN_DB_PREFIX."societe_commerciaux";
$sql.= " WHERE fk_soc = ".$this->id." AND fk_user =".$commid;
$this->db->begin();
$this->db->query($sql);
if (! $error)
{
$sql = "DELETE FROM ".MAIN_DB_PREFIX."societe_commerciaux";
$sql.= " WHERE fk_soc = ".$this->id." AND fk_user =".$commid;
$sql = "INSERT INTO ".MAIN_DB_PREFIX."societe_commerciaux";
$sql.= " ( fk_soc, fk_user )";
$sql.= " VALUES (".$this->id.",".$commid.")";
$resql = $this->db->query($sql);
if (! $resql)
{
dol_syslog(get_class($this)."::add_commercial Error ".$this->db->lasterror());
$error++;
}
}
if (! $this->db->query($sql) )
if (! $error)
{
$sql = "INSERT INTO ".MAIN_DB_PREFIX."societe_commerciaux";
$sql.= " (fk_soc, fk_user)";
$sql.= " VALUES (".$this->id.", ".$commid.")";
$resql = $this->db->query($sql);
if (! $resql)
{
dol_syslog(get_class($this)."::add_commercial Error ".$this->db->lasterror());
$error++;
}
}
if (! $error)
{
dol_syslog(get_class($this)."::add_commercial Erreur");
return -2;
}
else {
$this->context=array('commercial_modified'=>$commid);
$result=$this->call_trigger('COMPANY_LINK_SALE_REPRESENTATIVE', $user);
if ($result < 0) return $result;
if ($result < 0)
{
$error++;
}
}
if (! $error)
{
$this->db->commit();
return 1;
}
else
{
$this->db->rollback();
return -1;
}
}
return -1;
return 0;
}
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps