PageRenderTime 484ms CodeModel.GetById 98ms app.highlight 124ms RepoModel.GetById 168ms app.codeStats 1ms

/htdocs/societe/societe.php

https://bitbucket.org/speedealing/speedealing
PHP | 434 lines | 295 code | 47 blank | 92 comment | 68 complexity | 7c3a2dd4b54f76be47da134398bcc139 MD5 | raw file
  1<?php
  2/* Copyright (C) 2001-2004 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3 * Copyright (C) 2004-2011 Laurent Destailleur  <eldy@users.sourceforge.net>
  4 * Copyright (C) 2005-2012 Regis Houssin        <regis.houssin@capnetworks.com>
  5 * Copyright (C) 2012      Marcos GarcĂ­a        <marcosgdf@gmail.com>
  6 *
  7 * This program is free software; you can redistribute it and/or modify
  8 * it under the terms of the GNU General Public License as published by
  9 * the Free Software Foundation; either version 3 of the License, or
 10 * (at your option) any later version.
 11 *
 12 * This program is distributed in the hope that it will be useful,
 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 15 * GNU General Public License for more details.
 16 *
 17 * You should have received a copy of the GNU General Public License
 18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 19 */
 20
 21/**
 22 *	\file       htdocs/societe/societe.php
 23 *	\ingroup    societe
 24 *	\brief      Page to show a third party
 25 */
 26
 27require_once '../main.inc.php';
 28include_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
 29require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
 30
 31$langs->load("companies");
 32$langs->load("customers");
 33$langs->load("suppliers");
 34
 35// Security check
 36$socid = GETPOST('socid','int');
 37if ($user->societe_id) $socid=$user->societe_id;
 38$result = restrictedArea($user,'societe',$socid,'');
 39
 40$search_nom=trim(GETPOST("search_nom"));
 41$search_nom_only=trim(GETPOST("search_nom_only"));
 42$search_all=trim(GETPOST("search_all"));
 43$search_ville=trim(GETPOST("search_ville"));
 44$socname=trim(GETPOST("socname"));
 45$search_idprof1=trim(GETPOST('search_idprof1'));
 46$search_idprof2=trim(GETPOST('search_idprof2'));
 47$search_idprof3=trim(GETPOST('search_idprof3'));
 48$search_idprof4=trim(GETPOST('search_idprof4'));
 49$search_sale=trim(GETPOST("search_sale"));
 50$search_categ=trim(GETPOST("search_categ"));
 51$mode=GETPOST("mode");
 52$modesearch=GETPOST("mode_search");
 53
 54$sortfield=GETPOST("sortfield",'alpha');
 55$sortorder=GETPOST("sortorder",'alpha');
 56$page=GETPOST("page",'int');
 57if (! $sortorder) $sortorder="ASC";
 58if (! $sortfield) $sortfield="s.nom";
 59if ($page == -1) { $page = 0 ; }
 60$offset = $conf->liste_limit * $page ;
 61$pageprev = $page - 1;
 62$pagenext = $page + 1;
 63
 64
 65/*
 66 * Actions
 67 */
 68
 69// Recherche
 70if ($mode == 'search')
 71{
 72	$search_nom=$socname;
 73
 74	$sql = "SELECT s.rowid";
 75	$sql.= " FROM ".MAIN_DB_PREFIX."societe as s";
 76	if ($search_sale || !$user->rights->societe->client->voir && !$socid) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
 77    // We'll need this table joined to the select in order to filter by categ
 78    if ($search_categ) $sql.= ", ".MAIN_DB_PREFIX."categorie_societe as cs";
 79    $sql.= " WHERE s.entity IN (".getEntity('societe', 1).")";
 80	$sql.= " AND (";
 81	$sql.= " s.nom LIKE '%".$db->escape($socname)."%'";
 82	$sql.= " OR s.code_client LIKE '%".$db->escape($socname)."%'";
 83	$sql.= " OR s.email LIKE '%".$db->escape($socname)."%'";
 84	$sql.= " OR s.url LIKE '%".$db->escape($socname)."%'";
 85
 86	if (!empty($conf->barcode->enabled))
 87	{
 88		$sql.= "OR s.barcode LIKE '".$db->escape($socname)."'";
 89	}
 90
 91	$sql.= ")";
 92	if (!$user->rights->societe->client->voir && !$socid) $sql.= " AND s.rowid = sc.fk_soc AND sc.fk_user = " .$user->id;
 93	if ($socid) $sql.= " AND s.rowid = ".$socid;
 94    if ($search_sale) $sql.= " AND s.rowid = sc.fk_soc";        // Join for the needed table to filter by sale
 95    if ($search_categ) $sql.= " AND s.rowid = cs.fk_societe";   // Join for the needed table to filter by categ
 96	if (! $user->rights->societe->lire || ! $user->rights->fournisseur->lire)
 97	{
 98		if (! $user->rights->fournisseur->lire) $sql.=" AND s.fourn != 1";
 99	}
100    // Insert sale filter
101    if ($search_sale)
102    {
103        $sql .= " AND sc.fk_user = ".$search_sale;
104    }
105    // Insert categ filter
106    if ($search_categ)
107    {
108        $sql .= " AND cs.fk_categorie = ".$search_categ;
109    }
110	$result=$db->query($sql);
111	if ($result)
112	{
113		if ($db->num_rows($result) == 1)
114		{
115			$obj = $db->fetch_object($result);
116			$socid = $obj->rowid;
117			header("Location: ".DOL_URL_ROOT."/societe/soc.php?socid=".$socid);
118			exit;
119		}
120		$db->free($result);
121	}
122}
123
124
125
126/*
127 * View
128 */
129
130$form=new Form($db);
131$htmlother=new FormOther($db);
132$companystatic=new Societe($db);
133
134
135llxHeader('',$langs->trans("ThirdParty"));
136
137
138// Do we click on purge search criteria ?
139if (GETPOST("button_removefilter_x"))
140{
141    $search_categ='';
142    $search_sale='';
143    $socname="";
144	$search_nom="";
145	$search_ville="";
146	$search_idprof1='';
147	$search_idprof2='';
148	$search_idprof3='';
149	$search_idprof4='';
150}
151
152if ($socname)
153{
154	$search_nom=$socname;
155}
156
157
158/*
159 * Mode Liste
160 */
161/*
162 REM: Regle sur droits "Voir tous les clients"
163 REM: Exemple, voir la page societe.php dans le mode liste.
164 Utilisateur interne socid=0 + Droits voir tous clients        => Voit toute societe
165 Utilisateur interne socid=0 + Pas de droits voir tous clients => Ne voit que les societes liees comme commercial
166 Utilisateur externe socid=x + Droits voir tous clients        => Ne voit que lui meme
167 Utilisateur externe socid=x + Pas de droits voir tous clients => Ne voit que lui meme
168 */
169$title=$langs->trans("ListOfThirdParties");
170
171$sql = "SELECT s.rowid, s.nom as name, s.ville, s.datec, s.datea,";
172$sql.= " st.libelle as stcomm, s.prefix_comm, s.client, s.fournisseur, s.canvas, s.status as status,";
173$sql.= " s.siren as idprof1, s.siret as idprof2, ape as idprof3, idprof4 as idprof4";
174// We'll need these fields in order to filter by sale (including the case where the user can only see his prospects)
175if ($search_sale) $sql .= ", sc.fk_soc, sc.fk_user";
176// We'll need these fields in order to filter by categ
177if ($search_categ) $sql .= ", cs.fk_categorie, cs.fk_societe";
178$sql.= " FROM ".MAIN_DB_PREFIX."societe as s,";
179$sql.= " ".MAIN_DB_PREFIX."c_stcomm as st";
180// We'll need this table joined to the select in order to filter by sale
181if ($search_sale || !$user->rights->societe->client->voir) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
182// We'll need this table joined to the select in order to filter by categ
183if ($search_categ) $sql.= ", ".MAIN_DB_PREFIX."categorie_societe as cs";
184$sql.= " WHERE s.fk_stcomm = st.id";
185$sql.= " AND s.entity IN (".getEntity('societe', 1).")";
186if (! $user->rights->societe->client->voir && ! $socid)	$sql.= " AND s.rowid = sc.fk_soc AND sc.fk_user = " .$user->id;
187if ($socid)	$sql.= " AND s.rowid = ".$socid;
188if ($search_sale) $sql.= " AND s.rowid = sc.fk_soc";        // Join for the needed table to filter by sale
189if ($search_categ) $sql.= " AND s.rowid = cs.fk_societe";   // Join for the needed table to filter by categ
190// TODO $stcomm is not defined !
191/*
192if (dol_strlen($stcomm))
193{
194	$sql.= " AND s.fk_stcomm=".$stcomm;
195}
196*/
197if (! $user->rights->fournisseur->lire) $sql.=" AND (s.fournisseur <> 1 OR s.client <> 0)";    // client=0, fournisseur=0 must be visible
198// Insert sale filter
199if ($search_sale)
200{
201    $sql .= " AND sc.fk_user = ".$search_sale;
202}
203// Insert categ filter
204if ($search_categ)
205{
206    $sql .= " AND cs.fk_categorie = ".$search_categ;
207}
208if ($search_nom_only)
209{
210	$sql.= " AND s.nom LIKE '%".$db->escape($search_nom_only)."%'";
211}
212if ($search_all)
213{
214	$sql.= " AND (";
215	$sql.= "s.nom LIKE '%".$db->escape($search_all)."%'";
216	$sql.= " OR s.code_client LIKE '%".$db->escape($search_all)."%'";
217	$sql.= " OR s.email LIKE '%".$db->escape($search_all)."%'";
218	$sql.= " OR s.url LIKE '%".$db->escape($search_all)."%'";
219	$sql.= ")";
220}
221if ($search_nom)
222{
223	$sql.= " AND (";
224	$sql.= "s.nom LIKE '%".$db->escape($search_nom)."%'";
225	$sql.= " OR s.code_client LIKE '%".$db->escape($search_nom)."%'";
226	$sql.= " OR s.email LIKE '%".$db->escape($search_nom)."%'";
227	$sql.= " OR s.url LIKE '%".$db->escape($search_nom)."%'";
228	$sql.= ")";
229}
230
231if ($search_ville)
232{
233	$sql .= " AND s.ville LIKE '%".$db->escape($search_ville)."%'";
234}
235if ($search_idprof1)
236{
237	$sql .= " AND s.siren LIKE '%".$db->escape($search_idprof1)."%'";
238}
239if ($search_idprof2)
240{
241	$sql .= " AND s.siret LIKE '%".$db->escape($search_idprof2)."%'";
242}
243if ($search_idprof3)
244{
245	$sql .= " AND s.ape LIKE '%".$db->escape($search_idprof3)."%'";
246}
247if ($search_idprof4)
248{
249	$sql .= " AND s.idprof4 LIKE '%".$db->escape($search_idprof4)."%'";
250}
251//print $sql;
252
253// Count total nb of records
254$nbtotalofrecords = 0;
255if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST))
256{
257	$result = $db->query($sql);
258	$nbtotalofrecords = $db->num_rows($result);
259}
260
261$sql.= $db->order($sortfield,$sortorder);
262$sql.= $db->plimit($conf->liste_limit+1, $offset);
263
264$resql = $db->query($sql);
265if ($resql)
266{
267	$num = $db->num_rows($resql);
268	$i = 0;
269
270	$params = "&amp;socname=".$socname."&amp;search_nom=".$search_nom."&amp;search_ville=".$search_ville;
271	$params.= '&amp;search_idprof1='.$search_idprof1;
272	$params.= '&amp;search_idprof2='.$search_idprof2;
273	$params.= '&amp;search_idprof3='.$search_idprof3;
274	$params.= '&amp;search_idprof4='.$search_idprof4;
275
276	print_barre_liste($title, $page, $_SERVER["PHP_SELF"],$params,$sortfield,$sortorder,'',$num,$nbtotalofrecords);
277
278    // Show delete result message
279    if (GETPOST('delsoc'))
280    {
281        dol_htmloutput_mesg($langs->trans("CompanyDeleted",GETPOST('delsoc')),'','ok');
282    }
283
284	$langs->load("other");
285	$textprofid=array();
286	foreach(array(1,2,3,4) as $key)
287	{
288		$label=$langs->transnoentities("ProfId".$key.$mysoc->country_code);
289		$textprofid[$key]='';
290		if ($label != "ProfId".$key.$mysoc->country_code)
291		{	// Get only text between ()
292			if (preg_match('/\((.*)\)/i',$label,$reg)) $label=$reg[1];
293			$textprofid[$key]=$langs->trans("ProfIdShortDesc",$key,$mysoc->country_code,$label);
294		}
295	}
296
297	print '<form method="post" action="'.$_SERVER["PHP_SELF"].'" name="formfilter">';
298	print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
299
300	print '<table class="liste" width="100%">';
301
302    // Filter on categories
303    /* Not possible in this page because list is for ALL third parties type
304	$moreforfilter='';
305    if (! empty($conf->categorie->enabled))
306    {
307        $moreforfilter.=$langs->trans('Categories'). ': ';
308        $moreforfilter.=$htmlother->select_categories(2,$search_categ,'search_categ');
309        $moreforfilter.=' &nbsp; &nbsp; &nbsp; ';
310    }
311    // If the user can view prospects other than his'
312    if ($user->rights->societe->client->voir || $socid)
313    {
314        $moreforfilter.=$langs->trans('SalesRepresentatives'). ': ';
315        $moreforfilter.=$htmlother->select_salesrepresentatives($search_sale,'search_sale',$user);
316    }
317    if ($moreforfilter)
318    {
319        print '<tr class="liste_titre">';
320        print '<td class="liste_titre" colspan="8">';
321        print $moreforfilter;
322        print '</td></tr>';
323    }
324	*/
325
326    // Lines of titles
327    print '<tr class="liste_titre">';
328	print_liste_field_titre($langs->trans("Company"),$_SERVER["PHP_SELF"],"s.nom","",$params,"",$sortfield,$sortorder);
329	print_liste_field_titre($langs->trans("Town"),$_SERVER["PHP_SELF"],"s.ville","",$params,'',$sortfield,$sortorder);
330	print_liste_field_titre($form->textwithpicto($langs->trans("ProfId1Short"),$textprofid[1],1,0),$_SERVER["PHP_SELF"],"s.siren","",$params,'nowrap="nowrap"',$sortfield,$sortorder);
331	print_liste_field_titre($form->textwithpicto($langs->trans("ProfId2Short"),$textprofid[2],1,0),$_SERVER["PHP_SELF"],"s.siret","",$params,'nowrap="nowrap"',$sortfield,$sortorder);
332	print_liste_field_titre($form->textwithpicto($langs->trans("ProfId3Short"),$textprofid[3],1,0),$_SERVER["PHP_SELF"],"s.ape","",$params,'nowrap="nowrap"',$sortfield,$sortorder);
333	print_liste_field_titre($form->textwithpicto($langs->trans("ProfId4Short"),$textprofid[4],1,0),$_SERVER["PHP_SELF"],"s.idprof4","",$params,'nowrap="nowrap"',$sortfield,$sortorder);
334	print '<td></td>';
335	print_liste_field_titre($langs->trans("Status"),$_SERVER["PHP_SELF"],"s.status","",$params,'align="right"',$sortfield,$sortorder);
336	print "</tr>\n";
337
338	// Lignes des champs de filtre
339	print '<tr class="liste_titre">';
340	print '<td class="liste_titre">';
341	print '<input type="hidden" name="sortfield" value="'.$sortfield.'">';
342	print '<input type="hidden" name="sortorder" value="'.$sortorder.'">';
343	if (! empty($search_nom_only) && empty($search_nom)) $search_nom=$search_nom_only;
344	print '<input class="flat" type="text" name="search_nom" value="'.$search_nom.'">';
345	print '</td><td class="liste_titre">';
346	print '<input class="flat" size="10" type="text" name="search_ville" value="'.$search_ville.'">';
347	print '</td>';
348	// IdProf1
349	print '<td class="liste_titre">';
350	print '<input class="flat" size="8" type="text" name="search_idprof1" value="'.$search_idprof1.'">';
351	print '</td>';
352	// IdProf2
353	print '<td class="liste_titre">';
354	print '<input class="flat" size="8" type="text" name="search_idprof2" value="'.$search_idprof2.'">';
355	print '</td>';
356	// IdProf3
357	print '<td class="liste_titre">';
358	print '<input class="flat" size="8" type="text" name="search_idprof3" value="'.$search_idprof3.'">';
359	print '</td>';
360	// IdProf4
361	print '<td class="liste_titre">';
362	print '<input class="flat" size="8" type="text" name="search_idprof4" value="'.$search_idprof4.'">';
363	print '</td>';
364	// Type (customer/prospect/supplier)
365	print '<td colspan="2" class="liste_titre" align="right">';
366	print '<input type="image" class="liste_titre" name="button_search" src="'.DOL_URL_ROOT.'/theme/'.$conf->theme.'/img/search.png" value="'.dol_escape_htmltag($langs->trans("Search")).'" title="'.dol_escape_htmltag($langs->trans("Search")).'">';
367	print '&nbsp; ';
368	print '<input type="image" class="liste_titre" name="button_removefilter" src="'.DOL_URL_ROOT.'/theme/'.$conf->theme.'/img/searchclear.png" value="'.dol_escape_htmltag($langs->trans("RemoveFilter")).'" title="'.dol_escape_htmltag($langs->trans("RemoveFilter")).'">';
369	print '</td>';
370	print "</tr>\n";
371
372	$var=True;
373
374	while ($i < min($num,$conf->liste_limit))
375	{
376		$obj = $db->fetch_object($resql);
377		$var=!$var;
378		print "<tr $bc[$var]><td>";
379		$companystatic->id=$obj->rowid;
380		$companystatic->name=$obj->name;
381		$companystatic->canvas=$obj->canvas;
382        $companystatic->client=$obj->client;
383        $companystatic->status=$obj->status;
384		print $companystatic->getNomUrl(1,'',24);
385		print "</td>\n";
386		print "<td>".$obj->ville."</td>\n";
387		print "<td>".$obj->idprof1."</td>\n";
388		print "<td>".$obj->idprof2."</td>\n";
389		print "<td>".$obj->idprof3."</td>\n";
390		print "<td>".$obj->idprof4."</td>\n";
391		print '<td align="center">';
392		$s='';
393		if (($obj->client==1 || $obj->client==3) && empty($conf->global->SOCIETE_DISABLE_CUSTOMERS))
394		{
395	  		$companystatic->name=$langs->trans("Customer");
396		    $s.=$companystatic->getNomUrl(0,'customer');
397		}
398		if (($obj->client==2 || $obj->client==3) && empty($conf->global->SOCIETE_DISABLE_PROSPECTS))
399		{
400            if ($s) $s.=" / ";
401		    $companystatic->name=$langs->trans("Prospect");
402            $s.=$companystatic->getNomUrl(0,'prospect');
403		}
404		if (! empty($conf->fournisseur->enabled) && $obj->fournisseur)
405		{
406			if ($s) $s.=" / ";
407            $companystatic->name=$langs->trans("Supplier");
408            $s.=$companystatic->getNomUrl(0,'supplier');
409		}
410		print $s;
411		print '</td>';
412        print '<td align="right">'.$companystatic->getLibStatut(3).'</td>';
413
414		print '</tr>'."\n";
415		$i++;
416	}
417
418	$db->free($resql);
419
420	print "</table>";
421
422	print '</form>';
423
424}
425else
426{
427	dol_print_error($db);
428}
429
430llxFooter();
431
432$db->close();
433
434?>