dolibarr  17.0.3
receptionstats.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 <https://www.gnu.org/licenses/>.
19  */
20 
27 include_once DOL_DOCUMENT_ROOT.'/core/class/stats.class.php';
28 include_once DOL_DOCUMENT_ROOT.'/reception/class/reception.class.php';
29 include_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
30 
31 
35 class ReceptionStats extends Stats
36 {
37  public $table_element;
38 
39  public $socid;
40  public $userid;
41 
42  public $from;
43  public $field;
44  public $where;
45 
46 
55  public 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 Reception($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 IN (".getEntity('reception').")";
73  if (empty($user->rights->societe->client->voir) && !$this->socid) {
74  $this->where .= " AND c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
75  }
76  if ($this->socid) {
77  $this->where .= " AND c.fk_soc = ".((int) $this->socid);
78  }
79  if ($this->userid > 0) {
80  $this->where .= ' AND c.fk_user_author = '.((int) $this->userid);
81  }
82  }
83 
91  public function getNbByMonth($year, $format = 0)
92  {
93  global $user;
94 
95  $sql = "SELECT date_format(c.date_valid,'%m') as dm, COUNT(*) as nb";
96  $sql .= " FROM ".$this->from;
97  if (empty($user->rights->societe->client->voir) && !$this->socid) {
98  $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
99  }
100  $sql .= " WHERE c.date_valid BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
101  $sql .= " AND ".$this->where;
102  $sql .= " GROUP BY dm";
103  $sql .= $this->db->order('dm', 'DESC');
104 
105  $res = $this->_getNbByMonth($year, $sql, $format);
106  return $res;
107  }
108 
115  public function getNbByYear()
116  {
117  global $user;
118 
119  $sql = "SELECT date_format(c.date_valid,'%Y') as dm, COUNT(*) as nb, SUM(c.".$this->field.")";
120  $sql .= " FROM ".$this->from;
121  if (empty($user->rights->societe->client->voir) && !$this->socid) {
122  $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
123  }
124  $sql .= " WHERE ".$this->where;
125  $sql .= " GROUP BY dm";
126  $sql .= $this->db->order('dm', 'DESC');
127 
128  return $this->_getNbByYear($sql);
129  }
130 
138  public function getAmountByMonth($year, $format = 0)
139  {
140  global $user;
141 
142  $sql = "SELECT date_format(c.date_valid,'%m') as dm, SUM(c.".$this->field.")";
143  $sql .= " FROM ".$this->from;
144  if (empty($user->rights->societe->client->voir) && !$this->socid) {
145  $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
146  }
147  $sql .= " WHERE c.date_valid BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
148  $sql .= " AND ".$this->where;
149  $sql .= " GROUP BY dm";
150  $sql .= $this->db->order('dm', 'DESC');
151 
152  $res = $this->_getAmountByMonth($year, $sql, $format);
153  return $res;
154  }
155 
162  public function getAverageByMonth($year)
163  {
164  global $user;
165 
166  $sql = "SELECT date_format(c.date_valid,'%m') as dm, AVG(c.".$this->field.")";
167  $sql .= " FROM ".$this->from;
168  if (empty($user->rights->societe->client->voir) && !$this->socid) {
169  $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
170  }
171  $sql .= " WHERE c.date_valid BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
172  $sql .= " AND ".$this->where;
173  $sql .= " GROUP BY dm";
174  $sql .= $this->db->order('dm', 'DESC');
175 
176  return $this->_getAverageByMonth($year, $sql);
177  }
178 
184  public function getAllByYear()
185  {
186  global $user;
187 
188  $sql = "SELECT date_format(c.date_valid,'%Y') as year, COUNT(*) as nb, SUM(c.".$this->field.") as total, AVG(".$this->field.") as avg";
189  $sql .= " FROM ".$this->from;
190  if (empty($user->rights->societe->client->voir) && !$this->socid) {
191  $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
192  }
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 }
db
$conf db
API class for accounts.
Definition: inc.php:41
Reception
Class to manage receptions.
Definition: reception.class.php:50
ReceptionStats
Class to manage reception statistics.
Definition: receptionstats.class.php:35
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:357
Stats\_getAmountByMonth
_getAmountByMonth($year, $sql, $format=0)
Return the amount per month for a given year.
Definition: stats.class.php:494
ReceptionStats\__construct
__construct($db, $socid, $mode, $userid=0)
Constructor.
Definition: receptionstats.class.php:55
ReceptionStats\getNbByYear
getNbByYear()
Return receptions number per year.
Definition: receptionstats.class.php:115
Stats\_getAllByYear
_getAllByYear($sql)
Return nb of elements, total amount and avg amount each year.
Definition: stats.class.php:386
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:575
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:594
ReceptionStats\getAllByYear
getAllByYear()
Return nb, total and average.
Definition: receptionstats.class.php:184
ReceptionStats\getAmountByMonth
getAmountByMonth($year, $format=0)
Return the orders amount by month for a year.
Definition: receptionstats.class.php:138
ReceptionStats\getNbByMonth
getNbByMonth($year, $format=0)
Return reception number by month for a year.
Definition: receptionstats.class.php:91
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:437
ReceptionStats\getAverageByMonth
getAverageByMonth($year)
Return the orders amount average by month for a year.
Definition: receptionstats.class.php:162
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:552