From 2e0a010dafdf325b394be1393149600f27cbbf19 Mon Sep 17 00:00:00 2001
From: Laurent Destailleur <eldy@users.sourceforge.net>
Date: Sun, 20 Aug 2006 04:13:46 +0000
Subject: [PATCH] =?UTF-8?q?Fix:=20Protection=20pour=20qu'une=20remise=20so?=
 =?UTF-8?q?it=20appliqu=E9e=20qu'une=20fois.=20Si=20on=20a=202=20brouillon?=
 =?UTF-8?q?s=20sur=20la=20meme=20remise,=20le=20deuxieme=20qui=20valide=20?=
 =?UTF-8?q?a=20une=20erreur=20de=20remise=20non=20disponible?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 htdocs/compta/facture.php |  6 +++-
 htdocs/facture.class.php  | 60 ++++++++++++++++++++++++++++++---------
 2 files changed, 51 insertions(+), 15 deletions(-)

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='<div class="error">'.$fac->error.'</div>';
+	}
 }
 
 // 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;
             }
-- 
GitLab