dolibarr  13.0.2
donstats.class.php
Go to the documentation of this file.
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 <https://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  public $socid;
43  public $userid;
44 
48  public $from;
49 
53  public $field;
54 
58  public $where;
59 
60 
69  public function __construct($db, $socid, $mode, $userid = 0)
70  {
71  global $user, $conf;
72 
73  $this->db = $db;
74 
75  $this->socid = ($socid > 0 ? $socid : 0);
76  $this->userid = $userid;
77  $this->cachefilesuffix = $mode;
78 
79  $object = new Don($this->db);
80  $this->from = MAIN_DB_PREFIX.$object->table_element." as d";
81  //$this->from.= ", ".MAIN_DB_PREFIX."societe as s";
82  //$this->field='weight'; // Warning, unit of weight is NOT USED AND MUST BE
83  $this->where .= " d.fk_statut > 0"; // Not draft and not cancelled
84 
85  //$this->where.= " AND c.fk_soc = s.rowid AND c.entity = ".$conf->entity;
86  $this->where .= " AND d.entity = ".$conf->entity;
87  if ($this->userid > 0) $this->where .= ' WHERE c.fk_user_author = '.$this->userid;
88  }
89 
97  public function getNbByMonth($year, $format = 0)
98  {
99  global $user;
100 
101  $sql = "SELECT date_format(d.datedon,'%m') as dm, COUNT(*) as nb";
102  $sql .= " FROM ".$this->from;
103  $sql .= " WHERE d.datedon BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
104  $sql .= " AND ".$this->where;
105  $sql .= " GROUP BY dm";
106  $sql .= $this->db->order('dm', 'DESC');
107 
108  $res = $this->_getNbByMonth($year, $sql, $format);
109  return $res;
110  }
111 
118  public function getNbByYear()
119  {
120  global $user;
121 
122  $sql = "SELECT date_format(d.datedon,'%Y') as dm, COUNT(*) as nb, SUM(d.".$this->field.")";
123  $sql .= " FROM ".$this->from;
124  $sql .= " WHERE ".$this->where;
125  $sql .= " GROUP BY dm";
126  $sql .= $this->db->order('dm', 'DESC');
127 
128  return $this->_getNbByYear($sql);
129  }
130 
136  public function getAllByYear()
137  {
138  global $user;
139 
140  $sql = "SELECT date_format(d.datedon,'%Y') as year, COUNT(*) as nb, SUM(d.".$this->field.") as total, AVG(".$this->field.") as avg";
141  $sql .= " FROM ".$this->from;
142  $sql .= " WHERE ".$this->where;
143  $sql .= " GROUP BY year";
144  $sql .= $this->db->order('year', 'DESC');
145 
146  return $this->_getAllByYear($sql);
147  }
148 }
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:481
getAllByYear()
Return nb, total and average.
_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
_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:498
Class to manage donations.
Definition: don.class.php:37
Class to manage donations statistics.
_getNbByYear($sql)
Return nb of elements by year.