dolibarr 19.0.3
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
25include_once DOL_DOCUMENT_ROOT.'/core/class/stats.class.php';
26include_once DOL_DOCUMENT_ROOT.'/compta/deplacement/class/deplacement.class.php';
27
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 > 0) {
68 $this->where .= " AND fk_soc = ".((int) $this->socid);
69 }
70 if (is_array($this->userid) && count($this->userid) > 0) {
71 $this->where .= ' AND fk_user IN ('.$this->db->sanitize(join(',', $this->userid)).')';
72 } elseif ($this->userid > 0) {
73 $this->where .= ' AND fk_user = '.((int) $this->userid);
74 }
75 }
76
77
83 public function getNbByYear()
84 {
85 $sql = "SELECT YEAR(dated) as dm, count(*)";
86 $sql .= " FROM ".$this->from;
87 $sql .= " GROUP BY dm DESC";
88 $sql .= " WHERE ".$this->where;
89
90 return $this->_getNbByYear($sql);
91 }
92
93
101 public function getNbByMonth($year, $format = 0)
102 {
103 $sql = "SELECT MONTH(dated) as dm, count(*)";
104 $sql .= " FROM ".$this->from;
105 $sql .= " WHERE YEAR(dated) = ".((int) $year);
106 $sql .= " AND ".$this->where;
107 $sql .= " GROUP BY dm";
108 $sql .= $this->db->order('dm', 'DESC');
109
110 $res = $this->_getNbByMonth($year, $sql, $format);
111 //var_dump($res);print '<br>';
112 return $res;
113 }
114
115
123 public function getAmountByMonth($year, $format = 0)
124 {
125 $sql = "SELECT date_format(dated,'%m') as dm, sum(".$this->field.")";
126 $sql .= " FROM ".$this->from;
127 $sql .= " WHERE date_format(dated,'%Y') = '".$this->db->escape($year)."'";
128 $sql .= " AND ".$this->where;
129 $sql .= " GROUP BY dm";
130 $sql .= $this->db->order('dm', 'DESC');
131
132 $res = $this->_getAmountByMonth($year, $sql, $format);
133 //var_dump($res);print '<br>';
134 return $res;
135 }
136
143 public function getAverageByMonth($year)
144 {
145 $sql = "SELECT date_format(dated,'%m') as dm, avg(".$this->field.")";
146 $sql .= " FROM ".$this->from;
147 $sql .= " WHERE date_format(dated,'%Y') = '".$this->db->escape($year)."'";
148 $sql .= " AND ".$this->where;
149 $sql .= " GROUP BY dm";
150 $sql .= $this->db->order('dm', 'DESC');
151
152 return $this->_getAverageByMonth($year, $sql);
153 }
154
160 public function getAllByYear()
161 {
162 $sql = "SELECT date_format(dated,'%Y') as year, count(*) as nb, sum(".$this->field.") as total, avg(".$this->field.") as avg";
163 $sql .= " FROM ".$this->from;
164 $sql .= " WHERE ".$this->where;
165 $sql .= " GROUP BY year";
166 $sql .= $this->db->order('year', 'DESC');
167
168 return $this->_getAllByYear($sql);
169 }
170}
Class to manage trips and working credit notes.
Classe permettant la gestion des stats des deplacements et notes de frais.
getNbByYear()
Renvoie le nombre de facture par annee.
__construct($db, $socid=0, $userid=0)
Constructor.
getAmountByMonth($year, $format=0)
Renvoie le montant de facture par mois pour une annee donnee.
getNbByMonth($year, $format=0)
Renvoie le nombre de facture par mois pour une annee donnee.
getAverageByMonth($year)
Return average amount.
getAllByYear()
Return nb, total and average.
Parent class of statistics class.
_getAverageByMonth($year, $sql, $format=0)
Renvoie le montant moyen par mois pour une annee donnee Return the amount average par month for a giv...
_getAmountByMonth($year, $sql, $format=0)
Return the amount per month for a given year.
_getNbByYear($sql)
Return nb of elements by year.
_getAllByYear($sql)
Return nb of elements, total amount and avg amount each year.
_getNbByMonth($year, $sql, $format=0)
Renvoie le nombre de documents par mois pour une annee donnee Return number of documents per month fo...