dolibarr  13.0.2
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($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  {
72  $object = new Facture($this->db);
73  $this->from = MAIN_DB_PREFIX.$object->table_element." as f";
74  $this->from_line = MAIN_DB_PREFIX.$object->table_element_line." as tl";
75  $this->field = 'total';
76  $this->field_line = 'total_ht';
77  }
78  if ($mode == 'supplier')
79  {
80  $object = new FactureFournisseur($this->db);
81  $this->from = MAIN_DB_PREFIX.$object->table_element." as f";
82  $this->from_line = MAIN_DB_PREFIX.$object->table_element_line." as tl";
83  $this->field = 'total_ht';
84  $this->field_line = 'total_ht';
85  }
86 
87 
88  $this->where = " f.fk_statut >= 0";
89  $this->where .= " AND f.entity IN (".getEntity('invoice').")";
90  if (!$user->rights->societe->client->voir && !$this->socid) $this->where .= " AND f.fk_soc = sc.fk_soc AND sc.fk_user = ".$user->id;
91  if ($mode == 'customer') $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)
92  if ($this->socid)
93  {
94  $this->where .= " AND f.fk_soc = ".$this->socid;
95  }
96  if ($this->userid > 0) $this->where .= ' AND f.fk_user_author = '.$this->userid;
97  if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) $this->where .= " AND f.type IN (0,1,2,5)";
98  else $this->where .= " AND f.type IN (0,1,2,3,5)";
99 
100  if ($typentid)
101  {
102  $this->join .= ' LEFT JOIN '.MAIN_DB_PREFIX.'societe as s ON s.rowid = f.fk_soc';
103  $this->where .= ' AND s.fk_typent = '.$typentid;
104  }
105 
106  if ($categid)
107  {
108  $this->join .= ' LEFT JOIN '.MAIN_DB_PREFIX.'categorie_societe as cs ON cs.fk_soc = f.fk_soc';
109  $this->join .= ' LEFT JOIN '.MAIN_DB_PREFIX.'categorie as c ON c.rowid = cs.fk_categorie';
110  $this->where .= ' AND c.rowid = '.$categid;
111  }
112  }
113 
114 
122  public function getNbByMonth($year, $format = 0)
123  {
124  global $user;
125 
126  $sql = "SELECT date_format(f.datef,'%m') as dm, COUNT(*) as nb";
127  $sql .= " FROM ".$this->from;
128  if (!$user->rights->societe->client->voir && !$this->socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
129  $sql .= $this->join;
130  $sql .= " WHERE f.datef BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
131  $sql .= " AND ".$this->where;
132  $sql .= " GROUP BY dm";
133  $sql .= $this->db->order('dm', 'DESC');
134 
135  $res = $this->_getNbByMonth($year, $sql, $format);
136  //var_dump($res);print '<br>';
137  return $res;
138  }
139 
140 
146  public function getNbByYear()
147  {
148  global $user;
149 
150  $sql = "SELECT date_format(f.datef,'%Y') as dm, COUNT(*), SUM(c.".$this->field.")";
151  $sql .= " FROM ".$this->from;
152  if (!$user->rights->societe->client->voir && !$this->socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
153  $sql .= $this->join;
154  $sql .= " WHERE ".$this->where;
155  $sql .= " GROUP BY dm";
156  $sql .= $this->db->order('dm', 'DESC');
157 
158  return $this->_getNbByYear($sql);
159  }
160 
161 
169  public function getAmountByMonth($year, $format = 0)
170  {
171  global $user;
172 
173  $sql = "SELECT date_format(datef,'%m') as dm, SUM(f.".$this->field.")";
174  $sql .= " FROM ".$this->from;
175  if (!$user->rights->societe->client->voir && !$this->socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
176  $sql .= $this->join;
177  $sql .= " WHERE f.datef BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
178  $sql .= " AND ".$this->where;
179  $sql .= " GROUP BY dm";
180  $sql .= $this->db->order('dm', 'DESC');
181 
182  $res = $this->_getAmountByMonth($year, $sql, $format);
183  //var_dump($res);print '<br>';
184  return $res;
185  }
186 
193  public function getAverageByMonth($year)
194  {
195  global $user;
196 
197  $sql = "SELECT date_format(datef,'%m') as dm, AVG(f.".$this->field.")";
198  $sql .= " FROM ".$this->from;
199  if (!$user->rights->societe->client->voir && !$this->socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
200  $sql .= $this->join;
201  $sql .= " WHERE f.datef BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
202  $sql .= " AND ".$this->where;
203  $sql .= " GROUP BY dm";
204  $sql .= $this->db->order('dm', 'DESC');
205 
206  return $this->_getAverageByMonth($year, $sql);
207  }
208 
214  public function getAllByYear()
215  {
216  global $user;
217 
218  $sql = "SELECT date_format(datef,'%Y') as year, COUNT(*) as nb, SUM(f.".$this->field.") as total, AVG(f.".$this->field.") as avg";
219  $sql .= " FROM ".$this->from;
220  if (!$user->rights->societe->client->voir && !$this->socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
221  $sql .= $this->join;
222  $sql .= " WHERE ".$this->where;
223  $sql .= " GROUP BY year";
224  $sql .= $this->db->order('year', 'DESC');
225 
226  return $this->_getAllByYear($sql);
227  }
228 
236  public function getAllByProduct($year, $limit = 10)
237  {
238  global $user;
239 
240  $sql = "SELECT product.ref, COUNT(product.ref) as nb, SUM(tl.".$this->field_line.") as total, AVG(tl.".$this->field_line.") as avg";
241  $sql .= " FROM ".$this->from.", ".$this->from_line.", ".MAIN_DB_PREFIX."product as product";
242  if (!$user->rights->societe->client->voir && !$this->socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
243  $sql .= $this->join;
244  $sql .= " WHERE ".$this->where;
245  $sql .= " AND f.rowid = tl.fk_facture AND tl.fk_product = product.rowid";
246  $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))."'";
247  $sql .= " GROUP BY product.ref";
248  $sql .= $this->db->order('nb', 'DESC');
249  //$sql.= $this->db->plimit(20);
250 
251  return $this->_getAllByProduct($sql, $limit);
252  }
253 }
Parent class of statistics class.
Definition: stats.class.php:30
getNbByMonth($year, $format=0)
Return orders number by month for a year.
getAllByProduct($year, $limit=10)
Return nb, amount of predefined product for 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:481
getAverageByMonth($year)
Return average amount.
Class to manage suppliers invoices.
_getNbByMonth($year, $sql, $format=0)
Renvoie le nombre de documents par mois pour une annee donnee Return number of documents per month fo...
$conf db
API class for accounts.
Definition: inc.php:54
_getAllByProduct($sql, $limit=10)
Return number or total of product refs.
getNbByYear()
Return invoices number per year.
getAllByYear()
Return nb, total and average.
_getAllByYear($sql)
Return nb of elements, total amount and avg amount each year.
getAmountByMonth($year, $format=0)
Return the invoices amount by month for a year.
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
Definition: date.lib.php:498
__construct($db, $socid, $mode, $userid=0, $typentid=0, $categid=0)
Constructor.
_getAmountByMonth($year, $sql, $format=0)
Return the amount per month for a given year.
_getNbByYear($sql)
Return nb of elements by year.
Class to manage invoices.
_getAverageByMonth($year, $sql, $format=0)
Renvoie le montant moyen par mois pour une annee donnee Return the amount average par month for a giv...
Class to manage stats for invoices (customer and supplier)