dolibarr 21.0.0-alpha
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 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.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
26include_once DOL_DOCUMENT_ROOT.'/core/class/stats.class.php';
27include_once DOL_DOCUMENT_ROOT.'/compta/deplacement/class/deplacement.class.php';
28
33{
37 public $table_element;
38
42 public $socid;
46 public $userid;
47
51 public $from;
55 public $field;
59 public $where;
60
69 public function __construct($db, $socid = 0, $userid = 0)
70 {
71 global $conf;
72
73 $this->db = $db;
74 $this->socid = $socid;
75 $this->userid = $userid;
76
77 $object = new Deplacement($this->db);
78 $this->from = MAIN_DB_PREFIX.$object->table_element;
79 $this->field = 'km';
80
81 $this->where = " fk_statut > 0";
82 $this->where .= " AND entity = ".$conf->entity;
83 if ($this->socid > 0) {
84 $this->where .= " AND fk_soc = ".((int) $this->socid);
85 }
86 if (is_array($this->userid) && count($this->userid) > 0) {
87 $this->where .= ' AND fk_user IN ('.$this->db->sanitize(implode(',', $this->userid)).')';
88 } elseif ($this->userid > 0) {
89 $this->where .= ' AND fk_user = '.((int) $this->userid);
90 }
91 }
92
93
99 public function getNbByYear()
100 {
101 $sql = "SELECT YEAR(dated) as dm, count(*)";
102 $sql .= " FROM ".$this->from;
103 $sql .= " GROUP BY dm DESC";
104 $sql .= " WHERE ".$this->where;
105
106 return $this->_getNbByYear($sql);
107 }
108
109
117 public function getNbByMonth($year, $format = 0)
118 {
119 $sql = "SELECT MONTH(dated) as dm, count(*)";
120 $sql .= " FROM ".$this->from;
121 $sql .= " WHERE YEAR(dated) = ".((int) $year);
122 $sql .= " AND ".$this->where;
123 $sql .= " GROUP BY dm";
124 $sql .= $this->db->order('dm', 'DESC');
125
126 $res = $this->_getNbByMonth($year, $sql, $format);
127 //var_dump($res);print '<br>';
128 return $res;
129 }
130
131
139 public function getAmountByMonth($year, $format = 0)
140 {
141 $sql = "SELECT date_format(dated,'%m') as dm, sum(".$this->field.")";
142 $sql .= " FROM ".$this->from;
143 $sql .= " WHERE date_format(dated,'%Y') = '".$this->db->escape($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 //var_dump($res);print '<br>';
150 return $res;
151 }
152
159 public function getAverageByMonth($year)
160 {
161 $sql = "SELECT date_format(dated,'%m') as dm, avg(".$this->field.")";
162 $sql .= " FROM ".$this->from;
163 $sql .= " WHERE date_format(dated,'%Y') = '".$this->db->escape($year)."'";
164 $sql .= " AND ".$this->where;
165 $sql .= " GROUP BY dm";
166 $sql .= $this->db->order('dm', 'DESC');
167
168 return $this->_getAverageByMonth($year, $sql);
169 }
170
176 public function getAllByYear()
177 {
178 $sql = "SELECT date_format(dated,'%Y') as year, count(*) as nb, sum(".$this->field.") as total, avg(".$this->field.") as avg";
179 $sql .= " FROM ".$this->from;
180 $sql .= " WHERE ".$this->where;
181 $sql .= " GROUP BY year";
182 $sql .= $this->db->order('year', 'DESC');
183
184 return $this->_getAllByYear($sql);
185 }
186}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
Class to manage trips and working credit notes.
Class to manage the statistics of travel and expense notes.
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)
Return the amount average par month for a given year.
_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...