dolibarr  16.0.5
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) {
88  $this->where .= ' WHERE c.fk_user_author = '.((int) $this->userid);
89  }
90  }
91 
99  public function getNbByMonth($year, $format = 0)
100  {
101  global $user;
102 
103  $sql = "SELECT date_format(d.datedon,'%m') as dm, COUNT(*) as nb";
104  $sql .= " FROM ".$this->from;
105  $sql .= " WHERE d.datedon BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
106  $sql .= " AND ".$this->where;
107  $sql .= " GROUP BY dm";
108  $sql .= $this->db->order('dm', 'DESC');
109 
110  $res = $this->_getNbByMonth($year, $sql, $format);
111  return $res;
112  }
113 
120  public function getNbByYear()
121  {
122  global $user;
123 
124  $sql = "SELECT date_format(d.datedon,'%Y') as dm, COUNT(*) as nb, SUM(d.".$this->field.")";
125  $sql .= " FROM ".$this->from;
126  $sql .= " WHERE ".$this->where;
127  $sql .= " GROUP BY dm";
128  $sql .= $this->db->order('dm', 'DESC');
129 
130  return $this->_getNbByYear($sql);
131  }
132 
138  public function getAllByYear()
139  {
140  global $user;
141 
142  $sql = "SELECT date_format(d.datedon,'%Y') as year, COUNT(*) as nb, SUM(d.".$this->field.") as total, AVG(".$this->field.") as avg";
143  $sql .= " FROM ".$this->from;
144  $sql .= " WHERE ".$this->where;
145  $sql .= " GROUP BY year";
146  $sql .= $this->db->order('year', 'DESC');
147 
148  return $this->_getAllByYear($sql);
149  }
150 }
db
$conf db
API class for accounts.
Definition: inc.php:41
DonationStats\__construct
__construct($db, $socid, $mode, $userid=0)
Constructor.
Definition: donstats.class.php:69
Stats
Parent class of statistics class.
Definition: stats.class.php:30
Don
Class to manage donations.
Definition: don.class.php:38
Stats\_getNbByYear
_getNbByYear($sql)
Return nb of elements by year.
Definition: stats.class.php:355
DonationStats\getAllByYear
getAllByYear()
Return nb, total and average.
Definition: donstats.class.php:138
Stats\_getAllByYear
_getAllByYear($sql)
Return nb of elements, total amount and avg amount each year.
Definition: stats.class.php:384
dol_get_first_day
dol_get_first_day($year, $month=1, $gm=false)
Return GMT time for first day of a month or year.
Definition: date.lib.php:551
dol_get_last_day
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
Definition: date.lib.php:570
DonationStats\getNbByYear
getNbByYear()
Return shipments number per year.
Definition: donstats.class.php:120
DonationStats\getNbByMonth
getNbByMonth($year, $format=0)
Return shipment number by month for a year.
Definition: donstats.class.php:99
Stats\_getNbByMonth
_getNbByMonth($year, $sql, $format=0)
Renvoie le nombre de documents par mois pour une annee donnee Return number of documents per month fo...
Definition: stats.class.php:435
DonationStats
Class to manage donations statistics.
Definition: donstats.class.php:35