Qual: Mise en transaction de la cration de propales et leur validation. Ajout des appels aux triggers.

This commit is contained in:
Laurent Destailleur 2005-12-16 23:47:07 +00:00
parent 543b7e7ec0
commit 8dfd0b0fa0

View File

@ -332,78 +332,82 @@ class Propal
return -2; return -2;
} }
} }
/** /**
* * \brief Crée une propal
* * \return int <0 si ko, >=0 si ok
* */
*/ function create()
function create()
{ {
/* global $langs,$conf;
* Insertion dans la base
*/
$this->fin_validite = $this->datep + ($this->duree_validite * 24 * 3600);
$sql = "INSERT INTO ".MAIN_DB_PREFIX."propal (fk_soc, fk_soc_contact, price, remise, tva, total, datep, datec, ref, fk_user_author, note, model_pdf, fin_validite) "; // Définition paramètres
$sql .= " VALUES ($this->socidp, $this->contactid, 0, $this->remise, 0,0,".$this->db->idate($this->datep).", now(), '$this->ref', $this->author, '".addslashes($this->note)."','$this->modelpdf',".$this->db->idate($this->fin_validite).")"; $this->fin_validite = $this->datep + ($this->duree_validite * 24 * 3600);
$sqlok = 0;
$this->db->begin();
if ( $this->db->query($sql) )
{
$this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."propal"); // Insertion dans la base
$sql = "INSERT INTO ".MAIN_DB_PREFIX."propal (fk_soc, fk_soc_contact, price, remise, tva, total, datep, datec, ref, fk_user_author, note, model_pdf, fin_validite) ";
$sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."propal WHERE ref='$this->ref';"; $sql.= " VALUES ($this->socidp, $this->contactid, 0, $this->remise, 0,0,".$this->db->idate($this->datep).", now(), '$this->ref', $this->author, '".addslashes($this->note)."','$this->modelpdf',".$this->db->idate($this->fin_validite).")";
if ( $this->db->query($sql) )
{
/*
* Insertion du detail des produits dans la base
*/
if ( $this->db->num_rows() )
{
$propalid = $this->db->result( 0, 0);
$this->db->free();
for ($i = 0 ; $i < sizeof($this->products) ; $i++)
{
$prod = new Product($this->db, $this->products[$i]);
$prod->fetch($this->products[$i]);
$this->insert_product($this->products[$i], $resql=$this->db->query($sql);
$this->products_qty[$i], if ($resql)
$this->products_remise_percent[$i]); {
} $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."propal");
if ($this->id)
{
/*
* Insertion du detail des produits dans la base
*/
for ($i = 0 ; $i < sizeof($this->products) ; $i++)
{
$prod = new Product($this->db, $this->products[$i]);
$result=$prod->fetch($this->products[$i]);
/* $result=$this->insert_product($this->products[$i],
* $this->products_qty[$i],
*/ $this->products_remise_percent[$i]);
}
$this->update_price(); // Affectation au projet
if ($this->projetidp)
{
$sql = "UPDATE ".MAIN_DB_PREFIX."propal SET fk_projet=$this->projetidp WHERE ref='$this->ref'";
$result=$this->db->query($sql);
}
/* $resql=$this->update_price();
* Affectation au projet if ($resql)
*/ {
if ($this->projetidp) // Appel des triggers
{ include_once(DOL_DOCUMENT_ROOT . "/interfaces.class.php");
$sql = "UPDATE ".MAIN_DB_PREFIX."propal SET fk_projet=$this->projetidp WHERE ref='$this->ref';"; $interface=new Interfaces($this->db);
$this->db->query($sql); $result=$interface->run_triggers('PROPAL_CREATE',$this,$user,$langs,$conf);
} // Fin appel triggers
}
} $this->db->commit();
else return $this->id;
{ }
dolibarr_syslog("Propal::Create -2"); else
return -2; {
} $this->error=$this->db->error();
} dolibarr_syslog("Propal::Create -2 ".$this->error);
else $this->db->rollback();
{ return -2;
dolibarr_syslog("Propal::Create -1 $sql"); }
return -1; }
} }
return $this->id; else
{
$this->error=$this->db->error();
dolibarr_syslog("Propal::Create -1 ".$this->error);
$this->db->rollback();
return -1;
}
return $this->id;
} }
/** /**
@ -610,29 +614,42 @@ class Propal
} }
} }
/**
/* * \brief Passe au statut valider une propale
* * \param user Objet utilisateur qui valide
* * \return int <0 si ko, >=0 si ok
*/ */
function valid($user) function valid($user)
{ {
if ($user->rights->propale->valider) global $conf,$langs;
{
if ($user->rights->propale->valider)
$sql = "UPDATE ".MAIN_DB_PREFIX."propal SET fk_statut = 1, date_valid=now(), fk_user_valid=$user->id"; {
$sql .= " WHERE rowid = $this->id AND fk_statut = 0 ;"; $this->db->begin();
if ($this->db->query($sql) ) $sql = "UPDATE ".MAIN_DB_PREFIX."propal";
{ $sql.= " SET fk_statut = 1, date_valid=now(), fk_user_valid=".$user->id;
return 1; $sql.= " WHERE rowid = ".$this->id." AND fk_statut = 0";
}
else if ($this->db->query($sql))
{ {
return -1;
} // Appel des triggers
} include_once(DOL_DOCUMENT_ROOT . "/interfaces.class.php");
} $interface=new Interfaces($this->db);
$result=$interface->run_triggers('PROPAL_VALIDATE',$this,$user,$langs,$conf);
// Fin appel triggers
$this->db->commit();
return 1;
}
else
{
$this->db->rollback();
return -1;
}
}
}
/** /**