dolibarr 24.0.0-beta
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 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
6 * Copyright (C) 2024-2026 MDW <mdeweerd@users.noreply.github.com>
7 * Copyright (C) 2026 Charlene Benke <charlemen@free.fr>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 */
22
28require_once DOL_DOCUMENT_ROOT.'/core/class/stats.class.php';
29require_once DOL_DOCUMENT_ROOT.'/expensereport/class/expensereport.class.php';
30
35{
39 public $table_element;
40
44 public $socid;
45
49 public $userid;
50
54 public $from;
55
59 public $field;
60
64 public $where;
65
69 private $datetouse = 'date_debut';
70
71
80 public function __construct($db, $socid = 0, $userid = 0)
81 {
82 global $conf, $user;
83
84 $this->db = $db;
85 $this->socid = $socid;
86 $this->userid = $userid;
87
88 $object = new ExpenseReport($this->db);
89 $this->from = MAIN_DB_PREFIX.$object->table_element." as e";
90 $this->field = 'total_ht';
91
92 //$this->where = " e.fk_statut > 0";
93 //$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.
94 $this->where .= ' e.entity IN ('.getEntity('expensereport').')';
95
96 //$this->where.= " AND entity = ".$conf->entity;
97 if ($this->socid) {
98 $this->where .= " AND e.fk_soc = ".((int) $this->socid);
99 }
100
101 // Only me and subordinates
102 if (!$user->hasRight('expensereport', 'readall') && !$user->hasRight('expensereport', 'lire_tous')) {
103 $childids = $user->getAllChildIds();
104 $childids[] = $user->id;
105 $this->where .= " AND e.fk_user_author IN (".$this->db->sanitize(implode(',', $childids)).")";
106 }
107
108 if ($this->userid > 0) {
109 $this->where .= ' AND e.fk_user_author = '.((int) $this->userid);
110 }
111 }
112
113
119 public function getNbByYear()
120 {
121 $sql = "SELECT YEAR(".$this->db->ifsql("e.".$this->db->sanitize($this->datetouse)." IS NULL", "e.date_create", "e.".$this->db->sanitize($this->datetouse)).") as dm, count(*)";
122 $sql .= " FROM ".$this->db->sanitize($this->from, 0, 1, 1);
123 $sql .= " GROUP BY dm DESC";
124 $sql .= " WHERE ".$this->where;
125
126 return $this->_getNbByYear($sql);
127 }
128
129
137 public function getNbByMonth($year, $format = 0)
138 {
139 $sql = "SELECT MONTH(".$this->db->ifsql("e.".$this->db->sanitize($this->datetouse)." IS NULL", "e.date_create", "e.".$this->db->sanitize($this->datetouse)).") as dm, count(*)";
140 $sql .= " FROM ".$this->db->sanitize($this->from, 0, 1, 1);
141 $sql .= " WHERE YEAR(e.".$this->datetouse.") = ".((int) $year);
142 $sql .= " AND ".$this->where;
143 $sql .= " GROUP BY dm";
144 $sql .= $this->db->order('dm', 'DESC');
145
146 $res = $this->_getNbByMonth($year, $sql, $format);
147
148 return $res;
149 }
150
151
159 public function getAmountByMonth($year, $format = 0)
160 {
161 $sql = "SELECT date_format(".$this->db->ifsql("e.".$this->db->sanitize($this->datetouse)." IS NULL", "e.date_create", "e.".$this->db->sanitize($this->datetouse)).",'%m') as dm, sum(".$this->db->sanitize($this->field).")";
162 $sql .= " FROM ".$this->db->sanitize($this->from, 0, 1, 1);
163 $sql .= " WHERE date_format(".$this->db->ifsql("e.".$this->db->sanitize($this->datetouse)." IS NULL", "e.date_create", "e.".$this->db->sanitize($this->datetouse)).",'%Y') = '".$this->db->escape((string) $year)."'";
164 $sql .= " AND ".$this->where;
165 $sql .= " GROUP BY dm";
166 $sql .= $this->db->order('dm', 'DESC');
167
168 $res = $this->_getAmountByMonth($year, $sql, $format);
169
170 return $res;
171 }
172
179 public function getAverageByMonth($year)
180 {
181 $sql = "SELECT date_format(".$this->db->ifsql("e.".$this->db->sanitize($this->datetouse)." IS NULL", "e.date_create", "e.".$this->db->sanitize($this->datetouse)).",'%m') as dm, avg(".$this->db->sanitize($this->field).")";
182 $sql .= " FROM ".$this->db->sanitize($this->from, 0, 1, 1);
183 $sql .= " WHERE date_format(".$this->db->ifsql("e.".$this->db->sanitize($this->datetouse)." IS NULL", "e.date_create", "e.".$this->db->sanitize($this->datetouse)).",'%Y') = '".$this->db->escape((string) $year)."'";
184 $sql .= " AND ".$this->where;
185 $sql .= " GROUP BY dm";
186 $sql .= $this->db->order('dm', 'DESC');
187
188 return $this->_getAverageByMonth($year, $sql);
189 }
190
196 public function getAllByYear()
197 {
198 $sql = "SELECT date_format(".$this->db->ifsql("e.".$this->db->sanitize($this->datetouse)." IS NULL", "e.date_create", "e.".$this->db->sanitize($this->datetouse)).",'%Y') as year, count(*) as nb, sum(".$this->db->sanitize($this->field).") as total, avg(".$this->db->sanitize($this->field).") as avg";
199 $sql .= " FROM ".$this->db->sanitize($this->from, 0, 1, 1);
200 $sql .= " WHERE ".$this->where;
201 $sql .= " GROUP BY year";
202 $sql .= $this->db->order('year', 'DESC');
203
204 return $this->_getAllByYear($sql);
205 }
206}
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
Class to manage Trips and Expenses.
Class to manage the statistics of the expensereports and expense notes.
getNbByMonth($year, $format=0)
Return the quantity of invoices per month for a given year.
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)
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)
Return number of documents per month for a given year.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined: