From ee36b2ea435ede98b649f3f2964ddb800c9fa98a Mon Sep 17 00:00:00 2001
From: Laurent Destailleur <eldy@users.sourceforge.net>
Date: Thu, 11 Jun 2009 01:05:43 +0000
Subject: [PATCH] Fix: Pb with multicompany. If a project is not linked to a
 company, it was invisible.

---
 htdocs/projet/activity/index.php |  4 ++--
 htdocs/projet/index.php          | 38 ++++++++++++++++++--------------
 htdocs/projet/liste.php          |  2 +-
 mysql/migration/2.6.0-2.7.0.sql  |  7 ++++++
 mysql/tables/llx_projet.key.sql  | 27 +++++++++++++++++++++++
 mysql/tables/llx_projet.sql      |  6 ++---
 6 files changed, 60 insertions(+), 24 deletions(-)
 create mode 100644 mysql/tables/llx_projet.key.sql

diff --git a/htdocs/projet/activity/index.php b/htdocs/projet/activity/index.php
index 34d08c0aeb0..29abfaa2c92 100644
--- a/htdocs/projet/activity/index.php
+++ b/htdocs/projet/activity/index.php
@@ -71,7 +71,7 @@ if ($mode == 'mine') $sql.= ", ".MAIN_DB_PREFIX."projet_task_actors as pta";
 if (!$user->rights->societe->client->voir && !$socid) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
 $sql.= " WHERE t.fk_projet = p.rowid";
 $sql.= " AND p.fk_soc = s.rowid";
-$sql.= " AND s.entity = ".$conf->entity;
+$sql.= " AND p.entity = ".$conf->entity;
 if (!$user->rights->societe->client->voir && !$socid) $sql.= " AND p.fk_soc = sc.fk_soc AND sc.fk_user = " .$user->id;
 if ($socid) $sql.= " AND p.fk_soc = ".$socid;
 if ($mode == 'mine') $sql.=" AND t.rowid = pta.fk_projet_task";
@@ -295,7 +295,7 @@ else
 }
 print "</table>";
 
-/* Affichage de la liste des projets de l'ann�e */
+/* Affichage de la liste des projets de l'ann�e */
 print '<br /><table class="noborder" width="100%">';
 print '<tr class="liste_titre">';
 print '<td>'.$langs->trans("ActivityOnProjectThisYear").': '.strftime("%Y", $now).'</td>';
diff --git a/htdocs/projet/index.php b/htdocs/projet/index.php
index 3343a64ace4..779f42f573c 100644
--- a/htdocs/projet/index.php
+++ b/htdocs/projet/index.php
@@ -57,15 +57,12 @@ print '<td align="right">'.$langs->trans("NbOpenTasks").'</td>';
 print "</tr>\n";
 
 $sql = "SELECT p.title, p.rowid, count(t.rowid)";
-$sql.= " FROM ".MAIN_DB_PREFIX."societe as s";
-if (!$user->rights->societe->client->voir && !$socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc,";
-$sql.= ", ".MAIN_DB_PREFIX."projet as p";
-//$sql.= ", ".MAIN_DB_PREFIX."projet_task as t"; // pourquoi est-ce que c'�tait en commentaire ? => Si on laisse ce lien, les projet sans taches se retrouvent invisibles
+$sql.= " FROM ".MAIN_DB_PREFIX."projet as p";
+$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s on p.fk_soc = s.rowid";
+if (!$user->rights->societe->client->voir && !$socid) $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON s.rowid = sc.fk_soc AND sc.fk_user = ".$user->id;
 $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."projet_task as t ON p.rowid = t.fk_projet";
-$sql.= " WHERE p.fk_soc = s.rowid";
-$sql.= " AND s.entity = ".$conf->entity;
+$sql.= " WHERE p.entity = ".$conf->entity;
 if ($_REQUEST["mode"]=='mine') $sql.=' AND p.fk_user_resp='.$user->id;
-if (!$user->rights->societe->client->voir && !$socid) $sql.= " AND p.fk_soc = sc.fk_soc AND sc.fk_user = " .$user->id;
 if ($socid)	$sql.= " AND p.fk_soc = ".$socid;
 $sql.= " GROUP BY p.rowid";
 
@@ -105,14 +102,12 @@ print_liste_field_titre($langs->trans("Company"),"index.php","s.nom","","","",$s
 print '<td align="right">'.$langs->trans("NbOfProjects").'</td>';
 print "</tr>\n";
 
-$sql = "SELECT s.nom, s.rowid as socid, count(p.rowid)";
-$sql.= " FROM ".MAIN_DB_PREFIX."societe as s";
-$sql.= ", ".MAIN_DB_PREFIX."projet as p";
-if (!$user->rights->societe->client->voir && !$socid) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
-$sql.= " WHERE p.fk_soc = s.rowid";
-$sql.= " AND s.entity = ".$conf->entity;
+$sql = "SELECT count(p.rowid) as nb, s.nom, s.rowid as socid";
+$sql.= " FROM ".MAIN_DB_PREFIX."projet as p";
+$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s on p.fk_soc = s.rowid";
+if (!$user->rights->societe->client->voir && !$socid) $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON s.rowid = sc.fk_soc AND sc.fk_user = ".$user->id;
+$sql.= " WHERE p.entity = ".$conf->entity;
 if ($_REQUEST["mode"]=='mine') $sql.=' AND p.fk_user_resp='.$user->id;
-if (!$user->rights->societe->client->voir && !$socid) $sql.= " AND s.rowid = sc.fk_soc AND sc.fk_user = " .$user->id;
 if ($socid) $sql.= " AND s.rowid = ".$socid;
 $sql.= " GROUP BY s.nom";
 //$sql .= " ORDER BY $sortfield $sortorder " . $db->plimit($conf->liste_limit, $offset);
@@ -126,11 +121,20 @@ if ( $resql )
 
 	while ($i < $num)
 	{
-		$row = $db->fetch_row( $resql);
+		$obj = $db->fetch_object($resql);
 		$var=!$var;
 		print "<tr $bc[$var]>";
-		print '<td nowrap="nowrap"><a href="'.DOL_URL_ROOT.'/projet/liste.php?socid='.$row[1].'">'.img_object($langs->trans("ShowCompany"),"company")." ".$row[0].'</a></td>';
-		print '<td align="right">'.$row[2].'</td>';
+		print '<td nowrap="nowrap">';
+		if ($obj->socid)
+		{
+			print '<a href="'.DOL_URL_ROOT.'/projet/liste.php?socid='.$obj->socid.'">'.img_object($langs->trans("ShowCompany"),"company")." ".$obj->nom.'</a>';
+		}
+		else
+		{
+			print $langs->trans("Public");
+		}
+		print '</td>';
+		print '<td align="right">'.$obj->nb.'</td>';
 		print "</tr>\n";
 
 		$i++;
diff --git a/htdocs/projet/liste.php b/htdocs/projet/liste.php
index 6fd09d39a54..345887b8a17 100644
--- a/htdocs/projet/liste.php
+++ b/htdocs/projet/liste.php
@@ -77,7 +77,7 @@ if (!$user->rights->societe->client->voir && !$socid) $sql.= ", ".MAIN_DB_PREFIX
 $sql.= ")";
 $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s on p.fk_soc = s.rowid";
 $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."user as u on p.fk_user_resp = u.rowid";
-$sql.= " WHERE s.entity = ".$conf->entity;
+$sql.= " WHERE p.entity = ".$conf->entity;
 if ($_REQUEST["mode"]=='mine') $sql.=' AND p.fk_user_resp='.$user->id;
 if (!$user->rights->societe->client->voir && !$socid) $sql.= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".$user->id;
 if ($socid)	$sql.= " AND s.rowid = ".$socid;
diff --git a/mysql/migration/2.6.0-2.7.0.sql b/mysql/migration/2.6.0-2.7.0.sql
index 9d595334426..3368b6afadd 100644
--- a/mysql/migration/2.6.0-2.7.0.sql
+++ b/mysql/migration/2.6.0-2.7.0.sql
@@ -84,6 +84,7 @@ ALTER TABLE llx_dolibarr_modules ADD COLUMN entity integer DEFAULT 1 NOT NULL AF
 ALTER TABLE llx_bank_categ ADD COLUMN entity integer DEFAULT 1 NOT NULL AFTER label;
 ALTER TABLE llx_bordereau_cheque ADD COLUMN entity integer DEFAULT 1 NOT NULL AFTER number;
 ALTER TABLE llx_prelevement_bons ADD COLUMN entity integer DEFAULT 1 NOT NULL AFTER ref;
+ALTER TABLE llx_projet ADD COLUMN entity integer DEFAULT 1 NOT NULL AFTER ref;
 
 ALTER TABLE llx_rights_def DROP PRIMARY KEY;
 ALTER TABLE llx_user_param DROP INDEX fk_user;
@@ -108,6 +109,7 @@ ALTER TABLE llx_livraison DROP INDEX idx_livraison_uk_ref;
 ALTER TABLE llx_fichinter DROP INDEX ref;
 ALTER TABLE llx_dolibarr_modules DROP PRIMARY KEY;
 ALTER TABLE llx_prelevement_bons DROP INDEX ref;
+ALTER TABLE llx_projet DROP INDEX ref;
 
 ALTER TABLE llx_rights_def ADD PRIMARY KEY (id, entity);
 ALTER TABLE llx_user_param ADD UNIQUE INDEX uk_user_param (fk_user,param,entity);
@@ -134,6 +136,11 @@ ALTER TABLE llx_contrat ADD UNIQUE INDEX uk_contrat_ref (ref, entity);
 ALTER TABLE llx_dolibarr_modules ADD PRIMARY KEY pk_dolibarr_modules (numero, entity);
 ALTER TABLE llx_bordereau_cheque ADD UNIQUE INDEX uk_bordereau_cheque (number, entity);
 ALTER TABLE llx_prelevement_bons ADD UNIQUE INDEX uk_prelevement_bons_ref (ref, entity);
+ALTER TABLE llx_projet ADD UNIQUE INDEX uk_projet_ref (ref, entity);
+
+ALTER TABLE llx_projet ADD INDEX idx_projet_fk_soc (fk_soc);
+ALTER TABLE llx_projet ADD CONSTRAINT fk_projet_fk_soc FOREIGN KEY (fk_soc) REFERENCES llx_societe (rowid);
+
 
 UPDATE llx_const SET entity=0 WHERE name='MAIN_MODULE_USER' AND entity=1;
 UPDATE llx_const SET entity=0 WHERE name='MAIN_POPUP_CALENDAR' AND entity=1;
diff --git a/mysql/tables/llx_projet.key.sql b/mysql/tables/llx_projet.key.sql
new file mode 100644
index 00000000000..552a985abd4
--- /dev/null
+++ b/mysql/tables/llx_projet.key.sql
@@ -0,0 +1,27 @@
+-- ============================================================================
+-- Copyright (C) 2002-2004 Rodolphe Quiedeville <rodolphe@quiedeville.org>
+-- Copyright (C) 2004-2009 Laurent Destailleur  <eldy@users.sourceforge.net>
+-- Copyright (C) 2005-2009 Regis Houssin        <regis@dolibarr.fr>
+--
+-- 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 2 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, write to the Free Software
+-- Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+--
+-- $Id$
+-- ============================================================================
+
+
+ALTER TABLE llx_projet ADD UNIQUE INDEX uk_projet_ref (ref, entity);
+
+ALTER TABLE llx_projet ADD INDEX idx_projet_fk_soc (fk_soc);
+ALTER TABLE llx_projet ADD CONSTRAINT fk_projet_fk_soc FOREIGN KEY (fk_soc) REFERENCES llx_societe (rowid);
diff --git a/mysql/tables/llx_projet.sql b/mysql/tables/llx_projet.sql
index d3465fd8190..7fdb45fbe9c 100644
--- a/mysql/tables/llx_projet.sql
+++ b/mysql/tables/llx_projet.sql
@@ -26,11 +26,9 @@ create table llx_projet
   tms              timestamp,
   dateo            date,         -- date d'ouverture du projet
   ref              varchar(50),
+  entity           integer DEFAULT 1 NOT NULL, -- multi company id
   title            varchar(255),
   fk_user_resp     integer,      -- responsable du projet
   fk_user_creat    integer,      -- createur du projet
-  note             text,
-
-  UNIQUE INDEX(ref)
-
+  note             text
 )type=innodb;
-- 
GitLab