From 21d35ccda8197e215fee2226731b83da86a42763 Mon Sep 17 00:00:00 2001
From: Juanjo Menent <jmenent@2byte.es>
Date: Tue, 7 Dec 2010 20:57:34 +0000
Subject: [PATCH] Task #10786: works for internal users in setup module

---
 htdocs/admin/prelevement.php                  | 73 ++++++++++++++-----
 .../class/bon-prelevement.class.php           | 11 +--
 htdocs/install/mysql/data/llx_action_def.sql  |  3 +-
 htdocs/langs/ca_ES/other.lang                 |  3 +
 htdocs/langs/en_US/other.lang                 |  3 +
 htdocs/langs/es_AR/other.lang                 |  3 +
 htdocs/langs/es_ES/other.lang                 |  3 +
 htdocs/langs/fr_FR/other.lang                 |  3 +
 8 files changed, 76 insertions(+), 26 deletions(-)

diff --git a/htdocs/admin/prelevement.php b/htdocs/admin/prelevement.php
index df4901c4bf3..606037cc33b 100644
--- a/htdocs/admin/prelevement.php
+++ b/htdocs/admin/prelevement.php
@@ -56,7 +56,7 @@ if ($_GET["action"] == "set")
 if ($_GET["action"] == "addnotif")
 {
 	$bon = new BonPrelevement($db);
-	$bon->AddNotification($_POST["user"],$_POST["action"]);
+	$bon->AddNotification($db,$_POST["user"],$_POST["action"]);
 
 	Header("Location: prelevement.php");
 	exit;
@@ -173,21 +173,33 @@ if ($conf->global->MAIN_MODULE_NOTIFICATION)
 {
 	$langs->load("mails");
 	print_titre($langs->trans("Notifications"));
-
-	print '<form method="post" action="'.$_SERVER["PHP_SELF"].'?action=addnotif">';
-	print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
-	print '<table class="noborder" width="100%">';
-	print '<tr class="liste_titre">';
-	print '<td>'.$langs->trans("User").'</td>';
-	print '<td>'.$langs->trans("Value").'</td>';
-	print '<td align="right">'.$langs->trans("Action").'</td>';
-	print "</tr>\n";
-	print '<tr class="impair"><td align="left">';
-    print $html->select_users(0,'user',0);
-    print '</td>';
-	print '<td>';
-	print '<select name="action">';
-    $sql = "SELECT rowid, code, titre";
+	
+	$sql = "SELECT rowid, name, firstname, fk_societe, email";
+	$sql.= " FROM ".MAIN_DB_PREFIX."user";
+	$sql.= " WHERE entity IN (0,".$conf->entity.")";
+	
+	$resql=$db->query($sql);
+	if ($resql)
+	{
+		$num = $db->num_rows($resql);
+		$var = true;
+		$i = 0;
+		while ($i < $num)
+		{
+			$obj = $db->fetch_object($resql);
+			$var=!$var;
+			if (!$obj->fk_societe)
+			{
+				$username= $obj->firstname.' '.$obj->name;
+				$internalusers[$obj->rowid] = $username;
+			}
+				
+			$i++;
+		}
+		$db->free($resql);
+	}
+	
+	$sql = "SELECT rowid, code, titre";
     $sql.= " FROM ".MAIN_DB_PREFIX."action_def";
     $sql.= " WHERE objet_type = 'withdraw'";
     $resql = $db->query($sql);
@@ -199,11 +211,31 @@ if ($conf->global->MAIN_MODULE_NOTIFICATION)
         while ($i < $num)
         {
             $obj = $db->fetch_object($resql);
-            print '<option value="'.$obj->code.'">'.$obj->titre.'</option>';
+            $label=($langs->trans("Notify_".$obj->code)!="Notify_".$obj->code?$langs->trans("Notify_".$obj->code):$obj->titre);
+            $actions[$obj->rowid]=$label;
             $i++;
         }
+        $db->free($resql);
     }
-	print '</select></td>';
+	
+
+	print '<form method="post" action="'.$_SERVER["PHP_SELF"].'?action=addnotif">';
+	print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
+	print '<table class="noborder" width="100%">';
+	print '<tr class="liste_titre">';
+	print '<td>'.$langs->trans("User").'</td>';
+	print '<td>'.$langs->trans("Value").'</td>';
+	print '<td align="right">'.$langs->trans("Action").'</td>';
+	print "</tr>\n";
+	
+	print '<tr class="impair"><td align="left">';
+    print $html->selectarray('user',$internalusers);//  select_users(0,'user',0);
+    print '</td>';
+    
+	print '<td>';
+	print $html->selectarray('action',$actions);//  select_users(0,'user',0);
+	print '</td>';
+	
 	print '<td align="right"><input type="submit" class="button" value="'.$langs->trans("Add").'"></td></tr>';
 }
 // List of current notifications for objet_type='withdraw'
@@ -212,7 +244,7 @@ $sql.= ", nd.rowid, ad.code, ad.titre";
 $sql.= " FROM ".MAIN_DB_PREFIX."user as u";
 $sql.= ", ".MAIN_DB_PREFIX."notify_def as nd";
 $sql.= ", ".MAIN_DB_PREFIX."action_def as ad";
-$sql.= " WHERE u.rowid = nd.fk_soc AND nd.fk_action = ad.rowid";
+$sql.= " WHERE u.rowid = nd.fk_user AND nd.fk_action = ad.rowid";
 $sql.= " AND ad.objet_type = 'withdraw'";
 $sql.= " AND u.entity IN (0,".$conf->entity.")";
 $resql = $db->query($sql);
@@ -228,7 +260,8 @@ if ($resql)
 
 		print "<tr $bc[$var]>";
 		print '<td>'.$obj->firstname." ".$obj->name.'</td>';
-		print '<td>'.$obj->titre.'</td>';
+		$label=($langs->trans("Notify_".$obj->code)!="Notify_".$obj->code?$langs->trans("Notify_".$obj->code):$obj->titre);
+		print '<td>'.$label.'</td>';
 		print '<td align="right"><a href="'.$_SERVER["PHP_SELF"].'?action=deletenotif&amp;notif='.$obj->rowid.'">'.img_delete().'</a></td>';
 		print '</tr>';
 		$i++;
diff --git a/htdocs/compta/prelevement/class/bon-prelevement.class.php b/htdocs/compta/prelevement/class/bon-prelevement.class.php
index cc4ec01f4ee..02adf6ee79c 100644
--- a/htdocs/compta/prelevement/class/bon-prelevement.class.php
+++ b/htdocs/compta/prelevement/class/bon-prelevement.class.php
@@ -1145,7 +1145,7 @@ class BonPrelevement extends CommonObject
         $result = 0;
 
         $sql = "DELETE FROM ".MAIN_DB_PREFIX."notify_def";
-		$sql .= " WHERE fk_soc=".$user." AND AND fk_action=".$action;
+		$sql .= " WHERE fk_user=".$user." AND fk_action=".$action;
 
         if ($this->db->query($sql))
         {
@@ -1163,15 +1163,16 @@ class BonPrelevement extends CommonObject
 	*	@param	action	notification action
 	*	@return	int		0 if OK, <0 if KO
 	*/
-    function AddNotification($user, $action)
+    function AddNotification($db, $user, $action)
     {
         $result = 0;
 
         if ($this->DeleteNotification($user, $action) == 0)
         {
-        	$sql = "INSERT INTO ".MAIN_DB_PREFIX."notify_def (datec,fk_soc, fk_contact, fk_action)";
-			$sql .= " VALUES (".$db->idate(mktime()).",".$user.", '',".$action.")";
-
+        	$sql = "INSERT INTO ".MAIN_DB_PREFIX."notify_def (datec,fk_user, fk_soc, fk_contact, fk_action)";
+			$sql .= " VALUES (".$db->idate(mktime()).",".$user.", 'NULL', 'NULL', ".$action.")";
+			
+            dol_syslog("adnotiff: ".$sql);
             if ($this->db->query($sql))
             {
                 $result = 0;
diff --git a/htdocs/install/mysql/data/llx_action_def.sql b/htdocs/install/mysql/data/llx_action_def.sql
index dcb4d6241f8..403c69f9ac2 100644
--- a/htdocs/install/mysql/data/llx_action_def.sql
+++ b/htdocs/install/mysql/data/llx_action_def.sql
@@ -5,7 +5,7 @@
 -- Copyright (C) 2004      Guillaume Delecourt  <guillaume.delecourt@opensides.be>
 -- Copyright (C) 2005-2009 Regis Houssin        <regis@dolibarr.fr>
 -- Copyright (C) 2007 	   Patrick Raguin       <patrick.raguin@gmail.com>
--- Copyright (C) 2010 	   Juanjo Menentn       <jmenent@2byte.es>
+-- Copyright (C) 2010 	   Juanjo Menent        <jmenent@2byte.es>
 --
 -- 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
@@ -41,3 +41,4 @@ insert into llx_action_def (rowid,code,titre,description,objet_type) values (5,'
 insert into llx_action_def (rowid,code,titre,description,objet_type) values (6,'NOTIFY_VAL_PROPAL','Validation proposition client','Executed when a commercial proposal is validated','propal');
 insert into llx_action_def (rowid,code,titre,description,objet_type) values (7,'NOTIFY_TRN_WITHDRAW','Transmission prélèvement','Executed when a withdrawal is transmited','withdraw');
 insert into llx_action_def (rowid,code,titre,description,objet_type) values (8,'NOTIFY_CRD_WITHDRAW','Créditer prélèvement','Executed when a withdrawal is credited','withdraw');
+insert into llx_action_def (rowid,code,titre,description,objet_type) values (9,'NOTIFY_EMT_WITHDRAW','Emission prélèvement','Executed when a withdrawal is emited','withdraw');
diff --git a/htdocs/langs/ca_ES/other.lang b/htdocs/langs/ca_ES/other.lang
index 4721d5d2fb8..47329259166 100644
--- a/htdocs/langs/ca_ES/other.lang
+++ b/htdocs/langs/ca_ES/other.lang
@@ -31,6 +31,9 @@ Notify_NOTIFY_APP_ORDER_SUPPLIER=Aprovació comanda a proveïdor
 Notify_NOTIFY_REF_ORDER_SUPPLIER=Rebuig comanda a proveïdor
 Notify_NOTIFY_VAL_ORDER=Validació comanda client
 Notify_NOTIFY_VAL_PROPAL=Validació pressupost client
+Notify_NOTIFY_TRN_WITHDRAW=Transmissió domiciliació
+Notify_NOTIFY_CRD_WITHDRAW=Abonament domiciliació
+Notify_NOTIFY_EMT_WITHDRAW=Emissió domiciliació
 NbOfAttachedFiles=Número arxius/documents adjunts
 TotalSizeOfAttachedFiles=Mida total dels arxius/documents adjunts
 MaxSize=Tamany màxim
diff --git a/htdocs/langs/en_US/other.lang b/htdocs/langs/en_US/other.lang
index 51d6dc469f5..9c507f334e8 100644
--- a/htdocs/langs/en_US/other.lang
+++ b/htdocs/langs/en_US/other.lang
@@ -31,6 +31,9 @@ Notify_NOTIFY_APP_ORDER_SUPPLIER=Supplier order approved
 Notify_NOTIFY_REF_ORDER_SUPPLIER=Supplier order refused
 Notify_NOTIFY_VAL_ORDER=Customer order validated
 Notify_NOTIFY_VAL_PROPAL=Customer proposal validated
+Notify_NOTIFY_TRN_WITHDRAW=Transmission withdrawal
+Notify_NOTIFY_CRD_WITHDRAW=Credit withdrawal
+Notify_NOTIFY_EMT_WITHDRAW=Isue withdrawal
 NbOfAttachedFiles=Number of attached files/documents
 TotalSizeOfAttachedFiles=Total size of attached files/documents
 MaxSize=Maximum size
diff --git a/htdocs/langs/es_AR/other.lang b/htdocs/langs/es_AR/other.lang
index baf643fbee0..d26ac0af5b5 100755
--- a/htdocs/langs/es_AR/other.lang
+++ b/htdocs/langs/es_AR/other.lang
@@ -31,6 +31,9 @@ Notify_NOTIFY_APP_ORDER_SUPPLIER=Aprobación pedido a proveedor
 Notify_NOTIFY_REF_ORDER_SUPPLIER=Rechazo pedido a proveedor
 Notify_NOTIFY_VAL_ORDER=Validación pedido cliente
 Notify_NOTIFY_VAL_PROPAL=Validación presupuesto cliente
+Notify_NOTIFY_TRN_WITHDRAW=Transmisión domiciliación
+Notify_NOTIFY_CRD_WITHDRAW=Abono domiciliación
+Notify_NOTIFY_EMT_WITHDRAW=Emisión domiciliación
 NbOfAttachedFiles=Número archivos/documentos adjuntos
 TotalSizeOfAttachedFiles=Tamaño total de los archivos/documentos adjuntos
 MaxSize=Tamaño máximo
diff --git a/htdocs/langs/es_ES/other.lang b/htdocs/langs/es_ES/other.lang
index 50050232afd..90848c97467 100644
--- a/htdocs/langs/es_ES/other.lang
+++ b/htdocs/langs/es_ES/other.lang
@@ -31,6 +31,9 @@ Notify_NOTIFY_APP_ORDER_SUPPLIER=Aprobación pedido a proveedor
 Notify_NOTIFY_REF_ORDER_SUPPLIER=Rechazo pedido a proveedor
 Notify_NOTIFY_VAL_ORDER=Validación pedido cliente
 Notify_NOTIFY_VAL_PROPAL=Validación presupuesto cliente
+Notify_NOTIFY_TRN_WITHDRAW=Transmisión domiciliación
+Notify_NOTIFY_CRD_WITHDRAW=Abono domiciliación
+Notify_NOTIFY_EMT_WITHDRAW=Emisión domiciliación
 NbOfAttachedFiles=Número archivos/documentos adjuntos
 TotalSizeOfAttachedFiles=Tamaño total de los archivos/documentos adjuntos
 MaxSize=Tamaño máximo
diff --git a/htdocs/langs/fr_FR/other.lang b/htdocs/langs/fr_FR/other.lang
index c8d739ff471..b2cfe350d36 100644
--- a/htdocs/langs/fr_FR/other.lang
+++ b/htdocs/langs/fr_FR/other.lang
@@ -31,6 +31,9 @@ Notify_NOTIFY_APP_ORDER_SUPPLIER=Approbation commande fournisseur
 Notify_NOTIFY_REF_ORDER_SUPPLIER=Refus commande fournisseur
 Notify_NOTIFY_VAL_ORDER=Validation commande client
 Notify_NOTIFY_VAL_PROPAL=Validation proposition commerciale client
+Notify_NOTIFY_TRN_WITHDRAW=Transmission prélèvement
+Notify_NOTIFY_CRD_WITHDRAW=Créditer prélèvement
+Notify_NOTIFY_EMT_WITHDRAW=Emission prélèvement
 NbOfAttachedFiles=Nombre de fichiers/documents liés
 TotalSizeOfAttachedFiles=Taille total fichiers/documents liés
 MaxSize=Taille maximum
-- 
GitLab