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