dolibarr  16.0.1
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 (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
101  $this->where .= " AND f.type IN (0,1,2,5)";
102  } else {
103  $this->where .= " AND f.type IN (0,1,2,3,5)";
104  }
105 
106  if ($typentid) {
107  $this->join .= ' LEFT JOIN '.MAIN_DB_PREFIX.'societe as s ON s.rowid = f.fk_soc';
108  $this->where .= ' AND s.fk_typent = '.((int) $typentid);
109  }
110 
111  if ($categid) {
112  $this->join .= ' LEFT JOIN '.MAIN_DB_PREFIX.'categorie_societe as cs ON cs.fk_soc = f.fk_soc';
113  $this->join .= ' LEFT JOIN '.MAIN_DB_PREFIX.'categorie as c ON c.rowid = cs.fk_categorie';
114  $this->where .= ' AND c.rowid = '.((int) $categid);
115  }
116  }
117 
118 
126  public function getNbByMonth($year, $format = 0)
127  {
128  global $user;
129 
130  $sql = "SELECT date_format(f.datef,'%m') as dm, COUNT(*) as nb";
131  $sql .= " FROM ".$this->from;
132  if (empty($user->rights->societe->client->voir) && !$this->socid) {
133  $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
134  }
135  $sql .= $this->join;
136  $sql .= " WHERE f.datef BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
137  $sql .= " AND ".$this->where;
138  $sql .= " GROUP BY dm";
139  $sql .= $this->db->order('dm', 'DESC');
140 
141  $res = $this->_getNbByMonth($year, $sql, $format);
142  //var_dump($res);print '<br>';
143  return $res;
144  }
145 
146 
152  public function getNbByYear()
153  {
154  global $user;
155 
156  $sql = "SELECT date_format(f.datef,'%Y') as dm, COUNT(*), SUM(c.".$this->field.")";
157  $sql .= " FROM ".$this->from;
158  if (empty($user->rights->societe->client->voir) && !$this->socid) {
159  $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
160  }
161  $sql .= $this->join;
162  $sql .= " WHERE ".$this->where;
163  $sql .= " GROUP BY dm";
164  $sql .= $this->db->order('dm', 'DESC');
165 
166  return $this->_getNbByYear($sql);
167  }
168 
169 
177  public function getAmountByMonth($year, $format = 0)
178  {
179  global $user;
180 
181  $sql = "SELECT date_format(datef,'%m') as dm, SUM(f.".$this->field.")";
182  $sql .= " FROM ".$this->from;
183  if (empty($user->rights->societe->client->voir) && !$this->socid) {
184  $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
185  }
186  $sql .= $this->join;
187  $sql .= " WHERE f.datef BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
188  $sql .= " AND ".$this->where;
189  $sql .= " GROUP BY dm";
190  $sql .= $this->db->order('dm', 'DESC');
191 
192  $res = $this->_getAmountByMonth($year, $sql, $format);
193  //var_dump($res);print '<br>';
194  return $res;
195  }
196 
203  public function getAverageByMonth($year)
204  {
205  global $user;
206 
207  $sql = "SELECT date_format(datef,'%m') as dm, AVG(f.".$this->field.")";
208  $sql .= " FROM ".$this->from;
209  if (empty($user->rights->societe->client->voir) && !$this->socid) {
210  $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
211  }
212  $sql .= $this->join;
213  $sql .= " WHERE f.datef BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
214  $sql .= " AND ".$this->where;
215  $sql .= " GROUP BY dm";
216  $sql .= $this->db->order('dm', 'DESC');
217 
218  return $this->_getAverageByMonth($year, $sql);
219  }
220 
226  public function getAllByYear()
227  {
228  global $user;
229 
230  $sql = "SELECT date_format(datef,'%Y') as year, COUNT(*) as nb, SUM(f.".$this->field.") as total, AVG(f.".$this->field.") as avg";
231  $sql .= " FROM ".$this->from;
232  if (empty($user->rights->societe->client->voir) && !$this->socid) {
233  $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
234  }
235  $sql .= $this->join;
236  $sql .= " WHERE ".$this->where;
237  $sql .= " GROUP BY year";
238  $sql .= $this->db->order('year', 'DESC');
239 
240  return $this->_getAllByYear($sql);
241  }
242 
250  public function getAllByProduct($year, $limit = 10)
251  {
252  global $user;
253 
254  $sql = "SELECT product.ref, COUNT(product.ref) as nb, SUM(tl.".$this->field_line.") as total, AVG(tl.".$this->field_line.") as avg";
255  $sql .= " FROM ".$this->from.", ".$this->from_line.", ".MAIN_DB_PREFIX."product as product";
256  if (empty($user->rights->societe->client->voir) && !$this->socid) {
257  $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
258  }
259  $sql .= $this->join;
260  $sql .= " WHERE ".$this->where;
261  $sql .= " AND f.rowid = tl.fk_facture AND tl.fk_product = product.rowid";
262  $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))."'";
263  $sql .= " GROUP BY product.ref";
264  $sql .= $this->db->order('nb', 'DESC');
265  //$sql.= $this->db->plimit(20);
266 
267  return $this->_getAllByProduct($sql, $limit);
268  }
276  public function getAmountByYear($numberYears, $format = 0)
277  {
278  global $user;
279 
280  $endYear = date('Y');
281  $startYear = $endYear - $numberYears;
282  $sql = "SELECT date_format(datef,'%Y') as dm, SUM(f.".$this->field.")";
283  $sql .= " FROM ".$this->from;
284  if (empty($user->rights->societe->client->voir) && !$this->socid) {
285  $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
286  }
287  $sql .= $this->join;
288  $sql .= " WHERE f.datef BETWEEN '".$this->db->idate(dol_get_first_day($startYear))."' AND '".$this->db->idate(dol_get_last_day($endYear))."'";
289  $sql .= " AND ".$this->where;
290  $sql .= " GROUP BY dm";
291  $sql .= $this->db->order('dm', 'ASC');
292 
293  $res = $this->_getAmountByYear($sql);
294  return $res;
295  }
296 }
getAmountByYear($numberYears, $format=0)
Return the invoices amount by year for a number of past years.
__construct(DoliDB $db, $socid, $mode, $userid=0, $typentid=0, $categid=0)
Constructor.
Parent class of statistics class.
Definition: stats.class.php:30
$conf db
API class for accounts.
Definition: inc.php:41
getNbByMonth($year, $format=0)
Return orders number by month for a year.
getAllByProduct($year, $limit=10)
Return nb, amount of predefined product for year.
_getAmountByYear($sql)
Returns the summed amounts per year for a given number of past years ending now.
Class to manage Dolibarr database access.
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
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...
_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:570
_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)