dolibarr 23.0.3
adherentstats.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3 * Copyright (c) 2005-2011 Laurent Destailleur <eldy@users.sourceforge.net>
4 * Copyright (C) 2005-2009 Regis Houssin <regis.houssin@inodbox.com>
5 * Copyright (C) 2023 Waël Almoman <info@almoman.com>
6 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
7 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 */
22
29include_once DOL_DOCUMENT_ROOT.'/core/class/stats.class.php';
30include_once DOL_DOCUMENT_ROOT.'/adherents/class/subscription.class.php';
31
32
36class AdherentStats extends Stats
37{
41 public $table_element;
42
46 public $memberid;
50 public $socid;
54 public $userid;
55
59 public $from;
63 public $field;
67 public $where;
68
69
77 public function __construct($db, $socid = 0, $userid = 0)
78 {
79 $this->db = $db;
80 $this->socid = $socid;
81 $this->userid = $userid;
82
83 $object = new Subscription($this->db);
84
85 $this->from = MAIN_DB_PREFIX.$object->table_element." as p";
86 $this->from .= ", ".MAIN_DB_PREFIX."adherent as m";
87
88 $this->field = 'subscription';
89
90 $this->where .= " m.statut != -1";
91 $this->where .= " AND p.fk_adherent = m.rowid AND m.entity IN (".getEntity('adherent').")";
92 if ($this->memberid) {
93 $this->where .= " AND m.rowid = ".((int) $this->memberid);
94 }
95 //if ($this->userid > 0) $this->where .= " AND fk_user_author = ".((int) $this->userid);
96 }
97
98
106 public function getNbByMonth($year, $format = 0)
107 {
108 $sql = "SELECT date_format(p.dateadh,'%m') as dm, count(*)";
109 $sql .= " FROM ".$this->from;
110 $sql .= " WHERE ".dolSqlDateFilter('p.dateadh', 0, 0, (int) $year, 1);
111 $sql .= " AND ".$this->where;
112 $sql .= " GROUP BY dm";
113 $sql .= $this->db->order('dm', 'DESC');
114
115 return $this->_getNbByMonth($year, $sql, $format);
116 }
117
123 public function getNbByYear()
124 {
125 $sql = "SELECT date_format(p.dateadh,'%Y') as dm, count(*)";
126 $sql .= " FROM ".$this->from;
127 $sql .= " WHERE ".$this->where;
128 $sql .= " GROUP BY dm";
129 $sql .= $this->db->order('dm', 'DESC');
130
131 return $this->_getNbByYear($sql);
132 }
133
141 public function getAmountByMonth($year, $format = 0)
142 {
143 $sql = "SELECT date_format(p.dateadh,'%m') as dm, sum(p.".$this->field.")";
144 $sql .= " FROM ".$this->from;
145 $sql .= " WHERE ".dolSqlDateFilter('p.dateadh', 0, 0, (int) $year, 1);
146 $sql .= " AND ".$this->where;
147 $sql .= " GROUP BY dm";
148 $sql .= $this->db->order('dm', 'DESC');
149
150 return $this->_getAmountByMonth($year, $sql, $format);
151 }
152
159 public function getAverageByMonth($year)
160 {
161 $sql = "SELECT date_format(p.dateadh,'%m') as dm, avg(p.".$this->field.")";
162 $sql .= " FROM ".$this->from;
163 $sql .= " WHERE ".dolSqlDateFilter('p.dateadh', 0, 0, (int) $year, 1);
164 $sql .= " AND ".$this->where;
165 $sql .= " GROUP BY dm";
166 $sql .= $this->db->order('dm', 'DESC');
167
168 return $this->_getAverageByMonth($year, $sql);
169 }
170
171
177 public function getAllByYear()
178 {
179 $sql = "SELECT date_format(p.dateadh,'%Y') as year, count(*) as nb, sum(".$this->field.") as total, avg(".$this->field.") as avg";
180 $sql .= " FROM ".$this->from;
181 $sql .= " WHERE ".$this->where;
182 $sql .= " GROUP BY year";
183 $sql .= $this->db->order('year', 'DESC');
184
185 return $this->_getAllByYear($sql);
186 }
187
194 public function countMembersByTypeAndStatus($numberYears = 0)
195 {
196 $now = dol_now();
197 $endYear = (int) date('Y');
198 $startYear = $endYear - $numberYears;
199
200 $sql = "SELECT t.rowid as fk_adherent_type, t.libelle as label";
201 $sql .= ", COUNT(".$this->db->ifsql("d.statut = ".Adherent::STATUS_DRAFT, "'members_draft'", 'NULL').") as members_draft";
202 $sql .= ", COUNT(".$this->db->ifsql("d.statut = ".Adherent::STATUS_VALIDATED." AND (d.datefin IS NULL AND t.subscription = '1')", "'members_pending'", 'NULL').") as members_pending";
203 $sql .= ", COUNT(".$this->db->ifsql("d.statut = ".Adherent::STATUS_VALIDATED." AND (d.datefin >= '".$this->db->idate($now)."' OR t.subscription = '0')", "'members_uptodate'", 'NULL').") as members_uptodate";
204 $sql .= ", COUNT(".$this->db->ifsql("d.statut = ".Adherent::STATUS_VALIDATED." AND (d.datefin < '".$this->db->idate($now)."' AND t.subscription = '1')", "'members_expired'", 'NULL').") as members_expired";
205 $sql .= ", COUNT(".$this->db->ifsql("d.statut = ".Adherent::STATUS_EXCLUDED, "'members_excluded'", 'NULL').") as members_excluded";
206 $sql .= ", COUNT(".$this->db->ifsql("d.statut = ".Adherent::STATUS_RESILIATED, "'members_resiliated'", 'NULL').") as members_resiliated";
207 $sql .= " FROM ".MAIN_DB_PREFIX."adherent_type as t";
208 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."adherent as d ON t.rowid = d.fk_adherent_type AND d.entity IN (" . getEntity('adherent') . ")";
209 if ($numberYears) {
210 $sql .= " AND d.datefin > '".$this->db->idate(dol_get_first_day($startYear))."'";
211 }
212 $sql .= " WHERE t.entity IN (".getEntity('member_type').")";
213 $sql .= " AND t.statut = 1";
214 $sql .= " GROUP BY t.rowid, t.libelle";
215
216 dol_syslog("box_members_by_type::select nb of members per type", LOG_DEBUG);
217 $result = $this->db->query($sql);
218
219 $MembersCountArray = array();
220
221 if ($result) {
222 $num = $this->db->num_rows($result);
223 $i = 0;
224 $totalstatus = array(
225 'members_draft' => 0,
226 'members_pending' => 0,
227 'members_uptodate' => 0,
228 'members_expired' => 0,
229 'members_excluded' => 0,
230 'members_resiliated' => 0
231 );
232 while ($i < $num) {
233 $objp = $this->db->fetch_object($result);
234 $MembersCountArray[$objp->fk_adherent_type] = array(
235 'label' => $objp->label,
236 'members_draft' => (int) $objp->members_draft,
237 'members_pending' => (int) $objp->members_pending,
238 'members_uptodate' => (int) $objp->members_uptodate,
239 'members_expired' => (int) $objp->members_expired,
240 'members_excluded' => (int) $objp->members_excluded,
241 'members_resiliated' => (int) $objp->members_resiliated
242 );
243 $totalrow = 0;
244 foreach ($MembersCountArray[$objp->fk_adherent_type] as $key => $nb) {
245 if ($key != 'label') {
246 $totalrow += $nb;
247 $totalstatus[$key] += $nb;
248 }
249 }
250 $MembersCountArray[$objp->fk_adherent_type]['total_adhtype'] = $totalrow;
251 $i++;
252 }
253 $this->db->free($result);
254 $MembersCountArray['total'] = $totalstatus;
255 $MembersCountArray['total']['all'] = array_sum($totalstatus);
256 }
257
258 return $MembersCountArray;
259 }
260
267 public function countMembersByTagAndStatus($numberYears = 0)
268 {
269 $now = dol_now();
270 $endYear = (int) date('Y');
271 $startYear = $endYear - $numberYears;
272 $MembersCountArray = [];
273
274 $sql = "SELECT c.rowid as fk_categorie, c.label as label";
275 $sql .= ", COUNT(".$this->db->ifsql("d.statut = ".Adherent::STATUS_DRAFT, "'members_draft'", 'NULL').") as members_draft";
276 $sql .= ", COUNT(".$this->db->ifsql("d.statut = ".Adherent::STATUS_VALIDATED." AND (d.datefin IS NULL AND t.subscription = '1')", "'members_pending'", 'NULL').") as members_pending";
277 $sql .= ", COUNT(".$this->db->ifsql("d.statut = ".Adherent::STATUS_VALIDATED." AND (d.datefin >= '".$this->db->idate($now)."' OR t.subscription = 0)", "'members_uptodate'", 'NULL').") as members_uptodate";
278 $sql .= ", COUNT(".$this->db->ifsql("d.statut = ".Adherent::STATUS_VALIDATED." AND (d.datefin < '".$this->db->idate($now)."' AND t.subscription = 1)", "'members_expired'", 'NULL').") as members_expired";
279 $sql .= ", COUNT(".$this->db->ifsql("d.statut = ".Adherent::STATUS_EXCLUDED, "'members_excluded'", 'NULL').") as members_excluded";
280 $sql .= ", COUNT(".$this->db->ifsql("d.statut = ".Adherent::STATUS_RESILIATED, "'members_resiliated'", 'NULL').") as members_resiliated";
281 $sql .= " FROM ".MAIN_DB_PREFIX."categorie as c";
282 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."categorie_member as ct ON c.rowid = ct.fk_categorie";
283 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."adherent as d ON d.rowid = ct.fk_member";
284 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."adherent_type as t ON t.rowid = d.fk_adherent_type";
285 $sql .= " WHERE c.entity IN (".getEntity('member_type').")";
286 $sql .= " AND d.entity IN (" . getEntity('adherent') . ")";
287 $sql .= " AND t.entity IN (" . getEntity('adherent') . ")";
288 if ($numberYears) {
289 $sql .= " AND d.datefin > '".$this->db->idate(dol_get_first_day($startYear))."'";
290 }
291 $sql .= " AND c.fk_parent = 0";
292 $sql .= " GROUP BY c.rowid, c.label";
293 $sql .= " ORDER BY label ASC";
294
295 dol_syslog("box_members_by_tag::select nb of members per tag", LOG_DEBUG);
296 $result = $this->db->query($sql);
297
298 if ($result) {
299 $num = $this->db->num_rows($result);
300 $i = 0;
301 $totalstatus = array(
302 'members_draft' => 0,
303 'members_pending' => 0,
304 'members_uptodate' => 0,
305 'members_expired' => 0,
306 'members_excluded' => 0,
307 'members_resiliated' => 0
308 );
309 while ($i < $num) {
310 $objp = $this->db->fetch_object($result);
311 $MembersCountArray[$objp->fk_categorie] = array(
312 'label' => $objp->label,
313 'members_draft' => (int) $objp->members_draft,
314 'members_pending' => (int) $objp->members_pending,
315 'members_uptodate' => (int) $objp->members_uptodate,
316 'members_expired' => (int) $objp->members_expired,
317 'members_excluded' => (int) $objp->members_excluded,
318 'members_resiliated' => (int) $objp->members_resiliated
319 );
320 $totalrow = 0;
321 foreach ($MembersCountArray[$objp->fk_categorie] as $key => $nb) {
322 if ($key != 'label') {
323 $totalrow += $nb;
324 $totalstatus[$key] += $nb;
325 }
326 }
327 $MembersCountArray[$objp->fk_categorie]['total_adhtag'] = $totalrow;
328 $i++;
329 }
330 $this->db->free($result);
331 $MembersCountArray['total'] = $totalstatus;
332 $MembersCountArray['total']['all'] = array_sum($totalstatus);
333 }
334
335 return $MembersCountArray;
336 }
337
344 public function getLastModifiedMembers($max = 0)
345 {
346 $lastModifiedMembers = [];
347
348 $sql = "SELECT a.rowid, a.ref, a.lastname, a.firstname, a.societe as company, a.fk_soc,";
349 $sql .= " a.datec, GREATEST(a.tms, aef.tms) as datem, a.statut as status, a.datefin as date_end_subscription,";
350 $sql .= ' a.photo, a.email, a.gender, a.morphy,';
351 $sql .= " t.rowid as typeid, t.subscription, t.libelle as label";
352 $sql .= " FROM ".MAIN_DB_PREFIX."adherent as a";
353 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'adherent_extrafields as aef ON aef.fk_object = a.rowid';
354 $sql .= ", ".MAIN_DB_PREFIX."adherent_type as t";
355 $sql .= " WHERE a.entity IN (".getEntity('member').")";
356 $sql .= " AND a.fk_adherent_type = t.rowid";
357 $sql .= " ORDER BY datem DESC";
358 $sql .= $this->db->plimit($max, 0);
359
360 $result = $this->db->query($sql);
361 if ($result) {
362 $num = $this->db->num_rows($result);
363
364 $line = 0;
365 while ($line < $num) {
366 $objp = $this->db->fetch_object($result);
367 $lastModifiedMembers[] = [
368 'id' => (int) $objp->rowid,
369 'ref' => (string) $objp->ref,
370 'lastname' => (string) $objp->lastname,
371 'firstname' => (string) $objp->firstname,
372 'company' => (string) $objp->company,
373 'fk_soc' => $objp->fk_soc ? (int) $objp->fk_soc : null,
374 'datec' => $this->db->jdate($objp->datec),
375 'datem' => $this->db->jdate($objp->datem),
376 'status' => (int) $objp->status,
377 'date_end_subscription' => $this->db->jdate($objp->date_end_subscription),
378 'photo' => isset($objp->photo) ? (string) $objp->photo : null,
379 'email' => $objp->email,
380 'gender' => $objp->gender,
381 'morphy' => $objp->morphy,
382 'typeid' => $objp->typeid,
383 'need_subscription' => isset($objp->subscription) ? ($objp->subscription ? 1 : 0) : null,
384 'subscription' => isset($objp->subscription) ? ($objp->subscription ? '1' : '0') : null,
385 'label' => (string) $objp->label,
386 ];
387
388 $line++;
389 }
390 $this->db->free($result);
391 }
392
393 return $lastModifiedMembers;
394 }
395}
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
const STATUS_EXCLUDED
Excluded.
const STATUS_DRAFT
Draft status.
const STATUS_RESILIATED
Resiliated (membership end and was not renew)
const STATUS_VALIDATED
Validated status.
Class to manage statistics of members.
getLastModifiedMembers($max=0)
Return array of last modified members.
getNbByMonth($year, $format=0)
Return the number of members by month for a given year.
getAllByYear()
Return nb, total and average.
__construct($db, $socid=0, $userid=0)
Constructor.
getNbByYear()
Return the number of subscriptions by year.
getAverageByMonth($year)
Return average amount each month.
countMembersByTypeAndStatus($numberYears=0)
Return count of member by status group by adh type, total and average.
getAmountByMonth($year, $format=0)
Return the number of subscriptions by month for a given year.
countMembersByTagAndStatus($numberYears=0)
Return count of member by status group by adh type, total and average.
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...
Class to manage subscriptions of foundation members.
dol_get_first_day($year, $month=1, $gm=false)
Return GMT time for first day of a month or year.
Definition date.lib.php:603
dol_now($mode='gmt')
Return date for now.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.
if(getDolGlobalString( 'TAKEPOS_SHOW_CUSTOMER')) print $langs trans('Date')." left Label right Qty right Price right TotalHT right TotalTTC right right right right right right right right right centpercent right TotalHT right n right VAT right n right TotalVAT right n No sujeto a RE IRPF right TotalLT1 right n right TotalLT2 right n right TotalTTC right n takeposcustomercurrency takeposcustomercurrency takeposcustomercurrency takeposcustomercurrency right TotalTTC takeposcustomercurrency right takeposcustomercurrency n right PaymentTypeShortLIQ right SELECT p pos_change as p datep as date
Definition receipt.php:464