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