From b4c20f1b1a6c2eb9d666ddcf88c2798bcf51e535 Mon Sep 17 00:00:00 2001
From: Laurent Destailleur <eldy@destailleur.fr>
Date: Tue, 8 Nov 2016 11:23:40 +0100
Subject: [PATCH] Fix regression in creation of invoice Uniformize code

---
 htdocs/core/class/html.formfile.class.php     |  2 +
 .../class/fournisseur.commande.class.php      | 27 +++++++---
 .../fourn/class/fournisseur.facture.class.php | 20 ++++++--
 htdocs/fourn/facture/card.php                 | 49 +++++++++----------
 htdocs/projet/element.php                     | 17 +++++--
 5 files changed, 76 insertions(+), 39 deletions(-)

diff --git a/htdocs/core/class/html.formfile.class.php b/htdocs/core/class/html.formfile.class.php
index 109d2c5d23d..a05d8b27c5e 100644
--- a/htdocs/core/class/html.formfile.class.php
+++ b/htdocs/core/class/html.formfile.class.php
@@ -776,6 +776,8 @@ class FormFile
 
     /**
      *	Show a Document icon with link(s)
+     *  You may want to call this into a div like this:
+     *  print '<div class="inline-block valignmiddle">'.$formfile->getDocumentsLink($element_doc, $filename, $filedir).'</div>';
      *
      *	@param	string	$modulepart		propal, facture, facture_fourn, ...
      *	@param	string	$modulesubdir	Sub-directory to scan (Example: '0/1/10', 'FA/DD/MM/YY/9999'). Use '' if file is not into subdir of module.
diff --git a/htdocs/fourn/class/fournisseur.commande.class.php b/htdocs/fourn/class/fournisseur.commande.class.php
index 286eaaebc88..e1d73ec6e09 100644
--- a/htdocs/fourn/class/fournisseur.commande.class.php
+++ b/htdocs/fourn/class/fournisseur.commande.class.php
@@ -587,9 +587,10 @@ class CommandeFournisseur extends CommonOrder
      *
      *	@param		int		$withpicto		0=No picto, 1=Include picto into link, 2=Only picto
      *	@param		string	$option			On what the link points
+     *  @param	    int   	$notooltip		1=Disable tooltip
      *	@return		string					Chain with URL
      */
-    public function getNomUrl($withpicto=0,$option='')
+    public function getNomUrl($withpicto=0,$option='',$notooltip=0)
     {
         global $langs, $conf;
 
@@ -606,14 +607,28 @@ class CommandeFournisseur extends CommonOrder
         if (! empty($this->total_ttc))
             $label.= '<br><b>' . $langs->trans('AmountTTC') . ':</b> ' . price($this->total_ttc, 0, $langs, 0, -1, -1, $conf->currency);
 
-        $link = '<a href="'.DOL_URL_ROOT.'/fourn/commande/card.php?id='.$this->id.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
-        $linkend='</a>';
-
         $picto='order';
+        $url = DOL_URL_ROOT.'/fourn/commande/card.php?id='.$this->id;
+            
+        $linkclose='';
+        if (empty($notooltip))
+        {
+            if (! empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER))
+            {
+                $label=$langs->trans("ShowOrder");
+                $linkclose.=' alt="'.dol_escape_htmltag($label, 1).'"';
+            }
+            $linkclose.= ' title="'.dol_escape_htmltag($label, 1).'"';
+            $linkclose.=' class="classfortooltip"';
+        }
+        
+        $linkstart = '<a href="'.$url.'"';
+        $linkstart.=$linkclose.'>';
+        $linkend='</a>';
 
-        if ($withpicto) $result.=($link.img_object($label, $picto, 'class="classfortooltip"').$linkend);
+        if ($withpicto) $result.=($linkstart.img_object(($notooltip?'':$label), $picto, ($notooltip?'':'class="classfortooltip"'), 0, 0, $notooltip?0:1).$linkend);
         if ($withpicto && $withpicto != 2) $result.=' ';
-        $result.=$link.$this->ref.$linkend;
+        $result.=$linkstart.$this->ref.$linkend;
         return $result;
     }
 
diff --git a/htdocs/fourn/class/fournisseur.facture.class.php b/htdocs/fourn/class/fournisseur.facture.class.php
index d8490fde20b..af7bec526a6 100644
--- a/htdocs/fourn/class/fournisseur.facture.class.php
+++ b/htdocs/fourn/class/fournisseur.facture.class.php
@@ -1765,9 +1765,10 @@ class FactureFournisseur extends CommonInvoice
      *	@param		int		$max			Max length of shown ref
      *	@param		int		$short			1=Return just URL
      *	@param		string	$moretitle		Add more text to title tooltip
+     *  @param	    int   	$notooltip		1=Disable tooltip
      * 	@return		string					String with URL
      */
-    public function getNomUrl($withpicto=0,$option='',$max=0,$short=0,$moretitle='')
+    public function getNomUrl($withpicto=0,$option='',$max=0,$short=0,$moretitle='',$notooltip=0)
     {
         global $langs, $conf;
 
@@ -1802,10 +1803,23 @@ class FactureFournisseur extends CommonInvoice
         $ref=$this->ref;
         if (empty($ref)) $ref=$this->id;
 
-        $linkstart='<a href="'.$url.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
+        $linkclose='';
+        if (empty($notooltip))
+        {
+            if (! empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER))
+            {
+                $label=$langs->trans("ShowSupplierInvoice");
+                $linkclose.=' alt="'.dol_escape_htmltag($label, 1).'"';
+            }
+            $linkclose.= ' title="'.dol_escape_htmltag($label, 1).'"';
+            $linkclose.=' class="classfortooltip"';
+        }
+        
+        $linkstart = '<a href="'.$url.'"';
+        $linkstart.=$linkclose.'>';
         $linkend='</a>';
 
-        if ($withpicto) $result.=($linkstart.img_object($label, $picto, 'class="classfortooltip"').$linkend.' ');
+        if ($withpicto) $result.=($linkstart.img_object(($notooltip?'':$label), $picto, ($notooltip?'':'class="classfortooltip"'), 0, 0, $notooltip?0:1).$linkend.' ');
         $result.=$linkstart.($max?dol_trunc($ref,$max):$ref).$linkend;
         return $result;
     }
diff --git a/htdocs/fourn/facture/card.php b/htdocs/fourn/facture/card.php
index 103850509a5..923d226a57c 100644
--- a/htdocs/fourn/facture/card.php
+++ b/htdocs/fourn/facture/card.php
@@ -412,6 +412,7 @@ if (empty($reshook))
 
 				$id = $object->createFromCurrent($user);
 				if ($id <= 0) {
+				    $error++;
 					setEventMessages($object->error, $object->errors, 'errors');
 				}
 			}
@@ -506,7 +507,7 @@ if (empty($reshook))
 
 							// Defined the new fk_parent_line
 							if ($result > 0 && $line->product_type == 9) {
-							$fk_parent_line = $result;
+                                $fk_parent_line = $result;
 							}
 						}
 
@@ -571,8 +572,6 @@ if (empty($reshook))
 
 			if (! $error)
 			{
-				$db->begin();
-
 				$tmpproject = GETPOST('projectid', 'int');
 
 				// Creation facture
@@ -745,34 +744,34 @@ if (empty($reshook))
 						}
 					}
 				}
+			}
+		}
 
-				if ($error)
-				{
-					$langs->load("errors");
-					$db->rollback();
-
-					setEventMessages($object->error, $object->errors, 'errors');
-					$action='create';
-					$_GET['socid']=$_POST['socid'];
-				}
-				else
-				{
-					$db->commit();
+		if ($error)
+		{
+			$langs->load("errors");
+			$db->rollback();
 
-					if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE)) {
-						$outputlangs = $langs;
-						$result = $object->generateDocument($object->modelpdf, $outputlangs, $hidedetails, $hidedesc, $hideref);
-						if ($result	< 0)
-						{
-							dol_print_error($db,$object->error,$object->errors);
-							exit;
-						}
-					}
+			setEventMessages($object->error, $object->errors, 'errors');
+			$action='create';
+			$_GET['socid']=$_POST['socid'];
+		}
+		else
+		{
+			$db->commit();
 
-					header("Location: ".$_SERVER['PHP_SELF']."?id=".$id);
+			if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE)) {
+				$outputlangs = $langs;
+				$result = $object->generateDocument($object->modelpdf, $outputlangs, $hidedetails, $hidedesc, $hideref);
+				if ($result	< 0)
+				{
+					dol_print_error($db,$object->error,$object->errors);
 					exit;
 				}
 			}
+
+			header("Location: ".$_SERVER['PHP_SELF']."?id=".$id);
+			exit;
 		}
 	}
 
diff --git a/htdocs/projet/element.php b/htdocs/projet/element.php
index e94042f43d3..958f0b242a2 100644
--- a/htdocs/projet/element.php
+++ b/htdocs/projet/element.php
@@ -506,8 +506,8 @@ foreach ($listofreferent as $key => $value)
 				$element->fetch($idofelement);
 				if ($idofelementuser) $elementuser->fetch($idofelementuser);
 
+                // Special cases				
 				if ($tablename != 'expensereport_det' && method_exists($element, 'fetch_thirdparty')) $element->fetch_thirdparty();
-
 				if ($tablename == 'don') $total_ht_by_line=$element->amount;
 				elseif ($tablename == 'projet_task')
 				{
@@ -737,6 +737,7 @@ foreach ($listofreferent as $key => $value)
 				$element->fetch($idofelement);
 				if ($idofelementuser) $elementuser->fetch($idofelementuser);
 
+				// Special cases
 				if ($tablename != 'expensereport_det')
 				{
 					if(method_exists($element, 'fetch_thirdparty')) $element->fetch_thirdparty();
@@ -787,28 +788,34 @@ foreach ($listofreferent as $key => $value)
 				}
 				else
 				{
+				    // Show ref with link
 					if ($element instanceof Task)
 					{
 						print $element->getNomUrl(1,'withproject','time');
 						print ' - '.dol_trunc($element->label, 48);
 					}
 					else print $element->getNomUrl(1);
-
+						
 					$element_doc = $element->element;
 					$filename=dol_sanitizeFileName($element->ref);
 					$filedir=$conf->{$element_doc}->dir_output . '/' . dol_sanitizeFileName($element->ref);
 
-					if($element_doc === 'order_supplier') {
+					if ($element_doc === 'order_supplier') {
 						$element_doc='commande_fournisseur';
 						$filedir = $conf->fournisseur->commande->dir_output.'/'.dol_sanitizeFileName($element->ref);
 					}
-					else if($element_doc === 'invoice_supplier') {
+					else if ($element_doc === 'invoice_supplier') {
 						$element_doc='facture_fournisseur';
 						$filename = get_exdir($element->id,2,0,0,$this,'product').dol_sanitizeFileName($element->ref);
 						$filedir = $conf->fournisseur->facture->dir_output.'/'.get_exdir($element->id,2,0,0,null,'invoice_supplier').dol_sanitizeFileName($element->ref);
 					}
 
-					print $formfile->getDocumentsLink($element_doc, $filename, $filedir);
+					print '<div class="inline-block valignmiddle">'.$formfile->getDocumentsLink($element_doc, $filename, $filedir).'</div>';
+					
+					// Show supplier ref
+					if (! empty($element->ref_supplier)) print ' - '.$element->ref_supplier;
+					// Show customer ref
+					if (! empty($element->ref_customer)) print ' - '.$element->ref_customer;
 				}
 
 				print "</td>\n";
-- 
GitLab