dolibarr 24.0.0-beta
contratstats.class.php
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-2012 Regis Houssin <regis.houssin@inodbox.com>
5 * Copyright (C) 2012 Marcos García <marcosgdf@gmail.com>
6 * Copyright (C) 2020 Maxime DEMAREST <maxime@indelog.com>
7 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
8 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
9 * Copyright (C) 2025 Charlene Benke <charlene@patas-monkey.com>
10 * Copyright (C) 2026 Alexandre Spangaro <alexandre@inovea-conseil.com>
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 3 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program. If not, see <https://www.gnu.org/licenses/>.
24 */
25
31include_once DOL_DOCUMENT_ROOT.'/core/class/stats.class.php';
32include_once DOL_DOCUMENT_ROOT.'/contrat/class/contrat.class.php';
33include_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
34
35
39class ContratStats extends Stats
40{
44 public $table_element;
45
49 public $socid;
50
54 public $userid;
55
59 public $from;
60
64 public $from_line;
65
69 public $field;
70
74 public $field_line;
75
79 public $categ_link;
80
84 public $where = '';
85
89 public $join;
90
91
102 public function __construct($db, $socid, $mode, $userid = 0, $typentid = 0, $categid = 0)
103 {
104 global $user, $conf;
105
106 $this->db = $db;
107
108 $this->socid = ($socid > 0 ? $socid : 0);
109 $this->userid = $userid;
110 $this->cachefilesuffix = $mode;
111 $this->join = '';
112
113 $object = new Contrat($this->db);
114 $this->from = MAIN_DB_PREFIX.$object->table_element." as c";
115 $this->from_line = MAIN_DB_PREFIX.$object->table_element_line." as cd";
116 $this->field = 'total_ht';
117 $this->field_line = 'total_ht';
118 //$this->where .= " c.fk_statut > 0"; // Not draft and not cancelled
119 $this->categ_link = MAIN_DB_PREFIX.'categorie_societe';
120
121 //$this->where.= " AND c.fk_soc = s.rowid AND c.entity = ".$conf->entity;
122 $this->where .= ($this->where ? ' AND ' : '').'c.entity IN ('.getEntity('contract').')';
123
124 if ($this->socid) {
125 $this->where .= " AND c.fk_soc = ".((int) $this->socid);
126 }
127 if ($this->userid > 0) {
128 $this->where .= ' AND c.fk_user_author = '.((int) $this->userid);
129 }
130
131 if ($typentid) {
132 $this->join .= ' LEFT JOIN '.MAIN_DB_PREFIX.'societe as s ON s.rowid = c.fk_soc';
133 $this->where .= ' AND s.fk_typent = '.((int) $typentid);
134 }
135
136 if ($categid) {
137 $this->where .= ' AND EXISTS (SELECT rowid FROM '.$this->categ_link.' as cats WHERE cats.fk_soc = c.fk_soc AND cats.fk_categorie = '.((int) $categid).')';
138 }
139 }
140
148 public function getNbByMonth($year, $format = 0)
149 {
150 global $user;
151
152 $sql = "SELECT date_format(c.date_contrat,'%m') as dm, COUNT(*) as nb";
153 $sql .= " FROM ".$this->db->sanitize($this->from, 0, 1, 1);
154 if (empty($user->socid) && !$user->hasRight('societe', 'client', 'voir')) {
155 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
156 }
157 $sql .= $this->join;
158 $sql .= " WHERE c.date_contrat BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
159 $sql .= " AND ".$this->where;
160 $sql .= " GROUP BY dm";
161 $sql .= $this->db->order('dm', 'DESC');
162
163 $res = $this->_getNbByMonth($year, $sql, $format);
164 return $res;
165 }
166
173 public function getNbByYear()
174 {
175 global $user;
176
177 $sql = "SELECT date_format(c.date_contrat,'%Y') as dm, COUNT(*) as nb, SUM(c.".$this->db->sanitize($this->field).")";
178 $sql .= " FROM ".$this->db->sanitize($this->from, 0, 1, 1);
179 if (empty($user->socid) && !$user->hasRight('societe', 'client', 'voir')) {
180 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
181 }
182 $sql .= $this->join;
183 $sql .= " WHERE ".$this->where;
184 $sql .= " GROUP BY dm";
185 $sql .= $this->db->order('dm', 'DESC');
186
187 return $this->_getNbByYear($sql);
188 }
189
197 public function getAmountByMonth($year, $format = 0)
198 {
199 global $user;
200
201 $sql = "SELECT date_format(c.date_contrat,'%m') as dm, count(cd.".$this->db->sanitize($this->field).")";
202 $sql .= " FROM ".$this->db->sanitize($this->from, 0, 1, 1);
203 $sql .= " INNER JOIN ".$this->db->sanitize($this->from_line, 0, 1, 1). " ON c.rowid = cd.fk_contrat";
204 if (empty($user->socid) && !$user->hasRight('societe', 'client', 'voir')) {
205 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
206 }
207 $sql .= $this->join;
208 $sql .= " WHERE c.date_contrat BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
209 $sql .= " AND ".$this->where;
210 $sql .= " GROUP BY dm";
211 $sql .= $this->db->order('dm', 'DESC');
212
213 $res = $this->_getAmountByMonth($year, $sql, $format);
214 return $res;
215 }
216
223 public function getAverageByMonth($year)
224 {
225 global $user;
226
227 $sql = "SELECT date_format(c.date_contrat,'%m') as dm, AVG(c.".$this->db->sanitize($this->field).")";
228 $sql .= " FROM ".$this->db->sanitize($this->from, 0, 1, 1);
229 if (empty($user->socid) && !$user->hasRight('societe', 'client', 'voir')) {
230 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
231 }
232 $sql .= $this->join;
233 $sql .= " WHERE c.date_contrat BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
234 $sql .= " AND ".$this->where;
235 $sql .= " GROUP BY dm";
236 $sql .= $this->db->order('dm', 'DESC');
237
238 return $this->_getAverageByMonth($year, $sql);
239 }
240
246 public function getAllByYear()
247 {
248 global $user;
249
250 $sql = "SELECT date_format(c.date_contrat,'%Y') as year, COUNT(*) as nb, SUM(c.".$this->db->sanitize($this->field).") as total, AVG(".$this->db->sanitize($this->field).") as avg";
251 $sql .= " FROM ".$this->db->sanitize($this->from, 0, 1, 1);
252 if (empty($user->socid) && !$user->hasRight('societe', 'client', 'voir')) {
253 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
254 }
255 $sql .= $this->join;
256 $sql .= " WHERE ".$this->where;
257 $sql .= " GROUP BY year";
258 $sql .= $this->db->order('year', 'DESC');
259
260 return $this->_getAllByYear($sql);
261 }
262
270 public function getAllByProduct($year, $limit = 10)
271 {
272 global $user;
273
274 $sql = "SELECT product.ref, COUNT(product.ref) as nb, SUM(tl.".$this->field_line.") as total, AVG(tl.".$this->field_line.") as avg";
275 $sql .= " FROM ".$this->db->sanitize($this->from, 0, 1, 1);
276 $sql .= " INNER JOIN ".$this->db->sanitize($this->from_line, 0, 1, 1)." ON c.rowid = tl.fk_commande";
277 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."product as product ON tl.fk_product = product.rowid";
278 if (empty($user->socid) && !$user->hasRight('societe', 'client', 'voir')) {
279 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
280 }
281 $sql .= $this->join;
282 $sql .= " WHERE ".$this->where;
283 $sql .= " AND c.date_contrat BETWEEN '".$this->db->idate(dol_get_first_day($year, 1, false))."' AND '".$this->db->idate(dol_get_last_day($year, 12, false))."'";
284 $sql .= " GROUP BY product.ref";
285 $sql .= $this->db->order('nb', 'DESC');
286 //$sql.= $this->db->plimit(20);
287
288 return $this->_getAllByProduct($sql, $limit);
289 }
290}
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
Class to manage contract statistics.
getAllByYear()
Return nb, total and average.
__construct($db, $socid, $mode, $userid=0, $typentid=0, $categid=0)
Constructor.
getAllByProduct($year, $limit=10)
Return nb, amount of predefined product for year.
getNbByMonth($year, $format=0)
Return contracts number by month for a year.
getNbByYear()
Return contracts number per year.
getAmountByMonth($year, $format=0)
Return the contracts amount by month for a year.
getAverageByMonth($year)
Return the contracts amount average by month for a year.
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.
_getAllByProduct($sql, $limit=10)
Return number or total of product refs.
_getNbByMonth($year, $sql, $format=0)
Return number of documents per month for a given year.
dol_get_first_day($year, $month=1, $gm=false)
Return GMT time for first day of a month or year.
Definition date.lib.php:604
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
Definition date.lib.php:623
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.