dolibarr 18.0.6
expensereportstats.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
25require_once DOL_DOCUMENT_ROOT.'/core/class/stats.class.php';
26require_once DOL_DOCUMENT_ROOT.'/expensereport/class/expensereport.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
45 private $datetouse = 'date_valid';
46
47
56 public function __construct($db, $socid = 0, $userid = 0)
57 {
58 global $conf, $user;
59
60 $this->db = $db;
61 $this->socid = $socid;
62 $this->userid = $userid;
63
64 $object = new ExpenseReport($this->db);
65 $this->from = MAIN_DB_PREFIX.$object->table_element." as e";
66 $this->field = 'total_ht';
67
68 //$this->where = " e.fk_statut > 0";
69 //$this->where.= " AND e.date_valid > '2000-01-01'"; // To filter only correct "valid date". If date is invalid, the group by on it will fails. Launch a repair.php if you have.
70 $this->where .= ' e.entity IN ('.getEntity('expensereport').')';
71
72 //$this->where.= " AND entity = ".$conf->entity;
73 if ($this->socid) {
74 $this->where .= " AND e.fk_soc = ".((int) $this->socid);
75 }
76
77 // Only me and subordinates
78 if (empty($user->rights->expensereport->readall) && empty($user->rights->expensereport->lire_tous)) {
79 $childids = $user->getAllChildIds();
80 $childids[] = $user->id;
81 $this->where .= " AND e.fk_user_author IN (".$this->db->sanitize(join(',', $childids)).")";
82 }
83
84 if ($this->userid > 0) {
85 $this->where .= ' AND e.fk_user_author = '.((int) $this->userid);
86 }
87 }
88
89
95 public function getNbByYear()
96 {
97 $sql = "SELECT YEAR(".$this->db->ifsql("e.".$this->datetouse." IS NULL", "e.date_create", "e.".$this->datetouse).") as dm, count(*)";
98 $sql .= " FROM ".$this->from;
99 $sql .= " GROUP BY dm DESC";
100 $sql .= " WHERE ".$this->where;
101
102 return $this->_getNbByYear($sql);
103 }
104
105
113 public function getNbByMonth($year, $format = 0)
114 {
115 $sql = "SELECT MONTH(".$this->db->ifsql("e.".$this->datetouse." IS NULL", "e.date_create", "e.".$this->datetouse).") as dm, count(*)";
116 $sql .= " FROM ".$this->from;
117 $sql .= " WHERE YEAR(e.".$this->datetouse.") = ".((int) $year);
118 $sql .= " AND ".$this->where;
119 $sql .= " GROUP BY dm";
120 $sql .= $this->db->order('dm', 'DESC');
121
122 $res = $this->_getNbByMonth($year, $sql, $format);
123 //var_dump($res);print '<br>';
124 return $res;
125 }
126
127
135 public function getAmountByMonth($year, $format = 0)
136 {
137 $sql = "SELECT date_format(".$this->db->ifsql("e.".$this->datetouse." IS NULL", "e.date_create", "e.".$this->datetouse).",'%m') as dm, sum(".$this->field.")";
138 $sql .= " FROM ".$this->from;
139 $sql .= " WHERE date_format(".$this->db->ifsql("e.".$this->datetouse." IS NULL", "e.date_create", "e.".$this->datetouse).",'%Y') = '".$this->db->escape($year)."'";
140 $sql .= " AND ".$this->where;
141 $sql .= " GROUP BY dm";
142 $sql .= $this->db->order('dm', 'DESC');
143
144 $res = $this->_getAmountByMonth($year, $sql, $format);
145 //var_dump($res);print '<br>';
146 return $res;
147 }
148
155 public function getAverageByMonth($year)
156 {
157 $sql = "SELECT date_format(".$this->db->ifsql("e.".$this->datetouse." IS NULL", "e.date_create", "e.".$this->datetouse).",'%m') as dm, avg(".$this->field.")";
158 $sql .= " FROM ".$this->from;
159 $sql .= " WHERE date_format(".$this->db->ifsql("e.".$this->datetouse." IS NULL", "e.date_create", "e.".$this->datetouse).",'%Y') = '".$this->db->escape($year)."'";
160 $sql .= " AND ".$this->where;
161 $sql .= " GROUP BY dm";
162 $sql .= $this->db->order('dm', 'DESC');
163
164 return $this->_getAverageByMonth($year, $sql);
165 }
166
172 public function getAllByYear()
173 {
174 $sql = "SELECT date_format(".$this->db->ifsql("e.".$this->datetouse." IS NULL", "e.date_create", "e.".$this->datetouse).",'%Y') as year, count(*) as nb, sum(".$this->field.") as total, avg(".$this->field.") as avg";
175 $sql .= " FROM ".$this->from;
176 $sql .= " WHERE ".$this->where;
177 $sql .= " GROUP BY year";
178 $sql .= $this->db->order('year', 'DESC');
179
180 return $this->_getAllByYear($sql);
181 }
182}
Class to manage Trips and Expenses.
Classe permettant la gestion des stats des expensereports et notes de frais.
getNbByMonth($year, $format=0)
Renvoie le nombre de facture par mois pour une annee donnee.
getAverageByMonth($year)
Return average amount.
__construct($db, $socid=0, $userid=0)
Constructor.
getAmountByMonth($year, $format=0)
Renvoie le montant de facture par mois pour une annee donnee.
getNbByYear()
Return nb of expense report per year.
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...