2
0
forked from Wavyzz/dolibarr

Fix: Plantage si parenthses dans cration facture fournisseur.

Qual: Mise en transaction de la cration d'un facture fournisseur
This commit is contained in:
Laurent Destailleur
2005-07-03 16:16:44 +00:00
parent 4cc925986f
commit f82f1adbf4
3 changed files with 182 additions and 134 deletions

View File

@@ -143,19 +143,29 @@ if ($_POST["action"] == 'add' && $user->rights->fournisseur->facture->creer)
if (strlen($_POST[$label]) > 0 && !empty($_POST[$amount])) if (strlen($_POST[$label]) > 0 && !empty($_POST[$amount]))
{ {
$atleastoneline=1; $atleastoneline=1;
$facfou->addline($_POST["$label"], $_POST["$amount"], $_POST["$tauxtva"], $_POST["$qty"], 1); $ret=$facfou->addline($_POST["$label"], $_POST["$amount"], $_POST["$tauxtva"], $_POST["$qty"], 1);
if ($ret < 0) $nberror++;
} }
else if (strlen($_POST[$label]) > 0 && empty($_POST[$amount])) else if (strlen($_POST[$label]) > 0 && empty($_POST[$amount]))
{ {
$ht = $_POST[$amountttc] / (1 + ($_POST[$tauxtva] / 100)); $ht = $_POST[$amountttc] / (1 + ($_POST[$tauxtva] / 100));
$atleastoneline=1; $atleastoneline=1;
$facfou->addline($_POST[$label], $ht, $_POST[$tauxtva], $_POST[$qty], 1); $ret=$facfou->addline($_POST[$label], $ht, $_POST[$tauxtva], $_POST[$qty], 1);
if ($ret < 0) $nberror++;
} }
} }
if ($nberror)
$db->commit(); {
header("Location: fiche.php?facid=$facid"); $db->rollback();
exit; $mesg='<div class="error">'.$facfou->error.'</div>';
$_GET["action"]='create';
}
else
{
$db->commit();
header("Location: fiche.php?facid=$facid");
exit;
}
} }
else else
{ {

View File

@@ -85,71 +85,78 @@ class FactureFournisseur
* \param user object utilisateur qui cr<63>e * \param user object utilisateur qui cr<63>e
* \return int id facture si ok, < 0 si erreur * \return int id facture si ok, < 0 si erreur
*/ */
function create($user) function create($user)
{ {
global $langs; global $langs;
$socid = $this->socidp; $socid = $this->socidp;
$number = $this->number; $number = sanitize_string(strtoupper($this->number));
$amount = $this->amount; $amount = $this->amount;
$remise = $this->remise; $remise = $this->remise;
if (! $remise)
{ $this->db->begin();
$remise = 0 ;
}
if (! $remise) $remise = 0 ;
$totalht = ($amount - $remise); $totalht = ($amount - $remise);
$tva = tva($totalht); $tva = tva($totalht);
$total = $totalht + $tva; $total = $totalht + $tva;
$sql = "INSERT INTO ".MAIN_DB_PREFIX."facture_fourn (facnumber, libelle, fk_soc, datec, datef, note, fk_user_author) "; $sql = "INSERT INTO ".MAIN_DB_PREFIX."facture_fourn (facnumber, libelle, fk_soc, datec, datef, note, fk_user_author) ";
$sql .= " VALUES ('".$this->number."','".$this->libelle."',"; $sql .= " VALUES ('".$number."','".addslashes($this->libelle)."',";
$sql .= $this->socid.", now(),".$this->db->idate($this->date).",'".$this->note."', ".$user->id.");"; $sql .= $this->socid.", now(),".$this->db->idate($this->date).",'".addslashes($this->note)."', ".$user->id.");";
if ( $this->db->query($sql) ) $resql=$this->db->query($sql);
{ if ($resql)
$this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."facture_fourn");
for ($i = 0 ; $i < sizeof($this->lignes) ; $i++)
{ {
$this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."facture_fourn");
$sql = "INSERT INTO ".MAIN_DB_PREFIX."facture_fourn_det (fk_facture_fourn)";
$sql .= " VALUES ($this->id);"; for ($i = 0 ; $i < sizeof($this->lignes) ; $i++)
if ($this->db->query($sql) )
{ {
$idligne = $this->db->last_insert_id(MAIN_DB_PREFIX."facture_fourn_det");
$sql = "INSERT INTO ".MAIN_DB_PREFIX."facture_fourn_det (fk_facture_fourn)";
$this->updateline($idligne, $sql .= " VALUES ($this->id);";
$this->lignes[$i][0], if ($this->db->query($sql))
$this->lignes[$i][1], {
$this->lignes[$i][2], $idligne = $this->db->last_insert_id(MAIN_DB_PREFIX."facture_fourn_det");
$this->lignes[$i][3]);
$this->updateline($idligne,
$this->lignes[$i][0],
$this->lignes[$i][1],
$this->lignes[$i][2],
$this->lignes[$i][3]);
}
} }
}
// Mise <20> jour prix
/* if ($this->updateprice($this->id) > 0)
* Mise <20> jour prix {
*/ $this->db->commit();
return $this->id;
$this->updateprice($this->id); }
else {
return $this->id; $this->error=$langs->trans("FailedToUpdatePrice");
} $this->db->rollback();
else return -3;
{ }
if ($this->db->errno() == DB_ERROR_RECORD_ALREADY_EXISTS)
{
$this->error=$langs->trans("ErrorBillRefAlreadyExists");
return -1;
} }
else else
{ {
dolibarr_print_error($this->db); if ($this->db->errno() == DB_ERROR_RECORD_ALREADY_EXISTS)
return -2; {
$this->error=$langs->trans("ErrorBillRefAlreadyExists");
$this->db->rollback();
return -1;
}
else
{
$this->error=$this->db->error();
$this->db->rollback();
return -2;
}
} }
} }
}
/** /**
* \brief Recup<75>re l'objet facture et ses lignes de factures * \brief Recup<75>re l'objet facture et ses lignes de factures
@@ -323,38 +330,41 @@ class FactureFournisseur
* \param puht prix unitaire * \param puht prix unitaire
* \param tauxtva taux tva * \param tauxtva taux tva
* \param qty quantit<69> * \param qty quantit<69>
* \return int 0 si erreur * \return int <0 si ko, >0 si ok
*/ */
function updateline($id, $label, $puht, $tauxtva, $qty=1) function updateline($id, $label, $puht, $tauxtva, $qty=1)
{ {
$puht = ereg_replace(",",".",$puht); $puht = ereg_replace(",",".",$puht);
$qty = ereg_replace(",",".",$qty); $qty = ereg_replace(",",".",$qty);
if (is_numeric($puht) && is_numeric($qty)) if (is_numeric($puht) && is_numeric($qty))
{ {
$totalht = ($puht * $qty); $totalht = ($puht * $qty);
$tva = ($totalht * $tauxtva / 100); $tva = ($totalht * $tauxtva / 100);
$totalttc = $totalht + $tva; $totalttc = $totalht + $tva;
$sql = "UPDATE ".MAIN_DB_PREFIX."facture_fourn_det "; $sql = "UPDATE ".MAIN_DB_PREFIX."facture_fourn_det ";
$sql .= "SET description ='".$label."'"; $sql .= "SET description ='".addslashes($label)."'";
$sql .= ", pu_ht = " .ereg_replace(",",".",$puht); $sql .= ", pu_ht = " .ereg_replace(",",".",$puht);
$sql .= ", qty =" .ereg_replace(",",".",$qty); $sql .= ", qty =" .ereg_replace(",",".",$qty);
$sql .= ", total_ht=" .ereg_replace(",",".",$totalht); $sql .= ", total_ht=" .ereg_replace(",",".",$totalht);
$sql .= ", tva=" .ereg_replace(",",".",$tva); $sql .= ", tva=" .ereg_replace(",",".",$tva);
$sql .= ", tva_taux=" .ereg_replace(",",".",$tauxtva); $sql .= ", tva_taux=" .ereg_replace(",",".",$tauxtva);
$sql .= ", total_ttc=".ereg_replace(",",".",$totalttc); $sql .= ", total_ttc=".ereg_replace(",",".",$totalttc);
$sql .= " WHERE rowid = ".$id;
$sql .= " WHERE rowid = ".$id;
$resql=$this->db->query($sql);
if (! $this->db->query($sql) ) if ($resql)
{ {
dolibarr_print_error($this->db); // Mise a jour prix facture
} return $this->updateprice($this->id);
}
// Mise a jour prix facture else
$this->updateprice($this->id); {
} $this->error=$this->db->error();
return -1;
}
}
} }
/** /**
@@ -378,54 +388,65 @@ class FactureFournisseur
return 1; return 1;
} }
/** /**
* \brief Mise <20> jour des sommes de la facture * \brief Mise <20> jour des sommes de la facture
* \param facid id de la facture a modifier * \param facid id de la facture a modifier
*/ * \return int <0 si ko, >0 si ok
function updateprice($facid) */
{ function updateprice($facid)
$total_ht = 0; {
$total_tva = 0; $total_ht = 0;
$total_ttc = 0; $total_tva = 0;
$total_ttc = 0;
$sql = "SELECT sum(total_ht), sum(tva), sum(total_ttc) FROM ".MAIN_DB_PREFIX."facture_fourn_det";
$sql .= " WHERE fk_facture_fourn = $facid;";
$result = $this->db->query($sql); $sql = "SELECT sum(total_ht), sum(tva), sum(total_ttc) FROM ".MAIN_DB_PREFIX."facture_fourn_det";
$sql .= " WHERE fk_facture_fourn = $facid;";
if ($result) $result = $this->db->query($sql);
{
if ($this->db->num_rows() ) if ($result)
{ {
$row = $this->db->fetch_row(); if ($this->db->num_rows() )
$total_ht = $row[0]; {
$total_tva = $row[1]; $row = $this->db->fetch_row();
$total_ttc = $row[2]; $total_ht = $row[0];
$total_tva = $row[1];
if ($total_ht == '') $total_ttc = $row[2];
$total_ht = 0;
if ($total_ht == '')
if ($total_tva == '') $total_ht = 0;
$total_tva = 0;
if ($total_tva == '')
if ($total_ttc == '') $total_tva = 0;
$total_ttc = 0;
if ($total_ttc == '')
} $total_ttc = 0;
$sql = "UPDATE ".MAIN_DB_PREFIX."facture_fourn SET"; }
$sql .= " total_ht = ". ereg_replace(",",".",$total_ht);
$sql .= ",total_tva = ".ereg_replace(",",".",$total_tva); $sql = "UPDATE ".MAIN_DB_PREFIX."facture_fourn SET";
$sql .= ",total_ttc = ".ereg_replace(",",".",$total_ttc); $sql .= " total_ht = ". ereg_replace(",",".",$total_ht);
$sql .= " WHERE rowid = $facid ;"; $sql .= ",total_tva = ".ereg_replace(",",".",$total_tva);
$sql .= ",total_ttc = ".ereg_replace(",",".",$total_ttc);
$result = $this->db->query($sql); $sql .= " WHERE rowid = $facid ;";
}
else $result = $this->db->query($sql);
{ if ($result)
dolibarr_print_error($this->db); {
} return 1;
} }
else
{
$this->error=$this->db->error();
return -2;
}
}
else
{
dolibarr_print_error($this->db);
return -1;
}
}
/** /**

View File

@@ -78,6 +78,11 @@ function check_mail ($mail)
} }
} }
/**
\brief Nettoie chaine de caract<63>re des accents
\param str Chaine a nettoyer
\return string Chaine nettoy<6F>
*/
function unaccent($str) function unaccent($str)
{ {
$acc = array("<EFBFBD>","<EFBFBD>","<EFBFBD>","<EFBFBD>","<EFBFBD>","<EFBFBD>","<EFBFBD>","<EFBFBD>","<EFBFBD>","<EFBFBD>","<EFBFBD>","'"); $acc = array("<EFBFBD>","<EFBFBD>","<EFBFBD>","<EFBFBD>","<EFBFBD>","<EFBFBD>","<EFBFBD>","<EFBFBD>","<EFBFBD>","<EFBFBD>","<EFBFBD>","'");
@@ -86,6 +91,18 @@ function unaccent($str)
return str_replace($acc, $uac, $str); return str_replace($acc, $uac, $str);
} }
/**
\brief Nettoie chaine de caract<63>re de caract<63>res sp<73>ciaux
\param str Chaine a nettoyer
\return string Chaine nettoy<6F>
*/
function sanitize_string($str)
{
$forbidden_chars=array("'","/","\\",":","*","?","\"","<",">","|","[","]",",",";","=");
return str_replace($forbidden_chars,"_",$str);
}
/** /**
\brief Envoi des messages dolibarr dans syslog ou dans un fichier \brief Envoi des messages dolibarr dans syslog ou dans un fichier
Pour syslog: facility d<>fini par SYSLOG_FACILITY Pour syslog: facility d<>fini par SYSLOG_FACILITY