PageRenderTime 60ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/htdocs/comm/prospect/list.php

https://github.com/asterix14/dolibarr
PHP | 429 lines | 292 code | 62 blank | 75 comment | 72 complexity | 28ca70e04df869c5b744f5468c404967 MD5 | raw file
Possible License(s): LGPL-2.0
  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-2011 Regis Houssin <regis@dolibarr.fr>
  5. * Copyright (C) 2011 Philippe Grand <philippe.grand@atoo-net.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 2 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. * \file htdocs/comm/prospect/list.php
  22. * \ingroup prospect
  23. * \brief Page to list prospects
  24. */
  25. require("../../main.inc.php");
  26. require_once(DOL_DOCUMENT_ROOT."/comm/prospect/class/prospect.class.php");
  27. require_once(DOL_DOCUMENT_ROOT."/core/class/html.formother.class.php");
  28. $langs->load("propal");
  29. $langs->load("companies");
  30. // Security check
  31. $socid = GETPOST("socid",'int');
  32. if ($user->societe_id) $socid=$user->societe_id;
  33. $result = restrictedArea($user, 'societe',$socid,'');
  34. $socname = GETPOST("socname",'alpha');
  35. $stcomm = GETPOST("stcomm",'int');
  36. $search_nom = GETPOST("search_nom");
  37. $search_ville = GETPOST("search_ville");
  38. $search_departement = GETPOST("search_departement");
  39. $search_datec = GETPOST("search_datec");
  40. $sortfield = GETPOST("sortfield",'alpha');
  41. $sortorder = GETPOST("sortorder",'alpha');
  42. $page = GETPOST("page",'int');
  43. if ($page == -1) { $page = 0; }
  44. $offset = $conf->liste_limit * $page;
  45. $pageprev = $page - 1;
  46. $pagenext = $page + 1;
  47. if (! $sortorder) $sortorder="ASC";
  48. if (! $sortfield) $sortfield="s.nom";
  49. $search_level_from = GETPOST("search_level_from","alpha");
  50. $search_level_to = GETPOST("search_level_to","alpha");
  51. // If both parameters are set, search for everything BETWEEN them
  52. if ($search_level_from != '' && $search_level_to != '')
  53. {
  54. // Ensure that these parameters are numbers
  55. $search_level_from = (int) $search_level_from;
  56. $search_level_to = (int) $search_level_to;
  57. // If from is greater than to, reverse orders
  58. if ($search_level_from > $search_level_to)
  59. {
  60. $tmp = $search_level_to;
  61. $search_level_to = $search_level_from;
  62. $search_level_from = $tmp;
  63. }
  64. // Generate the SQL request
  65. $sortwhere = '(sortorder BETWEEN '.$search_level_from.' AND '.$search_level_to.') AS is_in_range';
  66. }
  67. // If only "from" parameter is set, search for everything GREATER THAN it
  68. else if ($search_level_from != '')
  69. {
  70. // Ensure that this parameter is a number
  71. $search_level_from = (int) $search_level_from;
  72. // Generate the SQL request
  73. $sortwhere = '(sortorder >= '.$search_level_from.') AS is_in_range';
  74. }
  75. // If only "to" parameter is set, search for everything LOWER THAN it
  76. else if ($search_level_to != '')
  77. {
  78. // Ensure that this parameter is a number
  79. $search_level_to = (int) $search_level_to;
  80. // Generate the SQL request
  81. $sortwhere = '(sortorder <= '.$search_level_to.') AS is_in_range';
  82. }
  83. // If no parameters are set, dont search for anything
  84. else
  85. {
  86. $sortwhere = '0 as is_in_range';
  87. }
  88. // Select every potentiels, and note each potentiels which fit in search parameters
  89. dol_syslog('prospects::prospects_prospect_level',LOG_DEBUG);
  90. $sql = "SELECT code, label, sortorder, ".$sortwhere;
  91. $sql.= " FROM ".MAIN_DB_PREFIX."c_prospectlevel";
  92. $sql.= " WHERE active > 0";
  93. $sql.= " ORDER BY sortorder";
  94. $resql = $db->query($sql);
  95. if ($resql)
  96. {
  97. $tab_level = array();
  98. $search_levels = array();
  99. while ($obj = $db->fetch_object($resql))
  100. {
  101. // Compute level text
  102. $level=$langs->trans($obj->code);
  103. if ($level == $obj->code) $level=$langs->trans($obj->label);
  104. // Put it in the array sorted by sortorder
  105. $tab_level[$obj->sortorder] = $level;
  106. // If this potentiel fit in parameters, add its code to the $search_levels array
  107. if ($obj->is_in_range == 1)
  108. {
  109. $search_levels[] = '"'.preg_replace('[^A-Za-z0-9_-]', '', $obj->code).'"';
  110. }
  111. $i++;
  112. }
  113. // Implode the $search_levels array so that it can be use in a "IN (...)" where clause.
  114. // If no paramters was set, $search_levels will be empty
  115. $search_levels = implode(',', $search_levels);
  116. }
  117. else dol_print_error($db);
  118. // Load sale and categ filters
  119. $search_sale = isset($_GET["search_sale"])?$_GET["search_sale"]:$_POST["search_sale"];
  120. $search_categ = isset($_GET["search_categ"])?$_GET["search_categ"]:$_POST["search_categ"];
  121. // If the user must only see his prospect, force searching by him
  122. if (!$user->rights->societe->client->voir && !$socid) $search_sale = $user->id;
  123. // List of avaible states; we'll need that for each lines (quick changing prospect states) and for search bar (filter by prospect state)
  124. $sts = array(-1,0,1,2,3);
  125. /*
  126. * Actions
  127. */
  128. if ($_GET["action"] == 'cstc')
  129. {
  130. $sql = "UPDATE ".MAIN_DB_PREFIX."societe SET fk_stcomm = ".$_GET["pstcomm"];
  131. $sql .= " WHERE rowid = ".$_GET["socid"];
  132. $result=$db->query($sql);
  133. }
  134. /*
  135. * View
  136. */
  137. $htmlother=new FormOther($db);
  138. $sql = "SELECT s.rowid, s.nom, s.ville, s.datec, s.datea, s.status as status,";
  139. $sql.= " st.libelle as stcomm, s.prefix_comm, s.fk_stcomm, s.fk_prospectlevel,";
  140. $sql.= " d.nom as departement";
  141. // Updated by Matelli
  142. // We'll need these fields in order to filter by sale (including the case where the user can only see his prospects)
  143. if ($search_sale) $sql .= ", sc.fk_soc, sc.fk_user";
  144. // We'll need these fields in order to filter by categ
  145. if ($search_categ) $sql .= ", cs.fk_categorie, cs.fk_societe";
  146. $sql .= " FROM ".MAIN_DB_PREFIX."c_stcomm as st";
  147. // We'll need this table joined to the select in order to filter by sale
  148. if ($search_sale || !$user->rights->societe->client->voir) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  149. // We'll need this table joined to the select in order to filter by categ
  150. if ($search_categ) $sql.= ", ".MAIN_DB_PREFIX."categorie_societe as cs";
  151. $sql.= ", ".MAIN_DB_PREFIX."societe as s";
  152. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."c_departements as d on (d.rowid = s.fk_departement)";
  153. $sql.= " WHERE s.fk_stcomm = st.id";
  154. $sql.= " AND s.client IN (2, 3)";
  155. $sql.= " AND s.entity = ".$conf->entity;
  156. if ($user->societe_id) $sql.= " AND s.rowid = " .$user->societe_id;
  157. if ($search_sale) $sql.= " AND s.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
  158. if ($search_categ) $sql.= " AND s.rowid = cs.fk_societe"; // Join for the needed table to filter by categ
  159. if (isset($stcomm) && $stcomm != '') $sql.= " AND s.fk_stcomm=".$stcomm;
  160. if ($search_nom) $sql .= " AND s.nom LIKE '%".$db->escape(strtolower($search_nom))."%'";
  161. if ($search_ville) $sql .= " AND s.ville LIKE '%".$db->escape(strtolower($search_ville))."%'";
  162. if ($search_departement) $sql .= " AND d.nom LIKE '%".$db->escape(strtolower($search_departement))."%'";
  163. if ($search_datec) $sql .= " AND s.datec LIKE '%".$db->escape($search_datec)."%'";
  164. // Insert levels filters
  165. if ($search_levels)
  166. {
  167. $sql .= " AND s.fk_prospectlevel IN (".$search_levels.')';
  168. }
  169. // Insert sale filter
  170. if ($search_sale)
  171. {
  172. $sql .= " AND sc.fk_user = ".$db->escape($search_sale);
  173. }
  174. // Insert categ filter
  175. if ($search_categ)
  176. {
  177. $sql .= " AND cs.fk_categorie = ".$db->escape($search_categ);
  178. }
  179. if ($socname)
  180. {
  181. $sql .= " AND s.nom LIKE '%".$db->escape($socname)."%'";
  182. $sortfield = "s.nom";
  183. $sortorder = "ASC";
  184. }
  185. // Count total nb of records
  186. $nbtotalofrecords = 0;
  187. if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST))
  188. {
  189. $result = $db->query($sql);
  190. $nbtotalofrecords = $db->num_rows($result);
  191. }
  192. $sql.= " ORDER BY $sortfield $sortorder, s.nom ASC";
  193. $sql.= $db->plimit($conf->liste_limit+1, $offset);
  194. $resql = $db->query($sql);
  195. if ($resql)
  196. {
  197. $num = $db->num_rows($resql);
  198. if ($num == 1 && $socname)
  199. {
  200. $obj = $db->fetch_object($resql);
  201. Header("Location: fiche.php?socid=".$obj->rowid);
  202. exit;
  203. }
  204. else
  205. {
  206. $help_url='EN:Module_Third_Parties|FR:Module_Tiers|ES:Empresas';
  207. llxHeader('',$langs->trans("ThirdParty"),$help_url);
  208. }
  209. $param='&amp;stcomm='.$stcomm.'&amp;search_nom='.urlencode($search_nom).'&amp;search_ville='.urlencode($search_ville);
  210. // Added by Matelli
  211. // Store the status filter in the URL
  212. if (isSet($search_cstc))
  213. {
  214. foreach ($search_cstc as $key => $value)
  215. {
  216. if ($value == 'true')
  217. $param.='&amp;search_cstc['.((int) $key).']=true';
  218. else
  219. $param.='&amp;search_cstc['.((int) $key).']=false';
  220. }
  221. }
  222. if ($search_level_from != '') $param.='&amp;search_level_from='.$search_level_from;
  223. if ($search_level_to != '') $param.='&amp;search_level_to='.$search_level_to;
  224. if ($search_categ != '') $param.='&amp;search_categ='.$search_categ;
  225. if ($search_sale != '') $param.='&amp;search_sale='.$search_sale;
  226. // $param and $urladd should have the same value
  227. $urladd = $param;
  228. print_barre_liste($langs->trans("ListOfProspects"), $page, $_SERVER["PHP_SELF"], $param, $sortfield,$sortorder,'',$num,$nbtotalofrecords);
  229. // Print the search-by-sale and search-by-categ filters
  230. print '<form method="get" action="'.$_SERVER["PHP_SELF"].'" id="formulaire_recherche">';
  231. print '<table class="liste" width="100%">';
  232. // Filter on categories
  233. $moreforfilter='';
  234. if ($conf->categorie->enabled)
  235. {
  236. $moreforfilter.=$langs->trans('Categories'). ': ';
  237. $moreforfilter.=$htmlother->select_categories(2,$search_categ,'search_categ');
  238. $moreforfilter.=' &nbsp; &nbsp; &nbsp; ';
  239. }
  240. // If the user can view prospects other than his'
  241. if ($user->rights->societe->client->voir || $socid)
  242. {
  243. $moreforfilter.=$langs->trans('SalesRepresentatives'). ': ';
  244. $moreforfilter.=$htmlother->select_salesrepresentatives($search_sale,'search_sale',$user);
  245. }
  246. if ($moreforfilter)
  247. {
  248. print '<tr class="liste_titre">';
  249. print '<td class="liste_titre" colspan="8">';
  250. print $moreforfilter;
  251. print '</td></tr>';
  252. }
  253. print '<tr class="liste_titre">';
  254. print_liste_field_titre($langs->trans("Company"),$_SERVER["PHP_SELF"],"s.nom","",$param,'',$sortfield,$sortorder);
  255. print_liste_field_titre($langs->trans("Town"),$_SERVER["PHP_SELF"],"s.ville","",$param,"",$sortfield,$sortorder);
  256. print_liste_field_titre($langs->trans("State"),$_SERVER["PHP_SELF"],"s.fk_departement","",$param,'align="center"',$sortfield,$sortorder);
  257. print_liste_field_titre($langs->trans("DateCreation"),$_SERVER["PHP_SELF"],"s.datec","",$param,'align="center"',$sortfield,$sortorder);
  258. print_liste_field_titre($langs->trans("ProspectLevelShort"),$_SERVER["PHP_SELF"],"s.fk_prospectlevel","",$param,'align="center"',$sortfield,$sortorder);
  259. print_liste_field_titre($langs->trans("StatusProsp"),$_SERVER["PHP_SELF"],"s.fk_stcomm","",$param,'align="center"',$sortfield,$sortorder);
  260. print '<td class="liste_titre">&nbsp;</td>';
  261. print_liste_field_titre($langs->trans("Status"),$_SERVER["PHP_SELF"],"s.status","",$params,'align="right"',$sortfield,$sortorder);
  262. print "</tr>\n";
  263. print '<tr class="liste_titre">';
  264. print '<td class="liste_titre">';
  265. print '<input type="text" class="flat" name="search_nom" size="10" value="'.$search_nom.'">';
  266. print '</td><td class="liste_titre">';
  267. print '<input type="text" class="flat" name="search_ville" size="10" value="'.$search_ville.'">';
  268. print '</td>';
  269. print '<td class="liste_titre" align="center">';
  270. print '<input type="text" class="flat" name="search_departement" size="10" value="'.$search_departement.'">';
  271. print '</td>';
  272. print '<td align="center" class="liste_titre">';
  273. print '<input class="flat" type="text" size="10" name="search_datec" value="'.$search_datec.'">';
  274. print '</td>';
  275. // Added by Matelli
  276. print '<td class="liste_titre" align="center">';
  277. // Generate in $options_from the list of each option sorted
  278. $options_from = '<option value="">&nbsp;</option>';
  279. foreach ($tab_level as $tab_level_sortorder => $tab_level_label)
  280. {
  281. $options_from .= '<option value="'.$tab_level_sortorder.'"'.($search_level_from == $tab_level_sortorder ? ' selected="selected"':'').'>';
  282. $options_from .= $langs->trans($tab_level_label);
  283. $options_from .= '</option>';
  284. }
  285. // Reverse the list
  286. array_reverse($tab_level, true);
  287. // Generate in $options_to the list of each option sorted in the reversed order
  288. $options_to = '<option value="">&nbsp;</option>';
  289. foreach ($tab_level as $tab_level_sortorder => $tab_level_label)
  290. {
  291. $options_to .= '<option value="'.$tab_level_sortorder.'"'.($search_level_to == $tab_level_sortorder ? ' selected="selected"':'').'>';
  292. $options_to .= $langs->trans($tab_level_label);
  293. $options_to .= '</option>';
  294. }
  295. // Print these two select
  296. print $langs->trans("From").' <select class="flat" name="search_level_from">'.$options_from.'</select>';
  297. print ' ';
  298. print $langs->trans("To").' <select class="flat" name="search_level_to">'.$options_to.'</select>';
  299. print '</td>';
  300. print '<td class="liste_titre" align="center">';
  301. print '&nbsp;';
  302. print '</td>';
  303. print '<td class="liste_titre" align="center">';
  304. print '&nbsp;';
  305. print '</td>';
  306. // Print the search button
  307. print '<td class="liste_titre" align="right">';
  308. print '<input class="liste_titre" name="button_search" type="image" src="'.DOL_URL_ROOT.'/theme/'.$conf->theme.'/img/search.png" value="'.dol_escape_htmltag($langs->trans("Search")).'" title="'.dol_escape_htmltag($langs->trans("Search")).'">';
  309. print '</td>';
  310. print "</tr>\n";
  311. $i = 0;
  312. $var=true;
  313. $prospectstatic=new Prospect($db);
  314. $prospectstatic->client=2;
  315. while ($i < min($num,$conf->liste_limit))
  316. {
  317. $obj = $db->fetch_object($resql);
  318. $var=!$var;
  319. print '<tr '.$bc[$var].'>';
  320. print '<td>';
  321. $prospectstatic->id=$obj->rowid;
  322. $prospectstatic->nom=$obj->nom;
  323. $prospectstatic->status=$obj->status;
  324. print $prospectstatic->getNomUrl(1,'prospect');
  325. print '</td>';
  326. print "<td>".$obj->ville."&nbsp;</td>";
  327. print '<td align="center">'.$obj->departement.'</td>';
  328. // Creation date
  329. print '<td align="center">'.dol_print_date($db->jdate($obj->datec)).'</td>';
  330. // Level
  331. print '<td align="center">';
  332. print $prospectstatic->LibLevel($obj->fk_prospectlevel);
  333. print "</td>";
  334. // Statut
  335. print '<td align="center" nowrap="nowrap">';
  336. print $prospectstatic->LibProspStatut($obj->fk_stcomm,2);
  337. print "</td>";
  338. //$sts = array(-1,0,1,2,3);
  339. print '<td align="right" nowrap="nowrap">';
  340. foreach ($sts as $key => $value)
  341. {
  342. if ($value <> $obj->fk_stcomm)
  343. {
  344. print '<a href="'.$_SERVER["PHP_SELF"].'?socid='.$obj->rowid.'&amp;pstcomm='.$value.'&amp;action=cstc&amp;'.$param.($page?'&amp;page='.$page:'').'">';
  345. print img_action(0,$value);
  346. print '</a>&nbsp;';
  347. }
  348. }
  349. print '</td>';
  350. print '<td align="right">';
  351. print $prospectstatic->getLibStatut(3);
  352. print '</td>';
  353. print "</tr>\n";
  354. $i++;
  355. }
  356. if ($num > $conf->liste_limit || $page > 0) print_barre_liste('', $page, $_SERVER["PHP_SELF"],$param,$sortfield,$sortorder,'',$num,$nbtotalofrecords);
  357. print "</table>";
  358. print "</form>";
  359. $db->free($resql);
  360. }
  361. else
  362. {
  363. dol_print_error($db);
  364. }
  365. $db->close();
  366. llxFooter();
  367. ?>