diff --git a/htdocs/core/class/html.form.class.php b/htdocs/core/class/html.form.class.php
index 70274040fd3..77f13357ac4 100644
--- a/htdocs/core/class/html.form.class.php
+++ b/htdocs/core/class/html.form.class.php
@@ -1463,7 +1463,7 @@ class Form
* @return string HTML select string
* @see select_dolgroups
*/
- function select_dolusers($selected='', $htmlname='userid', $show_empty=0, $exclude=null, $disabled=0, $include='', $enableonly='', $force_entity=0, $maxlength=0, $showstatus=0, $morefilter='', $show_every=0, $enableonlytext='', $morecss='', $noactive=0)
+ function select_dolusers($selected='', $htmlname='userid', $show_empty=0, $exclude=null, $disabled=0, $include='', $enableonly='', $force_entity=0, $maxlength=0, $showstatus=0, $morefilter='', $show_every=0, $enableonlytext='', $morecss='', $noactive=0, $outputmode=0)
{
global $conf,$user,$langs;
@@ -1489,6 +1489,7 @@ class Form
}
$out='';
+ $outarray = array();
// Forge request to select users
$sql = "SELECT DISTINCT u.rowid, u.lastname as lastname, u.firstname, u.statut, u.login, u.admin, u.entity";
@@ -1617,6 +1618,7 @@ class Form
$out.=' - '.$disableline; // This is text from $enableonlytext parameter
}
$out.= '';
+ $outarray[$userstatic->id] = $userstatic->getFullName($langs, $fullNameMode, -1, $maxlength);
$i++;
}
@@ -1633,6 +1635,7 @@ class Form
dol_print_error($this->db);
}
+ if ($outputmode) return $outarray;
return $out;
}
diff --git a/htdocs/societe/card.php b/htdocs/societe/card.php
index aafea674bcf..46595790c67 100644
--- a/htdocs/societe/card.php
+++ b/htdocs/societe/card.php
@@ -534,6 +534,15 @@ if (empty($reshook))
$error++;
}
}
+
+ // Links with users
+ $salesreps = GETPOST('commercial', 'array');
+ $result = $object->setSalesRep($salesreps);
+ if ($result < 0)
+ {
+ $error++;
+ setEventMessages($object->error, $object->errors, 'errors');
+ }
// Customer categories association
$custcats = GETPOST('custcats', 'array');
@@ -663,6 +672,15 @@ if (empty($reshook))
setEventMessages($object->error, $object->errors, 'errors');
$error++;
}
+
+ // Links with users
+ $salesreps = GETPOST('commercial', 'array');
+ $result = $object->setSalesRep($salesreps);
+ if ($result < 0)
+ {
+ $error++;
+ setEventMessages($object->error, $object->errors, 'errors');
+ }
// Prevent thirdparty's emptying if a user hasn't rights $user->rights->categorie->lire (in such a case, post of 'custcats' is not defined)
if (! $error && !empty($user->rights->categorie->lire))
@@ -1356,7 +1374,8 @@ else
print '
';
print '| '.fieldLabel('AllocateCommercial','commercial_id').' | ';
print '';
- print $form->select_dolusers((! empty($object->commercial_id)?$object->commercial_id:$user->id),'commercial_id',1); // Add current user by default
+ $userlist = $form->select_dolusers('', '', 0, null, 0, '', '', 0, 0, 0, '', 0, '', '', 0, 1);
+ print $form->multiselectarray('commercial', $userlist, GETPOST('commercial', 'array'), null, null, null, null, "90%");
print ' |
';
}
@@ -1926,6 +1945,16 @@ else
// Capital
print '| '.fieldLabel('Capital','capital').' | ';
print ' '.$langs->trans("Currency".$conf->currency).' |
';
+
+ // Assign a Name
+ print '';
+ print '| '.fieldLabel('AllocateCommercial','commercial_id').' | ';
+ print '';
+ $userlist = $form->select_dolusers('', '', 0, null, 0, '', '', 0, 0, 0, '', 0, '', '', 0, 1);
+ $arrayselected = GETPOST('commercial', 'array');
+ if(empty($arrayselected)) $arrayselected = $object->get_users();
+ print $form->multiselectarray('commercial', $userlist, $arrayselected, null, null, null, null, "90%");
+ print ' |
';
// Default language
if (! empty($conf->global->MAIN_MULTILANGS))
diff --git a/htdocs/societe/class/societe.class.php b/htdocs/societe/class/societe.class.php
index dbf3015ba1e..587665eb0b1 100644
--- a/htdocs/societe/class/societe.class.php
+++ b/htdocs/societe/class/societe.class.php
@@ -3895,6 +3895,73 @@ class Societe extends CommonObject
return $error ? -1 : 1;
}
+ /**
+ * Sets company to supplied users.
+ *
+ * @param int[]|int $users User ID or array of user IDs
+ * @return int <0 if KO, >0 if OK
+ */
+ public function setSalesRep($salesrep)
+ {
+ global $user;
+
+ // Handle single user
+ if (!is_array($salesrep)) {
+ $salesrep = array($salesrep);
+ }
+
+ // Get current users
+ $existing = $this->get_users();
+
+ // Diff
+ if (is_array($existing)) {
+ $to_del = array_diff($existing, $salesrep);
+ $to_add = array_diff($salesrep, $existing);
+ } else {
+ $to_del = array(); // Nothing to delete
+ $to_add = $salesrep;
+ }
+
+ $error = 0;
+
+ // Process
+ foreach ($to_del as $del) {
+ $this->del_commercial($user, $del);
+ }
+ foreach ($to_add as $add) {
+ $result = $this->add_commercial($user, $add);
+ if ($result < 0)
+ {
+ $error++;
+ $this->error = $c->error;
+ $this->errors = $c->errors;
+ break;
+ }
+ }
+
+ return $error ? -1 : 1;
+ }
+
+ /**
+ * Get all linked user to company
+ *
+ * @return Array
+ */
+ public function get_users() {
+ $sql = 'SELECT fk_user FROM '.MAIN_DB_PREFIX.'societe_commerciaux ';
+ $sql.= 'WHERE fk_soc = '.$this->id;
+
+ $resql = $this->db->query($sql);
+
+ $users = array();
+
+ while ($obj = $this->db->fetch_object($resql)) {
+ $users[] = $obj->fk_user;
+ }
+
+ return $users;
+ }
+
/**
* Function used to replace a thirdparty id with another one.
diff --git a/htdocs/societe/commerciaux.php b/htdocs/societe/commerciaux.php
deleted file mode 100644
index d6add5eacd5..00000000000
--- a/htdocs/societe/commerciaux.php
+++ /dev/null
@@ -1,300 +0,0 @@
-
- * Copyright (C) 2010 Laurent Destailleur
- * Copyright (C) 2010-2011 Regis Houssin
- *
- * 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
- * the Free Software Foundation; either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see .
- */
-
-/**
- * \file htdocs/societe/commerciaux.php
- * \ingroup societe
- * \brief Page of links to sales representatives
- */
-
-require '../main.inc.php';
-require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
-
-$langs->load("companies");
-$langs->load("commercial");
-$langs->load("customers");
-$langs->load("suppliers");
-$langs->load("banks");
-
-// Security check
-$socid = GETPOST('socid', 'int');
-if ($user->societe_id) $socid=$user->societe_id;
-$result = restrictedArea($user, 'societe','','');
-
-$hookmanager->initHooks(array('salesrepresentativescard','globalcard'));
-
-/*
- * Actions
- */
-
-if (! empty($socid) && $_GET["commid"])
-{
- $action = 'add';
-
- if ($user->rights->societe->creer)
- {
- $object = new Societe($db);
- $object->fetch($socid);
-
- $parameters=array('id'=>$_GET["commid"]);
- $reshook=$hookmanager->executeHooks('doActions',$parameters,$object,$action); // Note that $action and $object may have been modified by some hooks
- if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
-
- if (empty($reshook)) $object->add_commercial($user, $_GET["commid"]);
-
- header("Location: ".$_SERVER["PHP_SELF"]."?socid=".$object->id);
- exit;
- }
- else
- {
- header("Location: ".$_SERVER["PHP_SELF"]."?socid=".$socid);
- exit;
- }
-}
-
-if (! empty($socid) && $_GET["delcommid"])
-{
- $action = 'delete';
-
- if ($user->rights->societe->creer)
- {
- $object = new Societe($db);
- $object->fetch($socid);
-
- $parameters=array('id'=>$_GET["delcommid"]);
- $reshook=$hookmanager->executeHooks('doActions',$parameters,$object,$action); // Note that $action and $object may have been modified by some hooks
- if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
-
- if (empty($reshook)) $object->del_commercial($user, $_GET["delcommid"]);
-
- header("Location: commerciaux.php?socid=".$object->id);
- exit;
- }
- else
- {
- header("Location: ".$_SERVER["PHP_SELF"]."?socid=".$socid);
- exit;
- }
-}
-
-
-/*
- * View
- */
-
-$help_url='EN:Module_Third_Parties|FR:Module_Tiers|ES:Empresas';
-llxHeader('',$langs->trans("ThirdParty"),$help_url);
-
-$form = new Form($db);
-
-if (! empty($socid))
-{
- $object = new Societe($db);
- $result=$object->fetch($socid);
-
- $action='view';
-
- $head=societe_prepare_head2($object);
-
- dol_fiche_head($head, 'salesrepresentative', $langs->trans("ThirdParty"), -1, 'company');
-
- $linkback = ''.$langs->trans("BackToList").'';
-
- dol_banner_tab($object, 'socid', $linkback, ($user->societe_id?0:1), 'rowid', 'nom');
-
- print '';
-
- print '
';
- print '
';
-
- print '';
- print '| '.$langs->trans('CustomerCode').' | global->SOCIETE_USEPREFIX)?' colspan="3"':'').'>';
- print $object->code_client;
- if ($object->check_codeclient() <> 0) print ' '.$langs->trans("WrongCustomerCode");
- print ' | ';
- if (! empty($conf->global->SOCIETE_USEPREFIX)) // Old not used prefix field
- {
- print ''.$langs->trans('Prefix').' | '.$object->prefix_comm.' | ';
- }
- print '';
- print '
';
-
- // Liste les commerciaux
- print '| '.$langs->trans("SalesRepresentatives").' | ';
- print '';
-
- $sql = "SELECT DISTINCT u.rowid, u.login, u.fk_soc, u.lastname, u.firstname, u.statut, u.entity, u.photo";
- $sql .= " FROM ".MAIN_DB_PREFIX."user as u";
- $sql .= " , ".MAIN_DB_PREFIX."societe_commerciaux as sc";
- if (! empty($conf->multicompany->enabled) && ! empty($conf->global->MULTICOMPANY_TRANSVERSE_MODE))
- {
- $sql.= ", ".MAIN_DB_PREFIX."usergroup_user as ug";
- }
- $sql .= " WHERE sc.fk_soc =".$object->id;
- $sql .= " AND sc.fk_user = u.rowid";
- if (! empty($conf->multicompany->enabled) && ! empty($conf->global->MULTICOMPANY_TRANSVERSE_MODE))
- {
- $sql.= " AND ((ug.fk_user = sc.fk_user";
- $sql.= " AND ug.entity = ".$conf->entity.")";
- $sql.= " OR u.admin = 1)";
- }
- else
- $sql.= " AND u.entity IN (0,".$conf->entity.")";
-
- $sql .= " ORDER BY u.lastname ASC ";
-
- dol_syslog('societe/commerciaux.php::list salesman sql = '.$sql,LOG_DEBUG);
- $resql = $db->query($sql);
- if ($resql)
- {
- $num = $db->num_rows($resql);
- $i = 0;
-
- $tmpuser = new User($db);
-
- while ($i < $num)
- {
- $obj = $db->fetch_object($resql);
-
- $parameters=array('socid'=>$object->id);
- $reshook=$hookmanager->executeHooks('formObjectOptions',$parameters,$obj,$action); // Note that $action and $object may have been modified by hook
- print $hookmanager->resPrint;
- if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
-
- $tmpuser->id = $obj->rowid;
- $tmpuser->firstname = $obj->firstname;
- $tmpuser->lastname = $obj->lastname;
- $tmpuser->statut = $obj->statut;
- $tmpuser->login = $obj->login;
- $tmpuser->entity = $obj->entity;
- $tmpuser->societe_id = $obj->fk_soc;
- $tmpuser->photo = $obj->photo;
- print $tmpuser->getNomUrl(-1);
-
- /*print '';
- print img_object($langs->trans("ShowUser"),"user").' ';
- print dolGetFirstLastname($obj->firstname, $obj->lastname)."\n";
- print '';*/
- print ' ';
- if ($user->rights->societe->creer)
- {
- print 'id.'&delcommid='.$obj->rowid.'">';
- print img_delete();
- print '';
- }
- print ' ';
- $i++;
- }
-
- $db->free($resql);
- }
- else
- {
- dol_print_error($db);
- }
- if($i == 0) { print $langs->trans("NoSalesRepresentativeAffected"); }
-
- print " |
";
-
- print '
';
- print "
\n";
-
- dol_fiche_end();
-
-
- if ($user->rights->societe->creer && $user->rights->societe->client->voir)
- {
- /*
- * Liste
- *
- */
-
- $langs->load("users");
- $title=$langs->trans("ListOfUsers");
-
- $sql = "SELECT DISTINCT u.rowid, u.lastname, u.firstname, u.login, u.email, u.statut, u.fk_soc, u.photo";
- $sql.= " FROM ".MAIN_DB_PREFIX."user as u";
- if (! empty($conf->multicompany->enabled) && ! empty($conf->global->MULTICOMPANY_TRANSVERSE_MODE))
- {
- $sql.= ", ".MAIN_DB_PREFIX."usergroup_user as ug";
- $sql.= " WHERE ((ug.fk_user = u.rowid";
- $sql.= " AND ug.entity = ".$conf->entity.")";
- $sql.= " OR u.admin = 1)";
- }
- else
- $sql.= " WHERE u.entity IN (0,".$conf->entity.")";
- if (! empty($conf->global->USER_HIDE_INACTIVE_IN_COMBOBOX)) $sql.= " AND u.statut<>0 ";
- $sql.= " ORDER BY u.lastname ASC ";
-
- $resql = $db->query($sql);
- if ($resql)
- {
- $num = $db->num_rows($resql);
- $i = 0;
-
- print load_fiche_titre($title);
-
- // Lignes des titres
- print '';
- print '';
- print '| '.$langs->trans("Name").' | ';
- print ''.$langs->trans("Login").' | ';
- print ''.$langs->trans("Status").' | ';
- print ' | ';
- print "
\n";
-
- $var=True;
- $tmpuser=new User($db);
-
- while ($i < $num)
- {
- $obj = $db->fetch_object($resql);
-
- print "| ";
- $tmpuser->id=$obj->rowid;
- $tmpuser->firstname=$obj->firstname;
- $tmpuser->lastname=$obj->lastname;
- $tmpuser->statut=$obj->statut;
- $tmpuser->login=$obj->login;
- $tmpuser->email=$obj->email;
- $tmpuser->societe_id=$obj->fk_soc;
- $tmpuser->photo=$obj->photo;
- print $tmpuser->getNomUrl(-1);
- print ' | ';
- print ''.$obj->login.' | ';
- print ''.$tmpuser->getLibStatut(2).' | ';
- print 'id.'&commid='.$obj->rowid.'">'.$langs->trans("Add").' | ';
-
- print '
'."\n";
- $i++;
- }
-
- print "
";
- $db->free($resql);
- }
- else
- {
- dol_print_error($db);
- }
- }
-
-}
-
-llxFooter();
-$db->close();
diff --git a/htdocs/societe/tpl/linesalesrepresentative.tpl.php b/htdocs/societe/tpl/linesalesrepresentative.tpl.php
index 0714a547f22..2f325f3001f 100644
--- a/htdocs/societe/tpl/linesalesrepresentative.tpl.php
+++ b/htdocs/societe/tpl/linesalesrepresentative.tpl.php
@@ -24,18 +24,7 @@ if (empty($conf) || ! is_object($conf))
// Sale representative
print '';
-print '| ';
print $langs->trans('SalesRepresentatives');
-print ' | ';
-if ($user->rights->societe->creer && $user->rights->societe->client->voir)
-{
- print ''.img_edit('',1).'';
-}
-else
-{
- print ' ';
-}
-print ' | ';
print ' | ';
print '';
@@ -44,7 +33,6 @@ $nbofsalesrepresentative=count($listsalesrepresentatives);
if ($nbofsalesrepresentative > 0)
{
$userstatic=new User($db);
- $i=0;
foreach($listsalesrepresentatives as $val)
{
$userstatic->id=$val['id'];
@@ -56,27 +44,7 @@ if ($nbofsalesrepresentative > 0)
$userstatic->email=$val['email'];
$userstatic->entity=$val['entity'];
print $userstatic->getNomUrl(-1);
- $i++;
- if ($i < $nbofsalesrepresentative)
- {
- print ' ';
- if ($i >= 3) // We print only number
- {
- $userstatic->id=0;
- $userstatic->login='';
- $userstatic->lastname='';
- $userstatic->firstname='';
- $userstatic->statut=0;
- $userstatic->photo='';
- $userstatic->email='';
- $userstatic->entity=0;
- print '';
- print $userstatic->getNomUrl(-1, 'nolink', 0, 1);
- print '+'.($nbofsalesrepresentative - $i);
- print '';
- break;
- }
- }
+ print ' ';
}
}
else print ''.$langs->trans("NoSalesRepresentativeAffected").'';
|