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 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
27include_once DOL_DOCUMENT_ROOT.'/core/class/stats.class.php';
28include_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
29include_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
30include_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
31
35class FactureStats extends Stats
36{
40 public $socid;
44 public $userid;
45
49 public $table_element;
50
54 public $from;
58 public $field;
62 public $where = '';
66 public $join;
67
68
79 public function __construct(DoliDB $db, $socid, $mode, $userid = 0, $typentid = 0, $categid = 0)
80 {
81 global $user, $conf;
82
83 $this->db = $db;
84 $this->socid = ($socid > 0 ? $socid : 0);
85 $this->userid = $userid;
86 $this->cachefilesuffix = $mode;
87 $this->join = '';
88
89 if ($mode == 'customer') {
90 $object = new Facture($this->db);
91 $this->from = MAIN_DB_PREFIX.$object->table_element." as f";
92 $this->from_line = MAIN_DB_PREFIX.$object->table_element_line." as tl";
93 $this->field = 'total_ht';
94 $this->field_line = 'total_ht';
95 }
96 if ($mode == 'supplier') {
97 $object = new FactureFournisseur($this->db);
98 $this->from = MAIN_DB_PREFIX.$object->table_element." as f";
99 $this->from_line = MAIN_DB_PREFIX.$object->table_element_line." as tl";
100 $this->field = 'total_ht';
101 $this->field_line = 'total_ht';
102 }
103
104 $this->where .= ($this->where ? ' AND ' : '')." f.fk_statut >= 0";
105 $this->where .= " AND f.entity IN (".getEntity('invoice').")";
106 if (!$user->hasRight('societe', 'client', 'voir')) {
107 $this->where .= " AND f.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
108 }
109 if ($mode == 'customer') {
110 $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)
111 }
112 if ($this->socid) {
113 $this->where .= " AND f.fk_soc = ".((int) $this->socid);
114 }
115 if ($this->userid > 0) {
116 $this->where .= ' AND f.fk_user_author = '.((int) $this->userid);
117 }
118 if ($mode == 'customer') {
119 if (getDolGlobalString('FACTURE_DEPOSITS_ARE_JUST_PAYMENTS')) {
120 $this->where .= " AND f.type IN (0,1,2,5)";
121 } else {
122 $this->where .= " AND f.type IN (0,1,2,3,5)";
123 }
124 }
125 if ($mode == 'supplier') {
126 if (getDolGlobalString('FACTURE_SUPPLIER_DEPOSITS_ARE_JUST_PAYMENTS')) {
127 $this->where .= " AND f.type IN (0,1,2,5)";
128 } else {
129 $this->where .= " AND f.type IN (0,1,2,3,5)";
130 }
131 }
132
133 if ($typentid) {
134 $this->join .= ' LEFT JOIN '.MAIN_DB_PREFIX.'societe as s ON s.rowid = f.fk_soc';
135 $this->where .= ' AND s.fk_typent = '.((int) $typentid);
136 }
137
138 if ($categid) {
139 $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).')';
140 }
141 }
142
143
151 public function getNbByMonth($year, $format = 0)
152 {
153 global $user;
154
155 $sql = "SELECT date_format(f.datef,'%m') as dm, COUNT(*) as nb";
156 $sql .= " FROM ".$this->from;
157 if (!$user->hasRight('societe', 'client', 'voir')) {
158 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
159 }
160 $sql .= $this->join;
161 $sql .= " WHERE f.datef BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
162 $sql .= " AND ".$this->where;
163 $sql .= " GROUP BY dm";
164 $sql .= $this->db->order('dm', 'DESC');
165
166 $res = $this->_getNbByMonth($year, $sql, $format);
167 //var_dump($res);print '<br>';
168 return $res;
169 }
170
171
177 public function getNbByYear()
178 {
179 global $user;
180
181 $sql = "SELECT date_format(f.datef,'%Y') as dm, COUNT(*), SUM(c.".$this->field.")";
182 $sql .= " FROM ".$this->from;
183 if (!$user->hasRight('societe', 'client', 'voir')) {
184 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
185 }
186 $sql .= $this->join;
187 $sql .= " WHERE ".$this->where;
188 $sql .= " GROUP BY dm";
189 $sql .= $this->db->order('dm', 'DESC');
190
191 return $this->_getNbByYear($sql);
192 }
193
194
202 public function getAmountByMonth($year, $format = 0)
203 {
204 global $user;
205
206 $sql = "SELECT date_format(datef,'%m') as dm, SUM(f.".$this->field.")";
207 $sql .= " FROM ".$this->from;
208 if (!$user->hasRight('societe', 'client', 'voir')) {
209 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
210 }
211 $sql .= $this->join;
212 $sql .= " WHERE f.datef BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
213 $sql .= " AND ".$this->where;
214 $sql .= " GROUP BY dm";
215 $sql .= $this->db->order('dm', 'DESC');
216
217 $res = $this->_getAmountByMonth($year, $sql, $format);
218 //var_dump($res);print '<br>';
219 return $res;
220 }
221
228 public function getAverageByMonth($year)
229 {
230 global $user;
231
232 $sql = "SELECT date_format(datef,'%m') as dm, AVG(f.".$this->field.")";
233 $sql .= " FROM ".$this->from;
234 if (!$user->hasRight('societe', 'client', 'voir')) {
235 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
236 }
237 $sql .= $this->join;
238 $sql .= " WHERE f.datef BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
239 $sql .= " AND ".$this->where;
240 $sql .= " GROUP BY dm";
241 $sql .= $this->db->order('dm', 'DESC');
242
243 return $this->_getAverageByMonth($year, $sql);
244 }
245
251 public function getAllByYear()
252 {
253 global $user;
254
255 $sql = "SELECT date_format(datef,'%Y') as year, COUNT(*) as nb, SUM(f.".$this->field.") as total, AVG(f.".$this->field.") as avg";
256 $sql .= " FROM ".$this->from;
257 if (!$user->hasRight('societe', 'client', 'voir')) {
258 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
259 }
260 $sql .= $this->join;
261 $sql .= " WHERE ".$this->where;
262 $sql .= " GROUP BY year";
263 $sql .= $this->db->order('year', 'DESC');
264
265 return $this->_getAllByYear($sql);
266 }
267
275 public function getAllByProduct($year, $limit = 10)
276 {
277 global $user;
278
279 $sql = "SELECT product.ref, COUNT(product.ref) as nb, SUM(tl.".$this->field_line.") as total, AVG(tl.".$this->field_line.") as avg";
280 $sql .= " FROM ".$this->from.", ".$this->from_line.", ".MAIN_DB_PREFIX."product as product";
281 if (!$user->hasRight('societe', 'client', 'voir')) {
282 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
283 }
284 $sql .= $this->join;
285 $sql .= " WHERE ".$this->where;
286 $sql .= " AND f.rowid = tl.fk_facture AND tl.fk_product = product.rowid";
287 $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))."'";
288 $sql .= " GROUP BY product.ref";
289 $sql .= $this->db->order('nb', 'DESC');
290 //$sql.= $this->db->plimit(20);
291
292 return $this->_getAllByProduct($sql, $limit);
293 }
301 public function getAmountByYear($numberYears, $format = 0)
302 {
303 global $user;
304
305 $endYear = (int) date('Y');
306 $startYear = $endYear - $numberYears;
307 $sql = "SELECT date_format(datef,'%Y') as dm, SUM(f.".$this->field.")";
308 $sql .= " FROM ".$this->from;
309 if (!$user->hasRight('societe', 'client', 'voir')) {
310 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
311 }
312 $sql .= $this->join;
313 $sql .= " WHERE f.datef BETWEEN '".$this->db->idate(dol_get_first_day($startYear))."' AND '".$this->db->idate(dol_get_last_day($endYear))."'";
314 $sql .= " AND ".$this->where;
315 $sql .= " GROUP BY dm";
316 $sql .= $this->db->order('dm', 'ASC');
317
318 $res = $this->_getAmountByYear($sql);
319 return $res;
320 }
321}
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:596
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
Definition date.lib.php:615
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.