2005-09-17 04:32:22 +02:00
#!/usr/bin/php
2005-07-31 19:24:43 +02:00
< ? PHP
/* Copyright ( C ) 2005 Rodolphe Quiedeville < rodolphe @ quiedeville . org >
*
* 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 .
*
* $Id $
* $Source $
*/
2005-09-17 04:32:22 +02:00
/**
\file scripts / banque / graph - solde . php
\ingroup banque
\brief Script de g<EFBFBD> n<EFBFBD> ration des images des soldes des comptes
*/
// 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 mailing-send.php en ligne de commande, vous devez utiliser l'interpreteur PHP pour le mode CLI. \n " ;
exit ;
}
// Recupere root dolibarr
$path = eregi_replace ( 'graph-solde.php' , '' , $_SERVER [ " PHP_SELF " ]);
2005-10-18 16:03:05 +02:00
require_once ( $path . " ../../htdocs/master.inc.php " );
2005-09-17 04:32:22 +02:00
// V<> rifie que chemin vers JPGRAHP est connu et defini $jpgraph
if ( ! defined ( 'JPGRAPH_DIR' ) && ! defined ( 'JPGRAPH_PATH' ))
{
print 'Erreur: D<> finissez la constante JPGRAPH_PATH sur la valeur du r<> pertoire contenant JPGraph' ;
exit ;
}
if ( ! defined ( 'JPGRAPH_DIR' )) define ( 'JPGRAPH_DIR' , JPGRAPH_PATH );
$jpgraphdir = JPGRAPH_DIR ;
if ( ! eregi ( '[\\\/]$' , $jpgraphdir )) $jpgraphdir .= '/' ;
2005-07-31 19:24:43 +02:00
2005-09-17 04:32:22 +02:00
2005-10-18 16:03:05 +02:00
include_once ( $jpgraphdir . " jpgraph.php " );
include_once ( $jpgraphdir . " jpgraph_line.php " );
include_once ( $jpgraphdir . " jpgraph_bar.php " );
include_once ( $jpgraphdir . " jpgraph_pie.php " );
include_once ( $jpgraphdir . " jpgraph_error.php " );
include_once ( $jpgraphdir . " jpgraph_canvas.php " );
2005-07-31 19:24:43 +02:00
$error = 0 ;
2005-09-17 04:32:22 +02:00
// 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 ]);
}
2005-11-13 18:09:00 +01:00
if ( ! $conf -> banque -> dir_images )
{
$conf -> banque -> dir_images = DOL_DATA_ROOT . " /graph/banque/ " ;
}
2005-09-17 04:32:22 +02:00
// Cr<43> e r<> pertoire accueil
create_exdir ( $conf -> banque -> dir_images );
2005-07-31 19:24:43 +02:00
$datetime = time ();
2005-08-01 10:48:40 +02:00
if ( $opt [ 'm' ] > 0 )
{
$month = $opt [ 'm' ];
}
else
{
$month = strftime ( " %m " , $datetime );
}
2005-07-31 19:24:43 +02:00
$year = strftime ( " %Y " , $datetime );
if ( $month == 1 )
{
$monthprev = " 12 " ;
$yearprev = $year - 1 ;
}
else
{
$monthprev = substr ( " 00 " . ( $month - 1 ), - 2 ) ;
2005-08-01 10:48:40 +02:00
$yearprev = $year ;
2005-07-31 19:24:43 +02:00
}
if ( $month == 12 )
{
$monthnext = " 01 " ;
$yearnext = $year + 1 ;
}
else
{
$monthnext = substr ( " 00 " . ( $month + 1 ), - 2 ) ;
}
2005-07-31 20:33:29 +02:00
$sql = " SELECT distinct(fk_account) " ;
2005-07-31 19:24:43 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . " bank " ;
2005-08-01 10:52:03 +02:00
$sql .= " WHERE fk_account IS NOT NULL " ;
2005-07-31 19:24:43 +02:00
$resql = $db -> query ( $sql );
2005-07-31 20:33:29 +02:00
2005-07-31 20:36:05 +02:00
$accounts = array ();
2005-07-31 20:33:29 +02:00
2005-07-31 19:24:43 +02:00
if ( $resql )
{
2005-07-31 20:33:29 +02:00
$num = $db -> num_rows ( $resql );
$i = 0 ;
while ( $i < $num )
{
$row = $db -> fetch_row ( $resql );
array_push ( $accounts , $row [ 0 ]);
$i ++ ;
}
2005-07-31 19:24:43 +02:00
}
2005-07-31 20:33:29 +02:00
$account = 1 ;
foreach ( $accounts as $account )
2005-07-31 19:24:43 +02:00
{
2005-07-31 20:36:05 +02:00
$labels = array ();
$datas = array ();
$amounts = array ();
2005-07-31 20:33:29 +02:00
$sql = " SELECT sum(amount) " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " bank " ;
$sql .= " WHERE fk_account = " . $account ;
$sql .= " AND datev < ' " . $year . " - " . $month . " -01'; " ;
$resql = $db -> query ( $sql );
if ( $resql )
2005-08-01 10:48:40 +02:00
{
$row = $db -> fetch_row ( $resql );
$solde = $row [ 0 ];
}
2005-07-31 20:33:29 +02:00
else
{
print $sql ;
}
2005-07-31 19:24:43 +02:00
2005-07-31 20:36:05 +02:00
$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'); " ;
2005-07-31 19:24:43 +02:00
2005-07-31 20:36:05 +02:00
$resql = $db -> query ( $sql );
$amounts = array ();
if ( $resql )
{
$num = $db -> num_rows ( $resql );
$i = 0 ;
while ( $i < $num )
{
$row = $db -> fetch_row ( $resql );
$amounts [ $row [ 0 ]] = $row [ 1 ];
$i ++ ;
2005-08-01 10:48:40 +02:00
}
2005-07-31 20:36:05 +02:00
}
else
{
print $sql ;
}
$subtotal = 0 ;
$day = mktime ( 1 , 1 , 1 , $month , 1 , $year );
$xmonth = substr ( " 00 " . strftime ( " %m " , $day ), - 2 );
2005-07-31 19:24:43 +02:00
$i = 0 ;
2005-07-31 20:36:05 +02:00
while ( $xmonth == $month )
2005-07-31 19:24:43 +02:00
{
2005-07-31 20:36:05 +02:00
//print strftime ("%e %d %m %y",$day)."\n";
$subtotal = $subtotal + $amounts [ strftime ( " %Y%m%d " , $day )];
2005-08-01 10:48:40 +02:00
if ( $day > time ())
{
$datas [ $i ] = 0 ;
}
else
{
$datas [ $i ] = $solde + $subtotal ;
}
2005-07-31 20:36:05 +02:00
$labels [ $i ] = strftime ( " %d " , $day );
$day += 86400 ;
$xmonth = substr ( " 00 " . strftime ( " %m " , $day ), - 2 );
2005-07-31 19:24:43 +02:00
$i ++ ;
}
2005-08-01 10:48:40 +02:00
2005-07-31 20:36:05 +02:00
$width = 750 ;
$height = 350 ;
$graph = new Graph ( $width , $height , " auto " );
$graph -> SetScale ( " textlin " );
$graph -> yaxis -> scale -> SetGrace ( 2 );
$graph -> SetFrame ( 1 );
2005-08-10 14:20:51 +02:00
$graph -> img -> SetMargin ( 60 , 20 , 20 , 35 );
2005-07-31 20:36:05 +02:00
$b2plot = new BarPlot ( $datas );
$b2plot -> SetColor ( " blue " );
//$b2plot->SetWeight(2);
2005-08-01 10:48:40 +02:00
$graph -> title -> Set ( " Solde $month $year " );
2005-07-31 20:36:05 +02:00
$graph -> xaxis -> SetTickLabels ( $labels );
//$graph->xaxis->title->Set(strftime("%d/%m/%y %H:%M:%S", time()));
$graph -> Add ( $b2plot );
$graph -> img -> SetImgFormat ( " png " );
2005-09-17 04:32:22 +02:00
$file = $conf -> banque -> dir_images . " /solde. $account . $year . $month .png " ;
2005-07-31 20:36:05 +02:00
$graph -> Stroke ( $file );
2005-07-31 20:33:29 +02:00
}
2005-08-10 14:20:51 +02:00
/*
* Graph annuels
*
*/
2005-08-10 12:00:24 +02:00
foreach ( $accounts as $account )
{
$labels = array ();
$datas = array ();
$amounts = array ();
$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
{
print $sql ;
}
$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 );
$amounts = array ();
if ( $resql )
{
$num = $db -> num_rows ( $resql );
$i = 0 ;
while ( $i < $num )
{
$row = $db -> fetch_row ( $resql );
$amounts [ $row [ 0 ]] = $row [ 1 ];
$i ++ ;
}
}
else
{
dolibarr_syslog ( " graph-solde.php Error " );
}
$subtotal = 0 ;
$day = mktime ( 1 , 1 , 1 , 1 , 1 , $year );
$xyear = strftime ( " %Y " , $day );
$i = 0 ;
while ( $xyear == $year )
{
//print strftime ("%e %d %m %y",$day)."\n";
$subtotal = $subtotal + $amounts [ strftime ( " %Y%m%d " , $day )];
if ( $day > time ())
{
2005-08-10 14:20:51 +02:00
$datas [ $i ] = 'x' ; // Valeur sp<73> ciale permettant de ne pas tracer le graph
2005-08-10 12:00:24 +02:00
}
else
{
$datas [ $i ] = $solde + $subtotal ;
}
if ( strftime ( " %d " , $day ) == 1 )
{
$labels [ $i ] = strftime ( " %d " , $day );
}
else
{
}
$day += 86400 ;
$xyear = strftime ( " %Y " , $day );
$i ++ ;
}
$width = 750 ;
$height = 350 ;
$graph = new Graph ( $width , $height , " auto " );
$graph -> SetScale ( " textlin " );
$graph -> yaxis -> scale -> SetGrace ( 2 );
$graph -> SetFrame ( 1 );
2005-08-10 14:20:51 +02:00
$graph -> img -> SetMargin ( 60 , 20 , 20 , 35 );
2005-08-10 12:00:24 +02:00
$b2plot = new LinePlot ( $datas );
$b2plot -> SetColor ( " blue " );
//$b2plot->SetWeight(2);
$graph -> title -> Set ( " Solde $year " );
$graph -> xaxis -> SetTickLabels ( $labels );
$graph -> xaxis -> Hide ();
//$graph->xaxis->HideTicks();
//$graph->xaxis->title->Set(strftime("%d/%m/%y %H:%M:%S", time()));
$graph -> Add ( $b2plot );
$graph -> img -> SetImgFormat ( " png " );
2005-09-17 04:32:22 +02:00
$file = $conf -> banque -> dir_images . " /solde. $account . $year .png " ;
2005-08-10 12:00:24 +02:00
$graph -> Stroke ( $file );
}
2005-08-10 14:20:51 +02:00
/*
* Graph annuels
*
*/
foreach ( $accounts as $account )
{
$labels = array ();
$datas = array ();
$amounts = array ();
$sql = " SELECT min( " . $db -> pdate ( " datev " ) . " ),max( " . $db -> pdate ( " 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 = $row [ 0 ];
$max = $row [ 1 ];
}
else
{
dolibarr_syslog ( " graph-solde.php Error " );
}
$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 );
$amounts = array ();
if ( $resql )
{
$num = $db -> num_rows ( $resql );
$i = 0 ;
while ( $i < $num )
{
$row = $db -> fetch_row ( $resql );
$amounts [ $row [ 0 ]] = $row [ 1 ];
$i ++ ;
}
}
else
{
dolibarr_syslog ( " graph-solde.php Error " );
}
$subtotal = 0 ;
$day = $min ;
$i = 0 ;
while ( $day <= $max )
{
//print strftime ("%e %d %m %y",$day)."\n";
$subtotal = $subtotal + $amounts [ strftime ( " %Y%m%d " , $day )];
$datas [ $i ] = $solde + $subtotal ;
$labels [ $i ] = strftime ( " %d " , $day );
$day += 86400 ;
$i ++ ;
}
2005-08-10 12:00:24 +02:00
2005-08-10 14:20:51 +02:00
if ( sizeof ( $amounts ) > 3 )
{
$width = 750 ;
$height = 350 ;
$graph = new Graph ( $width , $height , " auto " );
$graph -> SetScale ( " textlin " );
$graph -> yaxis -> scale -> SetGrace ( 2 );
$graph -> SetFrame ( 1 );
$graph -> img -> SetMargin ( 60 , 20 , 20 , 35 );
$b2plot = new LinePlot ( $datas );
$b2plot -> SetColor ( " blue " );
$graph -> title -> Set ( " Solde " );
$graph -> xaxis -> SetTickLabels ( $labels );
$graph -> xaxis -> Hide ();
$graph -> Add ( $b2plot );
$graph -> img -> SetImgFormat ( " png " );
2005-09-17 04:32:22 +02:00
$file = $conf -> banque -> dir_images . " /solde. $account .png " ;
2005-08-10 14:20:51 +02:00
$graph -> Stroke ( $file );
}
}
2005-11-13 18:05:59 +01:00
foreach ( $accounts as $account )
{
$labels = array ();
$datas = array ();
$amounts = array ();
$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 );
$amounts = array ();
if ( $resql )
{
$num = $db -> num_rows ( $resql );
$i = 0 ;
while ( $i < $num )
{
$row = $db -> fetch_row ( $resql );
$credits [ $row [ 0 ]] = $row [ 1 ];
$i ++ ;
}
}
else
{
print $sql ;
}
$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
{
print $sql ;
}
for ( $i = 0 ; $i < 12 ; $i ++ )
{
$data_credit [ $i ] = $credits [ substr ( " 0 " . ( $i + 1 ), - 2 )];
$data_debit [ $i ] = $debits [ substr ( " 0 " . ( $i + 1 ), - 2 )];
$labels [ $i ] = $i + 1 ;
}
$width = 750 ;
$height = 350 ;
$graph = new Graph ( $width , $height , " auto " );
$graph -> SetScale ( " textlin " );
$graph -> yaxis -> scale -> SetGrace ( 2 );
//$graph->SetFrame(1);
$graph -> img -> SetMargin ( 60 , 20 , 20 , 35 );
$bsplot = new BarPlot ( $data_debit );
$bsplot -> SetColor ( " red " );
$beplot = new BarPlot ( $data_credit );
$beplot -> SetColor ( " green " );
$bg = new GroupBarPlot ( array ( $beplot , $bsplot ));
$graph -> title -> Set ( " Mouvements $year " );
$graph -> xaxis -> SetTickLabels ( $labels );
$graph -> Add ( $bg );
$graph -> img -> SetImgFormat ( " png " );
$file = DOL_DATA_ROOT . " /graph/banque/mouvement. $account . $year .png " ;
$graph -> Stroke ( $file );
}
2005-07-31 19:24:43 +02:00
?>