diff --git a/htdocs/compta/facture.php b/htdocs/compta/facture.php
index 78c9c4bf065..f725d90734b 100644
--- a/htdocs/compta/facture.php
+++ b/htdocs/compta/facture.php
@@ -150,7 +150,7 @@ if ($_POST['action'] == 'confirm_valid' && $_POST['confirm'] == 'yes' && $user->
$soc = new Societe($db);
$soc->fetch($fac->socidp);
$result = $fac->set_valid($fac->id, $user, $soc);
- if ($result)
+ if ($result >= 0)
{
if ($_REQUEST['lang_id'])
{
@@ -159,6 +159,10 @@ if ($_POST['action'] == 'confirm_valid' && $_POST['confirm'] == 'yes' && $user->
}
facture_pdf_create($db, $fac->id, '', $fac->modelpdf, $outputlangs);
}
+ else
+ {
+ $msg='
'.$fac->error.'
';
+ }
}
// Repasse la facture en mode brouillon
diff --git a/htdocs/facture.class.php b/htdocs/facture.class.php
index 6e3f18cb862..cd0e3501973 100644
--- a/htdocs/facture.class.php
+++ b/htdocs/facture.class.php
@@ -887,7 +887,7 @@ class Facture extends CommonObject
{
dolibarr_syslog("Facture::set_valid() renommage rep ".$dirsource." en ".$dirdest);
- if (rename($dirsource, $dirdest))
+ if (@rename($dirsource, $dirdest))
{
dolibarr_syslog("Renommage ok");
// Suppression ancien fichier PDF dans nouveau rep
@@ -904,25 +904,59 @@ class Facture extends CommonObject
{
if (($this->lignes[$i]->info_bits & 2) == 2)
{
- // Ligne de remis
- dolibarr_syslog("Facture.class::set_valid top remises de ligne ".$this->lignes[$i]->fk_remise_except." comme utilisee");
+ // Ligne de remise
+ dolibarr_syslog("Facture.class::set_valid: recherche si remise ".$this->lignes[$i]->fk_remise_except." toujours dispo");
- // On met à jour ligne de remise comme utilisée
- $sql = 'UPDATE '.MAIN_DB_PREFIX.'societe_remise_except';
- $sql.= ' SET fk_facture = '.$this->lignes[$i]->rowid;
- $sql.= ' WHERE rowid ='.$this->lignes[$i]->fk_remise_except;
+ // On recherche si ligne de remise pas deja attribuée
+ $sql = 'SELECT fk_facture';
+ $sql.= ' FROM '.MAIN_DB_PREFIX.'societe_remise_except';
+ $sql.= ' WHERE fk_facture IS NULL AND rowid ='.$this->lignes[$i]->fk_remise_except;
+ $sql.= ' FOR UPDATE';
$resql=$this->db->query($sql);
- if (! $resql)
+ if ($resql)
+ {
+ $num=$this->db->num_rows($resql);
+ if ($num >= 1)
+ {
+ dolibarr_syslog("Facture.class::set_valid: top ligne de remise ".$this->lignes[$i]->fk_remise_except." pour ligne de facture ".$this->lignes[$i]->rowid);
+
+ // On met à jour ligne de remise comme utilisée
+ $sql = 'UPDATE '.MAIN_DB_PREFIX.'societe_remise_except';
+ $sql.= ' SET fk_facture = '.$this->lignes[$i]->rowid;
+ $sql.= ' WHERE fk_facture IS NULL AND rowid ='.$this->lignes[$i]->fk_remise_except;
+ $resql=$this->db->query($sql);
+ if ($resql)
+ {
+
+ }
+ else
+ {
+ $this->error=$this->db->error().' sql='.$sql;
+ dolibarr_syslog("Facture.class::set_valid: Error ".$this->error);
+ $error++;
+ break;
+ }
+ }
+ else
+ {
+ $error++;
+ $this->error=$langs->trans("InvoiceDiscountNotAvailable");
+ dolibarr_syslog("Facture.class::set_valid: Error ".$this->error);
+ break;
+ }
+ }
+ else
{
$this->error=$this->db->error().' sql='.$sql;
- dolibarr_syslog("Facture.class::set_valid Error ".$this->error);
+ dolibarr_syslog("Facture.class::set_valid: Error ".$this->error);
$error++;
+ break;
}
}
}
// On vérifie si la facture était une provisoire
- if ($facref == 'PROV')
+ if (! $error && $facref == 'PROV')
{
/*
* Pour chaque produit, on met a jour indicateur nbvente
@@ -948,12 +982,12 @@ class Facture extends CommonObject
else
{
$error++;
+ $this->error=$this->db->error().' sql='.$sql;
}
}
- if ($error == 0)
+ if (! $error)
{
-
// Classe la société rattachée comme client
$soc=new Societe($this->db);
$soc->id = $this->socidp;
@@ -970,12 +1004,10 @@ class Facture extends CommonObject
// Fin appel triggers
$this->db->commit();
-
return 1;
}
else
{
- $this->error=$this->db->error();
$this->db->rollback();
return -1;
}