Qual: Fiabilisation de la création d'un produit au sein d'une transaction.

This commit is contained in:
Laurent Destailleur 2005-08-13 22:13:50 +00:00
parent aec4deb681
commit 956339acef

View File

@ -61,6 +61,7 @@ class Product
$this->db = $DB; $this->db = $DB;
$this->id = $id ; $this->id = $id ;
$this->envente = 0; $this->envente = 0;
$this->seuil_stock_alerte = 0;
} }
@ -94,23 +95,25 @@ class Product
/** /**
* \brief Insère le produit en base * \brief Insère le produit en base
* \param user utilisateur qui effectue l'insertion * \param user Utilisateur qui effectue l'insertion
*/ */
function create($user) function create($user)
{ {
$this->ref = ereg_replace("'","",stripslashes($this->ref)); $this->ref = ereg_replace("'","",stripslashes($this->ref));
$this->ref = ereg_replace("\"","",stripslashes($this->ref)); $this->ref = ereg_replace("\"","",stripslashes($this->ref));
dolibarr_syslog("Product::Create Categorie : ".$this->catid); dolibarr_syslog("Product::Create ref=".$this->ref." Categorie : ".$this->catid);
$this->db->begin();
$sql = "SELECT count(*)"; $sql = "SELECT count(*)";
$sql .= " FROM ".MAIN_DB_PREFIX."product WHERE ref = '" .trim($this->ref)."'"; $sql .= " FROM ".MAIN_DB_PREFIX."product WHERE ref = '" .trim($this->ref)."'";
$result = $this->db->query($sql) ;
$result = $this->db->query($sql) ;
if ($result) if ($result)
{ {
$row = $this->db->fetch_array(); $row = $this->db->fetch_array($result);
if ($row[0] == 0) if ($row[0] == 0)
{ {
if (strlen($this->tva_tx)==0) $this->tva_tx = 0; if (strlen($this->tva_tx)==0) $this->tva_tx = 0;
@ -131,32 +134,42 @@ class Product
{ {
$this->id = $id; $this->id = $id;
$this->_log_price($user); $this->_log_price($user);
if ( $this->update($id, $user) ) if ( $this->update($id, $user) > 0)
{ {
if ($this->catid > 0) if ($this->catid > 0)
{ {
$cat = new Categorie ($this->db, $this->catid); $cat = new Categorie ($this->db, $this->catid);
$cat->add_product($this); $cat->add_product($this);
} }
$this->db->commit();
return $id; return $id;
} }
} else {
else $this->db->rollback();
{ return -5;
return -2;
} }
} }
else else
{ {
dolibarr_print_error($this->db); $this->db->rollback();
return -1; return -4;
} }
} }
else else
{ {
$this->db->rollback();
return -3; return -3;
} }
} }
else
{
$this->db->rollback();
return -2;
}
}
$this->db->rollback();
return -1;
} }
@ -197,12 +210,12 @@ class Product
} }
else else
{ {
if ($this->db->errno() == DB_ERROR_RECORD_ALREADY_EXISTS) { if ($this->db->errno() == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
$this->mesg_error=$langs->trans("Error")." : ".$langs->trans("ErrorProductAlreadyExists",$this->ref); $this->error=$langs->trans("Error")." : ".$langs->trans("ErrorProductAlreadyExists",$this->ref);
return -1; return -1;
} }
else { else {
$this->mesg_error=$langs->trans("Error")." : ".$this->db->error(); $this->error=$langs->trans("Error")." : ".$this->db->error()." - ".$sql;
return -2; return -2;
} }
} }
@ -370,7 +383,7 @@ class Product
} }
else else
{ {
$this->mesg_error = "Prix saisi invalide."; $this->error = "Prix saisi invalide.";
return -2; return -2;
} }
} }