dolibarr  13.0.2
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  * Copyright (C) 2020 Maxime DEMAREST <maxime@indelog.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <https://www.gnu.org/licenses/>.
20  */
21 
27 include_once DOL_DOCUMENT_ROOT.'/core/class/stats.class.php';
28 include_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php';
29 include_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.commande.class.php';
30 include_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
31 
32 
36 class CommandeStats extends Stats
37 {
41  public $table_element;
42 
43  public $socid;
44  public $userid;
45 
46  public $from;
47  public $field;
48  public $where;
49  public $join;
50 
51 
62  public function __construct($db, $socid, $mode, $userid = 0, $typentid = 0, $categid = 0)
63  {
64  global $user, $conf;
65 
66  $this->db = $db;
67 
68  $this->socid = ($socid > 0 ? $socid : 0);
69  $this->userid = $userid;
70  $this->cachefilesuffix = $mode;
71  $this->join = '';
72 
73  if ($mode == 'customer')
74  {
75  $object = new Commande($this->db);
76  $this->from = MAIN_DB_PREFIX.$object->table_element." as c";
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  //$this->where .= " c.fk_statut > 0"; // Not draft and not cancelled
81  } elseif ($mode == 'supplier')
82  {
83  $object = new CommandeFournisseur($this->db);
84  $this->from = MAIN_DB_PREFIX.$object->table_element." as c";
85  $this->from_line = MAIN_DB_PREFIX.$object->table_element_line." as tl";
86  $this->field = 'total_ht';
87  $this->field_line = 'total_ht';
88  //$this->where .= " c.fk_statut > 2"; // Only approved & ordered
89  }
90  //$this->where.= " AND c.fk_soc = s.rowid AND c.entity = ".$conf->entity;
91  $this->where .= ($this->where ? ' AND ' : '').'c.entity IN ('.getEntity('commande').')';
92 
93  if (!$user->rights->societe->client->voir && !$this->socid) $this->where .= " AND c.fk_soc = sc.fk_soc AND sc.fk_user = ".$user->id;
94  if ($this->socid)
95  {
96  $this->where .= " AND c.fk_soc = ".$this->socid;
97  }
98  if ($this->userid > 0) $this->where .= ' AND c.fk_user_author = '.$this->userid;
99 
100  if ($typentid)
101  {
102  $this->join .= ' LEFT JOIN '.MAIN_DB_PREFIX.'societe as s ON s.rowid = c.fk_soc';
103  $this->where .= ' AND s.fk_typent = '.$typentid;
104  }
105 
106  if ($categid)
107  {
108  $this->join .= ' LEFT JOIN '.MAIN_DB_PREFIX.'categorie_societe as cats ON cats.fk_soc = c.fk_soc';
109  $this->join .= ' LEFT JOIN '.MAIN_DB_PREFIX.'categorie as cat ON cat.rowid = cats.fk_categorie';
110  $this->where .= ' AND cat.rowid = '.$categid;
111  }
112  }
113 
121  public function getNbByMonth($year, $format = 0)
122  {
123  global $user;
124 
125  $sql = "SELECT date_format(c.date_commande,'%m') as dm, COUNT(*) as nb";
126  $sql .= " FROM ".$this->from;
127  if (!$user->rights->societe->client->voir && !$this->socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
128  $sql .= $this->join;
129  $sql .= " WHERE c.date_commande BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
130  $sql .= " AND ".$this->where;
131  $sql .= " GROUP BY dm";
132  $sql .= $this->db->order('dm', 'DESC');
133 
134  $res = $this->_getNbByMonth($year, $sql, $format);
135  return $res;
136  }
137 
144  public function getNbByYear()
145  {
146  global $user;
147 
148  $sql = "SELECT date_format(c.date_commande,'%Y') as dm, COUNT(*) as nb, 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 .= $this->join;
152  $sql .= " WHERE ".$this->where;
153  $sql .= " GROUP BY dm";
154  $sql .= $this->db->order('dm', 'DESC');
155 
156  return $this->_getNbByYear($sql);
157  }
158 
166  public function getAmountByMonth($year, $format = 0)
167  {
168  global $user;
169 
170  $sql = "SELECT date_format(c.date_commande,'%m') as dm, SUM(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 .= $this->join;
174  $sql .= " WHERE c.date_commande 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  $res = $this->_getAmountByMonth($year, $sql, $format);
180  return $res;
181  }
182 
189  public function getAverageByMonth($year)
190  {
191  global $user;
192 
193  $sql = "SELECT date_format(c.date_commande,'%m') as dm, AVG(c.".$this->field.")";
194  $sql .= " FROM ".$this->from;
195  if (!$user->rights->societe->client->voir && !$this->socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
196  $sql .= $this->join;
197  $sql .= " WHERE c.date_commande BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
198  $sql .= " AND ".$this->where;
199  $sql .= " GROUP BY dm";
200  $sql .= $this->db->order('dm', 'DESC');
201 
202  return $this->_getAverageByMonth($year, $sql);
203  }
204 
210  public function getAllByYear()
211  {
212  global $user;
213 
214  $sql = "SELECT date_format(c.date_commande,'%Y') as year, COUNT(*) as nb, SUM(c.".$this->field.") as total, AVG(".$this->field.") as avg";
215  $sql .= " FROM ".$this->from;
216  if (!$user->rights->societe->client->voir && !$this->socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
217  $sql .= $this->join;
218  $sql .= " WHERE ".$this->where;
219  $sql .= " GROUP BY year";
220  $sql .= $this->db->order('year', 'DESC');
221 
222  return $this->_getAllByYear($sql);
223  }
224 
232  public function getAllByProduct($year, $limit = 10)
233  {
234  global $user;
235 
236  $sql = "SELECT product.ref, COUNT(product.ref) as nb, SUM(tl.".$this->field_line.") as total, AVG(tl.".$this->field_line.") as avg";
237  $sql .= " FROM ".$this->from.", ".$this->from_line.", ".MAIN_DB_PREFIX."product as product";
238  if (!$user->rights->societe->client->voir && !$user->socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
239  $sql .= $this->join;
240  $sql .= " WHERE ".$this->where;
241  $sql .= " AND c.rowid = tl.fk_commande AND tl.fk_product = product.rowid";
242  $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))."'";
243  $sql .= " GROUP BY product.ref";
244  $sql .= $this->db->order('nb', 'DESC');
245  //$sql.= $this->db->plimit(20);
246 
247  return $this->_getAllByProduct($sql, $limit);
248  }
249 }
getAllByYear()
Return nb, total and average.
__construct($db, $socid, $mode, $userid=0, $typentid=0, $categid=0)
Constructor.
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:481
_getNbByMonth($year, $sql, $format=0)
Renvoie le nombre de documents par mois pour une annee donnee Return number of documents per month fo...
$conf db
API class for accounts.
Definition: inc.php:54
_getAllByProduct($sql, $limit=10)
Return number or total of product refs.
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.
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:498
_getAmountByMonth($year, $sql, $format=0)
Return the amount per month for a given year.
_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 Return the amount average par month for a giv...
getAllByProduct($year, $limit=10)
Return nb, amount of predefined product for year.
getNbByMonth($year, $format=0)
Return orders number by month for a year.