dolibarr 21.0.0-alpha
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
45 public $socid;
46 public $userid;
47
48 public $from;
49 public $field;
50 public $where;
51 public $join;
52
53
64 public function __construct($db, $socid = 0, $userid = 0, $mode = 'customer', $typentid = 0, $categid = 0)
65 {
66 $this->db = $db;
67 $this->socid = ($socid > 0 ? $socid : 0);
68 $this->userid = $userid;
69 $this->join = '';
70
71 if ($mode == 'customer') {
72 $object = new Propal($this->db);
73
74 $this->from = MAIN_DB_PREFIX.$object->table_element." as p";
75 $this->from_line = MAIN_DB_PREFIX.$object->table_element_line." as tl";
76 $this->field_date = 'p.datep';
77 $this->field = 'total_ht';
78 $this->field_line = 'total_ht';
79
80 //$this->where .= " p.fk_statut > 0";
81 }
82 if ($mode == 'supplier') {
83 $object = new SupplierProposal($this->db);
84
85 $this->from = MAIN_DB_PREFIX.$object->table_element." as p";
86 $this->from_line = MAIN_DB_PREFIX.$object->table_element_line." as tl";
87 $this->field_date = 'p.date_valid';
88 $this->field = 'total_ht';
89 $this->field_line = 'total_ht';
90
91 //$this->where .= " p.fk_statut > 0"; // Validated, accepted, refused and closed
92 }
93 //$this->where.= " AND p.fk_soc = s.rowid AND p.entity = ".$conf->entity;
94 $this->where .= ($this->where ? ' AND ' : '')."p.entity IN (".getEntity('propal').")";
95 if ($this->socid) {
96 $this->where .= " AND p.fk_soc = ".((int) $this->socid);
97 }
98 if ($this->userid > 0) {
99 $this->where .= ' AND fk_user_author = '.((int) $this->userid);
100 }
101
102 if ($typentid) {
103 $this->join .= ' LEFT JOIN '.MAIN_DB_PREFIX.'societe as s ON s.rowid = p.fk_soc';
104 $this->where .= ' AND s.fk_typent = '.((int) $typentid);
105 }
106
107 if ($categid) {
108 $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).')';
109 }
110 }
111
112
120 public function getNbByMonth($year, $format = 0)
121 {
122 global $user;
123
124 $sql = "SELECT date_format(".$this->field_date.",'%m') as dm, COUNT(*) as nb";
125 $sql .= " FROM ".$this->from;
126 if (!$user->hasRight('societe', 'client', 'voir')) {
127 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON p.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
128 }
129 $sql .= $this->join;
130 $sql .= " WHERE ".$this->field_date." BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
131 $sql .= " AND ".$this->where;
132 $sql .= " GROUP BY dm";
133 $sql .= $this->db->order('dm', 'DESC');
134
135 $res = $this->_getNbByMonth($year, $sql, $format);
136 return $res;
137 }
138
145 public function getNbByYear()
146 {
147 global $user;
148
149 $sql = "SELECT date_format(".$this->field_date.",'%Y') as dm, COUNT(*) as nb, SUM(c.".$this->field.")";
150 $sql .= " FROM ".$this->from;
151 if (!$user->hasRight('societe', 'client', 'voir')) {
152 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON p.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
153 }
154 $sql .= $this->join;
155 $sql .= " WHERE ".$this->where;
156 $sql .= " GROUP BY dm";
157 $sql .= $this->db->order('dm', 'DESC');
158
159 return $this->_getNbByYear($sql);
160 }
161
169 public function getAmountByMonth($year, $format = 0)
170 {
171 global $user;
172
173 $sql = "SELECT date_format(".$this->field_date.",'%m') as dm, SUM(p.".$this->field.")";
174 $sql .= " FROM ".$this->from;
175 if (!$user->hasRight('societe', 'client', 'voir')) {
176 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON p.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
177 }
178 $sql .= $this->join;
179 $sql .= " WHERE ".$this->field_date." BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
180 $sql .= " AND ".$this->where;
181 $sql .= " GROUP BY dm";
182 $sql .= $this->db->order('dm', 'DESC');
183
184 $res = $this->_getAmountByMonth($year, $sql, $format);
185 return $res;
186 }
187
194 public function getAverageByMonth($year)
195 {
196 global $user;
197
198 $sql = "SELECT date_format(".$this->field_date.",'%m') as dm, AVG(p.".$this->field.")";
199 $sql .= " FROM ".$this->from;
200 if (!$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 return $this->_getAverageByMonth($year, $sql);
210 }
211
217 public function getAllByYear()
218 {
219 global $user;
220
221 $sql = "SELECT date_format(".$this->field_date.",'%Y') as year, COUNT(*) as nb, SUM(".$this->field.") as total, AVG(".$this->field.") as avg";
222 $sql .= " FROM ".$this->from;
223 if (!$user->hasRight('societe', 'client', 'voir')) {
224 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON p.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
225 }
226 $sql .= $this->join;
227 $sql .= " WHERE ".$this->where;
228 $sql .= " GROUP BY year";
229 $sql .= $this->db->order('year', 'DESC');
230
231 return $this->_getAllByYear($sql);
232 }
233
234
242 public function getAllByProduct($year, $limit = 10)
243 {
244 global $user;
245
246 $sql = "SELECT product.ref, COUNT(product.ref) as nb, SUM(tl.".$this->field_line.") as total, AVG(tl.".$this->field_line.") as avg";
247 $sql .= " FROM ".$this->from;
248 $sql .= " INNER JOIN ".$this->from_line." ON p.rowid = tl.fk_propal";
249 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."product as product ON tl.fk_product = product.rowid";
250 if (!$user->hasRight('societe', 'client', 'voir')) {
251 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON p.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
252 }
253 $sql .= $this->join;
254 $sql .= " WHERE ".$this->where;
255 $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))."'";
256 $sql .= " GROUP BY product.ref";
257 $sql .= $this->db->order('nb', 'DESC');
258 //$sql.= $this->db->plimit(20);
259
260 return $this->_getAllByProduct($sql, $limit);
261 }
262}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
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:595
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
Definition date.lib.php:614
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.