dolibarr 18.0.6
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
86 $this->where = " f.fk_statut >= 0";
87 $this->where .= " AND f.entity IN (".getEntity('invoice').")";
88 if (empty($user->rights->societe->client->voir) && !$this->socid) {
89 $this->where .= " AND f.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
90 }
91 if ($mode == 'customer') {
92 $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)
93 }
94 if ($this->socid) {
95 $this->where .= " AND f.fk_soc = ".((int) $this->socid);
96 }
97 if ($this->userid > 0) {
98 $this->where .= ' AND f.fk_user_author = '.((int) $this->userid);
99 }
100 if ($mode == 'customer') {
101 if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
102 $this->where .= " AND f.type IN (0,1,2,5)";
103 } else {
104 $this->where .= " AND f.type IN (0,1,2,3,5)";
105 }
106 }
107 if ($mode == 'supplier') {
108 if (!empty($conf->global->FACTURE_SUPPLIER_DEPOSITS_ARE_JUST_PAYMENTS)) {
109 $this->where .= " AND f.type IN (0,1,2,5)";
110 } else {
111 $this->where .= " AND f.type IN (0,1,2,3,5)";
112 }
113 }
114
115 if ($typentid) {
116 $this->join .= ' LEFT JOIN '.MAIN_DB_PREFIX.'societe as s ON s.rowid = f.fk_soc';
117 $this->where .= ' AND s.fk_typent = '.((int) $typentid);
118 }
119
120 if ($categid) {
121 $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).')';
122 }
123 }
124
125
133 public function getNbByMonth($year, $format = 0)
134 {
135 global $user;
136
137 $sql = "SELECT date_format(f.datef,'%m') as dm, COUNT(*) as nb";
138 $sql .= " FROM ".$this->from;
139 if (empty($user->rights->societe->client->voir) && !$this->socid) {
140 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
141 }
142 $sql .= $this->join;
143 $sql .= " WHERE f.datef BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
144 $sql .= " AND ".$this->where;
145 $sql .= " GROUP BY dm";
146 $sql .= $this->db->order('dm', 'DESC');
147
148 $res = $this->_getNbByMonth($year, $sql, $format);
149 //var_dump($res);print '<br>';
150 return $res;
151 }
152
153
159 public function getNbByYear()
160 {
161 global $user;
162
163 $sql = "SELECT date_format(f.datef,'%Y') as dm, COUNT(*), SUM(c.".$this->field.")";
164 $sql .= " FROM ".$this->from;
165 if (empty($user->rights->societe->client->voir) && !$this->socid) {
166 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
167 }
168 $sql .= $this->join;
169 $sql .= " WHERE ".$this->where;
170 $sql .= " GROUP BY dm";
171 $sql .= $this->db->order('dm', 'DESC');
172
173 return $this->_getNbByYear($sql);
174 }
175
176
184 public function getAmountByMonth($year, $format = 0)
185 {
186 global $user;
187
188 $sql = "SELECT date_format(datef,'%m') as dm, SUM(f.".$this->field.")";
189 $sql .= " FROM ".$this->from;
190 if (empty($user->rights->societe->client->voir) && !$this->socid) {
191 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
192 }
193 $sql .= $this->join;
194 $sql .= " WHERE f.datef BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
195 $sql .= " AND ".$this->where;
196 $sql .= " GROUP BY dm";
197 $sql .= $this->db->order('dm', 'DESC');
198
199 $res = $this->_getAmountByMonth($year, $sql, $format);
200 //var_dump($res);print '<br>';
201 return $res;
202 }
203
210 public function getAverageByMonth($year)
211 {
212 global $user;
213
214 $sql = "SELECT date_format(datef,'%m') as dm, AVG(f.".$this->field.")";
215 $sql .= " FROM ".$this->from;
216 if (empty($user->rights->societe->client->voir) && !$this->socid) {
217 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
218 }
219 $sql .= $this->join;
220 $sql .= " WHERE f.datef BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
221 $sql .= " AND ".$this->where;
222 $sql .= " GROUP BY dm";
223 $sql .= $this->db->order('dm', 'DESC');
224
225 return $this->_getAverageByMonth($year, $sql);
226 }
227
233 public function getAllByYear()
234 {
235 global $user;
236
237 $sql = "SELECT date_format(datef,'%Y') as year, COUNT(*) as nb, SUM(f.".$this->field.") as total, AVG(f.".$this->field.") as avg";
238 $sql .= " FROM ".$this->from;
239 if (empty($user->rights->societe->client->voir) && !$this->socid) {
240 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
241 }
242 $sql .= $this->join;
243 $sql .= " WHERE ".$this->where;
244 $sql .= " GROUP BY year";
245 $sql .= $this->db->order('year', 'DESC');
246
247 return $this->_getAllByYear($sql);
248 }
249
257 public function getAllByProduct($year, $limit = 10)
258 {
259 global $user;
260
261 $sql = "SELECT product.ref, COUNT(product.ref) as nb, SUM(tl.".$this->field_line.") as total, AVG(tl.".$this->field_line.") as avg";
262 $sql .= " FROM ".$this->from.", ".$this->from_line.", ".MAIN_DB_PREFIX."product as product";
263 if (empty($user->rights->societe->client->voir) && !$this->socid) {
264 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
265 }
266 $sql .= $this->join;
267 $sql .= " WHERE ".$this->where;
268 $sql .= " AND f.rowid = tl.fk_facture AND tl.fk_product = product.rowid";
269 $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))."'";
270 $sql .= " GROUP BY product.ref";
271 $sql .= $this->db->order('nb', 'DESC');
272 //$sql.= $this->db->plimit(20);
273
274 return $this->_getAllByProduct($sql, $limit);
275 }
283 public function getAmountByYear($numberYears, $format = 0)
284 {
285 global $user;
286
287 $endYear = date('Y');
288 $startYear = $endYear - $numberYears;
289 $sql = "SELECT date_format(datef,'%Y') as dm, SUM(f.".$this->field.")";
290 $sql .= " FROM ".$this->from;
291 if (empty($user->rights->societe->client->voir) && !$this->socid) {
292 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
293 }
294 $sql .= $this->join;
295 $sql .= " WHERE f.datef BETWEEN '".$this->db->idate(dol_get_first_day($startYear))."' AND '".$this->db->idate(dol_get_last_day($endYear))."'";
296 $sql .= " AND ".$this->where;
297 $sql .= " GROUP BY dm";
298 $sql .= $this->db->order('dm', 'ASC');
299
300 $res = $this->_getAmountByYear($sql);
301 return $res;
302 }
303}
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)
Renvoie le montant moyen par mois pour une annee donnee Return the amount average par month for a giv...
_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:578
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
Definition date.lib.php:597