dolibarr  7.0.0-beta
deplacementstats.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3  * Copyright (c) 2005-2008 Laurent Destailleur <eldy@users.sourceforge.net>
4  * Copyright (C) 2005-2009 Regis Houssin <regis.houssin@capnetworks.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
25 include_once DOL_DOCUMENT_ROOT . '/core/class/stats.class.php';
26 include_once DOL_DOCUMENT_ROOT . '/compta/deplacement/class/deplacement.class.php';
27 
31 class DeplacementStats extends Stats
32 {
33  public $table_element;
34 
35  var $socid;
36  var $userid;
37 
38  var $from;
39  var $field;
40  var $where;
41 
50  function __construct($db, $socid=0, $userid=0)
51  {
52  global $conf;
53 
54  $this->db = $db;
55  $this->socid = $socid;
56  $this->userid = $userid;
57 
58  $object=new Deplacement($this->db);
59  $this->from = MAIN_DB_PREFIX.$object->table_element;
60  $this->field='km';
61 
62  $this->where = " fk_statut > 0";
63  $this->where.= " AND entity = ".$conf->entity;
64  if ($this->socid)
65  {
66  $this->where.=" AND fk_soc = ".$this->socid;
67  }
68  if (is_array($this->userid) && count($this->userid) > 0) $this->where.=' AND fk_user IN ('.join(',',$this->userid).')';
69  else if ($this->userid > 0) $this->where.=' AND fk_user = '.$this->userid;
70  }
71 
72 
78  function getNbByYear()
79  {
80  $sql = "SELECT YEAR(dated) as dm, count(*)";
81  $sql.= " FROM ".$this->from;
82  $sql.= " GROUP BY dm DESC";
83  $sql.= " WHERE ".$this->where;
84 
85  return $this->_getNbByYear($sql);
86  }
87 
88 
96  function getNbByMonth($year, $format=0)
97  {
98  $sql = "SELECT MONTH(dated) as dm, count(*)";
99  $sql.= " FROM ".$this->from;
100  $sql.= " WHERE YEAR(dated) = ".$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  //var_dump($res);print '<br>';
107  return $res;
108  }
109 
110 
118  function getAmountByMonth($year, $format=0)
119  {
120  $sql = "SELECT date_format(dated,'%m') as dm, sum(".$this->field.")";
121  $sql.= " FROM ".$this->from;
122  $sql.= " WHERE date_format(dated,'%Y') = '".$year."'";
123  $sql.= " AND ".$this->where;
124  $sql.= " GROUP BY dm";
125  $sql.= $this->db->order('dm','DESC');
126 
127  $res=$this->_getAmountByMonth($year, $sql, $format);
128  //var_dump($res);print '<br>';
129  return $res;
130  }
131 
138  function getAverageByMonth($year)
139  {
140  $sql = "SELECT date_format(dated,'%m') as dm, avg(".$this->field.")";
141  $sql.= " FROM ".$this->from;
142  $sql.= " WHERE date_format(dated,'%Y') = '".$year."'";
143  $sql.= " AND ".$this->where;
144  $sql.= " GROUP BY dm";
145  $sql.= $this->db->order('dm','DESC');
146 
147  return $this->_getAverageByMonth($year, $sql);
148  }
149 
155  function getAllByYear()
156  {
157  $sql = "SELECT date_format(dated,'%Y') as year, count(*) as nb, sum(".$this->field.") as total, avg(".$this->field.") as avg";
158  $sql.= " FROM ".$this->from;
159  $sql.= " WHERE ".$this->where;
160  $sql.= " GROUP BY year";
161  $sql.= $this->db->order('year','DESC');
162 
163  return $this->_getAllByYear($sql);
164  }
165 }
166 
getNbByMonth($year, $format=0)
Renvoie le nombre de facture par mois pour une annee donnee.
Class to manage trips and working credit notes.
Classe permettant la gestion des stats des deplacements et notes de frais.
Parent class of statistics class.
Definition: stats.class.php:30
_getNbByMonth($year, $sql, $format=0)
Renvoie le nombre de proposition par mois pour une annee donnee.
__construct($db, $socid=0, $userid=0)
Constructor.
getNbByYear()
Renvoie le nombre de facture par annee.
getAmountByMonth($year, $format=0)
Renvoie le montant de facture par mois pour une annee donnee.
_getAllByYear($sql)
Return nb of elements, total amount and avg amount each 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.
_getAverageByMonth($year, $sql, $format=0)
Renvoie le montant moyen par mois pour une annee donnee.
getAllByYear()
Return nb, total and average.
getAverageByMonth($year)
Return average amount.