dolibarr  7.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@capnetworks.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 include_once DOL_DOCUMENT_ROOT . '/core/class/stats.class.php';
26 include_once DOL_DOCUMENT_ROOT . '/expensereport/class/expensereport.class.php';
27 
32 {
33  public $table_element;
34 
35  var $socid;
36  var $userid;
37 
38  var $from;
39  var $field;
40  var $where;
41 
50  function __construct($db, $socid=0, $userid=0)
51  {
52  global $conf, $user;
53 
54  $this->db = $db;
55  $this->socid = $socid;
56  $this->userid = $userid;
57 
58  $object=new ExpenseReport($this->db);
59  $this->from = MAIN_DB_PREFIX.$object->table_element." as e";
60  $this->field='total_ht';
61 
62  //$this->where = " e.fk_statut > 0";
63  //$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.
64  $this->where.= ' e.entity IN ('.getEntity('expensereport').')';
65 
66  //$this->where.= " AND entity = ".$conf->entity;
67  if ($this->socid)
68  {
69  $this->where.=" AND e.fk_soc = ".$this->socid;
70  }
71 
72  // Only me and subordinates
73  if (empty($user->rights->expensereport->readall) && empty($user->rights->expensereport->lire_tous))
74  {
75  $childids = $user->getAllChildIds();
76  $childids[]=$user->id;
77  $this->where.=" AND e.fk_user_author IN (".(join(',',$childids)).")";
78  }
79 
80  if ($this->userid > 0) $this->where.=' AND e.fk_user_author = '.$this->userid;
81  }
82 
83 
89  function getNbByYear()
90  {
91  $sql = "SELECT YEAR(".$this->db->ifsql('e.date_valid IS NULL','e.date_create','e.date_valid').") as dm, count(*)";
92  $sql.= " FROM ".$this->from;
93  $sql.= " GROUP BY dm DESC";
94  $sql.= " WHERE ".$this->where;
95 
96  return $this->_getNbByYear($sql);
97  }
98 
99 
107  function getNbByMonth($year, $format=0)
108  {
109  $sql = "SELECT MONTH(".$this->db->ifsql('e.date_valid IS NULL','e.date_create','e.date_valid').") as dm, count(*)";
110  $sql.= " FROM ".$this->from;
111  $sql.= " WHERE YEAR(e.date_valid) = ".$year;
112  $sql.= " AND ".$this->where;
113  $sql.= " GROUP BY dm";
114  $sql.= $this->db->order('dm','DESC');
115 
116  $res=$this->_getNbByMonth($year, $sql, $format);
117  //var_dump($res);print '<br>';
118  return $res;
119  }
120 
121 
129  function getAmountByMonth($year, $format=0)
130  {
131  $sql = "SELECT date_format(".$this->db->ifsql('e.date_valid IS NULL','e.date_create','e.date_valid').",'%m') as dm, sum(".$this->field.")";
132  $sql.= " FROM ".$this->from;
133  $sql.= " WHERE date_format(".$this->db->ifsql('e.date_valid IS NULL','e.date_create','e.date_valid').",'%Y') = '".$year."'";
134  $sql.= " AND ".$this->where;
135  $sql.= " GROUP BY dm";
136  $sql.= $this->db->order('dm','DESC');
137 
138  $res=$this->_getAmountByMonth($year, $sql, $format);
139  //var_dump($res);print '<br>';
140  return $res;
141  }
142 
149  function getAverageByMonth($year)
150  {
151  $sql = "SELECT date_format(".$this->db->ifsql('e.date_valid IS NULL','e.date_create','e.date_valid').",'%m') as dm, avg(".$this->field.")";
152  $sql.= " FROM ".$this->from;
153  $sql.= " WHERE date_format(".$this->db->ifsql('e.date_valid IS NULL','e.date_create','e.date_valid').",'%Y') = '".$year."'";
154  $sql.= " AND ".$this->where;
155  $sql.= " GROUP BY dm";
156  $sql.= $this->db->order('dm','DESC');
157 
158  return $this->_getAverageByMonth($year, $sql);
159  }
160 
166  function getAllByYear()
167  {
168  $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";
169  $sql.= " FROM ".$this->from;
170  $sql.= " WHERE ".$this->where;
171  $sql.= " GROUP BY year";
172  $sql.= $this->db->order('year','DESC');
173 
174  return $this->_getAllByYear($sql);
175  }
176 }
177 
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'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.