dolibarr  9.0.0
fichinterstats.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-2012 Regis Houssin <regis.houssin@inodbox.com>
5  * Copyright (C) 2012 Marcos GarcĂ­a <marcosgdf@gmail.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
26 include_once DOL_DOCUMENT_ROOT . '/core/class/stats.class.php';
27 include_once DOL_DOCUMENT_ROOT . '/fichinter/class/fichinter.class.php';
28 include_once DOL_DOCUMENT_ROOT . '/core/lib/date.lib.php';
29 
30 
34 class FichinterStats extends Stats
35 {
39  public $table_element;
40 
41  var $socid;
42  var $userid;
43 
44  var $from;
45  var $field;
46  var $where;
47 
48 
57  function __construct($db, $socid, $mode, $userid=0)
58  {
59  global $user, $conf;
60 
61  $this->db = $db;
62 
63  $this->socid = ($socid > 0 ? $socid : 0);
64  $this->userid = $userid;
65  $this->cachefilesuffix = $mode;
66 
67  $this->where.= " c.entity = ".$conf->entity;
68  if ($mode == 'customer')
69  {
70  $object=new Fichinter($this->db);
71  $this->from = MAIN_DB_PREFIX.$object->table_element." as c";
72  $this->from_line = MAIN_DB_PREFIX.$object->table_element_line." as tl";
73  $this->field='0';
74  $this->field_line='0';
75  //$this->where.= " AND c.fk_statut > 0"; // Not draft and not cancelled
76  }
77  if (!$user->rights->societe->client->voir && !$this->socid) $this->where .= " AND c.fk_soc = sc.fk_soc AND sc.fk_user = " .$user->id;
78  if ($this->socid)
79  {
80  $this->where.=" AND c.fk_soc = ".$this->socid;
81  }
82  if ($this->userid > 0) $this->where.=' AND c.fk_user_author = '.$this->userid;
83  }
84 
92  function getNbByMonth($year, $format=0)
93  {
94  global $user;
95 
96  $sql = "SELECT date_format(c.date_valid,'%m') as dm, COUNT(*) as nb";
97  $sql.= " FROM ".$this->from;
98  if (!$user->rights->societe->client->voir && !$this->socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
99  $sql.= " WHERE c.date_valid BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
100  $sql.= " AND ".$this->where;
101  $sql.= " GROUP BY dm";
102  $sql.= $this->db->order('dm','DESC');
103 
104  $res=$this->_getNbByMonth($year, $sql, $format);
105  return $res;
106  }
107 
114  function getNbByYear()
115  {
116  global $user;
117 
118  $sql = "SELECT date_format(c.date_valid,'%Y') as dm, COUNT(*) as nb, 0";
119  $sql.= " FROM ".$this->from;
120  if (!$user->rights->societe->client->voir && !$this->socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
121  $sql.= " WHERE ".$this->where;
122  $sql.= " GROUP BY dm";
123  $sql.= $this->db->order('dm','DESC');
124 
125  return $this->_getNbByYear($sql);
126  }
127 
135  function getAmountByMonth($year, $format=0)
136  {
137  global $user;
138 
139  $sql = "SELECT date_format(c.date_valid,'%m') as dm, 0";
140  $sql.= " FROM ".$this->from;
141  if (!$user->rights->societe->client->voir && !$this->socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
142  $sql.= " WHERE c.date_valid BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
143  $sql.= " AND ".$this->where;
144  $sql.= " GROUP BY dm";
145  $sql.= $this->db->order('dm','DESC');
146 
147  $res=$this->_getAmountByMonth($year, $sql, $format);
148  return $res;
149  }
150 
157  function getAverageByMonth($year)
158  {
159  global $user;
160 
161  $sql = "SELECT date_format(c.date_valid,'%m') as dm, 0";
162  $sql.= " FROM ".$this->from;
163  if (!$user->rights->societe->client->voir && !$this->socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
164  $sql.= " WHERE c.date_valid BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
165  $sql.= " AND ".$this->where;
166  $sql.= " GROUP BY dm";
167  $sql.= $this->db->order('dm','DESC');
168 
169  return $this->_getAverageByMonth($year, $sql);
170  }
171 
177  function getAllByYear()
178  {
179  global $user;
180 
181  $sql = "SELECT date_format(c.date_valid,'%Y') as year, COUNT(*) as nb, 0 as total, 0 as avg";
182  $sql.= " FROM ".$this->from;
183  if (!$user->rights->societe->client->voir && !$this->socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
184  $sql.= " WHERE ".$this->where;
185  $sql.= " GROUP BY year";
186  $sql.= $this->db->order('year','DESC');
187 
188  return $this->_getAllByYear($sql);
189  }
190 
197  function getAllByProduct($year)
198  {
199  global $user;
200 
201  $sql = "SELECT product.ref, COUNT(product.ref) as nb, 0 as total, 0 as avg";
202  $sql.= " FROM ".$this->from.", ".$this->from_line.", ".MAIN_DB_PREFIX."product as product";
203  //if (!$user->rights->societe->client->voir && !$user->societe_id) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
204  $sql.= " WHERE ".$this->where;
205  $sql.= " AND c.rowid = tl.fk_fichinter AND tl.fk_product = product.rowid";
206  $sql.= " AND c.date_valid BETWEEN '".$this->db->idate(dol_get_first_day($year,1,false))."' AND '".$this->db->idate(dol_get_last_day($year,12,false))."'";
207  $sql.= " GROUP BY product.ref";
208  $sql.= $this->db->order('nb','DESC');
209  //$sql.= $this->db->plimit(20);
210 
211  return $this->_getAllByProduct($sql);
212  }
213 }
214 
__construct($db, $socid, $mode, $userid=0)
Constructor.
getAllByYear()
Return nb, total and average.
Parent class of statistics class.
Definition: stats.class.php:30
Class to manage interventions.
getAverageByMonth($year)
Return the intervention amount average by month for a year.
dol_get_first_day($year, $month=1, $gm=false)
Return GMT time for first day of a month or year.
Definition: date.lib.php:453
_getNbByMonth($year, $sql, $format=0)
Renvoie le nombre de proposition par mois pour une annee donnee.
getAmountByMonth($year, $format=0)
Return the intervention amount by month for a year.
_getAllByProduct($sql, $limit=10)
Return number or total of product refs.
getAllByProduct($year)
Return nb, amount of predefined product for year.
getNbByYear()
Return interventions number per year.
_getAllByYear($sql)
Return nb of elements, total amount and avg amount each year.
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
Definition: date.lib.php:467
getNbByMonth($year, $format=0)
Return intervention number by month for a year.
_getAmountByMonth($year, $sql, $format=0)
Renvoie le nombre d&#39;element par mois pour une annee donnee.
_getNbByYear($sql)
Return nb of elements by year.
Class to manage intervention statistics.
_getAverageByMonth($year, $sql, $format=0)
Renvoie le montant moyen par mois pour une annee donnee.