dolibarr  9.0.0
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  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
25 include_once DOL_DOCUMENT_ROOT . '/core/class/stats.class.php';
26 include_once DOL_DOCUMENT_ROOT . '/compta/facture/class/facture.class.php';
27 include_once DOL_DOCUMENT_ROOT . '/fourn/class/fournisseur.facture.class.php';
28 include_once DOL_DOCUMENT_ROOT . '/core/lib/date.lib.php';
29 
33 class FactureStats extends Stats
34 {
35  var $socid;
36  var $userid;
37 
41  public $table_element;
42 
43  var $from;
44  var $field;
45  var $where;
46 
47 
56  function __construct($db, $socid, $mode, $userid=0)
57  {
58  global $user, $conf;
59 
60  $this->db = $db;
61  $this->socid = ($socid > 0 ? $socid : 0);
62  $this->userid = $userid;
63  $this->cachefilesuffix = $mode;
64 
65  if ($mode == 'customer')
66  {
67  $object=new Facture($this->db);
68  $this->from = MAIN_DB_PREFIX.$object->table_element." as f";
69  $this->from_line = MAIN_DB_PREFIX.$object->table_element_line." as tl";
70  $this->field='total';
71  $this->field_line='total_ht';
72  }
73  if ($mode == 'supplier')
74  {
75  $object=new FactureFournisseur($this->db);
76  $this->from = MAIN_DB_PREFIX.$object->table_element." as f";
77  $this->from_line = MAIN_DB_PREFIX.$object->table_element_line." as tl";
78  $this->field='total_ht';
79  $this->field_line='total_ht';
80  }
81 
82  $this->where = " f.fk_statut > 0";
83  $this->where.= " AND f.entity = ".$conf->entity;
84  if (!$user->rights->societe->client->voir && !$this->socid) $this->where .= " AND f.fk_soc = sc.fk_soc AND sc.fk_user = " .$user->id;
85  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)
86  if ($this->socid)
87  {
88  $this->where.=" AND f.fk_soc = ".$this->socid;
89  }
90  if ($this->userid > 0) $this->where.=' AND f.fk_user_author = '.$this->userid;
91  if (! empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) $this->where.= " AND f.type IN (0,1,2,5)";
92  else $this->where.= " AND f.type IN (0,1,2,3,5)";
93  }
94 
95 
103  function getNbByMonth($year, $format=0)
104  {
105  global $user;
106 
107  $sql = "SELECT date_format(f.datef,'%m') as dm, COUNT(*) as nb";
108  $sql.= " FROM ".$this->from;
109  if (!$user->rights->societe->client->voir && !$this->socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
110  $sql.= " WHERE f.datef BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
111  $sql.= " AND ".$this->where;
112  $sql.= " GROUP BY dm";
113  $sql.= $this->db->order('dm','DESC');
114 
115  $res=$this->_getNbByMonth($year, $sql, $format);
116  //var_dump($res);print '<br>';
117  return $res;
118  }
119 
120 
126  function getNbByYear()
127  {
128  global $user;
129 
130  $sql = "SELECT date_format(f.datef,'%Y') as dm, COUNT(*), SUM(c.".$this->field.")";
131  $sql.= " FROM ".$this->from;
132  if (!$user->rights->societe->client->voir && !$this->socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
133  $sql.= " WHERE ".$this->where;
134  $sql.= " GROUP BY dm";
135  $sql.= $this->db->order('dm','DESC');
136 
137  return $this->_getNbByYear($sql);
138  }
139 
140 
148  function getAmountByMonth($year, $format=0)
149  {
150  global $user;
151 
152  $sql = "SELECT date_format(datef,'%m') as dm, SUM(f.".$this->field.")";
153  $sql.= " FROM ".$this->from;
154  if (!$user->rights->societe->client->voir && !$this->socid) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
155  $sql.= " WHERE f.datef BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
156  $sql.= " AND ".$this->where;
157  $sql.= " GROUP BY dm";
158  $sql.= $this->db->order('dm','DESC');
159 
160  $res=$this->_getAmountByMonth($year, $sql, $format);
161  //var_dump($res);print '<br>';
162  return $res;
163  }
164 
171  function getAverageByMonth($year)
172  {
173  global $user;
174 
175  $sql = "SELECT date_format(datef,'%m') as dm, AVG(f.".$this->field.")";
176  $sql.= " FROM ".$this->from;
177  if (!$user->rights->societe->client->voir && !$this->socid) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
178  $sql.= " WHERE f.datef BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
179  $sql.= " AND ".$this->where;
180  $sql.= " GROUP BY dm";
181  $sql.= $this->db->order('dm','DESC');
182 
183  return $this->_getAverageByMonth($year, $sql);
184  }
185 
191  function getAllByYear()
192  {
193  global $user;
194 
195  $sql = "SELECT date_format(datef,'%Y') as year, COUNT(*) as nb, SUM(f.".$this->field.") as total, AVG(f.".$this->field.") as avg";
196  $sql.= " FROM ".$this->from;
197  if (!$user->rights->societe->client->voir && !$this->socid) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
198  $sql.= " WHERE ".$this->where;
199  $sql.= " GROUP BY year";
200  $sql.= $this->db->order('year','DESC');
201 
202  return $this->_getAllByYear($sql);
203  }
204 
211  function getAllByProduct($year)
212  {
213  global $user;
214 
215  $sql = "SELECT product.ref, COUNT(product.ref) as nb, SUM(tl.".$this->field_line.") as total, AVG(tl.".$this->field_line.") as avg";
216  $sql.= " FROM ".$this->from.", ".$this->from_line.", ".MAIN_DB_PREFIX."product as product";
217  if (!$user->rights->societe->client->voir && !$this->socid) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
218  $sql.= " WHERE ".$this->where;
219  $sql.= " AND f.rowid = tl.fk_facture AND tl.fk_product = product.rowid";
220  $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))."'";
221  $sql.= " GROUP BY product.ref";
222  $sql.= $this->db->order('nb','DESC');
223  //$sql.= $this->db->plimit(20);
224 
225  return $this->_getAllByProduct($sql);
226  }
227 }
228 
Parent class of statistics class.
Definition: stats.class.php:30
getNbByMonth($year, $format=0)
Return orders number by month for a 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:453
getAverageByMonth($year)
Return average amount.
Class to manage suppliers invoices.
_getNbByMonth($year, $sql, $format=0)
Renvoie le nombre de proposition par mois pour une annee donnee.
getAllByProduct($year)
Return nb, amount of predefined product for year.
__construct($db, $socid, $mode, $userid=0)
Constructor.
_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:467
_getAmountByMonth($year, $sql, $format=0)
Renvoie le nombre d&#39;element par mois pour une annee donnee.
_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.
Class to manage stats for invoices (customer and supplier)