dolibarr  7.0.0-beta
expeditionstats.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  * Copyright (C) 2011 Juanjo Menent <jmenent@2byte.es>
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 
27 include_once DOL_DOCUMENT_ROOT . '/core/class/stats.class.php';
28 include_once DOL_DOCUMENT_ROOT . '/expedition/class/expedition.class.php';
29 include_once DOL_DOCUMENT_ROOT . '/core/lib/date.lib.php';
30 
31 
35 class ExpeditionStats 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  $object=new Expedition($this->db);
66  $this->from = MAIN_DB_PREFIX.$object->table_element." as c";
67  //$this->from.= ", ".MAIN_DB_PREFIX."societe as s";
68  $this->field='weight'; // Warning, unit of weight is NOT USED AND MUST BE
69  $this->where.= " c.fk_statut > 0"; // Not draft and not cancelled
70 
71  //$this->where.= " AND c.fk_soc = s.rowid AND c.entity = ".$conf->entity;
72  $this->where.= " AND c.entity = ".$conf->entity;
73  if (!$user->rights->societe->client->voir && !$this->socid) $this->where .= " AND c.fk_soc = sc.fk_soc AND sc.fk_user = " .$user->id;
74  if ($this->socid)
75  {
76  $this->where.=" AND c.fk_soc = ".$this->socid;
77  }
78  if ($this->userid > 0) $this->where.=' AND c.fk_user_author = '.$this->userid;
79  }
80 
88  function getNbByMonth($year, $format=0)
89  {
90  global $user;
91 
92  $sql = "SELECT date_format(c.date_valid,'%m') as dm, COUNT(*) as nb";
93  $sql.= " FROM ".$this->from;
94  if (!$user->rights->societe->client->voir && !$this->socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
95  $sql.= " WHERE c.date_valid BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
96  $sql.= " AND ".$this->where;
97  $sql.= " GROUP BY dm";
98  $sql.= $this->db->order('dm','DESC');
99 
100  $res=$this->_getNbByMonth($year, $sql, $format);
101  return $res;
102  }
103 
110  function getNbByYear()
111  {
112  global $user;
113 
114  $sql = "SELECT date_format(c.date_valid,'%Y') as dm, COUNT(*) as nb, SUM(c.".$this->field.")";
115  $sql.= " FROM ".$this->from;
116  if (!$user->rights->societe->client->voir && !$this->socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
117  $sql.= " WHERE ".$this->where;
118  $sql.= " GROUP BY dm";
119  $sql.= $this->db->order('dm','DESC');
120 
121  return $this->_getNbByYear($sql);
122  }
123 
129  function getAllByYear()
130  {
131  global $user;
132 
133  $sql = "SELECT date_format(c.date_valid,'%Y') as year, COUNT(*) as nb, SUM(c.".$this->field.") as total, AVG(".$this->field.") as avg";
134  $sql.= " FROM ".$this->from;
135  if (!$user->rights->societe->client->voir && !$this->socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
136  $sql.= " WHERE ".$this->where;
137  $sql.= " GROUP BY year";
138  $sql.= $this->db->order('year','DESC');
139 
140  return $this->_getAllByYear($sql);
141  }
142 }
143 
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
getNbByYear()
Return shipments number per year.
_getNbByMonth($year, $sql, $format=0)
Renvoie le nombre de proposition par mois pour une annee donnee.
getNbByMonth($year, $format=0)
Return shipment number by month for a year.
Class to manage shipments.
_getAllByYear($sql)
Return nb of elements, total amount and avg amount each 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
__construct($db, $socid, $mode, $userid=0)
Constructor.
_getNbByYear($sql)
Return nb of elements by year.
Class to manage shipment statistics.
getAllByYear()
Return nb, total and average.