dolibarr 23.0.3
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 * 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 global $user, $conf;
83
84 $this->db = $db;
85 $this->socid = ($socid > 0 ? $socid : 0);
86 $this->userid = $userid;
87 $this->cachefilesuffix = $mode;
88 $this->join = '';
89
90 if ($mode == 'customer') {
91 $object = new Facture($this->db);
92 $this->from = MAIN_DB_PREFIX.$object->table_element." as f";
93 $this->from_line = MAIN_DB_PREFIX.$object->table_element_line." as tl";
94 $this->field = 'total_ht';
95 $this->field_line = 'total_ht';
96 }
97 if ($mode == 'supplier') {
98 $object = new FactureFournisseur($this->db);
99 $this->from = MAIN_DB_PREFIX.$object->table_element." as f";
100 $this->from_line = MAIN_DB_PREFIX.$object->table_element_line." as tl";
101 $this->field = 'total_ht';
102 $this->field_line = 'total_ht';
103 }
104
105 $this->where .= ($this->where ? ' AND ' : '')." f.fk_statut >= 0";
106 $this->where .= " AND f.entity IN (".getEntity('invoice').")";
107
108 if ($mode == 'customer') {
109 $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)
110 }
111 if ($this->socid) {
112 $this->where .= " AND f.fk_soc = ".((int) $this->socid);
113 }
114 if ($this->userid > 0) {
115 $this->where .= ' AND f.fk_user_author = '.((int) $this->userid);
116 }
117 if ($mode == 'customer') {
118 if (getDolGlobalString('FACTURE_DEPOSITS_ARE_JUST_PAYMENTS')) {
119 $this->where .= " AND f.type IN (0,1,2,5)";
120 } else {
121 $this->where .= " AND f.type IN (0,1,2,3,5)";
122 }
123 }
124 if ($mode == 'supplier') {
125 if (getDolGlobalString('FACTURE_SUPPLIER_DEPOSITS_ARE_JUST_PAYMENTS')) {
126 $this->where .= " AND f.type IN (0,1,2,5)";
127 } else {
128 $this->where .= " AND f.type IN (0,1,2,3,5)";
129 }
130 }
131
132 if ($typentid) {
133 $this->join .= ' LEFT JOIN '.MAIN_DB_PREFIX.'societe as s ON s.rowid = f.fk_soc';
134 $this->where .= ' AND s.fk_typent = '.((int) $typentid);
135 }
136
137 if ($categid) {
138 $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).')';
139 }
140 }
141
142
150 public function getNbByMonth($year, $format = 0)
151 {
152 global $user;
153
154 $sql = "SELECT date_format(f.datef,'%m') as dm, COUNT(*) as nb";
155 $sql .= " FROM ".$this->from;
156 if (empty($user->socid) && !$user->hasRight('societe', 'client', 'voir')) {
157 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON f.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
158 }
159 $sql .= $this->join;
160 $sql .= " WHERE f.datef BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
161 $sql .= " AND ".$this->where;
162 $sql .= " GROUP BY dm";
163 $sql .= $this->db->order('dm', 'DESC');
164
165 $res = $this->_getNbByMonth($year, $sql, $format);
166 //var_dump($res);print '<br>';
167 return $res;
168 }
169
170
176 public function getNbByYear()
177 {
178 global $user;
179
180 $sql = "SELECT date_format(f.datef,'%Y') as dm, COUNT(*), SUM(c.".$this->field.")";
181 $sql .= " FROM ".$this->from;
182 if (empty($user->socid) && !$user->hasRight('societe', 'client', 'voir')) {
183 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON f.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
184 }
185 $sql .= $this->join;
186 $sql .= " WHERE ".$this->where;
187 $sql .= " GROUP BY dm";
188 $sql .= $this->db->order('dm', 'DESC');
189
190 return $this->_getNbByYear($sql);
191 }
192
193
201 public function getAmountByMonth($year, $format = 0)
202 {
203 global $user;
204
205 $sql = "SELECT date_format(datef,'%m') as dm, SUM(f.".$this->field.")";
206 $sql .= " FROM ".$this->from;
207 if (empty($user->socid) && !$user->hasRight('societe', 'client', 'voir')) {
208 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON f.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
209 }
210 $sql .= $this->join;
211 $sql .= " WHERE f.datef BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
212 $sql .= " AND ".$this->where;
213 $sql .= " GROUP BY dm";
214 $sql .= $this->db->order('dm', 'DESC');
215
216 $res = $this->_getAmountByMonth($year, $sql, $format);
217 //var_dump($res);print '<br>';
218 return $res;
219 }
220
227 public function getAverageByMonth($year)
228 {
229 global $user;
230
231 $sql = "SELECT date_format(datef,'%m') as dm, AVG(f.".$this->field.")";
232 $sql .= " FROM ".$this->from;
233 if (empty($user->socid) && !$user->hasRight('societe', 'client', 'voir')) {
234 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON f.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
235 }
236 $sql .= $this->join;
237 $sql .= " WHERE f.datef BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
238 $sql .= " AND ".$this->where;
239 $sql .= " GROUP BY dm";
240 $sql .= $this->db->order('dm', 'DESC');
241
242 return $this->_getAverageByMonth($year, $sql);
243 }
244
250 public function getAllByYear()
251 {
252 global $user;
253
254 $sql = "SELECT date_format(datef,'%Y') as year, COUNT(*) as nb, SUM(f.".$this->field.") as total, AVG(f.".$this->field.") as avg";
255 $sql .= " FROM ".$this->from;
256 if (empty($user->socid) && !$user->hasRight('societe', 'client', 'voir')) {
257 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON f.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
258 }
259 $sql .= $this->join;
260 $sql .= " WHERE ".$this->where;
261 $sql .= " GROUP BY year";
262 $sql .= $this->db->order('year', 'DESC');
263
264 return $this->_getAllByYear($sql);
265 }
266
274 public function getAllByProduct($year, $limit = 10)
275 {
276 global $user;
277
278 $sql = "SELECT product.ref, COUNT(product.ref) as nb, SUM(tl.".$this->field_line.") as total, AVG(tl.".$this->field_line.") as avg";
279 $sql .= " FROM ".$this->from;
280 $sql .= " INNER JOIN ".$this->from_line." ON f.rowid = tl.fk_facture";
281 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."product as product ON tl.fk_product = product.rowid";
282 if (empty($user->socid) && !$user->hasRight('societe', 'client', 'voir')) {
283 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON f.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
284 }
285 $sql .= $this->join;
286 $sql .= " WHERE ".$this->where;
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 (empty($user->socid) && !$user->hasRight('societe', 'client', 'voir')) {
310 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON f.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
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(! $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)
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:603
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
Definition date.lib.php:622
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
if(getDolGlobalString( 'TAKEPOS_SHOW_CUSTOMER')) print $langs trans('Date')." left 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 PaymentTypeShortLIQ right SELECT p pos_change as p datep as date
Definition receipt.php:464