dolibarr 21.0.0-alpha
salariesstats.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2018 Alexandre Spangaro <aspangaro@open-dsi.fr>
3 * Copyright (c) 2018 Fidesio <contact@fidesio.com>
4 * Copyright (C) 2021 Gauthier VERDOL <gauthier.verdol@atm-consulting.fr>
5 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
6 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
27include_once DOL_DOCUMENT_ROOT.'/core/class/stats.class.php';
28include_once DOL_DOCUMENT_ROOT.'/salaries/class/salary.class.php';
29
33class SalariesStats extends Stats
34{
38 public $table_element;
39
43 public $socid;
44
48 public $userid;
49
58 public function __construct($db, $socid = 0, $userid = 0)
59 {
60 global $conf;
61
62 $this->db = $db;
63 $this->socid = $socid;
64 $this->userid = $userid;
65
66 $object = new Salary($this->db);
67 $this->from = MAIN_DB_PREFIX.$object->table_element;
68 $this->field = 'amount';
69
70 $this->where = " entity = ".$conf->entity;
71 if ($this->socid > 0) {
72 $this->where .= " AND fk_soc = ".((int) $this->socid);
73 }
74 if (is_array($this->userid) && count($this->userid) > 0) {
75 $this->where .= ' AND fk_user IN ('.$this->db->sanitize(implode(',', $this->userid)).')';
76 } elseif ($this->userid > 0) {
77 $this->where .= " AND fk_user = ".((int) $this->userid);
78 }
79 }
80
81
87 public function getNbByYear()
88 {
89 $sql = "SELECT YEAR(dateep) as dm, count(*)";
90 $sql .= " FROM ".$this->from;
91 $sql .= " WHERE ".$this->where;
92 $sql .= " GROUP BY dm DESC";
93
94 return $this->_getNbByYear($sql);
95 }
96
97
105 public function getNbByMonth($year, $format = 0)
106 {
107 $sql = "SELECT MONTH(dateep) as dm, count(*)";
108 $sql .= " FROM ".$this->from;
109 $sql .= " WHERE YEAR(dateep) = ".((int) $year);
110 $sql .= " AND ".$this->where;
111 $sql .= " GROUP BY dm";
112 $sql .= $this->db->order('dm', 'DESC');
113
114 $res = $this->_getNbByMonth($year, $sql, $format);
115
116 return $res;
117 }
118
119
127 public function getAmountByMonth($year, $format = 0)
128 {
129 $sql = "SELECT date_format(dateep,'%m') as dm, sum(".$this->field.")";
130 $sql .= " FROM ".$this->from;
131 $sql .= " WHERE date_format(dateep,'%Y') = '".$this->db->escape($year)."'";
132 $sql .= " AND ".$this->where;
133 $sql .= " GROUP BY dm";
134 $sql .= $this->db->order('dm', 'DESC');
135
136 $res = $this->_getAmountByMonth($year, $sql, $format);
137
138 return $res;
139 }
140
147 public function getAverageByMonth($year)
148 {
149 $sql = "SELECT date_format(dateep,'%m') as dm, avg(".$this->field.")";
150 $sql .= " FROM ".$this->from;
151 $sql .= " WHERE date_format(dateep,'%Y') = '".$this->db->escape($year)."'";
152 $sql .= " AND ".$this->where;
153 $sql .= " GROUP BY dm";
154 $sql .= $this->db->order('dm', 'DESC');
155
156 return $this->_getAverageByMonth($year, $sql);
157 }
158
164 public function getAllByYear()
165 {
166 $sql = "SELECT date_format(dateep,'%Y') as year, count(*) as nb, sum(".$this->field.") as total, avg(".$this->field.") as avg";
167 $sql .= " FROM ".$this->from;
168 $sql .= " WHERE ".$this->where;
169 $sql .= " GROUP BY year";
170 $sql .= $this->db->order('year', 'DESC');
171
172 return $this->_getAllByYear($sql);
173 }
174}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
Class to manage salary statistics.
getNbByMonth($year, $format=0)
Return the number of salary by month, for a given year.
__construct($db, $socid=0, $userid=0)
Constructor.
getAverageByMonth($year)
Return average amount.
getAmountByMonth($year, $format=0)
Return amount of salaries by month for a given year.
getNbByYear()
Return the number of salary by year.
getAllByYear()
Return nb, total and average.
Class to manage salary payments.
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...