dolibarr 21.0.0-alpha
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@inodbox.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
27include_once DOL_DOCUMENT_ROOT.'/core/class/stats.class.php';
28include_once DOL_DOCUMENT_ROOT.'/expedition/class/expedition.class.php';
29include_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
30
31
36{
40 public $table_element;
41
45 public $socid;
46
50 public $userid;
51
55 public $from;
56
60 public $join;
61
65 public $field;
66
70 public $where;
71
72
81 public function __construct($db, $socid, $mode, $userid = 0)
82 {
83 global $user, $conf;
84
85 $this->db = $db;
86
87 $this->socid = ($socid > 0 ? $socid : 0);
88 $this->userid = $userid;
89 $this->cachefilesuffix = $mode;
90
91 $object = new Expedition($this->db);
92 $this->from = MAIN_DB_PREFIX.$object->table_element." as c";
93 //$this->from.= ", ".MAIN_DB_PREFIX."societe as s";
94 $this->field = 'weight'; // Warning, unit of weight is NOT USED AND MUST BE
95 $this->where .= " c.fk_statut > 0"; // Not draft and not cancelled
96
97 //$this->where.= " AND c.fk_soc = s.rowid AND c.entity = ".$conf->entity;
98 $this->where .= " AND c.entity = ".$conf->entity;
99 if (!$user->hasRight('societe', 'client', 'voir')) {
100 $this->where .= " AND c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
101 }
102 if ($this->socid) {
103 $this->where .= " AND c.fk_soc = ".((int) $this->socid);
104 }
105 if ($this->userid > 0) {
106 $this->where .= ' AND c.fk_user_author = '.((int) $this->userid);
107 }
108 }
109
117 public function getNbByMonth($year, $format = 0)
118 {
119 global $user;
120
121 $sql = "SELECT date_format(c.date_valid,'%m') as dm, COUNT(*) as nb";
122 $sql .= " FROM ".$this->from;
123 if (!$user->hasRight('societe', 'client', 'voir')) {
124 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
125 }
126 $sql .= " WHERE c.date_valid BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
127 $sql .= " AND ".$this->where;
128 $sql .= " GROUP BY dm";
129 $sql .= $this->db->order('dm', 'DESC');
130
131 $res = $this->_getNbByMonth($year, $sql, $format);
132 return $res;
133 }
134
141 public function getNbByYear()
142 {
143 global $user;
144
145 $sql = "SELECT date_format(c.date_valid,'%Y') as dm, COUNT(*) as nb, SUM(c.".$this->field.")";
146 $sql .= " FROM ".$this->from;
147 if (!$user->hasRight('societe', 'client', 'voir')) {
148 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
149 }
150 $sql .= " WHERE ".$this->where;
151 $sql .= " GROUP BY dm";
152 $sql .= $this->db->order('dm', 'DESC');
153
154 return $this->_getNbByYear($sql);
155 }
156
164 public function getAmountByMonth($year, $format = 0)
165 {
166 global $user;
167
168 $sql = "SELECT date_format(c.date_valid,'%m') as dm, SUM(c.".$this->field.")";
169 $sql .= " FROM ".$this->from;
170 if (!$user->hasRight('societe', 'client', 'voir')) {
171 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
172 }
173 $sql .= $this->join;
174 $sql .= " WHERE ".$this->where;
175 $sql .= " GROUP BY dm";
176 $sql .= $this->db->order('dm', 'DESC');
177
178 $res = $this->_getAmountByMonth($year, $sql, $format);
179 return $res;
180 }
181
188 public function getAverageByMonth($year)
189 {
190 global $user;
191
192 $sql = "SELECT date_format(c.date_valid,'%m') as dm, AVG(c.".$this->field.")";
193 $sql .= " FROM ".$this->from;
194 if (!$user->hasRight('societe', 'client', 'voir')) {
195 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
196 }
197 $sql .= $this->join;
198 $sql .= " WHERE ".$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_valid,'%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->hasRight('societe', 'client', 'voir')) {
217 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
218 }
219 $sql .= " WHERE ".$this->where;
220 $sql .= " GROUP BY year";
221 $sql .= $this->db->order('year', 'DESC');
222
223 return $this->_getAllByYear($sql);
224 }
225}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
Class to manage shipment statistics.
getNbByMonth($year, $format=0)
Return shipment number by month for a year.
__construct($db, $socid, $mode, $userid=0)
Constructor.
getAverageByMonth($year)
Return the orders amount average by month for a year.
getAmountByMonth($year, $format=0)
Return the orders amount by month for a year.
getNbByYear()
Return shipments number per year.
getAllByYear()
Return nb, total and average.
Parent class of statistics class.
_getAverageByMonth($year, $sql, $format=0)
Return the amount average par month for a given year.
_getAmountByMonth($year, $sql, $format=0)
Return the amount per month for a given year.
_getNbByYear($sql)
Return nb of elements by year.
_getAllByYear($sql)
Return nb of elements, total amount and avg amount each year.
_getNbByMonth($year, $sql, $format=0)
Renvoie le nombre de documents par mois pour une annee donnee Return number of documents per month fo...
dol_get_first_day($year, $month=1, $gm=false)
Return GMT time for first day of a month or year.
Definition date.lib.php:595
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
Definition date.lib.php:614