2007-05-31 20:55:47 +02:00
< ? php
2012-07-12 19:14:13 +02:00
/* Copyright ( C ) 2003 Rodolphe Quiedeville < rodolphe @ quiedeville . org >
2013-07-16 18:42:43 +02:00
* Copyright ( c ) 2008 - 2013 Laurent Destailleur < eldy @ users . sourceforge . net >
2018-10-27 14:43:12 +02:00
* Copyright ( C ) 2012 Regis Houssin < regis . houssin @ inodbox . com >
2012-08-14 15:43:14 +02:00
* Copyright ( C ) 2012 Marcos García < marcosgdf @ gmail . com >
2007-05-31 20:55:47 +02: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
2013-01-16 15:36:08 +01:00
* the Free Software Foundation ; either version 3 of the License , or
2007-05-31 20:55:47 +02: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 />.
2007-05-31 20:55:47 +02:00
*/
2008-09-07 23:36:42 +02:00
/**
2012-03-12 15:09:46 +01:00
* \file htdocs / core / class / stats . class . php
* \ingroup core
* \brief Common class to manage statistics reports
2008-09-07 23:36:42 +02:00
*/
2008-11-09 13:54:44 +01:00
/**
2012-03-12 15:09:46 +01:00
* Parent class of statistics class
2008-11-09 13:54:44 +01:00
*/
2011-08-28 16:58:29 +02:00
abstract class Stats
2007-05-31 20:55:47 +02:00
{
2020-10-31 14:32:18 +01:00
protected $db ;
protected $lastfetchdate = array (); // Dates of cache file read by methods
public $cachefilesuffix = '' ; // Suffix to add to name of cache file (to avoid file name conflicts)
2011-09-20 19:19:46 +02:00
2008-09-07 23:36:42 +02:00
/**
2013-06-28 19:19:18 +02:00
* Return nb of elements by month for several years
2011-09-20 19:19:46 +02:00
*
2013-06-28 19:19:18 +02:00
* @ param int $endyear Start year
* @ param int $startyear End year
* @ param int $cachedelay Delay we accept for cache file ( 0 = No read , no save of cache , - 1 = No read but save )
2020-10-31 14:32:18 +01:00
* @ param int $format 0 = Label of abscissa is a translated text , 1 = Label of abscissa is month number , 2 = Label of abscissa is first letter of month
2019-08-03 06:04:42 +02:00
* @ param int $startmonth month of the fiscal year start min 1 max 12 ; if 1 = january
2020-10-31 14:32:18 +01:00
* @ return array Array of values
2008-09-07 23:36:42 +02:00
*/
2019-08-03 06:04:42 +02:00
public function getNbByMonthWithPrevYear ( $endyear , $startyear , $cachedelay = 0 , $format = 0 , $startmonth = 1 )
2008-09-07 23:36:42 +02:00
{
2020-03-12 12:45:44 +01:00
global $conf , $user , $langs ;
2013-06-29 02:23:23 +02:00
2021-02-23 22:03:23 +01:00
if ( $startyear > $endyear ) {
return - 1 ;
}
2011-05-25 20:16:58 +02:00
2020-03-12 12:45:44 +01:00
$datay = array ();
2013-06-29 02:23:23 +02:00
// Search into cache
2021-02-23 22:03:23 +01:00
if ( ! empty ( $cachedelay )) {
2020-10-31 14:32:18 +01:00
include_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php' ;
include_once DOL_DOCUMENT_ROOT . '/core/lib/json.lib.php' ;
}
2010-06-08 01:52:43 +02:00
2020-03-12 12:45:44 +01:00
$newpathofdestfile = $conf -> user -> dir_temp . '/' . get_class ( $this ) . '_' . __FUNCTION__ . '_' . ( empty ( $this -> cachefilesuffix ) ? '' : $this -> cachefilesuffix . '_' ) . $langs -> defaultlang . '_entity.' . $conf -> entity . '_user' . $user -> id . '.cache' ;
$newmask = '0644' ;
2013-07-01 12:00:42 +02:00
$nowgmt = dol_now ();
2020-03-12 12:45:44 +01:00
$foundintocache = 0 ;
2021-02-23 22:03:23 +01:00
if ( $cachedelay > 0 ) {
2020-03-12 12:45:44 +01:00
$filedate = dol_filemtime ( $newpathofdestfile );
2021-02-23 22:03:23 +01:00
if ( $filedate >= ( $nowgmt - $cachedelay )) {
2020-03-12 12:45:44 +01:00
$foundintocache = 1 ;
2013-07-01 12:00:42 +02:00
2020-03-12 12:45:44 +01:00
$this -> lastfetchdate [ get_class ( $this ) . '_' . __FUNCTION__ ] = $filedate ;
2020-05-21 15:05:19 +02:00
} else {
2013-08-05 17:19:49 +02:00
dol_syslog ( get_class ( $this ) . '::' . __FUNCTION__ . " cache file " . $newpathofdestfile . " is not found or older than now - cachedelay ( " . $nowgmt . " - " . $cachedelay . " ) so we can't use it. " );
2013-07-01 12:00:42 +02:00
}
}
// Load file into $data
2021-02-23 22:03:23 +01:00
if ( $foundintocache ) { // Cache file found and is not too old
2013-08-05 17:19:49 +02:00
dol_syslog ( get_class ( $this ) . '::' . __FUNCTION__ . " read data from cache file " . $newpathofdestfile . " " . $filedate . " . " );
2014-07-30 15:47:19 +02:00
$data = json_decode ( file_get_contents ( $newpathofdestfile ), true );
2020-05-21 15:05:19 +02:00
} else {
2020-03-12 12:45:44 +01:00
$year = $startyear ;
2019-08-03 06:04:42 +02:00
$sm = $startmonth - 1 ;
2021-02-23 22:03:23 +01:00
if ( $sm != 0 ) {
$year = $year - 1 ;
}
while ( $year <= $endyear ) {
2017-12-08 16:15:41 +01:00
$datay [ $year ] = $this -> getNbByMonth ( $year , $format );
2008-09-07 23:36:42 +02:00
$year ++ ;
}
2013-06-29 02:23:23 +02:00
2013-06-28 19:19:18 +02:00
$data = array ();
2013-06-29 02:23:23 +02:00
2021-02-23 22:03:23 +01:00
for ( $i = 0 ; $i < 12 ; $i ++ ) {
2020-03-12 12:45:44 +01:00
$data [ $i ][] = $datay [ $endyear ][( $i + $sm ) % 12 ][ 0 ];
$year = $startyear ;
2021-02-23 22:03:23 +01:00
while ( $year <= $endyear ) {
2020-03-12 12:45:44 +01:00
$data [ $i ][] = $datay [ $year - ( 1 - (( int ) ( $i + $sm ) / 12 )) + ( $sm == 0 ? 1 : 0 )][( $i + $sm ) % 12 ][ 1 ];
2013-06-28 19:19:18 +02:00
$year ++ ;
}
}
2008-09-07 23:36:42 +02:00
}
2010-06-08 01:52:43 +02:00
2013-06-28 19:19:18 +02:00
// Save cache file
2021-02-23 22:03:23 +01:00
if ( empty ( $foundintocache ) && ( $cachedelay > 0 || $cachedelay == - 1 )) {
2013-08-05 17:19:49 +02:00
dol_syslog ( get_class ( $this ) . '::' . __FUNCTION__ . " save cache file " . $newpathofdestfile . " onto disk. " );
2021-02-23 22:03:23 +01:00
if ( ! dol_is_dir ( $conf -> user -> dir_temp )) {
dol_mkdir ( $conf -> user -> dir_temp );
}
2013-07-01 12:00:42 +02:00
$fp = fopen ( $newpathofdestfile , 'w' );
2014-07-30 15:47:19 +02:00
fwrite ( $fp , json_encode ( $data ));
2013-07-01 12:00:42 +02:00
fclose ( $fp );
2021-02-23 22:03:23 +01:00
if ( ! empty ( $conf -> global -> MAIN_UMASK )) {
$newmask = $conf -> global -> MAIN_UMASK ;
}
2013-07-01 12:00:42 +02:00
@ chmod ( $newpathofdestfile , octdec ( $newmask ));
2020-03-12 12:45:44 +01:00
$this -> lastfetchdate [ get_class ( $this ) . '_' . __FUNCTION__ ] = $nowgmt ;
2013-06-28 19:19:18 +02:00
}
2013-06-29 02:23:23 +02:00
2010-06-08 01:52:43 +02:00
// return array(array('Month',val1,val2,val3),...)
2008-09-07 23:36:42 +02:00
return $data ;
}
/**
2015-07-09 12:29:01 +02:00
* Return amount of elements by month for several years .
* Criterias used to build request are defined into the constructor of parent class into xxx / class / xxxstats . class . php
* The caller of class can add more filters into sql request by adding criteris into the $stats -> where property just after
* calling constructor .
2008-09-07 23:36:42 +02:00
*
2012-02-21 00:18:48 +01:00
* @ param int $endyear Start year
* @ param int $startyear End year
2013-06-29 02:23:23 +02:00
* @ param int $cachedelay Delay we accept for cache file ( 0 = No read , no save of cache , - 1 = No read but save )
2020-10-31 14:32:18 +01:00
* @ param int $format 0 = Label of abscissa is a translated text , 1 = Label of abscissa is month number , 2 = Label of abscissa is first letter of month
2019-08-03 06:04:42 +02:00
* @ param int $startmonth month of the fiscal year start min 1 max 12 ; if 1 = january
2012-02-21 00:18:48 +01:00
* @ return array Array of values
2008-09-07 23:36:42 +02:00
*/
2019-08-03 06:04:42 +02:00
public function getAmountByMonthWithPrevYear ( $endyear , $startyear , $cachedelay = 0 , $format = 0 , $startmonth = 1 )
2008-09-07 23:36:42 +02:00
{
2020-03-12 12:45:44 +01:00
global $conf , $user , $langs ;
2013-06-29 02:23:23 +02:00
2021-02-23 22:03:23 +01:00
if ( $startyear > $endyear ) {
return - 1 ;
}
2020-10-31 14:32:18 +01:00
$datay = array ();
// Search into cache
2021-02-23 22:03:23 +01:00
if ( ! empty ( $cachedelay )) {
2020-10-31 14:32:18 +01:00
include_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php' ;
include_once DOL_DOCUMENT_ROOT . '/core/lib/json.lib.php' ;
}
$newpathofdestfile = $conf -> user -> dir_temp . '/' . get_class ( $this ) . '_' . __FUNCTION__ . '_' . ( empty ( $this -> cachefilesuffix ) ? '' : $this -> cachefilesuffix . '_' ) . $langs -> defaultlang . '_entity.' . $conf -> entity . '_user' . $user -> id . '.cache' ;
$newmask = '0644' ;
$nowgmt = dol_now ();
$foundintocache = 0 ;
2021-02-23 22:03:23 +01:00
if ( $cachedelay > 0 ) {
2020-10-31 14:32:18 +01:00
$filedate = dol_filemtime ( $newpathofdestfile );
2021-02-23 22:03:23 +01:00
if ( $filedate >= ( $nowgmt - $cachedelay )) {
2020-10-31 14:32:18 +01:00
$foundintocache = 1 ;
$this -> lastfetchdate [ get_class ( $this ) . '_' . __FUNCTION__ ] = $filedate ;
} else {
dol_syslog ( get_class ( $this ) . '::' . __FUNCTION__ . " cache file " . $newpathofdestfile . " is not found or older than now - cachedelay ( " . $nowgmt . " - " . $cachedelay . " ) so we can't use it. " );
}
}
// Load file into $data
2021-02-23 22:03:23 +01:00
if ( $foundintocache ) { // Cache file found and is not too old
2020-10-31 14:32:18 +01:00
dol_syslog ( get_class ( $this ) . '::' . __FUNCTION__ . " read data from cache file " . $newpathofdestfile . " " . $filedate . " . " );
$data = json_decode ( file_get_contents ( $newpathofdestfile ), true );
} else {
2020-03-12 12:45:44 +01:00
$year = $startyear ;
2019-08-03 06:04:42 +02:00
$sm = $startmonth - 1 ;
2021-02-23 22:03:23 +01:00
if ( $sm != 0 ) {
$year = $year - 1 ;
}
while ( $year <= $endyear ) {
2017-12-08 16:15:41 +01:00
$datay [ $year ] = $this -> getAmountByMonth ( $year , $format );
2008-09-07 23:36:42 +02:00
$year ++ ;
}
2013-06-29 02:23:23 +02:00
$data = array ();
2013-07-26 18:40:43 +02:00
// $data = array('xval'=>array(0=>xlabel,1=>yval1,2=>yval2...),...)
2021-02-23 22:03:23 +01:00
for ( $i = 0 ; $i < 12 ; $i ++ ) {
2020-03-28 13:38:25 +01:00
$data [ $i ][] = isset ( $datay [ $endyear ][( $i + $sm ) % 12 ][ 'label' ]) ? $datay [ $endyear ][( $i + $sm ) % 12 ][ 'label' ] : $datay [ $endyear ][( $i + $sm ) % 12 ][ 0 ]; // set label
2020-03-12 12:45:44 +01:00
$year = $startyear ;
2021-02-23 22:03:23 +01:00
while ( $year <= $endyear ) {
2020-03-12 12:45:44 +01:00
$data [ $i ][] = $datay [ $year - ( 1 - (( int ) ( $i + $sm ) / 12 )) + ( $sm == 0 ? 1 : 0 )][( $i + $sm ) % 12 ][ 1 ]; // set yval for x=i
2013-06-29 02:23:23 +02:00
$year ++ ;
}
}
}
2013-07-01 12:00:42 +02:00
// Save cache file
2021-02-23 22:03:23 +01:00
if ( empty ( $foundintocache ) && ( $cachedelay > 0 || $cachedelay == - 1 )) {
2013-08-05 17:19:49 +02:00
dol_syslog ( get_class ( $this ) . '::' . __FUNCTION__ . " save cache file " . $newpathofdestfile . " onto disk. " );
2021-02-23 22:03:23 +01:00
if ( ! dol_is_dir ( $conf -> user -> dir_temp )) {
dol_mkdir ( $conf -> user -> dir_temp );
}
2013-07-01 12:00:42 +02:00
$fp = fopen ( $newpathofdestfile , 'w' );
2021-02-23 22:03:23 +01:00
if ( $fp ) {
2015-04-03 15:25:24 +02:00
fwrite ( $fp , json_encode ( $data ));
fclose ( $fp );
2021-02-23 22:03:23 +01:00
if ( ! empty ( $conf -> global -> MAIN_UMASK )) {
$newmask = $conf -> global -> MAIN_UMASK ;
}
2015-04-03 15:25:24 +02:00
@ chmod ( $newpathofdestfile , octdec ( $newmask ));
2021-02-23 22:03:23 +01:00
} else {
dol_syslog ( " Failed to write cache file " , LOG_ERR );
}
2020-03-12 12:45:44 +01:00
$this -> lastfetchdate [ get_class ( $this ) . '_' . __FUNCTION__ ] = $nowgmt ;
2008-09-07 23:36:42 +02:00
}
return $data ;
}
2012-08-14 15:43:14 +02:00
/**
* Return average of entity by month for several years
*
* @ param int $endyear Start year
* @ param int $startyear End year
* @ return array Array of values
*/
2020-10-31 14:32:18 +01:00
public function getAverageByMonthWithPrevYear ( $endyear , $startyear )
{
2021-02-23 22:03:23 +01:00
if ( $startyear > $endyear ) {
return - 1 ;
}
2012-08-14 15:43:14 +02:00
2020-10-31 14:32:18 +01:00
$datay = array ();
2012-08-14 15:43:14 +02:00
2020-03-12 12:45:44 +01:00
$year = $startyear ;
2021-02-23 22:03:23 +01:00
while ( $year <= $endyear ) {
2012-08-14 15:43:14 +02:00
$datay [ $year ] = $this -> getAverageByMonth ( $year );
$year ++ ;
}
$data = array ();
2021-02-23 22:03:23 +01:00
for ( $i = 0 ; $i < 12 ; $i ++ ) {
2020-03-12 12:45:44 +01:00
$data [ $i ][] = $datay [ $endyear ][ $i ][ 0 ];
$year = $startyear ;
2021-02-23 22:03:23 +01:00
while ( $year <= $endyear ) {
2020-03-12 12:45:44 +01:00
$data [ $i ][] = $datay [ $year ][ $i ][ 1 ];
2012-08-14 15:43:14 +02:00
$year ++ ;
}
}
return $data ;
}
2013-08-05 17:19:49 +02:00
/**
* Return count , and sum of products
*
* @ param int $year Year
* @ param int $cachedelay Delay we accept for cache file ( 0 = No read , no save of cache , - 1 = No read but save )
2020-10-31 14:32:18 +01:00
* @ param int $limit Limit
2013-08-05 17:19:49 +02:00
* @ return array Array of values
*/
2020-10-31 14:32:18 +01:00
public function getAllByProductEntry ( $year , $cachedelay = 0 , $limit = 10 )
{
global $conf , $user , $langs ;
$data = array ();
// Search into cache
2021-02-23 22:03:23 +01:00
if ( ! empty ( $cachedelay )) {
2020-10-31 14:32:18 +01:00
include_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php' ;
include_once DOL_DOCUMENT_ROOT . '/core/lib/json.lib.php' ;
}
$newpathofdestfile = $conf -> user -> dir_temp . '/' . get_class ( $this ) . '_' . __FUNCTION__ . '_' . ( empty ( $this -> cachefilesuffix ) ? '' : $this -> cachefilesuffix . '_' ) . $langs -> defaultlang . '_entity.' . $conf -> entity . '_user' . $user -> id . '.cache' ;
$newmask = '0644' ;
$nowgmt = dol_now ();
$foundintocache = 0 ;
2021-02-23 22:03:23 +01:00
if ( $cachedelay > 0 ) {
2020-10-31 14:32:18 +01:00
$filedate = dol_filemtime ( $newpathofdestfile );
2021-02-23 22:03:23 +01:00
if ( $filedate >= ( $nowgmt - $cachedelay )) {
2020-10-31 14:32:18 +01:00
$foundintocache = 1 ;
$this -> lastfetchdate [ get_class ( $this ) . '_' . __FUNCTION__ ] = $filedate ;
} else {
dol_syslog ( get_class ( $this ) . '::' . __FUNCTION__ . " cache file " . $newpathofdestfile . " is not found or older than now - cachedelay ( " . $nowgmt . " - " . $cachedelay . " ) so we can't use it. " );
}
}
// Load file into $data
2021-02-23 22:03:23 +01:00
if ( $foundintocache ) { // Cache file found and is not too old
2020-10-31 14:32:18 +01:00
dol_syslog ( get_class ( $this ) . '::' . __FUNCTION__ . " read data from cache file " . $newpathofdestfile . " " . $filedate . " . " );
$data = json_decode ( file_get_contents ( $newpathofdestfile ), true );
} else {
2020-03-06 12:45:02 +01:00
$data = $this -> getAllByProduct ( $year , $limit );
2013-08-05 17:19:49 +02:00
// $data[$i][]=$datay[$year][$i][1]; // set yval for x=i
}
2008-09-07 23:36:42 +02:00
2013-08-05 17:19:49 +02:00
// Save cache file
2021-02-23 22:03:23 +01:00
if ( empty ( $foundintocache ) && ( $cachedelay > 0 || $cachedelay == - 1 )) {
2013-08-05 17:19:49 +02:00
dol_syslog ( get_class ( $this ) . '::' . __FUNCTION__ . " save cache file " . $newpathofdestfile . " onto disk. " );
2021-02-23 22:03:23 +01:00
if ( ! dol_is_dir ( $conf -> user -> dir_temp )) {
dol_mkdir ( $conf -> user -> dir_temp );
}
2013-08-05 17:19:49 +02:00
$fp = fopen ( $newpathofdestfile , 'w' );
2021-02-23 22:03:23 +01:00
if ( $fp ) {
2015-04-03 15:25:24 +02:00
fwrite ( $fp , json_encode ( $data ));
fclose ( $fp );
2021-02-23 22:03:23 +01:00
if ( ! empty ( $conf -> global -> MAIN_UMASK )) {
$newmask = $conf -> global -> MAIN_UMASK ;
}
2015-04-03 15:25:24 +02:00
@ chmod ( $newpathofdestfile , octdec ( $newmask ));
}
2020-03-12 12:45:44 +01:00
$this -> lastfetchdate [ get_class ( $this ) . '_' . __FUNCTION__ ] = $nowgmt ;
2013-08-05 17:19:49 +02:00
}
return $data ;
2015-04-03 15:25:24 +02:00
}
2013-08-05 17:19:49 +02:00
// Here we have low level of shared code called by XxxStats.class.php
2015-04-03 15:25:24 +02:00
2020-10-31 14:32:18 +01:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
/**
2011-05-25 20:16:58 +02:00
* Return nb of elements by year
2011-09-20 19:19:46 +02:00
*
2012-02-21 00:18:48 +01:00
* @ param string $sql SQL request
* @ return array
2008-09-07 23:36:42 +02:00
*/
2020-10-31 14:32:18 +01:00
protected function _getNbByYear ( $sql )
{
// phpcs:enable
2008-09-07 23:36:42 +02:00
$result = array ();
2010-06-08 01:52:43 +02:00
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . '::' . __FUNCTION__ . " " , LOG_DEBUG );
2020-03-12 12:45:44 +01:00
$resql = $this -> db -> query ( $sql );
2021-02-23 22:03:23 +01:00
if ( $resql ) {
2008-09-07 23:36:42 +02:00
$num = $this -> db -> num_rows ( $resql );
$i = 0 ;
2021-02-23 22:03:23 +01:00
while ( $i < $num ) {
2008-09-07 23:36:42 +02:00
$row = $this -> db -> fetch_row ( $resql );
$result [ $i ] = $row ;
$i ++ ;
}
$this -> db -> free ( $resql );
2020-05-21 15:05:19 +02:00
} else {
2009-02-20 23:53:15 +01:00
dol_print_error ( $this -> db );
2008-09-07 23:36:42 +02:00
}
return $result ;
}
2020-10-31 14:32:18 +01:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
2008-09-08 09:32:03 +02:00
/**
2011-12-28 13:25:05 +01:00
* Return nb of elements , total amount and avg amount each year
2011-09-20 19:19:46 +02:00
*
2012-02-21 00:18:48 +01:00
* @ param string $sql SQL request
* @ return array Array with nb , total amount , average for each year
2008-09-08 09:32:03 +02:00
*/
2019-02-26 22:42:19 +01:00
protected function _getAllByYear ( $sql )
2008-09-08 09:32:03 +02:00
{
2020-10-31 14:32:18 +01:00
// phpcs:enable
2008-09-08 09:32:03 +02:00
$result = array ();
2010-06-08 01:52:43 +02:00
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . '::' . __FUNCTION__ . " " , LOG_DEBUG );
2020-03-12 12:45:44 +01:00
$resql = $this -> db -> query ( $sql );
2021-02-23 22:03:23 +01:00
if ( $resql ) {
2008-09-08 09:32:03 +02:00
$num = $this -> db -> num_rows ( $resql );
$i = 0 ;
2021-02-23 22:03:23 +01:00
while ( $i < $num ) {
2008-09-08 09:32:03 +02:00
$row = $this -> db -> fetch_object ( $resql );
$result [ $i ][ 'year' ] = $row -> year ;
$result [ $i ][ 'nb' ] = $row -> nb ;
2021-02-23 22:03:23 +01:00
if ( $i > 0 && $row -> nb > 0 ) {
$result [ $i - 1 ][ 'nb_diff' ] = ( $result [ $i - 1 ][ 'nb' ] - $row -> nb ) / $row -> nb * 100 ;
}
2008-09-08 09:32:03 +02:00
$result [ $i ][ 'total' ] = $row -> total ;
2021-02-23 22:03:23 +01:00
if ( $i > 0 && $row -> total > 0 ) {
$result [ $i - 1 ][ 'total_diff' ] = ( $result [ $i - 1 ][ 'total' ] - $row -> total ) / $row -> total * 100 ;
}
2008-09-08 09:32:03 +02:00
$result [ $i ][ 'avg' ] = $row -> avg ;
2021-02-23 22:03:23 +01:00
if ( $i > 0 && $row -> avg > 0 ) {
$result [ $i - 1 ][ 'avg_diff' ] = ( $result [ $i - 1 ][ 'avg' ] - $row -> avg ) / $row -> avg * 100 ;
}
2016-07-08 10:59:13 +02:00
// For some $sql only
2021-02-23 22:03:23 +01:00
if ( isset ( $row -> weighted )) {
2020-10-31 14:32:18 +01:00
$result [ $i ][ 'weighted' ] = $row -> weighted ;
2021-02-23 22:03:23 +01:00
if ( $i > 0 && $row -> weighted > 0 ) {
$result [ $i - 1 ][ 'avg_weighted' ] = ( $result [ $i - 1 ][ 'weighted' ] - $row -> weighted ) / $row -> weighted * 100 ;
}
2016-07-08 10:59:13 +02:00
}
2008-09-08 09:32:03 +02:00
$i ++ ;
}
$this -> db -> free ( $resql );
2020-05-21 15:05:19 +02:00
} else {
2009-02-20 23:53:15 +01:00
dol_print_error ( $this -> db );
2008-09-08 09:32:03 +02:00
}
return $result ;
2010-06-08 01:52:43 +02:00
}
2020-10-31 14:32:18 +01:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
2008-09-07 23:36:42 +02:00
/**
2019-08-03 06:04:42 +02:00
* Renvoie le nombre de documents par mois pour une annee donnee
* Return number of documents per month for a given year
2011-09-20 19:19:46 +02:00
*
2020-10-31 14:32:18 +01:00
* @ param int $year Year
* @ param string $sql SQL
* @ param int $format 0 = Label of abscissa is a translated text , 1 = Label of abscissa is month number , 2 = Label of abscissa is first letter of month
* @ return array Array of nb each month
*/
protected function _getNbByMonth ( $year , $sql , $format = 0 )
{
// phpcs:enable
2017-12-08 16:15:41 +01:00
global $langs ;
2020-03-12 12:45:44 +01:00
$result = array ();
$res = array ();
2008-09-07 23:36:42 +02:00
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . '::' . __FUNCTION__ . " " , LOG_DEBUG );
2020-03-12 12:45:44 +01:00
$resql = $this -> db -> query ( $sql );
2021-02-23 22:03:23 +01:00
if ( $resql ) {
2008-09-07 23:36:42 +02:00
$num = $this -> db -> num_rows ( $resql );
2021-03-01 20:37:16 +01:00
$i = 0 ;
$j = 0 ;
2021-02-23 22:03:23 +01:00
while ( $i < $num ) {
2008-09-07 23:36:42 +02:00
$row = $this -> db -> fetch_row ( $resql );
$j = $row [ 0 ] * 1 ;
$result [ $j ] = $row [ 1 ];
$i ++ ;
}
$this -> db -> free ( $resql );
2020-05-21 15:05:19 +02:00
} else {
2009-02-20 23:53:15 +01:00
dol_print_error ( $this -> db );
2008-11-17 00:43:10 +01:00
}
2010-06-08 01:52:43 +02:00
2021-02-23 22:03:23 +01:00
for ( $i = 1 ; $i < 13 ; $i ++ ) {
2020-03-12 12:45:44 +01:00
$res [ $i ] = ( isset ( $result [ $i ]) ? $result [ $i ] : 0 );
2008-09-07 23:36:42 +02:00
}
$data = array ();
2021-02-23 22:03:23 +01:00
for ( $i = 1 ; $i < 13 ; $i ++ ) {
2020-03-12 12:45:44 +01:00
$month = 'unknown' ;
2021-02-23 22:03:23 +01:00
if ( $format == 0 ) {
$month = $langs -> transnoentitiesnoconv ( 'MonthShort' . sprintf ( " %02d " , $i ));
} elseif ( $format == 1 ) {
$month = $i ;
} elseif ( $format == 2 ) {
$month = $langs -> transnoentitiesnoconv ( 'MonthVeryShort' . sprintf ( " %02d " , $i ));
}
2017-12-08 16:15:41 +01:00
//$month=dol_print_date(dol_mktime(12,0,0,$i,1,$year),($format?"%m":"%b"));
//$month=dol_substr($month,0,3);
2020-03-12 12:45:44 +01:00
$data [ $i - 1 ] = array ( $month , $res [ $i ]);
2008-09-07 23:36:42 +02:00
}
return $data ;
}
2020-10-31 14:32:18 +01:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
2008-09-07 23:36:42 +02:00
/**
2020-10-31 14:32:18 +01:00
* Return the amount per month for a given year
2011-09-20 19:19:46 +02:00
*
2013-06-28 19:19:18 +02:00
* @ param int $year Year
* @ param string $sql SQL
2020-10-31 14:32:18 +01:00
* @ param int $format 0 = Label of abscissa is a translated text , 1 = Label of abscissa is month number , 2 = Label of abscissa is first letter of month
2012-02-21 00:18:48 +01:00
* @ return array
2008-09-07 23:36:42 +02:00
*/
2020-10-31 14:32:18 +01:00
protected function _getAmountByMonth ( $year , $sql , $format = 0 )
{
// phpcs:enable
2017-12-08 16:15:41 +01:00
global $langs ;
2020-03-12 12:45:44 +01:00
$result = array ();
$res = array ();
2008-09-07 23:36:42 +02:00
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . '::' . __FUNCTION__ . " " , LOG_DEBUG );
2011-06-21 14:39:09 +02:00
2020-03-12 12:45:44 +01:00
$resql = $this -> db -> query ( $sql );
2021-02-23 22:03:23 +01:00
if ( $resql ) {
2008-09-07 23:36:42 +02:00
$num = $this -> db -> num_rows ( $resql );
$i = 0 ;
2021-02-23 22:03:23 +01:00
while ( $i < $num ) {
$row = $this -> db -> fetch_row ( $resql );
$j = $row [ 0 ] * 1 ;
$result [ $j ] = $row [ 1 ];
$i ++ ;
}
$this -> db -> free ( $resql );
} else {
dol_print_error ( $this -> db );
}
for ( $i = 1 ; $i < 13 ; $i ++ ) {
2020-03-12 12:45:44 +01:00
$res [ $i ] = ( int ) round (( isset ( $result [ $i ]) ? $result [ $i ] : 0 ));
2008-09-07 23:36:42 +02:00
}
$data = array ();
2021-02-23 22:03:23 +01:00
for ( $i = 1 ; $i < 13 ; $i ++ ) {
2020-03-12 12:45:44 +01:00
$month = 'unknown' ;
2021-02-23 22:03:23 +01:00
if ( $format == 0 ) {
$month = $langs -> transnoentitiesnoconv ( 'MonthShort' . sprintf ( " %02d " , $i ));
} elseif ( $format == 1 ) {
$month = $i ;
} elseif ( $format == 2 ) {
$month = $langs -> transnoentitiesnoconv ( 'MonthVeryShort' . sprintf ( " %02d " , $i ));
}
2017-12-08 16:15:41 +01:00
//$month=dol_print_date(dol_mktime(12,0,0,$i,1,$year),($format?"%m":"%b"));
//$month=dol_substr($month,0,3);
2020-03-12 12:45:44 +01:00
$data [ $i - 1 ] = array ( $month , $res [ $i ]);
2008-09-07 23:36:42 +02:00
}
return $data ;
2020-10-31 14:32:18 +01:00
}
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
/**
* Renvoie le montant moyen par mois pour une annee donnee
* Return the amount average par month for a given year
*
* @ param int $year Year
* @ param string $sql SQL
* @ param int $format 0 = Label of abscissa is a translated text , 1 = Label of abscissa is month number , 2 = Label of abscissa is first letter of month
* @ return array
*/
protected function _getAverageByMonth ( $year , $sql , $format = 0 )
{
// phpcs:enable
2017-12-08 16:15:41 +01:00
global $langs ;
2020-03-12 12:45:44 +01:00
$result = array ();
$res = array ();
2008-09-07 23:36:42 +02:00
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . '::' . __FUNCTION__ . " " , LOG_DEBUG );
2020-03-12 12:45:44 +01:00
$resql = $this -> db -> query ( $sql );
2021-02-23 22:03:23 +01:00
if ( $resql ) {
2008-09-07 23:36:42 +02:00
$num = $this -> db -> num_rows ( $resql );
2021-03-01 20:37:16 +01:00
$i = 0 ;
$j = 0 ;
2021-02-23 22:03:23 +01:00
while ( $i < $num ) {
$row = $this -> db -> fetch_row ( $resql );
$j = $row [ 0 ] * 1 ;
$result [ $j ] = $row [ 1 ];
$i ++ ;
}
$this -> db -> free ( $resql );
} else {
dol_print_error ( $this -> db );
}
for ( $i = 1 ; $i < 13 ; $i ++ ) {
2020-03-12 12:45:44 +01:00
$res [ $i ] = ( isset ( $result [ $i ]) ? $result [ $i ] : 0 );
2008-09-07 23:36:42 +02:00
}
2012-08-14 15:43:14 +02:00
$data = array ();
2021-02-23 22:03:23 +01:00
for ( $i = 1 ; $i < 13 ; $i ++ ) {
2020-03-12 12:45:44 +01:00
$month = 'unknown' ;
2021-02-23 22:03:23 +01:00
if ( $format == 0 ) {
$month = $langs -> transnoentitiesnoconv ( 'MonthShort' . sprintf ( " %02d " , $i ));
} elseif ( $format == 1 ) {
$month = $i ;
} elseif ( $format == 2 ) {
$month = $langs -> transnoentitiesnoconv ( 'MonthVeryShort' . sprintf ( " %02d " , $i ));
}
2017-12-08 16:15:41 +01:00
//$month=dol_print_date(dol_mktime(12,0,0,$i,1,$year),($format?"%m":"%b"));
//$month=dol_substr($month,0,3);
2020-03-12 12:45:44 +01:00
$data [ $i - 1 ] = array ( $month , $res [ $i ]);
2012-08-14 15:43:14 +02:00
}
return $data ;
2020-10-31 14:32:18 +01:00
}
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
/**
* Return number or total of product refs
*
* @ param string $sql SQL
* @ param int $limit Limit
* @ return array
*/
protected function _getAllByProduct ( $sql , $limit = 10 )
{
// phpcs:enable
2013-08-27 11:54:07 +02:00
global $langs ;
2015-04-03 15:25:24 +02:00
2020-03-12 12:45:44 +01:00
$result = array ();
2013-08-05 17:19:49 +02:00
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . '::' . __FUNCTION__ . " " , LOG_DEBUG );
2020-03-12 12:45:44 +01:00
$resql = $this -> db -> query ( $sql );
2021-02-23 22:03:23 +01:00
if ( $resql ) {
2013-08-05 17:19:49 +02:00
$num = $this -> db -> num_rows ( $resql );
2021-03-01 20:37:16 +01:00
$i = 0 ;
$other = 0 ;
2021-02-23 22:03:23 +01:00
while ( $i < $num ) {
2020-10-31 14:32:18 +01:00
$row = $this -> db -> fetch_row ( $resql );
2021-02-23 22:03:23 +01:00
if ( $i < $limit || $num == $limit ) {
$result [ $i ] = array ( $row [ 0 ], $row [ 1 ]); // Ref of product, nb
} else {
$other += $row [ 1 ];
}
$i ++ ;
2020-10-31 14:32:18 +01:00
}
2021-02-23 22:03:23 +01:00
if ( $num > $limit ) {
$result [ $i ] = array ( $langs -> transnoentitiesnoconv ( " Other " ), $other );
}
$this -> db -> free ( $resql );
} else {
dol_print_error ( $this -> db );
}
2013-08-05 17:19:49 +02:00
return $result ;
2020-10-31 14:32:18 +01:00
}
2007-05-31 20:55:47 +02:00
}