dolibarr 21.0.0-beta
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 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
28include_once DOL_DOCUMENT_ROOT.'/core/class/stats.class.php';
29include_once DOL_DOCUMENT_ROOT.'/don/class/don.class.php';
30include_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
31
35class DonationStats extends Stats
36{
40 public $table_element;
41
45 public $socid;
49 public $userid;
50
54 public $from;
55
59 public $field;
60
64 public $where;
65
69 public $join;
70
81 public function __construct($db, $socid, $mode, $userid = 0, $typentid = 0, $status = 4)
82 {
83 global $conf;
84
85 $this->db = $db;
86
87 $this->field = 'amount';
88 $this->socid = ($socid > 0 ? $socid : 0);
89 $this->userid = $userid;
90 $this->cachefilesuffix = $mode;
91 $this->join = '';
92
93 if ($status == 0 || $status == 1 || $status == 2) {
94 $this->where = ' d.fk_statut IN ('.$this->db->sanitize($status).')';
95 } elseif ($status == 3) {
96 $this->where = ' d.fk_statut IN (-1)';
97 } elseif ($status == 4) {
98 $this->where = ' d.fk_statut >= 0';
99 }
100
101 $object = new Don($this->db);
102 $this->from = MAIN_DB_PREFIX.$object->table_element." as d";
103
104 if ($socid !== "-1" && $socid > 0) {
105 $this->where .= " AND d.fk_soc = ".((int) $socid);
106 }
107
108 $this->where .= " AND d.entity = ".$conf->entity;
109 if ($this->userid > 0) {
110 $this->where .= ' AND d.fk_user_author = '.((int) $this->userid);
111 }
112
113 if ($typentid) {
114 $this->join .= ' LEFT JOIN '.MAIN_DB_PREFIX.'societe as s ON s.rowid = d.fk_soc';
115 $this->where .= ' AND s.fk_typent = '.((int) $typentid);
116 }
117 }
118
126 public function getNbByMonth($year, $format = 0)
127 {
128 $sql = "SELECT date_format(d.datedon,'%m') as dm, COUNT(*) as nb";
129 $sql .= " FROM ".$this->from;
130 $sql .= $this->join;
131 $sql .= " WHERE d.datedon BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
132 $sql .= " AND ".$this->where;
133 $sql .= " GROUP BY dm";
134 $sql .= $this->db->order('dm', 'DESC');
135
136 return $this->_getNbByMonth($year, $sql, $format);
137 }
138
145 public function getNbByYear()
146 {
147 $sql = "SELECT date_format(d.datedon,'%Y') as dm, COUNT(*) as nb, SUM(d.".$this->field.")";
148 $sql .= " FROM ".$this->from;
149 $sql .= $this->join;
150 $sql .= " WHERE ".$this->where;
151 $sql .= " GROUP BY dm";
152 $sql .= $this->db->order('dm', 'DESC');
153
154 return $this->_getNbByYear($sql);
155 }
156
164 public function getAmountByMonth($year, $format = 0)
165 {
166 $sql = "SELECT date_format(d.datedon,'%m') as dm, sum(d.".$this->field.")";
167 $sql .= " FROM ".$this->from;
168 $sql .= $this->join;
169 $sql .= " WHERE ".dolSqlDateFilter('d.datedon', 0, 0, (int) $year, 1);
170 $sql .= " AND ".$this->where;
171 $sql .= " GROUP BY dm";
172 $sql .= $this->db->order('dm', 'DESC');
173
174 return $this->_getAmountByMonth($year, $sql, $format);
175 }
176
183 public function getAverageByMonth($year)
184 {
185 $sql = "SELECT date_format(d.datedon,'%m') as dm, avg(d.".$this->field.")";
186 $sql .= " FROM ".$this->from;
187 $sql .= $this->join;
188 $sql .= " WHERE ".dolSqlDateFilter('d.datedon', 0, 0, (int) $year, 1);
189 $sql .= " AND ".$this->where;
190 $sql .= " GROUP BY dm";
191 $sql .= $this->db->order('dm', 'DESC');
192
193 return $this->_getAverageByMonth($year, $sql);
194 }
195
201 public function getAllByYear()
202 {
203 $sql = "SELECT date_format(d.datedon,'%Y') as year, COUNT(*) as nb, SUM(d.".$this->field.") as total, AVG(".$this->field.") as avg";
204 $sql .= " FROM ".$this->from;
205 $sql .= $this->join;
206 $sql .= " WHERE ".$this->where;
207 $sql .= " GROUP BY year";
208 $sql .= $this->db->order('year', 'DESC');
209
210 return $this->_getAllByYear($sql);
211 }
212}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:66
Class to manage donations.
Definition don.class.php:41
Class to manage donations statistics.
getAmountByMonth($year, $format=0)
Return the number of subscriptions by month for a given year.
getNbByMonth($year, $format=0)
Return shipment number by month for a year.
__construct($db, $socid, $mode, $userid=0, $typentid=0, $status=4)
Constructor.
getAllByYear()
Return nb, total and average.
getNbByYear()
Return shipments number per year.
getAverageByMonth($year)
Return average amount each month.
Parent class of statistics class.
_getAverageByMonth($year, $sql, $format=0)
Return the amount average par month for a given year.
_getAmountByMonth($year, $sql, $format=0)
Return the amount per month for a given year.
_getNbByYear($sql)
Return nb of elements by year.
_getAllByYear($sql)
Return nb of elements, total amount and avg amount each year.
_getNbByMonth($year, $sql, $format=0)
Renvoie le nombre de documents par mois pour une annee donnee Return number of documents per month fo...
dol_get_first_day($year, $month=1, $gm=false)
Return GMT time for first day of a month or year.
Definition date.lib.php:600
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
Definition date.lib.php:619
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79