dolibarr/htdocs/commande/stats/index.php

373 lines
13 KiB
PHP
Raw Normal View History

<?php
2003-11-09 18:32:09 +01:00
/* Copyright (C) 2001-2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
* Copyright (C) 2004-2013 Laurent Destailleur <eldy@users.sourceforge.net>
2018-10-27 14:43:12 +02:00
* Copyright (C) 2005-2012 Regis Houssin <regis.houssin@inodbox.com>
* Copyright (C) 2012 Marcos García <marcosgdf@gmail.com>
2015-04-18 21:28:55 +02:00
* Copyright (C) 2015 Jean-François Ferry <jfefe@aternatik.fr>
2003-11-09 18:32:09 +01:00
*
* 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
2003-11-09 18:32:09 +01:00
* (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
2019-09-23 21:55:30 +02:00
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2003-11-09 18:32:09 +01:00
*/
/**
2009-05-18 13:39:51 +02:00
* \file htdocs/commande/stats/index.php
* \ingroup commande
2011-06-13 15:07:10 +02:00
* \brief Page with customers or suppliers orders statistics
*/
require '../../main.inc.php';
require_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php';
require_once DOL_DOCUMENT_ROOT.'/commande/class/commandestats.class.php';
require_once DOL_DOCUMENT_ROOT.'/core/class/html.formorder.class.php';
require_once DOL_DOCUMENT_ROOT.'/core/class/dolgraph.class.php';
2013-04-04 21:13:51 +02:00
$WIDTH=DolGraph::getDefaultGraphSizeForStats('width');
$HEIGHT=DolGraph::getDefaultGraphSizeForStats('height');
$mode=GETPOST("mode")?GETPOST("mode"):'customer';
2009-05-18 13:39:51 +02:00
if ($mode == 'customer' && ! $user->rights->commande->lire) accessforbidden();
if ($mode == 'supplier' && ! $user->rights->fournisseur->commande->lire) accessforbidden();
$object_status=GETPOST('object_status');
$userid=GETPOST('userid', 'int');
$socid=GETPOST('socid', 'int');
2009-05-18 13:39:51 +02:00
// Security check
if ($user->socid > 0)
2003-11-24 13:18:02 +01:00
{
2011-06-13 15:07:10 +02:00
$action = '';
$socid = $user->socid;
2003-11-24 13:18:02 +01:00
}
2003-11-09 18:32:09 +01:00
$nowyear=strftime("%Y", dol_now());
$year = GETPOST('year')>0?GETPOST('year'):$nowyear;
//$startyear=$year-2;
$startyear=$year-1;
2008-09-08 15:32:37 +02:00
$endyear=$year;
2018-05-26 20:32:23 +02:00
// Load translation files required by the page
$langs->loadLangs(array('orders', 'companies', 'other', 'suppliers'));
2009-05-18 13:39:51 +02:00
2008-09-08 21:04:19 +02:00
2008-09-08 15:32:37 +02:00
/*
* View
*/
2009-05-18 13:39:51 +02:00
$form=new Form($db);
$formorder=new FormOrder($db);
2009-05-18 13:39:51 +02:00
if ($mode == 'customer')
2008-09-08 21:04:19 +02:00
{
2011-06-13 15:07:10 +02:00
$title=$langs->trans("OrdersStatistics");
$dir=$conf->commande->dir_temp;
2008-09-08 21:04:19 +02:00
}
2009-05-18 13:39:51 +02:00
if ($mode == 'supplier')
2008-09-08 21:04:19 +02:00
{
$title=$langs->trans("OrdersStatisticsSuppliers").' ('.$langs->trans("SentToSuppliers").")";
2017-06-20 12:17:13 +02:00
$dir=$conf->fournisseur->commande->dir_temp;
2008-09-08 21:04:19 +02:00
}
2003-11-09 18:32:09 +01:00
llxHeader('', $title);
2019-09-30 10:14:55 +02:00
print load_fiche_titre($title, '', 'commercial');
2008-09-08 15:32:37 +02:00
dol_mkdir($dir);
2008-09-08 15:32:37 +02:00
$stats = new CommandeStats($db, $socid, $mode, ($userid>0?$userid:0));
if ($mode == 'customer')
{
2017-09-08 13:23:12 +02:00
if ($object_status != '' && $object_status >= -1) $stats->where .= ' AND c.fk_statut IN ('.$db->escape($object_status).')';
}
if ($mode == 'supplier')
{
2017-09-08 13:23:12 +02:00
if ($object_status != '' && $object_status >= 0) $stats->where .= ' AND c.fk_statut IN ('.$db->escape($object_status).')';
}
2008-09-08 15:32:37 +02:00
// Build graphic number of object
$data = $stats->getNbByMonthWithPrevYear($endyear, $startyear);
2008-09-08 15:32:37 +02:00
//var_dump($data);
// $data = array(array('Lib',val1,val2,val3),...)
if (!$user->rights->societe->client->voir || $user->socid)
{
2011-06-13 15:07:10 +02:00
$filenamenb = $dir.'/ordersnbinyear-'.$user->id.'-'.$year.'.png';
if ($mode == 'customer') $fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstats&file=ordersnbinyear-'.$user->id.'-'.$year.'.png';
if ($mode == 'supplier') $fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstatssupplier&file=ordersnbinyear-'.$user->id.'-'.$year.'.png';
}
else
{
2011-06-13 15:07:10 +02:00
$filenamenb = $dir.'/ordersnbinyear-'.$year.'.png';
if ($mode == 'customer') $fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstats&file=ordersnbinyear-'.$year.'.png';
if ($mode == 'supplier') $fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstatssupplier&file=ordersnbinyear-'.$year.'.png';
}
2003-11-16 17:45:13 +01:00
$px1 = new DolGraph();
$mesg = $px1->isGraphKo();
if (! $mesg)
{
$px1->SetData($data);
$i=$startyear;$legend=array();
2011-06-13 15:07:10 +02:00
while ($i <= $endyear)
{
$legend[]=$i;
$i++;
}
$px1->SetLegend($legend);
$px1->SetMaxValue($px1->GetCeilMaxValue());
$px1->SetMinValue(min(0, $px1->GetFloorMinValue()));
$px1->SetWidth($WIDTH);
$px1->SetHeight($HEIGHT);
$px1->SetYLabel($langs->trans("NbOfOrder"));
$px1->SetShading(3);
$px1->SetHorizTickIncrement(1);
$px1->mode='depth';
$px1->SetTitle($langs->trans("NumberOfOrdersByMonth"));
$px1->draw($filenamenb, $fileurlnb);
2009-05-18 13:39:51 +02:00
}
2008-09-08 15:32:37 +02:00
// Build graphic amount of object
$data = $stats->getAmountByMonthWithPrevYear($endyear, $startyear);
2008-09-08 15:32:37 +02:00
//var_dump($data);
// $data = array(array('Lib',val1,val2,val3),...)
if (!$user->rights->societe->client->voir || $user->socid)
2008-09-08 15:32:37 +02:00
{
2011-06-13 15:07:10 +02:00
$filenameamount = $dir.'/ordersamountinyear-'.$user->id.'-'.$year.'.png';
if ($mode == 'customer') $fileurlamount = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstats&file=ordersamountinyear-'.$user->id.'-'.$year.'.png';
if ($mode == 'supplier') $fileurlamount = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstatssupplier&file=ordersamountinyear-'.$user->id.'-'.$year.'.png';
2008-09-08 15:32:37 +02:00
}
else
{
2011-06-13 15:07:10 +02:00
$filenameamount = $dir.'/ordersamountinyear-'.$year.'.png';
if ($mode == 'customer') $fileurlamount = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstats&file=ordersamountinyear-'.$year.'.png';
if ($mode == 'supplier') $fileurlamount = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstatssupplier&file=ordersamountinyear-'.$year.'.png';
2008-09-08 15:32:37 +02:00
}
$px2 = new DolGraph();
$mesg = $px2->isGraphKo();
2008-09-08 15:32:37 +02:00
if (! $mesg)
{
$px2->SetData($data);
$i=$startyear;$legend=array();
2011-06-13 15:07:10 +02:00
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("AmountOfOrders"));
$px2->SetShading(3);
$px2->SetHorizTickIncrement(1);
$px2->mode='depth';
$px2->SetTitle($langs->trans("AmountOfOrdersByMonthHT"));
$px2->draw($filenameamount, $fileurlamount);
2008-09-08 15:32:37 +02:00
}
$data = $stats->getAverageByMonthWithPrevYear($endyear, $startyear);
if (!$user->rights->societe->client->voir || $user->socid)
{
$filename_avg = $dir.'/ordersaverage-'.$user->id.'-'.$year.'.png';
if ($mode == 'customer') $fileurl_avg = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstats&file=ordersaverage-'.$user->id.'-'.$year.'.png';
if ($mode == 'supplier') $fileurl_avg = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstatssupplier&file=ordersaverage-'.$user->id.'-'.$year.'.png';
}
else
{
$filename_avg = $dir.'/ordersaverage-'.$year.'.png';
if ($mode == 'customer') $fileurl_avg = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstats&file=ordersaverage-'.$year.'.png';
if ($mode == 'supplier') $fileurl_avg = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstatssupplier&file=ordersaverage-'.$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->SetYLabel($langs->trans("AmountAverage"));
$px3->SetMaxValue($px3->GetCeilMaxValue());
$px3->SetMinValue($px3->GetFloorMinValue());
$px3->SetWidth($WIDTH);
$px3->SetHeight($HEIGHT);
$px3->SetShading(3);
$px3->SetHorizTickIncrement(1);
$px3->mode='depth';
$px3->SetTitle($langs->trans("AmountAverage"));
$px3->draw($filename_avg, $fileurl_avg);
}
// Show array
$data = $stats->getAllByYear();
$arrayyears=array();
foreach($data as $val) {
2012-07-13 10:15:47 +02:00
if (! empty($val['year'])) {
$arrayyears[$val['year']]=$val['year'];
}
}
if (! count($arrayyears)) $arrayyears[$nowyear]=$nowyear;
$h=0;
$head = array();
2012-02-12 23:29:31 +01:00
$head[$h][0] = DOL_URL_ROOT . '/commande/stats/index.php?mode='.$mode;
$head[$h][1] = $langs->trans("ByMonthYear");
$head[$h][2] = 'byyear';
$h++;
2012-02-12 23:29:31 +01:00
if ($mode == 'customer') $type='order_stats';
if ($mode == 'supplier') $type='supplier_order_stats';
complete_head_from_modules($conf, $langs, null, $head, $h, $type);
2012-02-12 23:29:31 +01:00
2017-03-31 18:40:53 +02:00
dol_fiche_head($head, 'byyear', $langs->trans("Statistics"), -1);
2013-04-04 19:42:21 +02:00
print '<div class="fichecenter"><div class="fichethirdleft">';
2012-07-26 17:45:08 +02:00
// Show filter box
print '<form name="stats" method="POST" action="'.$_SERVER["PHP_SELF"].'">';
print '<input type="hidden" name="mode" value="'.$mode.'">';
2019-11-05 21:24:41 +01:00
print '<table class="noborder centpercent">';
print '<tr class="liste_titre"><td class="liste_titre" colspan="2">'.$langs->trans("Filter").'</td></tr>';
// Company
2019-01-19 12:28:18 +01:00
print '<tr><td class="left">'.$langs->trans("ThirdParty").'</td><td class="left">';
if ($mode == 'customer') $filter='s.client IN (1,2,3)';
if ($mode == 'supplier') $filter='s.fournisseur = 1';
print $form->select_company($socid, 'socid', $filter, 1, 0, 0, array(), 0, '', 'style="width: 95%"');
print '</td></tr>';
// User
2019-01-19 12:28:18 +01:00
print '<tr><td class="left">'.$langs->trans("CreatedBy").'</td><td class="left">';
print $form->select_dolusers($userid, 'userid', 1, '', 0, '', '', 0, 0, 0, '', 0, '', 'maxwidth300');
// Status
2019-01-19 12:28:18 +01:00
print '<tr><td class="left">'.$langs->trans("Status").'</td><td class="left">';
if ($mode == 'customer')
{
$liststatus=array(
Commande::STATUS_DRAFT=>$langs->trans("StatusOrderDraft"),
Commande::STATUS_VALIDATED=>$langs->trans("StatusOrderValidated"),
Commande::STATUS_SHIPMENTONPROCESS=>$langs->trans("StatusOrderSent"),
Commande::STATUS_CLOSED=>$langs->trans("StatusOrderDelivered"),
Commande::STATUS_CANCELED=>$langs->trans("StatusOrderCanceled")
);
print $form->selectarray('object_status', $liststatus, GETPOST('object_status', 'int'), -4);
}
if ($mode == 'supplier')
{
$formorder->selectSupplierOrderStatus((strstr($object_status, ',')?-1:$object_status), 0, 'object_status');
}
print '</td></tr>';
// Year
2019-01-17 14:27:07 +01:00
print '<tr><td class="left">'.$langs->trans("Year").'</td><td class="left">';
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>';
2018-10-26 15:46:17 +02:00
print '<div class="div-table-responsive-no-min">';
2019-11-05 21:24:41 +01:00
print '<table class="noborder centpercent">';
2015-10-09 20:00:43 +02:00
print '<tr class="liste_titre" height="24">';
2019-12-12 10:31:08 +01:00
print '<td class="center">'.$langs->trans("Year").'</td>';
2019-01-23 17:34:30 +01:00
print '<td class="right">'.$langs->trans("NbOfOrders").'</td>';
print '<td class="right">%</td>';
print '<td class="right">'.$langs->trans("AmountTotal").'</td>';
print '<td class="right">%</td>';
print '<td class="right">'.$langs->trans("AmountAverage").'</td>';
print '<td class="right">%</td>';
2008-09-08 15:32:37 +02:00
print '</tr>';
2009-05-18 13:39:51 +02:00
2008-09-08 15:32:37 +02:00
$oldyear=0;
foreach ($data as $val)
2003-11-16 17:45:13 +01:00
{
2012-07-13 10:15:47 +02:00
$year = $val['year'];
while (! empty($year) && $oldyear > $year+1)
{ // If we have empty year
2015-08-12 22:28:30 +02:00
$oldyear--;
2017-06-20 12:17:13 +02:00
2017-05-10 16:41:19 +02:00
print '<tr class="oddeven" height="24">';
2015-08-12 22:28:30 +02:00
print '<td align="center"><a href="'.$_SERVER["PHP_SELF"].'?year='.$oldyear.'&amp;mode='.$mode.($socid>0?'&socid='.$socid:'').($userid>0?'&userid='.$userid:'').'">'.$oldyear.'</a></td>';
2019-01-23 17:34:30 +01:00
print '<td class="right">0</td>';
print '<td class="right"></td>';
print '<td class="right">0</td>';
print '<td class="right"></td>';
print '<td class="right">0</td>';
print '<td class="right"></td>';
2015-08-12 22:28:30 +02:00
print '</tr>';
2012-07-13 10:15:47 +02:00
}
2017-06-20 12:17:13 +02:00
2017-05-10 16:41:19 +02:00
print '<tr class="oddeven" height="24">';
print '<td align="center"><a href="'.$_SERVER["PHP_SELF"].'?year='.$year.'&amp;mode='.$mode.($socid>0?'&socid='.$socid:'').($userid>0?'&userid='.$userid:'').'">'.$year.'</a></td>';
2019-01-23 17:34:30 +01:00
print '<td class="right">'.$val['nb'].'</td>';
print '<td class="right" style="'.(($val['nb_diff'] >= 0) ? 'color: green;':'color: red;').'">'.round($val['nb_diff']).'</td>';
print '<td class="right">'.price(price2num($val['total'], 'MT'), 1).'</td>';
2019-01-23 17:34:30 +01:00
print '<td class="right" style="'.(($val['total_diff'] >= 0) ? 'color: green;':'color: red;').'">'.round($val['total_diff']).'</td>';
print '<td class="right">'.price(price2num($val['avg'], 'MT'), 1).'</td>';
2019-01-23 17:34:30 +01:00
print '<td class="right" style="'.(($val['avg_diff'] >= 0) ? 'color: green;':'color: red;').'">'.round($val['avg_diff']).'</td>';
2012-07-13 10:15:47 +02:00
print '</tr>';
$oldyear=$year;
2003-11-09 18:32:09 +01:00
}
2003-11-24 13:18:02 +01:00
print '</table>';
2018-10-26 15:46:17 +02:00
print '</div>';
2003-11-24 13:18:02 +01:00
2003-11-09 18:32:09 +01:00
2013-04-04 19:42:21 +02:00
print '</div><div class="fichetwothirdright"><div class="ficheaddleft">';
2008-09-08 15:32:37 +02:00
// Show graphs
2019-11-05 21:24:41 +01:00
print '<table class="border centpercent"><tr class="pair nohover"><td align="center">';
2008-09-08 15:32:37 +02:00
if ($mesg) { print $mesg; }
else {
print $px1->show();
2011-06-13 15:07:10 +02:00
print "<br>\n";
print $px2->show();
print "<br>\n";
print $px3->show();
2008-09-08 15:32:37 +02:00
}
print '</td></tr></table>';
2013-04-04 19:42:21 +02:00
print '</div></div></div>';
print '<div style="clear:both"></div>';
2008-09-08 15:32:37 +02:00
dol_fiche_end();
2018-07-29 19:16:28 +02:00
// End of page
llxFooter();
$db->close();