dolibarr 21.0.0-alpha
facturestats.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3 * Copyright (c) 2005-2013 Laurent Destailleur <eldy@users.sourceforge.net>
4 * Copyright (C) 2005-2009 Regis Houssin <regis.houssin@inodbox.com>
5 * Copyright (C) 2020 Maxime DEMAREST <maxime@indelog.fr>
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
26include_once DOL_DOCUMENT_ROOT.'/core/class/stats.class.php';
27include_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
28include_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
29include_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
30
34class FactureStats extends Stats
35{
36 public $socid;
37 public $userid;
38
42 public $table_element;
43
44 public $from;
45 public $field;
46 public $where = '';
47 public $join;
48
49
60 public function __construct(DoliDB $db, $socid, $mode, $userid = 0, $typentid = 0, $categid = 0)
61 {
62 global $user, $conf;
63
64 $this->db = $db;
65 $this->socid = ($socid > 0 ? $socid : 0);
66 $this->userid = $userid;
67 $this->cachefilesuffix = $mode;
68 $this->join = '';
69
70 if ($mode == 'customer') {
71 $object = new Facture($this->db);
72 $this->from = MAIN_DB_PREFIX.$object->table_element." as f";
73 $this->from_line = MAIN_DB_PREFIX.$object->table_element_line." as tl";
74 $this->field = 'total_ht';
75 $this->field_line = 'total_ht';
76 }
77 if ($mode == 'supplier') {
78 $object = new FactureFournisseur($this->db);
79 $this->from = MAIN_DB_PREFIX.$object->table_element." as f";
80 $this->from_line = MAIN_DB_PREFIX.$object->table_element_line." as tl";
81 $this->field = 'total_ht';
82 $this->field_line = 'total_ht';
83 }
84
85 $this->where .= ($this->where ? ' AND ' : '')." f.fk_statut >= 0";
86 $this->where .= " AND f.entity IN (".getEntity('invoice').")";
87 if (!$user->hasRight('societe', 'client', 'voir')) {
88 $this->where .= " AND f.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
89 }
90 if ($mode == 'customer') {
91 $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)
92 }
93 if ($this->socid) {
94 $this->where .= " AND f.fk_soc = ".((int) $this->socid);
95 }
96 if ($this->userid > 0) {
97 $this->where .= ' AND f.fk_user_author = '.((int) $this->userid);
98 }
99 if ($mode == 'customer') {
100 if (getDolGlobalString('FACTURE_DEPOSITS_ARE_JUST_PAYMENTS')) {
101 $this->where .= " AND f.type IN (0,1,2,5)";
102 } else {
103 $this->where .= " AND f.type IN (0,1,2,3,5)";
104 }
105 }
106 if ($mode == 'supplier') {
107 if (getDolGlobalString('FACTURE_SUPPLIER_DEPOSITS_ARE_JUST_PAYMENTS')) {
108 $this->where .= " AND f.type IN (0,1,2,5)";
109 } else {
110 $this->where .= " AND f.type IN (0,1,2,3,5)";
111 }
112 }
113
114 if ($typentid) {
115 $this->join .= ' LEFT JOIN '.MAIN_DB_PREFIX.'societe as s ON s.rowid = f.fk_soc';
116 $this->where .= ' AND s.fk_typent = '.((int) $typentid);
117 }
118
119 if ($categid) {
120 $this->where .= ' AND EXISTS (SELECT rowid FROM '.MAIN_DB_PREFIX.'categorie_societe as cats WHERE cats.fk_soc = f.fk_soc AND cats.fk_categorie = '.((int) $categid).')';
121 }
122 }
123
124
132 public function getNbByMonth($year, $format = 0)
133 {
134 global $user;
135
136 $sql = "SELECT date_format(f.datef,'%m') as dm, COUNT(*) as nb";
137 $sql .= " FROM ".$this->from;
138 if (!$user->hasRight('societe', 'client', 'voir')) {
139 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
140 }
141 $sql .= $this->join;
142 $sql .= " WHERE f.datef BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
143 $sql .= " AND ".$this->where;
144 $sql .= " GROUP BY dm";
145 $sql .= $this->db->order('dm', 'DESC');
146
147 $res = $this->_getNbByMonth($year, $sql, $format);
148 //var_dump($res);print '<br>';
149 return $res;
150 }
151
152
158 public function getNbByYear()
159 {
160 global $user;
161
162 $sql = "SELECT date_format(f.datef,'%Y') as dm, COUNT(*), SUM(c.".$this->field.")";
163 $sql .= " FROM ".$this->from;
164 if (!$user->hasRight('societe', 'client', 'voir')) {
165 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
166 }
167 $sql .= $this->join;
168 $sql .= " WHERE ".$this->where;
169 $sql .= " GROUP BY dm";
170 $sql .= $this->db->order('dm', 'DESC');
171
172 return $this->_getNbByYear($sql);
173 }
174
175
183 public function getAmountByMonth($year, $format = 0)
184 {
185 global $user;
186
187 $sql = "SELECT date_format(datef,'%m') as dm, SUM(f.".$this->field.")";
188 $sql .= " FROM ".$this->from;
189 if (!$user->hasRight('societe', 'client', 'voir')) {
190 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
191 }
192 $sql .= $this->join;
193 $sql .= " WHERE f.datef BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
194 $sql .= " AND ".$this->where;
195 $sql .= " GROUP BY dm";
196 $sql .= $this->db->order('dm', 'DESC');
197
198 $res = $this->_getAmountByMonth($year, $sql, $format);
199 //var_dump($res);print '<br>';
200 return $res;
201 }
202
209 public function getAverageByMonth($year)
210 {
211 global $user;
212
213 $sql = "SELECT date_format(datef,'%m') as dm, AVG(f.".$this->field.")";
214 $sql .= " FROM ".$this->from;
215 if (!$user->hasRight('societe', 'client', 'voir')) {
216 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
217 }
218 $sql .= $this->join;
219 $sql .= " WHERE f.datef BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
220 $sql .= " AND ".$this->where;
221 $sql .= " GROUP BY dm";
222 $sql .= $this->db->order('dm', 'DESC');
223
224 return $this->_getAverageByMonth($year, $sql);
225 }
226
232 public function getAllByYear()
233 {
234 global $user;
235
236 $sql = "SELECT date_format(datef,'%Y') as year, COUNT(*) as nb, SUM(f.".$this->field.") as total, AVG(f.".$this->field.") as avg";
237 $sql .= " FROM ".$this->from;
238 if (!$user->hasRight('societe', 'client', 'voir')) {
239 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
240 }
241 $sql .= $this->join;
242 $sql .= " WHERE ".$this->where;
243 $sql .= " GROUP BY year";
244 $sql .= $this->db->order('year', 'DESC');
245
246 return $this->_getAllByYear($sql);
247 }
248
256 public function getAllByProduct($year, $limit = 10)
257 {
258 global $user;
259
260 $sql = "SELECT product.ref, COUNT(product.ref) as nb, SUM(tl.".$this->field_line.") as total, AVG(tl.".$this->field_line.") as avg";
261 $sql .= " FROM ".$this->from.", ".$this->from_line.", ".MAIN_DB_PREFIX."product as product";
262 if (!$user->hasRight('societe', 'client', 'voir')) {
263 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
264 }
265 $sql .= $this->join;
266 $sql .= " WHERE ".$this->where;
267 $sql .= " AND f.rowid = tl.fk_facture AND tl.fk_product = product.rowid";
268 $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))."'";
269 $sql .= " GROUP BY product.ref";
270 $sql .= $this->db->order('nb', 'DESC');
271 //$sql.= $this->db->plimit(20);
272
273 return $this->_getAllByProduct($sql, $limit);
274 }
282 public function getAmountByYear($numberYears, $format = 0)
283 {
284 global $user;
285
286 $endYear = date('Y');
287 $startYear = $endYear - $numberYears;
288 $sql = "SELECT date_format(datef,'%Y') as dm, SUM(f.".$this->field.")";
289 $sql .= " FROM ".$this->from;
290 if (!$user->hasRight('societe', 'client', 'voir')) {
291 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
292 }
293 $sql .= $this->join;
294 $sql .= " WHERE f.datef BETWEEN '".$this->db->idate(dol_get_first_day($startYear))."' AND '".$this->db->idate(dol_get_last_day($endYear))."'";
295 $sql .= " AND ".$this->where;
296 $sql .= " GROUP BY dm";
297 $sql .= $this->db->order('dm', 'ASC');
298
299 $res = $this->_getAmountByYear($sql);
300 return $res;
301 }
302}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
Class to manage Dolibarr database access.
Class to manage suppliers invoices.
Class to manage invoices.
Class to manage stats for invoices (customer and supplier)
getAverageByMonth($year)
Return average amount.
getNbByMonth($year, $format=0)
Return orders number by month for a year.
getAmountByMonth($year, $format=0)
Return the invoices amount by month for a year.
__construct(DoliDB $db, $socid, $mode, $userid=0, $typentid=0, $categid=0)
Constructor.
getAllByProduct($year, $limit=10)
Return nb, amount of predefined product for year.
getNbByYear()
Return invoices number per year.
getAmountByYear($numberYears, $format=0)
Return the invoices amount by year for a number of past years.
getAllByYear()
Return nb, total and average.
Parent class of statistics class.
_getAmountByYear($sql)
Returns the summed amounts per year for a given number of past years ending now.
_getAverageByMonth($year, $sql, $format=0)
Return the amount average par month for a given year.
_getAmountByMonth($year, $sql, $format=0)
Return the amount per month for a given year.
_getNbByYear($sql)
Return nb of elements by year.
_getAllByYear($sql)
Return nb of elements, total amount and avg amount each year.
_getAllByProduct($sql, $limit=10)
Return number or total of product refs.
_getNbByMonth($year, $sql, $format=0)
Renvoie le nombre de documents par mois pour une annee donnee Return number of documents per month fo...
dol_get_first_day($year, $month=1, $gm=false)
Return GMT time for first day of a month or year.
Definition date.lib.php:594
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
Definition date.lib.php:613
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.