forked from FHenry/lead
-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.php
436 lines (365 loc) · 12.3 KB
/
index.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
<?php
/* Lead
* Copyright (C) 2014-2015 Florian HENRY <[email protected]>
*
* 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/>.
*/
$res = @include '../main.inc.php'; // For root directory
if (! $res)
$res = @include '../../main.inc.php'; // For "custom" directory
if (! $res)
die("Include of main fails");
require_once DOL_DOCUMENT_ROOT.'/core/class/dolgraph.class.php';
require_once 'lib/lead.lib.php';
require_once './class/leadstats.class.php';
// Security check
if (! $user->hasRight('lead', 'read'))
accessforbidden();
$WIDTH=DolGraph::getDefaultGraphSizeForStats('width');
$HEIGHT=DolGraph::getDefaultGraphSizeForStats('height');
$userid=GETPOST('userid','int');
$socid=GETPOST('socid','int');
$mode=GETPOST('mode','alpha');
// Security check
if (((float) DOL_VERSION < 13) ? $user->societe_id : $user->socid > 0)
{
$action = '';
$socid = ((float) DOL_VERSION < 13) ? $user->societe_id : $user->socid;
}
$nowyear=strftime("%Y", dol_now());
$year = GETPOST('year','int')>0?GETPOST('year','int'):$nowyear;
//$startyear=$year-2;
$startyear=$year-1;
$endyear=$year;
$showpointvalue=1;
$nocolor=0;
$includeuserlist=array();
if (getDolGlobalInt('LEAD_GRP_USER_AFFECT')) {
dol_include_once('/user/class/usergroup.class.php');
$usergroup=new UserGroup($db);
$result=$usergroup->fetch(getDolGlobalInt('LEAD_GRP_USER_AFFECT'));
if ($result < 0)
setEventMessage($usergroup->error, 'errors');
$includeuserlisttmp=$usergroup->listUsersForGroup();
if (is_array($includeuserlisttmp) && count($includeuserlisttmp)>0) {
foreach($includeuserlisttmp as $usertmp) {
$includeuserlist[]=$usertmp->id;
}
}
}
$langs->load('lead@lead');
llxHeader('', $langs->trans('Module103111Name'));
$stats_lead= new LeadStats($db);
if (!empty($userid) && $userid!=-1) $stats_lead->userid=$userid;
if (!empty($socid) && $socid!=-1) $stats_lead->socid=$socid;
if (!empty($year)) $stats_lead->year=$year;
$stats_lead->year=0;
$data_all_year = $stats_lead->getAllByYear();
if (!empty($year)) $stats_lead->year=$year;
$arrayyears=array();
foreach($data_all_year as $val) {
$arrayyears[$val['year']]=$val['year'];
}
if (! count($arrayyears)) $arrayyears[$nowyear]=$nowyear;
$data1 = $stats_lead->getAllLeadByType();
if (!is_array($data1) && $data1<0) {
setEventMessages($stats_lead->error, null, 'errors');
}
if (empty($data1))
{
$showpointvalue=0;
$nocolor=1;
$data1=array(array(0=>$langs->trans("None"),1=>1));
}
$filenamenb = $conf->lead->dir_output . "/stats/leadbytype.png";
$fileurlnb = DOL_URL_ROOT . '/viewimage.php?modulepart=leadstats&file=leadbytype.png';
$px = new DolGraph();
$mesg = $px->isGraphKo();
if (empty($mesg)) {
$i=0;$tot=count($data1);$legend=array();
while ($i < $tot)
{
$data1[$i][0]=$data1[$i][0]; // Required to avoid error "Could not draw pie with labels contained inside canvas"
$legend[]=$data1[$i][0];
$i++;
}
$px->SetData($data1);
unset($data1);
if ($nocolor)
$px->SetDataColor(array (
array (
220,
220,
220
)
));
if(function_exists('SetPrecisionY')){
$px->SetPrecisionY(0);
}
$px->SetLegend($legend);
$px->setShowLegend(0);
$px->setShowPointValue($showpointvalue);
$px->setShowPercent(1);
$px->SetMaxValue($px->GetCeilMaxValue());
$px->SetWidth(300);
$px->SetHeight(300);
$px->SetShading(3);
$px->SetHorizTickIncrement(1);
$px->SetCssPrefix("cssboxes");
$px->SetType(array (
'pie'
));
$px->SetTitle($langs->trans('LeadNbLeadByType'));
$result=$px->draw($filenamenb, $fileurlnb);
if ($result<0) {
setEventMessages($px->error, null, 'errors');
}
} else {
setEventMessages(null, $mesgs, 'errors');
}
$stringtoshow = '<div class="fichecenter"><div class="containercenter"><div class="fichehalfleft">';
$stringtoshow .= $px->show();
$stringtoshow .= '</div>';
$data1 = $stats_lead->getAllLeadByStatus();
if (!is_array($data1) && $data1<0) {
setEventMessages($stats_lead->error, null, 'errors');
}
if (empty($data1))
{
$showpointvalue=0;
$nocolor=1;
$data1=array(array(0=>$langs->trans("None"),1=>1));
}
$filenamenb = $conf->lead->dir_output . "/stats/leadbystatus.png";
$fileurlnb = DOL_URL_ROOT . '/viewimage.php?modulepart=leadstats&file=leadbystatus.png';
$px = new DolGraph();
$mesg = $px->isGraphKo();
if (empty($mesg)) {
$i=0;$tot=count($data1);$legend=array();
while ($i < $tot)
{
$data1[$i][0]=$data1[$i][0]; // Required to avoid error "Could not draw pie with labels contained inside canvas"
$legend[]=$data1[$i][0];
$i++;
}
$px->SetData($data1);
unset($data1);
if ($nocolor)
$px->SetDataColor(array (
array (
220,
220,
220
)
));
if(function_exists('SetPrecisionY')) $px->SetPrecisionY(0);
$px->setShowLegend(0);
$px->setShowPointValue($showpointvalue);
$px->setShowPercent(1);
$px->SetMaxValue($px->GetCeilMaxValue());
$px->SetWidth(300);
$px->SetHeight(300);
$px->SetShading(3);
$px->SetHorizTickIncrement(1);
$px->SetCssPrefix("cssboxes");
$px->SetType(array (
'pie'
));
$px->SetTitle($langs->trans('LeadNbLeadByStatus'));
$result=$px->draw($filenamenb, $fileurlnb);
if ($result<0) {
setEventMessages($px->error, null, 'errors');
}
} else {
setEventMessages(null, $mesgs, 'errors');
}
$stringtoshow .='<div class="fichehalfright">';
$stringtoshow .= $px->show();
$stringtoshow .= '</div></div></div>';
// Build graphic number of object
// $data = array(array('Lib',val1,val2,val3),...)
$data = $stats_lead->getNbByMonthWithPrevYear($endyear,$startyear);
//var_dump($data);
$filenamenb = $conf->lead->dir_output . "/stats/leadnbprevyear-".$year.".png";
$fileurlnb = DOL_URL_ROOT . '/viewimage.php?modulepart=leadstats&file=leadnbprevyear-'.$year.'.png';
$px1 = new DolGraph();
$mesg = $px1->isGraphKo();
if (! $mesg)
{
$px1->SetData($data);
if(function_exists('SetPrecisionY')) $px->SetPrecisionY(0);
$i=$startyear;$legend=array();
while ($i <= $endyear)
{
$legend[]=$i;
$i++;
}
$px1->SetLegend($legend);
$px1->SetMaxValue($px1->GetCeilMaxValue());
$px1->SetWidth($WIDTH);
$px1->SetHeight($HEIGHT);
$px1->SetYLabel($langs->trans("LeadNbLead"));
$px1->SetShading(3);
$px1->SetHorizTickIncrement(1);
if(function_exists('SetPrecisionY')) $px->SetPrecisionY(0);
$px1->mode='depth';
$px1->SetTitle($langs->trans("LeadNbLeadByMonth"));
$px1->draw($filenamenb,$fileurlnb);
}
// Build graphic amount of object
$data = $stats_lead->getAmountByMonthWithPrevYear($endyear,$startyear);
//var_dump($data);
// $data = array(array('Lib',val1,val2,val3),...)
$filenamenb = $conf->lead->dir_output . "/stats/leadamountprevyear-".$year.".png";
$fileurlnb = DOL_URL_ROOT . '/viewimage.php?modulepart=leadstats&file=leadamountprevyear-'.$year.'.png';
$px2 = new DolGraph();
$mesg = $px2->isGraphKo();
if (! $mesg)
{
$px2->SetData($data);
$i=$startyear;$legend=array();
while ($i <= $endyear)
{
$legend[]=$i;
$i++;
}
$px2->SetLegend($legend);
$px2->SetMaxValue($px2->GetCeilMaxValue());
$px2->SetMinValue(min(0,$px2->GetFloorMinValue()));
$px2->SetWidth($WIDTH);
$px2->SetHeight($HEIGHT);
$px2->SetYLabel($langs->trans("LeadAmountOfLead"));
$px2->SetShading(3);
$px2->SetHorizTickIncrement(1);
if(function_exists('SetPrecisionY')) $px->SetPrecisionY(0);
$px2->mode='depth';
$px2->SetTitle($langs->trans("LeadAmountOfLeadsByMonth"));
$px2->draw($filenamenb,$fileurlnb);
}
// Build graphic with transformation rate
$data = $stats_lead->getTransformRateByMonthWithPrevYear($endyear,$startyear);
//var_dump($data);
// $data = array(array('Lib',val1,val2,val3),...)
$filenamenb = $conf->lead->dir_output . "/stats/leadtransrateprevyear-".$year.".png";
$fileurlnb = DOL_URL_ROOT . '/viewimage.php?modulepart=leadstats&file=leadtransrateprevyear-'.$year.'.png';
$px3 = new DolGraph();
$mesg = $px3->isGraphKo();
if (! $mesg)
{
$px3->SetData($data);
$i=$startyear;$legend=array();
while ($i <= $endyear)
{
$legend[]=$i;
$i++;
}
$px3->SetLegend($legend);
$px3->SetMaxValue($px3->GetCeilMaxValue());
$px3->SetMinValue(min(0,$px3->GetFloorMinValue()));
$px3->SetWidth($WIDTH);
$px3->SetHeight($HEIGHT);
$px3->SetYLabel($langs->trans("LeadTransRateOfLead"));
$px3->SetShading(3);
$px3->SetHorizTickIncrement(1);
if(function_exists('SetPrecisionY')) $px3->SetPrecisionY(0);
$px3->mode='depth';
$px3->SetTitle($langs->trans("LeadTransRateOfLeadsByMonth"));
$px3->draw($filenamenb,$fileurlnb);
}
$stringtoshow.= '<table class="border" width="100%"><tr valign="top"><td align="center">';
if ($mesg) { print $mesg; }
else {
$stringtoshow.= $px1->show();
$stringtoshow.= "<br>\n";
$stringtoshow.= $px2->show();
$stringtoshow.= "<br>\n";
$stringtoshow.= $px3->show();
}
$stringtoshow.= '</td></tr></table>';
$head = lead_stats_prepare_head();
dol_fiche_head($head,'stat',$langs->trans("Statistics"), 0, dol_buildpath('/lead/img/object_lead.png', 1), 1);
$form=new Form($db);
print '<div class="fichecenter"><div class="fichethirdleft">';
print '<form name="stats" method="POST" action="'.$_SERVER["PHP_SELF"].'">';
$newToken = function_exists('newToken') ? newToken() : $_SESSION['newtoken'];
print '<input type="hidden" name="token" id="tocken" value="'.$newToken.'">';
print '<table class="border" width="100%">';
print '<tr class="liste_titre"><td class="liste_titre" colspan="2">'.$langs->trans("Filter").'</td></tr>';
// Company
print '<tr><td>'.$langs->trans("ThirdParty").'</td><td>';
$filter='';
if ($mode == 'customer') $filter='s.client in (1,2,3)';
if ($mode == 'supplier') $filter='s.fournisseur = 1';
print $form->select_thirdparty_list($socid,'socid',$filter,1);
print '</td></tr>';
// User
print '<tr><td>'.$langs->trans("LeadCommercial").'</td><td>';
print $form->select_dolusers($userid, 'userid', 1, array(),0,$includeuserlist);
print '</td></tr>';
// Year
print '<tr><td>'.$langs->trans("Year").'</td><td>';
if (! in_array($year,$arrayyears)) $arrayyears[$year]=$year;
if (! in_array($nowyear,$arrayyears)) $arrayyears[$nowyear]=$nowyear;
arsort($arrayyears);
print $form->selectarray('year',$arrayyears,$year,0);
print '</td></tr>';
print '<tr><td align="center" colspan="2"><input type="submit" name="submit" class="button" value="'.$langs->trans("Refresh").'"></td></tr>';
print '</table>';
print '</form>';
print '<br><br>';
print '<table class="border" width="100%">';
print '<tr style="height:24px">';
print '<td align="center">'.$langs->trans("Year").'</td>';
print '<td align="center">'.$langs->trans("LeadNbLead").'</td>';
print '<td align="center">'.$langs->trans("AmountTotal").'</td>';
print '<td align="center">'.$langs->trans("AmountAverage").'</td>';
print '</tr>';
$oldyear=0;
foreach ($data_all_year as $val)
{
$year = $val['year'];
while ($year && $oldyear > $year+1)
{ // If we have empty year
$oldyear--;
print '<tr style="height:24px">';
print '<td align="center"><a href="'.$_SERVER["PHP_SELF"].'?year='.$oldyear.'&mode='.$mode.($socid>0?'&socid='.$socid:'').($userid>0?'&userid='.$userid:'').'">'.$oldyear.'</a></td>';
print '<td align="right">0</td>';
print '<td align="right">0</td>';
print '<td align="right">0</td>';
print '</tr>';
}
print '<tr style="height:24px">';
print '<td align="center"><a href="'.$_SERVER["PHP_SELF"].'?year='.$year.'&mode='.$mode.($socid>0?'&socid='.$socid:'').($userid>0?'&userid='.$userid:'').'">'.$year.'</a></td>';
print '<td align="right">'.$val['nb'].'</td>';
print '<td align="right">'.price(price2num($val['total'],'MT'),1).'</td>';
print '<td align="right">'.price(price2num($val['avg'],'MT'),1).'</td>';
print '</tr>';
$oldyear=$year;
}
print '</table>';
print '</div><div class="fichetwothirdright"><div class="ficheaddleft">';
// Show graphs
print '<table class="border" width="100%"><tr valign="top"><td align="center">';
if ($mesg) { print $mesg; }
else {
print $stringtoshow;
}
print '</td></tr></table>';
print '</div></div></div>';
print '<div style="clear:both"></div>';
print '<table class="noborder" width="100%">';
print '</table>';
dol_fiche_end();
llxFooter();
$db->close();