dolibarr  7.0.0-beta
commandestats.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-2012 Regis Houssin <regis.houssin@capnetworks.com>
5  * Copyright (C) 2012 Marcos GarcĂ­a <marcosgdf@gmail.com>
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 <http://www.gnu.org/licenses/>.
19  */
20 
26 include_once DOL_DOCUMENT_ROOT . '/core/class/stats.class.php';
27 include_once DOL_DOCUMENT_ROOT . '/commande/class/commande.class.php';
28 include_once DOL_DOCUMENT_ROOT . '/fourn/class/fournisseur.commande.class.php';
29 include_once DOL_DOCUMENT_ROOT . '/core/lib/date.lib.php';
30 
31 
35 class CommandeStats extends Stats
36 {
37  public $table_element;
38 
39  var $socid;
40  var $userid;
41 
42  var $from;
43  var $field;
44  var $where;
45 
46 
55  function __construct($db, $socid, $mode, $userid=0)
56  {
57  global $user, $conf;
58 
59  $this->db = $db;
60 
61  $this->socid = ($socid > 0 ? $socid : 0);
62  $this->userid = $userid;
63  $this->cachefilesuffix = $mode;
64 
65  if ($mode == 'customer')
66  {
67  $object=new Commande($this->db);
68  $this->from = MAIN_DB_PREFIX.$object->table_element." as c";
69  $this->from_line = MAIN_DB_PREFIX.$object->table_element_line." as tl";
70  $this->field='total_ht';
71  $this->field_line='total_ht';
72  $this->where.= " c.fk_statut > 0"; // Not draft and not cancelled
73  }
74  if ($mode == 'supplier')
75  {
76  $object=new CommandeFournisseur($this->db);
77  $this->from = MAIN_DB_PREFIX.$object->table_element." as c";
78  $this->from_line = MAIN_DB_PREFIX.$object->table_element_line." as tl";
79  $this->field='total_ht';
80  $this->field_line='total_ht';
81  $this->where.= " c.fk_statut > 2"; // Only approved & ordered
82  }
83  //$this->where.= " AND c.fk_soc = s.rowid AND c.entity = ".$conf->entity;
84  $this->where.= ' AND c.entity IN ('.getEntity('commande').')';
85 
86  if (!$user->rights->societe->client->voir && !$this->socid) $this->where .= " AND c.fk_soc = sc.fk_soc AND sc.fk_user = " .$user->id;
87  if ($this->socid)
88  {
89  $this->where.=" AND c.fk_soc = ".$this->socid;
90  }
91  if ($this->userid > 0) $this->where.=' AND c.fk_user_author = '.$this->userid;
92  }
93 
101  function getNbByMonth($year, $format=0)
102  {
103  global $user;
104 
105  $sql = "SELECT date_format(c.date_commande,'%m') as dm, COUNT(*) as nb";
106  $sql.= " FROM ".$this->from;
107  if (!$user->rights->societe->client->voir && !$this->socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
108  $sql.= " WHERE c.date_commande BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
109  $sql.= " AND ".$this->where;
110  $sql.= " GROUP BY dm";
111  $sql.= $this->db->order('dm','DESC');
112 
113  $res=$this->_getNbByMonth($year, $sql, $format);
114  return $res;
115  }
116 
123  function getNbByYear()
124  {
125  global $user;
126 
127  $sql = "SELECT date_format(c.date_commande,'%Y') as dm, COUNT(*) as nb, SUM(c.".$this->field.")";
128  $sql.= " FROM ".$this->from;
129  if (!$user->rights->societe->client->voir && !$this->socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
130  $sql.= " WHERE ".$this->where;
131  $sql.= " GROUP BY dm";
132  $sql.= $this->db->order('dm','DESC');
133 
134  return $this->_getNbByYear($sql);
135  }
136 
144  function getAmountByMonth($year, $format=0)
145  {
146  global $user;
147 
148  $sql = "SELECT date_format(c.date_commande,'%m') as dm, SUM(c.".$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 c.date_commande 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  return $res;
158  }
159 
166  function getAverageByMonth($year)
167  {
168  global $user;
169 
170  $sql = "SELECT date_format(c.date_commande,'%m') as dm, AVG(c.".$this->field.")";
171  $sql.= " FROM ".$this->from;
172  if (!$user->rights->societe->client->voir && !$this->socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
173  $sql.= " WHERE c.date_commande BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
174  $sql.= " AND ".$this->where;
175  $sql.= " GROUP BY dm";
176  $sql.= $this->db->order('dm','DESC');
177 
178  return $this->_getAverageByMonth($year, $sql);
179  }
180 
186  function getAllByYear()
187  {
188  global $user;
189 
190  $sql = "SELECT date_format(c.date_commande,'%Y') as year, COUNT(*) as nb, SUM(c.".$this->field.") as total, AVG(".$this->field.") as avg";
191  $sql.= " FROM ".$this->from;
192  if (!$user->rights->societe->client->voir && !$this->socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
193  $sql.= " WHERE ".$this->where;
194  $sql.= " GROUP BY year";
195  $sql.= $this->db->order('year','DESC');
196 
197  return $this->_getAllByYear($sql);
198  }
199 
206  function getAllByProduct($year)
207  {
208  global $user;
209 
210  $sql = "SELECT product.ref, COUNT(product.ref) as nb, SUM(tl.".$this->field_line.") as total, AVG(tl.".$this->field_line.") as avg";
211  $sql.= " FROM ".$this->from.", ".$this->from_line.", ".MAIN_DB_PREFIX."product as product";
212  if (!$user->rights->societe->client->voir && !$user->societe_id) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
213  $sql.= " WHERE ".$this->where;
214  $sql.= " AND c.rowid = tl.fk_commande AND tl.fk_product = product.rowid";
215  $sql.= " AND c.date_commande BETWEEN '".$this->db->idate(dol_get_first_day($year,1,false))."' AND '".$this->db->idate(dol_get_last_day($year,12,false))."'";
216  $sql.= " GROUP BY product.ref";
217  $sql.= $this->db->order('nb','DESC');
218  //$sql.= $this->db->plimit(20);
219 
220  return $this->_getAllByProduct($sql);
221  }
222 
223 }
224 
getAllByYear()
Return nb, total and average.
Parent class of statistics class.
Definition: stats.class.php:30
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
_getNbByMonth($year, $sql, $format=0)
Renvoie le nombre de proposition par mois pour une annee donnee.
_getAllByProduct($sql, $limit=10)
Return number or total of product refs.
__construct($db, $socid, $mode, $userid=0)
Constructor.
Class to manage customers orders.
getNbByYear()
Return orders number per year.
_getAllByYear($sql)
Return nb of elements, total amount and avg amount each year.
Class to manage predefined suppliers products.
Class to manage order statistics (customer and supplier)
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.
getAmountByMonth($year, $format=0)
Return the orders amount by month for a year.
getAverageByMonth($year)
Return the orders amount average by month for a year.
_getAverageByMonth($year, $sql, $format=0)
Renvoie le montant moyen par mois pour une annee donnee.
getAllByProduct($year)
Return nb, amount of predefined product for year.
getNbByMonth($year, $format=0)
Return orders number by month for a year.