dolibarr 21.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 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 $this->db = $db;
89 $this->socid = ($socid > 0 ? $socid : 0);
90 $this->userid = $userid;
91 $this->join = '';
92
93 if ($mode == 'customer') {
94 $object = new Propal($this->db);
95
96 $this->from = MAIN_DB_PREFIX.$object->table_element." as p";
97 $this->from_line = MAIN_DB_PREFIX.$object->table_element_line." as tl";
98 $this->field_date = 'p.datep';
99 $this->field = 'total_ht';
100 $this->field_line = 'total_ht';
101
102 //$this->where .= " p.fk_statut > 0";
103 }
104 if ($mode == 'supplier') {
105 $object = new SupplierProposal($this->db);
106
107 $this->from = MAIN_DB_PREFIX.$object->table_element." as p";
108 $this->from_line = MAIN_DB_PREFIX.$object->table_element_line." as tl";
109 $this->field_date = 'p.date_valid';
110 $this->field = 'total_ht';
111 $this->field_line = 'total_ht';
112
113 //$this->where .= " p.fk_statut > 0"; // Validated, accepted, refused and closed
114 }
115 //$this->where.= " AND p.fk_soc = s.rowid AND p.entity = ".$conf->entity;
116 $this->where .= ($this->where ? ' AND ' : '')."p.entity IN (".getEntity('propal').")";
117 if ($this->socid) {
118 $this->where .= " AND p.fk_soc = ".((int) $this->socid);
119 }
120 if ($this->userid > 0) {
121 $this->where .= ' AND fk_user_author = '.((int) $this->userid);
122 }
123
124 if ($typentid) {
125 $this->join .= ' LEFT JOIN '.MAIN_DB_PREFIX.'societe as s ON s.rowid = p.fk_soc';
126 $this->where .= ' AND s.fk_typent = '.((int) $typentid);
127 }
128
129 if ($categid) {
130 $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).')';
131 }
132 }
133
134
142 public function getNbByMonth($year, $format = 0)
143 {
144 global $user;
145
146 $sql = "SELECT date_format(".$this->field_date.",'%m') as dm, COUNT(*) as nb";
147 $sql .= " FROM ".$this->from;
148 if (!$user->hasRight('societe', 'client', 'voir')) {
149 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON p.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
150 }
151 $sql .= $this->join;
152 $sql .= " WHERE ".$this->field_date." BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
153 $sql .= " AND ".$this->where;
154 $sql .= " GROUP BY dm";
155 $sql .= $this->db->order('dm', 'DESC');
156
157 $res = $this->_getNbByMonth($year, $sql, $format);
158 return $res;
159 }
160
167 public function getNbByYear()
168 {
169 global $user;
170
171 $sql = "SELECT date_format(".$this->field_date.",'%Y') as dm, COUNT(*) as nb, SUM(c.".$this->field.")";
172 $sql .= " FROM ".$this->from;
173 if (!$user->hasRight('societe', 'client', 'voir')) {
174 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON p.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
175 }
176 $sql .= $this->join;
177 $sql .= " WHERE ".$this->where;
178 $sql .= " GROUP BY dm";
179 $sql .= $this->db->order('dm', 'DESC');
180
181 return $this->_getNbByYear($sql);
182 }
183
191 public function getAmountByMonth($year, $format = 0)
192 {
193 global $user;
194
195 $sql = "SELECT date_format(".$this->field_date.",'%m') as dm, SUM(p.".$this->field.")";
196 $sql .= " FROM ".$this->from;
197 if (!$user->hasRight('societe', 'client', 'voir')) {
198 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON p.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
199 }
200 $sql .= $this->join;
201 $sql .= " WHERE ".$this->field_date." BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
202 $sql .= " AND ".$this->where;
203 $sql .= " GROUP BY dm";
204 $sql .= $this->db->order('dm', 'DESC');
205
206 $res = $this->_getAmountByMonth($year, $sql, $format);
207 return $res;
208 }
209
216 public function getAverageByMonth($year)
217 {
218 global $user;
219
220 $sql = "SELECT date_format(".$this->field_date.",'%m') as dm, AVG(p.".$this->field.")";
221 $sql .= " FROM ".$this->from;
222 if (!$user->hasRight('societe', 'client', 'voir')) {
223 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON p.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
224 }
225 $sql .= $this->join;
226 $sql .= " WHERE ".$this->field_date." BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
227 $sql .= " AND ".$this->where;
228 $sql .= " GROUP BY dm";
229 $sql .= $this->db->order('dm', 'DESC');
230
231 return $this->_getAverageByMonth($year, $sql);
232 }
233
239 public function getAllByYear()
240 {
241 global $user;
242
243 $sql = "SELECT date_format(".$this->field_date.",'%Y') as year, COUNT(*) as nb, SUM(".$this->field.") as total, AVG(".$this->field.") as avg";
244 $sql .= " FROM ".$this->from;
245 if (!$user->hasRight('societe', 'client', 'voir')) {
246 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON p.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
247 }
248 $sql .= $this->join;
249 $sql .= " WHERE ".$this->where;
250 $sql .= " GROUP BY year";
251 $sql .= $this->db->order('year', 'DESC');
252
253 return $this->_getAllByYear($sql);
254 }
255
256
264 public function getAllByProduct($year, $limit = 10)
265 {
266 global $user;
267
268 $sql = "SELECT product.ref, COUNT(product.ref) as nb, SUM(tl.".$this->field_line.") as total, AVG(tl.".$this->field_line.") as avg";
269 $sql .= " FROM ".$this->from;
270 $sql .= " INNER JOIN ".$this->from_line." ON p.rowid = tl.fk_propal";
271 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."product as product ON tl.fk_product = product.rowid";
272 if (!$user->hasRight('societe', 'client', 'voir')) {
273 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON p.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
274 }
275 $sql .= $this->join;
276 $sql .= " WHERE ".$this->where;
277 $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))."'";
278 $sql .= " GROUP BY product.ref";
279 $sql .= $this->db->order('nb', 'DESC');
280 //$sql.= $this->db->plimit(20);
281
282 return $this->_getAllByProduct($sql, $limit);
283 }
284}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:66
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)
Renvoie le nombre de documents par mois pour une annee donnee Return number of documents per month fo...
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: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
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.