dolibarr  9.0.0
adherent.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2002-2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3  * Copyright (C) 2002-2003 Jean-Louis Bergamo <jlb@j1b.org>
4  * Copyright (C) 2004-2012 Laurent Destailleur <eldy@users.sourceforge.net>
5  * Copyright (C) 2004 Sebastien Di Cintio <sdicintio@ressource-toi.org>
6  * Copyright (C) 2004 Benoit Mortier <benoit.mortier@opensides.be>
7  * Copyright (C) 2009-2017 Regis Houssin <regis.houssin@inodbox.com>
8  * Copyright (C) 2014-2016 Alexandre Spangaro <aspangaro.dolibarr@gmail.com>
9  * Copyright (C) 2015 Marcos García <marcosgdf@gmail.com>
10  * Copyright (C) 2015-2018 Frédéric France <frederic.france@netlogic.fr>
11  * Copyright (C) 2015 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
12  * Copyright (C) 2016 Juanjo Menent <jmenent@2byte.es>
13  *
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 3 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program. If not, see <http://www.gnu.org/licenses/>.
26  */
27 
34 require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
35 require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
36 require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
37 
38 
42 class Adherent extends CommonObject
43 {
47  public $element='member';
48 
52  public $table_element='adherent';
53 
58  public $ismultientitymanaged = 1;
59 
60  public $mesgs;
61 
65  public $login;
66 
68  public $pass;
73 
78  public $societe;
79 
83  public $company;
84 
88  public $fk_soc;
89 
93  public $address;
94 
98  public $zip;
99 
103  public $town;
104 
108  public $state_id;
109 
113  public $state_code;
114 
118  public $state;
119 
123  public $email;
124 
128  public $skype;
129 
133  public $twitter;
134 
138  public $facebook;
139 
143  public $phone;
144 
148  public $phone_perso;
149 
153  public $phone_mobile;
154 
158  public $fax;
159 
163  public $poste;
164 
165  public $morphy;
166  public $public;
167 
168  // -1:brouillon, 0:resilie, >=1:valide,paye
169  // def in common object
170  //public $statut;
171 
172  public $photo;
173 
174  public $datec;
175  public $datem;
176  public $datevalid;
177 
178  public $birth;
179 
183  public $typeid;
184 
188  public $type;
189  public $need_subscription;
190 
191  public $user_id;
192  public $user_login;
193 
194  public $datefin; // From member table
195 
196  // Fields loaded by fetch_subscriptions()
197  public $first_subscription_date;
198  public $first_subscription_amount;
199  public $last_subscription_date;
200  public $last_subscription_date_start;
201  public $last_subscription_date_end;
202  public $last_subscription_amount;
203  public $subscriptions=array();
204 
208  public $oldcopy;
209 
213  public $entity;
214 
220  function __construct($db)
221  {
222  $this->db = $db;
223  $this->statut = -1;
224  // l'adherent n'est pas public par defaut
225  $this->public = 0;
226  // les champs optionnels sont vides
227  $this->array_options=array();
228  }
229 
230 
231  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
248  function send_an_email($text, $subject, $filename_list=array(), $mimetype_list=array(), $mimefilename_list=array(), $addr_cc="", $addr_bcc="", $deliveryreceipt=0, $msgishtml=-1, $errors_to='', $moreinheader='')
249  {
250  // phpcs:enable
251  global $conf,$langs;
252 
253  // Detect if message is HTML
254  if ($msgishtml == -1)
255  {
256  $msgishtml = 0;
257  if (dol_textishtml($text,0)) $msgishtml = 1;
258  }
259 
260  dol_syslog('send_an_email msgishtml='.$msgishtml);
261 
262  $texttosend=$this->makeSubstitution($text);
263  $subjecttosend=$this->makeSubstitution($subject);
264  if ($msgishtml) $texttosend=dol_htmlentitiesbr($texttosend);
265 
266  // Envoi mail confirmation
267  $from=$conf->email_from;
268  if (! empty($conf->global->ADHERENT_MAIL_FROM)) $from=$conf->global->ADHERENT_MAIL_FROM;
269 
270  $trackid = 'mem'.$this->id;
271 
272  // Send email (substitutionarray must be done just before this)
273  include_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
274  $mailfile = new CMailFile($subjecttosend, $this->email, $from, $texttosend, $filename_list, $mimetype_list, $mimefilename_list, $addr_cc, $addr_bcc, $deliveryreceipt, $msgishtml, '', '', $trackid, $moreinheader);
275  if ($mailfile->sendfile())
276  {
277  return 1;
278  }
279  else
280  {
281  $this->error=$langs->trans("ErrorFailedToSendMail",$from,$this->email).'. '.$mailfile->error;
282  return -1;
283  }
284  }
285 
286 
293  function makeSubstitution($text)
294  {
295  global $conf,$langs;
296 
297  $birthday = dol_print_date($this->birth,'day');
298 
299  $msgishtml = 0;
300  if (dol_textishtml($text,1)) $msgishtml = 1;
301 
302  $infos='';
303  if ($this->civility_id) $infos.= $langs->transnoentities("UserTitle").": ".$this->getCivilityLabel()."\n";
304  $infos.= $langs->transnoentities("id").": ".$this->id."\n";
305  $infos.= $langs->transnoentities("Lastname").": ".$this->lastname."\n";
306  $infos.= $langs->transnoentities("Firstname").": ".$this->firstname."\n";
307  $infos.= $langs->transnoentities("Company").": ".$this->societe."\n";
308  $infos.= $langs->transnoentities("Address").": ".$this->address."\n";
309  $infos.= $langs->transnoentities("Zip").": ".$this->zip."\n";
310  $infos.= $langs->transnoentities("Town").": ".$this->town."\n";
311  $infos.= $langs->transnoentities("Country").": ".$this->country."\n";
312  $infos.= $langs->transnoentities("EMail").": ".$this->email."\n";
313  $infos.= $langs->transnoentities("PhonePro").": ".$this->phone."\n";
314  $infos.= $langs->transnoentities("PhonePerso").": ".$this->phone_perso."\n";
315  $infos.= $langs->transnoentities("PhoneMobile").": ".$this->phone_mobile."\n";
316  if (empty($conf->global->ADHERENT_LOGIN_NOT_REQUIRED))
317  {
318  $infos.= $langs->transnoentities("Login").": ".$this->login."\n";
319  $infos.= $langs->transnoentities("Password").": ".$this->pass."\n";
320  }
321  $infos.= $langs->transnoentities("Birthday").": ".$birthday."\n";
322  $infos.= $langs->transnoentities("Photo").": ".$this->photo."\n";
323  $infos.= $langs->transnoentities("Public").": ".yn($this->public);
324 
325  // Substitutions
326  $substitutionarray=array(
327  '__ID__'=>$this->id,
328  '__MEMBER_ID__'=>$this->id,
329  '__CIVILITY__'=>$this->getCivilityLabel(),
330  '__FIRSTNAME__'=>$msgishtml?dol_htmlentitiesbr($this->firstname):($this->firstname?$this->firstname:''),
331  '__LASTNAME__'=>$msgishtml?dol_htmlentitiesbr($this->lastname):($this->lastname?$this->lastname:''),
332  '__FULLNAME__'=>$msgishtml?dol_htmlentitiesbr($this->getFullName($langs)):$this->getFullName($langs),
333  '__COMPANY__'=>$msgishtml?dol_htmlentitiesbr($this->societe):($this->societe?$this->societe:''),
334  '__ADDRESS__'=>$msgishtml?dol_htmlentitiesbr($this->address):($this->address?$this->address:''),
335  '__ZIP__'=>$msgishtml?dol_htmlentitiesbr($this->zip):($this->zip?$this->zip:''),
336  '__TOWN__'=>$msgishtml?dol_htmlentitiesbr($this->town):($this->town?$this->town:''),
337  '__COUNTRY__'=>$msgishtml?dol_htmlentitiesbr($this->country):($this->country?$this->country:''),
338  '__EMAIL__'=>$msgishtml?dol_htmlentitiesbr($this->email):($this->email?$this->email:''),
339  '__BIRTH__'=>$msgishtml?dol_htmlentitiesbr($birthday):($birthday?$birthday:''),
340  '__PHOTO__'=>$msgishtml?dol_htmlentitiesbr($this->photo):($this->photo?$this->photo:''),
341  '__LOGIN__'=>$msgishtml?dol_htmlentitiesbr($this->login):($this->login?$this->login:''),
342  '__PASSWORD__'=>$msgishtml?dol_htmlentitiesbr($this->pass):($this->pass?$this->pass:''),
343  '__PHONE__'=>$msgishtml?dol_htmlentitiesbr($this->phone):($this->phone?$this->phone:''),
344  '__PHONEPRO__'=>$msgishtml?dol_htmlentitiesbr($this->phone_perso):($this->phone_perso?$this->phone_perso:''),
345  '__PHONEMOBILE__'=>$msgishtml?dol_htmlentitiesbr($this->phone_mobile):($this->phone_mobile?$this->phone_mobile:'')
346  );
347 
348  complete_substitutions_array($substitutionarray, $langs, $this);
349 
350  return make_substitutions($text, $substitutionarray, $langs);
351  }
352 
353 
360  function getmorphylib($morphy='')
361  {
362  global $langs;
363  if (! $morphy) { $morphy=$this->morphy; }
364  if ($morphy == 'phy') { return $langs->trans("Physical"); }
365  if ($morphy == 'mor') { return $langs->trans("Moral"); }
366  return $morphy;
367  }
368 
376  function create($user,$notrigger=0)
377  {
378  global $conf,$langs;
379 
380  $error=0;
381 
382  $now=dol_now();
383 
384  // Clean parameters
385  $this->import_key = trim($this->import_key);
386 
387  // Check parameters
388  if (! empty($conf->global->ADHERENT_MAIL_REQUIRED) && ! isValidEMail($this->email))
389  {
390  $langs->load("errors");
391  $this->error = $langs->trans("ErrorBadEMail",$this->email);
392  return -1;
393  }
394  if (! $this->datec) $this->datec=$now;
395  if (empty($conf->global->ADHERENT_LOGIN_NOT_REQUIRED))
396  {
397  if (empty($this->login))
398  {
399  $this->error = $langs->trans("ErrorWrongValueForParameterX","Login");
400  return -1;
401  }
402  }
403 
404  $this->db->begin();
405 
406  // Insert member
407  $sql = "INSERT INTO ".MAIN_DB_PREFIX."adherent";
408  $sql.= " (datec,login,fk_user_author,fk_user_mod,fk_user_valid,morphy,fk_adherent_type,entity,import_key)";
409  $sql.= " VALUES (";
410  $sql.= " '".$this->db->idate($this->datec)."'";
411  $sql.= ", ".($this->login?"'".$this->db->escape($this->login)."'":"null");
412  $sql.= ", ".($user->id>0?$user->id:"null"); // Can be null because member can be created by a guest or a script
413  $sql.= ", null, null, '".$this->db->escape($this->morphy)."'";
414  $sql.= ", ".$this->typeid;
415  $sql.= ", ".$conf->entity;
416  $sql.= ", ".(! empty($this->import_key) ? "'".$this->db->escape($this->import_key)."'":"null");
417  $sql.= ")";
418 
419  dol_syslog(get_class($this)."::create", LOG_DEBUG);
420  $result = $this->db->query($sql);
421  if ($result)
422  {
423  $id = $this->db->last_insert_id(MAIN_DB_PREFIX."adherent");
424  if ($id > 0)
425  {
426  $this->id=$id;
427  $this->ref=(string) $id;
428 
429  // Update minor fields
430  $result=$this->update($user,1,1,0,0,'add'); // nosync is 1 to avoid update data of user
431  if ($result < 0)
432  {
433  $this->db->rollback();
434  return -1;
435  }
436 
437  // Add link to user
438  if ($this->user_id)
439  {
440  // Add link to user
441  $sql = "UPDATE ".MAIN_DB_PREFIX."user SET";
442  $sql.= " fk_member = ".$this->id;
443  $sql.= " WHERE rowid = ".$this->user_id;
444  dol_syslog(get_class($this)."::create", LOG_DEBUG);
445  $resql = $this->db->query($sql);
446  if (! $resql)
447  {
448  $this->error='Failed to update user to make link with member';
449  $this->db->rollback();
450  return -4;
451  }
452  }
453 
454  if (! $notrigger)
455  {
456  // Call trigger
457  $result=$this->call_trigger('MEMBER_CREATE',$user);
458  if ($result < 0) { $error++; }
459  // End call triggers
460  }
461 
462  if (count($this->errors))
463  {
464  dol_syslog(get_class($this)."::create ".implode(',',$this->errors), LOG_ERR);
465  $this->db->rollback();
466  return -3;
467  }
468  else
469  {
470  $this->db->commit();
471  return $this->id;
472  }
473  }
474  else
475  {
476  $this->error='Failed to get last insert id';
477  dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
478  $this->db->rollback();
479  return -2;
480  }
481  }
482  else
483  {
484  $this->error=$this->db->error();
485  $this->db->rollback();
486  return -1;
487  }
488  }
489 
490 
502  function update($user,$notrigger=0,$nosyncuser=0,$nosyncuserpass=0,$nosyncthirdparty=0,$action='update')
503  {
504  global $conf, $langs, $hookmanager;
505 
506  $nbrowsaffected=0;
507  $error=0;
508 
509  dol_syslog(get_class($this)."::update notrigger=".$notrigger.", nosyncuser=".$nosyncuser.", nosyncuserpass=".$nosyncuserpass." nosyncthirdparty=".$nosyncthirdparty.", email=".$this->email);
510 
511  // Clean parameters
512  $this->lastname=trim($this->lastname)?trim($this->lastname):trim($this->lastname);
513  $this->firstname=trim($this->firstname)?trim($this->firstname):trim($this->firstname);
514  $this->address=($this->address?$this->address:$this->address);
515  $this->zip=($this->zip?$this->zip:$this->zip);
516  $this->town=($this->town?$this->town:$this->town);
517  $this->country_id=($this->country_id > 0?$this->country_id:$this->country_id);
518  $this->state_id=($this->state_id > 0?$this->state_id:$this->state_id);
519  if (! empty($conf->global->MAIN_FIRST_TO_UPPER)) $this->lastname=ucwords(trim($this->lastname));
520  if (! empty($conf->global->MAIN_FIRST_TO_UPPER)) $this->firstname=ucwords(trim($this->firstname));
521  $this->note_public=($this->note_public?$this->note_public:$this->note_public);
522  $this->note_private=($this->note_private?$this->note_private:$this->note_private);
523 
524  // Check parameters
525  if (! empty($conf->global->ADHERENT_MAIL_REQUIRED) && ! isValidEMail($this->email))
526  {
527  $langs->load("errors");
528  $this->error = $langs->trans("ErrorBadEMail",$this->email);
529  return -1;
530  }
531 
532  $this->db->begin();
533 
534  $sql = "UPDATE ".MAIN_DB_PREFIX."adherent SET";
535  $sql.= " civility = ".($this->civility_id?"'".$this->db->escape($this->civility_id)."'":"null");
536  $sql.= ", firstname = ".($this->firstname?"'".$this->db->escape($this->firstname)."'":"null");
537  $sql.= ", lastname = ".($this->lastname?"'".$this->db->escape($this->lastname)."'":"null");
538  $sql.= ", login = ".($this->login?"'".$this->db->escape($this->login)."'":"null");
539  $sql.= ", societe = ".($this->societe?"'".$this->db->escape($this->societe)."'":"null");
540  $sql.= ", fk_soc = ".($this->fk_soc > 0?$this->db->escape($this->fk_soc):"null");
541  $sql.= ", address = ".($this->address?"'".$this->db->escape($this->address)."'":"null");
542  $sql.= ", zip = ".($this->zip?"'".$this->db->escape($this->zip)."'":"null");
543  $sql.= ", town = ".($this->town?"'".$this->db->escape($this->town)."'":"null");
544  $sql.= ", country = ".($this->country_id>0?$this->db->escape($this->country_id):"null");
545  $sql.= ", state_id = ".($this->state_id>0?$this->db->escape($this->state_id):"null");
546  $sql.= ", email = '".$this->db->escape($this->email)."'";
547  $sql.= ", skype = '".$this->db->escape($this->skype)."'";
548  $sql.= ", twitter = '".$this->db->escape($this->twitter)."'";
549  $sql.= ", facebook = '".$this->db->escape($this->facebook)."'";
550  $sql.= ", phone = ".($this->phone?"'".$this->db->escape($this->phone)."'":"null");
551  $sql.= ", phone_perso = ".($this->phone_perso?"'".$this->db->escape($this->phone_perso)."'":"null");
552  $sql.= ", phone_mobile = ".($this->phone_mobile?"'".$this->db->escape($this->phone_mobile)."'":"null");
553  $sql.= ", note_private = ".($this->note_private?"'".$this->db->escape($this->note_private)."'":"null");
554  $sql.= ", note_public = ".($this->note_public?"'".$this->db->escape($this->note_public)."'":"null");
555  $sql.= ", photo = ".($this->photo?"'".$this->db->escape($this->photo)."'":"null");
556  $sql.= ", public = '".$this->db->escape($this->public)."'";
557  $sql.= ", statut = ".$this->db->escape($this->statut);
558  $sql.= ", fk_adherent_type = ".$this->db->escape($this->typeid);
559  $sql.= ", morphy = '".$this->db->escape($this->morphy)."'";
560  $sql.= ", birth = ".($this->birth?"'".$this->db->idate($this->birth)."'":"null");
561  if ($this->datefin) $sql.= ", datefin = '".$this->db->idate($this->datefin)."'"; // Must be modified only when deleting a subscription
562  if ($this->datevalid) $sql.= ", datevalid = '".$this->db->idate($this->datevalid)."'"; // Must be modified only when validating a member
563  $sql.= ", fk_user_mod = ".($user->id>0?$user->id:'null'); // Can be null because member can be create by a guest
564  $sql.= " WHERE rowid = ".$this->id;
565 
566  dol_syslog(get_class($this)."::update update member", LOG_DEBUG);
567  $resql = $this->db->query($sql);
568  if ($resql)
569  {
570  unset($this->country_code);
571  unset($this->country);
572  unset($this->state_code);
573  unset($this->state);
574 
575  $nbrowsaffected+=$this->db->affected_rows($resql);
576 
577  $action='update';
578 
579  // Actions on extra fields
580  if (! $error && empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) // For avoid conflicts if trigger used
581  {
582  $result=$this->insertExtraFields();
583  if ($result < 0)
584  {
585  $error++;
586  }
587  }
588 
589  // Update password
590  if (! $error && $this->pass)
591  {
592  dol_syslog(get_class($this)."::update update password");
593  if ($this->pass != $this->pass_indatabase && $this->pass != $this->pass_indatabase_crypted)
594  {
595  $isencrypted = empty($conf->global->DATABASE_PWD_ENCRYPTED)?0:1;
596 
597  // If password to set differs from the one found into database
598  $result=$this->setPassword($user,$this->pass,$isencrypted,$notrigger,$nosyncuserpass);
599  if (! $nbrowsaffected) $nbrowsaffected++;
600  }
601  }
602 
603  // Remove links to user and replace with new one
604  if (! $error)
605  {
606  dol_syslog(get_class($this)."::update update link to user");
607  $sql = "UPDATE ".MAIN_DB_PREFIX."user SET fk_member = NULL WHERE fk_member = ".$this->id;
608  dol_syslog(get_class($this)."::update", LOG_DEBUG);
609  $resql = $this->db->query($sql);
610  if (! $resql) { $this->error=$this->db->error(); $this->db->rollback(); return -5; }
611  // If there is a user linked to this member
612  if ($this->user_id > 0)
613  {
614  $sql = "UPDATE ".MAIN_DB_PREFIX."user SET fk_member = ".$this->id." WHERE rowid = ".$this->user_id;
615  dol_syslog(get_class($this)."::update", LOG_DEBUG);
616  $resql = $this->db->query($sql);
617  if (! $resql) { $this->error=$this->db->error(); $this->db->rollback(); return -5; }
618  }
619  }
620 
621  if (! $error && $nbrowsaffected) // If something has change in main data
622  {
623  // Update information on linked user if it is an update
624  if (! $error && $this->user_id > 0 && ! $nosyncuser)
625  {
626  require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
627 
628  dol_syslog(get_class($this)."::update update linked user");
629 
630  $luser=new User($this->db);
631  $result=$luser->fetch($this->user_id);
632 
633  if ($result >= 0)
634  {
635  //var_dump($this->user_login);exit;
636  //var_dump($this->login);exit;
637 
638  // If option ADHERENT_LOGIN_NOT_REQUIRED is on, there is no login of member, so we do not overwrite user login to keep existing one.
639  if (empty($conf->global->ADHERENT_LOGIN_NOT_REQUIRED)) $luser->login=$this->login;
640 
641  $luser->civility_id=$this->civility_id;
642  $luser->firstname=$this->firstname;
643  $luser->lastname=$this->lastname;
644  $luser->pass=$this->pass;
645  $luser->societe_id=$this->societe;
646 
647  $luser->birth=$this->birth;
648  $luser->address=$this->address;
649  $luser->zip=$this->zip;
650  $luser->town=$this->town;
651  $luser->country_id=$this->country_id;
652  $luser->state_id=$this->state_id;
653 
654  $luser->email=$this->email;
655  $luser->skype=$this->skype;
656  $luser->twitter=$this->twitter;
657  $luser->facebook=$this->facebook;
658  $luser->office_phone=$this->phone;
659  $luser->user_mobile=$this->phone_mobile;
660 
661  $luser->fk_member=$this->id;
662 
663  $result=$luser->update($user,0,1,1); // Use nosync to 1 to avoid cyclic updates
664  if ($result < 0)
665  {
666  $this->error=$luser->error;
667  dol_syslog(get_class($this)."::update ".$this->error,LOG_ERR);
668  $error++;
669  }
670  }
671  else
672  {
673  $this->error=$luser->error;
674  $error++;
675  }
676  }
677 
678  // Update information on linked thirdparty if it is an update
679  if (! $error && $this->fk_soc > 0 && ! $nosyncthirdparty)
680  {
681  require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
682 
683  dol_syslog(get_class($this)."::update update linked thirdparty");
684 
685  // This member is linked with a thirdparty, so we also update thirdparty informations
686  // if this is an update.
687  $lthirdparty=new Societe($this->db);
688  $result=$lthirdparty->fetch($this->fk_soc);
689 
690  if ($result >= 0)
691  {
692  $lthirdparty->address=$this->address;
693  $lthirdparty->zip=$this->zip;
694  $lthirdparty->town=$this->town;
695  $lthirdparty->email=$this->email;
696  $lthirdparty->skype=$this->skype;
697  $lthirdparty->twitter=$this->twitter;
698  $lthirdparty->facebook=$this->facebook;
699  $lthirdparty->phone=$this->phone;
700  $lthirdparty->state_id=$this->state_id;
701  $lthirdparty->country_id=$this->country_id;
702  //$lthirdparty->phone_mobile=$this->phone_mobile;
703 
704  $result=$lthirdparty->update($this->fk_soc, $user, 0, 1, 1, 'update'); // Use sync to 0 to avoid cyclic updates
705 
706  if ($result < 0)
707  {
708  $this->error=$lthirdparty->error;
709  $this->errors=$lthirdparty->errors;
710  dol_syslog(get_class($this)."::update ".$this->error,LOG_ERR);
711  $error++;
712  }
713  }
714  else
715  {
716  $this->error=$lthirdparty->error;
717  $error++;
718  }
719  }
720  }
721 
722  if (! $error && ! $notrigger)
723  {
724  // Call trigger
725  $result=$this->call_trigger('MEMBER_MODIFY',$user);
726  if ($result < 0) { $error++; }
727  // End call triggers
728  }
729 
730  if (! $error)
731  {
732  $this->db->commit();
733  return $nbrowsaffected;
734  }
735  else
736  {
737  $this->db->rollback();
738  return -1;
739  }
740  }
741  else
742  {
743  $this->db->rollback();
744  $this->error=$this->db->lasterror();
745  return -2;
746  }
747  }
748 
749 
750  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
758  function update_end_date($user)
759  {
760  // phpcs:enable
761  $this->db->begin();
762 
763  // Search for last subscription id and end date
764  $sql = "SELECT rowid, datec as dateop, dateadh as datedeb, datef as datefin";
765  $sql.= " FROM ".MAIN_DB_PREFIX."subscription";
766  $sql.= " WHERE fk_adherent=".$this->id;
767  $sql.= " ORDER by dateadh DESC"; // Sort by start subscription date
768 
769  dol_syslog(get_class($this)."::update_end_date", LOG_DEBUG);
770  $resql=$this->db->query($sql);
771  if ($resql)
772  {
773  $obj=$this->db->fetch_object($resql);
774  $dateop=$this->db->jdate($obj->dateop);
775  $datedeb=$this->db->jdate($obj->datedeb);
776  $datefin=$this->db->jdate($obj->datefin);
777 
778  $sql = "UPDATE ".MAIN_DB_PREFIX."adherent SET";
779  $sql.= " datefin=".($datefin != '' ? "'".$this->db->idate($datefin)."'" : "null");
780  $sql.= " WHERE rowid = ".$this->id;
781 
782  dol_syslog(get_class($this)."::update_end_date", LOG_DEBUG);
783  $resql=$this->db->query($sql);
784  if ($resql)
785  {
786  $this->last_subscription_date=$dateop;
787  $this->last_subscription_date_start=$datedeb;
788  $this->last_subscription_date_end=$datefin;
789  $this->datefin=$datefin;
790  $this->db->commit();
791  return 1;
792  }
793  else
794  {
795  $this->db->rollback();
796  return -1;
797  }
798  }
799  else
800  {
801  $this->error=$this->db->lasterror();
802  $this->db->rollback();
803  return -1;
804  }
805  }
806 
815  function delete($rowid, $user, $notrigger=0)
816  {
817  global $conf, $langs;
818 
819  $result = 0;
820  $error=0;
821  $errorflag=0;
822 
823  // Check parameters
824  if (empty($rowid)) $rowid=$this->id;
825 
826  $this->db->begin();
827 
828  if (! $error && ! $notrigger)
829  {
830  // Call trigger
831  $result=$this->call_trigger('MEMBER_DELETE',$user);
832  if ($result < 0) $error++;
833  // End call triggers
834  }
835 
836  // Remove category
837  $sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_member WHERE fk_member = ".$rowid;
838  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
839  $resql=$this->db->query($sql);
840  if (! $resql)
841  {
842  $error++;
843  $this->error .= $this->db->lasterror();
844  $errorflag=-1;
845  }
846 
847  // Remove subscription
848  if (! $error)
849  {
850  $sql = "DELETE FROM ".MAIN_DB_PREFIX."subscription WHERE fk_adherent = ".$rowid;
851  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
852  $resql=$this->db->query($sql);
853  if (! $resql)
854  {
855  $error++;
856  $this->error .= $this->db->lasterror();
857  $errorflag=-2;
858  }
859  }
860 
861  // Remove linked user
862  if (! $error)
863  {
864  $ret=$this->setUserId(0);
865  if ($ret < 0)
866  {
867  $error++;
868  $this->error .= $this->db->lasterror();
869  $errorflag=-3;
870  }
871  }
872 
873  // Removed extrafields
874  if (! $error)
875  {
876  if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) // For avoid conflicts if trigger used
877  {
878  $result=$this->deleteExtraFields();
879  if ($result < 0)
880  {
881  $error++;
882  $errorflag=-4;
883  dol_syslog(get_class($this)."::delete erreur ".$errorflag." ".$this->error, LOG_ERR);
884  }
885  }
886  }
887 
888  // Remove adherent
889  if (! $error)
890  {
891  $sql = "DELETE FROM ".MAIN_DB_PREFIX."adherent WHERE rowid = ".$rowid;
892  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
893  $resql=$this->db->query($sql);
894  if (! $resql)
895  {
896  $error++;
897  $this->error .= $this->db->lasterror();
898  $errorflag=-5;
899  }
900  }
901 
902  if (! $error)
903  {
904  $this->db->commit();
905  return 1;
906  }
907  else
908  {
909  $this->db->rollback();
910  return $errorflag;
911  }
912  }
913 
914 
925  function setPassword($user, $password='', $isencrypted=0, $notrigger=0, $nosyncuser=0)
926  {
927  global $conf, $langs;
928 
929  $error=0;
930 
931  dol_syslog(get_class($this)."::setPassword user=".$user->id." password=".preg_replace('/./i','*',$password)." isencrypted=".$isencrypted);
932 
933  // If new password not provided, we generate one
934  if (! $password)
935  {
936  require_once DOL_DOCUMENT_ROOT.'/core/lib/security2.lib.php';
937  $password=getRandomPassword(false);
938  }
939 
940  // Crypt password
941  $password_crypted = dol_hash($password);
942 
943  $password_indatabase = '';
944  if (! $isencrypted)
945  {
946  $password_indatabase = $password;
947  }
948 
949  $this->db->begin();
950 
951  // Mise a jour
952  $sql = "UPDATE ".MAIN_DB_PREFIX."adherent";
953  $sql.= " SET pass_crypted = '".$this->db->escape($password_crypted)."'";
954  //if (! empty($conf->global->DATABASE_PWD_ENCRYPTED))
955  if ($isencrypted)
956  {
957  $sql.= ", pass = null";
958  }
959  else
960  {
961  $sql.= ", pass = '".$this->db->escape($password_indatabase)."'";
962  }
963  $sql.= " WHERE rowid = ".$this->id;
964 
965  //dol_syslog("Adherent::Password sql=hidden");
966  dol_syslog(get_class($this)."::setPassword", LOG_DEBUG);
967  $result = $this->db->query($sql);
968  if ($result)
969  {
970  $nbaffectedrows=$this->db->affected_rows($result);
971 
972  if ($nbaffectedrows)
973  {
974  $this->pass=$password;
975  $this->pass_indatabase=$password_indatabase;
976  $this->pass_indatabase_crypted=$password_crypted;
977 
978  if ($this->user_id && ! $nosyncuser)
979  {
980  require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
981 
982  // This member is linked with a user, so we also update users informations
983  // if this is an update.
984  $luser=new User($this->db);
985  $result=$luser->fetch($this->user_id);
986 
987  if ($result >= 0)
988  {
989  $result=$luser->setPassword($user,$this->pass,0,0,1);
990  if ($result < 0)
991  {
992  $this->error=$luser->error;
993  dol_syslog(get_class($this)."::setPassword ".$this->error,LOG_ERR);
994  $error++;
995  }
996  }
997  else
998  {
999  $this->error=$luser->error;
1000  $error++;
1001  }
1002  }
1003 
1004  if (! $error && ! $notrigger)
1005  {
1006  // Call trigger
1007  $result=$this->call_trigger('MEMBER_NEW_PASSWORD',$user);
1008  if ($result < 0) { $error++; $this->db->rollback(); return -1; }
1009  // End call triggers
1010  }
1011 
1012  $this->db->commit();
1013  return $this->pass;
1014  }
1015  else
1016  {
1017  $this->db->rollback();
1018  return 0;
1019  }
1020  }
1021  else
1022  {
1023  $this->db->rollback();
1024  dol_print_error($this->db);
1025  return -1;
1026  }
1027  }
1028 
1029 
1036  function setUserId($userid)
1037  {
1038  global $conf, $langs;
1039 
1040  $this->db->begin();
1041 
1042  // If user is linked to this member, remove old link to this member
1043  $sql = "UPDATE ".MAIN_DB_PREFIX."user SET fk_member = NULL WHERE fk_member = ".$this->id;
1044  dol_syslog(get_class($this)."::setUserId", LOG_DEBUG);
1045  $resql = $this->db->query($sql);
1046  if (! $resql) { $this->error=$this->db->error(); $this->db->rollback(); return -1; }
1047 
1048  // Set link to user
1049  if ($userid > 0)
1050  {
1051  $sql = "UPDATE ".MAIN_DB_PREFIX."user SET fk_member = ".$this->id;
1052  $sql.= " WHERE rowid = ".$userid;
1053  dol_syslog(get_class($this)."::setUserId", LOG_DEBUG);
1054  $resql = $this->db->query($sql);
1055  if (! $resql) { $this->error=$this->db->error(); $this->db->rollback(); return -2; }
1056  }
1057 
1058  $this->db->commit();
1059 
1060  return 1;
1061  }
1062 
1063 
1070  function setThirdPartyId($thirdpartyid)
1071  {
1072  global $conf, $langs;
1073 
1074  $this->db->begin();
1075 
1076  // Remove link to third party onto any other members
1077  if ($thirdpartyid > 0)
1078  {
1079  $sql = "UPDATE ".MAIN_DB_PREFIX."adherent SET fk_soc = null";
1080  $sql.= " WHERE fk_soc = '".$thirdpartyid."'";
1081  $sql.= " AND entity = ".$conf->entity;
1082  dol_syslog(get_class($this)."::setThirdPartyId", LOG_DEBUG);
1083  $resql = $this->db->query($sql);
1084  }
1085 
1086  // Add link to third party for current member
1087  $sql = "UPDATE ".MAIN_DB_PREFIX."adherent SET fk_soc = ".($thirdpartyid>0 ? $thirdpartyid : 'null');
1088  $sql.= " WHERE rowid = ".$this->id;
1089 
1090  dol_syslog(get_class($this)."::setThirdPartyId", LOG_DEBUG);
1091  $resql = $this->db->query($sql);
1092  if ($resql)
1093  {
1094  $this->db->commit();
1095  return 1;
1096  }
1097  else
1098  {
1099  $this->error=$this->db->error();
1100  $this->db->rollback();
1101  return -1;
1102  }
1103  }
1104 
1105 
1106  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
1113  function fetch_login($login)
1114  {
1115  // phpcs:enable
1116  global $conf;
1117 
1118  $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."adherent";
1119  $sql.= " WHERE login='".$this->db->escape($login)."'";
1120  $sql.= " AND entity = ".$conf->entity;
1121 
1122  $resql=$this->db->query($sql);
1123  if ($resql)
1124  {
1125  if ($this->db->num_rows($resql))
1126  {
1127  $obj = $this->db->fetch_object($resql);
1128  $this->fetch($obj->rowid);
1129  }
1130  }
1131  else
1132  {
1133  dol_print_error($this->db);
1134  }
1135  }
1136 
1137  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
1145  function fetch_name($firstname,$lastname)
1146  {
1147  // phpcs:enable
1148  global $conf;
1149 
1150  $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."adherent";
1151  $sql.= " WHERE firstname='".$this->db->escape($firstname)."'";
1152  $sql.= " AND lastname='".$this->db->escape($lastname)."'";
1153  $sql.= " AND entity = ".$conf->entity;
1154 
1155  $resql=$this->db->query($sql);
1156  if ($resql)
1157  {
1158  if ($this->db->num_rows($resql))
1159  {
1160  $obj = $this->db->fetch_object($resql);
1161  $this->fetch($obj->rowid);
1162  }
1163  }
1164  else
1165  {
1166  dol_print_error($this->db);
1167  }
1168  }
1169 
1181  function fetch($rowid,$ref='',$fk_soc='',$ref_ext='',$fetch_optionals=true,$fetch_subscriptions=true)
1182  {
1183  global $langs;
1184 
1185  $sql = "SELECT d.rowid, d.ref_ext, d.civility as civility_id, d.firstname, d.lastname, d.societe as company, d.fk_soc, d.statut, d.public, d.address, d.zip, d.town, d.note_private,";
1186  $sql.= " d.note_public,";
1187  $sql.= " d.email, d.skype, d.twitter, d.facebook, d.phone, d.phone_perso, d.phone_mobile, d.login, d.pass, d.pass_crypted,";
1188  $sql.= " d.photo, d.fk_adherent_type, d.morphy, d.entity,";
1189  $sql.= " d.datec as datec,";
1190  $sql.= " d.tms as datem,";
1191  $sql.= " d.datefin as datefin,";
1192  $sql.= " d.birth as birthday,";
1193  $sql.= " d.datevalid as datev,";
1194  $sql.= " d.country,";
1195  $sql.= " d.state_id,";
1196  $sql.= " d.model_pdf,";
1197  $sql.= " c.rowid as country_id, c.code as country_code, c.label as country,";
1198  $sql.= " dep.nom as state, dep.code_departement as state_code,";
1199  $sql.= " t.libelle as type, t.subscription as subscription,";
1200  $sql.= " u.rowid as user_id, u.login as user_login";
1201  $sql.= " FROM ".MAIN_DB_PREFIX."adherent_type as t, ".MAIN_DB_PREFIX."adherent as d";
1202  $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."c_country as c ON d.country = c.rowid";
1203  $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."c_departements as dep ON d.state_id = dep.rowid";
1204  $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."user as u ON d.rowid = u.fk_member";
1205  $sql.= " WHERE d.fk_adherent_type = t.rowid";
1206  if ($rowid) $sql.= " AND d.rowid=".$rowid;
1207  elseif ($ref || $fk_soc) {
1208  $sql.= " AND d.entity IN (".getEntity('adherent').")";
1209  if ($ref) $sql.= " AND d.rowid='".$this->db->escape($ref)."'";
1210  elseif ($fk_soc > 0) $sql.= " AND d.fk_soc=".$fk_soc;
1211  }
1212  elseif ($ref_ext)
1213  {
1214  $sql.= " AND d.ref_ext='".$this->db->escape($ref_ext)."'";
1215  }
1216 
1217  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
1218  $resql=$this->db->query($sql);
1219  if ($resql)
1220  {
1221  if ($this->db->num_rows($resql))
1222  {
1223  $obj = $this->db->fetch_object($resql);
1224 
1225  $this->entity = $obj->entity;
1226  $this->ref = $obj->rowid;
1227  $this->id = $obj->rowid;
1228  $this->ref_ext = $obj->ref_ext;
1229  $this->civility_id = $obj->civility_id;
1230  $this->firstname = $obj->firstname;
1231  $this->lastname = $obj->lastname;
1232  $this->login = $obj->login;
1233  $this->societe = $obj->company;
1234  $this->company = $obj->company;
1235  $this->fk_soc = $obj->fk_soc;
1236  $this->address = $obj->address;
1237  $this->zip = $obj->zip;
1238  $this->town = $obj->town;
1239 
1240  $this->pass = $obj->pass;
1241  $this->pass_indatabase = $obj->pass;
1242  $this->pass_indatabase_crypted = $obj->pass_crypted;
1243 
1244  $this->state_id = $obj->state_id;
1245  $this->state_code = $obj->state_id?$obj->state_code:'';
1246  $this->state = $obj->state_id?$obj->state:'';
1247 
1248  $this->country_id = $obj->country_id;
1249  $this->country_code = $obj->country_code;
1250  if ($langs->trans("Country".$obj->country_code) != "Country".$obj->country_code)
1251  $this->country = $langs->transnoentitiesnoconv("Country".$obj->country_code);
1252  else
1253  $this->country=$obj->country;
1254 
1255  $this->phone = $obj->phone;
1256  $this->phone_perso = $obj->phone_perso;
1257  $this->phone_mobile = $obj->phone_mobile;
1258  $this->email = $obj->email;
1259 
1260  $this->skype = $obj->skype;
1261  $this->twitter = $obj->twitter;
1262  $this->facebook = $obj->facebook;
1263 
1264  $this->photo = $obj->photo;
1265  $this->statut = $obj->statut;
1266  $this->public = $obj->public;
1267 
1268  $this->datec = $this->db->jdate($obj->datec);
1269  $this->datem = $this->db->jdate($obj->datem);
1270  $this->datefin = $this->db->jdate($obj->datefin);
1271  $this->datevalid = $this->db->jdate($obj->datev);
1272  $this->birth = $this->db->jdate($obj->birthday);
1273 
1274  $this->note_private = $obj->note_private;
1275  $this->note_public = $obj->note_public;
1276  $this->morphy = $obj->morphy;
1277 
1278  $this->typeid = $obj->fk_adherent_type;
1279  $this->type = $obj->type;
1280  $this->need_subscription = $obj->subscription;
1281 
1282  $this->user_id = $obj->user_id;
1283  $this->user_login = $obj->user_login;
1284 
1285  $this->model_pdf = $obj->model_pdf;
1286 
1287  // Retreive all extrafield
1288  // fetch optionals attributes and labels
1289  if ($fetch_optionals) {
1290  $this->fetch_optionals();
1291  }
1292 
1293  // Load other properties
1294  if ($fetch_subscriptions) {
1295  $result=$this->fetch_subscriptions();
1296  }
1297 
1298  return $this->id;
1299  }
1300  else
1301  {
1302  return 0;
1303  }
1304  }
1305  else
1306  {
1307  $this->error=$this->db->lasterror();
1308  return -1;
1309  }
1310  }
1311 
1312 
1313  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
1322  {
1323  // phpcs:enable
1324  global $langs;
1325 
1326  require_once DOL_DOCUMENT_ROOT.'/adherents/class/subscription.class.php';
1327 
1328  $sql = "SELECT c.rowid, c.fk_adherent, c.subscription, c.note, c.fk_bank,";
1329  $sql.= " c.tms as datem,";
1330  $sql.= " c.datec as datec,";
1331  $sql.= " c.dateadh as dateh,";
1332  $sql.= " c.datef as datef";
1333  $sql.= " FROM ".MAIN_DB_PREFIX."subscription as c";
1334  $sql.= " WHERE c.fk_adherent = ".$this->id;
1335  $sql.= " ORDER BY c.dateadh";
1336  dol_syslog(get_class($this)."::fetch_subscriptions", LOG_DEBUG);
1337 
1338  $resql=$this->db->query($sql);
1339  if ($resql)
1340  {
1341  $this->subscriptions=array();
1342 
1343  $i=0;
1344  while ($obj = $this->db->fetch_object($resql))
1345  {
1346  if ($i==0)
1347  {
1348  $this->first_subscription_date=$this->db->jdate($obj->datec);
1349  $this->first_subscription_date_start=$this->db->jdate($obj->dateh);
1350  $this->first_subscription_date_end=$this->db->jdate($obj->datef);
1351  $this->first_subscription_amount=$obj->subscription;
1352  }
1353  $this->last_subscription_date=$this->db->jdate($obj->datec);
1354  $this->last_subscription_date_start=$this->db->jdate($obj->datef);
1355  $this->last_subscription_date_end=$this->db->jdate($obj->datef);
1356  $this->last_subscription_amount=$obj->subscription;
1357 
1358  $subscription=new Subscription($this->db);
1359  $subscription->id=$obj->rowid;
1360  $subscription->fk_adherent=$obj->fk_adherent;
1361  $subscription->amount=$obj->subscription;
1362  $subscription->note=$obj->note;
1363  $subscription->fk_bank=$obj->fk_bank;
1364  $subscription->datem=$this->db->jdate($obj->datem);
1365  $subscription->datec=$this->db->jdate($obj->datec);
1366  $subscription->dateh=$this->db->jdate($obj->dateh);
1367  $subscription->datef=$this->db->jdate($obj->datef);
1368 
1369  $this->subscriptions[]=$subscription;
1370 
1371  $i++;
1372  }
1373  return 1;
1374  }
1375  else
1376  {
1377  $this->error=$this->db->error().' sql='.$sql;
1378  return -1;
1379  }
1380  }
1381 
1382 
1397  function subscription($date, $amount, $accountid=0, $operation='', $label='', $num_chq='', $emetteur_nom='', $emetteur_banque='', $datesubend=0)
1398  {
1399  global $conf,$langs,$user;
1400 
1401  require_once DOL_DOCUMENT_ROOT.'/adherents/class/subscription.class.php';
1402 
1403  $error=0;
1404 
1405  // Clean parameters
1406  if (! $amount) $amount=0;
1407 
1408  $this->db->begin();
1409 
1410  if ($datesubend)
1411  {
1412  $datefin=$datesubend;
1413  }
1414  else
1415  {
1416  // If no end date, end date = date + 1 year - 1 day
1417  $datefin = dol_time_plus_duree($date,1,'y');
1418  $datefin = dol_time_plus_duree($datefin,-1,'d');
1419  }
1420 
1421  // Create subscription
1422  $subscription=new Subscription($this->db);
1423  $subscription->fk_adherent=$this->id;
1424  $subscription->dateh=$date; // Date of new subscription
1425  $subscription->datef=$datefin; // End data of new subscription
1426  $subscription->amount=$amount;
1427  $subscription->note=$label; // deprecated
1428  $subscription->note_public=$label;
1429 
1430  $rowid=$subscription->create($user);
1431  if ($rowid > 0)
1432  {
1433  // Update denormalized subscription end date (read database subscription to find values)
1434  // This will also update this->datefin
1435  $result=$this->update_end_date($user);
1436  if ($result > 0)
1437  {
1438  // Change properties of object (used by triggers)
1439  $this->last_subscription_date=dol_now();
1440  $this->last_subscription_date_start=$date;
1441  $this->last_subscription_date_end=$datefin;
1442  $this->last_subscription_amount=$amount;
1443  }
1444 
1445  if (! $error)
1446  {
1447  $this->db->commit();
1448  return $rowid;
1449  }
1450  else
1451  {
1452  $this->db->rollback();
1453  return -2;
1454  }
1455  }
1456  else
1457  {
1458  $this->error=$subscription->error;
1459  $this->errors=$subscription->errors;
1460  $this->db->rollback();
1461  return -1;
1462  }
1463  }
1464 
1465 
1483  function subscriptionComplementaryActions($subscriptionid, $option, $accountid, $datesubscription, $paymentdate, $operation, $label, $amount, $num_chq, $emetteur_nom='', $emetteur_banque='', $autocreatethirdparty=0)
1484  {
1485  global $conf, $langs, $user, $mysoc;
1486 
1487  $error = 0;
1488 
1489  $this->invoice = null; // This will contains invoice if an invoice is created
1490 
1491  dol_syslog("subscriptionComplementaryActions subscriptionid=".$subscriptionid." option=".$option." accountid=".$accountid." datesubscription=".$datesubscription." paymentdate=".$paymentdate." label=".$label." amount=".$amount." num_chq=".$num_chq." autocreatethirdparty=".$autocreatethirdparty);
1492 
1493  // Insert into bank account directlty (if option choosed for) + link to llx_subscription if option is 'bankdirect'
1494  if ($option == 'bankdirect' && $accountid)
1495  {
1496  require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
1497 
1498  $acct=new Account($this->db);
1499  $result=$acct->fetch($accountid);
1500 
1501  $dateop=$paymentdate;
1502 
1503  $insertid=$acct->addline($dateop, $operation, $label, $amount, $num_chq, '', $user, $emetteur_nom, $emetteur_banque);
1504  if ($insertid > 0)
1505  {
1506  $inserturlid=$acct->add_url_line($insertid, $this->id, DOL_URL_ROOT.'/adherents/card.php?rowid=', $this->getFullname($langs), 'member');
1507  if ($inserturlid > 0)
1508  {
1509  // Update table subscription
1510  $sql ="UPDATE ".MAIN_DB_PREFIX."subscription SET fk_bank=".$insertid;
1511  $sql.=" WHERE rowid=".$subscriptionid;
1512 
1513  dol_syslog("subscription::subscription", LOG_DEBUG);
1514  $resql = $this->db->query($sql);
1515  if (! $resql)
1516  {
1517  $error++;
1518  $this->error=$this->db->lasterror();
1519  $this->errors[]=$this->error;
1520  }
1521  }
1522  else
1523  {
1524  $error++;
1525  $this->error=$acct->error;
1526  $this->errors=$acct->errors;
1527  }
1528  }
1529  else
1530  {
1531  $error++;
1532  $this->error=$acct->error;
1533  $this->errors=$acct->errors;
1534  }
1535  }
1536 
1537  // If option choosed, we create invoice
1538  if (($option == 'bankviainvoice' && $accountid) || $option == 'invoiceonly')
1539  {
1540  require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
1541  require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/paymentterm.class.php';
1542 
1543  $invoice=new Facture($this->db);
1544  $customer=new Societe($this->db);
1545 
1546  if (! $error)
1547  {
1548  if (! ($this->fk_soc > 0)) // If not yet linked to a company
1549  {
1550  if ($autocreatethirdparty)
1551  {
1552  // Create a linked thirdparty to member
1553  $companyalias='';
1554  $fullname = $this->getFullName($langs);
1555 
1556  if ($this->morphy == 'mor')
1557  {
1558  $companyname=$this->societe;
1559  if (! empty($fullname)) $companyalias=$fullname;
1560  }
1561  else
1562  {
1563  $companyname=$fullname;
1564  if (! empty($this->societe)) $companyalias=$this->societe;
1565  }
1566 
1567  $result=$customer->create_from_member($this, $companyname, $companyalias);
1568  if ($result < 0)
1569  {
1570  $this->error = $customer->error;
1571  $this->errors = $customer->errors;
1572  $error++;
1573  }
1574  else
1575  {
1576  $this->fk_soc = $result;
1577  }
1578  }
1579  else
1580  {
1581  $langs->load("errors");
1582  $this->error=$langs->trans("ErrorMemberNotLinkedToAThirpartyLinkOrCreateFirst");
1583  $this->errors[]=$this->error;
1584  $error++;
1585  }
1586  }
1587  }
1588  if (! $error)
1589  {
1590  $result=$customer->fetch($this->fk_soc);
1591  if ($result <= 0)
1592  {
1593  $this->error=$customer->error;
1594  $this->errors=$customer->errors;
1595  $error++;
1596  }
1597  }
1598 
1599  if (! $error)
1600  {
1601  // Create draft invoice
1602  $invoice->type=Facture::TYPE_STANDARD;
1603  $invoice->cond_reglement_id=$customer->cond_reglement_id;
1604  if (empty($invoice->cond_reglement_id))
1605  {
1606  $paymenttermstatic=new PaymentTerm($this->db);
1607  $invoice->cond_reglement_id=$paymenttermstatic->getDefaultId();
1608  if (empty($invoice->cond_reglement_id))
1609  {
1610  $error++;
1611  $this->error='ErrorNoPaymentTermRECEPFound';
1612  $this->errors[]=$this->error;
1613  }
1614  }
1615  $invoice->socid=$this->fk_soc;
1616  $invoice->date=$datesubscription;
1617 
1618  // Possibility to add external linked objects with hooks
1619  $invoice->linked_objects['subscription'] = $subscriptionid;
1620  if (! empty($_POST['other_linked_objects']) && is_array($_POST['other_linked_objects']))
1621  {
1622  $invoice->linked_objects = array_merge($invoice->linked_objects, $_POST['other_linked_objects']);
1623  }
1624 
1625  $result=$invoice->create($user);
1626  if ($result <= 0)
1627  {
1628  $this->error=$invoice->error;
1629  $this->errors=$invoice->errors;
1630  $error++;
1631  }
1632  else
1633  {
1634  $this->invoice = $invoice;
1635  }
1636  }
1637 
1638  if (! $error)
1639  {
1640  // Add line to draft invoice
1641  $idprodsubscription=0;
1642  if (! empty($conf->global->ADHERENT_PRODUCT_ID_FOR_SUBSCRIPTIONS) && (! empty($conf->product->enabled) || ! empty($conf->service->enabled))) $idprodsubscription = $conf->global->ADHERENT_PRODUCT_ID_FOR_SUBSCRIPTIONS;
1643 
1644  $vattouse=0;
1645  if (isset($conf->global->ADHERENT_VAT_FOR_SUBSCRIPTIONS) && $conf->global->ADHERENT_VAT_FOR_SUBSCRIPTIONS == 'defaultforfoundationcountry')
1646  {
1647  $vattouse=get_default_tva($mysoc, $mysoc, $idprodsubscription);
1648  }
1649  //print xx".$vattouse." - ".$mysoc." - ".$customer;exit;
1650  $result=$invoice->addline($label,0,1,$vattouse,0,0,$idprodsubscription,0,$datesubscription,'',0,0,'','TTC',$amount,1);
1651  if ($result <= 0)
1652  {
1653  $this->error=$invoice->error;
1654  $this->errors=$invoice->errors;
1655  $error++;
1656  }
1657  }
1658 
1659  if (! $error)
1660  {
1661  // Validate invoice
1662  $result=$invoice->validate($user);
1663  if ($result <= 0)
1664  {
1665  $this->error=$invoice->error;
1666  $this->errors=$invoice->errors;
1667  $error++;
1668  }
1669  }
1670 
1671  if (! $error)
1672  {
1673  // TODO Link invoice with subscription ?
1674  }
1675 
1676  // Add payment onto invoice
1677  if (! $error && $option == 'bankviainvoice' && $accountid)
1678  {
1679  require_once DOL_DOCUMENT_ROOT.'/compta/paiement/class/paiement.class.php';
1680  require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
1681  require_once DOL_DOCUMENT_ROOT.'/core/lib/functions.lib.php';
1682 
1683  $amounts = array();
1684  $amounts[$invoice->id] = price2num($amount);
1685 
1686  $paiement = new Paiement($this->db);
1687  $paiement->datepaye = $paymentdate;
1688  $paiement->amounts = $amounts;
1689  $paiement->paiementid = dol_getIdFromCode($this->db,$operation,'c_paiement','code','id',1);
1690  $paiement->num_paiement = $num_chq;
1691  $paiement->note = $label;
1692  $paiement->note_public = $label;
1693 
1694  if (! $error)
1695  {
1696  // Create payment line for invoice
1697  $paiement_id = $paiement->create($user);
1698  if (! $paiement_id > 0)
1699  {
1700  $this->error=$paiement->error;
1701  $this->errors=$paiement->errors;
1702  $error++;
1703  }
1704  }
1705 
1706  if (! $error)
1707  {
1708  // Add transaction into bank account
1709  $bank_line_id=$paiement->addPaymentToBank($user,'payment','(SubscriptionPayment)',$accountid,$emetteur_nom,$emetteur_banque);
1710  if (! ($bank_line_id > 0))
1711  {
1712  $this->error=$paiement->error;
1713  $this->errors=$paiement->errors;
1714  $error++;
1715  }
1716  }
1717 
1718  if (! $error && !empty($bank_line_id))
1719  {
1720  // Update fk_bank into subscription table
1721  $sql = 'UPDATE '.MAIN_DB_PREFIX.'subscription SET fk_bank='.$bank_line_id;
1722  $sql.= ' WHERE rowid='.$subscriptionid;
1723 
1724  $result = $this->db->query($sql);
1725  if (! $result)
1726  {
1727  $error++;
1728  }
1729  }
1730 
1731  if (! $error)
1732  {
1733  // Set invoice as paid
1734  $invoice->set_paid($user);
1735  }
1736  }
1737 
1738  if (! $error)
1739  {
1740  // Define output language
1741  $outputlangs = $langs;
1742  $newlang = '';
1743  $lang_id=GETPOST('lang_id');
1744  if ($conf->global->MAIN_MULTILANGS && empty($newlang) && ! empty($lang_id))
1745  $newlang = $lang_id;
1746  if ($conf->global->MAIN_MULTILANGS && empty($newlang))
1747  $newlang = $customer->default_lang;
1748  if (! empty($newlang)) {
1749  $outputlangs = new Translate("", $conf);
1750  $outputlangs->setDefaultLang($newlang);
1751  }
1752  // Generate PDF (whatever is option MAIN_DISABLE_PDF_AUTOUPDATE) so we can include it into email
1753  //if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE))
1754 
1755  $invoice->generateDocument($invoice->modelpdf, $outputlangs);
1756  }
1757  }
1758 
1759  if ($error)
1760  {
1761  return -1;
1762  }
1763  else
1764  {
1765  return 1;
1766  }
1767  }
1768 
1769 
1776  function validate($user)
1777  {
1778  global $langs,$conf;
1779 
1780  $error=0;
1781  $now=dol_now();
1782 
1783  // Check parameters
1784  if ($this->statut == 1)
1785  {
1786  dol_syslog(get_class($this)."::validate statut of member does not allow this", LOG_WARNING);
1787  return 0;
1788  }
1789 
1790  $this->db->begin();
1791 
1792  $sql = "UPDATE ".MAIN_DB_PREFIX."adherent SET";
1793  $sql.= " statut = 1";
1794  $sql.= ", datevalid = '".$this->db->idate($now)."'";
1795  $sql.= ", fk_user_valid=".$user->id;
1796  $sql.= " WHERE rowid = ".$this->id;
1797 
1798  dol_syslog(get_class($this)."::validate", LOG_DEBUG);
1799  $result = $this->db->query($sql);
1800  if ($result)
1801  {
1802  $this->statut=1;
1803 
1804  // Call trigger
1805  $result=$this->call_trigger('MEMBER_VALIDATE',$user);
1806  if ($result < 0) { $error++; $this->db->rollback(); return -1; }
1807  // End call triggers
1808 
1809  $this->datevalid = $now;
1810 
1811  $this->db->commit();
1812  return 1;
1813  }
1814  else
1815  {
1816  $this->error=$this->db->error();
1817  $this->db->rollback();
1818  return -1;
1819  }
1820  }
1821 
1822 
1829  function resiliate($user)
1830  {
1831  global $langs,$conf;
1832 
1833  $error=0;
1834 
1835  // Check paramaters
1836  if ($this->statut == 0)
1837  {
1838  dol_syslog(get_class($this)."::resiliate statut of member does not allow this", LOG_WARNING);
1839  return 0;
1840  }
1841 
1842  $this->db->begin();
1843 
1844  $sql = "UPDATE ".MAIN_DB_PREFIX."adherent SET";
1845  $sql.= " statut = 0";
1846  $sql.= ", fk_user_valid=".$user->id;
1847  $sql.= " WHERE rowid = ".$this->id;
1848 
1849  $result = $this->db->query($sql);
1850  if ($result)
1851  {
1852  $this->statut=0;
1853 
1854  // Call trigger
1855  $result=$this->call_trigger('MEMBER_RESILIATE',$user);
1856  if ($result < 0) { $error++; $this->db->rollback(); return -1; }
1857  // End call triggers
1858 
1859  $this->db->commit();
1860  return 1;
1861  }
1862  else
1863  {
1864  $this->error=$this->db->error();
1865  $this->db->rollback();
1866  return -1;
1867  }
1868  }
1869 
1870 
1871  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
1877  function add_to_abo()
1878  {
1879  // phpcs:enable
1880  global $conf,$langs;
1881 
1882  include_once DOL_DOCUMENT_ROOT.'/mailmanspip/class/mailmanspip.class.php';
1883  $mailmanspip=new MailmanSpip($this->db);
1884 
1885  $err=0;
1886 
1887  // mailman
1888  if (! empty($conf->global->ADHERENT_USE_MAILMAN) && ! empty($conf->mailmanspip->enabled))
1889  {
1890  $result=$mailmanspip->add_to_mailman($this);
1891 
1892  if ($result < 0)
1893  {
1894  if (! empty($mailmanspip->error)) $this->errors[]=$mailmanspip->error;
1895  $err+=1;
1896  }
1897  foreach ($mailmanspip->mladded_ko as $tmplist => $tmpemail)
1898  {
1899  $langs->load("errors");
1900  $this->errors[]=$langs->trans("ErrorFailedToAddToMailmanList",$tmpemail,$tmplist);
1901  }
1902  foreach ($mailmanspip->mladded_ok as $tmplist => $tmpemail)
1903  {
1904  $langs->load("mailmanspip");
1905  $this->mesgs[]=$langs->trans("SuccessToAddToMailmanList",$tmpemail,$tmplist);
1906  }
1907  }
1908 
1909  // spip
1910  if (! empty($conf->global->ADHERENT_USE_SPIP) && ! empty($conf->mailmanspip->enabled))
1911  {
1912  $result=$mailmanspip->add_to_spip($this);
1913  if ($result < 0)
1914  {
1915  $this->errors[]=$mailmanspip->error;
1916  $err+=1;
1917  }
1918  }
1919  if ($err)
1920  {
1921  return -$err;
1922  }
1923  else
1924  {
1925  return 1;
1926  }
1927  }
1928 
1929 
1930  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
1936  function del_to_abo()
1937  {
1938  // phpcs:enable
1939  global $conf,$langs;
1940 
1941  include_once DOL_DOCUMENT_ROOT.'/mailmanspip/class/mailmanspip.class.php';
1942  $mailmanspip=new MailmanSpip($this->db);
1943 
1944  $err=0;
1945 
1946  // mailman
1947  if (! empty($conf->global->ADHERENT_USE_MAILMAN))
1948  {
1949  $result=$mailmanspip->del_to_mailman($this);
1950  if ($result < 0)
1951  {
1952  if (! empty($mailmanspip->error)) $this->errors[]=$mailmanspip->error;
1953  $err+=1;
1954  }
1955 
1956  foreach ($mailmanspip->mlremoved_ko as $tmplist => $tmpemail)
1957  {
1958  $langs->load("errors");
1959  $this->errors[]=$langs->trans("ErrorFailedToRemoveToMailmanList",$tmpemail,$tmplist);
1960  }
1961  foreach ($mailmanspip->mlremoved_ok as $tmplist => $tmpemail)
1962  {
1963  $langs->load("mailmanspip");
1964  $this->mesgs[]=$langs->trans("SuccessToRemoveToMailmanList",$tmpemail,$tmplist);
1965  }
1966  }
1967 
1968  if ($conf->global->ADHERENT_USE_SPIP && ! empty($conf->mailmanspip->enabled))
1969  {
1970  $result=$mailmanspip->del_to_spip($this);
1971  if ($result < 0)
1972  {
1973  $this->errors[]=$mailmanspip->error;
1974  $err+=1;
1975  }
1976  }
1977  if ($err)
1978  {
1979  // error
1980  return -$err;
1981  }
1982  else
1983  {
1984  return 1;
1985  }
1986  }
1987 
1988 
1994  function getCivilityLabel()
1995  {
1996  global $langs;
1997  $langs->load("dict");
1998 
1999  $code=(empty($this->civility_id)?'':$this->civility_id);
2000  if (empty($code)) return '';
2001  return $langs->getLabelFromKey($this->db, "Civility".$code, "c_civility", "code", "label", $code);
2002  }
2003 
2015  function getNomUrl($withpictoimg=0, $maxlen=0, $option='card', $mode='', $morecss='', $save_lastsearch_value=-1)
2016  {
2017  global $conf, $langs;
2018 
2019  if (! empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER) && $withpictoimg) $withpictoimg=0;
2020 
2021  $notooltip=0;
2022 
2023  $result=''; $label='';
2024  $link=''; $linkstart=''; $linkend='';
2025 
2026  if (! empty($this->photo))
2027  {
2028  $label.= '<div class="photointooltip">';
2029  $label.= Form::showphoto('memberphoto', $this, 80, 0, 0, 'photowithmargin photologintooltip', 'small', 0, 1);
2030  $label.= '</div><div style="clear: both;"></div>';
2031  }
2032 
2033  $label.= '<div class="centpercent">';
2034  $label.= '<u>' . $langs->trans("Member") . '</u>';
2035  if (! empty($this->ref))
2036  $label.= '<br><b>' . $langs->trans('Ref') . ':</b> ' . $this->ref;
2037  if (! empty($this->firstname) || ! empty($this->lastname))
2038  $label.= '<br><b>' . $langs->trans('Name') . ':</b> ' . $this->getFullName($langs);
2039  if (! empty($this->societe))
2040  $label.= '<br><b>' . $langs->trans('Company') . ':</b> ' . $this->societe;
2041  $label.='</div>';
2042 
2043  $url = DOL_URL_ROOT.'/adherents/card.php?rowid='.$this->id;
2044  if ($option == 'subscription')
2045  {
2046  $url = DOL_URL_ROOT.'/adherents/subscription.php?rowid='.$this->id;
2047  }
2048 
2049  if ($option != 'nolink')
2050  {
2051  // Add param to save lastsearch_values or not
2052  $add_save_lastsearch_values=($save_lastsearch_value == 1 ? 1 : 0);
2053  if ($save_lastsearch_value == -1 && preg_match('/list\.php/',$_SERVER["PHP_SELF"])) $add_save_lastsearch_values=1;
2054  if ($add_save_lastsearch_values) $url.='&save_lastsearch_values=1';
2055  }
2056 
2057  $link = '<a href="'.$url.'"';
2058  $linkclose="";
2059  if (empty($notooltip))
2060  {
2061  if (! empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER))
2062  {
2063  $langs->load("users");
2064  $label=$langs->trans("ShowUser");
2065  $linkclose.=' alt="'.dol_escape_htmltag($label, 1).'"';
2066  }
2067  $linkclose.= ' title="'.dol_escape_htmltag($label, 1).'"';
2068  $linkclose.= ' class="classfortooltip'.($morecss?' '.$morecss:'').'"';
2069  }
2070 
2071  $link.=$linkclose.'>';
2072  $linkend='</a>';
2073 
2074  //if ($withpictoimg == -1) $result.='<div class="nowrap">';
2075  $result.=$link;
2076  if ($withpictoimg)
2077  {
2078  $paddafterimage='';
2079  if (abs($withpictoimg) == 1) $paddafterimage='style="margin-right: 3px;"';
2080  // Only picto
2081  if ($withpictoimg > 0) $picto='<div class="inline-block nopadding valignmiddle'.($morecss?' userimg'.$morecss:'').'">'.img_object('', 'user', $paddafterimage.' '.($notooltip?'':'class="classfortooltip"'), 0, 0, $notooltip?0:1).'</div>';
2082  // Picto must be a photo
2083  else $picto='<div class="inline-block nopadding valignmiddle'.($morecss?' userimg'.$morecss:'').'"'.($paddafterimage?' '.$paddafterimage:'').'>'.Form::showphoto('memberphoto', $this, 0, 0, 0, 'userphoto'.($withpictoimg==-3?'small':''), 'mini', 0, 1).'</div>';
2084  $result.=$picto;
2085  }
2086  if ($withpictoimg > -2 && $withpictoimg != 2)
2087  {
2088  if (empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) $result.='<div class="inline-block nopadding valignmiddle'.((! isset($this->statut) || $this->statut)?'':' strikefordisabled').($morecss?' usertext'.$morecss:'').'">';
2089  if ($mode == 'login') $result.=dol_trunc($this->login, $maxlen);
2090  elseif ($mode == 'ref') $result.=$this->id;
2091  else $result.=$this->getFullName($langs,'',($mode == 'firstname' ? 2 : -1),$maxlen);
2092  if (empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) $result.='</div>';
2093  }
2094  $result.=$linkend;
2095  //if ($withpictoimg == -1) $result.='</div>';
2096 
2097  return $result;
2098  }
2099 
2106  function getLibStatut($mode=0)
2107  {
2108  return $this->LibStatut($this->statut,$this->need_subscription,$this->datefin,$mode);
2109  }
2110 
2111  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
2121  function LibStatut($statut,$need_subscription,$date_end_subscription,$mode=0)
2122  {
2123  // phpcs:enable
2124  global $langs;
2125  $langs->load("members");
2126  if ($mode == 0)
2127  {
2128  if ($statut == -1) return $langs->trans("MemberStatusDraft");
2129  elseif ($statut >= 1) {
2130  if (! $date_end_subscription) return $langs->trans("MemberStatusActive");
2131  elseif ($date_end_subscription < time()) return $langs->trans("MemberStatusActiveLate");
2132  else return $langs->trans("MemberStatusPaid");
2133  }
2134  elseif ($statut == 0) return $langs->trans("MemberStatusResiliated");
2135  }
2136  elseif ($mode == 1)
2137  {
2138  if ($statut == -1) return $langs->trans("MemberStatusDraftShort");
2139  elseif ($statut >= 1) {
2140  if (! $date_end_subscription) return $langs->trans("MemberStatusActiveShort");
2141  elseif ($date_end_subscription < time()) return $langs->trans("MemberStatusActiveLateShort");
2142  else return $langs->trans("MemberStatusPaidShort");
2143  }
2144  elseif ($statut == 0) return $langs->trans("MemberStatusResiliatedShort");
2145  }
2146  elseif ($mode == 2)
2147  {
2148  if ($statut == -1) return img_picto($langs->trans('MemberStatusDraft'),'statut0').' '.$langs->trans("MemberStatusDraftShort");
2149  elseif ($statut >= 1) {
2150  if (! $date_end_subscription) return img_picto($langs->trans('MemberStatusActive'),'statut1').' '.$langs->trans("MemberStatusActiveShort");
2151  elseif ($date_end_subscription < time()) return img_picto($langs->trans('MemberStatusActiveLate'),'statut3').' '.$langs->trans("MemberStatusActiveLateShort");
2152  else return img_picto($langs->trans('MemberStatusPaid'),'statut4').' '.$langs->trans("MemberStatusPaidShort");
2153  }
2154  elseif ($statut == 0) return img_picto($langs->trans('MemberStatusResiliated'),'statut5').' '.$langs->trans("MemberStatusResiliatedShort");
2155  }
2156  elseif ($mode == 3)
2157  {
2158  if ($statut == -1) return img_picto($langs->trans('MemberStatusDraft'),'statut0');
2159  elseif ($statut >= 1) {
2160  if (! $date_end_subscription) return img_picto($langs->trans('MemberStatusActive'),'statut1');
2161  elseif ($date_end_subscription < time()) return img_picto($langs->trans('MemberStatusActiveLate'),'statut3');
2162  else return img_picto($langs->trans('MemberStatusPaid'),'statut4');
2163  }
2164  elseif ($statut == 0) return img_picto($langs->trans('MemberStatusResiliated'),'statut5');
2165  }
2166  elseif ($mode == 4)
2167  {
2168  if ($statut == -1) return img_picto($langs->trans('MemberStatusDraft'),'statut0').' '.$langs->trans("MemberStatusDraft");
2169  elseif ($statut >= 1) {
2170  if (! $date_end_subscription) return img_picto($langs->trans('MemberStatusActive'),'statut1').' '.$langs->trans("MemberStatusActive");
2171  elseif ($date_end_subscription < time()) return img_picto($langs->trans('MemberStatusActiveLate'),'statut3').' '.$langs->trans("MemberStatusActiveLate");
2172  else return img_picto($langs->trans('MemberStatusPaid'),'statut4').' '.$langs->trans("MemberStatusPaid");
2173  }
2174  if ($statut == 0) return img_picto($langs->trans('MemberStatusResiliated'),'statut5').' '.$langs->trans("MemberStatusResiliated");
2175  }
2176  elseif ($mode == 5)
2177  {
2178  if ($statut == -1) return $langs->trans("MemberStatusDraft").' '.img_picto($langs->trans('MemberStatusDraft'),'statut0');
2179  elseif ($statut >= 1) {
2180  if (! $date_end_subscription) return '<span class="hideonsmartphone">'.$langs->trans("MemberStatusActiveShort").' </span>'.img_picto($langs->trans('MemberStatusActive'),'statut1');
2181  elseif ($date_end_subscription < time()) return '<span class="hideonsmartphone">'.$langs->trans("MemberStatusActiveLateShort").' </span>'.img_picto($langs->trans('MemberStatusActiveLate'),'statut3');
2182  else return '<span class="hideonsmartphone">'.$langs->trans("MemberStatusPaidShort").' </span>'.img_picto($langs->trans('MemberStatusPaid'),'statut4');
2183  }
2184  if ($statut == 0) return '<span class="hideonsmartphone">'.$langs->trans("MemberStatusResiliated").' </span>'.img_picto($langs->trans('MemberStatusResiliated'),'statut5');
2185  }
2186  elseif ($mode == 6)
2187  {
2188  if ($statut == -1) return $langs->trans("MemberStatusDraft").' '.img_picto($langs->trans('MemberStatusDraft'),'statut0');
2189  if ($statut >= 1) {
2190  if (! $date_end_subscription) return '<span class="hideonsmartphone">'.$langs->trans("MemberStatusActive").' </span>'.img_picto($langs->trans('MemberStatusActive'),'statut1');
2191  elseif ($date_end_subscription < time()) return '<span class="hideonsmartphone">'.$langs->trans("MemberStatusActiveLate").' </span>'.img_picto($langs->trans('MemberStatusActiveLate'),'statut3');
2192  else return '<span class="hideonsmartphone">'.$langs->trans("MemberStatusPaid").' </span>'.img_picto($langs->trans('MemberStatusPaid'),'statut4');
2193  }
2194  if ($statut == 0) return '<span class="hideonsmartphone">'.$langs->trans("MemberStatusResiliated").' </span>'.img_picto($langs->trans('MemberStatusResiliated'),'statut5');
2195  }
2196  }
2197 
2198 
2199  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
2205  function load_state_board()
2206  {
2207  // phpcs:enable
2208  global $conf;
2209 
2210  $this->nb = array();
2211 
2212  $sql = "SELECT count(a.rowid) as nb";
2213  $sql.= " FROM ".MAIN_DB_PREFIX."adherent as a";
2214  $sql.= " WHERE a.statut > 0";
2215  $sql.= " AND a.entity IN (".getEntity('adherent').")";
2216 
2217  $resql=$this->db->query($sql);
2218  if ($resql)
2219  {
2220  while ($obj=$this->db->fetch_object($resql))
2221  {
2222  $this->nb["members"]=$obj->nb;
2223  }
2224  $this->db->free($resql);
2225  return 1;
2226  }
2227  else
2228  {
2229  dol_print_error($this->db);
2230  $this->error=$this->db->error();
2231  return -1;
2232  }
2233  }
2234 
2235  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
2242  function load_board($user)
2243  {
2244  // phpcs:enable
2245  global $conf, $langs;
2246 
2247  if ($user->societe_id) return -1; // protection pour eviter appel par utilisateur externe
2248 
2249  $now=dol_now();
2250 
2251  $sql = "SELECT a.rowid, a.datefin, a.statut";
2252  $sql.= " FROM ".MAIN_DB_PREFIX."adherent as a";
2253  $sql.= " WHERE a.statut = 1";
2254  $sql.= " AND a.entity IN (".getEntity('adherent').")";
2255  $sql.= " AND (a.datefin IS NULL or a.datefin < '".$this->db->idate($now)."')";
2256 
2257  $resql=$this->db->query($sql);
2258  if ($resql)
2259  {
2260  $langs->load("members");
2261 
2262  $response = new WorkboardResponse();
2263  $response->warning_delay=$conf->adherent->subscription->warning_delay/60/60/24;
2264  $response->label=$langs->trans("MembersWithSubscriptionToReceive");
2265  $response->url=DOL_URL_ROOT.'/adherents/list.php?mainmenu=members&amp;statut=1&amp;filter=outofdate';
2266  $response->img=img_object('',"user");
2267 
2268  $adherentstatic = new Adherent($this->db);
2269 
2270  while ($obj=$this->db->fetch_object($resql))
2271  {
2272  $response->nbtodo++;
2273 
2274  $adherentstatic->datefin = $this->db->jdate($obj->datefin);
2275  $adherentstatic->statut = $obj->statut;
2276 
2277  if ($adherentstatic->hasDelay()) {
2278  $response->nbtodolate++;
2279  }
2280  }
2281 
2282  return $response;
2283  }
2284  else
2285  {
2286  dol_print_error($this->db);
2287  $this->error=$this->db->error();
2288  return -1;
2289  }
2290  }
2291 
2292 
2304  public function generateDocument($modele, $outputlangs, $hidedetails=0, $hidedesc=0, $hideref=0, $moreparams=null)
2305  {
2306  global $conf,$langs;
2307 
2308  $langs->load("orders");
2309 
2310  if (! dol_strlen($modele)) {
2311 
2312  $modele = 'standard';
2313 
2314  if ($this->modelpdf) {
2315  $modele = $this->modelpdf;
2316  } elseif (! empty($conf->global->ADHERENT_ADDON_PDF)) {
2317  $modele = $conf->global->ADHERENT_ADDON_PDF;
2318  }
2319  }
2320 
2321  $modelpath = "core/modules/member/doc/";
2322 
2323  return $this->commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
2324  }
2325 
2326 
2334  function initAsSpecimen()
2335  {
2336  global $user,$langs;
2337 
2338  // Initialise parametres
2339  $this->id=0;
2340  $this->specimen=1;
2341  $this->civility_id = 0;
2342  $this->lastname = 'DOLIBARR';
2343  $this->firstname = 'SPECIMEN';
2344  $this->login='dolibspec';
2345  $this->pass='dolibspec';
2346  $this->societe = 'Societe ABC';
2347  $this->address = '61 jump street';
2348  $this->zip = '75000';
2349  $this->town = 'Paris';
2350  $this->country_id = 1;
2351  $this->country_code = 'FR';
2352  $this->country = 'France';
2353  $this->morphy = 1;
2354  $this->email = 'specimen@specimen.com';
2355  $this->skype = 'skypepseudo';
2356  $this->twitter = 'twitterpseudo';
2357  $this->facebook = 'facebookpseudo';
2358  $this->phone = '0999999999';
2359  $this->phone_perso = '0999999998';
2360  $this->phone_mobile = '0999999997';
2361  $this->note_private='No comment';
2362  $this->birth=time();
2363  $this->photo='';
2364  $this->public=1;
2365  $this->statut=0;
2366 
2367  $this->datefin=time();
2368  $this->datevalid=time();
2369 
2370  $this->typeid=1; // Id type adherent
2371  $this->type='Type adherent'; // Libelle type adherent
2372  $this->need_subscription=0;
2373 
2374  $this->first_subscription_date=time();
2375  $this->first_subscription_date_start=$this->first_subscription_date;
2376  $this->first_subscription_date_end=dol_time_plus_duree($this->first_subscription_date_start, 1, 'y');
2377  $this->first_subscription_amount=10;
2378 
2379  $this->last_subscription_date=$this->first_subscription_date;
2380  $this->last_subscription_date_start=$this->first_subscription_date;
2381  $this->last_subscription_date_end=dol_time_plus_duree($this->last_subscription_date_start, 1, 'y');
2382  $this->last_subscription_amount=10;
2383  }
2384 
2385 
2386  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
2396  function _load_ldap_dn($info,$mode=0)
2397  {
2398  // phpcs:enable
2399  global $conf;
2400  $dn='';
2401  if ($mode==0) $dn=$conf->global->LDAP_KEY_MEMBERS."=".$info[$conf->global->LDAP_KEY_MEMBERS].",".$conf->global->LDAP_MEMBER_DN;
2402  if ($mode==1) $dn=$conf->global->LDAP_MEMBER_DN;
2403  if ($mode==2) $dn=$conf->global->LDAP_KEY_MEMBERS."=".$info[$conf->global->LDAP_KEY_MEMBERS];
2404  return $dn;
2405  }
2406 
2407 
2408  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
2414  function _load_ldap_info()
2415  {
2416  // phpcs:enable
2417  global $conf,$langs;
2418 
2419  $info=array();
2420  $keymodified=false;
2421 
2422  // Object classes
2423  $info["objectclass"]=explode(',',$conf->global->LDAP_MEMBER_OBJECT_CLASS);
2424 
2425  $this->fullname=$this->getFullName($langs);
2426 
2427  // For avoid ldap error when firstname and lastname are empty
2428  if ($this->morphy == 'mor' && (empty($this->fullname) || $this->fullname == $this->societe)) {
2429  $this->fullname = $this->societe;
2430  $this->lastname = $this->societe;
2431  }
2432 
2433  // Possible LDAP KEY (constname => varname)
2434  $ldapkey = array(
2435  'LDAP_MEMBER_FIELD_FULLNAME' => 'fullname',
2436  'LDAP_MEMBER_FIELD_NAME' => 'lastname',
2437  'LDAP_MEMBER_FIELD_LOGIN' => 'login',
2438  'LDAP_MEMBER_FIELD_LOGIN_SAMBA' => 'login',
2439  'LDAP_MEMBER_FIELD_MAIL' => 'email'
2440  );
2441 
2442  // Member
2443  foreach ($ldapkey as $constname => $varname)
2444  {
2445  if (! empty($this->$varname) && ! empty($conf->global->$constname))
2446  {
2447  $info[$conf->global->$constname] = $this->$varname;
2448 
2449  // Check if it is the LDAP key and if its value has been changed
2450  if (! empty($conf->global->LDAP_KEY_MEMBERS) && $conf->global->LDAP_KEY_MEMBERS == $conf->global->$constname)
2451  {
2452  if (! empty($this->oldcopy) && $this->$varname != $this->oldcopy->$varname) $keymodified=true; // For check if LDAP key has been modified
2453  }
2454  }
2455  }
2456  if ($this->firstname && ! empty($conf->global->LDAP_MEMBER_FIELD_FIRSTNAME)) $info[$conf->global->LDAP_MEMBER_FIELD_FIRSTNAME] = $this->firstname;
2457  if ($this->poste && ! empty($conf->global->LDAP_MEMBER_FIELD_TITLE)) $info[$conf->global->LDAP_MEMBER_FIELD_TITLE] = $this->poste;
2458  if ($this->societe && ! empty($conf->global->LDAP_MEMBER_FIELD_COMPANY)) $info[$conf->global->LDAP_MEMBER_FIELD_COMPANY] = $this->societe;
2459  if ($this->address && ! empty($conf->global->LDAP_MEMBER_FIELD_ADDRESS)) $info[$conf->global->LDAP_MEMBER_FIELD_ADDRESS] = $this->address;
2460  if ($this->zip && ! empty($conf->global->LDAP_MEMBER_FIELD_ZIP)) $info[$conf->global->LDAP_MEMBER_FIELD_ZIP] = $this->zip;
2461  if ($this->town && ! empty($conf->global->LDAP_MEMBER_FIELD_TOWN)) $info[$conf->global->LDAP_MEMBER_FIELD_TOWN] = $this->town;
2462  if ($this->country_code && ! empty($conf->global->LDAP_MEMBER_FIELD_COUNTRY)) $info[$conf->global->LDAP_MEMBER_FIELD_COUNTRY] = $this->country_code;
2463  if ($this->skype && ! empty($conf->global->LDAP_MEMBER_FIELD_SKYPE)) $info[$conf->global->LDAP_MEMBER_FIELD_SKYPE] = $this->skype;
2464  if ($this->twitter && ! empty($conf->global->LDAP_MEMBER_FIELD_TWITTER)) $info[$conf->global->LDAP_MEMBER_FIELD_TWITTER] = $this->twitter;
2465  if ($this->facebook && ! empty($conf->global->LDAP_MEMBER_FIELD_FACEBOOK)) $info[$conf->global->LDAP_MEMBER_FIELD_FACEBOOK] = $this->facebook;
2466  if ($this->phone && ! empty($conf->global->LDAP_MEMBER_FIELD_PHONE)) $info[$conf->global->LDAP_MEMBER_FIELD_PHONE] = $this->phone;
2467  if ($this->phone_perso && ! empty($conf->global->LDAP_MEMBER_FIELD_PHONE_PERSO)) $info[$conf->global->LDAP_MEMBER_FIELD_PHONE_PERSO] = $this->phone_perso;
2468  if ($this->phone_mobile && ! empty($conf->global->LDAP_MEMBER_FIELD_MOBILE)) $info[$conf->global->LDAP_MEMBER_FIELD_MOBILE] = $this->phone_mobile;
2469  if ($this->fax && ! empty($conf->global->LDAP_MEMBER_FIELD_FAX)) $info[$conf->global->LDAP_MEMBER_FIELD_FAX] = $this->fax;
2470  if ($this->note_private && ! empty($conf->global->LDAP_MEMBER_FIELD_DESCRIPTION)) $info[$conf->global->LDAP_MEMBER_FIELD_DESCRIPTION] = dol_string_nohtmltag($this->note_private, 2);
2471  if ($this->note_public && ! empty($conf->global->LDAP_MEMBER_FIELD_NOTE_PUBLIC)) $info[$conf->global->LDAP_MEMBER_FIELD_NOTE_PUBLIC] = dol_string_nohtmltag($this->note_public, 2);
2472  if ($this->birth && ! empty($conf->global->LDAP_MEMBER_FIELD_BIRTHDATE)) $info[$conf->global->LDAP_MEMBER_FIELD_BIRTHDATE] = dol_print_date($this->birth,'dayhourldap');
2473  if (isset($this->statut) && ! empty($conf->global->LDAP_FIELD_MEMBER_STATUS)) $info[$conf->global->LDAP_FIELD_MEMBER_STATUS] = $this->statut;
2474  if ($this->datefin && ! empty($conf->global->LDAP_FIELD_MEMBER_END_LASTSUBSCRIPTION)) $info[$conf->global->LDAP_FIELD_MEMBER_END_LASTSUBSCRIPTION] = dol_print_date($this->datefin,'dayhourldap');
2475 
2476  // When password is modified
2477  if (! empty($this->pass))
2478  {
2479  if (! empty($conf->global->LDAP_MEMBER_FIELD_PASSWORD)) $info[$conf->global->LDAP_MEMBER_FIELD_PASSWORD] = $this->pass; // this->pass = mot de passe non crypte
2480  if (! empty($conf->global->LDAP_MEMBER_FIELD_PASSWORD_CRYPTED)) $info[$conf->global->LDAP_MEMBER_FIELD_PASSWORD_CRYPTED] = dol_hash($this->pass, 4); // Create OpenLDAP MD5 password (TODO add type of encryption)
2481  }
2482  // Set LDAP password if possible
2483  else if ($conf->global->LDAP_SERVER_PROTOCOLVERSION !== '3') // If ldap key is modified and LDAPv3 we use ldap_rename function for avoid lose encrypt password
2484  {
2485  if (! empty($conf->global->DATABASE_PWD_ENCRYPTED))
2486  {
2487  // Just for the default MD5 !
2488  if (empty($conf->global->MAIN_SECURITY_HASH_ALGO))
2489  {
2490  if ($this->pass_indatabase_crypted && ! empty($conf->global->LDAP_MEMBER_FIELD_PASSWORD_CRYPTED)) {
2491  // Create OpenLDAP MD5 password from Dolibarr MD5 password
2492  // Note: This suppose that "pass_indatabase_crypted" is a md5 (guaranted by the previous test if "(empty($conf->global->MAIN_SECURITY_HASH_ALGO))"
2493  $info[$conf->global->LDAP_MEMBER_FIELD_PASSWORD_CRYPTED] = '{md5}'.base64_encode(hex2bin($this->pass_indatabase_crypted));
2494  }
2495  }
2496  }
2497  // Use $this->pass_indatabase value if exists
2498  else if (! empty($this->pass_indatabase))
2499  {
2500  if (! empty($conf->global->LDAP_MEMBER_FIELD_PASSWORD)) $info[$conf->global->LDAP_MEMBER_FIELD_PASSWORD] = $this->pass_indatabase; // $this->pass_indatabase = mot de passe non crypte
2501  if (! empty($conf->global->LDAP_MEMBER_FIELD_PASSWORD_CRYPTED)) $info[$conf->global->LDAP_MEMBER_FIELD_PASSWORD_CRYPTED] = dol_hash($this->pass_indatabase, 4); // md5 for OpenLdap TODO add type of encryption
2502  }
2503  }
2504 
2505  // Subscriptions
2506  if ($this->first_subscription_date && ! empty($conf->global->LDAP_FIELD_MEMBER_FIRSTSUBSCRIPTION_DATE)) $info[$conf->global->LDAP_FIELD_MEMBER_FIRSTSUBSCRIPTION_DATE] = dol_print_date($this->first_subscription_date,'dayhourldap');
2507  if (isset($this->first_subscription_amount) && ! empty($conf->global->LDAP_FIELD_MEMBER_FIRSTSUBSCRIPTION_AMOUNT)) $info[$conf->global->LDAP_FIELD_MEMBER_FIRSTSUBSCRIPTION_AMOUNT] = $this->first_subscription_amount;
2508  if ($this->last_subscription_date && ! empty($conf->global->LDAP_FIELD_MEMBER_LASTSUBSCRIPTION_DATE)) $info[$conf->global->LDAP_FIELD_MEMBER_LASTSUBSCRIPTION_DATE] = dol_print_date($this->last_subscription_date,'dayhourldap');
2509  if (isset($this->last_subscription_amount) && ! empty($conf->global->LDAP_FIELD_MEMBER_LASTSUBSCRIPTION_AMOUNT)) $info[$conf->global->LDAP_FIELD_MEMBER_LASTSUBSCRIPTION_AMOUNT] = $this->last_subscription_amount;
2510 
2511  return $info;
2512  }
2513 
2514 
2521  function info($id)
2522  {
2523  $sql = 'SELECT a.rowid, a.datec as datec,';
2524  $sql.= ' a.datevalid as datev,';
2525  $sql.= ' a.tms as datem,';
2526  $sql.= ' a.fk_user_author, a.fk_user_valid, a.fk_user_mod';
2527  $sql.= ' FROM '.MAIN_DB_PREFIX.'adherent as a';
2528  $sql.= ' WHERE a.rowid = '.$id;
2529 
2530  dol_syslog(get_class($this)."::info", LOG_DEBUG);
2531  $result=$this->db->query($sql);
2532  if ($result)
2533  {
2534  if ($this->db->num_rows($result))
2535  {
2536  $obj = $this->db->fetch_object($result);
2537  $this->id = $obj->rowid;
2538  if ($obj->fk_user_author)
2539  {
2540  $cuser = new User($this->db);
2541  $cuser->fetch($obj->fk_user_author);
2542  $this->user_creation = $cuser;
2543  }
2544 
2545  if ($obj->fk_user_valid)
2546  {
2547  $vuser = new User($this->db);
2548  $vuser->fetch($obj->fk_user_valid);
2549  $this->user_validation = $vuser;
2550  }
2551 
2552  if ($obj->fk_user_mod)
2553  {
2554  $muser = new User($this->db);
2555  $muser->fetch($obj->fk_user_mod);
2556  $this->user_modification = $muser;
2557  }
2558 
2559  $this->date_creation = $this->db->jdate($obj->datec);
2560  $this->date_validation = $this->db->jdate($obj->datev);
2561  $this->date_modification = $this->db->jdate($obj->datem);
2562  }
2563 
2564  $this->db->free($result);
2565  }
2566  else
2567  {
2568  dol_print_error($this->db);
2569  }
2570  }
2571 
2577  function getNbOfEMailings()
2578  {
2579  $sql = "SELECT count(mc.email) as nb";
2580  $sql.= " FROM ".MAIN_DB_PREFIX."mailing_cibles as mc";
2581  $sql.= " WHERE mc.email = '".$this->db->escape($this->email)."'";
2582  $sql.= " AND mc.statut NOT IN (-1,0)"; // -1 erreur, 0 non envoye, 1 envoye avec succes
2583 
2584  $resql=$this->db->query($sql);
2585  if ($resql)
2586  {
2587  $obj = $this->db->fetch_object($resql);
2588  $nb=$obj->nb;
2589 
2590  $this->db->free($resql);
2591  return $nb;
2592  }
2593  else
2594  {
2595  $this->error=$this->db->error();
2596  return -1;
2597  }
2598  }
2599 
2610  public function setCategories($categories)
2611  {
2612  // Handle single category
2613  if (!is_array($categories)) {
2614  $categories = array($categories);
2615  }
2616 
2617  // Get current categories
2618  require_once DOL_DOCUMENT_ROOT . '/categories/class/categorie.class.php';
2619  $c = new Categorie($this->db);
2620  $existing = $c->containing($this->id, Categorie::TYPE_MEMBER, 'id');
2621 
2622  // Diff
2623  if (is_array($existing)) {
2624  $to_del = array_diff($existing, $categories);
2625  $to_add = array_diff($categories, $existing);
2626  } else {
2627  $to_del = array(); // Nothing to delete
2628  $to_add = $categories;
2629  }
2630 
2631  // Process
2632  foreach ($to_del as $del) {
2633  if ($c->fetch($del) > 0) {
2634  $c->del_type($this, 'member');
2635  }
2636  }
2637  foreach ($to_add as $add) {
2638  if ($c->fetch($add) > 0) {
2639  $c->add_type($this, 'member');
2640  }
2641  }
2642 
2643  return;
2644  }
2645 
2654  public static function replaceThirdparty($db, $origin_id, $dest_id)
2655  {
2656  $tables = array(
2657  'adherent'
2658  );
2659 
2660  return CommonObject::commonReplaceThirdparty($db, $origin_id, $dest_id, $tables);
2661  }
2662 
2668  public function hasDelay()
2669  {
2670  global $conf;
2671 
2672  //Only valid members
2673  if ($this->statut <= 0) return false;
2674  if (! $this->datefin) return false;
2675 
2676  $now = dol_now();
2677 
2678  return $this->datefin < ($now - $conf->adherent->subscription->warning_delay);
2679  }
2680 
2681 
2682 
2690  public function sendReminderForExpiredSubscription($daysbeforeendlist='10')
2691  {
2692  global $conf, $langs, $mysoc, $user;
2693 
2694  $error = 0;
2695  $this->output = '';
2696  $this->error='';
2697 
2698  $blockingerrormsg = '';
2699 
2700  if (empty($conf->adherent->enabled)) // Should not happen. If module disabled, cron job should not be visible.
2701  {
2702  $langs->load("agenda");
2703  $this->output = $langs->trans('ModuleNotEnabled', $langs->transnoentitiesnoconv("Adherent"));
2704  return 0;
2705  }
2706  if (empty($conf->global->MEMBER_REMINDER_EMAIL))
2707  {
2708  $langs->load("agenda");
2709  $this->output = $langs->trans('EventRemindersByEmailNotEnabled', $langs->transnoentitiesnoconv("Adherent"));
2710  return 0;
2711  }
2712 
2713  $now = dol_now();
2714  $nbok = 0;
2715  $nbko = 0;
2716 
2717  $arraydaysbeforeend=explode(';',$daysbeforeendlist);
2718  foreach($arraydaysbeforeend as $daysbeforeend) // Loop on each delay
2719  {
2720  dol_syslog(__METHOD__.' - Process delta = '.$daysbeforeend, LOG_DEBUG);
2721 
2722  if (! is_numeric($daysbeforeend))
2723  {
2724  $blockingerrormsg="Value for delta is not a positive or negative numeric";
2725  $nbko++;
2726  break;
2727  }
2728 
2729  $tmp=dol_getdate($now);
2730  $datetosearchfor = dol_time_plus_duree(dol_mktime(0, 0, 0, $tmp['mon'], $tmp['mday'], $tmp['year']), $daysbeforeend, 'd');
2731 
2732  $sql = 'SELECT rowid FROM '.MAIN_DB_PREFIX.'adherent';
2733  $sql.= " WHERE datefin = '".$this->db->idate($datetosearchfor)."'";
2734 
2735  $resql = $this->db->query($sql);
2736  if ($resql)
2737  {
2738  $num_rows = $this->db->num_rows($resql);
2739 
2740  include_once DOL_DOCUMENT_ROOT.'/core/class/html.formmail.class.php';
2741  $adherent = new Adherent($this->db);
2742  $formmail = new FormMail($this->db);
2743 
2744  $i=0;
2745  while ($i < $num_rows)
2746  {
2747  $obj = $this->db->fetch_object($resql);
2748 
2749  $adherent->fetch($obj->rowid, '', '', '', true, true);
2750 
2751  if (empty($adherent->email))
2752  {
2753  $nbko++;
2754  }
2755  else
2756  {
2757  $adherent->fetch_thirdparty();
2758 
2759  // Send reminder email
2760  $outputlangs = new Translate('', $conf);
2761  $outputlangs->setDefaultLang(empty($adherent->thirdparty->default_lang) ? $mysoc->default_lang : $adherent->thirdparty->default_lang);
2762  $outputlangs->loadLangs(array("main", "members"));
2763  dol_syslog("sendReminderForExpiredSubscription Language set to ".$outputlangs->defaultlang);
2764 
2765  $arraydefaultmessage=null;
2766  $labeltouse = $conf->global->ADHERENT_EMAIL_TEMPLATE_REMIND_EXPIRATION;
2767 
2768  if (! empty($labeltouse)) $arraydefaultmessage=$formmail->getEMailTemplate($this->db, 'member', $user, $outputlangs, 0, 1, $labeltouse);
2769 
2770  if (! empty($labeltouse) && is_object($arraydefaultmessage) && $arraydefaultmessage->id > 0)
2771  {
2772  $substitutionarray=getCommonSubstitutionArray($outputlangs, 0, null, $adherent);
2773  //if (is_array($adherent->thirdparty)) $substitutionarraycomp = ...
2774  complete_substitutions_array($substitutionarray, $outputlangs, $adherent);
2775 
2776  $subject = make_substitutions($arraydefaultmessage->topic, $substitutionarray, $outputlangs);
2777  $msg = make_substitutions($arraydefaultmessage->content, $substitutionarray, $outputlangs);
2778  $from = $conf->global->ADHERENT_MAIL_FROM;
2779  $to = $adherent->email;
2780 
2781  $trackid = 'mem'.$adherent->id;
2782  $moreinheader='X-Dolibarr-Info: sendReminderForExpiredSubscription'."\r\n";
2783 
2784  include_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
2785  $cmail = new CMailFile($subject, $to, $from, $msg, array(), array(), array(), '', '', 0, 1, '', '', $trackid, $moreinheader);
2786  $result = $cmail->sendfile();
2787  if (! $result)
2788  {
2789  $error++;
2790  $this->error = $cmail->error;
2791  $this->errors += $cmail->errors;
2792  $nbko++;
2793  }
2794  else
2795  {
2796  $nbok++;
2797 
2798  $message = $msg;
2799  $sendto = $to;
2800  $sendtocc = '';
2801  $sendtobcc = '';
2802  $actioncode='EMAIL';
2803  $extraparams='';
2804 
2805  $actionmsg='';
2806  $actionmsg2=$langs->transnoentities('MailSentBy').' '.CMailFile::getValidAddress($from,4,0,1).' '.$langs->transnoentities('To').' '.CMailFile::getValidAddress($sendto,4,0,1);
2807  if ($message)
2808  {
2809  $actionmsg=$langs->transnoentities('MailFrom').': '.dol_escape_htmltag($from);
2810  $actionmsg=dol_concatdesc($actionmsg, $langs->transnoentities('MailTo').': '.dol_escape_htmltag($sendto));
2811  if ($sendtocc) $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('Bcc') . ": " . dol_escape_htmltag($sendtocc));
2812  $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('MailTopic') . ": " . $subject);
2813  $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('TextUsedInTheMessageBody') . ":");
2814  $actionmsg = dol_concatdesc($actionmsg, $message);
2815  }
2816 
2817  require_once DOL_DOCUMENT_ROOT.'/comm/action/class/actioncomm.class.php';
2818 
2819  // Insert record of emails sent
2820  $actioncomm = new ActionComm($this->db);
2821 
2822  $actioncomm->type_code = 'AC_OTH_AUTO'; // Type of event ('AC_OTH', 'AC_OTH_AUTO', 'AC_XXX'...)
2823  $actioncomm->code = 'AC_'.$actioncode;
2824  $actioncomm->label = $actionmsg2;
2825  $actioncomm->note = $actionmsg;
2826  $actioncomm->fk_project = 0;
2827  $actioncomm->datep = $now;
2828  $actioncomm->datef = $now;
2829  $actioncomm->percentage = -1; // Not applicable
2830  $actioncomm->socid = $adherent->thirdparty->id;
2831  $actioncomm->contactid = 0;
2832  $actioncomm->authorid = $user->id; // User saving action
2833  $actioncomm->userownerid = $user->id; // Owner of action
2834  // Fields when action is en email (content should be added into note)
2835  $actioncomm->email_msgid = $cmail->msgid;
2836  $actioncomm->email_from = $from;
2837  $actioncomm->email_sender= '';
2838  $actioncomm->email_to = $to;
2839  $actioncomm->email_tocc = $sendtocc;
2840  $actioncomm->email_tobcc = $sendtobcc;
2841  $actioncomm->email_subject = $subject;
2842  $actioncomm->errors_to = '';
2843 
2844  $actioncomm->fk_element = $adherent->id;
2845  $actioncomm->elementtype = $adherent->element;
2846 
2847  $actioncomm->extraparams = $extraparams;
2848 
2849  $actioncomm->create($user);
2850  }
2851  }
2852  else
2853  {
2854  $blockingerrormsg="Can't find email template, defined into member module setup, to use for reminding";
2855  $nbko++;
2856  break;
2857  }
2858  }
2859 
2860  $i++;
2861  }
2862  }
2863  else
2864  {
2865  $this->error = $this->db->lasterror();
2866  return 1;
2867  }
2868  }
2869 
2870  if ($blockingerrormsg)
2871  {
2872  $this->error = $blockingerrormsg;
2873  return 1;
2874  }
2875  else
2876  {
2877  $this->output = 'Found '.($nbok + $nbko).' members to send reminder to.';
2878  $this->output.= ' Send email successfuly to '.$nbok.' members';
2879  if ($nbko) $this->output.= ' - Canceled for '.$nbko.' member (no email or email sending error)';
2880  }
2881 
2882  return 0;
2883  }
2884 }
resiliate($user)
Fonction qui resilie un adherent.
del_to_abo()
Function to delete a member from external tools like mailing-list, spip, etc.
fetch($rowid, $ref='', $fk_soc='', $ref_ext='', $fetch_optionals=true, $fetch_subscriptions=true)
Load member from database.
GETPOST($paramname, $check='none', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
setCategories($categories)
Sets object to supplied categories.
Classe permettant la generation du formulaire html d&#39;envoi de mail unitaire Usage: $formail = new For...
const TYPE_STANDARD
Standard invoice.
yn($yesno, $case=1, $color=0)
Return yes or no in current language.
dol_trunc($string, $size=40, $trunc='right', $stringencoding='UTF-8', $nodot=0, $display=0)
Truncate a string to a particular length adding &#39;...&#39; if string larger than length.
if(! empty($conf->facture->enabled) && $user->rights->facture->lire) if(! empty($conf->fournisseur->enabled) && $user->rights->fournisseur->facture->lire) if(! empty($conf->don->enabled) && $user->rights->societe->lire) if(! empty($conf->tax->enabled) && $user->rights->tax->charges->lire) if(! empty($conf->facture->enabled) &&! empty($conf->commande->enabled) && $user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(! empty($conf->facture->enabled) && $user->rights->facture->lire) if(! empty($conf->fournisseur->enabled) && $user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1053
getCivilityLabel()
Return civility label of a member.
Class to manage agenda events (actions)
subscriptionComplementaryActions($subscriptionid, $option, $accountid, $datesubscription, $paymentdate, $operation, $label, $amount, $num_chq, $emetteur_nom='', $emetteur_banque='', $autocreatethirdparty=0)
Do complementary actions after subscription recording.
dol_mktime($hour, $minute, $second, $month, $day, $year, $gm=false, $check=1)
Return a timestamp date built from detailed informations (by default a local PHP server timestamp) Re...
validate($user)
Function that validate a member.
getNomUrl($withpictoimg=0, $maxlen=0, $option='card', $mode='', $morecss='', $save_lastsearch_value=-1)
Return clicable name (with picto eventually)
generateDocument($modele, $outputlangs, $hidedetails=0, $hidedesc=0, $hideref=0, $moreparams=null)
Create a document onto disk according to template module.
Class to manage Dolibarr users.
Definition: user.class.php:41
add_to_abo()
Function to add member into external tools mailing-list, spip, etc.
dol_getIdFromCode($db, $key, $tablename, $fieldkey='code', $fieldid='id', $entityfilter=0)
Return an id or code from a code or id.
setUserId($userid)
Set link to a user.
send_an_email($text, $subject, $filename_list=array(), $mimetype_list=array(), $mimefilename_list=array(), $addr_cc="", $addr_bcc="", $deliveryreceipt=0, $msgishtml=-1, $errors_to='', $moreinheader='')
Function sending an email to the current member with the text supplied in parameter.
commonGenerateDocument($modelspath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams=null)
Common function for all objects extending CommonObject for generating documents.
static showphoto($modulepart, $object, $width=100, $height=0, $caneditfield=0, $cssclass='photowithmargin', $imagesize='', $addlinktofullsize=1, $cache=0, $forcecapture='')
Return HTML code to output a photo.
setThirdPartyId($thirdpartyid)
Set link to a third party.
_load_ldap_info()
Initialise tableau info (tableau des attributs LDAP)
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
dol_concatdesc($text1, $text2, $forxml=false)
Concat 2 descriptions with a new line between them (second operand after first one with appropriate n...
getFullName($langs, $option=0, $nameorder=-1, $maxlen=0)
Return full name (civility+&#39; &#39;+name+&#39; &#39;+lastname)
create($user, $notrigger=0)
Create a member into database.
fetch_name($firstname, $lastname)
Method to load member from its name.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0)
Returns text escaped for inclusion in HTML alt or title tags, or into values of HTML input fields...
Class to manage bank accounts.
_load_ldap_dn($info, $mode=0)
Retourne chaine DN complete dans l&#39;annuaire LDAP pour l&#39;objet.
$pass_indatabase_crypted
Encrypted password in database (always defined)
sendReminderForExpiredSubscription($daysbeforeendlist='10')
Send reminders by emails before subscription end CAN BE A CRON TASK.
LibStatut($statut, $need_subscription, $date_end_subscription, $mode=0)
Renvoi le libelle d&#39;un statut donne.
getCommonSubstitutionArray($outputlangs, $onlykey=0, $exclude=null, $object=null)
Return array of possible common substitutions.
getNbOfEMailings()
Return number of mass Emailing received by this member with its email.
info($id)
Charge les informations d&#39;ordre info dans l&#39;objet adherent.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
type
Definition: viewcat.php:284
Class to manage third parties objects (customers, suppliers, prospects...)
fetch_subscriptions()
Function to get member subscriptions data first_subscription_date, first_subscription_date_start, first_subscription_date_end, first_subscription_amount last_subscription_date, last_subscription_date_start, last_subscription_date_end, last_subscription_amount.
insertExtraFields($trigger='', $userused=null)
Add/Update all extra fields values for the current object.
Class to manage categories.
static getValidAddress($address, $format, $encode=0, $maxnumberofemail=0)
Return a formatted address string for SMTP protocol.
static replaceThirdparty($db, $origin_id, $dest_id)
Function used to replace a thirdparty id with another one.
Class to send emails (with attachments or not) Usage: $mailfile = new CMailFile($subject,$sendto,$replyto,$message,$filepath,$mimetype,$filename,$cc,$ccc,$deliveryreceipt,$msgishtml,$errors_to,$css,$trackid,$moreinheader,$sendcontext); $mailfile->sendfile();.
static commonReplaceThirdparty(DoliDB $db, $origin_id, $dest_id, array $tables, $ignoreerrors=0)
Function used to replace a thirdparty id with another one.
Class to manage payments of customer invoices.
setPassword($user, $password='', $isencrypted=0, $notrigger=0, $nosyncuser=0)
Change password of a user.
Class to manage members of a foundation.
fetch_login($login)
Method to load member from its login.
$pass_indatabase
Clear password in database (defined if DATABASE_PWD_ENCRYPTED=0)
hasDelay()
Return if a member is late (subscription late) or not.
load_board($user)
Load indicators for dashboard (this->nbtodo and this->nbtodolate)
deleteExtraFields()
Delete all extra fields values for the current object.
Class to manage translations.
__construct($db)
Constructor.
dol_now($mode='gmt')
Return date for now.
fetch_optionals($rowid=null, $optionsArray=null)
Function to get extra fields of an object into $this->array_options This method is in most cases call...
dol_string_nohtmltag($stringtoclean, $removelinefeed=1, $pagecodeto='UTF-8', $strip_tags=0)
Clean a string from all HTML tags and entities.
Class to manage subscriptions of foundation members.
getRandomPassword($generic=false)
Return a generated password using default module.
dol_print_date($time, $format='', $tzoutput='tzserver', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
load_state_board()
Charge indicateurs this->nb de tableau de bord.
$pass
Clear password in memory.
getmorphylib($morphy='')
Return translated label by the nature of a adherent (physical or moral)
makeSubstitution($text)
Make substitution of tags into text with value of current object.
get_default_tva(Societe $thirdparty_seller, Societe $thirdparty_buyer, $idprod=0, $idprodfournprice=0)
Function that return vat rate of a product line (according to seller, buyer and product vat rate) Si ...
dol_htmlentitiesbr($stringtoencode, $nl2brmode=0, $pagecodefrom='UTF-8', $removelasteolbr=1)
This function is called to encode a string into a HTML string but differs from htmlentities because a...
make_substitutions($text, $substitutionarray, $outputlangs=null)
Make substitution into a text string, replacing keys with vals from $substitutionarray (oldval=>newva...
dol_hash($chain, $type='0')
Returns a hash of a string.
Class to manage invoices.
dol_getdate($timestamp, $fast=false)
Return an array with locale date info.
call_trigger($trigger_name, $user)
Call trigger based on this instance.
update_end_date($user)
Update denormalized last subscription date.
Class to manage mailman and spip.
dol_time_plus_duree($time, $duration_value, $duration_unit)
Add a delay to a date.
Definition: date.lib.php:116
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='')
Show picto whatever it&#39;s its name (generic function)
initAsSpecimen()
Initialise an instance with random values.
update($user, $notrigger=0, $nosyncuser=0, $nosyncuserpass=0, $nosyncthirdparty=0, $action='update')
Update a member in database (standard information and password)
price2num($amount, $rounding='', $alreadysqlnb=0)
Function that return a number with universal decimal format (decimal separator is &#39;...
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
Parent class of all other business classes (invoices, contracts, proposals, orders, ...)
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
Class to manage payment terms records in dictionary.
subscription($date, $amount, $accountid=0, $operation='', $label='', $num_chq='', $emetteur_nom='', $emetteur_banque='', $datesubend=0)
Insert subscription into database and eventually add links to banks, mailman, etc...
complete_substitutions_array(&$substitutionarray, $outputlangs, $object=null, $parameters=null, $callfunc="completesubstitutionarray")
Complete the $substitutionarray with more entries coming from external module that had set the "subst...
getLibStatut($mode=0)
Retourne le libelle du statut d&#39;un adherent (brouillon, valide, resilie)
dol_textishtml($msg, $option=0)
Return if a text is a html content.