dolibarr 21.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@inodbox.com>
5 * Copyright (C) 2011 Juanjo Menent <jmenent@2byte.es>
6 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.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
28include_once DOL_DOCUMENT_ROOT.'/core/class/stats.class.php';
29include_once DOL_DOCUMENT_ROOT.'/expedition/class/expedition.class.php';
30include_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
31
32
37{
41 public $table_element;
42
46 public $socid;
47
51 public $userid;
52
56 public $from;
57
61 public $join;
62
66 public $field;
67
71 public $where;
72
73
82 public function __construct($db, $socid, $mode, $userid = 0)
83 {
84 global $user, $conf;
85
86 $this->db = $db;
87
88 $this->socid = ($socid > 0 ? $socid : 0);
89 $this->userid = $userid;
90 $this->cachefilesuffix = $mode;
91
92 $object = new Expedition($this->db);
93 $this->from = MAIN_DB_PREFIX.$object->table_element." as c";
94 //$this->from.= ", ".MAIN_DB_PREFIX."societe as s";
95 $this->field = 'weight'; // Warning, unit of weight is NOT USED AND MUST BE
96 $this->where .= " c.fk_statut > 0"; // Not draft and not cancelled
97
98 //$this->where.= " AND c.fk_soc = s.rowid AND c.entity = ".$conf->entity;
99 $this->where .= " AND c.entity = ".$conf->entity;
100 if (!$user->hasRight('societe', 'client', 'voir')) {
101 $this->where .= " AND c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
102 }
103 if ($this->socid) {
104 $this->where .= " AND c.fk_soc = ".((int) $this->socid);
105 }
106 if ($this->userid > 0) {
107 $this->where .= ' AND c.fk_user_author = '.((int) $this->userid);
108 }
109 }
110
118 public function getNbByMonth($year, $format = 0)
119 {
120 global $user;
121
122 $sql = "SELECT date_format(c.date_valid,'%m') as dm, COUNT(*) as nb";
123 $sql .= " FROM ".$this->from;
124 if (!$user->hasRight('societe', 'client', 'voir')) {
125 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
126 }
127 $sql .= " WHERE c.date_valid BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
128 $sql .= " AND ".$this->where;
129 $sql .= " GROUP BY dm";
130 $sql .= $this->db->order('dm', 'DESC');
131
132 $res = $this->_getNbByMonth($year, $sql, $format);
133 return $res;
134 }
135
142 public function getNbByYear()
143 {
144 global $user;
145
146 $sql = "SELECT date_format(c.date_valid,'%Y') as dm, COUNT(*) as nb, SUM(c.".$this->field.")";
147 $sql .= " FROM ".$this->from;
148 if (!$user->hasRight('societe', 'client', 'voir')) {
149 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
150 }
151 $sql .= " WHERE ".$this->where;
152 $sql .= " GROUP BY dm";
153 $sql .= $this->db->order('dm', 'DESC');
154
155 return $this->_getNbByYear($sql);
156 }
157
165 public function getAmountByMonth($year, $format = 0)
166 {
167 global $user;
168
169 $sql = "SELECT date_format(c.date_valid,'%m') as dm, SUM(c.".$this->field.")";
170 $sql .= " FROM ".$this->from;
171 if (!$user->hasRight('societe', 'client', 'voir')) {
172 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
173 }
174 $sql .= $this->join;
175 $sql .= " WHERE ".$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_valid,'%m') as dm, AVG(c.".$this->field.")";
194 $sql .= " FROM ".$this->from;
195 if (!$user->hasRight('societe', 'client', 'voir')) {
196 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
197 }
198 $sql .= $this->join;
199 $sql .= " WHERE ".$this->where;
200 $sql .= " GROUP BY dm";
201 $sql .= $this->db->order('dm', 'DESC');
202
203 return $this->_getAverageByMonth($year, $sql);
204 }
205
211 public function getAllByYear()
212 {
213 global $user;
214
215 $sql = "SELECT date_format(c.date_valid,'%Y') as year, COUNT(*) as nb, SUM(c.".$this->field.") as total, AVG(".$this->field.") as avg";
216 $sql .= " FROM ".$this->from;
217 if (!$user->hasRight('societe', 'client', 'voir')) {
218 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
219 }
220 $sql .= " WHERE ".$this->where;
221 $sql .= " GROUP BY year";
222 $sql .= $this->db->order('year', 'DESC');
223
224 return $this->_getAllByYear($sql);
225 }
226}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:66
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:600
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
Definition date.lib.php:619
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79