dolibarr/scripts/cron/batch_stock_buildgraph.php

240 lines
5.2 KiB
PHP
Raw Normal View History

2006-12-06 11:25:04 +01:00
<?PHP
/* Copyright (C) 2006 Rodolphe Quiedeville <rodolphe@quiedeville.org>
* Copyright (C) 2007 Laurent Destailleur <eldy@users.sourceforge.net>
2006-12-06 11:25:04 +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 2 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, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/**
2008-11-26 13:21:33 +01:00
\file scripts/cron/stock-graph.php
\ingroup stock
2009-03-07 01:56:00 +01:00
\brief Cr<EFBFBD><EFBFBD> le graph de valorisation du stock
2008-11-26 13:21:33 +01:00
\version $Id$
*/
// Test si mode CLI
$sapi_type = php_sapi_name();
2008-11-26 13:21:33 +01:00
$script_file=__FILE__;
if (eregi('([^\\\/]+)$',$script_file,$reg)) $script_file=$reg[1];
if (substr($sapi_type, 0, 3) == 'cgi') {
2008-11-26 13:21:33 +01:00
echo "Erreur: Vous utilisez l'interpreteur PHP pour le mode CGI. Pour executer $script_file en ligne de commande, vous devez utiliser l'interpreteur PHP pour le mode CLI.\n";
exit;
}
2008-11-26 13:21:33 +01:00
// Recupere env dolibarr
$version='$Revision$';
$path=eregi_replace($script_file,'',$_SERVER["PHP_SELF"]);
require_once($path."../../htdocs/master.inc.php");
require_once(DOL_DOCUMENT_ROOT."/core/dolgraph.class.php");
2009-03-07 01:56:00 +01:00
require_once(DOL_DOCUMENT_ROOT."//cron/functions_cron.lib.php");
print '***** '.$script_file.' ('.$version.') *****'."\n";
print '--- start'."\n";
/*
* Main
*/
$error=0;
2006-12-06 11:25:04 +01:00
$verbose = 0;
2006-12-06 11:25:04 +01:00
for ($i = 1 ; $i < sizeof($argv) ; $i++)
{
2008-11-26 13:21:33 +01:00
if ($argv[$i] == "-v")
{
$verbose = 1;
}
if ($argv[$i] == "-vv")
{
$verbose = 2;
}
if ($argv[$i] == "-vvv")
{
$verbose = 3;
}
2006-12-06 11:25:04 +01:00
}
$dir = DOL_DATA_ROOT."/entrepot/temp";
$result=create_exdir($dir);
2006-12-06 11:50:26 +01:00
$sql = "SELECT distinct(fk_entrepot)";
$sql .= " FROM ".MAIN_DB_PREFIX."entrepot_valorisation";
2006-12-06 11:25:04 +01:00
$resql = $db->query($sql) ;
2006-12-06 11:50:26 +01:00
$entrepots = array();
if ($resql)
{
2008-11-26 13:21:33 +01:00
$i = 0;
while ($row = $db->fetch_row($resql))
{
$entrepots[$row[0]] = $row[0];
}
$db->free($resql);
2006-12-06 11:50:26 +01:00
}
else
{
dol_print_error($db,$sql);
2006-12-06 11:50:26 +01:00
}
2006-12-06 14:45:00 +01:00
$now = time();
$year = strftime('%Y',$now);
2006-12-18 14:37:29 +01:00
$day = strftime('%j', $now);
2006-12-06 14:45:00 +01:00
for ($i = 0 ; $i < strftime('%j',$now) ; $i++)
2006-12-06 11:25:04 +01:00
{
2008-11-26 13:21:33 +01:00
foreach ($entrepots as $key => $ent)
{
$values[$key][$i] = 0;
}
$values[0][$i] = 0;
$legends[$i] = strftime('%b',dol_mktime(12,0,0,1,1,2006) + ($i * 3600 * 24));
2008-11-26 13:21:33 +01:00
//print $legends[$i].strftime('%j',$now);
2006-12-06 11:25:04 +01:00
}
2006-12-06 11:50:26 +01:00
/*
* Read values
2006-12-06 11:50:26 +01:00
*/
2006-12-13 15:26:05 +01:00
$sql = "SELECT date_format(date_calcul,'%j'), valo_pmp, fk_entrepot";
2006-12-06 11:50:26 +01:00
$sql .= " FROM ".MAIN_DB_PREFIX."entrepot_valorisation as e";
2006-12-06 14:45:00 +01:00
$sql .= " WHERE date_format(date_calcul, '%Y') = '".$year."'";
2006-12-06 11:50:26 +01:00
$sql .= " ORDER BY date_calcul ASC";
$resql = $db->query($sql) ;
2006-12-06 11:25:04 +01:00
if ($resql)
{
2008-11-26 13:21:33 +01:00
$i = 0;
$last_day = 0;
while ($row = $db->fetch_row($resql))
2006-12-18 14:45:52 +01:00
{
2008-11-26 13:21:33 +01:00
if ($last_day > 0)
2006-12-18 14:45:52 +01:00
{
2008-11-26 13:21:33 +01:00
for ($j = $last_day + 1 ; $j < $row[0] ; $j++)
{
foreach ($entrepots as $key => $ent)
{
$values[$key][$j] = $values[$key][$last_day];
}
$values[0][$j] = $values[0][$last_day];
}
2006-12-18 14:45:52 +01:00
}
2008-11-26 13:21:33 +01:00
$last_day = $row[0];
2006-12-18 14:45:52 +01:00
2008-11-26 13:21:33 +01:00
$max_day = $row[0];
$values[$row[2]][$row[0]] = $row[1];
$values[0][$row[0]] += $row[1];
2006-12-19 10:00:20 +01:00
2008-11-26 13:21:33 +01:00
$total[$row[2]] += abs($row[1]);
$total[0] += abs($row[1]);
$i++;
}
$db->free($resql);
2006-12-06 11:25:04 +01:00
}
else
{
dol_print_error($db,$sql);
2006-12-06 11:25:04 +01:00
}
2006-12-18 14:37:29 +01:00
for ($i = $max_day + 1 ; $i < ($day + 1) ; $i++)
{
2008-11-26 13:21:33 +01:00
foreach ($entrepots as $key => $ent)
{
$values[$key][$i] = $values[$key][$max_day];
}
$values[0][$i] = $values[0][$max_day];
2006-12-18 14:37:29 +01:00
}
2009-03-07 01:56:00 +01:00
// PMP = (quantit<69>s en stock x pmp ancien + nouvelles quantit<69>s x prix d'acquisition)/ (anciennes quantit<69>s + nouvelles quantit<69>s)
2006-12-18 14:37:29 +01:00
/*
* For each warehouse
*/
2006-12-06 14:45:00 +01:00
foreach ($entrepots as $key => $ent)
{
2008-11-26 13:21:33 +01:00
$file = $dir ."/entrepot-".$key."-".$year.".png";
2009-03-07 01:56:00 +01:00
$title = "Valorisation PMP du stock de l'entrep<65>t (euros HT) sur l'ann<6E>e ".$year;
2006-12-19 10:00:20 +01:00
2008-11-26 13:21:33 +01:00
if ($total[$key] > 0)
graph_datas($file, $title, $values[$key], $legends);
2006-12-19 10:00:20 +01:00
2008-11-26 13:21:33 +01:00
if ($verbose)
print "$file\n";
2006-12-06 14:45:00 +01:00
}
2006-12-06 14:53:40 +01:00
/*
* For all warehouses
2006-12-06 14:53:40 +01:00
*/
$file = $dir."/entrepot-".$year.".png";
2009-03-07 01:56:00 +01:00
$title = "Valorisation PMP (Prix Moyen Pond<6E>r<EFBFBD>) du stock global (euros HT) sur l'ann<6E>e ".$year;
2006-12-06 11:25:04 +01:00
2006-12-19 10:00:20 +01:00
if ($total[$key] > 0)
2008-11-26 13:21:33 +01:00
graph_datas($file, $title, $values[0], $legends);
2006-12-19 10:00:20 +01:00
2006-12-06 14:53:40 +01:00
if ($verbose)
2008-11-26 13:21:33 +01:00
print "$file\n";
2006-12-06 11:25:04 +01:00
/** \brief Build graph
2008-11-26 13:21:33 +01:00
* \param file File
* \param title Title
* \param values Value
* \param legends Legend
*/
2006-12-06 11:25:04 +01:00
function graph_datas($file, $title, $values, $legends)
{
$width=800;
$height=230;
2008-11-26 13:21:33 +01:00
print "Build graph ".$file."\n";
$newvalues=array();
foreach ($values as $abs=>$ord)
{
$newvalues[]=array($legends[$abs],$ord);
}
2008-11-26 13:21:33 +01:00
$px = new DolGraph();
$px->SetData($newvalues);
//$px->SetLegend('');
2008-11-26 13:21:33 +01:00
$px->SetMaxValue($px->GetCeilMaxValue());
$px->SetMinValue($px->GetFloorMinValue());
$px->SetTitle($title);
$px->SetWidth($width);
$px->SetHeight($height);
$px->SetType('lines');
$px->setBgColor('default');
2008-11-26 13:21:33 +01:00
$px->setBgColorGrid(array(255,255,255));
$px->SetHideXGrid(true);
$px->SetLabelInterval(31);
$px->draw($file);
2006-12-06 11:25:04 +01:00
}
2009-03-07 01:56:00 +01:00
if (! $error)
{
print '--- end ok'."\n";
}
else
{
print '--- end error code='.$error."\n";
}
2006-12-06 11:25:04 +01:00
?>