dolibarr  13.0.2
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  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <https://www.gnu.org/licenses/>.
17  */
18 
24 include_once DOL_DOCUMENT_ROOT.'/core/class/stats.class.php';
25 include_once DOL_DOCUMENT_ROOT.'/salaries/class/paymentsalary.class.php';
26 
30 class SalariesStats extends Stats
31 {
35  public $table_element;
36 
37  public $socid;
38  public $userid;
39 
40  public $from;
41  public $field;
42  public $where;
43 
52  public function __construct($db, $socid = 0, $userid = 0)
53  {
54  global $conf;
55 
56  $this->db = $db;
57  $this->socid = $socid;
58  $this->userid = $userid;
59 
60  $object = new PaymentSalary($this->db);
61  $this->from = MAIN_DB_PREFIX.$object->table_element;
62  $this->field = 'amount';
63 
64  $this->where .= " entity = ".$conf->entity;
65  if ($this->socid) {
66  $this->where .= " AND fk_soc = ".$this->socid;
67  }
68  if (is_array($this->userid) && count($this->userid) > 0) $this->where .= ' AND fk_user IN ('.join(',', $this->userid).')';
69  elseif ($this->userid > 0) $this->where .= ' AND fk_user = '.$this->userid;
70  }
71 
72 
78  public function getNbByYear()
79  {
80  $sql = "SELECT YEAR(datep) as dm, count(*)";
81  $sql .= " FROM ".$this->from;
82  $sql .= " GROUP BY dm DESC";
83  $sql .= " WHERE ".$this->where;
84 
85  return $this->_getNbByYear($sql);
86  }
87 
88 
96  public function getNbByMonth($year, $format = 0)
97  {
98  $sql = "SELECT MONTH(datep) as dm, count(*)";
99  $sql .= " FROM ".$this->from;
100  $sql .= " WHERE YEAR(datep) = ".$year;
101  $sql .= " AND ".$this->where;
102  $sql .= " GROUP BY dm";
103  $sql .= $this->db->order('dm', 'DESC');
104 
105  $res = $this->_getNbByMonth($year, $sql, $format);
106  //var_dump($res);print '<br>';
107  return $res;
108  }
109 
110 
118  public function getAmountByMonth($year, $format = 0)
119  {
120  $sql = "SELECT date_format(datep,'%m') as dm, sum(".$this->field.")";
121  $sql .= " FROM ".$this->from;
122  $sql .= " WHERE date_format(datep,'%Y') = '".$this->db->escape($year)."'";
123  $sql .= " AND ".$this->where;
124  $sql .= " GROUP BY dm";
125  $sql .= $this->db->order('dm', 'DESC');
126 
127  $res = $this->_getAmountByMonth($year, $sql, $format);
128  //var_dump($res);print '<br>';
129 
130  return $res;
131  }
132 
139  public function getAverageByMonth($year)
140  {
141  $sql = "SELECT date_format(datep,'%m') as dm, avg(".$this->field.")";
142  $sql .= " FROM ".$this->from;
143  $sql .= " WHERE date_format(datep,'%Y') = '".$this->db->escape($year)."'";
144  $sql .= " AND ".$this->where;
145  $sql .= " GROUP BY dm";
146  $sql .= $this->db->order('dm', 'DESC');
147 
148  return $this->_getAverageByMonth($year, $sql);
149  }
150 
156  public function getAllByYear()
157  {
158  $sql = "SELECT date_format(datep,'%Y') as year, count(*) as nb, sum(".$this->field.") as total, avg(".$this->field.") as avg";
159  $sql .= " FROM ".$this->from;
160  $sql .= " WHERE ".$this->where;
161  $sql .= " GROUP BY year";
162  $sql .= $this->db->order('year', 'DESC');
163 
164  return $this->_getAllByYear($sql);
165  }
166 }
Parent class of statistics class.
Definition: stats.class.php:30
Class to manage salary payments.
getAmountByMonth($year, $format=0)
Return amount of salaries by month for a given year.
Classe permettant la gestion des stats des salaires.
_getNbByMonth($year, $sql, $format=0)
Renvoie le nombre de documents par mois pour une annee donnee Return number of documents per month fo...
$conf db
API class for accounts.
Definition: inc.php:54
__construct($db, $socid=0, $userid=0)
Constructor.
getAllByYear()
Return nb, total and average.
_getAllByYear($sql)
Return nb of elements, total amount and avg amount each year.
getAverageByMonth($year)
Return average amount.
_getAmountByMonth($year, $sql, $format=0)
Return the amount per month for a given year.
getNbByMonth($year, $format=0)
Return the number of salary by month, for a given year.
_getNbByYear($sql)
Return nb of elements by year.
getNbByYear()
Return the number of salary by year.
_getAverageByMonth($year, $sql, $format=0)
Renvoie le montant moyen par mois pour une annee donnee Return the amount average par month for a giv...