New: Dolgraph function works with jflot (use DOL_MAIN_GRAPH=artichow to

use old libraries)
This commit is contained in:
Laurent Destailleur 2012-01-07 20:56:52 +01:00
parent 52cbfc240b
commit 9e065b2395
8 changed files with 223 additions and 567 deletions

View File

@ -1,6 +1,6 @@
<?php
/* Copyright (C) 2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
* Copyright (C) 2004-2006 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2004-2012 Laurent Destailleur <eldy@users.sourceforge.net>
*
* 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
@ -123,7 +123,7 @@ $data = array();
for ($i = 1 ; $i < 13 ; $i++)
{
$data[$i-1] = array(ucfirst(substr(dol_print_date(dol_mktime(12,0,0,$i,1,$year),"%b"),0,3)), $res[$i]);
$data[$i-1] = array(ucfirst(dol_substr(dol_print_date(dol_mktime(12,0,0,$i,1,$year),"%b"),0,3)), $res[$i]);
}
if (!$user->rights->societe->client->voir || $user->societe_id)
@ -171,7 +171,8 @@ if ($mesg) { print $mesg; }
else { print $px3->show(); }
print '</td></tr></table>';
$db->close();
llxFooter();
$db->close();
?>

View File

@ -139,7 +139,7 @@ $data = array();
for ($i = 1 ; $i < 13 ; $i++)
{
$data[$i-1] = array(ucfirst(substr(dol_print_date(dol_mktime(12,0,0,$i,1,$year),"%b"),0,3)), $res[$i]);
$data[$i-1] = array(ucfirst(dol_substr(dol_print_date(dol_mktime(12,0,0,$i,1,$year),"%b"),0,3)), $res[$i]);
}
if (!$user->rights->societe->client->voir || $user->societe_id)

View File

@ -241,7 +241,7 @@ else
$px1->SetTitle($title);
$px1->SetWidth($width);
$px1->SetHeight($height);
$px1->SetType('lines');
$px1->SetType(array('lines','lines'));
$px1->setBgColor('onglet');
$px1->setBgColorGrid(array(255,255,255));
$px1->SetHorizTickIncrement(1);
@ -371,7 +371,7 @@ else
$px2->SetTitle($title);
$px2->SetWidth($width);
$px2->SetHeight($height);
$px2->SetType('lines');
$px2->SetType(array('lines','lines'));
$px2->setBgColor('onglet');
$px2->setBgColorGrid(array(255,255,255));
$px2->SetHideXGrid(true);
@ -480,7 +480,7 @@ else
$px3->SetTitle($title);
$px3->SetWidth($width);
$px3->SetHeight($height);
$px3->SetType('lines');
$px3->SetType(array('lines','lines'));
$px3->setBgColor('onglet');
$px3->setBgColorGrid(array(255,255,255));
$px3->SetPrecisionY(0);
@ -607,7 +607,7 @@ else
$px4->SetTitle($title);
$px4->SetWidth($width);
$px4->SetHeight($height);
$px4->SetType('bars');
$px4->SetType(array('bars','bars'));
$px4->SetShading(3);
$px4->setBgColor('onglet');
$px4->setBgColorGrid(array(255,255,255));
@ -716,7 +716,7 @@ else
$px5->SetTitle($title);
$px5->SetWidth($width);
$px5->SetHeight($height);
$px5->SetType('bars');
$px5->SetType(array('bars','bars'));
$px5->SetShading(3);
$px5->setBgColor('onglet');
$px5->setBgColorGrid(array(255,255,255));
@ -862,7 +862,7 @@ print '</table>';
print "\n</div>\n";
$db->close();
llxFooter();
$db->close();
?>

View File

@ -115,7 +115,7 @@ $data = array();
for ($i = 1 ; $i < 13 ; $i++)
{
$data[$i-1] = array(ucfirst(substr(dol_print_date(dol_mktime(12,0,0,$i,1,$year),"%b"),0,3)), $res[$i]);
$data[$i-1] = array(ucfirst(dol_substr(dol_print_date(dol_mktime(12,0,0,$i,1,$year),"%b"),0,3)), $res[$i]);
}
$filename_avg = $dir."/tripsexpensesaverage-".$year.".png";

View File

@ -126,7 +126,7 @@ $data = array();
for ($i = 1 ; $i < 13 ; $i++)
{
$data[$i-1] = array(ucfirst(substr(dol_print_date(dol_mktime(12,0,0,$i,1,$year),"%b"),0,3)), $res[$i]);
$data[$i-1] = array(ucfirst(dol_substr(dol_print_date(dol_mktime(12,0,0,$i,1,$year),"%b"),0,3)), $res[$i]);
}
$filename_avg = $dir."/invoicesaverage-".$year.".png";

View File

@ -1,6 +1,6 @@
<?php
/* Copyright (c) 2003-2006 Rodolphe Quiedeville <rodolphe@quiedeville.org>
* Copyright (c) 2004-2011 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (c) 2004-2012 Laurent Destailleur <eldy@users.sourceforge.net>
*
* 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
@ -22,17 +22,18 @@
* \brief File for class to generate graph
*
* Usage:
* $graph_data = array(array('labelA',yA),array('labelB',yB));
* array(array('labelA',yA1,...,yAn),array('labelB',yB1,...yBn));
* $graph_data = array(array('labelxA',yA),array('labelxB',yB));
* $graph_data = array(array('labelxA',yA1,...,yAn),array('labelxB',yB1,...yBn)); // when there is n value to show for each x
* $legend = array("Val1",...,"Valn"); // list of n series name
* $px = new DolGraph();
* $px->SetData($graph_data);
* $px->SetMaxValue($px->GetCeilMaxValue());
* $px->SetMinValue($px->GetFloorMinValue());
* $px->SetTitle("title");
* $px->SetLegend(array("Val1","Val2"));
* $px->SetLegend($legend);
* $px->SetWidth(width);
* $px->SetHeight(height);
* $px->draw("file.png");
* $px->draw("file.png","/viewdownload?...");
*/
@ -42,11 +43,12 @@
*/
class DolGraph
{
//! Type du graphique
var $type='bars'; // bars, lines, ...
var $mode='side'; // Mode bars graph: side, depth
//! Type of graph
var $type=array('bars'); // bars, lines, ...
var $mode='side'; // Mode bars graph: side, depth
private $_library='jflot'; // Graphic library to use (jflot, artichow)
//! Tableau de donnees
//! Array of data
var $data; // array(array('abs1',valA1,valB1), array('abs2',valA2,valB2), ...)
var $title;
var $width=380;
@ -70,11 +72,9 @@ class DolGraph
var $graph; // Objet Graph (Artichow, Phplot...)
var $error;
var $library='artichow'; // Graphic library to use
var $bordercolor; // array(R,G,B)
var $bgcolor; // array(R,G,B)
var $bgcolorgrid; // array(R,G,B)
var $bgcolorgrid=array(255,255,255); // array(R,G,B)
var $datacolor; // array(array(R,G,B),...)
private $_stringtoshow; // To store string to output graph into HTML page
@ -88,19 +88,23 @@ class DolGraph
global $conf;
global $theme_bordercolor, $theme_datacolor, $theme_bgcolor, $theme_bgcoloronglet;
// Test si module GD present
$modules_list = get_loaded_extensions();
$isgdinstalled=0;
foreach ($modules_list as $module)
// To use old feature
if ($conf->global->MAIN_GRAPH_LIBRARY == 'artichow')
{
if ($module == 'gd') {
$isgdinstalled=1;
$this->_library='artichow';
// Test if module GD present
$modules_list = get_loaded_extensions();
$isgdinstalled=0;
foreach ($modules_list as $module)
{
if ($module == 'gd') $isgdinstalled=1;
}
if (! $isgdinstalled)
{
$this->error="Error: PHP GD module is not available. It is required to build graphics.";
return -1;
}
}
if (! $isgdinstalled)
{
$this->error="Error: PHP GD module is not available. It is required to build graphics.";
return -1;
}
$this->bordercolor = array(235,235,224);
@ -234,12 +238,13 @@ class DolGraph
function SetData($data)
{
$this->data = $data;
//var_dump($this->data);
}
/**
* Set type
*
* @param string $type Type
* @param array $type Array with type for each serie
* @return void
*/
function SetType($type)
@ -538,8 +543,8 @@ class DolGraph
dol_syslog(get_class($this)."::draw ".$this->error, LOG_ERR);
return -1;
}
$call = "draw_".$this->library;
$this->$call($file,$fileurl);
$call = "draw_".$this->_library;
call_user_func_array(array($this,$call), array($file,$fileurl));
}
@ -554,7 +559,7 @@ class DolGraph
{
global $artichow_defaultfont;
dol_syslog(get_class($this)."::draw_artichow this->type=".$this->type);
dol_syslog(get_class($this)."::draw_artichow this->type=".join(',',$this->type));
if (! defined('SHADOW_RIGHT_TOP')) define('SHADOW_RIGHT_TOP',3);
if (! defined('LEGEND_BACKGROUND')) define('LEGEND_BACKGROUND',2);
@ -562,8 +567,8 @@ class DolGraph
// Create graph
$classname='';
if ($this->type == 'bars') $classname='BarPlot';
if ($this->type == 'lines') $classname='LinePlot';
if ($this->type[0] == 'bars') $classname='BarPlot'; // Only first type of type is supported by artichow
if ($this->type[0] == 'lines') $classname='LinePlot';
include_once(ARTICHOW_PATH.$classname.".class.php");
// Definition de couleurs
@ -614,17 +619,16 @@ class DolGraph
while ($i < $nblot)
{
$j=0;
$x=0;
$values=array();
foreach($this->data as $key => $valarray)
{
$legends[$j] = $valarray[0];
$values[$j] = $valarray[$i+1];
$j++;
$legends[$x] = $valarray[0];
$values[$x] = $valarray[$i+1];
$x++;
}
// Artichow ne gere pas les valeurs inconnues
// Donc si inconnu, on la fixe a null
// We fix unknown values to null
$newvalues=array();
foreach($values as $val)
{
@ -632,7 +636,7 @@ class DolGraph
}
if ($this->type == 'bars')
if ($this->type[0] == 'bars')
{
//print "Lot de donnees $i<br>";
//print_r($values);
@ -668,7 +672,7 @@ class DolGraph
$plot->SetYMin($this->MinValue);
}
if ($this->type == 'lines')
if ($this->type[0] == 'lines')
{
$color=new Color($this->datacolor[$i][0],$this->datacolor[$i][1],$this->datacolor[$i][2],20);
$colorbis=new Color(min($this->datacolor[$i][0]+20,255),min($this->datacolor[$i][1]+20,255),min($this->datacolor[$i][2]+20,255),60);
@ -699,8 +703,8 @@ class DolGraph
// solve a bug in Artichow with UTF8
if (count($this->Legend))
{
if ($this->type == 'bars') $group->legend->add($plot, $this->Legend[$i], LEGEND_BACKGROUND);
if ($this->type == 'lines') $group->legend->add($plot, $this->Legend[$i], LEGEND_LINE);
if ($this->type[0] == 'bars') $group->legend->add($plot, $this->Legend[$i], LEGEND_BACKGROUND);
if ($this->type[0] == 'lines') $group->legend->add($plot, $this->Legend[$i], LEGEND_LINE);
}
$group->add($plot);
@ -718,10 +722,169 @@ class DolGraph
// Generate file
$graph->draw($file);
$this->_stringtoshow='<img src="'.$fileurl.'" title="'.dol_escape_htmltag($this->title?$this->title:$this->YLabel).'" alt="'.dol_escape_htmltag($this->title?$this->title:$this->YLabel).'">';
$this->_stringtoshow='<!-- Build using '.$this->_library.' --><img src="'.$fileurl.'" title="'.dol_escape_htmltag($this->title?$this->title:$this->YLabel).'" alt="'.dol_escape_htmltag($this->title?$this->title:$this->YLabel).'">';
}
/**
* Build a graph onto disk using JFlot library
* $graph_data = array(array('labelxA',yA),array('labelxB',yB));
* $graph_data = array(array('labelxA',yA1,...,yAn),array('labelxB',yB1,...yBn)); // when there is n value to show for each x
* $legend = array("Val1",...,"Valn"); // list of n series name
*
* @param string $file Image file name to use if we save onto disk
* @param string $fileurl Url path to show image if saved onto disk
* @return void
*/
private function draw_jflot($file,$fileurl)
{
global $artichow_defaultfont;
dol_syslog(get_class($this)."::draw_jflot this->type=".join(',',$this->type));
// On boucle sur chaque lot de donnees
$legends=array();
$nblot=count($this->data[0])-1; // -1 to remove legend
$firstlot=0;
if ($nblot > 2) $firstlot = ($nblot - 2); // We limit nblot to 2 because jflot can't manage more than 2 bars on same x
$i=$firstlot;
$serie=array();
while ($i < $nblot)
{
$x=0;
$values=array();
foreach($this->data as $key => $valarray)
{
$legends[$x] = $valarray[0];
$values[$x] = $valarray[$i+1];
$x++;
}
// We fix unknown values to null
$newvalues=array();
foreach($values as $val)
{
$newvalues[]=(is_numeric($val) ? $val : null);
}
//print "Lot de donnees $i<br>";
//print_r($values);
//print '<br>';
$serie[$i]="var d".$i." = [];\n";
$x=0;
foreach($newvalues as $key => $val)
{
if (isset($val)) $serie[$i].="d".$i.".push([".$x.", ".$val."]);\n";
$x++;
}
$i++;
}
$tag=dol_escape_htmltag(dol_string_unaccent(dol_string_nospecial(basename($file),'_',array('-','.'))));
$this->_stringtoshow ='<!-- Build using '.$this->_library.' -->'."\n";
$this->_stringtoshow.='<br><div align="center">'.$this->title.'</div><br>';
$this->_stringtoshow.='<div id="placeholder_'.$tag.'" style="width:'.$this->width.'px;height:'.$this->height.'px;"></div>'."\n";
$this->_stringtoshow.='<script id="'.$tag.'">'."\n";
$this->_stringtoshow.='$(function () {'."\n";
$i=$firstlot;
while ($i < $nblot)
{
$this->_stringtoshow.=$serie[$i];
$i++;
}
$this->_stringtoshow.="\n";
$this->_stringtoshow.='
function showTooltip_'.$tag.'(x, y, contents) {
$(\'<div id="tooltip_'.$tag.'">\' + contents + \'</div>\').css( {
position: \'absolute\',
display: \'none\',
top: y + 5,
left: x + 5,
border: \'1px solid #ddd\',
padding: \'2px\',
\'background-color\': \'#ffe\',
width: 200,
opacity: 0.80
}).appendTo("body").fadeIn(20);
}
var previousPoint = null;
$("#placeholder_'.$tag.'").bind("plothover", function (event, pos, item) {
$("#x").text(pos.x.toFixed(2));
$("#y").text(pos.y.toFixed(2));
if (item) {
if (previousPoint != item.dataIndex) {
previousPoint = item.dataIndex;
$("#tooltip").remove();
/* console.log(item); */
var x = item.datapoint[0].toFixed(2);
var y = item.datapoint[1].toFixed(2);
var z = item.series.xaxis.ticks[item.dataIndex].label;
showTooltip_'.$tag.'(item.pageX, item.pageY,
item.series.label + "<br>" + z + " => " + y);
}
}
else {
$("#tooltip_'.$tag.'").remove();
previousPoint = null;
}
});
';
$this->_stringtoshow.='var stack = null, steps = false;'."\n";
$this->_stringtoshow.='function plotWithOptions_'.$tag.'() {'."\n";
$this->_stringtoshow.='$.plot($("#placeholder_'.$tag.'"), [ '."\n";
$i=$firstlot;
while ($i < $nblot)
{
if ($i > $firstlot) $this->_stringtoshow.=', '."\n";
$color=sprintf("%02x%02x%02x",$this->datacolor[$i][0],$this->datacolor[$i][1],$this->datacolor[$i][2]);
$this->_stringtoshow.='{ ';
if (! isset($this->type[$i]) || $this->type[$i] == 'bars') $this->_stringtoshow.='bars: { show: true, align: "'.($i==$firstlot?'center':'left').'", barWidth: 0.5 }, ';
if (isset($this->type[$i]) && $this->type[$i] == 'lines') $this->_stringtoshow.='lines: { show: true, fill: false }, ';
$this->_stringtoshow.='color: "#'.$color.'", label: "'.dol_escape_js($this->Legend[$i]).'", data: d'.$i.' }';
$i++;
}
$this->_stringtoshow.="\n".' ], { series: { stack: stack, lines: { fill: false, steps: steps }, bars: { barWidth: 0.6 } }'."\n";
// Xaxis
$this->_stringtoshow.=', xaxis: { ticks: ['."\n";
$x=0;
foreach($this->data as $key => $valarray)
{
if ($x > 0) $this->_stringtoshow.=', '."\n";
$this->_stringtoshow.= ' ['.$x.', "'.$valarray[0].'"]';
$x++;
}
$this->_stringtoshow.='] }'."\n";
// Yaxis
$this->_stringtoshow.=', yaxis: { min: '.$this->MinValue.', max: '.($this->MaxValue).' }'."\n";
// Background color
$color1=sprintf("%02x%02x%02x",$this->bgcolorgrid[0],$this->bgcolorgrid[0],$this->bgcolorgrid[2]);
$color2=sprintf("%02x%02x%02x",$this->bgcolorgrid[0],$this->bgcolorgrid[1],$this->bgcolorgrid[2]);
$this->_stringtoshow.=', grid: { hoverable: true, backgroundColor: { colors: ["#'.$color1.'", "#'.$color2.'"] } }'."\n";
//$this->_stringtoshow.=', shadowSize: 20'."\n"; TODO Uncommet this
$this->_stringtoshow.='});'."\n";
$this->_stringtoshow.='}'."\n";
$this->_stringtoshow.='plotWithOptions_'.$tag.'();'."\n";
$this->_stringtoshow.='});'."\n";
$this->_stringtoshow.='</script>'."\n";
}
/**
* Output HTML string to show graph
*

View File

@ -1,6 +1,6 @@
<?php
/* Copyright (C) 2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
* Copyright (c) 2008-2011 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (c) 2008-2012 Laurent Destailleur <eldy@users.sourceforge.net>
*
* 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
@ -115,7 +115,7 @@ abstract class Stats
{
$result = array();
dol_syslog("Stats::_getNbByYear sql=".$sql);
dol_syslog(get_class($this)."::_getNbByYear sql=".$sql);
$resql=$this->db->query($sql);
if ($resql)
{
@ -145,7 +145,7 @@ abstract class Stats
{
$result = array();
dol_syslog("Stats::_getAllByYear sql=".$sql);
dol_syslog(get_class($this)."::_getAllByYear sql=".$sql);
$resql=$this->db->query($sql);
if ($resql)
{
@ -179,7 +179,7 @@ abstract class Stats
{
$result = array();
dol_syslog("Stats::_getNbByMonth sql=".$sql);
dol_syslog(get_class($this)."::_getNbByMonth sql=".$sql);
$resql=$this->db->query($sql);
if ($resql)
{
@ -227,7 +227,7 @@ abstract class Stats
{
$result = array();
dol_syslog("Stats::_getAmountByMonth sql=".$sql);
dol_syslog(get_class($this)."::_getAmountByMonth sql=".$sql);
$resql=$this->db->query($sql);
if ($resql)
@ -272,7 +272,7 @@ abstract class Stats
{
$result = array();
dol_syslog("Stats::_getAverageByMonth sql=".$sql);
dol_syslog(get_class($this)."::_getAverageByMonth sql=".$sql);
$resql=$this->db->query($sql);
if ($resql)
{

View File

@ -1,508 +0,0 @@
#!/usr/bin/php
<?php
/*
* Copyright (C) 2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
* Copyright (C) 2006-2009 Laurent Destailleur <eldy@users.sourceforge.net>
*
* 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, see <http://www.gnu.org/licenses/>.
*/
/**
* \file scripts/banks/build-graph-sold-example.php
* \ingroup banque
* \brief Script to build graph of sold for each bank account
* \deprecated Ce script n'est plus utilise car les graphiques sont generes dynamiquement maintenant.
*/
$sapi_type = php_sapi_name();
$script_file = basename(__FILE__);
$path=dirname(__FILE__).'/';
// Test si mode batch
$sapi_type = php_sapi_name();
if (substr($sapi_type, 0, 3) == 'cgi') {
echo "Erreur: Vous utilisez l'interpreteur PHP pour le mode CGI. Pour executer graph-solde.php en ligne de commande, vous devez utiliser l'interpreteur PHP pour le mode CLI.\n";
exit;
}
require_once($path."../../htdocs/master.inc.php");
require_once(DOL_DOCUMENT_ROOT."/core/class/dolgraph.class.php");
if (! isset($argv[1]) || ! $argv[1]) {
print "Usage: ".$script_file." now\n";
exit;
}
$id=$argv[1];
$error = 0;
// Initialise opt, tableau des parametres
if (function_exists("getopt"))
{
// getopt existe sur ce PHP
$opt = getopt("m:y:");
}
else
{
// getopt n'existe sur ce PHP
$opt=array('m'=>$argv[1]);
}
// Create output directory
create_exdir($conf->banque->dir_temp);
$datetime = time();
if ($opt['m'] > 0)
{
$month = $opt['m'];
}
else
{
$month = strftime("%m", $datetime);
}
$year = strftime("%Y", $datetime);
if ($month == 1)
{
$monthprev = "12";
$yearprev = $year - 1;
}
else
{
$monthprev = substr("00".($month - 1), -2);
$yearprev = $year ;
}
if ($month == 12)
{
$monthnext = "01";
$yearnext = $year + 1;
}
else
{
$monthnext = substr("00".($month + 1), -2);
}
$sql = "SELECT distinct(fk_account)";
$sql .= " FROM ".MAIN_DB_PREFIX."bank";
$sql .= " WHERE fk_account IS NOT NULL";
$resql = $db->query($sql);
$accounts = array();
if ($resql)
{
$num = $db->num_rows($resql);
$i = 0;
while ($i < $num)
{
$row = $db->fetch_row($resql);
array_push($accounts, $row[0]);
$i++;
}
}
$width = 750;
$height = 350;
foreach ($accounts as $account)
{
$datetime = time();
$year = strftime("%Y", $datetime);
$month = strftime("%m", $datetime);
$day = strftime("%d", $datetime);
// Definition de $width et $height
$width = 750;
$height = 280;
// Calcul de $min et $max
$sql = "SELECT min(datev), max(datev)";
$sql.= " FROM ".MAIN_DB_PREFIX."bank";
$sql.= " WHERE fk_account = ".$account;
$resql = $db->query($sql);
if ($resql)
{
$num = $db->num_rows($resql);
$row = $db->fetch_row($resql);
$min = $db->jdate($row[0]);
$max = $db->jdate($row[1]);
}
else
{
dol_print_error($db);
}
// print strftime("%Y%m%d",$max);
// Chargement du tableau $amounts
// TODO peut etre optimise en virant les date_format
$amounts = array();
$sql = "SELECT date_format(datev,'%Y%m%d'), sum(amount)";
$sql .= " FROM ".MAIN_DB_PREFIX."bank";
$sql .= " WHERE fk_account = ".$account;
$sql .= " AND date_format(datev,'%Y%m') = '".$year.$month."'";
$sql .= " GROUP BY date_format(datev,'%Y%m%d')";
$resql = $db->query($sql);
if ($resql)
{
$num = $db->num_rows($resql);
$i = 0;
while ($i < $num)
{
$row = $db->fetch_row($resql);
$amounts[$row[0]] = $row[1];
$i++;
}
}
else
{
dol_print_error($db);
}
// Calcul de $solde avant le debut du graphe
$solde = 0;
$sql = "SELECT sum(amount)";
$sql .= " FROM ".MAIN_DB_PREFIX."bank";
$sql .= " WHERE fk_account = ".$account;
$sql .= " AND datev < '".$year."-".sprintf("%02s",$month)."-01'";
$resql = $db->query($sql);
if ($resql)
{
$row = $db->fetch_row($resql);
$solde = $row[0];
}
else
{
dol_print_error($db);
}
// Chargement de labels et datas pour tableau 1
$labels = array();
$datas = array();
$subtotal = 0;
$day = mktime(1,1,1,$month,1,$year);
$xmonth = substr("00".strftime("%m",$day), -2);
$i = 0;
while ($xmonth == $month)
{
//print strftime ("%e %d %m %y",$day)."\n";
$subtotal = $subtotal + (isset($amounts[strftime("%Y%m%d",$day)]) ? $amounts[strftime("%Y%m%d",$day)] : 0);
if ($day > time())
{
$datas[$i] = ''; // Valeur sp<73>ciale permettant de ne pas tracer le graph
}
else
{
$datas[$i] = $solde + $subtotal;
}
//$labels[$i] = strftime("%d",$day);
$labels[$i] = strftime("%d",$day);
$day += 86400;
$xmonth = substr("00".strftime("%m",$day), -2);
$i++;
}
// Fabrication tableau 1
$file= $conf->banque->dir_temp."/solde.$account.$year.$month.png";
$title=$langs->trans("Balance").' '.$langs->trans("Month").': '.$month.' '.$langs->trans("Year").': '.$year;
$graph_datas=array();
foreach($datas as $i => $val)
{
$graph_datas[$i]=array("$labels[$i]",$datas[$i]);
}
$px1 = new DolGraph();
$px1->SetData($graph_datas);
$px1->SetLegend(array($langs->trans("Balance")));
$px1->SetMaxValue($px1->GetCeilMaxValue());
$px1->SetMinValue($px1->GetFloorMinValue());
$px1->SetTitle($title);
$px1->SetWidth($width);
$px1->SetHeight($height);
$px1->SetType('lines');
$px1->draw($file,'');
// Chargement du tableau $amounts
// TODO peut etre optimise en virant les date_format
$amounts = array();
$sql = "SELECT date_format(datev,'%Y%m%d'), sum(amount)";
$sql .= " FROM ".MAIN_DB_PREFIX."bank";
$sql .= " WHERE fk_account = ".$account;
$sql .= " AND date_format(datev,'%Y') = '".$year."'";
$sql .= " GROUP BY date_format(datev,'%Y%m%d')";
$resql = $db->query($sql);
if ($resql)
{
$num = $db->num_rows($resql);
$i = 0;
while ($i < $num)
{
$row = $db->fetch_row($resql);
$amounts[$row[0]] = $row[1];
$i++;
}
}
else
{
dol_print_error($db);
}
// Calcul de $solde avant le debut du graphe
$solde = 0;
$sql = "SELECT sum(amount)";
$sql .= " FROM ".MAIN_DB_PREFIX."bank";
$sql .= " WHERE fk_account = ".$account;
$sql .= " AND datev < '".$year."-01-01'";
$resql = $db->query($sql);
if ($resql)
{
$row = $db->fetch_row($resql);
$solde = $row[0];
}
else
{
dol_print_error($db);
}
// Chargement de labels et datas pour tableau 2
$labels = array();
$datas = array();
$subtotal = 0;
$day = mktime(1,1,1,1,1,$year);
$xyear = strftime("%Y",$day);
$i = 0;
while ($xyear == $year)
{
$subtotal = $subtotal + (isset($amounts[strftime("%Y%m%d",$day)]) ? $amounts[strftime("%Y%m%d",$day)] : 0);
//print strftime ("%e %d %m %y",$day)." ".$subtotal."\n<br>";
if ($day > time())
{
$datas[$i] = ''; // Valeur sp<73>ciale permettant de ne pas tracer le graph
}
else
{
$datas[$i] = $solde + $subtotal;
}
if (strftime("%d",$day) == 15)
{
$labels[$i] = strftime("%m",$day);
}
$day += 86400;
$xyear = strftime("%Y",$day);
$i++;
}
// Fabrication tableau 2
$file= $conf->banque->dir_temp."/solde.$account.$year.png";
$title=$langs->trans("Balance").' '.$langs->trans("Year").': '.$year;
$graph_datas=array();
foreach($datas as $i => $val)
{
$graph_datas[$i]=array(isset($labels[$i])?$labels[$i]:'',$datas[$i]);
}
$px2 = new DolGraph();
$px2->SetData($graph_datas);
$px2->SetLegend(array($langs->trans("Balance")));
$px2->SetMaxValue($px2->GetCeilMaxValue());
$px2->SetTitle($title);
$px2->SetWidth($width);
$px2->SetHeight($height);
$px2->SetType('lines');
$px2->draw($file,'');
// Chargement du tableau $amounts
// TODO peut etre optimise en virant les date_format
$amounts = array();
$sql = "SELECT date_format(datev,'%Y%m%d'), sum(amount)";
$sql .= " FROM ".MAIN_DB_PREFIX."bank";
$sql .= " WHERE fk_account = ".$account;
$sql .= " GROUP BY date_format(datev,'%Y%m%d')";
$resql = $db->query($sql);
if ($resql)
{
$num = $db->num_rows($resql);
$i = 0;
while ($i < $num)
{
$row = $db->fetch_row($resql);
$amounts[$row[0]] = $row[1];
$i++;
}
}
else
{
dol_print_error($db);
}
// Calcul de $solde avant le debut du graphe
$solde = 0;
// Chargement de labels et datas pour tableau 3
$labels = array();
$datas = array();
$subtotal = 0;
$day = $min;
$i = 0;
while ($day <= ($max+1000000)) // On va bien au dela du dernier jour
{
$subtotal = $subtotal + (isset($amounts[strftime("%Y%m%d",$day)]) ? $amounts[strftime("%Y%m%d",$day)] : 0);
//print strftime ("%e %d %m %y",$day)." ".$subtotal."\n<br>";
if ($day > ($max+86400))
{
$datas[$i] = ''; // Valeur sp<73>ciale permettant de ne pas tracer le graph
}
else
{
$datas[$i] = $solde + $subtotal;
}
if (strftime("%d",$day) == 1)
{
$labels[$i] = strftime("%m",$day);
}
$day += 86400;
$i++;
}
// Fabrication tableau 3
$file= $conf->banque->dir_temp."/solde.$account.png";
$title=$langs->trans("Balance");
$graph_datas=array();
foreach($datas as $i => $val)
{
$graph_datas[$i]=array(isset($labels[$i])?$labels[$i]:'',$datas[$i]);
}
$px3 = new DolGraph();
$px3->SetData($graph_datas);
$px3->SetLegend(array($langs->trans("Balance")));
$px3->SetMaxValue($px3->GetCeilMaxValue());
$px3->SetMinValue($px3->GetFloorMinValue());
$px3->SetTitle($title);
$px3->SetWidth($width);
$px3->SetHeight($height);
$px3->SetType('lines');
$px3->draw($file,'');
// Chargement du tableau $credits, $debits
$credits = array();
$debits = array();
$sql = "SELECT date_format(datev,'%m'), sum(amount)";
$sql .= " FROM ".MAIN_DB_PREFIX."bank";
$sql .= " WHERE fk_account = ".$account;
$sql .= " AND date_format(datev,'%Y') = '".$year."'";
$sql .= " AND amount > 0";
$sql .= " GROUP BY date_format(datev,'%m');";
$resql = $db->query($sql);
if ($resql)
{
$num = $db->num_rows($resql);
$i = 0;
while ($i < $num)
{
$row = $db->fetch_row($resql);
$credits[$row[0]] = $row[1];
$i++;
}
}
else
{
dol_print_error($db);
}
$sql = "SELECT date_format(datev,'%m'), sum(amount)";
$sql .= " FROM ".MAIN_DB_PREFIX."bank";
$sql .= " WHERE fk_account = ".$account;
$sql .= " AND date_format(datev,'%Y') = '".$year."'";
$sql .= " AND amount < 0";
$sql .= " GROUP BY date_format(datev,'%m');";
$resql = $db->query($sql);
if ($resql)
{
while ($row = $db->fetch_row($resql))
{
$debits[$row[0]] = abs($row[1]);
}
}
else
{
dol_print_error($db);
}
// Chargement de labels et data_xxx pour tableau 4
$labels = array();
$data_credit = array();
$data_debit = array();
for ($i = 0 ; $i < 12 ; $i++)
{
$data_credit[$i] = isset($credits[substr("0".($i+1),-2)]) ? $credits[substr("0".($i+1),-2)] : 0;
$data_debit[$i] = isset($debits[substr("0".($i+1),-2)]) ? $debits[substr("0".($i+1),-2)] : 0;
$labels[$i] = $i+1;
}
// Fabrication tableau 4
$file= $conf->banque->dir_temp."/mouvement.$account.$year.png";
$title=$langs->trans("Movements").' '.$langs->trans("Year").': '.$year;
$graph_datas=array();
foreach($data_credit as $i => $val)
{
$graph_datas[$i]=array($labels[$i],$data_credit[$i],$data_debit[$i]);
}
$px4 = new DolGraph();
$px4->SetData($graph_datas);
$px4->SetLegend(array($langs->trans("Debit"),$langs->trans("Credit")));
$px4->SetMaxValue($px4->GetCeilMaxValue());
$px4->SetMinValue($px4->GetFloorMinValue());
$px4->SetTitle($title);
$px4->SetWidth($width);
$px4->SetHeight($height);
$px4->SetType('bars');
$px4->SetShading(3);
$px4->draw($file,'');
}
$db->close();
print 'Graph files generated into directory '.$conf->banque->dir_temp."\n";
?>