dolibarr  9.0.0
donstats.class.php
1 <?php
2 /* Copyright (C) 2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3  * Copyright (c) 2005-2013 Laurent Destailleur <eldy@users.sourceforge.net>
4  * Copyright (C) 2005-2009 Regis Houssin <regis.houssin@inodbox.com>
5  * Copyright (C) 2011 Juanjo Menent <jmenent@2byte.es>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
27 include_once DOL_DOCUMENT_ROOT . '/core/class/stats.class.php';
28 include_once DOL_DOCUMENT_ROOT . '/don/class/don.class.php';
29 include_once DOL_DOCUMENT_ROOT . '/core/lib/date.lib.php';
30 
31 
35 class DonationStats extends Stats
36 {
40  public $table_element;
41 
42  var $socid;
43  var $userid;
44 
45  var $from;
46  var $field;
47  var $where;
48 
49 
58  function __construct($db, $socid, $mode, $userid=0)
59  {
60  global $user, $conf;
61 
62  $this->db = $db;
63 
64  $this->socid = ($socid > 0 ? $socid : 0);
65  $this->userid = $userid;
66  $this->cachefilesuffix = $mode;
67 
68  $object=new Don($this->db);
69  $this->from = MAIN_DB_PREFIX.$object->table_element." as d";
70  //$this->from.= ", ".MAIN_DB_PREFIX."societe as s";
71  //$this->field='weight'; // Warning, unit of weight is NOT USED AND MUST BE
72  $this->where.= " d.fk_statut > 0"; // Not draft and not cancelled
73 
74  //$this->where.= " AND c.fk_soc = s.rowid AND c.entity = ".$conf->entity;
75  $this->where.= " AND d.entity = ".$conf->entity;
76  if ($this->userid > 0) $this->where.=' WHERE c.fk_user_author = '.$this->userid;
77  }
78 
86  function getNbByMonth($year, $format=0)
87  {
88  global $user;
89 
90  $sql = "SELECT date_format(d.datedon,'%m') as dm, COUNT(*) as nb";
91  $sql.= " FROM ".$this->from;
92  $sql.= " WHERE d.datedon BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
93  $sql.= " AND ".$this->where;
94  $sql.= " GROUP BY dm";
95  $sql.= $this->db->order('dm','DESC');
96 
97  $res=$this->_getNbByMonth($year, $sql, $format);
98  return $res;
99  }
100 
107  function getNbByYear()
108  {
109  global $user;
110 
111  $sql = "SELECT date_format(d.datedon,'%Y') as dm, COUNT(*) as nb, SUM(d.".$this->field.")";
112  $sql.= " FROM ".$this->from;
113  $sql.= " WHERE ".$this->where;
114  $sql.= " GROUP BY dm";
115  $sql.= $this->db->order('dm','DESC');
116 
117  return $this->_getNbByYear($sql);
118  }
119 
125  function getAllByYear()
126  {
127  global $user;
128 
129  $sql = "SELECT date_format(d.datedon,'%Y') as year, COUNT(*) as nb, SUM(d.".$this->field.") as total, AVG(".$this->field.") as avg";
130  $sql.= " FROM ".$this->from;
131  $sql.= " WHERE ".$this->where;
132  $sql.= " GROUP BY year";
133  $sql.= $this->db->order('year','DESC');
134 
135  return $this->_getAllByYear($sql);
136  }
137 }
Parent class of statistics class.
Definition: stats.class.php:30
__construct($db, $socid, $mode, $userid=0)
Constructor.
getNbByYear()
Return shipments number per year.
getNbByMonth($year, $format=0)
Return shipment number by month for a year.
dol_get_first_day($year, $month=1, $gm=false)
Return GMT time for first day of a month or year.
Definition: date.lib.php:453
getAllByYear()
Return nb, total and average.
_getNbByMonth($year, $sql, $format=0)
Renvoie le nombre de proposition par mois pour une annee donnee.
_getAllByYear($sql)
Return nb of elements, total amount and avg amount each year.
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
Definition: date.lib.php:467
Class to manage donations.
Definition: don.class.php:35
Class to manage donations statistics.
_getNbByYear($sql)
Return nb of elements by year.