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