From 74847f56de79bc09fb5c395091c10b7570e93ead Mon Sep 17 00:00:00 2001
From: Regis Houssin <regis@dolibarr.fr>
Date: Sun, 16 Jul 2006 12:26:36 +0000
Subject: [PATCH] =?UTF-8?q?Am=E9lioration=20s=E9curit=E9=20sur=20l'=E9diti?=
 =?UTF-8?q?on=20d'un=20facture=20valid=E9e?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 htdocs/compta/facture.php | 35 ++++++++--------------------
 htdocs/facture.class.php  | 48 +++++++++++++++++++++++++++------------
 2 files changed, 44 insertions(+), 39 deletions(-)

diff --git a/htdocs/compta/facture.php b/htdocs/compta/facture.php
index c776e6706ce..32a1970fc0f 100644
--- a/htdocs/compta/facture.php
+++ b/htdocs/compta/facture.php
@@ -161,27 +161,19 @@ if ($_POST['action'] == 'confirm_valid' && $_POST['confirm'] == 'yes' && $user->
 	}
 }
 
+// Repasse la facture en mode brouillon
 if ($_GET['action'] == 'modif' && $user->rights->facture->modifier && $conf->global->FACTURE_ENABLE_EDITDELETE)
 {
-  /*
-   *  Repasse la facture en mode brouillon
-   */
-  $fac = new Facture($db);
+	$fac = new Facture($db);
   $fac->fetch($_GET['facid']);
   
-  // On v�rifie si les lignes de factures ont �t� export�es en compta et/ou ventil�es
-  $ventilExportCompta = 0 ;
-  for ($i = 0 ; $i < sizeof($fac->lignes) ; $i++)
-  {
-  	if ($fac->lignes[$i]->export_compta <> 0 && $fac->lignes[$i]->code_ventilation <> 0)
-  	{
-  		$ventilExportCompta++;
-  	}
-  }
-  
-  if ($ventilExportCompta == 0)
-  {
-  	$fac->reopen($user);
+	// On v�rifie si les lignes de factures ont �t� export�es en compta et/ou ventil�es
+	$ventilExportCompta = $fac->getVentilExportCompta();
+	
+	// On v�rifie si aucun paiement n'a �t� effectu�
+	if ($resteapayer == $fac->total_ttc	&& $fac->paye == 0 && $ventilExportCompta == 0)
+	{
+		$fac->reopen($user);
   }
 }
 
@@ -2111,14 +2103,7 @@ else
 				if ($fac->statut == 1)
 				{
 					// On v�rifie si les lignes de factures ont �t� export�es en compta et/ou ventil�es
-					$ventilExportCompta = 0 ;
-					for ($i = 0 ; $i < sizeof($fac->lignes) ; $i++)
-					{
-						if ($fac->lignes[$i]->export_compta <> 0 && $fac->lignes[$i]->code_ventilation <> 0)
-						{
-							$ventilExportCompta++;
-						}
-					}
+					$ventilExportCompta = $fac->getVentilExportCompta();
 			
 					if ($conf->global->FACTURE_ENABLE_EDITDELETE && $user->rights->facture->modifier
 					&& ($resteapayer == $fac->total_ttc	&& $fac->paye == 0 && $ventilExportCompta == 0))
diff --git a/htdocs/facture.class.php b/htdocs/facture.class.php
index e96e48faa18..7523173b716 100644
--- a/htdocs/facture.class.php
+++ b/htdocs/facture.class.php
@@ -1835,13 +1835,41 @@ class Facture extends CommonObject
 
 
    /**
-	*   \brief      Renvoi si une facture peut etre supprim�e compl�tement
+	  *   \brief      Renvoi si les lignes de facture sont ventil�es et/ou export�es en compta
+    *   \param      user        Utilisateur cr�ant la demande
+    *   \return     int         <0 si ko, 0=non, 1=oui
+	  */
+	  function getVentilExportCompta()
+	  {
+	  	// On v�rifie si les lignes de factures ont �t� export�es en compta et/ou ventil�es
+			$ventilExportCompta = 0 ;
+			for ($i = 0 ; $i < sizeof($this->lignes) ; $i++)
+			{
+				if ($this->lignes[$i]->export_compta <> 0 && $this->lignes[$i]->code_ventilation <> 0)
+				{
+					$ventilExportCompta++;
+				}
+			}
+			
+			if ($ventilExportCompta <> 0)
+			{
+				return 1;
+			}
+			else
+			{
+				return 0;
+			}
+		}
+   
+   
+   /**
+	  *   \brief      Renvoi si une facture peut etre supprim�e compl�tement
     *				La r�gle est la suivante:
     *				Si facture derni�re, non provisoire, sans paiement et non export� en compta -> oui fin de r�gle
     *       Si facture brouillon et provisoire -> oui
     *   \param      user        Utilisateur cr�ant la demande
     *   \return     int         <0 si ko, 0=non, 1=oui
-	*/
+	  */
 	function is_erasable()
 	{
 		global $conf, $db;
@@ -1862,16 +1890,8 @@ class Facture extends CommonObject
 			{
 				$maxfacnumber = $db->fetch_row($resql);
 			}
-
-			// On v�rifie si les lignes de factures ont �t� export�es en compta et/ou ventil�es
-			$ventilExportCompta = 0 ;
-			for ($i = 0 ; $i < sizeof($this->lignes) ; $i++)
-			{
-				if ($this->lignes[$i]->export_compta <> 0 && $this->lignes[$i]->code_ventilation <> 0)
-				{
-					$ventilExportCompta++;
-				}
-			}
+			
+			$ventilExportCompta = $this->getVentilExportCompta();
 
 			// Si derniere facture et si non ventil�e, on peut supprimer
 			if ($maxfacnumber[0] == $this->ref && $ventilExportCompta == 0)
@@ -1889,10 +1909,10 @@ class Facture extends CommonObject
 	
 	
    /**
-	*   \brief      Cr�� une demande de pr�l�vement
+	  *   \brief      Cr�� une demande de pr�l�vement
     *   \param      user        Utilisateur cr�ant la demande
     *   \return     int         <0 si ko, >0 si ok
-	*/
+	  */
 	function demande_prelevement($user)
 	{
         dolibarr_syslog("Facture::demande_prelevement $this->statut $this->paye $this->mode_reglement_id");
-- 
GitLab