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 *
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.'/comm/propal/class/propal.class.php';
30include_once DOL_DOCUMENT_ROOT.'/supplier_proposal/class/supplier_proposal.class.php';
31include_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
32
33
37class PropaleStats extends Stats
38{
42 public $table_element;
43
44 public $socid;
45 public $userid;
46
47 public $from;
48 public $field;
49 public $where;
50 public $join;
51
52
63 public function __construct($db, $socid = 0, $userid = 0, $mode = 'customer', $typentid = 0, $categid = 0)
64 {
65 $this->db = $db;
66 $this->socid = ($socid > 0 ? $socid : 0);
67 $this->userid = $userid;
68 $this->join = '';
69
70 if ($mode == 'customer') {
71 $object = new Propal($this->db);
72
73 $this->from = MAIN_DB_PREFIX.$object->table_element." as p";
74 $this->from_line = MAIN_DB_PREFIX.$object->table_element_line." as tl";
75 $this->field_date = 'p.datep';
76 $this->field = 'total_ht';
77 $this->field_line = 'total_ht';
78
79 //$this->where .= " p.fk_statut > 0";
80 }
81 if ($mode == 'supplier') {
82 $object = new SupplierProposal($this->db);
83
84 $this->from = MAIN_DB_PREFIX.$object->table_element." as p";
85 $this->from_line = MAIN_DB_PREFIX.$object->table_element_line." as tl";
86 $this->field_date = 'p.date_valid';
87 $this->field = 'total_ht';
88 $this->field_line = 'total_ht';
89
90 //$this->where .= " p.fk_statut > 0"; // Validated, accepted, refused and closed
91 }
92 //$this->where.= " AND p.fk_soc = s.rowid AND p.entity = ".$conf->entity;
93 $this->where .= ($this->where ? ' AND ' : '')."p.entity IN (".getEntity('propal').")";
94 if ($this->socid) {
95 $this->where .= " AND p.fk_soc = ".((int) $this->socid);
96 }
97 if ($this->userid > 0) {
98 $this->where .= ' AND fk_user_author = '.((int) $this->userid);
99 }
100
101 if ($typentid) {
102 $this->join .= ' LEFT JOIN '.MAIN_DB_PREFIX.'societe as s ON s.rowid = p.fk_soc';
103 $this->where .= ' AND s.fk_typent = '.((int) $typentid);
104 }
105
106 if ($categid) {
107 $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).')';
108 }
109 }
110
111
119 public function getNbByMonth($year, $format = 0)
120 {
121 global $user;
122
123 $sql = "SELECT date_format(".$this->field_date.",'%m') as dm, COUNT(*) as nb";
124 $sql .= " FROM ".$this->from;
125 if (!$user->hasRight('societe', 'client', 'voir')) {
126 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON p.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
127 }
128 $sql .= $this->join;
129 $sql .= " WHERE ".$this->field_date." BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
130 $sql .= " AND ".$this->where;
131 $sql .= " GROUP BY dm";
132 $sql .= $this->db->order('dm', 'DESC');
133
134 $res = $this->_getNbByMonth($year, $sql, $format);
135 return $res;
136 }
137
144 public function getNbByYear()
145 {
146 global $user;
147
148 $sql = "SELECT date_format(".$this->field_date.",'%Y') as dm, COUNT(*) as nb, SUM(c.".$this->field.")";
149 $sql .= " FROM ".$this->from;
150 if (!$user->hasRight('societe', 'client', 'voir')) {
151 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON p.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
152 }
153 $sql .= $this->join;
154 $sql .= " WHERE ".$this->where;
155 $sql .= " GROUP BY dm";
156 $sql .= $this->db->order('dm', 'DESC');
157
158 return $this->_getNbByYear($sql);
159 }
160
168 public function getAmountByMonth($year, $format = 0)
169 {
170 global $user;
171
172 $sql = "SELECT date_format(".$this->field_date.",'%m') as dm, SUM(p.".$this->field.")";
173 $sql .= " FROM ".$this->from;
174 if (!$user->hasRight('societe', 'client', 'voir')) {
175 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON p.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
176 }
177 $sql .= $this->join;
178 $sql .= " WHERE ".$this->field_date." BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
179 $sql .= " AND ".$this->where;
180 $sql .= " GROUP BY dm";
181 $sql .= $this->db->order('dm', 'DESC');
182
183 $res = $this->_getAmountByMonth($year, $sql, $format);
184 return $res;
185 }
186
193 public function getAverageByMonth($year)
194 {
195 global $user;
196
197 $sql = "SELECT date_format(".$this->field_date.",'%m') as dm, AVG(p.".$this->field.")";
198 $sql .= " FROM ".$this->from;
199 if (!$user->hasRight('societe', 'client', 'voir')) {
200 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON p.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
201 }
202 $sql .= $this->join;
203 $sql .= " WHERE ".$this->field_date." BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
204 $sql .= " AND ".$this->where;
205 $sql .= " GROUP BY dm";
206 $sql .= $this->db->order('dm', 'DESC');
207
208 return $this->_getAverageByMonth($year, $sql);
209 }
210
216 public function getAllByYear()
217 {
218 global $user;
219
220 $sql = "SELECT date_format(".$this->field_date.",'%Y') as year, COUNT(*) as nb, SUM(".$this->field.") as total, AVG(".$this->field.") as avg";
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->where;
227 $sql .= " GROUP BY year";
228 $sql .= $this->db->order('year', 'DESC');
229
230 return $this->_getAllByYear($sql);
231 }
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:594
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
Definition date.lib.php:613
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.