dolibarr  9.0.0
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@inodbox.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 {
40  public $table_element;
41 
42  var $socid;
43  var $userid;
44 
45  var $from;
46  var $field;
47  var $where;
48 
49 
58  function __construct($db, $socid, $mode, $userid=0)
59  {
60  global $user, $conf;
61 
62  $this->db = $db;
63 
64  $this->socid = ($socid > 0 ? $socid : 0);
65  $this->userid = $userid;
66  $this->cachefilesuffix = $mode;
67 
68  if ($mode == 'customer')
69  {
70  $object=new Commande($this->db);
71  $this->from = MAIN_DB_PREFIX.$object->table_element." as c";
72  $this->from_line = MAIN_DB_PREFIX.$object->table_element_line." as tl";
73  $this->field='total_ht';
74  $this->field_line='total_ht';
75  $this->where.= " c.fk_statut > 0"; // Not draft and not cancelled
76  }
77  if ($mode == 'supplier')
78  {
79  $object=new CommandeFournisseur($this->db);
80  $this->from = MAIN_DB_PREFIX.$object->table_element." as c";
81  $this->from_line = MAIN_DB_PREFIX.$object->table_element_line." as tl";
82  $this->field='total_ht';
83  $this->field_line='total_ht';
84  $this->where.= " c.fk_statut > 2"; // Only approved & ordered
85  }
86  //$this->where.= " AND c.fk_soc = s.rowid AND c.entity = ".$conf->entity;
87  $this->where.= ' AND c.entity IN ('.getEntity('commande').')';
88 
89  if (!$user->rights->societe->client->voir && !$this->socid) $this->where .= " AND c.fk_soc = sc.fk_soc AND sc.fk_user = " .$user->id;
90  if ($this->socid)
91  {
92  $this->where.=" AND c.fk_soc = ".$this->socid;
93  }
94  if ($this->userid > 0) $this->where.=' AND c.fk_user_author = '.$this->userid;
95  }
96 
104  function getNbByMonth($year, $format=0)
105  {
106  global $user;
107 
108  $sql = "SELECT date_format(c.date_commande,'%m') as dm, COUNT(*) as nb";
109  $sql.= " FROM ".$this->from;
110  if (!$user->rights->societe->client->voir && !$this->socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
111  $sql.= " WHERE c.date_commande BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
112  $sql.= " AND ".$this->where;
113  $sql.= " GROUP BY dm";
114  $sql.= $this->db->order('dm','DESC');
115 
116  $res=$this->_getNbByMonth($year, $sql, $format);
117  return $res;
118  }
119 
126  function getNbByYear()
127  {
128  global $user;
129 
130  $sql = "SELECT date_format(c.date_commande,'%Y') as dm, COUNT(*) as nb, 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 
147  function getAmountByMonth($year, $format=0)
148  {
149  global $user;
150 
151  $sql = "SELECT date_format(c.date_commande,'%m') as dm, SUM(c.".$this->field.")";
152  $sql.= " FROM ".$this->from;
153  if (!$user->rights->societe->client->voir && !$this->socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
154  $sql.= " WHERE c.date_commande BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
155  $sql.= " AND ".$this->where;
156  $sql.= " GROUP BY dm";
157  $sql.= $this->db->order('dm','DESC');
158 
159  $res=$this->_getAmountByMonth($year, $sql, $format);
160  return $res;
161  }
162 
169  function getAverageByMonth($year)
170  {
171  global $user;
172 
173  $sql = "SELECT date_format(c.date_commande,'%m') as dm, AVG(c.".$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.= " WHERE c.date_commande BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
177  $sql.= " AND ".$this->where;
178  $sql.= " GROUP BY dm";
179  $sql.= $this->db->order('dm','DESC');
180 
181  return $this->_getAverageByMonth($year, $sql);
182  }
183 
189  function getAllByYear()
190  {
191  global $user;
192 
193  $sql = "SELECT date_format(c.date_commande,'%Y') as year, COUNT(*) as nb, SUM(c.".$this->field.") as total, AVG(".$this->field.") as avg";
194  $sql.= " FROM ".$this->from;
195  if (!$user->rights->societe->client->voir && !$this->socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
196  $sql.= " WHERE ".$this->where;
197  $sql.= " GROUP BY year";
198  $sql.= $this->db->order('year','DESC');
199 
200  return $this->_getAllByYear($sql);
201  }
202 
209  function getAllByProduct($year)
210  {
211  global $user;
212 
213  $sql = "SELECT product.ref, COUNT(product.ref) as nb, SUM(tl.".$this->field_line.") as total, AVG(tl.".$this->field_line.") as avg";
214  $sql.= " FROM ".$this->from.", ".$this->from_line.", ".MAIN_DB_PREFIX."product as product";
215  if (!$user->rights->societe->client->voir && !$user->societe_id) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
216  $sql.= " WHERE ".$this->where;
217  $sql.= " AND c.rowid = tl.fk_commande AND tl.fk_product = product.rowid";
218  $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))."'";
219  $sql.= " GROUP BY product.ref";
220  $sql.= $this->db->order('nb','DESC');
221  //$sql.= $this->db->plimit(20);
222 
223  return $this->_getAllByProduct($sql);
224  }
225 }
226 
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:453
_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: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.
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.