1<?php
2/* Copyright (C) 2003      Rodolphe Quiedeville <rodolphe@quiedeville.org>
3 * Copyright (c) 2005-2011 Laurent Destailleur  <eldy@users.sourceforge.net>
4 * Copyright (C) 2005-2009 Regis Houssin        <regis.houssin@inodbox.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20/**
21 *	\file       htdocs/adherents/class/adherentstats.class.php
22 *	\ingroup    member
23 *	\brief      Fichier de la classe de gestion des stats des adhérents
24 */
25
26include_once DOL_DOCUMENT_ROOT.'/core/class/stats.class.php';
27include_once DOL_DOCUMENT_ROOT.'/adherents/class/subscription.class.php';
28
29
30/**
31 *	Class to manage statistics of members
32 */
33class AdherentStats extends Stats
34{
35	/**
36	 * @var string Name of table without prefix where object is stored
37	 */
38	public $table_element;
39
40	public $memberid;
41	public $socid;
42	public $userid;
43
44	public $from;
45	public $field;
46	public $where;
47
48
49	/**
50	 *	Constructor
51	 *
52	 *	@param 		DoliDB		$db			Database handler
53	 * 	@param 		int			$socid	   	Id third party
54	 * 	@param   	int			$userid    	Id user for filter
55	 */
56	public function __construct($db, $socid = 0, $userid = 0)
57	{
58		global $user, $conf;
59
60		$this->db = $db;
61		$this->socid = $socid;
62		$this->userid = $userid;
63
64		$object = new Subscription($this->db);
65
66		$this->from = MAIN_DB_PREFIX.$object->table_element." as p";
67		$this->from .= ", ".MAIN_DB_PREFIX."adherent as m";
68
69		$this->field = 'subscription';
70
71		$this->where .= " m.statut != -1";
72		$this->where .= " AND p.fk_adherent = m.rowid AND m.entity IN (".getEntity('adherent').")";
73		//if (!$user->rights->societe->client->voir && !$user->socid) $this->where .= " AND p.fk_soc = sc.fk_soc AND sc.fk_user = " .((int) $user->id);
74		if ($this->memberid) {
75			$this->where .= " AND m.rowid = ".((int) $this->memberid);
76		}
77		//if ($this->userid > 0) $this->where.=' AND fk_user_author = '.$this->userid;
78	}
79
80
81	/**
82	 * Return the number of proposition by month for a given year
83	 *
84	 * @param   int		$year       Year
85	 *	@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
86	 * @return	array				Array of nb each month
87	 */
88	public function getNbByMonth($year, $format = 0)
89	{
90		global $user;
91
92		$sql = "SELECT date_format(p.dateadh,'%m') as dm, count(*)";
93		$sql .= " FROM ".$this->from;
94		//if (!$user->rights->societe->client->voir && !$user->socid) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
95		$sql .= " WHERE ".dolSqlDateFilter('p.dateadh', 0, 0, (int) $year, 1);
96		$sql .= " AND ".$this->where;
97		$sql .= " GROUP BY dm";
98		$sql .= $this->db->order('dm', 'DESC');
99
100		return $this->_getNbByMonth($year, $sql, $format);
101	}
102
103	/**
104	 * Return the number of subscriptions by year
105	 *
106	 * @return	array				Array of nb each year
107	 */
108	public function getNbByYear()
109	{
110		global $user;
111
112		$sql = "SELECT date_format(p.dateadh,'%Y') as dm, count(*)";
113		$sql .= " FROM ".$this->from;
114		//if (!$user->rights->societe->client->voir && !$user->socid) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
115		$sql .= " WHERE ".$this->where;
116		$sql .= " GROUP BY dm";
117		$sql .= $this->db->order('dm', 'DESC');
118
119		return $this->_getNbByYear($sql);
120	}
121
122	/**
123	 * Return the number of subscriptions by month for a given year
124	 *
125	 * @param   int		$year       Year
126	 * @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
127	 * @return	array				Array of amount each month
128	 */
129	public function getAmountByMonth($year, $format = 0)
130	{
131		global $user;
132
133		$sql = "SELECT date_format(p.dateadh,'%m') as dm, sum(p.".$this->field.")";
134		$sql .= " FROM ".$this->from;
135		//if (!$user->rights->societe->client->voir && !$user->socid) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
136		$sql .= " WHERE ".dolSqlDateFilter('p.dateadh', 0, 0, (int) $year, 1);
137		$sql .= " AND ".$this->where;
138		$sql .= " GROUP BY dm";
139		$sql .= $this->db->order('dm', 'DESC');
140
141		return $this->_getAmountByMonth($year, $sql, $format);
142	}
143
144	/**
145	 * Return average amount each month
146	 *
147	 * @param   int		$year       Year
148	 * @return	array				Array of average each month
149	 */
150	public function getAverageByMonth($year)
151	{
152		global $user;
153
154		$sql = "SELECT date_format(p.dateadh,'%m') as dm, avg(p.".$this->field.")";
155		$sql .= " FROM ".$this->from;
156		//if (!$user->rights->societe->client->voir && !$this->socid) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
157		$sql .= " WHERE ".dolSqlDateFilter('p.dateadh', 0, 0, (int) $year, 1);
158		$sql .= " AND ".$this->where;
159		$sql .= " GROUP BY dm";
160		$sql .= $this->db->order('dm', 'DESC');
161
162		return $this->_getAverageByMonth($year, $sql);
163	}
164
165
166	/**
167	 *	Return nb, total and average
168	 *
169	 * 	@return		array					Array with nb, total amount, average for each year
170	 */
171	public function getAllByYear()
172	{
173		global $user;
174
175		$sql = "SELECT date_format(p.dateadh,'%Y') as year, count(*) as nb, sum(".$this->field.") as total, avg(".$this->field.") as avg";
176		$sql .= " FROM ".$this->from;
177		//if (!$user->rights->societe->client->voir && !$this->socid) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
178		$sql .= " WHERE ".$this->where;
179		$sql .= " GROUP BY year";
180		$sql .= $this->db->order('year', 'DESC');
181
182		return $this->_getAllByYear($sql);
183	}
184}
185