2010-07-17 22:25:56 +02:00
< ? php
/* Copyright ( C ) 2003 Rodolphe Quiedeville < rodolphe @ quiedeville . org >
2013-07-18 15:25:25 +02:00
* Copyright ( c ) 2005 - 2013 Laurent Destailleur < eldy @ users . sourceforge . net >
2018-10-27 14:43:12 +02:00
* Copyright ( C ) 2005 - 2009 Regis Houssin < regis . houssin @ inodbox . com >
2020-04-29 09:15:27 +02:00
* Copyright ( C ) 2020 Maxime DEMAREST < maxime @ indelog . fr >
2010-07-17 22:25:56 +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
2010-07-17 22:25:56 +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 />.
2010-07-17 22:25:56 +02:00
*/
/**
2010-07-17 22:25:56 +02:00
* \file htdocs / compta / facture / class / facturestats . class . php
2010-07-17 22:25:56 +02:00
* \ingroup factures
* \brief Fichier de la classe de gestion des stats des factures
*/
2020-03-12 12:45:44 +01:00
include_once DOL_DOCUMENT_ROOT . '/core/class/stats.class.php' ;
include_once DOL_DOCUMENT_ROOT . '/compta/facture/class/facture.class.php' ;
include_once DOL_DOCUMENT_ROOT . '/fourn/class/fournisseur.facture.class.php' ;
include_once DOL_DOCUMENT_ROOT . '/core/lib/date.lib.php' ;
2010-07-17 22:25:56 +02:00
/**
2013-06-28 19:19:18 +02:00
* Class to manage stats for invoices ( customer and supplier )
2010-07-17 22:25:56 +02:00
*/
class FactureStats extends Stats
{
2020-10-31 14:32:18 +01:00
public $socid ;
public $userid ;
2011-06-13 15:42:51 +02:00
2020-10-31 14:32:18 +01:00
/**
2018-08-22 18:34:50 +02:00
* @ var string Name of table without prefix where object is stored
*/
public $table_element ;
2019-03-02 00:14:22 +01:00
2020-10-31 14:32:18 +01:00
public $from ;
public $field ;
public $where ;
public $join ;
2017-10-17 11:08:49 +02:00
2010-07-17 22:25:56 +02:00
/**
2020-10-31 14:32:18 +01:00
* Constructor
*
2012-01-10 09:57:16 +01:00
* @ param DoliDB $db Database handler
2015-10-01 10:48:09 +02:00
* @ param int $socid Id third party for filter . This value must be forced during the new to external user company if user is an external user .
2013-06-28 19:19:18 +02:00
* @ param string $mode Option ( 'customer' , 'supplier' )
2020-10-31 14:32:18 +01:00
* @ param int $userid Id user for filter ( creation user )
* @ param int $typentid Id typent of thirdpary for filter
* @ param int $categid Id category of thirdpary for filter
2010-07-17 22:25:56 +02:00
*/
2022-05-18 11:00:43 +02:00
public function __construct ( DoliDB $db , $socid , $mode , $userid = 0 , $typentid = 0 , $categid = 0 )
2010-07-17 22:25:56 +02:00
{
2013-05-20 19:24:27 +02:00
global $user , $conf ;
2010-07-17 22:25:56 +02:00
2012-01-10 09:57:16 +01:00
$this -> db = $db ;
2020-10-31 14:32:18 +01:00
$this -> socid = ( $socid > 0 ? $socid : 0 );
$this -> userid = $userid ;
2017-10-17 11:08:49 +02:00
$this -> cachefilesuffix = $mode ;
2020-10-31 14:32:18 +01:00
$this -> join = '' ;
2017-10-17 11:08:49 +02:00
2021-02-23 21:09:01 +01:00
if ( $mode == 'customer' ) {
2020-03-12 12:45:44 +01:00
$object = new Facture ( $this -> db );
2013-05-20 19:24:27 +02:00
$this -> from = MAIN_DB_PREFIX . $object -> table_element . " as f " ;
2013-08-05 17:19:49 +02:00
$this -> from_line = MAIN_DB_PREFIX . $object -> table_element_line . " as tl " ;
2021-04-07 00:44:01 +02:00
$this -> field = 'total_ht' ;
2020-03-12 12:45:44 +01:00
$this -> field_line = 'total_ht' ;
2010-07-17 22:25:56 +02:00
}
2021-02-23 21:09:01 +01:00
if ( $mode == 'supplier' ) {
2020-03-12 12:45:44 +01:00
$object = new FactureFournisseur ( $this -> db );
2013-05-20 19:24:27 +02:00
$this -> from = MAIN_DB_PREFIX . $object -> table_element . " as f " ;
2013-08-05 17:19:49 +02:00
$this -> from_line = MAIN_DB_PREFIX . $object -> table_element_line . " as tl " ;
2020-03-12 12:45:44 +01:00
$this -> field = 'total_ht' ;
$this -> field_line = 'total_ht' ;
2010-07-17 22:25:56 +02:00
}
2020-04-29 09:15:27 +02:00
2020-01-08 11:27:52 +01:00
$this -> where = " f.fk_statut >= 0 " ;
2020-03-12 12:45:44 +01:00
$this -> where .= " AND f.entity IN ( " . getEntity ( 'invoice' ) . " ) " ;
2021-10-22 22:15:59 +02:00
if ( empty ( $user -> rights -> societe -> client -> voir ) && ! $this -> socid ) {
2021-08-23 17:41:11 +02:00
$this -> where .= " AND f.fk_soc = sc.fk_soc AND sc.fk_user = " . (( int ) $user -> id );
2021-02-23 21:09:01 +01:00
}
if ( $mode == 'customer' ) {
$this -> where .= " AND (f.fk_statut <> 3 OR f.close_code <> 'replaced') " ; // Exclude replaced invoices as they are duplicated (we count closed invoices for other reasons)
}
if ( $this -> socid ) {
2021-08-23 17:41:11 +02:00
$this -> where .= " AND f.fk_soc = " . (( int ) $this -> socid );
2010-07-17 22:25:56 +02:00
}
2021-02-23 21:09:01 +01:00
if ( $this -> userid > 0 ) {
2021-08-23 17:41:11 +02:00
$this -> where .= ' AND f.fk_user_author = ' . (( int ) $this -> userid );
2021-02-23 21:09:01 +01:00
}
2022-10-12 12:14:04 +02:00
if ( $mode == 'customer' ) {
if ( ! empty ( $conf -> global -> FACTURE_DEPOSITS_ARE_JUST_PAYMENTS )) {
$this -> where .= " AND f.type IN (0,1,2,5) " ;
} else {
$this -> where .= " AND f.type IN (0,1,2,3,5) " ;
}
}
if ( $mode == 'supplier' ) {
if ( ! empty ( $conf -> global -> FACTURE_SUPPLIER_DEPOSITS_ARE_JUST_PAYMENTS )) {
$this -> where .= " AND f.type IN (0,1,2,5) " ;
} else {
$this -> where .= " AND f.type IN (0,1,2,3,5) " ;
}
2021-02-23 21:09:01 +01:00
}
2020-04-29 09:15:27 +02:00
2021-02-23 21:09:01 +01:00
if ( $typentid ) {
2020-10-31 14:32:18 +01:00
$this -> join .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'societe as s ON s.rowid = f.fk_soc' ;
2021-03-30 11:36:50 +02:00
$this -> where .= ' AND s.fk_typent = ' . (( int ) $typentid );
2020-10-31 14:32:18 +01:00
}
2021-02-23 21:09:01 +01:00
if ( $categid ) {
2020-10-31 14:32:18 +01:00
$this -> join .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'categorie_societe as cs ON cs.fk_soc = f.fk_soc' ;
$this -> join .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'categorie as c ON c.rowid = cs.fk_categorie' ;
2021-03-30 11:36:50 +02:00
$this -> where .= ' AND c.rowid = ' . (( int ) $categid );
2020-10-31 14:32:18 +01:00
}
2010-07-17 22:25:56 +02:00
}
/**
2013-07-18 15:25:25 +02:00
* Return orders number by month for a year
2012-01-10 09:57:16 +01:00
*
2013-06-28 19:19:18 +02:00
* @ param int $year Year to scan
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
2013-06-28 19:19:18 +02:00
* @ return array Array of values
2010-07-17 22:25:56 +02:00
*/
2019-03-02 00:14:22 +01:00
public function getNbByMonth ( $year , $format = 0 )
2010-07-17 22:25:56 +02:00
{
2013-07-01 02:10:22 +02:00
global $user ;
2013-07-18 15:25:25 +02:00
$sql = " SELECT date_format(f.datef,'%m') as dm, COUNT(*) as nb " ;
2020-03-12 12:45:44 +01:00
$sql .= " FROM " . $this -> from ;
2021-10-22 22:15:59 +02:00
if ( empty ( $user -> rights -> societe -> client -> voir ) && ! $this -> socid ) {
2021-02-23 21:09:01 +01:00
$sql .= " , " . MAIN_DB_PREFIX . " societe_commerciaux as sc " ;
}
2020-10-31 14:32:18 +01:00
$sql .= $this -> join ;
2020-03-12 12:45:44 +01:00
$sql .= " WHERE f.datef BETWEEN ' " . $this -> db -> idate ( dol_get_first_day ( $year )) . " ' AND ' " . $this -> db -> idate ( dol_get_last_day ( $year )) . " ' " ;
$sql .= " AND " . $this -> where ;
$sql .= " GROUP BY dm " ;
2020-10-31 14:32:18 +01:00
$sql .= $this -> db -> order ( 'dm' , 'DESC' );
2010-07-17 22:25:56 +02:00
2020-03-12 12:45:44 +01:00
$res = $this -> _getNbByMonth ( $year , $sql , $format );
2013-05-20 19:24:27 +02:00
//var_dump($res);print '<br>';
return $res ;
2010-07-17 22:25:56 +02:00
}
/**
2013-07-18 15:25:25 +02:00
* Return invoices number per year
2012-01-10 09:57:16 +01:00
*
2013-07-18 15:25:25 +02:00
* @ return array Array with number by year
2010-07-17 22:25:56 +02:00
*/
2019-03-02 00:14:22 +01:00
public function getNbByYear ()
2010-07-17 22:25:56 +02:00
{
2013-07-01 12:00:42 +02:00
global $user ;
2013-07-01 02:10:22 +02:00
2013-07-18 15:25:25 +02:00
$sql = " SELECT date_format(f.datef,'%Y') as dm, COUNT(*), SUM(c. " . $this -> field . " ) " ;
2020-03-12 12:45:44 +01:00
$sql .= " FROM " . $this -> from ;
2021-10-22 22:15:59 +02:00
if ( empty ( $user -> rights -> societe -> client -> voir ) && ! $this -> socid ) {
2021-02-23 21:09:01 +01:00
$sql .= " , " . MAIN_DB_PREFIX . " societe_commerciaux as sc " ;
}
2020-10-31 14:32:18 +01:00
$sql .= $this -> join ;
2020-03-12 12:45:44 +01:00
$sql .= " WHERE " . $this -> where ;
$sql .= " GROUP BY dm " ;
2020-10-31 14:32:18 +01:00
$sql .= $this -> db -> order ( 'dm' , 'DESC' );
2010-07-17 22:25:56 +02:00
2013-05-20 19:24:27 +02:00
return $this -> _getNbByYear ( $sql );
2010-07-17 22:25:56 +02:00
}
/**
2013-07-18 15:25:25 +02:00
* Return the invoices amount by month for a year
2012-01-10 09:57:16 +01:00
*
2017-12-08 16:15:41 +01:00
* @ param int $year Year to scan
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
2017-12-08 16:15:41 +01:00
* @ return array Array with amount by month
2010-07-17 22:25:56 +02:00
*/
2019-03-02 00:14:22 +01:00
public function getAmountByMonth ( $year , $format = 0 )
2010-07-17 22:25:56 +02:00
{
2013-07-01 02:10:22 +02:00
global $user ;
2013-05-20 19:24:27 +02:00
$sql = " SELECT date_format(datef,'%m') as dm, SUM(f. " . $this -> field . " ) " ;
2020-03-12 12:45:44 +01:00
$sql .= " FROM " . $this -> from ;
2021-10-22 22:15:59 +02:00
if ( empty ( $user -> rights -> societe -> client -> voir ) && ! $this -> socid ) {
2021-02-23 21:09:01 +01:00
$sql .= " , " . MAIN_DB_PREFIX . " societe_commerciaux as sc " ;
}
2020-10-31 14:32:18 +01:00
$sql .= $this -> join ;
2020-03-12 12:45:44 +01:00
$sql .= " WHERE f.datef BETWEEN ' " . $this -> db -> idate ( dol_get_first_day ( $year )) . " ' AND ' " . $this -> db -> idate ( dol_get_last_day ( $year )) . " ' " ;
$sql .= " AND " . $this -> where ;
2020-10-31 14:32:18 +01:00
$sql .= " GROUP BY dm " ;
$sql .= $this -> db -> order ( 'dm' , 'DESC' );
2020-03-12 12:45:44 +01:00
$res = $this -> _getAmountByMonth ( $year , $sql , $format );
2010-07-17 22:25:56 +02:00
//var_dump($res);print '<br>';
return $res ;
}
/**
2011-12-16 16:31:18 +01:00
* Return average amount
*
* @ param int $year Year to scan
* @ return array Array of values
2010-07-17 22:25:56 +02:00
*/
2019-03-02 00:14:22 +01:00
public function getAverageByMonth ( $year )
2010-07-17 22:25:56 +02:00
{
2013-07-01 02:10:22 +02:00
global $user ;
2013-05-20 19:24:27 +02:00
$sql = " SELECT date_format(datef,'%m') as dm, AVG(f. " . $this -> field . " ) " ;
2020-03-12 12:45:44 +01:00
$sql .= " FROM " . $this -> from ;
2021-10-22 22:15:59 +02:00
if ( empty ( $user -> rights -> societe -> client -> voir ) && ! $this -> socid ) {
2021-02-23 21:09:01 +01:00
$sql .= " , " . MAIN_DB_PREFIX . " societe_commerciaux as sc " ;
}
2020-10-31 14:32:18 +01:00
$sql .= $this -> join ;
$sql .= " WHERE f.datef BETWEEN ' " . $this -> db -> idate ( dol_get_first_day ( $year )) . " ' AND ' " . $this -> db -> idate ( dol_get_last_day ( $year )) . " ' " ;
2020-03-12 12:45:44 +01:00
$sql .= " AND " . $this -> where ;
2020-10-31 14:32:18 +01:00
$sql .= " GROUP BY dm " ;
$sql .= $this -> db -> order ( 'dm' , 'DESC' );
2010-07-17 22:25:56 +02:00
return $this -> _getAverageByMonth ( $year , $sql );
}
/**
2011-12-16 16:31:18 +01:00
* Return nb , total and average
*
* @ return array Array of values
2010-07-17 22:25:56 +02:00
*/
2019-03-02 00:14:22 +01:00
public function getAllByYear ()
2010-07-17 22:25:56 +02:00
{
2013-07-01 02:10:22 +02:00
global $user ;
2013-05-20 19:24:27 +02:00
$sql = " SELECT date_format(datef,'%Y') as year, COUNT(*) as nb, SUM(f. " . $this -> field . " ) as total, AVG(f. " . $this -> field . " ) as avg " ;
2020-03-12 12:45:44 +01:00
$sql .= " FROM " . $this -> from ;
2021-10-22 22:15:59 +02:00
if ( empty ( $user -> rights -> societe -> client -> voir ) && ! $this -> socid ) {
2021-02-23 21:09:01 +01:00
$sql .= " , " . MAIN_DB_PREFIX . " societe_commerciaux as sc " ;
}
2020-10-31 14:32:18 +01:00
$sql .= $this -> join ;
2020-03-12 12:45:44 +01:00
$sql .= " WHERE " . $this -> where ;
$sql .= " GROUP BY year " ;
2020-10-31 14:32:18 +01:00
$sql .= $this -> db -> order ( 'year' , 'DESC' );
2010-07-17 22:25:56 +02:00
return $this -> _getAllByYear ( $sql );
}
2017-10-17 11:08:49 +02:00
2013-08-05 17:19:49 +02:00
/**
* Return nb , amount of predefined product for year
*
2020-03-06 12:45:02 +01:00
* @ param int $year Year to scan
2020-10-31 14:32:18 +01:00
* @ param int $limit Limit
2020-03-06 12:45:02 +01:00
* @ return array Array of values
2013-08-05 17:19:49 +02:00
*/
2020-03-06 12:45:02 +01:00
public function getAllByProduct ( $year , $limit = 10 )
2013-08-05 17:19:49 +02:00
{
global $user ;
$sql = " SELECT product.ref, COUNT(product.ref) as nb, SUM(tl. " . $this -> field_line . " ) as total, AVG(tl. " . $this -> field_line . " ) as avg " ;
2020-03-12 12:45:44 +01:00
$sql .= " FROM " . $this -> from . " , " . $this -> from_line . " , " . MAIN_DB_PREFIX . " product as product " ;
2021-10-22 22:15:59 +02:00
if ( empty ( $user -> rights -> societe -> client -> voir ) && ! $this -> socid ) {
2021-02-23 21:09:01 +01:00
$sql .= " , " . MAIN_DB_PREFIX . " societe_commerciaux as sc " ;
}
2020-10-31 14:32:18 +01:00
$sql .= $this -> join ;
2020-03-12 12:45:44 +01:00
$sql .= " WHERE " . $this -> where ;
$sql .= " AND f.rowid = tl.fk_facture AND tl.fk_product = product.rowid " ;
2020-10-31 14:32:18 +01:00
$sql .= " AND f.datef BETWEEN ' " . $this -> db -> idate ( dol_get_first_day ( $year , 1 , false )) . " ' AND ' " . $this -> db -> idate ( dol_get_last_day ( $year , 12 , false )) . " ' " ;
2020-03-12 12:45:44 +01:00
$sql .= " GROUP BY product.ref " ;
2020-10-31 14:32:18 +01:00
$sql .= $this -> db -> order ( 'nb' , 'DESC' );
//$sql.= $this->db->plimit(20);
2013-08-05 17:19:49 +02:00
2020-10-31 14:32:18 +01:00
return $this -> _getAllByProduct ( $sql , $limit );
2013-08-05 17:19:49 +02:00
}
2021-09-20 19:41:13 +02:00
/**
* Return the invoices amount by year for a number of past years
*
* @ param int $numberYears Years to scan
* @ param int $format 0 = Label of abscissa is a translated text , 1 = Label of abscissa is year , 2 = Label of abscissa is last number of year
* @ return array Array with amount by year
*/
public function getAmountByYear ( $numberYears , $format = 0 )
{
global $user ;
$endYear = date ( 'Y' );
$startYear = $endYear - $numberYears ;
$sql = " SELECT date_format(datef,'%Y') as dm, SUM(f. " . $this -> field . " ) " ;
$sql .= " FROM " . $this -> from ;
2021-10-22 22:15:59 +02:00
if ( empty ( $user -> rights -> societe -> client -> voir ) && ! $this -> socid ) {
2021-09-20 19:41:13 +02:00
$sql .= " , " . MAIN_DB_PREFIX . " societe_commerciaux as sc " ;
}
$sql .= $this -> join ;
$sql .= " WHERE f.datef BETWEEN ' " . $this -> db -> idate ( dol_get_first_day ( $startYear )) . " ' AND ' " . $this -> db -> idate ( dol_get_last_day ( $endYear )) . " ' " ;
$sql .= " AND " . $this -> where ;
$sql .= " GROUP BY dm " ;
$sql .= $this -> db -> order ( 'dm' , 'ASC' );
$res = $this -> _getAmountByYear ( $sql );
return $res ;
}
2010-07-17 22:25:56 +02:00
}