dolibarr  16.0.5
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 
26 include_once DOL_DOCUMENT_ROOT.'/core/class/stats.class.php';
27 include_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
28 include_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
29 include_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
30 
34 class 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->join .= ' LEFT JOIN '.MAIN_DB_PREFIX.'categorie_societe as cs ON cs.fk_soc = f.fk_soc';
122  $this->join .= ' LEFT JOIN '.MAIN_DB_PREFIX.'categorie as c ON c.rowid = cs.fk_categorie';
123  $this->where .= ' AND c.rowid = '.((int) $categid);
124  }
125  }
126 
127 
135  public function getNbByMonth($year, $format = 0)
136  {
137  global $user;
138 
139  $sql = "SELECT date_format(f.datef,'%m') as dm, COUNT(*) as nb";
140  $sql .= " FROM ".$this->from;
141  if (empty($user->rights->societe->client->voir) && !$this->socid) {
142  $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
143  }
144  $sql .= $this->join;
145  $sql .= " WHERE f.datef BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
146  $sql .= " AND ".$this->where;
147  $sql .= " GROUP BY dm";
148  $sql .= $this->db->order('dm', 'DESC');
149 
150  $res = $this->_getNbByMonth($year, $sql, $format);
151  //var_dump($res);print '<br>';
152  return $res;
153  }
154 
155 
161  public function getNbByYear()
162  {
163  global $user;
164 
165  $sql = "SELECT date_format(f.datef,'%Y') as dm, COUNT(*), SUM(c.".$this->field.")";
166  $sql .= " FROM ".$this->from;
167  if (empty($user->rights->societe->client->voir) && !$this->socid) {
168  $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
169  }
170  $sql .= $this->join;
171  $sql .= " WHERE ".$this->where;
172  $sql .= " GROUP BY dm";
173  $sql .= $this->db->order('dm', 'DESC');
174 
175  return $this->_getNbByYear($sql);
176  }
177 
178 
186  public function getAmountByMonth($year, $format = 0)
187  {
188  global $user;
189 
190  $sql = "SELECT date_format(datef,'%m') as dm, SUM(f.".$this->field.")";
191  $sql .= " FROM ".$this->from;
192  if (empty($user->rights->societe->client->voir) && !$this->socid) {
193  $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
194  }
195  $sql .= $this->join;
196  $sql .= " WHERE f.datef BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
197  $sql .= " AND ".$this->where;
198  $sql .= " GROUP BY dm";
199  $sql .= $this->db->order('dm', 'DESC');
200 
201  $res = $this->_getAmountByMonth($year, $sql, $format);
202  //var_dump($res);print '<br>';
203  return $res;
204  }
205 
212  public function getAverageByMonth($year)
213  {
214  global $user;
215 
216  $sql = "SELECT date_format(datef,'%m') as dm, AVG(f.".$this->field.")";
217  $sql .= " FROM ".$this->from;
218  if (empty($user->rights->societe->client->voir) && !$this->socid) {
219  $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
220  }
221  $sql .= $this->join;
222  $sql .= " WHERE f.datef BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
223  $sql .= " AND ".$this->where;
224  $sql .= " GROUP BY dm";
225  $sql .= $this->db->order('dm', 'DESC');
226 
227  return $this->_getAverageByMonth($year, $sql);
228  }
229 
235  public function getAllByYear()
236  {
237  global $user;
238 
239  $sql = "SELECT date_format(datef,'%Y') as year, COUNT(*) as nb, SUM(f.".$this->field.") as total, AVG(f.".$this->field.") as avg";
240  $sql .= " FROM ".$this->from;
241  if (empty($user->rights->societe->client->voir) && !$this->socid) {
242  $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
243  }
244  $sql .= $this->join;
245  $sql .= " WHERE ".$this->where;
246  $sql .= " GROUP BY year";
247  $sql .= $this->db->order('year', 'DESC');
248 
249  return $this->_getAllByYear($sql);
250  }
251 
259  public function getAllByProduct($year, $limit = 10)
260  {
261  global $user;
262 
263  $sql = "SELECT product.ref, COUNT(product.ref) as nb, SUM(tl.".$this->field_line.") as total, AVG(tl.".$this->field_line.") as avg";
264  $sql .= " FROM ".$this->from.", ".$this->from_line.", ".MAIN_DB_PREFIX."product as product";
265  if (empty($user->rights->societe->client->voir) && !$this->socid) {
266  $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
267  }
268  $sql .= $this->join;
269  $sql .= " WHERE ".$this->where;
270  $sql .= " AND f.rowid = tl.fk_facture AND tl.fk_product = product.rowid";
271  $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))."'";
272  $sql .= " GROUP BY product.ref";
273  $sql .= $this->db->order('nb', 'DESC');
274  //$sql.= $this->db->plimit(20);
275 
276  return $this->_getAllByProduct($sql, $limit);
277  }
285  public function getAmountByYear($numberYears, $format = 0)
286  {
287  global $user;
288 
289  $endYear = date('Y');
290  $startYear = $endYear - $numberYears;
291  $sql = "SELECT date_format(datef,'%Y') as dm, SUM(f.".$this->field.")";
292  $sql .= " FROM ".$this->from;
293  if (empty($user->rights->societe->client->voir) && !$this->socid) {
294  $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
295  }
296  $sql .= $this->join;
297  $sql .= " WHERE f.datef BETWEEN '".$this->db->idate(dol_get_first_day($startYear))."' AND '".$this->db->idate(dol_get_last_day($endYear))."'";
298  $sql .= " AND ".$this->where;
299  $sql .= " GROUP BY dm";
300  $sql .= $this->db->order('dm', 'ASC');
301 
302  $res = $this->_getAmountByYear($sql);
303  return $res;
304  }
305 }
db
$conf db
API class for accounts.
Definition: inc.php:41
Stats\_getAllByProduct
_getAllByProduct($sql, $limit=10)
Return number or total of product refs.
Definition: stats.class.php:607
DoliDB
Class to manage Dolibarr database access.
Definition: DoliDB.class.php:30
FactureStats\getAmountByYear
getAmountByYear($numberYears, $format=0)
Return the invoices amount by year for a number of past years.
Definition: facturestats.class.php:285
FactureFournisseur
Class to manage suppliers invoices.
Definition: fournisseur.facture.class.php:53
Stats
Parent class of statistics class.
Definition: stats.class.php:30
Stats\_getNbByYear
_getNbByYear($sql)
Return nb of elements by year.
Definition: stats.class.php:355
Stats\_getAmountByMonth
_getAmountByMonth($year, $sql, $format=0)
Return the amount per month for a given year.
Definition: stats.class.php:492
Facture
Class to manage invoices.
Definition: facture.class.php:60
FactureStats\getAverageByMonth
getAverageByMonth($year)
Return average amount.
Definition: facturestats.class.php:212
FactureStats
Class to manage stats for invoices (customer and supplier)
Definition: facturestats.class.php:34
FactureStats\getAllByProduct
getAllByProduct($year, $limit=10)
Return nb, amount of predefined product for year.
Definition: facturestats.class.php:259
Stats\_getAllByYear
_getAllByYear($sql)
Return nb of elements, total amount and avg amount each year.
Definition: stats.class.php:384
FactureStats\getAmountByMonth
getAmountByMonth($year, $format=0)
Return the invoices amount by month for a year.
Definition: facturestats.class.php:186
FactureStats\getNbByMonth
getNbByMonth($year, $format=0)
Return orders number by month for a year.
Definition: facturestats.class.php:135
FactureStats\getNbByYear
getNbByYear()
Return invoices number per year.
Definition: facturestats.class.php:161
FactureStats\getAllByYear
getAllByYear()
Return nb, total and average.
Definition: facturestats.class.php:235
dol_get_first_day
dol_get_first_day($year, $month=1, $gm=false)
Return GMT time for first day of a month or year.
Definition: date.lib.php:551
dol_get_last_day
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
Definition: date.lib.php:570
Stats\_getAmountByYear
_getAmountByYear($sql)
Returns the summed amounts per year for a given number of past years ending now.
Definition: stats.class.php:646
Stats\_getNbByMonth
_getNbByMonth($year, $sql, $format=0)
Renvoie le nombre de documents par mois pour une annee donnee Return number of documents per month fo...
Definition: stats.class.php:435
FactureStats\__construct
__construct(DoliDB $db, $socid, $mode, $userid=0, $typentid=0, $categid=0)
Constructor.
Definition: facturestats.class.php:60
Stats\_getAverageByMonth
_getAverageByMonth($year, $sql, $format=0)
Renvoie le montant moyen par mois pour une annee donnee Return the amount average par month for a giv...
Definition: stats.class.php:550