2015-03-21 06:21:34 +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 - 2009 Regis Houssin < regis . houssin @ inodbox . com >
2015-03-21 06:21:34 +01:00
* Copyright ( C ) 2011 Juanjo Menent < jmenent @ 2 byte . es >
2025-01-18 23:15:39 +01:00
* Copyright ( C ) 2024 - 2025 MDW < mdeweerd @ users . noreply . github . com >
2015-03-21 06:21:34 +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 />.
2015-03-21 06:21:34 +01:00
*/
/**
2019-10-22 17:10:52 +02:00
* \file htdocs / don / class / donstats . class . php
2015-03-21 06:21:34 +01:00
* \ingroup donations
* \brief File of class to manage donations statistics
*/
2020-04-10 10:59:32 +02:00
include_once DOL_DOCUMENT_ROOT . '/core/class/stats.class.php' ;
include_once DOL_DOCUMENT_ROOT . '/don/class/don.class.php' ;
include_once DOL_DOCUMENT_ROOT . '/core/lib/date.lib.php' ;
2015-03-21 06:21:34 +01:00
/**
* Class to manage donations statistics
*/
class DonationStats extends Stats
{
2018-08-22 18:48:53 +02:00
/**
* @ var string Name of table without prefix where object is stored
*/
2015-03-21 06:21:34 +01:00
public $table_element ;
2024-10-14 01:59:44 +02:00
/**
* @ var int
*/
2019-10-22 17:10:52 +02:00
public $socid ;
2024-10-14 01:59:44 +02:00
/**
* @ var int
*/
2020-10-31 14:32:18 +01:00
public $userid ;
2019-10-22 17:12:42 +02:00
2020-10-31 14:32:18 +01:00
/**
* @ var string FROM
*/
public $from ;
2019-10-22 17:12:42 +02:00
2020-10-31 14:32:18 +01:00
/**
* @ var string field
*/
2019-02-25 20:35:59 +01:00
public $field ;
2019-10-22 17:12:42 +02:00
2019-10-22 17:10:52 +02:00
/**
* @ var string WHERE
*/
2020-10-31 14:32:18 +01:00
public $where ;
2017-12-08 16:15:41 +01:00
2024-03-01 21:17:52 +01:00
/**
* @ var string JOIN
*/
public $join ;
2015-03-21 06:21:34 +01:00
2020-10-31 14:32:18 +01:00
/**
* Constructor
*
* @ param DoliDB $db Database handler
2015-03-21 06:21:34 +01:00
* @ param int $socid Id third party for filter
* @ param string $mode Option ( not used )
* @ param int $userid Id user for filter ( creation user )
2024-03-01 21:17:52 +01:00
* @ param int $typentid Id of type of third party for filter
* @ param int $status Status of donation for filter
2020-10-31 14:32:18 +01:00
*/
2024-03-01 21:17:52 +01:00
public function __construct ( $db , $socid , $mode , $userid = 0 , $typentid = 0 , $status = 4 )
2020-10-31 14:32:18 +01:00
{
2022-12-27 21:02:21 +01:00
global $conf ;
2017-12-08 16:15:41 +01:00
2015-03-21 06:21:34 +01:00
$this -> db = $db ;
2017-12-08 16:15:41 +01:00
2022-12-27 21:02:21 +01:00
$this -> field = 'amount' ;
2015-03-21 06:21:34 +01:00
$this -> socid = ( $socid > 0 ? $socid : 0 );
2020-10-31 14:32:18 +01:00
$this -> userid = $userid ;
2017-12-08 16:15:41 +01:00
$this -> cachefilesuffix = $mode ;
2024-03-01 21:17:52 +01:00
$this -> join = '' ;
if ( $status == 0 || $status == 1 || $status == 2 ) {
2025-02-05 14:13:16 +01:00
$this -> where = " d.fk_statut = " . (( int ) $status );
2024-03-01 21:17:52 +01:00
} elseif ( $status == 3 ) {
2025-02-05 14:11:32 +01:00
$this -> where = " d.fk_statut IN (-1) " ;
2024-03-01 21:17:52 +01:00
} elseif ( $status == 4 ) {
2025-02-05 14:11:32 +01:00
$this -> where = " d.fk_statut >= 0 " ;
2024-03-01 21:17:52 +01:00
}
2017-12-08 16:15:41 +01:00
2020-10-31 14:32:18 +01:00
$object = new Don ( $this -> db );
2015-03-23 06:56:12 +01:00
$this -> from = MAIN_DB_PREFIX . $object -> table_element . " as d " ;
2015-03-21 06:21:34 +01:00
2024-03-01 21:17:52 +01:00
if ( $socid !== " -1 " && $socid > 0 ) {
$this -> where .= " AND d.fk_soc = " . (( int ) $socid );
}
2025-02-05 14:11:32 +01:00
$this -> where .= " AND d.entity = " . (( int ) $conf -> entity );
2021-02-25 22:18:34 +01:00
if ( $this -> userid > 0 ) {
2024-03-01 21:17:52 +01:00
$this -> where .= ' AND d.fk_user_author = ' . (( int ) $this -> userid );
}
if ( $typentid ) {
2025-02-05 14:11:32 +01:00
$this -> join .= " LEFT JOIN '.MAIN_DB_PREFIX.'societe as s ON s.rowid = d.fk_soc " ;
$this -> where .= " AND s.fk_typent = " . (( int ) $typentid );
2021-02-25 22:18:34 +01:00
}
2020-10-31 14:32:18 +01:00
}
2015-03-21 06:21:34 +01:00
2020-10-31 14:32:18 +01:00
/**
* Return shipment number by month for a year
*
2019-02-25 20:35:59 +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
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 )
{
$sql = " SELECT date_format(d.datedon,'%m') as dm, COUNT(*) as nb " ;
2020-04-10 10:59:32 +02:00
$sql .= " FROM " . $this -> from ;
2024-03-01 21:17:52 +01:00
$sql .= $this -> join ;
2020-04-10 10:59:32 +02:00
$sql .= " WHERE d.datedon 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' );
2015-03-21 06:21:34 +01:00
2024-03-01 21:17:52 +01:00
return $this -> _getNbByMonth ( $year , $sql , $format );
2020-10-31 14:32:18 +01:00
}
2015-03-21 06:21:34 +01:00
/**
* Return shipments number per year
*
2024-10-14 01:59:44 +02:00
* @ return array < array { 0 : int , 1 : int } > Array of nb each year
2015-03-21 06:21:34 +01:00
*/
2019-02-25 20:35:59 +01:00
public function getNbByYear ()
2015-03-21 06:21:34 +01:00
{
2025-02-05 14:11:32 +01:00
$sql = " SELECT date_format(d.datedon,'%Y') as dm, COUNT(*) as nb, SUM(d. " . $this -> db -> sanitize ( $this -> field ) . " ) " ;
2020-04-10 10:59:32 +02:00
$sql .= " FROM " . $this -> from ;
2024-03-01 21:17:52 +01:00
$sql .= $this -> join ;
2020-04-10 10:59:32 +02:00
$sql .= " WHERE " . $this -> where ;
$sql .= " GROUP BY dm " ;
2020-10-31 14:32:18 +01:00
$sql .= $this -> db -> order ( 'dm' , 'DESC' );
2015-03-21 06:21:34 +01:00
return $this -> _getNbByYear ( $sql );
}
2022-12-27 21:02:21 +01:00
/**
* Return the number of subscriptions by month for a given year
*
* @ param int $year Year
* @ 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 of amount each month
2022-12-27 21:02:21 +01:00
*/
public function getAmountByMonth ( $year , $format = 0 )
{
2025-02-05 14:11:32 +01:00
$sql = " SELECT date_format(d.datedon,'%m') as dm, sum(d. " . $this -> db -> sanitize ( $this -> field ) . " ) " ;
2022-12-27 21:02:21 +01:00
$sql .= " FROM " . $this -> from ;
2024-03-01 21:17:52 +01:00
$sql .= $this -> join ;
2022-12-27 21:02:21 +01:00
$sql .= " WHERE " . dolSqlDateFilter ( 'd.datedon' , 0 , 0 , ( int ) $year , 1 );
$sql .= " AND " . $this -> where ;
$sql .= " GROUP BY dm " ;
$sql .= $this -> db -> order ( 'dm' , 'DESC' );
return $this -> _getAmountByMonth ( $year , $sql , $format );
}
/**
* Return average amount each month
*
* @ param int $year Year
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
2022-12-27 21:02:21 +01:00
*/
public function getAverageByMonth ( $year )
{
2025-02-05 14:11:32 +01:00
$sql = " SELECT date_format(d.datedon,'%m') as dm, avg(d. " . $this -> db -> sanitize ( $this -> field ) . " ) " ;
2022-12-27 21:02:21 +01:00
$sql .= " FROM " . $this -> from ;
2024-03-01 21:17:52 +01:00
$sql .= $this -> join ;
2022-12-27 21:02:21 +01:00
$sql .= " WHERE " . dolSqlDateFilter ( 'd.datedon' , 0 , 0 , ( int ) $year , 1 );
$sql .= " AND " . $this -> where ;
$sql .= " GROUP BY dm " ;
$sql .= $this -> db -> order ( 'dm' , 'DESC' );
return $this -> _getAverageByMonth ( $year , $sql );
}
2020-10-31 14:32:18 +01:00
/**
* 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 ()
{
2025-02-05 14:11:32 +01:00
$sql = " SELECT date_format(d.datedon,'%Y') as year, COUNT(*) as nb, SUM(d. " . $this -> db -> sanitize ( $this -> field ) . " ) as total, AVG( " . $this -> field . " ) as avg " ;
2020-10-31 14:32:18 +01:00
$sql .= " FROM " . $this -> from ;
2024-03-01 21:17:52 +01:00
$sql .= $this -> join ;
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 );
}
2018-08-13 17:26:32 +02:00
}