dolibarr 21.0.0-alpha
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 * 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.'/reception/class/reception.class.php';
30include_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
31
32
36class ReceptionStats extends Stats
37{
41 public $table_element;
42
46 public $socid;
50 public $userid;
51
52 public $from;
53 public $field;
54 public $where;
55
56
65 public function __construct($db, $socid, $mode, $userid = 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
75 $object = new Reception($this->db);
76 $this->from = MAIN_DB_PREFIX.$object->table_element." as c";
77 //$this->from.= ", ".MAIN_DB_PREFIX."societe as s";
78 $this->field = 'weight'; // Warning, unit of weight is NOT USED AND MUST BE
79 $this->where .= " c.fk_statut > 0"; // Not draft and not cancelled
80
81 //$this->where.= " AND c.fk_soc = s.rowid AND c.entity = ".$conf->entity;
82 $this->where .= " AND c.entity IN (".getEntity('reception').")";
83 if (!$user->hasRight('societe', 'client', 'voir')) {
84 $this->where .= " AND c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
85 }
86 if ($this->socid) {
87 $this->where .= " AND c.fk_soc = ".((int) $this->socid);
88 }
89 if ($this->userid > 0) {
90 $this->where .= ' AND c.fk_user_author = '.((int) $this->userid);
91 }
92 }
93
101 public function getNbByMonth($year, $format = 0)
102 {
103 global $user;
104
105 $sql = "SELECT date_format(c.date_valid,'%m') as dm, COUNT(*) as nb";
106 $sql .= " FROM ".$this->from;
107 if (!$user->hasRight('societe', 'client', 'voir')) {
108 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
109 }
110 $sql .= " WHERE c.date_valid BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
111 $sql .= " AND ".$this->where;
112 $sql .= " GROUP BY dm";
113 $sql .= $this->db->order('dm', 'DESC');
114
115 $res = $this->_getNbByMonth($year, $sql, $format);
116 return $res;
117 }
118
125 public function getNbByYear()
126 {
127 global $user;
128
129 $sql = "SELECT date_format(c.date_valid,'%Y') as dm, COUNT(*) as nb, SUM(c.".$this->field.")";
130 $sql .= " FROM ".$this->from;
131 if (!$user->hasRight('societe', 'client', 'voir')) {
132 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
133 }
134 $sql .= " WHERE ".$this->where;
135 $sql .= " GROUP BY dm";
136 $sql .= $this->db->order('dm', 'DESC');
137
138 return $this->_getNbByYear($sql);
139 }
140
148 public function getAmountByMonth($year, $format = 0)
149 {
150 global $user;
151
152 $sql = "SELECT date_format(c.date_valid,'%m') as dm, SUM(c.".$this->field.")";
153 $sql .= " FROM ".$this->from;
154 if (!$user->hasRight('societe', 'client', 'voir')) {
155 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
156 }
157 $sql .= " WHERE c.date_valid BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
158 $sql .= " AND ".$this->where;
159 $sql .= " GROUP BY dm";
160 $sql .= $this->db->order('dm', 'DESC');
161
162 $res = $this->_getAmountByMonth($year, $sql, $format);
163 return $res;
164 }
165
172 public function getAverageByMonth($year)
173 {
174 global $user;
175
176 $sql = "SELECT date_format(c.date_valid,'%m') as dm, AVG(c.".$this->field.")";
177 $sql .= " FROM ".$this->from;
178 if (!$user->hasRight('societe', 'client', 'voir')) {
179 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
180 }
181 $sql .= " WHERE c.date_valid 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 return $this->_getAverageByMonth($year, $sql);
187 }
188
194 public function getAllByYear()
195 {
196 global $user;
197
198 $sql = "SELECT date_format(c.date_valid,'%Y') as year, COUNT(*) as nb, SUM(c.".$this->field.") as total, AVG(".$this->field.") as avg";
199 $sql .= " FROM ".$this->from;
200 if (!$user->hasRight('societe', 'client', 'voir')) {
201 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
202 }
203 $sql .= " WHERE ".$this->where;
204 $sql .= " GROUP BY year";
205 $sql .= $this->db->order('year', 'DESC');
206
207 return $this->_getAllByYear($sql);
208 }
209}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
Class to manage receptions.
Class to manage reception statistics.
getNbByMonth($year, $format=0)
Return reception number by month for a year.
getNbByYear()
Return receptions number per year.
getAmountByMonth($year, $format=0)
Return the orders amount by month for a year.
getAverageByMonth($year)
Return the orders amount average by month for a year.
getAllByYear()
Return nb, total and average.
__construct($db, $socid, $mode, $userid=0)
Constructor.
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:596
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
Definition date.lib.php:615