list.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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) 2011 Philippe Grand <philippe.grand@atoo-net.com>
  6. * Copyright (C) 2013 Florian Henry <florian.henry@open-concept.pro>
  7. * Copyright (C) 2013 Cédric Salvador <csalvador@gpcsolutions.fr>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. /**
  23. * \file htdocs/comm/prospect/list.php
  24. * \ingroup prospect
  25. * \brief Page to list prospects
  26. */
  27. require '../../main.inc.php';
  28. require_once DOL_DOCUMENT_ROOT.'/comm/prospect/class/prospect.class.php';
  29. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
  30. $langs->load("propal");
  31. $langs->load("companies");
  32. // Security check
  33. $socid = GETPOST("socid",'int');
  34. if ($user->societe_id) $socid=$user->societe_id;
  35. $result = restrictedArea($user, 'societe',$socid,'');
  36. $action = GETPOST('action','alpha');
  37. $socname = GETPOST("socname",'alpha');
  38. $stcomm = GETPOST("stcomm",'int');
  39. $search_nom = GETPOST("search_nom");
  40. $search_zipcode = GETPOST("search_zipcode");
  41. $search_town = GETPOST("search_town");
  42. $search_state = GETPOST("search_state");
  43. $search_datec = GETPOST("search_datec");
  44. $search_categ = GETPOST("search_categ",'int');
  45. $search_status = GETPOST("search_status",'int');
  46. if ($search_status=='') $search_status=1; // always display activ customer first
  47. $catid = GETPOST("catid",'int');
  48. $sortfield = GETPOST("sortfield",'alpha');
  49. $sortorder = GETPOST("sortorder",'alpha');
  50. $page = GETPOST("page",'int');
  51. if ($page == -1) { $page = 0; }
  52. $offset = $conf->liste_limit * $page;
  53. $pageprev = $page - 1;
  54. $pagenext = $page + 1;
  55. if (! $sortorder) $sortorder="ASC";
  56. if (! $sortfield) $sortfield="s.nom";
  57. $search_level_from = GETPOST("search_level_from","alpha");
  58. $search_level_to = GETPOST("search_level_to","alpha");
  59. // If both parameters are set, search for everything BETWEEN them
  60. if ($search_level_from != '' && $search_level_to != '')
  61. {
  62. // Ensure that these parameters are numbers
  63. $search_level_from = (int) $search_level_from;
  64. $search_level_to = (int) $search_level_to;
  65. // If from is greater than to, reverse orders
  66. if ($search_level_from > $search_level_to)
  67. {
  68. $tmp = $search_level_to;
  69. $search_level_to = $search_level_from;
  70. $search_level_from = $tmp;
  71. }
  72. // Generate the SQL request
  73. $sortwhere = '(sortorder BETWEEN '.$search_level_from.' AND '.$search_level_to.') AS is_in_range';
  74. }
  75. // If only "from" parameter is set, search for everything GREATER THAN it
  76. else if ($search_level_from != '')
  77. {
  78. // Ensure that this parameter is a number
  79. $search_level_from = (int) $search_level_from;
  80. // Generate the SQL request
  81. $sortwhere = '(sortorder >= '.$search_level_from.') AS is_in_range';
  82. }
  83. // If only "to" parameter is set, search for everything LOWER THAN it
  84. else if ($search_level_to != '')
  85. {
  86. // Ensure that this parameter is a number
  87. $search_level_to = (int) $search_level_to;
  88. // Generate the SQL request
  89. $sortwhere = '(sortorder <= '.$search_level_to.') AS is_in_range';
  90. }
  91. // If no parameters are set, dont search for anything
  92. else
  93. {
  94. $sortwhere = '0 as is_in_range';
  95. }
  96. // Select every potentiels, and note each potentiels which fit in search parameters
  97. dol_syslog('prospects::prospects_prospect_level',LOG_DEBUG);
  98. $sql = "SELECT code, label, sortorder, ".$sortwhere;
  99. $sql.= " FROM ".MAIN_DB_PREFIX."c_prospectlevel";
  100. $sql.= " WHERE active > 0";
  101. $sql.= " ORDER BY sortorder";
  102. $resql = $db->query($sql);
  103. if ($resql)
  104. {
  105. $tab_level = array();
  106. $search_levels = array();
  107. while ($obj = $db->fetch_object($resql))
  108. {
  109. // Compute level text
  110. $level=$langs->trans($obj->code);
  111. if ($level == $obj->code) $level=$langs->trans($obj->label);
  112. // Put it in the array sorted by sortorder
  113. $tab_level[$obj->sortorder] = $level;
  114. // If this potentiel fit in parameters, add its code to the $search_levels array
  115. if ($obj->is_in_range == 1)
  116. {
  117. $search_levels[] = '"'.preg_replace('[^A-Za-z0-9_-]', '', $obj->code).'"';
  118. }
  119. }
  120. // Implode the $search_levels array so that it can be use in a "IN (...)" where clause.
  121. // If no paramters was set, $search_levels will be empty
  122. $search_levels = implode(',', $search_levels);
  123. }
  124. else dol_print_error($db);
  125. // Load sale and categ filters
  126. $search_sale = GETPOST('search_sale');
  127. $search_categ = GETPOST('search_categ');
  128. // If the internal user must only see his prospect, force searching by him
  129. if (!$user->rights->societe->client->voir && !$socid) $search_sale = $user->id;
  130. // List of avaible states; we'll need that for each lines (quick changing prospect states) and for search bar (filter by prospect state)
  131. $sts = array(-1,0,1,2,3);
  132. // Initialize technical object to manage hooks of thirdparties. Note that conf->hooks_modules contains array array
  133. $hookmanager->initHooks(array('prospectlist'));
  134. /*
  135. * Actions
  136. */
  137. $parameters=array();
  138. $reshook=$hookmanager->executeHooks('doActions',$parameters); // Note that $action and $object may have been modified by some hooks
  139. if ($action == 'cstc')
  140. {
  141. $sql = "UPDATE ".MAIN_DB_PREFIX."societe SET fk_stcomm = ".$_GET["pstcomm"];
  142. $sql .= " WHERE rowid = ".$_GET["socid"];
  143. $result=$db->query($sql);
  144. }
  145. /*
  146. * View
  147. */
  148. $formother=new FormOther($db);
  149. $form=new Form($db);
  150. $sql = "SELECT s.rowid, s.nom, s.zip, s.town, s.datec, s.status as status, s.code_client, s.client,";
  151. $sql.= " st.libelle as stcomm, s.prefix_comm, s.fk_stcomm, s.fk_prospectlevel,";
  152. $sql.= " d.nom as departement";
  153. if ((!$user->rights->societe->client->voir && !$socid) || $search_sale) $sql .= ", sc.fk_soc, sc.fk_user"; // We need these fields in order to filter by sale (including the case where the user can only see his prospects)
  154. $sql .= " FROM ".MAIN_DB_PREFIX."c_stcomm as st";
  155. $sql.= ", ".MAIN_DB_PREFIX."societe as s";
  156. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."c_departements as d on (d.rowid = s.fk_departement)";
  157. if (! empty($search_categ) || ! empty($catid)) $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX."categorie_societe as cs ON s.rowid = cs.fk_societe"; // We need this table joined to the select in order to filter by categ
  158. if ((!$user->rights->societe->client->voir && !$socid) || $search_sale) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale
  159. $sql.= " WHERE s.fk_stcomm = st.id";
  160. $sql.= " AND s.client IN (2, 3)";
  161. $sql.= ' AND s.entity IN ('.getEntity('societe', 1).')';
  162. if ((!$user->rights->societe->client->voir && !$socid) || $search_sale) $sql.= " AND s.rowid = sc.fk_soc";
  163. if ($socid) $sql.= " AND s.rowid = " .$socid;
  164. if (isset($stcomm) && $stcomm != '') $sql.= " AND s.fk_stcomm=".$stcomm;
  165. if ($catid > 0) $sql.= " AND cs.fk_categorie = ".$catid;
  166. if ($catid == -2) $sql.= " AND cs.fk_categorie IS NULL";
  167. if ($search_categ > 0) $sql.= " AND cs.fk_categorie = ".$search_categ;
  168. if ($search_categ == -2) $sql.= " AND cs.fk_categorie IS NULL";
  169. if ($search_nom) {
  170. $sql .= natural_search('s.nom', $search_nom);
  171. }
  172. if ($search_zipcode) $sql .= " AND s.zip LIKE '".$db->escape(strtolower($search_zipcode))."%'";
  173. if ($search_town) {
  174. $sql .= natural_search('s.town', $search_town);
  175. }
  176. if ($search_state) {
  177. $sql .= natural_search('d.nom', $search_state);
  178. }
  179. if ($search_datec) $sql .= " AND s.datec LIKE '%".$db->escape($search_datec)."%'";
  180. if ($search_status!='') $sql .= " AND s.status = ".$db->escape($search_status);
  181. // Insert levels filters
  182. if ($search_levels)
  183. {
  184. $sql .= " AND s.fk_prospectlevel IN (".$search_levels.')';
  185. }
  186. // Insert sale filter
  187. if ($search_sale)
  188. {
  189. $sql .= " AND sc.fk_user = ".$db->escape($search_sale);
  190. }
  191. if ($socname) {
  192. $sql .= natural_search('s.nom', $search_nom);
  193. $sortfield = "s.nom";
  194. $sortorder = "ASC";
  195. }
  196. // Count total nb of records
  197. $nbtotalofrecords = 0;
  198. if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST))
  199. {
  200. $result = $db->query($sql);
  201. $nbtotalofrecords = $db->num_rows($result);
  202. }
  203. $sql.= " ORDER BY $sortfield $sortorder, s.nom ASC";
  204. $sql.= $db->plimit($conf->liste_limit+1, $offset);
  205. dol_syslog('comm/prospect/list.php sql='.$sql,LOG_DEBUG);
  206. $resql = $db->query($sql);
  207. if ($resql)
  208. {
  209. $num = $db->num_rows($resql);
  210. if ($num == 1 && $socname)
  211. {
  212. $obj = $db->fetch_object($resql);
  213. header("Location: fiche.php?socid=".$obj->rowid);
  214. exit;
  215. }
  216. else
  217. {
  218. $help_url='EN:Module_Third_Parties|FR:Module_Tiers|ES:Empresas';
  219. llxHeader('',$langs->trans("ThirdParty"),$help_url);
  220. }
  221. $param='&amp;stcomm='.$stcomm.'&amp;search_nom='.urlencode($search_nom).'&amp;search_zipcode='.urlencode($search_zipcode).'&amp;search_town='.urlencode($search_town);
  222. // Store the status filter in the URL
  223. if (isSet($search_cstc))
  224. {
  225. foreach ($search_cstc as $key => $value)
  226. {
  227. if ($value == 'true')
  228. $param.='&amp;search_cstc['.((int) $key).']=true';
  229. else
  230. $param.='&amp;search_cstc['.((int) $key).']=false';
  231. }
  232. }
  233. if ($search_level_from != '') $param.='&amp;search_level_from='.$search_level_from;
  234. if ($search_level_to != '') $param.='&amp;search_level_to='.$search_level_to;
  235. if ($search_categ != '') $param.='&amp;search_categ='.$search_categ;
  236. if ($search_sale != '') $param.='&amp;search_sale='.$search_sale;
  237. if ($search_status != '') $param.='&amp;search_status='.$search_status;
  238. // $param and $urladd should have the same value
  239. $urladd = $param;
  240. print_barre_liste($langs->trans("ListOfProspects"), $page, $_SERVER["PHP_SELF"], $param, $sortfield,$sortorder,'',$num,$nbtotalofrecords);
  241. // Print the search-by-sale and search-by-categ filters
  242. print '<form method="GET" id="searchFormList" action="'.$_SERVER["PHP_SELF"].'">';
  243. // Filter on categories
  244. $moreforfilter='';
  245. if (! empty($conf->categorie->enabled))
  246. {
  247. $moreforfilter.=$langs->trans('Categories'). ': ';
  248. $moreforfilter.=$formother->select_categories(2,$search_categ,'search_categ',1);
  249. $moreforfilter.=' &nbsp; &nbsp; &nbsp; ';
  250. }
  251. // If the user can view prospects other than his'
  252. if ($user->rights->societe->client->voir || $socid)
  253. {
  254. $moreforfilter.=$langs->trans('SalesRepresentatives'). ': ';
  255. $moreforfilter.=$formother->select_salesrepresentatives($search_sale,'search_sale',$user);
  256. }
  257. if ($moreforfilter)
  258. {
  259. print '<div class="liste_titre">';
  260. print $moreforfilter;
  261. print '</div>';
  262. }
  263. print '<table class="liste" width="100%">';
  264. print '<tr class="liste_titre">';
  265. print_liste_field_titre($langs->trans("Company"),$_SERVER["PHP_SELF"],"s.nom","",$param,'',$sortfield,$sortorder);
  266. print_liste_field_titre($langs->trans("Zip"),$_SERVER["PHP_SELF"],"s.zip","",$param,"",$sortfield,$sortorder);
  267. print_liste_field_titre($langs->trans("Town"),$_SERVER["PHP_SELF"],"s.town","",$param,"",$sortfield,$sortorder);
  268. print_liste_field_titre($langs->trans("State"),$_SERVER["PHP_SELF"],"s.fk_departement","",$param,'align="center"',$sortfield,$sortorder);
  269. print_liste_field_titre($langs->trans("DateCreation"),$_SERVER["PHP_SELF"],"s.datec","",$param,'align="center"',$sortfield,$sortorder);
  270. print_liste_field_titre($langs->trans("ProspectLevelShort"),$_SERVER["PHP_SELF"],"s.fk_prospectlevel","",$param,'align="center"',$sortfield,$sortorder);
  271. print_liste_field_titre($langs->trans("StatusProsp"),$_SERVER["PHP_SELF"],"s.fk_stcomm","",$param,'align="center"',$sortfield,$sortorder);
  272. print '<td class="liste_titre">&nbsp;</td>';
  273. print_liste_field_titre($langs->trans("Status"),$_SERVER["PHP_SELF"],"s.status","",$param,'align="center"',$sortfield,$sortorder);
  274. print '<td class="liste_titre">&nbsp;</td>';
  275. $parameters=array();
  276. $formconfirm=$hookmanager->executeHooks('printFieldListTitle',$parameters); // Note that $action and $object may have been modified by hook
  277. print "</tr>\n";
  278. print '<tr class="liste_titre">';
  279. print '<td class="liste_titre">';
  280. print '<input type="text" class="flat" name="search_nom" size="10" value="'.$search_nom.'">';
  281. print '</td>';
  282. print '<td class="liste_titre">';
  283. print '<input type="text" class="flat" name="search_zipcode" size="10" value="'.$search_zipcode.'">';
  284. print '</td>';
  285. print '<td class="liste_titre">';
  286. print '<input type="text" class="flat" name="search_town" size="10" value="'.$search_town.'">';
  287. print '</td>';
  288. print '<td class="liste_titre" align="center">';
  289. print '<input type="text" class="flat" name="search_state" size="10" value="'.$search_state.'">';
  290. print '</td>';
  291. print '<td align="center" class="liste_titre">';
  292. print '<input class="flat" type="text" size="10" name="search_datec" value="'.$search_datec.'">';
  293. print '</td>';
  294. // Added by Matelli
  295. print '<td class="liste_titre" align="center">';
  296. // Generate in $options_from the list of each option sorted
  297. $options_from = '<option value="">&nbsp;</option>';
  298. foreach ($tab_level as $tab_level_sortorder => $tab_level_label)
  299. {
  300. $options_from .= '<option value="'.$tab_level_sortorder.'"'.($search_level_from == $tab_level_sortorder ? ' selected="selected"':'').'>';
  301. $options_from .= $langs->trans($tab_level_label);
  302. $options_from .= '</option>';
  303. }
  304. // Reverse the list
  305. array_reverse($tab_level, true);
  306. // Generate in $options_to the list of each option sorted in the reversed order
  307. $options_to = '<option value="">&nbsp;</option>';
  308. foreach ($tab_level as $tab_level_sortorder => $tab_level_label)
  309. {
  310. $options_to .= '<option value="'.$tab_level_sortorder.'"'.($search_level_to == $tab_level_sortorder ? ' selected="selected"':'').'>';
  311. $options_to .= $langs->trans($tab_level_label);
  312. $options_to .= '</option>';
  313. }
  314. // Print these two select
  315. print $langs->trans("From").' <select class="flat" name="search_level_from">'.$options_from.'</select>';
  316. print ' ';
  317. print $langs->trans("To").' <select class="flat" name="search_level_to">'.$options_to.'</select>';
  318. print '</td>';
  319. print '<td class="liste_titre" align="center">';
  320. print '&nbsp;';
  321. print '</td>';
  322. print '<td class="liste_titre" align="center">';
  323. print '&nbsp;';
  324. print '</td>';
  325. print '<td class="liste_titre" align="center">';
  326. print $form->selectarray('search_status', array('0'=>$langs->trans('ActivityCeased'),'1'=>$langs->trans('InActivity')),$search_status);
  327. print '</td>';
  328. // Print the search button
  329. print '<td class="liste_titre" align="right">';
  330. print '<input class="liste_titre" name="button_search" type="image" src="'.img_picto($langs->trans("Search"),'search.png','','',1).'" value="'.dol_escape_htmltag($langs->trans("Search")).'" title="'.dol_escape_htmltag($langs->trans("Search")).'">';
  331. print '</td>';
  332. $parameters=array();
  333. $formconfirm=$hookmanager->executeHooks('printFieldListOption',$parameters); // Note that $action and $object may have been modified by hook
  334. print "</tr>\n";
  335. $i = 0;
  336. $var=true;
  337. $prospectstatic=new Prospect($db);
  338. $prospectstatic->client=2;
  339. while ($i < min($num,$conf->liste_limit))
  340. {
  341. $obj = $db->fetch_object($resql);
  342. $var=!$var;
  343. print '<tr '.$bc[$var].'>';
  344. print '<td>';
  345. $prospectstatic->id=$obj->rowid;
  346. $prospectstatic->nom=$obj->nom;
  347. $prospectstatic->status=$obj->status;
  348. $prospectstatic->code_client=$obj->code_client;
  349. $prospectstatic->client=$obj->client;
  350. $prospectstatic->fk_prospectlevel=$obj->fk_prospectlevel;
  351. print $prospectstatic->getNomUrl(1,'prospect');
  352. print '</td>';
  353. print "<td>".$obj->zip."&nbsp;</td>";
  354. print "<td>".$obj->town."&nbsp;</td>";
  355. print '<td align="center">'.$obj->departement.'</td>';
  356. // Creation date
  357. print '<td align="center">'.dol_print_date($db->jdate($obj->datec)).'</td>';
  358. // Level
  359. print '<td align="center">';
  360. print $prospectstatic->getLibProspLevel();
  361. print "</td>";
  362. // Statut
  363. print '<td align="center" class="nowrap">';
  364. print $prospectstatic->LibProspStatut($obj->fk_stcomm,2);
  365. print "</td>";
  366. //$sts = array(-1,0,1,2,3);
  367. print '<td align="right" class="nowrap">';
  368. foreach ($sts as $key => $value)
  369. {
  370. if ($value <> $obj->fk_stcomm)
  371. {
  372. print '<a href="'.$_SERVER["PHP_SELF"].'?socid='.$obj->rowid.'&amp;pstcomm='.$value.'&amp;action=cstc&amp;'.$param.($page?'&amp;page='.$page:'').'">';
  373. print img_action(0,$value);
  374. print '</a>&nbsp;';
  375. }
  376. }
  377. print '</td>';
  378. print '<td align="center">';
  379. print $prospectstatic->LibStatut($prospectstatic->status,3);
  380. print '</td>';
  381. print '<td></td>';
  382. $parameters=array('obj' => $obj);
  383. $formconfirm=$hookmanager->executeHooks('printFieldListValue',$parameters); // Note that $action and $object may have been modified by hook
  384. print "</tr>\n";
  385. $i++;
  386. }
  387. if ($num > $conf->liste_limit || $page > 0) print_barre_liste('', $page, $_SERVER["PHP_SELF"],$param,$sortfield,$sortorder,'',$num,$nbtotalofrecords);
  388. print "</table>";
  389. print "</form>";
  390. $db->free($resql);
  391. $parameters=array('sql' => $sql);
  392. $formconfirm=$hookmanager->executeHooks('printFieldListFooter',$parameters); // Note that $action and $object may have been modified by hook
  393. }
  394. else
  395. {
  396. dol_print_error($db);
  397. }
  398. llxFooter();
  399. $db->close();