dolibarr  16.0.5
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 $from_line;
48  public $field;
49  public $field_line;
50  public $categ_link;
51  public $where;
52  public $join;
53 
54 
65  public function __construct($db, $socid, $mode, $userid = 0, $typentid = 0, $categid = 0)
66  {
67  global $user, $conf;
68 
69  $this->db = $db;
70 
71  $this->socid = ($socid > 0 ? $socid : 0);
72  $this->userid = $userid;
73  $this->cachefilesuffix = $mode;
74  $this->join = '';
75 
76  if ($mode == 'customer') {
77  $object = new Commande($this->db);
78  $this->from = MAIN_DB_PREFIX.$object->table_element." as c";
79  $this->from_line = MAIN_DB_PREFIX.$object->table_element_line." as tl";
80  $this->field = 'total_ht';
81  $this->field_line = 'total_ht';
82  //$this->where .= " c.fk_statut > 0"; // Not draft and not cancelled
83  $this->categ_link = MAIN_DB_PREFIX.'categorie_societe';
84  } elseif ($mode == 'supplier') {
85  $object = new CommandeFournisseur($this->db);
86  $this->from = MAIN_DB_PREFIX.$object->table_element." as c";
87  $this->from_line = MAIN_DB_PREFIX.$object->table_element_line." as tl";
88  $this->field = 'total_ht';
89  $this->field_line = 'total_ht';
90  //$this->where .= " c.fk_statut > 2"; // Only approved & ordered
91  $this->categ_link = MAIN_DB_PREFIX.'categorie_fournisseur';
92  }
93  //$this->where.= " AND c.fk_soc = s.rowid AND c.entity = ".$conf->entity;
94  $this->where .= ($this->where ? ' AND ' : '').'c.entity IN ('.getEntity('commande').')';
95 
96  if ($this->socid) {
97  $this->where .= " AND c.fk_soc = ".((int) $this->socid);
98  }
99  if ($this->userid > 0) {
100  $this->where .= ' AND c.fk_user_author = '.((int) $this->userid);
101  }
102 
103  if ($typentid) {
104  $this->join .= ' LEFT JOIN '.MAIN_DB_PREFIX.'societe as s ON s.rowid = c.fk_soc';
105  $this->where .= ' AND s.fk_typent = '.((int) $typentid);
106  }
107 
108  if ($categid) {
109  $this->join .= ' LEFT JOIN '.$this->categ_link.' as cats ON cats.fk_soc = c.fk_soc';
110  $this->join .= ' LEFT JOIN '.MAIN_DB_PREFIX.'categorie as cat ON cat.rowid = cats.fk_categorie';
111  $this->where .= ' AND cat.rowid = '.((int) $categid);
112  }
113  }
114 
122  public function getNbByMonth($year, $format = 0)
123  {
124  global $user;
125 
126  $sql = "SELECT date_format(c.date_commande,'%m') as dm, COUNT(*) as nb";
127  $sql .= " FROM ".$this->from;
128  if (empty($user->rights->societe->client->voir) && !$this->socid) {
129  $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
130  }
131  $sql .= $this->join;
132  $sql .= " WHERE c.date_commande BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
133  $sql .= " AND ".$this->where;
134  $sql .= " GROUP BY dm";
135  $sql .= $this->db->order('dm', 'DESC');
136 
137  $res = $this->_getNbByMonth($year, $sql, $format);
138  return $res;
139  }
140 
147  public function getNbByYear()
148  {
149  global $user;
150 
151  $sql = "SELECT date_format(c.date_commande,'%Y') as dm, COUNT(*) as nb, SUM(c.".$this->field.")";
152  $sql .= " FROM ".$this->from;
153  if (empty($user->rights->societe->client->voir) && !$this->socid) {
154  $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
155  }
156  $sql .= $this->join;
157  $sql .= " WHERE ".$this->where;
158  $sql .= " GROUP BY dm";
159  $sql .= $this->db->order('dm', 'DESC');
160 
161  return $this->_getNbByYear($sql);
162  }
163 
171  public function getAmountByMonth($year, $format = 0)
172  {
173  global $user;
174 
175  $sql = "SELECT date_format(c.date_commande,'%m') as dm, SUM(c.".$this->field.")";
176  $sql .= " FROM ".$this->from;
177  if (empty($user->rights->societe->client->voir) && !$this->socid) {
178  $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
179  }
180  $sql .= $this->join;
181  $sql .= " WHERE c.date_commande BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
182  $sql .= " AND ".$this->where;
183  $sql .= " GROUP BY dm";
184  $sql .= $this->db->order('dm', 'DESC');
185 
186  $res = $this->_getAmountByMonth($year, $sql, $format);
187  return $res;
188  }
189 
196  public function getAverageByMonth($year)
197  {
198  global $user;
199 
200  $sql = "SELECT date_format(c.date_commande,'%m') as dm, AVG(c.".$this->field.")";
201  $sql .= " FROM ".$this->from;
202  if (empty($user->rights->societe->client->voir) && !$this->socid) {
203  $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
204  }
205  $sql .= $this->join;
206  $sql .= " WHERE c.date_commande BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
207  $sql .= " AND ".$this->where;
208  $sql .= " GROUP BY dm";
209  $sql .= $this->db->order('dm', 'DESC');
210 
211  return $this->_getAverageByMonth($year, $sql);
212  }
213 
219  public function getAllByYear()
220  {
221  global $user;
222 
223  $sql = "SELECT date_format(c.date_commande,'%Y') as year, COUNT(*) as nb, SUM(c.".$this->field.") as total, AVG(".$this->field.") as avg";
224  $sql .= " FROM ".$this->from;
225  if (empty($user->rights->societe->client->voir) && !$this->socid) {
226  $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
227  }
228  $sql .= $this->join;
229  $sql .= " WHERE ".$this->where;
230  $sql .= " GROUP BY year";
231  $sql .= $this->db->order('year', 'DESC');
232 
233  return $this->_getAllByYear($sql);
234  }
235 
243  public function getAllByProduct($year, $limit = 10)
244  {
245  global $user;
246 
247  $sql = "SELECT product.ref, COUNT(product.ref) as nb, SUM(tl.".$this->field_line.") as total, AVG(tl.".$this->field_line.") as avg";
248  $sql .= " FROM ".$this->from;
249  $sql .= " INNER JOIN ".$this->from_line." ON c.rowid = tl.fk_commande ";
250  $sql .= " INNER JOIN ".MAIN_DB_PREFIX."product as product ON tl.fk_product = product.rowid";
251  if (empty($user->rights->societe->client->voir) && !$user->socid) {
252  $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
253  }
254  $sql .= $this->join;
255  $sql .= " WHERE ".$this->where;
256  $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))."'";
257  $sql .= " GROUP BY product.ref";
258  $sql .= $this->db->order('nb', 'DESC');
259  //$sql.= $this->db->plimit(20);
260 
261  return $this->_getAllByProduct($sql, $limit);
262  }
263 }
db
$conf db
API class for accounts.
Definition: inc.php:41
Stats\_getAllByProduct
_getAllByProduct($sql, $limit=10)
Return number or total of product refs.
Definition: stats.class.php:607
CommandeStats\getNbByYear
getNbByYear()
Return orders number per year.
Definition: commandestats.class.php:147
CommandeStats\getAmountByMonth
getAmountByMonth($year, $format=0)
Return the orders amount by month for a year.
Definition: commandestats.class.php:171
CommandeStats\getAllByProduct
getAllByProduct($year, $limit=10)
Return nb, amount of predefined product for year.
Definition: commandestats.class.php:243
Stats
Parent class of statistics class.
Definition: stats.class.php:30
Stats\_getNbByYear
_getNbByYear($sql)
Return nb of elements by year.
Definition: stats.class.php:355
Stats\_getAmountByMonth
_getAmountByMonth($year, $sql, $format=0)
Return the amount per month for a given year.
Definition: stats.class.php:492
getEntity
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.
Definition: functions.lib.php:148
Commande
Class to manage customers orders.
Definition: commande.class.php:46
Stats\_getAllByYear
_getAllByYear($sql)
Return nb of elements, total amount and avg amount each year.
Definition: stats.class.php:384
CommandeStats
Class to manage order statistics (customer and supplier)
Definition: commandestats.class.php:36
dol_get_first_day
dol_get_first_day($year, $month=1, $gm=false)
Return GMT time for first day of a month or year.
Definition: date.lib.php:551
dol_get_last_day
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
Definition: date.lib.php:570
CommandeFournisseur
Class to manage predefined suppliers products.
Definition: fournisseur.commande.class.php:47
CommandeStats\__construct
__construct($db, $socid, $mode, $userid=0, $typentid=0, $categid=0)
Constructor.
Definition: commandestats.class.php:65
Stats\_getNbByMonth
_getNbByMonth($year, $sql, $format=0)
Renvoie le nombre de documents par mois pour une annee donnee Return number of documents per month fo...
Definition: stats.class.php:435
CommandeStats\getAverageByMonth
getAverageByMonth($year)
Return the orders amount average by month for a year.
Definition: commandestats.class.php:196
CommandeStats\getAllByYear
getAllByYear()
Return nb, total and average.
Definition: commandestats.class.php:219
CommandeStats\getNbByMonth
getNbByMonth($year, $format=0)
Return orders number by month for a year.
Definition: commandestats.class.php:122
Stats\_getAverageByMonth
_getAverageByMonth($year, $sql, $format=0)
Renvoie le montant moyen par mois pour une annee donnee Return the amount average par month for a giv...
Definition: stats.class.php:550