dolibarr  13.0.2
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@inodbox.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 <https://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 {
36  public $table_element;
37 
38  public $socid;
39  public $userid;
40 
41  public $from;
42  public $field;
43  public $where;
44 
53  public function __construct($db, $socid = 0, $userid = 0)
54  {
55  global $conf;
56 
57  $this->db = $db;
58  $this->socid = $socid;
59  $this->userid = $userid;
60 
61  $object = new Deplacement($this->db);
62  $this->from = MAIN_DB_PREFIX.$object->table_element;
63  $this->field = 'km';
64 
65  $this->where = " fk_statut > 0";
66  $this->where .= " AND entity = ".$conf->entity;
67  if ($this->socid)
68  {
69  $this->where .= " AND fk_soc = ".$this->socid;
70  }
71  if (is_array($this->userid) && count($this->userid) > 0) $this->where .= ' AND fk_user IN ('.join(',', $this->userid).')';
72  elseif ($this->userid > 0) $this->where .= ' AND fk_user = '.$this->userid;
73  }
74 
75 
81  public function getNbByYear()
82  {
83  $sql = "SELECT YEAR(dated) as dm, count(*)";
84  $sql .= " FROM ".$this->from;
85  $sql .= " GROUP BY dm DESC";
86  $sql .= " WHERE ".$this->where;
87 
88  return $this->_getNbByYear($sql);
89  }
90 
91 
99  public function getNbByMonth($year, $format = 0)
100  {
101  $sql = "SELECT MONTH(dated) as dm, count(*)";
102  $sql .= " FROM ".$this->from;
103  $sql .= " WHERE YEAR(dated) = ".$year;
104  $sql .= " AND ".$this->where;
105  $sql .= " GROUP BY dm";
106  $sql .= $this->db->order('dm', 'DESC');
107 
108  $res = $this->_getNbByMonth($year, $sql, $format);
109  //var_dump($res);print '<br>';
110  return $res;
111  }
112 
113 
121  public function getAmountByMonth($year, $format = 0)
122  {
123  $sql = "SELECT date_format(dated,'%m') as dm, sum(".$this->field.")";
124  $sql .= " FROM ".$this->from;
125  $sql .= " WHERE date_format(dated,'%Y') = '".$this->db->escape($year)."'";
126  $sql .= " AND ".$this->where;
127  $sql .= " GROUP BY dm";
128  $sql .= $this->db->order('dm', 'DESC');
129 
130  $res = $this->_getAmountByMonth($year, $sql, $format);
131  //var_dump($res);print '<br>';
132  return $res;
133  }
134 
141  public function getAverageByMonth($year)
142  {
143  $sql = "SELECT date_format(dated,'%m') as dm, avg(".$this->field.")";
144  $sql .= " FROM ".$this->from;
145  $sql .= " WHERE date_format(dated,'%Y') = '".$this->db->escape($year)."'";
146  $sql .= " AND ".$this->where;
147  $sql .= " GROUP BY dm";
148  $sql .= $this->db->order('dm', 'DESC');
149 
150  return $this->_getAverageByMonth($year, $sql);
151  }
152 
158  public function getAllByYear()
159  {
160  $sql = "SELECT date_format(dated,'%Y') as year, count(*) as nb, sum(".$this->field.") as total, avg(".$this->field.") as avg";
161  $sql .= " FROM ".$this->from;
162  $sql .= " WHERE ".$this->where;
163  $sql .= " GROUP BY year";
164  $sql .= $this->db->order('year', 'DESC');
165 
166  return $this->_getAllByYear($sql);
167  }
168 }
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 documents par mois pour une annee donnee Return number of documents per month fo...
$conf db
API class for accounts.
Definition: inc.php:54
__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)
Return the amount per month for a given year.
_getNbByYear($sql)
Return nb of elements by year.
_getAverageByMonth($year, $sql, $format=0)
Renvoie le montant moyen par mois pour une annee donnee Return the amount average par month for a giv...
getAllByYear()
Return nb, total and average.
getAverageByMonth($year)
Return average amount.