From 84bc3f5b312ae2699576230b514137953dae6006 Mon Sep 17 00:00:00 2001
From: Laurent Destailleur <eldy@users.sourceforge.net>
Date: Sun, 4 Oct 2009 13:33:06 +0000
Subject: [PATCH] Uniformize code. Attached fils for supplier invoices are
 shown on invoice card

---
 htdocs/comm/propal.php                     |  2 +-
 htdocs/commande/commande.class.php         | 24 +++---
 htdocs/commande/fiche.php                  |  6 +-
 htdocs/document.php                        |  2 +-
 htdocs/fourn/commande/document.php         |  1 +
 htdocs/fourn/facture/contact.php           |  2 +-
 htdocs/fourn/facture/document.php          |  4 +-
 htdocs/fourn/facture/fiche.php             | 89 +++++++++++++++++++---
 htdocs/fourn/facture/note.php              |  4 +-
 htdocs/fourn/fournisseur.facture.class.php | 31 ++++++++
 10 files changed, 132 insertions(+), 33 deletions(-)

diff --git a/htdocs/comm/propal.php b/htdocs/comm/propal.php
index 37892228e9d..a7a3442bcd9 100644
--- a/htdocs/comm/propal.php
+++ b/htdocs/comm/propal.php
@@ -1009,7 +1009,7 @@ if ($id > 0 || ! empty($ref))
 
 	$rowspan=9;
 
-	// Societe
+	// Company
 	print '<tr><td>'.$langs->trans('Company').'</td><td colspan="5">'.$societe->getNomUrl(1).'</td>';
 	print '</tr>';
 
diff --git a/htdocs/commande/commande.class.php b/htdocs/commande/commande.class.php
index 7bc39f37caa..878359f6917 100644
--- a/htdocs/commande/commande.class.php
+++ b/htdocs/commande/commande.class.php
@@ -1601,14 +1601,14 @@ class Commande extends CommonObject
 			}
 			else
 			{
-				dol_syslog('Commande::cond_reglement Erreur '.$sql.' - '.$this->db->error());
-				$this->error=$this->db->error();
+				dol_syslog('Commande::cond_reglement Erreur '.$sql.' - '.$this->db->error(), LOG_ERR);
+				$this->error=$this->db->lasterror();
 				return -1;
 			}
 		}
 		else
 		{
-			dol_syslog('Commande::cond_reglement, etat commande incompatible');
+			dol_syslog('Commande::cond_reglement, etat commande incompatible', LOG_ERR);
 			$this->error='Etat commande incompatible '.$this->statut;
 			return -2;
 		}
@@ -1635,24 +1635,24 @@ class Commande extends CommonObject
 			}
 			else
 			{
-				dol_syslog('Commande::mode_reglement Erreur '.$sql.' - '.$this->db->error());
-				$this->error=$this->db->error();
+				dol_syslog('Commande::mode_reglement Erreur '.$sql.' - '.$this->db->error(), LOG_ERR);
+				$this->error=$this->db->lasterror();
 				return -1;
 			}
 		}
 		else
 		{
-			dol_syslog('Commande::mode_reglement, etat facture incompatible');
+			dol_syslog('Commande::mode_reglement, etat facture incompatible', LOG_ERR);
 			$this->error='Etat commande incompatible '.$this->statut;
 			return -2;
 		}
 	}
 
 	/**
-	 *      \brief      Positionne numero reference commande client
-	 *      \param      user            Utilisateur qui modifie
-	 *      \param      ref_client      Reference commande client
-	 *      \return     int             <0 si ko, >0 si ok
+	 *      \brief      Set customer ref
+	 *      \param      user            User that make change
+	 *      \param      ref_client      Customer ref
+	 *      \return     int             <0 if KO, >0 if OK
 	 */
 	function set_ref_client($user, $ref_client)
 	{
@@ -1669,8 +1669,8 @@ class Commande extends CommonObject
 			}
 			else
 			{
-				$this->error=$this->db->error();
-				dol_syslog('Commande::set_ref_client Erreur '.$this->error.' - '.$sql);
+				$this->error=$this->db->lasterror();
+				dol_syslog('Commande::set_ref_client Erreur '.$this->error.' - '.$sql, LOG_ERR);
 				return -2;
 			}
 		}
diff --git a/htdocs/commande/fiche.php b/htdocs/commande/fiche.php
index f167f655382..5b9a0544f54 100644
--- a/htdocs/commande/fiche.php
+++ b/htdocs/commande/fiche.php
@@ -1297,12 +1297,12 @@ else
 			print '<table class="nobordernopadding" width="100%"><tr><td nowrap="nowrap">';
 			print $langs->trans('RefCustomer').'</td><td align="left">';
 			print '</td>';
-			if ($_GET['action'] != 'RefCustomerOrder' && $commande->brouillon) print '<td align="right"><a href="'.$_SERVER['PHP_SELF'].'?action=RefCustomerOrder&amp;id='.$commande->id.'">'.img_edit($langs->trans('Modify')).'</a></td>';
+			if ($_GET['action'] != 'refcustomer' && $commande->brouillon) print '<td align="right"><a href="'.$_SERVER['PHP_SELF'].'?action=refcustomer&amp;id='.$commande->id.'">'.img_edit($langs->trans('Modify')).'</a></td>';
 			print '</tr></table>';
 			print '</td><td colspan="3">';
-			if ($user->rights->commande->creer && $_GET['action'] == 'RefCustomerOrder')
+			if ($user->rights->commande->creer && $_GET['action'] == 'refcustomer')
 			{
-				print '<form action="fiche.php?id='.$id.'" method="post">';
+				print '<form action="'.$_SERVER["PHP_SELF"].'?id='.$id.'" method="post">';
 				print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
 				print '<input type="hidden" name="action" value="set_ref_client">';
 				print '<input type="text" class="flat" size="20" name="ref_client" value="'.$commande->ref_client.'">';
diff --git a/htdocs/document.php b/htdocs/document.php
index abd217bb5a5..f455692ab88 100644
--- a/htdocs/document.php
+++ b/htdocs/document.php
@@ -195,7 +195,7 @@ if ($modulepart)
 		{
 			$accessallowed=1;
 		}
-		$original_file=$conf->fournisseur->dir_output.'/facture/'.$original_file;
+		$original_file=$conf->fournisseur->dir_output.'/facture/'.get_exdir(dirname($original_file),2,1).$original_file;
 		//$sqlprotectagainstexternals = "SELECT fk_soc as fk_soc FROM ".MAIN_DB_PREFIX."facture_fourn WHERE facnumber='$refname'";
 	}
 
diff --git a/htdocs/fourn/commande/document.php b/htdocs/fourn/commande/document.php
index 2fb8a8b5580..759ee20c86e 100644
--- a/htdocs/fourn/commande/document.php
+++ b/htdocs/fourn/commande/document.php
@@ -34,6 +34,7 @@ require_once(DOL_DOCUMENT_ROOT."/html.formfile.class.php");
 require_once DOL_DOCUMENT_ROOT."/fourn/fournisseur.commande.class.php";
 
 $langs->load('orders');
+$langs->load('other');
 $langs->load('sendings');
 $langs->load('companies');
 $langs->load('bills');
diff --git a/htdocs/fourn/facture/contact.php b/htdocs/fourn/facture/contact.php
index 293544674d8..c2e0ff6d410 100644
--- a/htdocs/fourn/facture/contact.php
+++ b/htdocs/fourn/facture/contact.php
@@ -187,7 +187,7 @@ if ($id > 0)
         print "</tr>\n";
 
 		// Third party
-		print "<tr><td>".$langs->trans("Company")."</td>";
+		print "<tr><td>".$langs->trans("Supplier")."</td>";
 		print '<td colspan="3">'.$facture->client->getNomUrl(1,'compta').'</td></tr>';
 		print "</table>";
 
diff --git a/htdocs/fourn/facture/document.php b/htdocs/fourn/facture/document.php
index 5da9a038faf..ac5f3e9ee95 100644
--- a/htdocs/fourn/facture/document.php
+++ b/htdocs/fourn/facture/document.php
@@ -91,7 +91,7 @@ if ($_POST['sendit'] && ! empty($conf->global->MAIN_UPLOAD_DOC))
 // Delete
 if ($action=='delete')
 {
-	$facid=$_GET["id"];
+	$facid=$_GET['id'];
 
 	$facture = new FactureFournisseur($db);
 	if ($facture->fetch($facid))
@@ -148,7 +148,7 @@ if ($facid > 0)
 		print "</tr>\n";
 
 		// Societe
-		print '<tr><td>'.$langs->trans('Company').'</td><td colspan="3">'.$facture->fournisseur->getNomUrl(1).'</td></tr>';
+		print '<tr><td>'.$langs->trans('Supplier').'</td><td colspan="3">'.$facture->fournisseur->getNomUrl(1).'</td></tr>';
 
 		print '<tr><td>'.$langs->trans('NbOfAttachedFiles').'</td><td colspan="3">'.sizeof($filearray).'</td></tr>';
 
diff --git a/htdocs/fourn/facture/fiche.php b/htdocs/fourn/facture/fiche.php
index dbc3e2379e8..b66994b0f95 100644
--- a/htdocs/fourn/facture/fiche.php
+++ b/htdocs/fourn/facture/fiche.php
@@ -28,6 +28,7 @@
  */
 
 require_once('./pre.inc.php');
+require_once(DOL_DOCUMENT_ROOT."/html.formfile.class.php");
 require_once(DOL_DOCUMENT_ROOT.'/fourn/facture/paiementfourn.class.php');
 require_once(DOL_DOCUMENT_ROOT.'/lib/fourn.lib.php');
 require_once(DOL_DOCUMENT_ROOT.'/product.class.php');
@@ -127,7 +128,15 @@ if ($_REQUEST['action'] == 'confirm_paid' && $_REQUEST['confirm'] == 'yes' && $u
 {
 	$facturefourn=new FactureFournisseur($db);
 	$facturefourn->fetch($_GET['facid']);
-	$facturefourn->set_paid($user);
+	$result=$facturefourn->set_paid($user);
+}
+
+// Set supplier ref
+if ($_POST['action'] == 'set_ref_supplier' && $user->rights->fournisseur->facture->creer)
+{
+	$facturefourn = new FactureFournisseur($db);
+	$facturefourn->fetch($_GET['facid']);
+	$result=$facturefourn->set_ref_supplier($user, $_POST['ref_supplier']);
 }
 
 if($_GET['action'] == 'deletepaiement')
@@ -397,6 +406,8 @@ if ($_POST['action'] == 'classin')
 
 llxHeader('','','');
 
+$formfile = new FormFile($db);
+
 
 // Mode creation
 if ($_GET['action'] == 'create')
@@ -515,8 +526,8 @@ else
 			print '<input type="hidden" name="action" value="update">';
 
 			print '<table class="border" width="100%">';
-			print '<tr><td>'.$langs->trans('Company').'</td>';
 
+			print '<tr><td>'.$langs->trans('Company').'</td>';
 			print '<td>'.$societe->getNomUrl(1).'</td>';
 			print '<td width="50%" valign="top">'.$langs->trans('NotePublic').'</td>';
 			print '</tr>';
@@ -524,16 +535,16 @@ else
 			// Ref
 			print '<tr><td valign="top">'.$langs->trans('Ref').'</td><td valign="top">';
 			print $fac->ref.'</td>';
-
-			print '<tr><td valign="top">'.$langs->trans('RefSupplier').'</td><td valign="top">';
-			print '<input name="facnumber" type="text" value="'.$fac->ref_supplier.'"></td>';
-
-			$rownb=9;
+			$rownb=10;
 			print '<td rowspan="'.$rownb.'" valign="top">';
-			print '<textarea name="note" wrap="soft" cols="60" rows="'.ROWS_9.'">';
+			print '<textarea name="note" wrap="soft" cols="60" rows="'.$rownb.'">';
 			print $fac->note;
 			print '</textarea></td></tr>';
 
+			print '<tr><td valign="top">'.$langs->trans('RefSupplier').'</td><td valign="top">';
+			print '<input name="facnumber" type="text" value="'.$fac->ref_supplier.'"></td>';
+			print '</tr>';
+
 			print '<tr><td valign="top">'.$langs->trans('Label').'</td><td>';
 			print '<input size="30" name="libelle" type="text" value="'.$fac->libelle.'"></td></tr>';
 
@@ -665,11 +676,31 @@ else
 			print "</tr>\n";
 
 			// Ref supplier
-			print '<tr><td nowrap="nowrap">'.$langs->trans("RefSupplier").'</td><td colspan="4">'.$fac->ref_supplier.'</td>';
-			print "</tr>\n";
+			print '<tr><td>';
+			print '<table class="nobordernopadding" width="100%"><tr><td nowrap>';
+			print $langs->trans('RefSupplier').'</td><td align="left">';
+			print '</td>';
+			if ($_GET['action'] != 'refsupplier' && $fac->brouillon) print '<td align="right"><a href="'.$_SERVER['PHP_SELF'].'?action=refsupplier&amp;facid='.$fac->id.'">'.img_edit($langs->trans('Modify')).'</a></td>';
+			print '</tr></table>';
+			print '</td><td colspan="5">';
+			if ($user->rights->fournisseur->facture->creer && $_GET['action'] == 'refsupplier')
+			{
+				print '<form action="'.$_SERVER["PHP_SELF"].'?facid='.$fac->id.'" method="post">';
+				print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
+				print '<input type="hidden" name="action" value="set_ref_supplier">';
+				print '<input type="text" class="flat" size="20" name="ref_supplier" value="'.$fac->ref_supplier.'">';
+				print ' <input type="submit" class="button" value="'.$langs->trans('Modify').'">';
+				print '</form>';
+			}
+			else
+			{
+				print $fac->ref_supplier;
+			}
+			print '</td>';
+			print '</tr>';
 
 			// Third party
-			print '<tr><td>'.$langs->trans('Company').'</td><td colspan="4">'.$societe->getNomUrl(1).' (<a href="index.php?socid='.$fac->socid.'">'.$langs->trans('OtherBills').'</a>)</td></tr>';
+			print '<tr><td>'.$langs->trans('Supplier').'</td><td colspan="4">'.$societe->getNomUrl(1).' (<a href="index.php?socid='.$fac->socid.'">'.$langs->trans('OtherBills').'</a>)</td></tr>';
 
 			// Type
 			print '<tr><td>'.$langs->trans('Type').'</td><td colspan="4">';
@@ -1153,6 +1184,42 @@ else
 			print '<a class="butActionDelete" href="fiche.php?facid='.$fac->id.'&amp;action=delete">'.$langs->trans('Delete').'</a>';
 		}
 		print '</div>';
+
+
+
+		if ($_GET['action'] != 'presend')
+		{
+			print '<table width="100%"><tr><td width="50%" valign="top">';
+			print '<a name="builddoc"></a>'; // ancre
+
+			/*
+			 * Documents generes
+			 */
+			$filename=dol_sanitizeFileName($fac->ref);
+			$filedir=$conf->fournisseur->dir_output.'/facture/'.get_exdir($fac->id,2).$fac->id;
+			$urlsource=$_SERVER['PHP_SELF'].'?facid='.$fac->id;
+			//$genallowed=$user->rights->fournisseur->facture->creer;
+			$genallowed=false;	// TODO Waiting for supplier invoice generation
+			$delallowed=$user->rights->fournisseur->facture->supprimer;
+
+			$var=true;
+
+			$somethingshown=$formfile->show_documents('facture_fournisseur',$filename,$filedir,$urlsource,$genallowed,$delallowed,$fac->modelpdf);
+
+
+			print '</td><td valign="top" width="50%">';
+
+			print '<br>';
+
+			// List of actions on element
+			/*
+			include_once(DOL_DOCUMENT_ROOT.'/html.formactions.class.php');
+			$formactions=new FormActions($db);
+			$somethingshown=$formactions->showactions($fac,'invoice_supplier',$socid);
+			*/
+
+			print '</td></tr></table>';
+		}
 	}
 }
 
diff --git a/htdocs/fourn/facture/note.php b/htdocs/fourn/facture/note.php
index 2ff6a173723..69621964908 100644
--- a/htdocs/fourn/facture/note.php
+++ b/htdocs/fourn/facture/note.php
@@ -109,8 +109,8 @@ if ($_GET["facid"])
 	print '<tr><td nowrap="nowrap">'.$langs->trans("RefSupplier").'</td><td colspan="3">'.$fac->ref_supplier.'</td>';
 	print "</tr>\n";
 
-    // Soci�t�
-    print '<tr><td>'.$langs->trans('Company').'</td><td colspan="3">'.$fac->fournisseur->getNomUrl(1).'</td></tr>';
+    // Company
+    print '<tr><td>'.$langs->trans('Supplier').'</td><td colspan="3">'.$fac->fournisseur->getNomUrl(1).'</td></tr>';
 
 	// Note publique
     print '<tr><td valign="top">'.$langs->trans("NotePublic").' :</td>';
diff --git a/htdocs/fourn/fournisseur.facture.class.php b/htdocs/fourn/fournisseur.facture.class.php
index 88a2cd0b468..da636d8471a 100644
--- a/htdocs/fourn/fournisseur.facture.class.php
+++ b/htdocs/fourn/fournisseur.facture.class.php
@@ -409,6 +409,37 @@ class FactureFournisseur extends Facture
 		}
 	}
 
+	/**
+	 *      \brief      Set supplier ref
+	 *      \param      user            User that make change
+	 *      \param      ref_supplier    Supplier ref
+	 *      \return     int             <0 if KO, >0 if OK
+	 */
+	function set_ref_supplier($user, $ref_supplier)
+	{
+		if ($user->rights->fournisseur->facture->creer)
+		{
+			dol_syslog('FactureFournisseur::set_ref_supplier this->id='.$this->id.', ref_supplier='.$ref_supplier);
+
+			$sql = 'UPDATE '.MAIN_DB_PREFIX.'facture_fourn SET facnumber = '.(empty($ref_supplier) ? 'NULL' : '\''.addslashes($ref_supplier).'\'');
+			$sql.= ' WHERE rowid = '.$this->id;
+			if ($this->db->query($sql))
+			{
+				$this->ref_supplier = $ref_supplier;
+				return 1;
+			}
+			else
+			{
+				$this->error=$this->db->lasterror();
+				dol_syslog('FactureFournisseur::set_ref_supplier '.$this->error.' - '.$sql, LOG_ERR);
+				return -2;
+			}
+		}
+		else
+		{
+			return -1;
+		}
+	}
 
 	/**
 	 *      \brief      Tag la facture comme payee completement
-- 
GitLab