From 1966d75cedb0a1ce79a5840c0e44b27fbf414b86 Mon Sep 17 00:00:00 2001
From: Laurent Destailleur <eldy@destailleur.fr>
Date: Sat, 12 Sep 2015 12:50:55 +0200
Subject: [PATCH] FIX #3490

---
 .../fourn/class/fournisseur.facture.class.php  | 18 +++++++++++-------
 htdocs/fourn/facture/card.php                  | 11 ++++++-----
 2 files changed, 17 insertions(+), 12 deletions(-)

diff --git a/htdocs/fourn/class/fournisseur.facture.class.php b/htdocs/fourn/class/fournisseur.facture.class.php
index 5e24417f618..892ee14e0f8 100644
--- a/htdocs/fourn/class/fournisseur.facture.class.php
+++ b/htdocs/fourn/class/fournisseur.facture.class.php
@@ -266,7 +266,7 @@ class FactureFournisseur extends CommonInvoice
 				{
 	            	if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) // For avoid conflicts if trigger used
 					{
-						$result=$this->insertExtraFields();
+						$result=$this->insertExtraFields();               // This also set $this->error or $this->errors if errors are found
 						if ($result < 0)
 						{
 							$error++;
@@ -274,11 +274,15 @@ class FactureFournisseur extends CommonInvoice
 					}
 				}
 				else if ($reshook < 0) $error++;
-                // Call trigger
-                $result=$this->call_trigger('BILL_SUPPLIER_CREATE',$user);
-                if ($result < 0) $error++;
-                // End call triggers
-
+				
+				if (! $error)
+				{
+                    // Call trigger
+                    $result=$this->call_trigger('BILL_SUPPLIER_CREATE',$user);
+                    if ($result < 0) $error++;
+                    // End call triggers
+				}
+				
                 if (! $error)
                 {
                     $this->db->commit();
@@ -307,7 +311,7 @@ class FactureFournisseur extends CommonInvoice
             }
             else
             {
-                $this->error=$this->db->error();
+                $this->error=$this->db->lasterror();
                 $this->db->rollback();
                 return -2;
             }
diff --git a/htdocs/fourn/facture/card.php b/htdocs/fourn/facture/card.php
index 6032a7a5036..c39128218d5 100644
--- a/htdocs/fourn/facture/card.php
+++ b/htdocs/fourn/facture/card.php
@@ -364,7 +364,7 @@ if (empty($reshook))
 			if(empty($object->date_echeance)) $object->date_echeance = $object->calculate_date_lim_reglement();
 
 	        // If creation from another object of another module
-	        if ($_POST['origin'] && $_POST['originid'])
+	        if (! $error && $_POST['origin'] && $_POST['originid'])
 	        {
 	            // Parse element/subelement (ex: project_task)
 	            $element = $subelement = $_POST['origin'];
@@ -468,17 +468,17 @@ if (empty($reshook))
 	                $error++;
 	            }
 	        }
-	        // If some invoice's lines already known
-	        else
+	        else if (! $error)
 	        {
 	            $id = $object->create($user);
 	            if ($id < 0)
 	            {
 	                $error++;
 	            }
-
+	            
 	            if (! $error)
 	            {
+        	        // If some invoice's lines already known
 	                for ($i = 1 ; $i < 9 ; $i++)
 	                {
 	                    $label = $_POST['label'.$i];
@@ -513,7 +513,8 @@ if (empty($reshook))
 	        {
 	            $langs->load("errors");
 	            $db->rollback();
-		        setEventMessage($langs->trans($object->error), 'errors');
+	            
+		        setEventMessages($object->error, $object->errors, 'errors');
 	            $action='create';
 	            $_GET['socid']=$_POST['socid'];
 	        }
-- 
GitLab