dolibarr  7.0.0-beta
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@capnetworks.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 {
36  public $table_element;
37 
38  var $socid;
39  var $userid;
40 
41  var $from;
42  var $field;
43  var $where;
44 
45 
54  function __construct($db, $socid, $mode, $userid=0)
55  {
56  global $user, $conf;
57 
58  $this->db = $db;
59 
60  $this->socid = ($socid > 0 ? $socid : 0);
61  $this->userid = $userid;
62  $this->cachefilesuffix = $mode;
63 
64  $this->where.= " c.entity = ".$conf->entity;
65  if ($mode == 'customer')
66  {
67  $object=new Fichinter($this->db);
68  $this->from = MAIN_DB_PREFIX.$object->table_element." as c";
69  $this->from_line = MAIN_DB_PREFIX.$object->table_element_line." as tl";
70  $this->field='0';
71  $this->field_line='0';
72  //$this->where.= " AND c.fk_statut > 0"; // Not draft and not cancelled
73  }
74  if (!$user->rights->societe->client->voir && !$this->socid) $this->where .= " AND c.fk_soc = sc.fk_soc AND sc.fk_user = " .$user->id;
75  if ($this->socid)
76  {
77  $this->where.=" AND c.fk_soc = ".$this->socid;
78  }
79  if ($this->userid > 0) $this->where.=' AND c.fk_user_author = '.$this->userid;
80  }
81 
89  function getNbByMonth($year, $format=0)
90  {
91  global $user;
92 
93  $sql = "SELECT date_format(c.date_valid,'%m') as dm, COUNT(*) as nb";
94  $sql.= " FROM ".$this->from;
95  if (!$user->rights->societe->client->voir && !$this->socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
96  $sql.= " WHERE c.date_valid BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
97  $sql.= " AND ".$this->where;
98  $sql.= " GROUP BY dm";
99  $sql.= $this->db->order('dm','DESC');
100 
101  $res=$this->_getNbByMonth($year, $sql, $format);
102  return $res;
103  }
104 
111  function getNbByYear()
112  {
113  global $user;
114 
115  $sql = "SELECT date_format(c.date_valid,'%Y') as dm, COUNT(*) as nb, 0";
116  $sql.= " FROM ".$this->from;
117  if (!$user->rights->societe->client->voir && !$this->socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
118  $sql.= " WHERE ".$this->where;
119  $sql.= " GROUP BY dm";
120  $sql.= $this->db->order('dm','DESC');
121 
122  return $this->_getNbByYear($sql);
123  }
124 
132  function getAmountByMonth($year, $format=0)
133  {
134  global $user;
135 
136  $sql = "SELECT date_format(c.date_valid,'%m') as dm, 0";
137  $sql.= " FROM ".$this->from;
138  if (!$user->rights->societe->client->voir && !$this->socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
139  $sql.= " WHERE c.date_valid BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
140  $sql.= " AND ".$this->where;
141  $sql.= " GROUP BY dm";
142  $sql.= $this->db->order('dm','DESC');
143 
144  $res=$this->_getAmountByMonth($year, $sql, $format);
145  return $res;
146  }
147 
154  function getAverageByMonth($year)
155  {
156  global $user;
157 
158  $sql = "SELECT date_format(c.date_valid,'%m') as dm, 0";
159  $sql.= " FROM ".$this->from;
160  if (!$user->rights->societe->client->voir && !$this->socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
161  $sql.= " WHERE c.date_valid BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
162  $sql.= " AND ".$this->where;
163  $sql.= " GROUP BY dm";
164  $sql.= $this->db->order('dm','DESC');
165 
166  return $this->_getAverageByMonth($year, $sql);
167  }
168 
174  function getAllByYear()
175  {
176  global $user;
177 
178  $sql = "SELECT date_format(c.date_valid,'%Y') as year, COUNT(*) as nb, 0 as total, 0 as avg";
179  $sql.= " FROM ".$this->from;
180  if (!$user->rights->societe->client->voir && !$this->socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
181  $sql.= " WHERE ".$this->where;
182  $sql.= " GROUP BY year";
183  $sql.= $this->db->order('year','DESC');
184 
185  return $this->_getAllByYear($sql);
186  }
187 
194  function getAllByProduct($year)
195  {
196  global $user;
197 
198  $sql = "SELECT product.ref, COUNT(product.ref) as nb, 0 as total, 0 as avg";
199  $sql.= " FROM ".$this->from.", ".$this->from_line.", ".MAIN_DB_PREFIX."product as product";
200  //if (!$user->rights->societe->client->voir && !$user->societe_id) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
201  $sql.= " WHERE ".$this->where;
202  $sql.= " AND c.rowid = tl.fk_fichinter AND tl.fk_product = product.rowid";
203  $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))."'";
204  $sql.= " GROUP BY product.ref";
205  $sql.= $this->db->order('nb','DESC');
206  //$sql.= $this->db->plimit(20);
207 
208  return $this->_getAllByProduct($sql);
209  }
210 
211 }
212 
__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:445
_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:459
getNbByMonth($year, $format=0)
Return intervention number by month for a year.
_getAmountByMonth($year, $sql, $format=0)
Renvoie le nombre d'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.