dolibarr  13.0.2
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 <https://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  public $socid;
42  public $userid;
43 
44  public $from;
45  public $field;
46  public $where;
47 
48 
57  public 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  if ($mode == 'customer')
68  {
69  $object = new Fichinter($this->db);
70  $this->from = MAIN_DB_PREFIX.$object->table_element." as c";
71  $this->from_line = MAIN_DB_PREFIX.$object->table_element_line." as tl";
72  $this->field = '0';
73  $this->field_line = '0';
74  //$this->where.= " AND c.fk_statut > 0"; // Not draft and not cancelled
75  }
76  if (!$user->rights->societe->client->voir && !$this->socid) $this->where .= " AND c.fk_soc = sc.fk_soc AND sc.fk_user = ".$user->id;
77  $this->where .= ($this->where ? ' AND ' : '')."c.entity IN (".getEntity('fichinter').')';
78 
79  if ($this->socid)
80  {
81  $this->where .= " AND c.fk_soc = ".$this->socid;
82  }
83  if ($this->userid > 0) $this->where .= ' AND c.fk_user_author = '.$this->userid;
84  }
85 
93  public function getNbByMonth($year, $format = 0)
94  {
95  global $user;
96 
97  $sql = "SELECT date_format(c.date_valid,'%m') as dm, COUNT(*) as nb";
98  $sql .= " FROM ".$this->from;
99  if (!$user->rights->societe->client->voir && !$this->socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
100  $sql .= " WHERE c.date_valid BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
101  $sql .= " AND ".$this->where;
102  $sql .= " GROUP BY dm";
103  $sql .= $this->db->order('dm', 'DESC');
104 
105  $res = $this->_getNbByMonth($year, $sql, $format);
106  return $res;
107  }
108 
115  public function getNbByYear()
116  {
117  global $user;
118 
119  $sql = "SELECT date_format(c.date_valid,'%Y') as dm, COUNT(*) as nb, 0";
120  $sql .= " FROM ".$this->from;
121  if (!$user->rights->societe->client->voir && !$this->socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
122  $sql .= " WHERE ".$this->where;
123  $sql .= " GROUP BY dm";
124  $sql .= $this->db->order('dm', 'DESC');
125 
126  return $this->_getNbByYear($sql);
127  }
128 
136  public function getAmountByMonth($year, $format = 0)
137  {
138  global $user;
139 
140  $sql = "SELECT date_format(c.date_valid,'%m') as dm, 0";
141  $sql .= " FROM ".$this->from;
142  if (!$user->rights->societe->client->voir && !$this->socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
143  $sql .= " WHERE c.date_valid BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
144  $sql .= " AND ".$this->where;
145  $sql .= " GROUP BY dm";
146  $sql .= $this->db->order('dm', 'DESC');
147 
148  $res = $this->_getAmountByMonth($year, $sql, $format);
149  return $res;
150  }
151 
158  public function getAverageByMonth($year)
159  {
160  global $user;
161 
162  $sql = "SELECT date_format(c.date_valid,'%m') as dm, 0";
163  $sql .= " FROM ".$this->from;
164  if (!$user->rights->societe->client->voir && !$this->socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
165  $sql .= " WHERE c.date_valid BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
166  $sql .= " AND ".$this->where;
167  $sql .= " GROUP BY dm";
168  $sql .= $this->db->order('dm', 'DESC');
169 
170  return $this->_getAverageByMonth($year, $sql);
171  }
172 
178  public function getAllByYear()
179  {
180  global $user;
181 
182  $sql = "SELECT date_format(c.date_valid,'%Y') as year, COUNT(*) as nb, 0 as total, 0 as avg";
183  $sql .= " FROM ".$this->from;
184  if (!$user->rights->societe->client->voir && !$this->socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
185  $sql .= " WHERE ".$this->where;
186  $sql .= " GROUP BY year";
187  $sql .= $this->db->order('year', 'DESC');
188 
189  return $this->_getAllByYear($sql);
190  }
191 
199  public function getAllByProduct($year, $limit = 0)
200  {
201  global $user;
202 
203  $sql = "SELECT product.ref, COUNT(product.ref) as nb, 0 as total, 0 as avg";
204  $sql .= " FROM ".$this->from.", ".$this->from_line.", ".MAIN_DB_PREFIX."product as product";
205  //if (!$user->rights->societe->client->voir && !$user->socid) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
206  $sql .= " WHERE ".$this->where;
207  $sql .= " AND c.rowid = tl.fk_fichinter AND tl.fk_product = product.rowid";
208  $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))."'";
209  $sql .= " GROUP BY product.ref";
210  $sql .= $this->db->order('nb', 'DESC');
211  //$sql.= $this->db->plimit(20);
212 
213  return $this->_getAllByProduct($sql, $limit);
214  }
215 }
__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:481
_getNbByMonth($year, $sql, $format=0)
Renvoie le nombre de documents par mois pour une annee donnee Return number of documents per month fo...
getAmountByMonth($year, $format=0)
Return the intervention amount by month for a year.
$conf db
API class for accounts.
Definition: inc.php:54
_getAllByProduct($sql, $limit=10)
Return number or total of product refs.
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.
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:498
getNbByMonth($year, $format=0)
Return intervention number by month for a year.
_getAmountByMonth($year, $sql, $format=0)
Return the amount per month for a given year.
getAllByProduct($year, $limit=0)
Return nb, amount of predefined product for year.
_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 Return the amount average par month for a giv...