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;
} }
@ -92,71 +93,83 @@ 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));
dolibarr_syslog("Product::Create Categorie : ".$this->catid); $this->ref = ereg_replace("'","",stripslashes($this->ref));
$this->ref = ereg_replace("\"","",stripslashes($this->ref));
$sql = "SELECT count(*)"; dolibarr_syslog("Product::Create ref=".$this->ref." Categorie : ".$this->catid);
$sql .= " FROM ".MAIN_DB_PREFIX."product WHERE ref = '" .trim($this->ref)."'";
$result = $this->db->query($sql) ;
if ( $result ) $this->db->begin();
{
$row = $this->db->fetch_array();
if ($row[0] == 0)
{
if (strlen($this->tva_tx)==0) $this->tva_tx = 0;
if (strlen($this->price)==0) $this->price = 0;
if (strlen($this->envente)==0) $this->envente = 0;
$this->price = ereg_replace(",",".",$this->price); $sql = "SELECT count(*)";
$sql .= " FROM ".MAIN_DB_PREFIX."product WHERE ref = '" .trim($this->ref)."'";
$sql = "INSERT INTO ".MAIN_DB_PREFIX."product "; $result = $this->db->query($sql) ;
$sql .= " (datec, fk_user_author, fk_product_type, price)"; if ($result)
$sql .= " VALUES (now(),".$user->id.",$this->type, '" . $this->price . "')"; {
$result = $this->db->query($sql); $row = $this->db->fetch_array($result);
if ( $result ) if ($row[0] == 0)
{ {
$id = $this->db->last_insert_id(MAIN_DB_PREFIX."product"); if (strlen($this->tva_tx)==0) $this->tva_tx = 0;
if (strlen($this->price)==0) $this->price = 0;
if (strlen($this->envente)==0) $this->envente = 0;
if ($id > 0) $this->price = ereg_replace(",",".",$this->price);
{
$this->id = $id; $sql = "INSERT INTO ".MAIN_DB_PREFIX."product ";
$this->_log_price($user); $sql .= " (datec, fk_user_author, fk_product_type, price)";
if ( $this->update($id, $user) ) $sql .= " VALUES (now(),".$user->id.",$this->type, '" . $this->price . "')";
{ $result = $this->db->query($sql);
if ($this->catid > 0) if ( $result )
{ {
$cat = new Categorie ($this->db, $this->catid); $id = $this->db->last_insert_id(MAIN_DB_PREFIX."product");
$cat->add_product($this);
} if ($id > 0)
return $id; {
} $this->id = $id;
} $this->_log_price($user);
else if ( $this->update($id, $user) > 0)
{ {
return -2; if ($this->catid > 0)
} {
} $cat = new Categorie ($this->db, $this->catid);
else $cat->add_product($this);
{ }
dolibarr_print_error($this->db); $this->db->commit();
return -1; return $id;
} }
} else {
else $this->db->rollback();
{ return -5;
return -3; }
} }
} else
{
$this->db->rollback();
return -4;
}
}
else
{
$this->db->rollback();
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;
} }
} }