dolibarr  17.0.4
box_members_by_type.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2003-2007 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3  * Copyright (C) 2004-2017 Laurent Destailleur <eldy@users.sourceforge.net>
4  * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@inodbox.com>
5  * Copyright (C) 2015-2020 Frederic France <frederic.france@netlogic.fr>
6  * Copyright (C) 2021 WaĆ«l Almoman <info@almoman.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 
28 include_once DOL_DOCUMENT_ROOT . '/core/boxes/modules_boxes.php';
29 
30 
35 {
36  public $boxcode = "box_members_by_type";
37  public $boximg = "object_user";
38  public $boxlabel = "BoxTitleMembersByType";
39  public $depends = array("adherent");
40 
44  public $db;
45 
46  public $param;
47  public $enabled = 1;
48 
49  public $info_box_head = array();
50  public $info_box_contents = array();
51 
52 
59  public function __construct($db, $param = '')
60  {
61  global $conf, $user;
62 
63  $this->db = $db;
64 
65  // disable module for such cases
66  $listofmodulesforexternal = explode(',', $conf->global->MAIN_MODULES_FOR_EXTERNAL);
67  if (!in_array('adherent', $listofmodulesforexternal) && !empty($user->socid)) {
68  $this->enabled = 0; // disabled for external users
69  }
70 
71  $this->hidden = !(isModEnabled('adherent') && $user->rights->adherent->lire);
72  }
73 
80  public function loadBox($max = 5)
81  {
82  global $user, $langs, $conf;
83  $langs->loadLangs(array("boxes", "members"));
84 
85  $this->max = $max;
86 
87  include_once DOL_DOCUMENT_ROOT . '/adherents/class/adherent.class.php';
88  require_once DOL_DOCUMENT_ROOT . '/adherents/class/adherent_type.class.php';
89  $staticmember = new Adherent($this->db);
90 
91  $this->info_box_head = array('text' => $langs->trans("BoxTitleMembersByType", $max));
92 
93  if ($user->rights->adherent->lire) {
94  $MembersToValidate = array();
95  $MembersPending = array();
96  $MembersValidated = array();
97  $MembersUpToDate = array();
98  $MembersExcluded = array();
99  $MembersResiliated = array();
100 
101  $SumToValidate = 0;
102  $SumPending = 0;
103  $SumExpired = 0;
104  $SumUpToDate = 0;
105  $SumResiliated = 0;
106  $SumExcluded = 0;
107 
108  $AdherentType = array();
109 
110  // Type of membership
111  $sql = "SELECT t.rowid, t.libelle as label, t.subscription,";
112  $sql .= " d.statut, count(d.rowid) as somme";
113  $sql .= " FROM " . MAIN_DB_PREFIX . "adherent_type as t";
114  $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "adherent as d";
115  $sql .= " ON t.rowid = d.fk_adherent_type";
116  $sql .= " AND d.entity IN (" . getEntity('adherent') . ")";
117  $sql .= " WHERE t.entity IN (" . getEntity('member_type') . ")";
118  $sql .= " GROUP BY t.rowid, t.libelle, t.subscription, d.statut";
119 
120  dol_syslog("box_members_by_type::select nb of members per type", LOG_DEBUG);
121  $result = $this->db->query($sql);
122  if ($result) {
123  $num = $this->db->num_rows($result);
124  $i = 0;
125  while ($i < $num) {
126  $objp = $this->db->fetch_object($result);
127 
128  $adhtype = new AdherentType($this->db);
129  $adhtype->id = $objp->rowid;
130  $adhtype->subscription = $objp->subscription;
131  $adhtype->label = $objp->label;
132  $AdherentType[$objp->rowid] = $adhtype;
133 
134  if ($objp->statut == Adherent::STATUS_DRAFT) {
135  $MembersToValidate[$objp->rowid] = $objp->somme;
136  }
137  if ($objp->statut == Adherent::STATUS_VALIDATED) {
138  $MembersValidated[$objp->rowid] = $objp->somme;
139  }
140  if ($objp->statut == Adherent::STATUS_EXCLUDED) {
141  $MembersExcluded[$objp->rowid] = $objp->somme;
142  }
143  if ($objp->statut == Adherent::STATUS_RESILIATED) {
144  $MembersResiliated[$objp->rowid] = $objp->somme;
145  }
146 
147  $i++;
148  }
149  $this->db->free($result);
150  $now = dol_now();
151 
152  // Members up to date list
153  // current rule: uptodate = the end date is in future whatever is type
154  // old rule: uptodate = if type does not need payment, that end date is null, if type need payment that end date is in future)
155  $sql = "SELECT count(*) as somme , d.fk_adherent_type";
156  $sql .= " FROM " . MAIN_DB_PREFIX . "adherent as d, " . MAIN_DB_PREFIX . "adherent_type as t";
157  $sql .= " WHERE d.entity IN (" . getEntity('adherent') . ")";
158  $sql .= " AND d.statut = 1 AND (d.datefin >= '" . $this->db->idate($now) . "' OR t.subscription = 0)";
159  $sql .= " AND t.rowid = d.fk_adherent_type";
160  $sql .= " GROUP BY d.fk_adherent_type";
161 
162  dol_syslog("index.php::select nb of uptodate members by type", LOG_DEBUG);
163  $result = $this->db->query($sql);
164  if ($result) {
165  $num2 = $this->db->num_rows($result);
166  $i = 0;
167  while ($i < $num2) {
168  $objp = $this->db->fetch_object($result);
169  $MembersUpToDate[$objp->fk_adherent_type] = $objp->somme;
170  $i++;
171  }
172  $this->db->free($result);
173  }
174  // Members pendding (Waiting for first subscription)
175  $sql = "SELECT count(*) as somme , d.fk_adherent_type";
176  $sql .= " FROM " . MAIN_DB_PREFIX . "adherent as d, " . MAIN_DB_PREFIX . "adherent_type as t";
177  $sql .= " WHERE d.entity IN (" . getEntity('adherent') . ")";
178  $sql .= " AND d.statut = 1 AND (d.datefin IS NULL AND t.subscription = 1)";
179  $sql .= " AND t.rowid = d.fk_adherent_type";
180  $sql .= " GROUP BY d.fk_adherent_type";
181 
182  dol_syslog("index.php::select nb of uptodate members by type", LOG_DEBUG);
183  $result = $this->db->query($sql);
184  if ($result) {
185  $num2 = $this->db->num_rows($result);
186  $i = 0;
187  while ($i < $num2) {
188  $objp = $this->db->fetch_object($result);
189  $MembersPending[$objp->fk_adherent_type] = $objp->somme;
190  $i++;
191  }
192  $this->db->free($result);
193  }
194 
195  $line = 0;
196  $this->info_box_contents[$line][] = array(
197  'td' => 'class=""',
198  'text' => '',
199  );
200  // Draft
201  $labelstatus = $staticmember->LibStatut($staticmember::STATUS_DRAFT, 0, 0, 1);
202  $this->info_box_contents[$line][] = array(
203  'td' => 'class="right tdoverflowmax100" width="15%" title="'.dol_escape_htmltag($labelstatus).'"',
204  'text' => $labelstatus
205  );
206  // Pending (Waiting for first subscription)
207  $labelstatus = $staticmember->LibStatut($staticmember::STATUS_VALIDATED, 1, 0, 1);
208  $this->info_box_contents[$line][] = array(
209  'td' => 'class="right tdoverflowmax100" width="15%" title="'.dol_escape_htmltag($labelstatus).'"',
210  'text' => $labelstatus
211  );
212  // Up to date
213  $labelstatus = $staticmember->LibStatut($staticmember::STATUS_VALIDATED, 1, dol_now() + 86400, 1);
214  $this->info_box_contents[$line][] = array(
215  'td' => 'class="right tdoverflowmax100" width="15%" title="'.dol_escape_htmltag($labelstatus).'"',
216  'text' => $labelstatus,
217  );
218  // Expired
219  $labelstatus = $staticmember->LibStatut($staticmember::STATUS_VALIDATED, 1, dol_now() - 86400, 1);
220  $this->info_box_contents[$line][] = array(
221  'td' => 'class="right tdoverflowmax100" width="15%" title="'.dol_escape_htmltag($labelstatus).'"',
222  'text' => $labelstatus
223  );
224  // Excluded
225  $labelstatus = $staticmember->LibStatut($staticmember::STATUS_EXCLUDED, 0, 0, 1);
226  $this->info_box_contents[$line][] = array(
227  'td' => 'class="right tdoverflowmax100" width="15%" title="'.dol_escape_htmltag($labelstatus).'"',
228  'text' => $labelstatus
229  );
230  // Resiliated
231  $labelstatus = $staticmember->LibStatut($staticmember::STATUS_RESILIATED, 0, 0, 1);
232  $this->info_box_contents[$line][] = array(
233  'td' => 'class="right tdoverflowmax100" width="15%" title="'.dol_escape_htmltag($labelstatus).'"',
234  'text' => $labelstatus
235  );
236  $line++;
237 
238  foreach ($AdherentType as $key => $adhtype) {
239  $SumToValidate += isset($MembersToValidate[$key]) ? $MembersToValidate[$key] : 0;
240  $SumPending += isset($MembersPending[$key]) ? $MembersPending[$key] : 0;
241  $SumExpired += isset($MembersValidated[$key]) ? $MembersValidated[$key] - (isset($MembersUpToDate[$key]) ? $MembersUpToDate[$key] : 0) - (isset($MembersPending[$key]) ? $MembersPending[$key] : 0): 0;
242  $SumUpToDate += isset($MembersUpToDate[$key]) ? $MembersUpToDate[$key] : 0;
243  $SumExcluded += isset($MembersExcluded[$key]) ? $MembersExcluded [$key] : 0;
244  $SumResiliated += isset($MembersResiliated[$key]) ? $MembersResiliated[$key] : 0;
245 
246  $this->info_box_contents[$line][] = array(
247  'td' => 'class="tdoverflowmax150 maxwidth150onsmartphone"',
248  'text' => $adhtype->getNomUrl(1, dol_size(32)),
249  'asis' => 1,
250  );
251  $this->info_box_contents[$line][] = array(
252  'td' => 'class="right"',
253  'text' => (isset($MembersToValidate[$key]) && $MembersToValidate[$key] > 0 ? $MembersToValidate[$key] : '') . ' ' . $staticmember->LibStatut(Adherent::STATUS_DRAFT, 1, 0, 3),
254  'asis' => 1,
255  );
256  $this->info_box_contents[$line][] = array(
257  'td' => 'class="right"',
258  'text' => (isset($MembersPending[$key]) && $MembersPending[$key] > 0 ? $MembersPending[$key] : '') . ' ' . $staticmember->LibStatut(Adherent::STATUS_VALIDATED, 1, 0, 3),
259  'asis' => 1,
260  );
261  $this->info_box_contents[$line][] = array(
262  'td' => 'class="right"',
263  'text' => (isset($MembersUpToDate[$key]) && $MembersUpToDate[$key] > 0 ? $MembersUpToDate[$key] : '') . ' ' . $staticmember->LibStatut(Adherent::STATUS_VALIDATED, 1, $now, 3),
264  'asis' => 1,
265  );
266  $this->info_box_contents[$line][] = array(
267  'td' => 'class="right"',
268  'text' => (isset($MembersValidated[$key]) && ($MembersValidated[$key] - (isset($MembersUpToDate[$key]) ? $MembersUpToDate[$key] : 0) > 0) ? $MembersValidated[$key] - (isset($MembersUpToDate[$key]) ? $MembersUpToDate[$key] : 0) : '') . ' ' . $staticmember->LibStatut(Adherent::STATUS_VALIDATED, 1, 1, 3),
269  'asis' => 1,
270  );
271  $this->info_box_contents[$line][] = array(
272  'td' => 'class="right"',
273  'text' => (isset($MembersExcluded[$key]) && $MembersExcluded[$key] > 0 ? $MembersExcluded[$key] : '') . ' ' . $staticmember->LibStatut(Adherent::STATUS_EXCLUDED, 1, $now, 3),
274  'asis' => 1,
275  );
276  $this->info_box_contents[$line][] = array(
277  'td' => 'class="right"',
278  'text' => (isset($MembersResiliated[$key]) && $MembersResiliated[$key] > 0 ? $MembersResiliated[$key] : '') . ' ' . $staticmember->LibStatut(Adherent::STATUS_RESILIATED, 1, 0, 3),
279  'asis' => 1,
280  );
281 
282  $line++;
283  }
284 
285  if ($num == 0) {
286  $this->info_box_contents[$line][0] = array(
287  'td' => 'colspan="7" class="center"',
288  'text' => $langs->trans("NoRecordedMembersByType")
289  );
290  } else {
291  $this->info_box_contents[$line][] = array(
292  'tr' => 'class="liste_total"',
293  'td' => 'class="liste_total"',
294  'text' => $langs->trans("Total")
295  );
296  $this->info_box_contents[$line][] = array(
297  'td' => 'class="liste_total right"',
298  'text' => $SumToValidate.' '.$staticmember->LibStatut(Adherent::STATUS_DRAFT, 1, 0, 3),
299  'asis' => 1
300  );
301  $this->info_box_contents[$line][] = array(
302  'td' => 'class="liste_total right"',
303  'text' => $SumPending.' '.$staticmember->LibStatut(Adherent::STATUS_VALIDATED, 1, 0, 3),
304  'asis' => 1
305  );
306  $this->info_box_contents[$line][] = array(
307  'td' => 'class="liste_total right"',
308  'text' => $SumUpToDate.' '.$staticmember->LibStatut(Adherent::STATUS_VALIDATED, 1, $now, 3),
309  'asis' => 1
310  );
311  $this->info_box_contents[$line][] = array(
312  'td' => 'class="liste_total right"',
313  'text' => $SumExpired.' '.$staticmember->LibStatut(Adherent::STATUS_VALIDATED, 1, 1, 3),
314  'asis' => 1
315  );
316  $this->info_box_contents[$line][] = array(
317  'td' => 'class="liste_total right"',
318  'text' => $SumExcluded.' '.$staticmember->LibStatut(Adherent::STATUS_EXCLUDED, 1, 0, 3),
319  'asis' => 1
320  );
321  $this->info_box_contents[$line][] = array(
322  'td' => 'class="liste_total right"',
323  'text' => $SumResiliated.' '.$staticmember->LibStatut(Adherent::STATUS_RESILIATED, 1, 0, 3),
324  'asis' => 1
325  );
326  }
327  } else {
328  $this->info_box_contents[0][0] = array(
329  'td' => '',
330  'maxlength' => 500,
331  'text' => ($this->db->error() . ' sql=' . $sql)
332  );
333  }
334  } else {
335  $this->info_box_contents[0][0] = array(
336  'td' => 'class="nohover opacitymedium left"',
337  'text' => $langs->trans("ReadPermissionNotAllowed")
338  );
339  }
340  }
341 
350  public function showBox($head = null, $contents = null, $nooutput = 0)
351  {
352  return parent::showBox($this->info_box_head, $this->info_box_contents, $nooutput);
353  }
354 }
Class to manage members of a foundation.
const STATUS_EXCLUDED
Excluded.
const STATUS_DRAFT
Draft status.
const STATUS_RESILIATED
Resiliated.
const STATUS_VALIDATED
Validated status.
Class to manage members type.
Class ModeleBoxes.
Class to manage the box to show last modofied members.
loadBox($max=5)
Load data into info_box_contents array to show array later.
__construct($db, $param='')
Constructor.
showBox($head=null, $contents=null, $nooutput=0)
Method to show box.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0)
Returns text escaped for inclusion in HTML alt or title tags, or into values of HTML input fields.
dol_now($mode='auto')
Return date for now.
dol_size($size, $type='')
Optimize a size for some browsers (phone, smarphone, ...)
isModEnabled($module)
Is Dolibarr module enabled.
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.
$conf db
API class for accounts.
Definition: inc.php:41