dolibarr 24.0.0-beta
propalestats.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) 2020 Maxime DEMAREST <maxime@indelog.fr>
7 * Copyright (C) 2024-2026 MDW <mdeweerd@users.noreply.github.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 */
22
29include_once DOL_DOCUMENT_ROOT.'/core/class/stats.class.php';
30include_once DOL_DOCUMENT_ROOT.'/comm/propal/class/propal.class.php';
31include_once DOL_DOCUMENT_ROOT.'/supplier_proposal/class/supplier_proposal.class.php';
32include_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
33
34
38class PropaleStats extends Stats
39{
43 public $table_element;
44
48 public $socid;
49
53 public $userid;
54
58 public $from;
59
63 public $field;
64
68 public $where;
69
73 public $join;
74
75
86 public function __construct($db, $socid = 0, $userid = 0, $mode = 'customer', $typentid = 0, $categid = 0)
87 {
88 global $hookmanager;
89 $this->db = $db;
90 $this->socid = ($socid > 0 ? $socid : 0);
91 $this->userid = $userid;
92 $this->join = '';
93 $object = null;
94 if ($mode == 'customer') {
95 $object = new Propal($this->db);
96
97 $this->from = MAIN_DB_PREFIX.$object->table_element." as p";
98 $this->from_line = MAIN_DB_PREFIX.$object->table_element_line." as tl";
99 $this->field_date = 'p.datep';
100 $this->field = 'total_ht';
101 $this->field_line = 'total_ht';
102
103 //$this->where .= " p.fk_statut > 0";
104 }
105 if ($mode == 'supplier') {
106 $object = new SupplierProposal($this->db);
107
108 $this->from = MAIN_DB_PREFIX.$object->table_element." as p";
109 $this->from_line = MAIN_DB_PREFIX.$object->table_element_line." as tl";
110 $this->field_date = 'p.date_valid';
111 $this->field = 'total_ht';
112 $this->field_line = 'total_ht';
113
114 //$this->where .= " p.fk_statut > 0"; // Validated, accepted, refused and closed
115 }
116 //$this->where.= " AND p.fk_soc = s.rowid AND p.entity = ".$conf->entity;
117 $this->where .= ($this->where ? ' AND ' : '')."p.entity IN (".getEntity('propal').")";
118 if ($this->socid) {
119 $this->where .= " AND p.fk_soc = ".((int) $this->socid);
120 }
121 if ($this->userid > 0) {
122 $this->where .= ' AND fk_user_author = '.((int) $this->userid);
123 }
124
125 if ($typentid) {
126 $this->join .= ' LEFT JOIN '.MAIN_DB_PREFIX.'societe as s ON s.rowid = p.fk_soc';
127 $this->where .= ' AND s.fk_typent = '.((int) $typentid);
128 }
129
130 if ($categid) {
131 $this->where .= ' AND EXISTS (SELECT rowid FROM '.MAIN_DB_PREFIX.'categorie_societe as cats WHERE cats.fk_soc = p.fk_soc AND cats.fk_categorie = '.((int) $categid).')';
132 }
133 // Add where from hooks
134 $parameters = array('socid' => $socid);
135 $hookmanager->executeHooks('printFieldListWhere', $parameters, $object); // Note that $action and $object may have been modified by hook
136 $this->where .= $hookmanager->resPrint;
137 }
138
139
147 public function getNbByMonth($year, $format = 0)
148 {
149 global $user;
150
151 $sql = "SELECT date_format(".$this->db->sanitize($this->field_date).", '%m') as dm, COUNT(*) as nb";
152 $sql .= " FROM ".$this->db->sanitize($this->from, 0, 1, 1);
153 if (empty($user->socid) && !$user->hasRight('societe', 'client', 'voir')) {
154 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON p.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
155 }
156 $sql .= $this->join;
157 $sql .= " WHERE ".$this->field_date." 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 $res = $this->_getNbByMonth($year, $sql, $format);
162 return $res;
163 }
164
170 public function getNbByYear()
171 {
172 global $user;
173
174 $sql = "SELECT date_format(".$this->db->sanitize($this->field_date).", '%Y') as dm, COUNT(*) as nb, SUM(c.".$this->db->sanitize($this->field).")";
175 $sql .= " FROM ".$this->db->sanitize($this->from, 0, 1, 1);
176 if (empty($user->socid) && !$user->hasRight('societe', 'client', 'voir')) {
177 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON p.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
178 }
179 $sql .= $this->join;
180 $sql .= " WHERE ".$this->where;
181 $sql .= " GROUP BY dm";
182 $sql .= $this->db->order('dm', 'DESC');
183
184 return $this->_getNbByYear($sql);
185 }
186
194 public function getAmountByMonth($year, $format = 0)
195 {
196 global $user;
197
198 $sql = "SELECT date_format(".$this->db->sanitize($this->field_date).", '%m') as dm, SUM(p.".$this->db->sanitize($this->field).")";
199 $sql .= " FROM ".$this->db->sanitize($this->from, 0, 1, 1);
200 if (empty($user->socid) && !$user->hasRight('societe', 'client', 'voir')) {
201 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON p.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
202 }
203 $sql .= $this->join;
204 $sql .= " WHERE ".$this->field_date." BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
205 $sql .= " AND ".$this->where;
206 $sql .= " GROUP BY dm";
207 $sql .= $this->db->order('dm', 'DESC');
208
209 $res = $this->_getAmountByMonth($year, $sql, $format);
210 return $res;
211 }
212
219 public function getAverageByMonth($year)
220 {
221 global $user;
222
223 $sql = "SELECT date_format(".$this->db->sanitize($this->field_date).", '%m') as dm, AVG(p.".$this->db->sanitize($this->field).")";
224 $sql .= " FROM ".$this->db->sanitize($this->from, 0, 1, 1);
225 if (empty($user->socid) && !$user->hasRight('societe', 'client', 'voir')) {
226 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON p.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
227 }
228 $sql .= $this->join;
229 $sql .= " WHERE ".$this->field_date." BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
230 $sql .= " AND ".$this->where;
231 $sql .= " GROUP BY dm";
232 $sql .= $this->db->order('dm', 'DESC');
233
234 return $this->_getAverageByMonth($year, $sql);
235 }
236
242 public function getAllByYear()
243 {
244 global $user;
245
246 $sql = "SELECT date_format(".$this->db->sanitize($this->field_date).", '%Y') as year, COUNT(*) as nb, SUM(".$this->db->sanitize($this->field).") as total, AVG(".$this->db->sanitize($this->field).") as avg";
247 $sql .= " FROM ".$this->db->sanitize($this->from, 0, 1, 1);
248 if (empty($user->socid) && !$user->hasRight('societe', 'client', 'voir')) {
249 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON p.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
250 }
251 $sql .= $this->join;
252 $sql .= " WHERE ".$this->where;
253 $sql .= " GROUP BY year";
254 $sql .= $this->db->order('year', 'DESC');
255
256 return $this->_getAllByYear($sql);
257 }
258
259
267 public function getAllByProduct($year, $limit = 10)
268 {
269 global $user;
270
271 $sql = "SELECT product.ref, COUNT(product.ref) as nb, SUM(tl.".$this->db->sanitize($this->field_line).") as total, AVG(tl.".$this->db->sanitize($this->field_line).") as avg";
272 $sql .= " FROM ".$this->db->sanitize($this->from, 0, 1, 1);
273 $sql .= " INNER JOIN ".$this->db->sanitize($this->from_line, 0, 0, 1)." ON p.rowid = tl.fk_propal";
274 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."product as product ON tl.fk_product = product.rowid";
275 if (empty($user->socid) && !$user->hasRight('societe', 'client', 'voir')) {
276 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON p.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
277 }
278 $sql .= $this->join;
279 $sql .= " WHERE ".$this->where;
280 $sql .= " AND ".$this->field_date." BETWEEN '".$this->db->idate(dol_get_first_day($year, 1, false))."' AND '".$this->db->idate(dol_get_last_day($year, 12, false))."'";
281 $sql .= " GROUP BY product.ref";
282 $sql .= $this->db->order('nb', 'DESC');
283 //$sql.= $this->db->plimit(20);
284
285 return $this->_getAllByProduct($sql, $limit);
286 }
287}
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
Class to manage proposals.
Class to manage proposals statistics.
getAllByYear()
Return nb, total and average.
getAverageByMonth($year)
Return the propals amount average by month for a year.
getAllByProduct($year, $limit=10)
Return nb, amount of predefined product for year.
getAmountByMonth($year, $format=0)
Return the propals amount by month for a year.
getNbByMonth($year, $format=0)
Return propals number by month for a year.
__construct($db, $socid=0, $userid=0, $mode='customer', $typentid=0, $categid=0)
Constructor.
getNbByYear()
Return propals number per 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.
Class to manage price ask supplier.
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
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.