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 >
2012-12-30 15:13:49 +01:00
* Copyright ( C ) 2005 - 2009 Regis Houssin < regis . houssin @ capnetworks . com >
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
2011-08-03 02:45:22 +02:00
* along with this program . If not , see < http :// 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
*/
2012-08-23 02:04:35 +02: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
{
2011-06-13 15:42:51 +02:00
var $socid ;
var $userid ;
2011-09-20 19:19:46 +02:00
public $table_element ;
2011-06-13 15:42:51 +02:00
var $from ;
var $field ;
var $where ;
2013-07-16 18:42:43 +02:00
2010-07-17 22:25:56 +02:00
/**
2011-09-08 22:02:57 +02:00
* Constructor
*
2012-01-10 09:57:16 +01:00
* @ param DoliDB $db Database handler
2013-07-18 15:25:25 +02:00
* @ param int $socid Id third party for filter
2013-06-28 19:19:18 +02:00
* @ param string $mode Option ( 'customer' , 'supplier' )
2013-05-20 19:24:27 +02:00
* @ param int $userid Id user for filter ( creation user )
2010-07-17 22:25:56 +02:00
*/
2012-07-30 17:17:33 +02:00
function __construct ( $db , $socid , $mode , $userid = 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 ;
2013-03-11 14:51:19 +01:00
$this -> socid = ( $socid > 0 ? $socid : 0 );
2011-06-13 15:42:51 +02:00
$this -> userid = $userid ;
2013-07-16 18:42:43 +02:00
$this -> cachefilesuffix = $mode ;
2010-07-17 22:25:56 +02:00
if ( $mode == 'customer' )
{
$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 " ;
2010-07-17 22:25:56 +02:00
$this -> field = 'total' ;
2013-08-05 17:19:49 +02:00
$this -> field_line = 'total_ht' ;
2010-07-17 22:25:56 +02:00
}
if ( $mode == 'supplier' )
{
$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 " ;
2010-07-17 22:25:56 +02:00
$this -> field = 'total_ht' ;
2013-08-05 17:19:49 +02:00
$this -> field_line = 'total_ht' ;
2010-07-17 22:25:56 +02:00
}
2013-05-20 19:24:27 +02:00
$this -> where = " f.fk_statut > 0 " ;
$this -> where .= " AND f.entity = " . $conf -> entity ;
2013-07-18 15:25:25 +02:00
if ( ! $user -> rights -> societe -> client -> voir && ! $this -> socid ) $this -> where .= " AND f.fk_soc = sc.fk_soc AND sc.fk_user = " . $user -> id ;
2013-05-20 19:24:27 +02: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)
2010-07-17 22:25:56 +02:00
if ( $this -> socid )
{
2013-05-20 19:24:27 +02:00
$this -> where .= " AND f.fk_soc = " . $this -> socid ;
2010-07-17 22:25:56 +02:00
}
2013-05-20 19:24:27 +02:00
if ( $this -> userid > 0 ) $this -> where .= ' AND f.fk_user_author = ' . $this -> userid ;
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
* @ return array Array of values
2010-07-17 22:25:56 +02:00
*/
2013-05-20 19:24:27 +02:00
function getNbByMonth ( $year )
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 " ;
2010-07-17 22:25:56 +02:00
$sql .= " FROM " . $this -> from ;
2013-05-20 19:24:27 +02:00
if ( ! $user -> rights -> societe -> client -> voir && ! $this -> socid ) $sql .= " , " . MAIN_DB_PREFIX . " societe_commerciaux as sc " ;
$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 ;
2010-08-29 22:34:20 +02:00
$sql .= " GROUP BY dm " ;
2011-03-09 11:47:28 +01:00
$sql .= $this -> db -> order ( 'dm' , 'DESC' );
2010-07-17 22:25:56 +02:00
2013-05-20 19:24:27 +02:00
$res = $this -> _getNbByMonth ( $year , $sql );
//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
*/
2013-05-20 19:24:27 +02:00
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 . " ) " ;
2010-07-17 22:25:56 +02:00
$sql .= " FROM " . $this -> from ;
2013-05-20 19:24:27 +02:00
if ( ! $user -> rights -> societe -> client -> voir && ! $this -> socid ) $sql .= " , " . MAIN_DB_PREFIX . " societe_commerciaux as sc " ;
$sql .= " WHERE " . $this -> where ;
2010-08-29 22:34:20 +02:00
$sql .= " GROUP BY dm " ;
2011-03-09 11:47:28 +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
*
* @ param int $year Year to scan
2013-07-18 15:25:25 +02:00
* @ return array Array with amount by month
2010-07-17 22:25:56 +02:00
*/
function getAmountByMonth ( $year )
{
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 . " ) " ;
2010-07-17 22:25:56 +02:00
$sql .= " FROM " . $this -> from ;
2013-07-18 15:25:25 +02:00
if ( ! $user -> rights -> societe -> client -> voir && ! $this -> socid ) $sql .= " , " . MAIN_DB_PREFIX . " societe_commerciaux as sc " ;
2013-05-20 19:24:27 +02:00
$sql .= " WHERE f.datef BETWEEN ' " . $this -> db -> idate ( dol_get_first_day ( $year )) . " ' AND ' " . $this -> db -> idate ( dol_get_last_day ( $year )) . " ' " ;
2010-07-17 22:25:56 +02:00
$sql .= " AND " . $this -> where ;
2010-08-29 22:34:20 +02:00
$sql .= " GROUP BY dm " ;
2011-03-09 11:47:28 +01:00
$sql .= $this -> db -> order ( 'dm' , 'DESC' );
2010-07-17 22:25:56 +02:00
$res = $this -> _getAmountByMonth ( $year , $sql );
//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
*/
function getAverageByMonth ( $year )
{
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 . " ) " ;
2010-07-17 22:25:56 +02:00
$sql .= " FROM " . $this -> from ;
2013-05-20 19:24:27 +02:00
if ( ! $user -> rights -> societe -> client -> voir && ! $user -> societe_id ) $sql .= " , " . MAIN_DB_PREFIX . " societe_commerciaux as sc " ;
$sql .= " WHERE f.datef BETWEEN ' " . $this -> db -> idate ( dol_get_first_day ( $year )) . " ' AND ' " . $this -> db -> idate ( dol_get_last_day ( $year )) . " ' " ;
2010-07-17 22:25:56 +02:00
$sql .= " AND " . $this -> where ;
2010-08-29 22:34:20 +02:00
$sql .= " GROUP BY dm " ;
2011-03-09 11:47:28 +01:00
$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
*/
function getAllByYear ()
{
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 " ;
2010-07-17 22:25:56 +02:00
$sql .= " FROM " . $this -> from ;
2013-05-20 19:24:27 +02:00
if ( ! $user -> rights -> societe -> client -> voir && ! $user -> societe_id ) $sql .= " , " . MAIN_DB_PREFIX . " societe_commerciaux as sc " ;
2010-07-17 22:25:56 +02:00
$sql .= " WHERE " . $this -> where ;
2010-08-29 22:34:20 +02:00
$sql .= " GROUP BY year " ;
2011-03-09 11:47:28 +01:00
$sql .= $this -> db -> order ( 'year' , 'DESC' );
2010-07-17 22:25:56 +02:00
return $this -> _getAllByYear ( $sql );
}
2013-08-05 17:19:49 +02:00
/**
* Return nb , amount of predefined product for year
*
* @ param int $year Year to scan
* @ return array Array of values
*/
function getAllByProduct ( $year )
{
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 " ;
$sql .= " FROM " . $this -> from . " , " . $this -> from_line . " , " . MAIN_DB_PREFIX . " product as product " ;
//if (!$user->rights->societe->client->voir && !$user->societe_id) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
$sql .= " WHERE " . $this -> where ;
$sql .= " AND f.rowid = tl.fk_facture AND tl.fk_product = product.rowid " ;
2013-08-09 19:19:48 +02: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 )) . " ' " ;
2013-08-05 17:19:49 +02:00
$sql .= " GROUP BY product.ref " ;
$sql .= $this -> db -> order ( 'nb' , 'DESC' );
2013-08-27 11:54:07 +02:00
//$sql.= $this->db->plimit(20);
2013-08-05 17:19:49 +02:00
return $this -> _getAllByProduct ( $sql );
}
2010-07-17 22:25:56 +02:00
}
?>