Qual: Mise en transaction de la création 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

@ -333,76 +333,80 @@ class Propal
} }
} }
/**
*
*
*
*/
/**
* \brief Crée une propal
* \return int <0 si ko, >=0 si ok
*/
function create() function create()
{ {
/* global $langs,$conf;
* Insertion dans la base
*/ // Définition paramètres
$this->fin_validite = $this->datep + ($this->duree_validite * 24 * 3600); $this->fin_validite = $this->datep + ($this->duree_validite * 24 * 3600);
$this->db->begin();
// 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 = "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 .= " 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).")"; $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).")";
$sqlok = 0;
if ( $this->db->query($sql) ) $resql=$this->db->query($sql);
if ($resql)
{ {
$this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."propal"); $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."propal");
$sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."propal WHERE ref='$this->ref';"; if ($this->id)
if ( $this->db->query($sql) )
{ {
/* /*
* Insertion du detail des produits dans la base * 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++) for ($i = 0 ; $i < sizeof($this->products) ; $i++)
{ {
$prod = new Product($this->db, $this->products[$i]); $prod = new Product($this->db, $this->products[$i]);
$prod->fetch($this->products[$i]); $result=$prod->fetch($this->products[$i]);
$this->insert_product($this->products[$i], $result=$this->insert_product($this->products[$i],
$this->products_qty[$i], $this->products_qty[$i],
$this->products_remise_percent[$i]); $this->products_remise_percent[$i]);
} }
/* // Affectation au projet
*
*/
$this->update_price();
/*
* Affectation au projet
*/
if ($this->projetidp) if ($this->projetidp)
{ {
$sql = "UPDATE ".MAIN_DB_PREFIX."propal SET fk_projet=$this->projetidp WHERE ref='$this->ref';"; $sql = "UPDATE ".MAIN_DB_PREFIX."propal SET fk_projet=$this->projetidp WHERE ref='$this->ref'";
$this->db->query($sql); $result=$this->db->query($sql);
}
} }
$resql=$this->update_price();
if ($resql)
{
// Appel des triggers
include_once(DOL_DOCUMENT_ROOT . "/interfaces.class.php");
$interface=new Interfaces($this->db);
$result=$interface->run_triggers('PROPAL_CREATE',$this,$user,$langs,$conf);
// Fin appel triggers
$this->db->commit();
return $this->id;
} }
else else
{ {
dolibarr_syslog("Propal::Create -2"); $this->error=$this->db->error();
dolibarr_syslog("Propal::Create -2 ".$this->error);
$this->db->rollback();
return -2; return -2;
} }
} }
}
else else
{ {
dolibarr_syslog("Propal::Create -1 $sql"); $this->error=$this->db->error();
dolibarr_syslog("Propal::Create -1 ".$this->error);
$this->db->rollback();
return -1; return -1;
} }
return $this->id; return $this->id;
} }
@ -610,25 +614,38 @@ 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)
{ {
global $conf,$langs;
if ($user->rights->propale->valider) if ($user->rights->propale->valider)
{ {
$this->db->begin();
$sql = "UPDATE ".MAIN_DB_PREFIX."propal SET fk_statut = 1, date_valid=now(), fk_user_valid=$user->id"; $sql = "UPDATE ".MAIN_DB_PREFIX."propal";
$sql .= " WHERE rowid = $this->id AND fk_statut = 0 ;"; $sql.= " SET fk_statut = 1, date_valid=now(), fk_user_valid=".$user->id;
$sql.= " WHERE rowid = ".$this->id." AND fk_statut = 0";
if ($this->db->query($sql) ) if ($this->db->query($sql))
{ {
// 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; return 1;
} }
else else
{ {
$this->db->rollback();
return -1; return -1;
} }
} }