dolibarr  7.0.0-beta
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@capnetworks.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 
38  public $table_element;
39  var $from;
40  var $field;
41  var $where;
42 
43 
52  function __construct($db, $socid, $mode, $userid=0)
53  {
54  global $user, $conf;
55 
56  $this->db = $db;
57  $this->socid = ($socid > 0 ? $socid : 0);
58  $this->userid = $userid;
59  $this->cachefilesuffix = $mode;
60 
61  if ($mode == 'customer')
62  {
63  $object=new Facture($this->db);
64  $this->from = MAIN_DB_PREFIX.$object->table_element." as f";
65  $this->from_line = MAIN_DB_PREFIX.$object->table_element_line." as tl";
66  $this->field='total';
67  $this->field_line='total_ht';
68  }
69  if ($mode == 'supplier')
70  {
71  $object=new FactureFournisseur($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 
78  $this->where = " f.fk_statut > 0";
79  $this->where.= " AND f.entity = ".$conf->entity;
80  if (!$user->rights->societe->client->voir && !$this->socid) $this->where .= " AND f.fk_soc = sc.fk_soc AND sc.fk_user = " .$user->id;
81  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)
82  if ($this->socid)
83  {
84  $this->where.=" AND f.fk_soc = ".$this->socid;
85  }
86  if ($this->userid > 0) $this->where.=' AND f.fk_user_author = '.$this->userid;
87  if (! empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) $this->where.= " AND f.type IN (0,1,2)";
88  else $this->where.= " AND f.type IN (0,1,2,3)";
89  }
90 
91 
99  function getNbByMonth($year, $format=0)
100  {
101  global $user;
102 
103  $sql = "SELECT date_format(f.datef,'%m') as dm, COUNT(*) as nb";
104  $sql.= " FROM ".$this->from;
105  if (!$user->rights->societe->client->voir && !$this->socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
106  $sql.= " WHERE f.datef BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
107  $sql.= " AND ".$this->where;
108  $sql.= " GROUP BY dm";
109  $sql.= $this->db->order('dm','DESC');
110 
111  $res=$this->_getNbByMonth($year, $sql, $format);
112  //var_dump($res);print '<br>';
113  return $res;
114  }
115 
116 
122  function getNbByYear()
123  {
124  global $user;
125 
126  $sql = "SELECT date_format(f.datef,'%Y') as dm, COUNT(*), SUM(c.".$this->field.")";
127  $sql.= " FROM ".$this->from;
128  if (!$user->rights->societe->client->voir && !$this->socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
129  $sql.= " WHERE ".$this->where;
130  $sql.= " GROUP BY dm";
131  $sql.= $this->db->order('dm','DESC');
132 
133  return $this->_getNbByYear($sql);
134  }
135 
136 
144  function getAmountByMonth($year, $format=0)
145  {
146  global $user;
147 
148  $sql = "SELECT date_format(datef,'%m') as dm, SUM(f.".$this->field.")";
149  $sql.= " FROM ".$this->from;
150  if (!$user->rights->societe->client->voir && !$this->socid) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
151  $sql.= " WHERE f.datef BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
152  $sql.= " AND ".$this->where;
153  $sql.= " GROUP BY dm";
154  $sql.= $this->db->order('dm','DESC');
155 
156  $res=$this->_getAmountByMonth($year, $sql, $format);
157  //var_dump($res);print '<br>';
158  return $res;
159  }
160 
167  function getAverageByMonth($year)
168  {
169  global $user;
170 
171  $sql = "SELECT date_format(datef,'%m') as dm, AVG(f.".$this->field.")";
172  $sql.= " FROM ".$this->from;
173  if (!$user->rights->societe->client->voir && !$this->socid) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
174  $sql.= " WHERE f.datef BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
175  $sql.= " AND ".$this->where;
176  $sql.= " GROUP BY dm";
177  $sql.= $this->db->order('dm','DESC');
178 
179  return $this->_getAverageByMonth($year, $sql);
180  }
181 
187  function getAllByYear()
188  {
189  global $user;
190 
191  $sql = "SELECT date_format(datef,'%Y') as year, COUNT(*) as nb, SUM(f.".$this->field.") as total, AVG(f.".$this->field.") as avg";
192  $sql.= " FROM ".$this->from;
193  if (!$user->rights->societe->client->voir && !$this->socid) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
194  $sql.= " WHERE ".$this->where;
195  $sql.= " GROUP BY year";
196  $sql.= $this->db->order('year','DESC');
197 
198  return $this->_getAllByYear($sql);
199  }
200 
207  function getAllByProduct($year)
208  {
209  global $user;
210 
211  $sql = "SELECT product.ref, COUNT(product.ref) as nb, SUM(tl.".$this->field_line.") as total, AVG(tl.".$this->field_line.") as avg";
212  $sql.= " FROM ".$this->from.", ".$this->from_line.", ".MAIN_DB_PREFIX."product as product";
213  if (!$user->rights->societe->client->voir && !$this->socid) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
214  $sql.= " WHERE ".$this->where;
215  $sql.= " AND f.rowid = tl.fk_facture AND tl.fk_product = product.rowid";
216  $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))."'";
217  $sql.= " GROUP BY product.ref";
218  $sql.= $this->db->order('nb','DESC');
219  //$sql.= $this->db->plimit(20);
220 
221  return $this->_getAllByProduct($sql);
222  }
223 
224 
225 }
226 
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:445
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:459
_getAmountByMonth($year, $sql, $format=0)
Renvoie le nombre d'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)