dolibarr  9.0.0
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 <http://www.gnu.org/licenses/>.
18  */
19 
25 require_once DOL_DOCUMENT_ROOT . '/core/class/stats.class.php';
26 require_once DOL_DOCUMENT_ROOT . '/expensereport/class/expensereport.class.php';
27 
32 {
36  public $table_element;
37 
38  var $socid;
39  var $userid;
40 
41  var $from;
42  var $field;
43  var $where;
44 
53  function __construct($db, $socid=0, $userid=0)
54  {
55  global $conf, $user;
56 
57  $this->db = $db;
58  $this->socid = $socid;
59  $this->userid = $userid;
60 
61  $object=new ExpenseReport($this->db);
62  $this->from = MAIN_DB_PREFIX.$object->table_element." as e";
63  $this->field='total_ht';
64 
65  //$this->where = " e.fk_statut > 0";
66  //$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.
67  $this->where.= ' e.entity IN ('.getEntity('expensereport').')';
68 
69  //$this->where.= " AND entity = ".$conf->entity;
70  if ($this->socid)
71  {
72  $this->where.=" AND e.fk_soc = ".$this->socid;
73  }
74 
75  // Only me and subordinates
76  if (empty($user->rights->expensereport->readall) && empty($user->rights->expensereport->lire_tous))
77  {
78  $childids = $user->getAllChildIds();
79  $childids[]=$user->id;
80  $this->where.=" AND e.fk_user_author IN (".(join(',',$childids)).")";
81  }
82 
83  if ($this->userid > 0) $this->where.=' AND e.fk_user_author = '.$this->userid;
84  }
85 
86 
92  function getNbByYear()
93  {
94  $sql = "SELECT YEAR(".$this->db->ifsql('e.date_valid IS NULL','e.date_create','e.date_valid').") as dm, count(*)";
95  $sql.= " FROM ".$this->from;
96  $sql.= " GROUP BY dm DESC";
97  $sql.= " WHERE ".$this->where;
98 
99  return $this->_getNbByYear($sql);
100  }
101 
102 
110  function getNbByMonth($year, $format=0)
111  {
112  $sql = "SELECT MONTH(".$this->db->ifsql('e.date_valid IS NULL','e.date_create','e.date_valid').") as dm, count(*)";
113  $sql.= " FROM ".$this->from;
114  $sql.= " WHERE YEAR(e.date_valid) = ".$year;
115  $sql.= " AND ".$this->where;
116  $sql.= " GROUP BY dm";
117  $sql.= $this->db->order('dm','DESC');
118 
119  $res=$this->_getNbByMonth($year, $sql, $format);
120  //var_dump($res);print '<br>';
121  return $res;
122  }
123 
124 
132  function getAmountByMonth($year, $format=0)
133  {
134  $sql = "SELECT date_format(".$this->db->ifsql('e.date_valid IS NULL','e.date_create','e.date_valid').",'%m') as dm, sum(".$this->field.")";
135  $sql.= " FROM ".$this->from;
136  $sql.= " WHERE date_format(".$this->db->ifsql('e.date_valid IS NULL','e.date_create','e.date_valid').",'%Y') = '".$year."'";
137  $sql.= " AND ".$this->where;
138  $sql.= " GROUP BY dm";
139  $sql.= $this->db->order('dm','DESC');
140 
141  $res=$this->_getAmountByMonth($year, $sql, $format);
142  //var_dump($res);print '<br>';
143  return $res;
144  }
145 
152  function getAverageByMonth($year)
153  {
154  $sql = "SELECT date_format(".$this->db->ifsql('e.date_valid IS NULL','e.date_create','e.date_valid').",'%m') as dm, avg(".$this->field.")";
155  $sql.= " FROM ".$this->from;
156  $sql.= " WHERE date_format(".$this->db->ifsql('e.date_valid IS NULL','e.date_create','e.date_valid').",'%Y') = '".$year."'";
157  $sql.= " AND ".$this->where;
158  $sql.= " GROUP BY dm";
159  $sql.= $this->db->order('dm','DESC');
160 
161  return $this->_getAverageByMonth($year, $sql);
162  }
163 
169  function getAllByYear()
170  {
171  $sql = "SELECT date_format(".$this->db->ifsql('e.date_valid IS NULL','e.date_create','e.date_valid').",'%Y') as year, count(*) as nb, sum(".$this->field.") as total, avg(".$this->field.") as avg";
172  $sql.= " FROM ".$this->from;
173  $sql.= " WHERE ".$this->where;
174  $sql.= " GROUP BY year";
175  $sql.= $this->db->order('year','DESC');
176 
177  return $this->_getAllByYear($sql);
178  }
179 }
180 
Parent class of statistics class.
Definition: stats.class.php:30
Classe permettant la gestion des stats des expensereports et notes de frais.
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.
_getNbByMonth($year, $sql, $format=0)
Renvoie le nombre de proposition par mois pour une annee donnee.
getAverageByMonth($year)
Return average amount.
__construct($db, $socid=0, $userid=0)
Constructor.
_getAllByYear($sql)
Return nb of elements, total amount and avg amount each year.
Class to manage Trips and Expenses.
_getAmountByMonth($year, $sql, $format=0)
Renvoie le nombre d&#39;element par mois pour une annee donnee.
getAllByYear()
Return nb, total and average.
_getNbByYear($sql)
Return nb of elements by year.
getNbByYear()
Return nb of expense report per year.
_getAverageByMonth($year, $sql, $format=0)
Renvoie le montant moyen par mois pour une annee donnee.