From c3e245d24ea4093911fb2b9d906b1ec1522bd401 Mon Sep 17 00:00:00 2001
From: Regis Houssin <regis@dolibarr.fr>
Date: Wed, 15 Feb 2012 15:16:33 +0100
Subject: [PATCH] Fix: start triggers before delete objects

---
 htdocs/comm/propal/class/propal.class.php     | 136 ++++++++++--------
 htdocs/commande/class/commande.class.php      | 127 ++++++++--------
 htdocs/compta/facture/class/facture.class.php |  35 +++--
 3 files changed, 160 insertions(+), 138 deletions(-)

diff --git a/htdocs/comm/propal/class/propal.class.php b/htdocs/comm/propal/class/propal.class.php
index 41028cdbe37..717208b704e 100644
--- a/htdocs/comm/propal/class/propal.class.php
+++ b/htdocs/comm/propal/class/propal.class.php
@@ -1,13 +1,13 @@
 <?php
-/* Copyright (C) 2002-2004 Rodolphe Quiedeville  <rodolphe@quiedeville.org>
- * Copyright (C) 2004      Eric Seigne           <eric.seigne@ryxeo.com>
- * Copyright (C) 2004-2011 Laurent Destailleur   <eldy@users.sourceforge.net>
- * Copyright (C) 2005      Marc Barilley / Ocebo <marc@ocebo.com>
- * Copyright (C) 2005-2011 Regis Houssin         <regis@dolibarr.fr>
- * Copyright (C) 2006      Andre Cianfarani      <acianfa@free.fr>
- * Copyright (C) 2008      Raphael Bertrand (Resultic)   <raphael.bertrand@resultic.fr>
- * Copyright (C) 2010-2011 Juanjo Menent         <jmenent@2byte.es>
- * Copyright (C) 2010-2011 Philippe Grand        <philippe.grand@atoo-net.com>
+/* Copyright (C) 2002-2004 Rodolphe Quiedeville		<rodolphe@quiedeville.org>
+ * Copyright (C) 2004      Eric Seigne				<eric.seigne@ryxeo.com>
+ * Copyright (C) 2004-2011 Laurent Destailleur		<eldy@users.sourceforge.net>
+ * Copyright (C) 2005      Marc Barilley			<marc@ocebo.com>
+ * Copyright (C) 2005-2012 Regis Houssin			<regis@dolibarr.fr>
+ * Copyright (C) 2006      Andre Cianfarani			<acianfa@free.fr>
+ * Copyright (C) 2008      Raphael Bertrand			<raphael.bertrand@resultic.fr>
+ * Copyright (C) 2010-2011 Juanjo Menent			<jmenent@2byte.es>
+ * Copyright (C) 2010-2011 Philippe Grand			<philippe.grand@atoo-net.com>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -1787,75 +1787,87 @@ class Propal extends CommonObject
 		$error=0;
 
 		$this->db->begin();
-
-		$sql = "DELETE FROM ".MAIN_DB_PREFIX."propaldet WHERE fk_propal = ".$this->id;
-		if ($this->db->query($sql))
+		
+		if (! $error && ! $notrigger)
+		{
+			// Call triggers
+			include_once(DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php");
+			$interface=new Interfaces($this->db);
+			$result=$interface->run_triggers('PROPAL_DELETE',$this,$user,$langs,$conf);
+			if ($result < 0) {
+				$error++; $this->errors=$interface->errors;
+			}
+			// End call triggers
+		}
+		
+		if (! $error)
 		{
-			$sql = "DELETE FROM ".MAIN_DB_PREFIX."propal WHERE rowid = ".$this->id;
+			$sql = "DELETE FROM ".MAIN_DB_PREFIX."propaldet WHERE fk_propal = ".$this->id;
 			if ($this->db->query($sql))
 			{
-				// Delete linked object
-				$res = $this->deleteObjectLinked();
-				if ($res < 0) $error++;
-				
-				// Delete linked contacts
-				$res = $this->delete_linked_contact();
-				if ($res < 0) $error++;
-				
-				if (! $error)
+				$sql = "DELETE FROM ".MAIN_DB_PREFIX."propal WHERE rowid = ".$this->id;
+				if ($this->db->query($sql))
 				{
-					// We remove directory
-					$propalref = dol_sanitizeFileName($this->ref);
-					if ($conf->propale->dir_output)
+					// Delete linked object
+					$res = $this->deleteObjectLinked();
+					if ($res < 0) $error++;
+			
+					// Delete linked contacts
+					$res = $this->delete_linked_contact();
+					if ($res < 0) $error++;
+			
+					if (! $error)
 					{
-						$dir = $conf->propale->dir_output . "/" . $propalref ;
-						$file = $conf->propale->dir_output . "/" . $propalref . "/" . $propalref . ".pdf";
-						if (file_exists($file))
+						// We remove directory
+						$propalref = dol_sanitizeFileName($this->ref);
+						if ($conf->propale->dir_output)
 						{
-							dol_delete_preview($this);
-					
-							if (!dol_delete_file($file))
+							$dir = $conf->propale->dir_output . "/" . $propalref ;
+							$file = $conf->propale->dir_output . "/" . $propalref . "/" . $propalref . ".pdf";
+							if (file_exists($file))
 							{
-								$this->error='ErrorFailToDeleteFile';
-								$this->db->rollback();
-								return 0;
+								dol_delete_preview($this);
+									
+								if (!dol_delete_file($file))
+								{
+									$this->error='ErrorFailToDeleteFile';
+									$this->db->rollback();
+									return 0;
+								}
 							}
-						}
-						if (file_exists($dir))
-						{
-							$res=@dol_delete_dir($dir);
-							if (! $res)
+							if (file_exists($dir))
 							{
-								$this->error='ErrorFailToDeleteDir';
-								$this->db->rollback();
-								return 0;
+								$res=@dol_delete_dir($dir);
+								if (! $res)
+								{
+									$this->error='ErrorFailToDeleteDir';
+									$this->db->rollback();
+									return 0;
+								}
 							}
 						}
 					}
-				}
-
-				if (! $error && ! $notrigger)
-				{
-					// Call triggers
-					include_once(DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php");
-					$interface=new Interfaces($this->db);
-					$result=$interface->run_triggers('PROPAL_DELETE',$this,$user,$langs,$conf);
-					if ($result < 0) { $error++; $this->errors=$interface->errors; }
-					// End call triggers
-				}
-
-				if (! $error)
-				{
-					dol_syslog(get_class($this)."::delete $this->id by $user->id", LOG_DEBUG);
-					$this->db->commit();
-					return 1;
+			
+					if (! $error)
+					{
+						dol_syslog(get_class($this)."::delete $this->id by $user->id", LOG_DEBUG);
+						$this->db->commit();
+						return 1;
+					}
+					else
+					{
+						$this->error=$this->db->lasterror();
+						dol_syslog(get_class($this)."::delete ".$this->error, LOG_ERR);
+						$this->db->rollback();
+						return 0;
+					}
 				}
 				else
 				{
 					$this->error=$this->db->lasterror();
 					dol_syslog(get_class($this)."::delete ".$this->error, LOG_ERR);
 					$this->db->rollback();
-					return 0;
+					return -3;
 				}
 			}
 			else
@@ -1869,8 +1881,8 @@ class Propal extends CommonObject
 		else
 		{
 			$this->error=$this->db->lasterror();
-            dol_syslog(get_class($this)."::delete ".$this->error, LOG_ERR);
-            $this->db->rollback();
+			dol_syslog(get_class($this)."::delete ".$this->error, LOG_ERR);
+			$this->db->rollback();
 			return -1;
 		}
 	}
diff --git a/htdocs/commande/class/commande.class.php b/htdocs/commande/class/commande.class.php
index a8457896ed4..46859b8e251 100644
--- a/htdocs/commande/class/commande.class.php
+++ b/htdocs/commande/class/commande.class.php
@@ -1,7 +1,7 @@
 <?php
 /* Copyright (C) 2003-2006 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  * Copyright (C) 2004-2011 Laurent Destailleur  <eldy@users.sourceforge.net>
- * Copyright (C) 2005-2011 Regis Houssin        <regis@dolibarr.fr>
+ * Copyright (C) 2005-2012 Regis Houssin        <regis@dolibarr.fr>
  * Copyright (C) 2006      Andre Cianfarani     <acianfa@free.fr>
  * Copyright (C) 2010-2011 Juanjo Menent        <jmenent@2byte.es>
  * Copyright (C) 2011      Jean Heimburger      <jean@tiaris.info>
@@ -2315,69 +2315,74 @@ class Commande extends CommonObject
         $error = 0;
 
         $this->db->begin();
-
-        // Delete order details
-        $sql = 'DELETE FROM '.MAIN_DB_PREFIX."commandedet WHERE fk_commande = ".$this->id;
-        dol_syslog("Commande::delete sql=".$sql);
-        if (! $this->db->query($sql) )
-        {
-            dol_syslog(get_class($this)."::delete error", LOG_ERR);
-            $error++;
-        }
-
-        // Delete order
-        $sql = 'DELETE FROM '.MAIN_DB_PREFIX."commande WHERE rowid = ".$this->id;
-        dol_syslog(get_class($this)."::delete sql=".$sql, LOG_DEBUG);
-        if (! $this->db->query($sql) )
+        
+        if (! $error && ! $notrigger)
         {
-            dol_syslog(get_class($this)."::delete error", LOG_ERR);
-            $error++;
+        	// Appel des triggers
+        	include_once(DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php");
+        	$interface=new Interfaces($this->db);
+        	$result=$interface->run_triggers('ORDER_DELETE',$this,$user,$langs,$conf);
+        	if ($result < 0) {
+        		$error++; $this->errors=$interface->errors;
+        	}
+        	// Fin appel triggers
         }
-
-        // Delete linked object
-        $res = $this->deleteObjectLinked();
-        if ($res < 0) $error++;
-
-        // Delete linked contacts
-        $res = $this->delete_linked_contact();
-        if ($res < 0) $error++;
-
-        // On efface le repertoire de pdf provisoire
-        $comref = dol_sanitizeFileName($this->ref);
-        if ($conf->commande->dir_output)
-        {
-            $dir = $conf->commande->dir_output . "/" . $comref ;
-            $file = $conf->commande->dir_output . "/" . $comref . "/" . $comref . ".pdf";
-            if (file_exists($file))	// We must delete all files before deleting directory
-            {
-                dol_delete_preview($this);
-
-                if (!dol_delete_file($file))
-                {
-                    $this->error=$langs->trans("ErrorCanNotDeleteFile",$file);
-                    $this->db->rollback();
-                    return 0;
-                }
-            }
-            if (file_exists($dir))
-            {
-                if (!dol_delete_dir($dir))
-                {
-                    $this->error=$langs->trans("ErrorCanNotDeleteDir",$dir);
-                    $this->db->rollback();
-                    return 0;
-                }
-            }
-        }
-
-        if (! $error && ! $notrigger)
+        
+        if (! $error)
         {
-            // Appel des triggers
-            include_once(DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php");
-            $interface=new Interfaces($this->db);
-            $result=$interface->run_triggers('ORDER_DELETE',$this,$user,$langs,$conf);
-            if ($result < 0) { $error++; $this->errors=$interface->errors; }
-            // Fin appel triggers
+        	// Delete order details
+        	$sql = 'DELETE FROM '.MAIN_DB_PREFIX."commandedet WHERE fk_commande = ".$this->id;
+        	dol_syslog("Commande::delete sql=".$sql);
+        	if (! $this->db->query($sql) )
+        	{
+        		dol_syslog(get_class($this)."::delete error", LOG_ERR);
+        		$error++;
+        	}
+        	
+        	// Delete order
+        	$sql = 'DELETE FROM '.MAIN_DB_PREFIX."commande WHERE rowid = ".$this->id;
+        	dol_syslog(get_class($this)."::delete sql=".$sql, LOG_DEBUG);
+        	if (! $this->db->query($sql) )
+        	{
+        		dol_syslog(get_class($this)."::delete error", LOG_ERR);
+        		$error++;
+        	}
+        	
+        	// Delete linked object
+        	$res = $this->deleteObjectLinked();
+        	if ($res < 0) $error++;
+        	
+        	// Delete linked contacts
+        	$res = $this->delete_linked_contact();
+        	if ($res < 0) $error++;
+        	
+        	// On efface le repertoire de pdf provisoire
+        	$comref = dol_sanitizeFileName($this->ref);
+        	if ($conf->commande->dir_output)
+        	{
+        		$dir = $conf->commande->dir_output . "/" . $comref ;
+        		$file = $conf->commande->dir_output . "/" . $comref . "/" . $comref . ".pdf";
+        		if (file_exists($file))	// We must delete all files before deleting directory
+        		{
+        			dol_delete_preview($this);
+        	
+        			if (!dol_delete_file($file))
+        			{
+        				$this->error=$langs->trans("ErrorCanNotDeleteFile",$file);
+        				$this->db->rollback();
+        				return 0;
+        			}
+        		}
+        		if (file_exists($dir))
+        		{
+        			if (!dol_delete_dir($dir))
+        			{
+        				$this->error=$langs->trans("ErrorCanNotDeleteDir",$dir);
+        				$this->db->rollback();
+        				return 0;
+        			}
+        		}
+        	}
         }
         
         if (! $error)
diff --git a/htdocs/compta/facture/class/facture.class.php b/htdocs/compta/facture/class/facture.class.php
index b8b580f0454..506c60327e8 100644
--- a/htdocs/compta/facture/class/facture.class.php
+++ b/htdocs/compta/facture/class/facture.class.php
@@ -4,7 +4,7 @@
  * Copyright (C) 2004      Sebastien Di Cintio   <sdicintio@ressource-toi.org>
  * Copyright (C) 2004      Benoit Mortier        <benoit.mortier@opensides.be>
  * Copyright (C) 2005      Marc Barilley / Ocebo <marc@ocebo.com>
- * Copyright (C) 2005-2011 Regis Houssin         <regis@dolibarr.fr>
+ * Copyright (C) 2005-2012 Regis Houssin         <regis@dolibarr.fr>
  * Copyright (C) 2006      Andre Cianfarani      <acianfa@free.fr>
  * Copyright (C) 2007      Franky Van Liedekerke <franky.van.liedekerke@telenet.be>
  * Copyright (C) 2010-2011 Juanjo Menent         <jmenent@2byte.es>
@@ -1131,10 +1131,25 @@ class Facture extends CommonObject
 
         $error=0;
         $this->db->begin();
-
-        // Delete linked object
-        $res = $this->deleteObjectLinked();
-        if ($res < 0) $error++;
+        
+        if (! $error && ! $notrigger)
+        {
+        	// Appel des triggers
+        	include_once(DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php");
+        	$interface=new Interfaces($this->db);
+        	$result=$interface->run_triggers('BILL_DELETE',$this,$user,$langs,$conf);
+        	if ($result < 0) {
+        		$error++; $this->errors=$interface->errors;
+        	}
+        	// Fin appel triggers
+        }
+        
+        if (! $error)
+        {
+        	// Delete linked object
+        	$res = $this->deleteObjectLinked();
+        	if ($res < 0) $error++;
+        }
 
         if (! $error)
         {
@@ -1177,16 +1192,6 @@ class Facture extends CommonObject
                 $resql=$this->db->query($sql);
                 if ($resql)
                 {
-                	if (! $notrigger)
-                	{
-                		// Appel des triggers
-                		include_once(DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php");
-                		$interface=new Interfaces($this->db);
-                		$result=$interface->run_triggers('BILL_DELETE',$this,$user,$langs,$conf);
-                		if ($result < 0) { $error++; $this->errors=$interface->errors; }
-                		// Fin appel triggers
-                	}
-
                     $this->db->commit();
                     return 1;
                 }
-- 
GitLab