2016-01-25 13:31:43 +01:00
< ? php
/* Copyright ( C ) 2003 Rodolphe Quiedeville < rodolphe @ quiedeville . org >
* Copyright ( c ) 2005 - 2013 Laurent Destailleur < eldy @ users . sourceforge . net >
2018-10-27 14:43:12 +02:00
* Copyright ( C ) 2005 - 2012 Regis Houssin < regis . houssin @ inodbox . com >
2016-01-25 13:31:43 +01:00
* Copyright ( C ) 2012 Marcos García < marcosgdf @ gmail . com >
2024-08-19 02:05:27 +02:00
* Copyright ( C ) 2024 MDW < mdeweerd @ users . noreply . github . com >
2016-01-25 13:31:43 +01: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
* the Free Software Foundation ; either version 3 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
2019-09-23 21:55:30 +02:00
* along with this program . If not , see < https :// www . gnu . org / licenses />.
2016-01-25 13:31:43 +01:00
*/
/**
* \file htdocs / fichinter / class / fichinterstats . class . php
* \ingroup fichinter
* \brief File of class to manage intervention statistics
*/
2020-03-12 12:45:44 +01:00
include_once DOL_DOCUMENT_ROOT . '/core/class/stats.class.php' ;
include_once DOL_DOCUMENT_ROOT . '/fichinter/class/fichinter.class.php' ;
include_once DOL_DOCUMENT_ROOT . '/core/lib/date.lib.php' ;
2016-01-25 13:31:43 +01:00
/**
* Class to manage intervention statistics
*/
class FichinterStats extends Stats
{
2020-10-31 14:32:18 +01:00
/**
* @ var string Name of table without prefix where object is stored
*/
public $table_element ;
2024-11-06 20:15:15 +01:00
/**
* @ var int
*/
2020-10-31 14:32:18 +01:00
public $socid ;
2024-11-06 20:15:15 +01:00
/**
* @ var int
*/
2020-10-31 14:32:18 +01:00
public $userid ;
2024-11-06 20:15:15 +01:00
/**
* @ var string
*/
2020-10-31 14:32:18 +01:00
public $from ;
2024-11-06 20:15:15 +01:00
/**
* @ var string
*/
2020-10-31 14:32:18 +01:00
public $field ;
2024-11-06 20:15:15 +01:00
/**
* @ var string
*/
2020-10-31 14:32:18 +01:00
public $where ;
/**
* Constructor
*
* @ param DoliDB $db Database handler
* @ 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 .
* @ param string $mode Option ( 'customer' , 'supplier' )
* @ param int $userid Id user for filter ( creation user )
*/
public function __construct ( $db , $socid , $mode , $userid = 0 )
{
global $user , $conf ;
$this -> db = $db ;
$this -> socid = ( $socid > 0 ? $socid : 0 );
$this -> userid = $userid ;
$this -> cachefilesuffix = $mode ;
2021-02-25 23:00:32 +01:00
if ( $mode == 'customer' ) {
2020-10-31 14:32:18 +01:00
$object = new Fichinter ( $this -> db );
$this -> from = MAIN_DB_PREFIX . $object -> table_element . " as c " ;
$this -> from_line = MAIN_DB_PREFIX . $object -> table_element_line . " as tl " ;
$this -> field = '0' ;
$this -> field_line = '0' ;
//$this->where.= " AND c.fk_statut > 0"; // Not draft and not cancelled
}
2024-01-09 13:08:22 +01:00
if ( ! $user -> hasRight ( 'societe' , 'client' , 'voir' )) {
2023-06-27 14:48:32 +02:00
$this -> where .= ( ! empty ( $this -> where ) ? ' AND ' : '' ) . " c.fk_soc = sc.fk_soc AND sc.fk_user = " . (( int ) $user -> id );
2021-02-25 23:00:32 +01:00
}
2023-12-18 09:27:52 +01:00
$this -> where .= ( $this -> where ? ' AND ' : '' ) . " c.entity IN ( " . getEntity ( 'intervention' ) . ')' ;
2020-10-31 14:32:18 +01:00
2021-02-25 23:00:32 +01:00
if ( $this -> socid ) {
2021-03-30 11:36:50 +02:00
$this -> where .= " AND c.fk_soc = " . (( int ) $this -> socid );
2020-10-31 14:32:18 +01:00
}
2021-02-25 23:00:32 +01:00
if ( $this -> userid > 0 ) {
2021-03-30 11:36:50 +02:00
$this -> where .= ' AND c.fk_user_author = ' . (( int ) $this -> userid );
2021-02-25 23:00:32 +01:00
}
2020-10-31 14:32:18 +01:00
}
/**
* Return intervention number by month for a year
*
* @ param int $year Year to scan
* @ 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
2024-10-14 01:59:44 +02:00
* @ return array < int < 0 , 11 > , array { 0 : int < 1 , 12 > , 1 : int } > Array with number by month
2020-10-31 14:32:18 +01:00
*/
public function getNbByMonth ( $year , $format = 0 )
{
global $user ;
$sql = " SELECT date_format(c.date_valid,'%m') as dm, COUNT(*) as nb " ;
$sql .= " FROM " . $this -> from ;
2025-01-13 11:58:16 +01:00
if ( empty ( $user -> socid ) && ! $user -> hasRight ( 'societe' , 'client' , 'voir' )) {
2021-02-25 23:00:32 +01:00
$sql .= " , " . MAIN_DB_PREFIX . " societe_commerciaux as sc " ;
}
2020-10-31 14:32:18 +01:00
$sql .= " WHERE c.date_valid 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 " ;
$sql .= $this -> db -> order ( 'dm' , 'DESC' );
$res = $this -> _getNbByMonth ( $year , $sql , $format );
return $res ;
}
/**
* Return interventions number per year
*
2024-10-14 01:59:44 +02:00
* @ return array < array { 0 : int , 1 : int } > Array of nb each year
2020-10-31 14:32:18 +01:00
*
*/
public function getNbByYear ()
{
global $user ;
$sql = " SELECT date_format(c.date_valid,'%Y') as dm, COUNT(*) as nb, 0 " ;
$sql .= " FROM " . $this -> from ;
2025-01-13 11:58:16 +01:00
if ( empty ( $user -> socid ) && ! $user -> hasRight ( 'societe' , 'client' , 'voir' )) {
2021-02-25 23:00:32 +01:00
$sql .= " , " . MAIN_DB_PREFIX . " societe_commerciaux as sc " ;
}
2020-10-31 14:32:18 +01:00
$sql .= " WHERE " . $this -> where ;
$sql .= " GROUP BY dm " ;
$sql .= $this -> db -> order ( 'dm' , 'DESC' );
return $this -> _getNbByYear ( $sql );
}
/**
* Return the intervention amount by month for a year
*
* @ param int $year Year to scan
* @ 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
2024-10-14 01:59:44 +02:00
* @ return array < int < 0 , 11 > , array { 0 : int < 1 , 12 > , 1 : int | float } > Array with amount by month
2020-10-31 14:32:18 +01:00
*/
public function getAmountByMonth ( $year , $format = 0 )
{
global $user ;
$sql = " SELECT date_format(c.date_valid,'%m') as dm, 0 " ;
$sql .= " FROM " . $this -> from ;
2025-01-13 11:58:16 +01:00
if ( empty ( $user -> socid ) && ! $user -> hasRight ( 'societe' , 'client' , 'voir' )) {
2021-02-25 23:00:32 +01:00
$sql .= " , " . MAIN_DB_PREFIX . " societe_commerciaux as sc " ;
}
2020-10-31 14:32:18 +01:00
$sql .= " WHERE c.date_valid 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 " ;
$sql .= $this -> db -> order ( 'dm' , 'DESC' );
$res = $this -> _getAmountByMonth ( $year , $sql , $format );
return $res ;
}
/**
* Return the intervention amount average by month for a year
*
* @ param int $year year for stats
2024-10-14 01:59:44 +02:00
* @ return array < int < 0 , 11 > , array { 0 : int < 1 , 12 > , 1 : int | float } > Array with number by month
2020-10-31 14:32:18 +01:00
*/
public function getAverageByMonth ( $year )
{
global $user ;
$sql = " SELECT date_format(c.date_valid,'%m') as dm, 0 " ;
$sql .= " FROM " . $this -> from ;
2025-01-13 11:58:16 +01:00
if ( empty ( $user -> socid ) && ! $user -> hasRight ( 'societe' , 'client' , 'voir' )) {
2021-02-25 23:00:32 +01:00
$sql .= " , " . MAIN_DB_PREFIX . " societe_commerciaux as sc " ;
}
2020-10-31 14:32:18 +01:00
$sql .= " WHERE c.date_valid 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 " ;
$sql .= $this -> db -> order ( 'dm' , 'DESC' );
return $this -> _getAverageByMonth ( $year , $sql );
}
/**
* Return nb , total and average
*
2024-10-14 01:59:44 +02:00
* @ return array < array { year : string , nb : string , nb_diff : float , total ? : float , avg ? : float , weighted ? : float , total_diff ? : float , avg_diff ? : float , avg_weighted ? : float } > Array of values
2020-10-31 14:32:18 +01:00
*/
public function getAllByYear ()
{
global $user ;
$sql = " SELECT date_format(c.date_valid,'%Y') as year, COUNT(*) as nb, 0 as total, 0 as avg " ;
$sql .= " FROM " . $this -> from ;
2025-01-13 11:58:16 +01:00
if ( empty ( $user -> socid ) && ! $user -> hasRight ( 'societe' , 'client' , 'voir' )) {
2021-02-25 23:00:32 +01:00
$sql .= " , " . MAIN_DB_PREFIX . " societe_commerciaux as sc " ;
}
2020-10-31 14:32:18 +01:00
$sql .= " WHERE " . $this -> where ;
$sql .= " GROUP BY year " ;
$sql .= $this -> db -> order ( 'year' , 'DESC' );
return $this -> _getAllByYear ( $sql );
}
/**
2024-08-19 02:05:27 +02:00
* Return nb , amount of predefined product for year
2020-10-31 14:32:18 +01:00
*
2024-08-19 02:05:27 +02:00
* @ param int $year Year to scan
* @ param int $limit Limit
* @ return array < int < 0 , 11 > , array { 0 : int < 1 , 12 > , 1 : int | float } > Array of values
2020-10-31 14:32:18 +01:00
*/
public function getAllByProduct ( $year , $limit = 0 )
{
global $user ;
$sql = " SELECT product.ref, COUNT(product.ref) as nb, 0 as total, 0 as avg " ;
$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) && !$user->socid) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
2020-10-31 14:32:18 +01:00
$sql .= " WHERE " . $this -> where ;
$sql .= " AND c.rowid = tl.fk_fichinter AND tl.fk_product = product.rowid " ;
$sql .= " AND c.date_valid BETWEEN ' " . $this -> db -> idate ( dol_get_first_day ( $year , 1 , false )) . " ' AND ' " . $this -> db -> idate ( dol_get_last_day ( $year , 12 , false )) . " ' " ;
$sql .= " GROUP BY product.ref " ;
$sql .= $this -> db -> order ( 'nb' , 'DESC' );
//$sql.= $this->db->plimit(20);
return $this -> _getAllByProduct ( $sql , $limit );
}
2016-01-25 13:31:43 +01:00
}