dolibarr 21.0.0-alpha
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
27include_once DOL_DOCUMENT_ROOT.'/core/class/stats.class.php';
28include_once DOL_DOCUMENT_ROOT.'/don/class/don.class.php';
29include_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
30
34class DonationStats extends Stats
35{
39 public $table_element;
40
41 public $socid;
42 public $userid;
43
47 public $from;
48
52 public $field;
53
57 public $where;
58
62 public $join;
63
74 public function __construct($db, $socid, $mode, $userid = 0, $typentid = 0, $status = 4)
75 {
76 global $conf;
77
78 $this->db = $db;
79
80 $this->field = 'amount';
81 $this->socid = ($socid > 0 ? $socid : 0);
82 $this->userid = $userid;
83 $this->cachefilesuffix = $mode;
84 $this->join = '';
85
86 if ($status == 0 || $status == 1 || $status == 2) {
87 $this->where = ' d.fk_statut IN ('.$this->db->sanitize($status).')';
88 } elseif ($status == 3) {
89 $this->where = ' d.fk_statut IN (-1)';
90 } elseif ($status == 4) {
91 $this->where = ' d.fk_statut >= 0';
92 }
93
94 $object = new Don($this->db);
95 $this->from = MAIN_DB_PREFIX.$object->table_element." as d";
96
97 if ($socid !== "-1" && $socid > 0) {
98 $this->where .= " AND d.fk_soc = ".((int) $socid);
99 }
100
101 $this->where .= " AND d.entity = ".$conf->entity;
102 if ($this->userid > 0) {
103 $this->where .= ' AND d.fk_user_author = '.((int) $this->userid);
104 }
105
106 if ($typentid) {
107 $this->join .= ' LEFT JOIN '.MAIN_DB_PREFIX.'societe as s ON s.rowid = d.fk_soc';
108 $this->where .= ' AND s.fk_typent = '.((int) $typentid);
109 }
110 }
111
119 public function getNbByMonth($year, $format = 0)
120 {
121 $sql = "SELECT date_format(d.datedon,'%m') as dm, COUNT(*) as nb";
122 $sql .= " FROM ".$this->from;
123 $sql .= $this->join;
124 $sql .= " WHERE d.datedon BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
125 $sql .= " AND ".$this->where;
126 $sql .= " GROUP BY dm";
127 $sql .= $this->db->order('dm', 'DESC');
128
129 return $this->_getNbByMonth($year, $sql, $format);
130 }
131
138 public function getNbByYear()
139 {
140 $sql = "SELECT date_format(d.datedon,'%Y') as dm, COUNT(*) as nb, SUM(d.".$this->field.")";
141 $sql .= " FROM ".$this->from;
142 $sql .= $this->join;
143 $sql .= " WHERE ".$this->where;
144 $sql .= " GROUP BY dm";
145 $sql .= $this->db->order('dm', 'DESC');
146
147 return $this->_getNbByYear($sql);
148 }
149
157 public function getAmountByMonth($year, $format = 0)
158 {
159 $sql = "SELECT date_format(d.datedon,'%m') as dm, sum(d.".$this->field.")";
160 $sql .= " FROM ".$this->from;
161 $sql .= $this->join;
162 $sql .= " WHERE ".dolSqlDateFilter('d.datedon', 0, 0, (int) $year, 1);
163 $sql .= " AND ".$this->where;
164 $sql .= " GROUP BY dm";
165 $sql .= $this->db->order('dm', 'DESC');
166
167 return $this->_getAmountByMonth($year, $sql, $format);
168 }
169
176 public function getAverageByMonth($year)
177 {
178 $sql = "SELECT date_format(d.datedon,'%m') as dm, avg(d.".$this->field.")";
179 $sql .= " FROM ".$this->from;
180 $sql .= $this->join;
181 $sql .= " WHERE ".dolSqlDateFilter('d.datedon', 0, 0, (int) $year, 1);
182 $sql .= " AND ".$this->where;
183 $sql .= " GROUP BY dm";
184 $sql .= $this->db->order('dm', 'DESC');
185
186 return $this->_getAverageByMonth($year, $sql);
187 }
188
194 public function getAllByYear()
195 {
196 $sql = "SELECT date_format(d.datedon,'%Y') as year, COUNT(*) as nb, SUM(d.".$this->field.") as total, AVG(".$this->field.") as avg";
197 $sql .= " FROM ".$this->from;
198 $sql .= $this->join;
199 $sql .= " WHERE ".$this->where;
200 $sql .= " GROUP BY year";
201 $sql .= $this->db->order('year', 'DESC');
202
203 return $this->_getAllByYear($sql);
204 }
205}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
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:594
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
Definition date.lib.php:613