dolibarr  16.0.5
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 
28 include_once DOL_DOCUMENT_ROOT.'/core/class/stats.class.php';
29 include_once DOL_DOCUMENT_ROOT.'/comm/propal/class/propal.class.php';
30 include_once DOL_DOCUMENT_ROOT.'/supplier_proposal/class/supplier_proposal.class.php';
31 include_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
32 
33 
37 class 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  global $user, $conf;
66 
67  $this->db = $db;
68  $this->socid = ($socid > 0 ? $socid : 0);
69  $this->userid = $userid;
70  $this->join = '';
71 
72  if ($mode == 'customer') {
73  $object = new Propal($this->db);
74 
75  $this->from = MAIN_DB_PREFIX.$object->table_element." as p";
76  $this->from_line = MAIN_DB_PREFIX.$object->table_element_line." as tl";
77  $this->field_date = 'p.datep';
78  $this->field = 'total_ht';
79  $this->field_line = 'total_ht';
80 
81  //$this->where .= " p.fk_statut > 0";
82  }
83  if ($mode == 'supplier') {
84  $object = new SupplierProposal($this->db);
85 
86  $this->from = MAIN_DB_PREFIX.$object->table_element." as p";
87  $this->from_line = MAIN_DB_PREFIX.$object->table_element_line." as tl";
88  $this->field_date = 'p.date_valid';
89  $this->field = 'total_ht';
90  $this->field_line = 'total_ht';
91 
92  //$this->where .= " p.fk_statut > 0"; // Validated, accepted, refused and closed
93  }
94  //$this->where.= " AND p.fk_soc = s.rowid AND p.entity = ".$conf->entity;
95  $this->where .= ($this->where ? ' AND ' : '')."p.entity IN (".getEntity('propal').")";
96  if ($this->socid) {
97  $this->where .= " AND p.fk_soc = ".((int) $this->socid);
98  }
99  if ($this->userid > 0) {
100  $this->where .= ' AND fk_user_author = '.((int) $this->userid);
101  }
102 
103  if ($typentid) {
104  $this->join .= ' LEFT JOIN '.MAIN_DB_PREFIX.'societe as s ON s.rowid = p.fk_soc';
105  $this->where .= ' AND s.fk_typent = '.((int) $typentid);
106  }
107 
108  if ($categid) {
109  $this->join .= ' LEFT JOIN '.MAIN_DB_PREFIX.'categorie_societe as cs ON cs.fk_soc = p.fk_soc';
110  $this->join .= ' LEFT JOIN '.MAIN_DB_PREFIX.'categorie as c ON c.rowid = cs.fk_categorie';
111  $this->where .= ' AND c.rowid = '.((int) $categid);
112  }
113  }
114 
115 
123  public function getNbByMonth($year, $format = 0)
124  {
125  global $user;
126 
127  $sql = "SELECT date_format(".$this->field_date.",'%m') as dm, COUNT(*) as nb";
128  $sql .= " FROM ".$this->from;
129  if (empty($user->rights->societe->client->voir) && !$user->socid) {
130  $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON p.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
131  }
132  $sql .= $this->join;
133  $sql .= " WHERE ".$this->field_date." BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
134  $sql .= " AND ".$this->where;
135  $sql .= " GROUP BY dm";
136  $sql .= $this->db->order('dm', 'DESC');
137 
138  $res = $this->_getNbByMonth($year, $sql, $format);
139  return $res;
140  }
141 
148  public function getNbByYear()
149  {
150  global $user;
151 
152  $sql = "SELECT date_format(".$this->field_date.",'%Y') as dm, COUNT(*) as nb, SUM(c.".$this->field.")";
153  $sql .= " FROM ".$this->from;
154  if (empty($user->rights->societe->client->voir) && !$this->socid) {
155  $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON p.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
156  }
157  $sql .= $this->join;
158  $sql .= " WHERE ".$this->where;
159  $sql .= " GROUP BY dm";
160  $sql .= $this->db->order('dm', 'DESC');
161 
162  return $this->_getNbByYear($sql);
163  }
164 
172  public function getAmountByMonth($year, $format = 0)
173  {
174  global $user;
175 
176  $sql = "SELECT date_format(".$this->field_date.",'%m') as dm, SUM(p.".$this->field.")";
177  $sql .= " FROM ".$this->from;
178  if (empty($user->rights->societe->client->voir) && !$this->socid) {
179  $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON p.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
180  }
181  $sql .= $this->join;
182  $sql .= " WHERE ".$this->field_date." BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
183  $sql .= " AND ".$this->where;
184  $sql .= " GROUP BY dm";
185  $sql .= $this->db->order('dm', 'DESC');
186 
187  $res = $this->_getAmountByMonth($year, $sql, $format);
188  return $res;
189  }
190 
197  public function getAverageByMonth($year)
198  {
199  global $user;
200 
201  $sql = "SELECT date_format(".$this->field_date.",'%m') as dm, AVG(p.".$this->field.")";
202  $sql .= " FROM ".$this->from;
203  if (empty($user->rights->societe->client->voir) && !$this->socid) {
204  $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON p.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
205  }
206  $sql .= $this->join;
207  $sql .= " WHERE ".$this->field_date." BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
208  $sql .= " AND ".$this->where;
209  $sql .= " GROUP BY dm";
210  $sql .= $this->db->order('dm', 'DESC');
211 
212  return $this->_getAverageByMonth($year, $sql);
213  }
214 
220  public function getAllByYear()
221  {
222  global $user;
223 
224  $sql = "SELECT date_format(".$this->field_date.",'%Y') as year, COUNT(*) as nb, SUM(".$this->field.") as total, AVG(".$this->field.") as avg";
225  $sql .= " FROM ".$this->from;
226  if (empty($user->rights->societe->client->voir) && !$this->socid) {
227  $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON p.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
228  }
229  $sql .= $this->join;
230  $sql .= " WHERE ".$this->where;
231  $sql .= " GROUP BY year";
232  $sql .= $this->db->order('year', 'DESC');
233 
234  return $this->_getAllByYear($sql);
235  }
236 
237 
238 
246  public function getAllByProduct($year, $limit = 10)
247  {
248  global $user;
249 
250  $sql = "SELECT product.ref, COUNT(product.ref) as nb, SUM(tl.".$this->field_line.") as total, AVG(tl.".$this->field_line.") as avg";
251  $sql .= " FROM ".$this->from;
252  $sql .= " INNER JOIN ".$this->from_line." ON p.rowid = tl.fk_propal";
253  $sql .= " INNER JOIN ".MAIN_DB_PREFIX."product as product ON tl.fk_product = product.rowid";
254  if (empty($user->rights->societe->client->voir) && !$user->socid) {
255  $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON p.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
256  }
257  $sql .= $this->join;
258  $sql .= " WHERE ".$this->where;
259  $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))."'";
260  $sql .= " GROUP BY product.ref";
261  $sql .= $this->db->order('nb', 'DESC');
262  //$sql.= $this->db->plimit(20);
263 
264  return $this->_getAllByProduct($sql, $limit);
265  }
266 }
db
$conf db
API class for accounts.
Definition: inc.php:41
Stats\_getAllByProduct
_getAllByProduct($sql, $limit=10)
Return number or total of product refs.
Definition: stats.class.php:607
PropaleStats\getAmountByMonth
getAmountByMonth($year, $format=0)
Return the propals amount by month for a year.
Definition: propalestats.class.php:172
PropaleStats\getAllByProduct
getAllByProduct($year, $limit=10)
Return nb, amount of predefined product for year.
Definition: propalestats.class.php:246
Stats
Parent class of statistics class.
Definition: stats.class.php:30
Stats\_getNbByYear
_getNbByYear($sql)
Return nb of elements by year.
Definition: stats.class.php:355
Stats\_getAmountByMonth
_getAmountByMonth($year, $sql, $format=0)
Return the amount per month for a given year.
Definition: stats.class.php:492
PropaleStats
Class to manage proposals statistics.
Definition: propalestats.class.php:37
PropaleStats\__construct
__construct($db, $socid=0, $userid=0, $mode='customer', $typentid=0, $categid=0)
Constructor.
Definition: propalestats.class.php:63
PropaleStats\getAllByYear
getAllByYear()
Return nb, total and average.
Definition: propalestats.class.php:220
getEntity
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.
Definition: functions.lib.php:148
Stats\_getAllByYear
_getAllByYear($sql)
Return nb of elements, total amount and avg amount each year.
Definition: stats.class.php:384
dol_get_first_day
dol_get_first_day($year, $month=1, $gm=false)
Return GMT time for first day of a month or year.
Definition: date.lib.php:551
dol_get_last_day
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
Definition: date.lib.php:570
PropaleStats\getNbByMonth
getNbByMonth($year, $format=0)
Return propals number by month for a year.
Definition: propalestats.class.php:123
Stats\_getNbByMonth
_getNbByMonth($year, $sql, $format=0)
Renvoie le nombre de documents par mois pour une annee donnee Return number of documents per month fo...
Definition: stats.class.php:435
PropaleStats\getNbByYear
getNbByYear()
Return propals number per year.
Definition: propalestats.class.php:148
Propal
Class to manage proposals.
Definition: propal.class.php:52
SupplierProposal
Class to manage price ask supplier.
Definition: supplier_proposal.class.php:50
PropaleStats\getAverageByMonth
getAverageByMonth($year)
Return the propals amount average by month for a year.
Definition: propalestats.class.php:197
Stats\_getAverageByMonth
_getAverageByMonth($year, $sql, $format=0)
Renvoie le montant moyen par mois pour une annee donnee Return the amount average par month for a giv...
Definition: stats.class.php:550