From ad8b92541d5c77d8b1992356981f71ba8cf82db6 Mon Sep 17 00:00:00 2001
From: Laurent Destailleur <eldy@destailleur.fr>
Date: Sat, 20 Sep 2014 00:57:29 +0200
Subject: [PATCH] Fix: sql request with pgsql Fix: bug reported by scrutinizer
 Doxygen Start to work on multi user assigned to an event.

---
 .../class/accountingaccount.class.php         |  59 +++--
 .../accountancy/journal/purchasesjournal.php  |  96 ++++----
 htdocs/accountancy/journal/sellsjournal.php   |  10 +-
 htdocs/comm/action/class/actioncomm.class.php |  14 +-
 htdocs/comm/action/fiche.php                  | 222 ++++++++++++------
 htdocs/compta/facture/class/facture.class.php |  40 ++--
 htdocs/compta/journal/purchasesjournal.php    |  10 +-
 htdocs/compta/journal/sellsjournal.php        |   8 +-
 htdocs/core/class/html.form.class.php         |  12 +-
 htdocs/product/stock/class/entrepot.class.php |  12 +-
 htdocs/product/stock/fiche.php                |   8 +-
 test/phpunit/AllTests.php                     |  14 +-
 12 files changed, 301 insertions(+), 204 deletions(-)

diff --git a/htdocs/accountancy/class/accountingaccount.class.php b/htdocs/accountancy/class/accountingaccount.class.php
index 82c3ec3ca10..e6ada14a7db 100644
--- a/htdocs/accountancy/class/accountingaccount.class.php
+++ b/htdocs/accountancy/class/accountingaccount.class.php
@@ -18,7 +18,7 @@
  */
 
 /**
- * \file		htdocs/accountancy/class/Accountingaccount.class.php
+ * \file		htdocs/accountancy/class/accountingaccount.class.php
  * \ingroup		Accounting Expert
  * \brief		Fichier de la classe des comptes comptable
  */
@@ -29,8 +29,11 @@
 class AccountingAccount
 {
 	var $db;
+	var $error;
+
 	var $id;
 	var $rowid;
+
 	var $datec; // Creation date
 	var $fk_pcg_version;
 	var $pcg_type;
@@ -62,8 +65,10 @@ class AccountingAccount
 	 */
 	function fetch($rowid = null, $account_number = null)
 	{
-		if ($rowid || $account_number) {
-			$sql = "SELECT * FROM " . MAIN_DB_PREFIX . "accountingaccount WHERE ";
+		if ($rowid || $account_number)
+		{
+			$sql = "SELECT rowid, datec, tms, fk_pcg_version, pcg_type, pcg_subtype, account_number, account_parent, labe, fk_user_author, fk_user_modifn active";
+			$sql.= " FROM " . MAIN_DB_PREFIX . "accountingaccount WHERE";
 			if ($rowid) {
 				$sql .= " rowid = '" . $rowid . "'";
 			} elseif ($account_number) {
@@ -72,28 +77,40 @@ class AccountingAccount
 
 			dol_syslog(get_class($this) . "::fetch sql=" . $sql, LOG_DEBUG);
 			$result = $this->db->query($sql);
-			if ($result) {
+			if ($result)
+			{
 				$obj = $this->db->fetch_object($result);
-			} else {
-				return null;
+
+				if ($obj)
+				{
+					$this->id = $obj->rowid;
+					$this->rowid = $obj->rowid;
+					$this->datec = $obj->datec;
+					$this->tms = $obj->tms;
+					$this->fk_pcg_version = $obj->fk_pcg_version;
+					$this->pcg_type = $obj->pcg_type;
+					$this->pcg_subtype = $obj->pcg_subtype;
+					$this->account_number = $obj->account_number;
+					$this->account_parent = $obj->account_parent;
+					$this->label = $obj->label;
+					$this->fk_user_author = $obj->fk_user_author;
+					$this->fk_user_modif = $obj->fk_user_modif;
+					$this->active = $obj->active;
+
+					return $this->id;
+				}
+				else
+				{
+					return 0;
+				}
+			}
+			else
+			{
+				dol_print_error($this->db);
 			}
 		}
 
-		$this->id = $obj->rowid;
-		$this->rowid = $obj->rowid;
-		$this->datec = $obj->datec;
-		$this->tms = $obj->tms;
-		$this->fk_pcg_version = $obj->fk_pcg_version;
-		$this->pcg_type = $obj->pcg_type;
-		$this->pcg_subtype = $obj->pcg_subtype;
-		$this->account_number = $obj->account_number;
-		$this->account_parent = $obj->account_parent;
-		$this->label = $obj->label;
-		$this->fk_user_author = $obj->fk_user_author;
-		$this->fk_user_modif = $obj->fk_user_modif;
-		$this->active = $obj->active;
-
-		return $obj->rowid;
+		return -1;
 	}
 
 	/**
diff --git a/htdocs/accountancy/journal/purchasesjournal.php b/htdocs/accountancy/journal/purchasesjournal.php
index e0c2ceb8e36..c9d21f5d9be 100644
--- a/htdocs/accountancy/journal/purchasesjournal.php
+++ b/htdocs/accountancy/journal/purchasesjournal.php
@@ -28,7 +28,7 @@
  */
 
 require '../../main.inc.php';
-	
+
 // Class
 require_once DOL_DOCUMENT_ROOT.'/core/lib/report.lib.php';
 require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
@@ -88,12 +88,12 @@ $sql = "SELECT f.rowid, f.ref, f.type, f.datef as df, f.libelle,";
 $sql .= " fd.rowid as fdid, fd.description, fd.total_ttc, fd.tva_tx, fd.total_ht, fd.tva as total_tva, fd.product_type,";
 $sql .= " s.rowid as socid, s.nom as name, s.code_compta_fournisseur, s.fournisseur,";
 $sql .= " s.code_compta_fournisseur, p.accountancy_code_buy , ct.accountancy_code_buy as account_tva, aa.rowid as fk_compte, aa.account_number as compte, aa.label as label_compte";
-$sql .= " FROM " . MAIN_DB_PREFIX . "facture_fourn_det fd";
-$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "c_tva ct ON fd.tva_tx = ct.taux AND ct.fk_pays = '" . $idpays . "'";
-$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "product p ON p.rowid = fd.fk_product";
-$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "accountingaccount aa ON aa.rowid = fd.fk_code_ventilation";
-$sql .= " JOIN " . MAIN_DB_PREFIX . "facture_fourn f ON f.rowid = fd.fk_facture_fourn";
-$sql .= " JOIN " . MAIN_DB_PREFIX . "societe s ON s.rowid = f.fk_soc";
+$sql .= " FROM " . MAIN_DB_PREFIX . "facture_fourn_det as fd";
+$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "c_tva as ct ON fd.tva_tx = ct.taux AND ct.fk_pays = '" . $idpays . "'";
+$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "product as p ON p.rowid = fd.fk_product";
+$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "accountingaccount as aa ON aa.rowid = fd.fk_code_ventilation";
+$sql .= " JOIN " . MAIN_DB_PREFIX . "facture_fourn as f ON f.rowid = fd.fk_facture_fourn";
+$sql .= " JOIN " . MAIN_DB_PREFIX . "societe as s ON s.rowid = f.fk_soc";
 $sql .= " WHERE f.fk_statut > 0 ";
 if (! empty($conf->multicompany->enabled)) {
 	$sql .= " AND f.entity = " . $conf->entity;
@@ -113,13 +113,13 @@ if ($result) {
 	// les variables
 	$cptfour = (! empty($conf->global->COMPTA_ACCOUNT_SUPPLIER)) ? $conf->global->COMPTA_ACCOUNT_SUPPLIER : $langs->trans("CodeNotDef");
 	$cpttva = (! empty($conf->global->COMPTA_VAT_ACCOUNT)) ? $conf->global->COMPTA_VAT_ACCOUNT : $langs->trans("CodeNotDef");
-	
+
 	$tabfac = array ();
 	$tabht = array ();
 	$tabtva = array ();
 	$tabttc = array ();
 	$tabcompany = array ();
-	
+
 	$i = 0;
 	while ( $i < $num ) {
 		$obj = $db->fetch_object($result);
@@ -133,7 +133,7 @@ if ($result) {
 				$compta_prod = (! empty($conf->global->COMPTA_SERVICE_BUY_ACCOUNT)) ? $conf->global->COMPTA_SERVICE_BUY_ACCOUNT : $langs->trans("CodeNotDef");
 		}
 		$compta_tva = (! empty($obj->account_tva) ? $obj->account_tva : $cpttva);
-		
+
 		$tabfac[$obj->rowid]["date"] = $obj->df;
 		$tabfac[$obj->rowid]["ref"] = $obj->ref;
 		$tabfac[$obj->rowid]["type"] = $obj->type;
@@ -145,9 +145,9 @@ if ($result) {
 		$tabcompany[$obj->rowid] = array (
 				'id' => $obj->socid,
 				'name' => $obj->name,
-				'code_fournisseur' => $obj->code_compta_fournisseur 
+				'code_fournisseur' => $obj->code_compta_fournisseur
 		);
-		
+
 		$i ++;
 	}
 } else {
@@ -160,11 +160,11 @@ if ($result) {
 // Bookkeeping Write
 if ($action == 'writebookkeeping') {
 	$now = dol_now();
-	
+
 	foreach ( $tabfac as $key => $val ) {
 		foreach ( $tabttc[$key] as $k => $mt ) {
 			// get compte id and label
-			
+
 			$bookkeeping = new BookKeeping($db);
 			$bookkeeping->doc_date = $val["date"];
 			$bookkeeping->doc_ref = $val["ref"];
@@ -180,10 +180,10 @@ if ($action == 'writebookkeeping') {
 			$bookkeeping->debit = ($mt <= 0) ? $mt : 0;
 			$bookkeeping->credit = ($mt > 0) ? $mt : 0;
 			$bookkeeping->code_journal = $conf->global->ACCOUNTING_PURCHASE_JOURNAL;
-			
+
 			$bookkeeping->create();
 		}
-		
+
 		// Product / Service
 		foreach ( $tabht[$key] as $k => $mt ) {
 			if ($mt) {
@@ -205,18 +205,18 @@ if ($action == 'writebookkeeping') {
 					$bookkeeping->debit = ($mt > 0) ? $mt : 0;
 					$bookkeeping->credit = ($mt <= 0) ? $mt : 0;
 					$bookkeeping->code_journal = $conf->global->ACCOUNTING_PURCHASE_JOURNAL;
-					
+
 					$bookkeeping->create();
 				}
 			}
 		}
-		
+
 		// VAT
 		// var_dump($tabtva);
 		foreach ( $tabtva[$key] as $k => $mt ) {
 			if ($mt) {
 				// get compte id and label
-				
+
 				$bookkeeping = new BookKeeping($db);
 				$bookkeeping->doc_date = $val["date"];
 				$bookkeeping->doc_ref = $val["ref"];
@@ -232,7 +232,7 @@ if ($action == 'writebookkeeping') {
 				$bookkeeping->debit = ($mt > 0) ? $mt : 0;
 				$bookkeeping->credit = ($mt <= 0) ? $mt : 0;
 				$bookkeeping->code_journal = $conf->global->ACCOUNTING_PURCHASE_JOURNAL;
-				
+
 				$bookkeeping->create();
 			}
 		}
@@ -243,21 +243,21 @@ if ($action == 'writebookkeeping') {
 
 if ($action == 'export_csv') {
 	$sep = $conf->global->ACCOUNTING_SEPARATORCSV;
-	
+
 	header('Content-Type: text/csv');
 	header('Content-Disposition: attachment;filename=journal_achats.csv');
-	
+
 	if ($conf->global->ACCOUNTING_MODELCSV == 1) 	// Modèle Export Cegid Expert
 	{
 		foreach ( $tabfac as $key => $val ) {
 			$date = dol_print_date($db->jdate($val["date"]), '%d%m%Y');
-			
+
 			// Product / Service
 			foreach ( $tabht[$key] as $k => $mt ) {
 				$companystatic->id = $tabcompany[$key]['id'];
 				$companystatic->name = $tabcompany[$key]['name'];
 				$companystatic->client = $tabcompany[$key]['code_client'];
-				
+
 				if ($mt) {
 					print $date . $sep;
 					print $conf->global->ACCOUNTING_PURCHASE_JOURNAL . $sep;
@@ -270,7 +270,7 @@ if ($action == 'export_csv') {
 					print "\n";
 				}
 			}
-			
+
 			// VAT
 			// var_dump($tabtva);
 			foreach ( $tabtva[$key] as $k => $mt ) {
@@ -289,7 +289,7 @@ if ($action == 'export_csv') {
 			print $date . $sep;
 			print $conf->global->ACCOUNTING_PURCHASE_JOURNAL . $sep;
 			print length_accountg($conf->global->COMPTA_ACCOUNT_SUPPLIER) . $sep;
-			
+
 			foreach ( $tabttc[$key] as $k => $mt ) {
 				print length_accounta(html_entity_decode($k)) . $sep;
 				print ($mt < 0 ? 'D' : 'C') . $sep;
@@ -303,11 +303,11 @@ if ($action == 'export_csv') {
 	{
 		foreach ( $tabfac as $key => $val ) {
 			$date = dol_print_date($db->jdate($val["date"]), 'day');
-			
+
 			$companystatic->id = $tabcompany[$key]['id'];
 			$companystatic->name = $tabcompany[$key]['name'];
 			$companystatic->client = $tabcompany[$key]['code_client'];
-			
+
 			// Product / Service
 			foreach ( $tabht[$key] as $k => $mt ) {
 				if ($mt) {
@@ -333,7 +333,7 @@ if ($action == 'export_csv') {
 					print "\n";
 				}
 			}
-			
+
 			// Third party
 			print '"' . $date . '"' . $sep;
 			print '"' . $val["ref"] . '"' . $sep;
@@ -347,11 +347,11 @@ if ($action == 'export_csv') {
 		}
 	}
 } else {
-	
+
 	llxHeader('', '', '');
-	
+
 	$form = new Form($db);
-	
+
 	$nom = $langs->trans("PurchasesJournal");
 	$nomlink = '';
 	$periodlink = '';
@@ -364,11 +364,11 @@ if ($action == 'export_csv') {
 		$description .= $langs->trans("DepositsAreIncluded");
 	$period = $form->select_date($date_start, 'date_start', 0, 0, 0, '', 1, 0, 1) . ' - ' . $form->select_date($date_end, 'date_end', 0, 0, 0, '', 1, 0, 1);
 	report_header($nom, $nomlink, $period, $periodlink, $description, $builddate, $exportlink, array('action' => ''));
-	
+
 	print '<input type="button" class="button" style="float: right;" value="Export CSV" onclick="launch_export();" />';
-	
+
 	print '<input type="button" class="button" value="' . $langs->trans("WriteBookKeeping") . '" onclick="writebookkeeping();" />';
-	
+
 	print '
 	<script type="text/javascript">
 		function launch_export() {
@@ -382,12 +382,12 @@ if ($action == 'export_csv') {
 		    $("div.fiche div.tabBar form input[name=\"action\"]").val("");
 		}
 	</script>';
-	
+
 	/*
 	 * Show result array
 	 */
 	print '<br><br>';
-	
+
 	$i = 0;
 	print "<table class=\"noborder\" width=\"100%\">";
 	print "<tr class=\"liste_titre\">";
@@ -397,21 +397,21 @@ if ($action == 'export_csv') {
 	print "<td>" . $langs->trans("Account") . "</td>";
 	print "<t><td>" . $langs->trans("Type") . "</td><td align='right'>" . $langs->trans("Debit") . "</td><td align='right'>" . $langs->trans("Credit") . "</td>";
 	print "</tr>\n";
-	
+
 	$var = true;
 	$r = '';
-	
+
 	$invoicestatic = new FactureFournisseur($db);
 	$companystatic = new Fournisseur($db);
-	
+
 	foreach ( $tabfac as $key => $val ) {
 		$invoicestatic->id = $key;
 		$invoicestatic->ref = $val["ref"];
 		$invoicestatic->type = $val["type"];
 		$invoicestatic->description = html_entity_decode(dol_trunc($val["description"], 32));
-		
+
 		$date = dol_print_date($db->jdate($val["date"]), 'day');
-		
+
 		// Product / Service
 		foreach ( $tabht[$key] as $k => $mt ) {
 			if ($mt) {
@@ -441,16 +441,16 @@ if ($action == 'export_csv') {
 			}
 		}
 		print "<tr " . $bc[$var] . ">";
-		
+
 		// Third party
 		// print "<td>".$conf->global->COMPTA_JOURNAL_BUY."</td>";
 		print "<td>" . $date . "</td>";
 		print "<td>" . $invoicestatic->getNomUrl(1) . "</td>";
-		
+
 		foreach ( $tabttc[$key] as $k => $mt ) {
 			$companystatic->id = $tabcompany[$key]['id'];
 			$companystatic->name = $tabcompany[$key]['name'];
-			
+
 			print "<td>" . length_accounta($k);
 			print "</td><td>" . $langs->trans("ThirdParty");
 			print ' (' . $companystatic->getNomUrl(0, 'supplier', 16) . ')';
@@ -459,12 +459,12 @@ if ($action == 'export_csv') {
 			print '<td align="right">' . ($mt >= 0 ? price($mt) : '') . "</td>";
 		}
 		print "</tr>";
-		
+
 		$var = ! $var;
 	}
-	
+
 	print "</table>";
-	
+
 	// End of page
 	llxFooter();
 }
diff --git a/htdocs/accountancy/journal/sellsjournal.php b/htdocs/accountancy/journal/sellsjournal.php
index 587a0d387a4..79444fbca60 100644
--- a/htdocs/accountancy/journal/sellsjournal.php
+++ b/htdocs/accountancy/journal/sellsjournal.php
@@ -90,11 +90,11 @@ $sql .= " fd.rowid as fdid, fd.description, fd.product_type, fd.total_ht, fd.tot
 $sql .= " s.rowid as socid, s.nom as name, s.code_compta, s.code_client,";
 $sql .= " p.rowid as pid, p.ref as pref, p.accountancy_code_sell, aa.rowid as fk_compte, aa.account_number as compte, aa.label as label_compte, ";
 $sql .= " ct.accountancy_code_sell as account_tva";
-$sql .= " FROM " . MAIN_DB_PREFIX . "facturedet fd";
-$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "product p ON p.rowid = fd.fk_product";
-$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "accountingaccount aa ON aa.rowid = fd.fk_code_ventilation";
-$sql .= " JOIN " . MAIN_DB_PREFIX . "facture f ON f.rowid = fd.fk_facture";
-$sql .= " JOIN " . MAIN_DB_PREFIX . "societe s ON s.rowid = f.fk_soc";
+$sql .= " FROM " . MAIN_DB_PREFIX . "facturedet as fd";
+$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "product as p ON p.rowid = fd.fk_product";
+$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "accountingaccount as aa ON aa.rowid = fd.fk_code_ventilation";
+$sql .= " JOIN " . MAIN_DB_PREFIX . "facture as f ON f.rowid = fd.fk_facture";
+$sql .= " JOIN " . MAIN_DB_PREFIX . "societe as s ON s.rowid = f.fk_soc";
 $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "c_tva ct ON fd.tva_tx = ct.taux AND ct.fk_pays = '" . $idpays . "'";
 $sql .= " WHERE fd.fk_code_ventilation > 0 ";
 if (! empty($conf->multicompany->enabled)) {
diff --git a/htdocs/comm/action/class/actioncomm.class.php b/htdocs/comm/action/class/actioncomm.class.php
index 434cc92dcb6..4fe49999111 100644
--- a/htdocs/comm/action/class/actioncomm.class.php
+++ b/htdocs/comm/action/class/actioncomm.class.php
@@ -57,12 +57,14 @@ class ActionComm extends CommonObject
     var $punctual = 1;        // Milestone
     var $percentage;    // Percentage
     var $location;      // Location
+
 	var $transparency;	// Transparency (ical standard). Used to say if people assigned to event are busy or not by event. 0=available, 1=busy, 2=busy (refused events)
     var $priority;      // Small int (0 By default)
     var $note;          // Description
 
-    var $usertodo;		// Object user that must do action
-    var $userdone;	 	// Object user that did action
+	var $userassigned;	// Array of user ids
+    var $usertodo;		// Object user of owner
+    var $userdone;	 	// Object user that did action (deprecated)
 
     var $societe;		// Company linked to action (optional)
     var $contact;		// Contact linked to action (optional)
@@ -89,10 +91,10 @@ class ActionComm extends CommonObject
     {
         $this->db = $db;
 
-        $this->author = new stdClass();
-        $this->usermod = new stdClass();
-        $this->usertodo = new stdClass();
-        $this->userdone = new stdClass();
+        //$this->author = new stdClass();
+        //$this->usermod = new stdClass();
+        //$this->usertodo = new stdClass();
+        //$this->userdone = new stdClass();
         $this->societe = new stdClass();
         $this->contact = new stdClass();
     }
diff --git a/htdocs/comm/action/fiche.php b/htdocs/comm/action/fiche.php
index 27be46bd9e1..929600512c7 100644
--- a/htdocs/comm/action/fiche.php
+++ b/htdocs/comm/action/fiche.php
@@ -1,6 +1,6 @@
 <?php
 /* Copyright (C) 2001-2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
- * Copyright (C) 2004-2013 Laurent Destailleur  <eldy@users.sourceforge.net>
+ * Copyright (C) 2004-2014 Laurent Destailleur  <eldy@users.sourceforge.net>
  * Copyright (C) 2005      Simon TOSSER         <simon@kornog-computing.com>
  * Copyright (C) 2005-2012 Regis Houssin        <regis.houssin@capnetworks.com>
  * Copyright (C) 2010-2013 Juanjo Menent        <jmenent@2byte.es>
@@ -87,20 +87,37 @@ $hookmanager->initHooks(array('actioncard'));
  * Actions
  */
 
-if (GETPOST('addassignedtouser'))
+// Remove user to assigned list
+if (! empty($_POST['removedassigned']))
+{
+	$idtoremove=$_POST['removedassigned'];
+	if (! empty($_SESSION['assignedtouser'])) $tmpassigneduserids=dol_json_decode($_SESSION['assignedtouser'],1);
+	else $tmpassigneduserids=array();
+	unset($tmpassigneduserids[$idtoremove]);
+	//var_dump($_POST['removedassigned']);exit;
+	$_SESSION['assignedtouser']=dol_json_encode($tmpassigneduserids);
+	$donotclearsession=1;
+	if ($action == 'add') $action = 'create';
+	if ($action == 'update') $action = 'edit';
+}
+
+// Add user to assigned list
+if (GETPOST('addassignedtouser') || GETPOST('updateassignedtouser'))
 {
 	// Add a new user
 	if (GETPOST('affectedto') > 0)
 	{
 		$assignedtouser=array();
-		if (!empty($_SESSION['assignedtouser'])) $assignedtouser=dol_json_decode($_SESSION['assignedtouser'], true);
+		if (! empty($_SESSION['assignedtouser'])) $assignedtouser=dol_json_decode($_SESSION['assignedtouser'], true);
 		$assignedtouser[GETPOST('affectedto')]=array('transparency'=>GETPOST('transparency'),'mandatory'=>1);
 		$_SESSION['assignedtouser']=dol_json_encode($assignedtouser);
 	}
 	$donotclearsession=1;
-	$action='create';
+	if ($action == 'add') $action = 'create';
+	if ($action == 'update') $action = 'edit';
 }
-// Add action
+
+// Add event
 if ($action == 'add')
 {
 	$error=0;
@@ -131,22 +148,22 @@ if ($action == 'add')
 	// Check parameters
 	if (! $datef && $percentage == 100)
 	{
-		$error++;
+		$error++; $donotclearsession=1;
 		$action = 'create';
 		setEventMessage($langs->trans("ErrorFieldRequired",$langs->transnoentitiesnoconv("DateEnd")), 'errors');
 	}
 
 	if (empty($conf->global->AGENDA_USE_EVENT_TYPE) && ! GETPOST('label'))
 	{
-		$error++;
+		$error++; $donotclearsession=1;
 		$action = 'create';
 		setEventMessage($langs->trans("ErrorFieldRequired",$langs->transnoentitiesnoconv("Title")), 'errors');
 	}
 
 	// Initialisation objet cactioncomm
-	if (! GETPOST('actioncode'))
+	if (! GETPOST('actioncode') > 0)
 	{
-		$error++;
+		$error++; $donotclearsession=1;
 		$action = 'create';
 		setEventMessage($langs->trans("ErrorFieldRequired",$langs->transnoentitiesnoconv("Type")), 'errors');
 	}
@@ -161,7 +178,6 @@ if ($action == 'add')
 	$object->priority = GETPOST("priority")?GETPOST("priority"):0;
 	$object->fulldayevent = (! empty($fulldayevent)?1:0);
 	$object->location = GETPOST("location");
-	$object->transparency = (GETPOST("transparency")=='on'?1:0);
 	$object->label = trim(GETPOST('label'));
 	$object->fk_element = GETPOST("fk_element");
 	$object->elementtype = GETPOST("elementtype");
@@ -186,18 +202,35 @@ if ($action == 'add')
 	$object->percentage = $percentage;
 	$object->duree=((float) (GETPOST('dureehour') * 60) + (float) GETPOST('dureemin')) * 60;
 
-	$usertodo=new User($db);
-	if ($_POST["affectedto"] > 0)
+	$listofuserid=dol_json_decode($_SESSION['assignedtouser']);
+	$i=0;
+	foreach($listofuserid as $key => $value)
 	{
-		$usertodo->fetch($_POST["affectedto"]);
+		if ($i == 0)	// First entry
+		{
+			$usertodo=new User($db);
+			if ($key > 0)
+			{
+				$usertodo->fetch($key);
+			}
+			$object->usertodo = $usertodo;
+			$object->transparency = (GETPOST("transparency")=='on'?1:0);
+		}
+
+		$object->userassigned[$key]=array('id'=>$key, 'transparency'=>(GETPOST("transparency")=='on'?1:0));
+
+		$i++;
 	}
-	$object->usertodo = $usertodo;
-	$userdone=new User($db);
-	if ($_POST["doneby"] > 0)
+
+	if (! empty($conf->global->AGENDA_ENABLE_DONEBY))
 	{
-		$userdone->fetch($_POST["doneby"]);
+		$userdone=new User($db);
+		if ($_POST["doneby"] > 0)
+		{
+			$userdone->fetch($_POST["doneby"]);
+		}
+		$object->userdone = $userdone;
 	}
-	$object->userdone = $userdone;
 
 	$object->note = trim($_POST["note"]);
 	if (isset($_POST["contactid"])) $object->contact = $contact;
@@ -214,16 +247,22 @@ if ($action == 'add')
 	if (! empty($conf->phenix->enabled) && GETPOST('add_phenix') == 'on') $object->use_phenix=1;
 
 	// Check parameters
+	if (empty($object->usertodo))
+	{
+		$error++; $donotclearsession=1;
+		$action = 'create';
+		setEventMessage($langs->trans("ErrorFieldRequired",$langs->transnoentitiesnoconv("ActionAffectedTo")), 'errors');
+	}
 	if ($object->type_code == 'AC_RDV' && ($datep == '' || ($datef == '' && empty($fulldayevent))))
 	{
-		$error++;
+		$error++; $donotclearsession=1;
 		$action = 'create';
 		setEventMessage($langs->trans("ErrorFieldRequired",$langs->transnoentitiesnoconv("DateEnd")), 'errors');
 	}
 
 	if (! GETPOST('apyear') && ! GETPOST('adyear'))
 	{
-		$error++;
+		$error++; $donotclearsession=1;
 		$action = 'create';
 		setEventMessage($langs->trans("ErrorFieldRequired",$langs->transnoentitiesnoconv("Date")), 'errors');
 	}
@@ -265,42 +304,14 @@ if ($action == 'add')
 				$langs->load("errors");
 				$error=$langs->trans($object->error);
 				setEventMessage($error,'errors');
-				$action = 'create';
+				$action = 'create'; $donotclearsession=1;
 			}
 		}
 		else
 		{
 			$db->rollback();
-			$langs->load("errors");
-
-			if (! empty($object->error)) setEventMessage($langs->trans($object->error), 'errors');
-			if (count($object->errors)) setEventMessage($object->errors, 'errors');
-
-			$action = 'create';
-		}
-	}
-}
-
-/*
- * Action suppression de l'action
- */
-if ($action == 'confirm_delete' && GETPOST("confirm") == 'yes')
-{
-	$object->fetch($id);
-
-	if ($user->rights->agenda->myactions->delete
-		|| $user->rights->agenda->allactions->delete)
-	{
-		$result=$object->delete();
-
-		if ($result >= 0)
-		{
-			header("Location: index.php");
-			exit;
-		}
-		else
-		{
-			setEventMessage($object->error,'errors');
+			setEventMessages($object->error, $object->errors, 'errors');
+			$action = 'create'; $donotclearsession=1;
 		}
 	}
 }
@@ -348,25 +359,59 @@ if ($action == 'update')
 
 		if (! $datef && $percentage == 100)
 		{
-			$error=$langs->trans("ErrorFieldRequired",$langs->trans("DateEnd"));
+			$error++; $donotclearsession=1;
+			setEventMessages($langs->trans("ErrorFieldRequired",$langs->transnoentitiesnoconv("DateEnd")),$object->errors,'errors');
 			$action = 'edit';
 		}
 
 		// Users
-		$usertodo=new User($db);
-		if ($_POST["affectedto"])
+		$listofuserid=dol_json_decode($_SESSION['assignedtouser']);
+		$i=0;
+		foreach($listofuserid as $key => $value)
+		{
+			if ($i == 0)	// First entry
+			{
+				$usertodo=new User($db);
+				if ($key > 0)
+				{
+					$usertodo->fetch($key);
+				}
+				$object->usertodo = $usertodo;
+				$object->transparency=(GETPOST("transparency")=='on'?1:0);
+			}
+
+			$object->userassigned[$key]=array('id'=>$key, 'transparency'=>(GETPOST("transparency")=='on'?1:0));
+
+			$i++;
+		}
+
+		if (! empty($conf->global->AGENDA_ENABLE_DONEBY))
 		{
-			$usertodo->fetch($_POST["affectedto"]);
+			$userdone=new User($db);
+			if ($_POST["doneby"])
+			{
+				$userdone->fetch($_POST["doneby"]);
+			}
+			$object->userdone = $userdone;
 		}
-		$object->usertodo = $usertodo;
-		$object->transparency=(GETPOST("transparency")=='on'?1:0);
 
-		$userdone=new User($db);
-		if ($_POST["doneby"])
+		// Check parameters
+		if (! GETPOST('actioncode') > 0)
 		{
-			$userdone->fetch($_POST["doneby"]);
+			$error++; $donotclearsession=1;
+			$action = 'edit';
+			setEventMessage($langs->trans("ErrorFieldRequired",$langs->transnoentitiesnoconv("Type")), 'errors');
+		}
+		else
+		{
+			$result=$cactioncomm->fetch(GETPOST('actioncode'));
+		}
+		if (empty($object->usertodo))
+		{
+			$error++; $donotclearsession=1;
+			$action = 'edit';
+			setEventMessage($langs->trans("ErrorFieldRequired",$langs->transnoentitiesnoconv("ActionAffectedTo")), 'errors');
 		}
-		$object->userdone = $userdone;
 
 		// Fill array 'array_options' with data from add form
 		$ret = $extrafields->setOptionalsFromPost($extralabels,$object);
@@ -383,17 +428,13 @@ if ($action == 'update')
 			}
 			else
 			{
+				setEventMessages($object->error,$object->errors,'errors');
 				$db->rollback();
 			}
 		}
 	}
 
-	if ($result < 0)
-	{
-		setEventMessage($object->error,'errors');
-		setEventMessage($object->errors,'errors');
-	}
-	else
+	if (! $error)
 	{
         if (! empty($backtopage))
         {
@@ -403,6 +444,30 @@ if ($action == 'update')
 	}
 }
 
+/*
+ * delete event
+ */
+if ($action == 'confirm_delete' && GETPOST("confirm") == 'yes')
+{
+	$object->fetch($id);
+
+	if ($user->rights->agenda->myactions->delete
+		|| $user->rights->agenda->allactions->delete)
+	{
+		$result=$object->delete();
+
+		if ($result >= 0)
+		{
+			header("Location: index.php");
+			exit;
+		}
+		else
+		{
+			setEventMessages($object->error,$object->errors,'errors');
+		}
+	}
+}
+
 /*
  * Action move update, used when user move an event in calendar by drag'n drop
  */
@@ -576,15 +641,14 @@ if ($action == 'create')
     print '<tr><td>'.$langs->trans("Location").'</td><td colspan="3"><input type="text" name="location" size="50" value="'.(GETPOST('location')?GETPOST('location'):$object->location).'"></td></tr>';
 
 	// Assigned to
-	$var=false;
 	print '<tr><td class="nowrap">'.$langs->trans("ActionAffectedTo").'</td><td>';
 	if (empty($donotclearsession))
 	{
 		$assignedtouser=GETPOST("affectedtouser")?GETPOST("affectedtouser"):(! empty($object->usertodo->id) && $object->usertodo->id > 0 ? $object->usertodo->id : $user->id);
 		$_SESSION['assignedtouser']=dol_json_encode(array($assignedtouser=>array('transparency'=>1,'mandatory'=>1)));
 	}
-	//print $form->select_dolusers_forevent('affectedto',1);
-	print $form->select_dolusers(GETPOST("affectedto")?GETPOST("affectedto"):(! empty($object->usertodo->id) && $object->usertodo->id > 0 ? $object->usertodo->id : $user->id),'affectedto',1);
+	print $form->select_dolusers_forevent(($action=='create'?'add':'update'),'affectedto',1);
+	//print $form->select_dolusers(GETPOST("affectedto")?GETPOST("affectedto"):(! empty($object->usertodo->id) && $object->usertodo->id > 0 ? $object->usertodo->id : $user->id),'affectedto',1);
 	print '</td></tr>';
 
 	print '</table>';
@@ -599,7 +663,7 @@ if ($action == 'create')
 	print '</td></tr>';
 
 	// Realised by
-	if ($conf->global->AGENDA_ENABLE_DONEBY)
+	if (! empty($conf->global->AGENDA_ENABLE_DONEBY))
 	{
 		print '<tr><td class="nowrap">'.$langs->trans("ActionDoneBy").'</td><td>';
 		print $form->select_dolusers(GETPOST("doneby")?GETPOST("doneby"):(! empty($object->userdone->id) && $percent==100?$object->userdone->id:0),'doneby',1);
@@ -704,11 +768,6 @@ if ($action == 'create')
 // View or edit
 if ($id > 0)
 {
-	if ($error)
-	{
-		dol_htmloutput_errors($error);
-	}
-
 	$result=$object->fetch($id);
 	$object->fetch_optionals($id,$extralabels);
 
@@ -833,7 +892,14 @@ if ($id > 0)
 
 		// Assigned to
 		print '<tr><td class="nowrap">'.$langs->trans("ActionAffectedTo").'</td><td colspan="3">';
-		print $form->select_dolusers($object->usertodo->id>0?$object->usertodo->id:-1,'affectedto',1);
+		$listofuserid=array();
+		if (empty($donotclearsession))
+		{
+			if (is_object($object->usertodo)) $listofuserid[$object->usertodo->id]=array('id'=>$object->usertodo->id,'transparency'=>$object->transparency);
+			$_SESSION['assignedtouser']=dol_json_encode($listofuserid);
+		}
+		print $form->select_dolusers_forevent(($action=='create'?'add':'update'),'affectedto',1);
+		//print $form->select_dolusers($object->usertodo->id>0?$object->usertodo->id:-1,'affectedto',1);
 		print '</td></tr>';
 
         print '</table><br><br><table class="border" width="100%">';
@@ -844,7 +910,7 @@ if ($id > 0)
 		print '</td></tr>';
 
 		// Realised by
-		if ($conf->global->AGENDA_ENABLE_DONEBY)
+		if (! empty($conf->global->AGENDA_ENABLE_DONEBY))
 		{
 			print '<tr><td class="nowrap">'.$langs->trans("ActionDoneBy").'</td><td colspan="3">';
 			print $form->select_dolusers($object->userdone->id> 0?$object->userdone->id:-1,'doneby',1);
diff --git a/htdocs/compta/facture/class/facture.class.php b/htdocs/compta/facture/class/facture.class.php
index c46f48ed1c6..c4b548c7e81 100644
--- a/htdocs/compta/facture/class/facture.class.php
+++ b/htdocs/compta/facture/class/facture.class.php
@@ -484,7 +484,7 @@ class Facture extends CommonInvoice
 
                     // Call trigger
                     $result=$this->call_trigger('BILL_CREATE',$user);
-                    if ($result < 0) $error++;             
+                    if ($result < 0) $error++;
                     // End call triggers
 
 					if (! $error)
@@ -593,8 +593,8 @@ class Facture extends CommonInvoice
 
 		// Load source object
 		$objFrom = dol_clone($this);
-		
-		
+
+
 
 		// Change socid if needed
 		if (! empty($socid) && $socid != $this->socid)
@@ -637,7 +637,7 @@ class Facture extends CommonInvoice
 				unset($this->products[$i]);	// Tant que products encore utilise
 			}
 		}
-		
+
 		// Create clone
 		$result=$this->create($user);
 		if ($result < 0) $error++;
@@ -645,7 +645,7 @@ class Facture extends CommonInvoice
 			// copy internal contacts
 			if ($this->copy_linked_contact($objFrom, 'internal') < 0)
 				$error++;
-			
+
 			// copy external contacts if same company
 			elseif ($objFrom->socid == $this->socid)
 			{
@@ -667,7 +667,7 @@ class Facture extends CommonInvoice
 
             // Call trigger
             $result=$this->call_trigger('BILL_CLONE',$user);
-            if ($result < 0) $error++;             
+            if ($result < 0) $error++;
             // End call triggers
 		}
 
@@ -1108,7 +1108,7 @@ class Facture extends CommonInvoice
 			{
 	            // Call trigger
 	            $result=$this->call_trigger('BILL_MODIFY',$user);
-	            if ($result < 0) $error++;             
+	            if ($result < 0) $error++;
 	            // End call triggers
 			}
 		}
@@ -1266,7 +1266,7 @@ class Facture extends CommonInvoice
 		{
             // Call trigger
             $result=$this->call_trigger('BILL_DELETE',$user);
-            if ($result < 0) $error++;             
+            if ($result < 0) $error++;
             // End call triggers
 		}
 
@@ -1438,7 +1438,7 @@ class Facture extends CommonInvoice
 			{
 	            // Call trigger
 	            $result=$this->call_trigger('BILL_PAYED',$user);
-	            if ($result < 0) $error++;             
+	            if ($result < 0) $error++;
 	            // End call triggers
 			}
 			else
@@ -1490,7 +1490,7 @@ class Facture extends CommonInvoice
 		{
             // Call trigger
             $result=$this->call_trigger('BILL_UNPAYED',$user);
-            if ($result < 0) $error++;             
+            if ($result < 0) $error++;
             // End call triggers
 		}
 		else
@@ -1554,7 +1554,7 @@ class Facture extends CommonInvoice
 	            // Call trigger
 	            $result=$this->call_trigger('BILL_CANCEL',$user);
 	            if ($result < 0)
-	            {             
+	            {
 					$this->db->rollback();
 					return -1;
 				}
@@ -1768,11 +1768,11 @@ class Facture extends CommonInvoice
 			{
 	            // Call trigger
 	            $result=$this->call_trigger('BILL_VALIDATE',$user);
-	            if ($result < 0) $error++;    
-	            //TODO: Restoring ref, facnumber, statut, brouillon to previous value if trigger fail           
+	            if ($result < 0) $error++;
+	            //TODO: Restoring ref, facnumber, statut, brouillon to previous value if trigger fail
 	            // End call triggers
 			}
-			
+
 			// Set new ref and define current statut
 			if (! $error)
 			{
@@ -1856,7 +1856,7 @@ class Facture extends CommonInvoice
 				$this->statut = 0;
 	            // Call trigger
 	            $result=$this->call_trigger('BILL_UNVALIDATE',$user);
-	            if ($result < 0)              
+	            if ($result < 0)
 				{
 					$error++;
 					$this->statut=$old_statut;
@@ -3164,7 +3164,7 @@ class Facture extends CommonInvoice
 		$sql.= ' p.ref as product_ref, p.fk_product_type, p.label as product_label,';
 		$sql.= ' p.description as product_desc';
 		$sql.= ' FROM '.MAIN_DB_PREFIX.'facturedet as l';
-		$sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'product p ON l.fk_product=p.rowid';
+		$sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON l.fk_product=p.rowid';
 		$sql.= ' WHERE l.fk_facture = '.$this->id;
 		$sql.= ' ORDER BY l.rang ASC, l.rowid';
 
@@ -3550,7 +3550,7 @@ class FactureLigne  extends CommonInvoiceLine
                 // Call trigger
                 $result=$this->call_trigger('LINEBILL_INSERT',$user);
                 if ($result < 0)
-                {            
+                {
 					$this->db->rollback();
 					return -2;
 				}
@@ -3661,7 +3661,7 @@ class FactureLigne  extends CommonInvoiceLine
 			{
                 // Call trigger
                 $result=$this->call_trigger('LINEBILL_UPDATE',$user);
-                if ($result < 0)            
+                if ($result < 0)
  				{
 					$this->db->rollback();
 					return -2;
@@ -3691,7 +3691,7 @@ class FactureLigne  extends CommonInvoiceLine
 		$error=0;
 
 		$this->db->begin();
-		
+
 		// Call trigger
 		$result=$this->call_trigger('LINEBILL_DELETE',$user);
 		if ($result < 0)
@@ -3700,7 +3700,7 @@ class FactureLigne  extends CommonInvoiceLine
 			return -1;
 		}
 		// End call triggers
-		
+
 
 		$sql = "DELETE FROM ".MAIN_DB_PREFIX."facturedet WHERE rowid = ".$this->rowid;
 		dol_syslog(get_class($this)."::delete", LOG_DEBUG);
diff --git a/htdocs/compta/journal/purchasesjournal.php b/htdocs/compta/journal/purchasesjournal.php
index 81ceeb6f39d..169fb32010a 100644
--- a/htdocs/compta/journal/purchasesjournal.php
+++ b/htdocs/compta/journal/purchasesjournal.php
@@ -104,11 +104,11 @@ $sql.= " fd.total_ttc, fd.tva_tx, fd.total_ht, fd.tva as total_tva, fd.product_t
 $sql.= " s.rowid as socid, s.nom as name, s.code_compta_fournisseur,";
 $sql.= " p.rowid as pid, p.ref as ref, p.accountancy_code_buy,";
 $sql.= " ct.accountancy_code_buy as account_tva, ct.recuperableonly";
-$sql.= " FROM ".MAIN_DB_PREFIX."facture_fourn_det fd";
-$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."c_tva ct ON fd.tva_tx = ct.taux AND fd.info_bits = ct.recuperableonly AND ct.fk_pays = '".$idpays."'";
-$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."product p ON p.rowid = fd.fk_product";
-$sql.= " JOIN ".MAIN_DB_PREFIX."facture_fourn f ON f.rowid = fd.fk_facture_fourn";
-$sql.= " JOIN ".MAIN_DB_PREFIX."societe s ON s.rowid = f.fk_soc" ;
+$sql.= " FROM ".MAIN_DB_PREFIX."facture_fourn_det as fd";
+$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."c_tva as ct ON fd.tva_tx = ct.taux AND fd.info_bits = ct.recuperableonly AND ct.fk_pays = '".$idpays."'";
+$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."product as p ON p.rowid = fd.fk_product";
+$sql.= " JOIN ".MAIN_DB_PREFIX."facture_fourn as f ON f.rowid = fd.fk_facture_fourn";
+$sql.= " JOIN ".MAIN_DB_PREFIX."societe as s ON s.rowid = f.fk_soc" ;
 $sql.= " WHERE f.fk_statut > 0 AND f.entity = ".$conf->entity;
 if (! empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) $sql.= " AND f.type IN (0,1,2)";
 else $sql.= " AND f.type IN (0,1,2,3)";
diff --git a/htdocs/compta/journal/sellsjournal.php b/htdocs/compta/journal/sellsjournal.php
index 6e65446bdda..f90add66b54 100644
--- a/htdocs/compta/journal/sellsjournal.php
+++ b/htdocs/compta/journal/sellsjournal.php
@@ -105,10 +105,10 @@ $sql.= " fd.product_type, fd.total_ht, fd.total_tva, fd.tva_tx, fd.total_ttc, fd
 $sql.= " s.rowid as socid, s.nom as name, s.code_compta, s.client,";
 $sql.= " p.rowid as pid, p.ref as pref, p.accountancy_code_sell,";
 $sql.= " ct.accountancy_code_sell as account_tva, ct.recuperableonly";
-$sql.= " FROM ".MAIN_DB_PREFIX."facturedet fd";
-$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."product p ON p.rowid = fd.fk_product";
-$sql.= " JOIN ".MAIN_DB_PREFIX."facture f ON f.rowid = fd.fk_facture";
-$sql.= " JOIN ".MAIN_DB_PREFIX."societe s ON s.rowid = f.fk_soc";
+$sql.= " FROM ".MAIN_DB_PREFIX."facturedet as fd";
+$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."product as p ON p.rowid = fd.fk_product";
+$sql.= " JOIN ".MAIN_DB_PREFIX."facture as f ON f.rowid = fd.fk_facture";
+$sql.= " JOIN ".MAIN_DB_PREFIX."societe as s ON s.rowid = f.fk_soc";
 $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."c_tva ct ON fd.tva_tx = ct.taux AND fd.info_bits = ct.recuperableonly AND ct.fk_pays = '".$idpays."'";
 $sql.= " WHERE f.entity = ".$conf->entity;
 $sql.= " AND f.fk_statut > 0";
diff --git a/htdocs/core/class/html.form.class.php b/htdocs/core/class/html.form.class.php
index 153ff769eb6..331705f0b3f 100644
--- a/htdocs/core/class/html.form.class.php
+++ b/htdocs/core/class/html.form.class.php
@@ -1289,7 +1289,7 @@ class Form
      * 	@return	string					HTML select string
      *  @see select_dolgroups
      */
-    function select_dolusers_forevent($htmlname='userid', $show_empty=0, $exclude='', $disabled=0, $include='', $enableonly='', $force_entity=0, $maxlength=0, $showstatus=0, $morefilter='')
+    function select_dolusers_forevent($action='', $htmlname='userid', $show_empty=0, $exclude='', $disabled=0, $include='', $enableonly='', $force_entity=0, $maxlength=0, $showstatus=0, $morefilter='')
     {
         global $conf,$user,$langs;
 
@@ -1297,18 +1297,24 @@ class Form
 
         // Method with no ajax
         //$out.='<form method="POST" action="'.$_SERVER["PHP_SELF"].'">';
-		$out.=$this->select_dolusers('', $htmlname, $show_empty, $exclude, $disabled, $include, $enableonly, $force_entity, $maxlength, $showstatus, $morefilter);
-		$out.='<input type="submit" class="button" name="addassignedtouser" value="'.dol_escape_htmltag($langs->trans("Add")).'">';
+		$out.='<input type="hidden" class="removedassignedhidden" name="removedassigned" value="">';
+		$out.='<script type="text/javascript" language="javascript">jQuery(document).ready(function () {    jQuery(".removedassigned").click(function() {        jQuery(".removedassignedhidden").val(jQuery(this).val());    });})</script>';
+        $out.=$this->select_dolusers('', $htmlname, $show_empty, $exclude, $disabled, $include, $enableonly, $force_entity, $maxlength, $showstatus, $morefilter);
+		$out.='<input type="submit" class="button" name="'.$action.'assignedtouser" value="'.dol_escape_htmltag($langs->trans("Add")).'">';
 		$assignedtouser=array();
 		if (!empty($_SESSION['assignedtouser'])) $assignedtouser=dol_json_decode($_SESSION['assignedtouser'], true);
 		if (count($assignedtouser)) $out.='<br>';
+		$i=0;
 		foreach($assignedtouser as $key => $value)
 		{
 			$userstatic->fetch($key);
 			$out.=$userstatic->getNomUrl(1);
+			if ($i == 0) $out.=' ('.$langs->trans("Owner").')';
+			$out.=' <input type="image" style="border: 0px;" src="'.img_picto($langs->trans("Remove"), 'delete', '', 0, 1).'" value="'.$userstatic->id.'" class="removedassigned" id="removedassigned_'.$userstatic->id.'" name="removedassigned_'.$userstatic->id.'">';
 			//$out.=' '.($value['mandatory']?$langs->trans("Mandatory"):$langs->trans("Optional"));
 			//$out.=' '.($value['transparency']?$langs->trans("Busy"):$langs->trans("NotBusy"));
 			$out.='<br>';
+			$i++;
 		}
 
 		//$out.='</form>';
diff --git a/htdocs/product/stock/class/entrepot.class.php b/htdocs/product/stock/class/entrepot.class.php
index 78d4cc55a4e..9d1c138bc5f 100644
--- a/htdocs/product/stock/class/entrepot.class.php
+++ b/htdocs/product/stock/class/entrepot.class.php
@@ -186,12 +186,12 @@ class Entrepot extends CommonObject
 
 		$sql = "DELETE FROM ".MAIN_DB_PREFIX."stock_mouvement";
 		$sql.= " WHERE fk_entrepot = " . $this->id;
-		dol_syslog("Entrepot::delete", LOG_DEBUG);
+		dol_syslog(get_class($this)."::delete", LOG_DEBUG);
 		$resql1=$this->db->query($sql);
 
 		$sql = "DELETE FROM ".MAIN_DB_PREFIX."product_stock";
 		$sql.= " WHERE fk_entrepot = " . $this->id;
-		dol_syslog("Entrepot::delete", LOG_DEBUG);
+		dol_syslog(get_class($this)."::delete", LOG_DEBUG);
 		$resql2=$this->db->query($sql);
 
 		if ($resql1 && $resql2)
@@ -202,8 +202,8 @@ class Entrepot extends CommonObject
 			dol_syslog(get_class($this)."::delete", LOG_DEBUG);
 			$resql1=$this->db->query($sql);
 
-			// Update denormalized fields because we change content of produt_stock
-			$sql = "UPDATE ".MAIN_DB_PREFIX."product p SET p.stock= (SELECT SUM(ps.reel) FROM ".MAIN_DB_PREFIX."product_stock ps WHERE ps.fk_product = p.rowid)";
+			// Update denormalized fields because we change content of produt_stock. Warning: Do not use "SET p.stock", does not works with pgsql
+			$sql = "UPDATE ".MAIN_DB_PREFIX."product as p SET stock = (SELECT SUM(ps.reel) FROM ".MAIN_DB_PREFIX."product_stock as ps WHERE ps.fk_product = p.rowid)";
 
 			dol_syslog(get_class($this)."::delete", LOG_DEBUG);
 			$resql2=$this->db->query($sql);
@@ -217,7 +217,7 @@ class Entrepot extends CommonObject
 			{
 				$this->db->rollback();
 				$this->error=$this->db->lasterror();
-				return -1;
+				return -2;
 			}
 		}
 		else
@@ -540,7 +540,7 @@ class Entrepot extends CommonObject
         $this->description = 'WAREHOUSE SPECIMEN '.dol_print_date($now,'dayhourlog');
 		$this->statut=1;
         $this->specimen=1;
-		
+
 		$this->lieu='Location test';
         $this->address='21 jump street';
         $this->zip='99999';
diff --git a/htdocs/product/stock/fiche.php b/htdocs/product/stock/fiche.php
index 86e930d8cf1..1155ef6cb40 100644
--- a/htdocs/product/stock/fiche.php
+++ b/htdocs/product/stock/fiche.php
@@ -261,7 +261,7 @@ else
 
 			// Country
 			print '<tr><td>'.$langs->trans('Country').'</td><td colspan="3">';
-			if (! empty($object->country_code)) 
+			if (! empty($object->country_code))
 			{
 				$img=picto_from_langcode($object->country_code);
 				print ($img?$img.' ':'');
@@ -339,14 +339,14 @@ else
 						print "<a class=\"butAction\" href=\"fiche.php?action=edit&id=".$object->id."\">".$langs->trans("Modify")."</a>";
 					else
 						print "<a class=\"butActionRefused\" href=\"#\">".$langs->trans("Modify")."</a>";
-	
+
 					if ($user->rights->stock->supprimer)
 						print "<a class=\"butActionDelete\" href=\"fiche.php?action=delete&id=".$object->id."\">".$langs->trans("Delete")."</a>";
 					else
 						print "<a class=\"butActionRefused\" href=\"#\">".$langs->trans("Delete")."</a>";
 				}
 			}
-			
+
 			print "</div>";
 
 
@@ -375,7 +375,7 @@ else
 
 			$sql = "SELECT p.rowid as rowid, p.ref, p.label as produit, p.fk_product_type as type, p.pmp as ppmp, p.price, p.price_ttc,";
 			$sql.= " ps.pmp, ps.reel as value";
-			$sql.= " FROM ".MAIN_DB_PREFIX."product_stock ps, ".MAIN_DB_PREFIX."product p";
+			$sql.= " FROM ".MAIN_DB_PREFIX."product_stock as ps, ".MAIN_DB_PREFIX."product as p";
 			$sql.= " WHERE ps.fk_product = p.rowid";
 			$sql.= " AND ps.reel <> 0";	// We do not show if stock is 0 (no product in this warehouse)
 			$sql.= " AND ps.fk_entrepot = ".$object->id;
diff --git a/test/phpunit/AllTests.php b/test/phpunit/AllTests.php
index d8dda85f2fc..a8a554ca25d 100644
--- a/test/phpunit/AllTests.php
+++ b/test/phpunit/AllTests.php
@@ -123,14 +123,21 @@ class AllTests
         require_once dirname(__FILE__).'/DiscountTest.php';
         $suite->addTestSuite('DiscountTest');
 
+        require_once dirname(__FILE__).'/ContratTest.php';
+        $suite->addTestSuite('ContratTest');
+
+        require_once dirname(__FILE__).'/FichinterTest.php';
+        $suite->addTestSuite('FichinterTest');
+
+        require_once dirname(__FILE__).'/PropalTest.php';
+        $suite->addTestSuite('PropalTest');
+
         require_once dirname(__FILE__).'/CommandeTest.php';
         $suite->addTestSuite('CommandeTest');
 
         require_once dirname(__FILE__).'/CommandeFournisseurTest.php';
         $suite->addTestSuite('CommandeFournisseurTest');
 
-        require_once dirname(__FILE__).'/ContratTest.php';
-        $suite->addTestSuite('ContratTest');
         require_once dirname(__FILE__).'/FactureTest.php';
         $suite->addTestSuite('FactureTest');
         require_once dirname(__FILE__).'/FactureRecTest.php';
@@ -139,8 +146,7 @@ class AllTests
         $suite->addTestSuite('FactureTestRounding');
         require_once dirname(__FILE__).'/FactureFournisseurTest.php';
         $suite->addTestSuite('FactureFournisseurTest');
-        require_once dirname(__FILE__).'/PropalTest.php';
-        $suite->addTestSuite('PropalTest');
+
 		require_once dirname(__FILE__).'/UserTest.php';
         $suite->addTestSuite('UserTest');
 		require_once dirname(__FILE__).'/UserGroupTest.php';
-- 
GitLab