Completed
Branch develop (15a4cd)
by
unknown
46:28
created

Adherent::getNbOfEMailings()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 14
nc 2
nop 0
dl 0
loc 22
rs 9.2
c 0
b 0
f 0
1
<?php
2
/* Copyright (C) 2002-2003	Rodolphe Quiedeville	<[email protected]>
3
 * Copyright (C) 2002-2003	Jean-Louis Bergamo		<[email protected]>
4
 * Copyright (C) 2004-2012	Laurent Destailleur		<[email protected]>
5
 * Copyright (C) 2004		Sebastien Di Cintio		<[email protected]>
6
 * Copyright (C) 2004		Benoit Mortier			<[email protected]>
7
 * Copyright (C) 2009-2017	Regis Houssin			<[email protected]>
8
 * Copyright (C) 2014-2016	Alexandre Spangaro		<[email protected]>
9
 * Copyright (C) 2015		Marcos García			<[email protected]>
10
 * Copyright (C) 2015		Frederic France			<[email protected]>
11
 * Copyright (C) 2015		Raphaël Doursenaud		<[email protected]>
12
 * Copyright (C) 2016		Juanjo Menent			<[email protected]>
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
28
/**
29
 *	\file       htdocs/adherents/class/adherent.class.php
30
 *	\ingroup    member
31
 *	\brief      File of class to manage members of a foundation
32
 */
33
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
39
/**
40
 *		Class to manage members of a foundation
41
 */
42
class Adherent extends CommonObject
43
{
44
	public $element='member';
45
	public $table_element='adherent';
46
	public $ismultientitymanaged = 1;  // 0=No test on entity, 1=Test with field entity, 2=Test with link by societe
47
48
	var $mesgs;
49
50
	var $login;
51
52
	//! Clear password in memory
53
	var $pass;
54
	//! Clear password in database (defined if DATABASE_PWD_ENCRYPTED=0)
55
	var $pass_indatabase;
56
	//! Encrypted password in database (always defined)
57
	var $pass_indatabase_crypted;
58
59
	var $societe;
60
	var $company;
61
	var $address;
62
	var $zip;
63
	var $town;
64
65
	var $state_id;              // Id of department
66
	var $state_code;            // Code of department
67
	var $state;                 // Label of department
68
69
	var $email;
70
	var $skype;
71
	var $phone;
72
	var $phone_perso;
73
	var $phone_mobile;
74
75
	var $morphy;
76
	var $public;
77
	var $statut;			// -1:brouillon, 0:resilie, >=1:valide,paye
78
	var $photo;
79
80
	var $datec;
81
	var $datem;
82
	var $datefin;
83
	var $datevalid;
84
	var $birth;
85
86
	var $note_public;
87
	var $note_private;
88
89
	var $typeid;			// Id type adherent
90
	var $type;				// Libelle type adherent
91
	var $need_subscription;
92
93
	var $user_id;
94
	var $user_login;
95
96
	var $fk_soc;
97
98
	// Fields loaded by fetch_subscriptions()
99
	var $first_subscription_date;
100
	var $first_subscription_amount;
101
	var $last_subscription_date;
102
	var $last_subscription_date_start;
103
	var $last_subscription_date_end;
104
	var $last_subscription_amount;
105
	var $subscriptions=array();
106
107
	var $oldcopy;		// To contains a clone of this when we need to save old properties of object
108
109
	public $entity;
110
111
	/**
112
	 *	Constructor
113
	 *
114
	 *	@param 		DoliDB		$db		Database handler
115
	 */
116
	function __construct($db)
117
	{
118
		$this->db = $db;
119
		$this->statut = -1;
120
		// l'adherent n'est pas public par defaut
121
		$this->public = 0;
122
		// les champs optionnels sont vides
123
		$this->array_options=array();
124
	}
125
126
127
	/**
128
	 *  Function sending an email has the adherent with the text supplied in parameter.
129
	 *
130
	 *  @param	string	$text				Content of message (not html entities encoded)
131
	 *  @param	string	$subject			Subject of message
132
	 *  @param 	array	$filename_list      Array of attached files
133
	 *  @param 	array	$mimetype_list      Array of mime types of attached files
134
	 *  @param 	array	$mimefilename_list  Array of public names of attached files
135
	 *  @param 	string	$addr_cc            Email cc
136
	 *  @param 	string	$addr_bcc           Email bcc
137
	 *  @param 	int		$deliveryreceipt	Ask a delivery receipt
138
	 *  @param	int		$msgishtml			1=String IS already html, 0=String IS NOT html, -1=Unknown need autodetection
139
	 *  @param	string	$errors_to			erros to
140
	 *  @return	int							<0 if KO, >0 if OK
141
	 */
142
	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='')
143
	{
144
		global $conf,$langs;
145
146
		// Detect if message is HTML
147
		if ($msgishtml == -1)
148
		{
149
			$msgishtml = 0;
150
			if (dol_textishtml($text,0)) $msgishtml = 1;
151
		}
152
153
		dol_syslog('send_an_email msgishtml='.$msgishtml);
154
155
		$texttosend=$this->makeSubstitution($text);
156
		$subjecttosend=$this->makeSubstitution($subject);
157
		if ($msgishtml) $texttosend=dol_htmlentitiesbr($texttosend);
158
159
		// Envoi mail confirmation
160
		$from=$conf->email_from;
161
		if (! empty($conf->global->ADHERENT_MAIL_FROM)) $from=$conf->global->ADHERENT_MAIL_FROM;
162
163
		// Send email (substitutionarray must be done just before this)
164
		include_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
165
		$mailfile = new CMailFile($subjecttosend, $this->email, $from, $texttosend, $filename_list, $mimetype_list, $mimefilename_list, $addr_cc, $addr_bcc, $deliveryreceipt, $msgishtml);
166
		if ($mailfile->sendfile())
167
		{
168
			return 1;
169
		}
170
		else
171
		{
172
			$this->error=$langs->trans("ErrorFailedToSendMail",$from,$this->email).'. '.$mailfile->error;
173
			return -1;
174
		}
175
	}
176
177
178
	/**
179
	 * Make substitution of tags into text with value of current object.
180
	 *
181
	 * @param	string	$text       Text to make substitution to
182
	 * @return  string      		Value of input text string with substitutions done
183
	 */
184
	function makeSubstitution($text)
185
	{
186
		global $conf,$langs;
187
188
		$birthday = dol_print_date($this->birth,'day');
189
190
		$msgishtml = 0;
191
		if (dol_textishtml($text,1)) $msgishtml = 1;
192
193
		$infos='';
194
		if ($this->civility_id) $infos.= $langs->transnoentities("UserTitle").": ".$this->getCivilityLabel()."\n";
195
		$infos.= $langs->transnoentities("id").": ".$this->id."\n";
196
		$infos.= $langs->transnoentities("Lastname").": ".$this->lastname."\n";
197
		$infos.= $langs->transnoentities("Firstname").": ".$this->firstname."\n";
198
		$infos.= $langs->transnoentities("Company").": ".$this->societe."\n";
199
		$infos.= $langs->transnoentities("Address").": ".$this->address."\n";
200
		$infos.= $langs->transnoentities("Zip").": ".$this->zip."\n";
201
		$infos.= $langs->transnoentities("Town").": ".$this->town."\n";
202
		$infos.= $langs->transnoentities("Country").": ".$this->country."\n";
203
		$infos.= $langs->transnoentities("EMail").": ".$this->email."\n";
204
		$infos.= $langs->transnoentities("PhonePro").": ".$this->phone."\n";
205
		$infos.= $langs->transnoentities("PhonePerso").": ".$this->phone_perso."\n";
206
		$infos.= $langs->transnoentities("PhoneMobile").": ".$this->phone_mobile."\n";
207
		if (empty($conf->global->ADHERENT_LOGIN_NOT_REQUIRED))
208
		{
209
			$infos.= $langs->transnoentities("Login").": ".$this->login."\n";
210
			$infos.= $langs->transnoentities("Password").": ".$this->pass."\n";
211
		}
212
		$infos.= $langs->transnoentities("Birthday").": ".$birthday."\n";
213
		$infos.= $langs->transnoentities("Photo").": ".$this->photo."\n";
214
		$infos.= $langs->transnoentities("Public").": ".yn($this->public);
215
216
		// Substitutions
217
		$substitutionarray=array(
218
			'__CIVILITY__'=>$this->getCivilityLabel(),
219
			'__FIRSTNAME__'=>$msgishtml?dol_htmlentitiesbr($this->firstname):$this->firstname,
220
			'__LASTNAME__'=>$msgishtml?dol_htmlentitiesbr($this->lastname):$this->lastname,
221
			'__FULLNAME__'=>$msgishtml?dol_htmlentitiesbr($this->getFullName($langs)):$this->getFullName($langs),
222
			'__COMPANY__'=>$msgishtml?dol_htmlentitiesbr($this->societe):$this->societe,
223
			'__ADDRESS__'=>$msgishtml?dol_htmlentitiesbr($this->address):$this->address,
224
			'__ZIP__'=>$msgishtml?dol_htmlentitiesbr($this->zip):$this->zip,
225
			'__TOWN__'=>$msgishtml?dol_htmlentitiesbr($this->town):$this->town,
226
			'__COUNTRY__'=>$msgishtml?dol_htmlentitiesbr($this->country):$this->country,
227
			'__EMAIL__'=>$msgishtml?dol_htmlentitiesbr($this->email):$this->email,
228
			'__BIRTH__'=>$msgishtml?dol_htmlentitiesbr($birthday):$birthday,
229
			'__PHOTO__'=>$msgishtml?dol_htmlentitiesbr($this->photo):$this->photo,
230
			'__LOGIN__'=>$msgishtml?dol_htmlentitiesbr($this->login):$this->login,
231
			'__PASSWORD__'=>$msgishtml?dol_htmlentitiesbr($this->pass):$this->pass,
232
			'__PHONE__'=>$msgishtml?dol_htmlentitiesbr($this->phone):$this->phone,
233
			'__PHONEPRO__'=>$msgishtml?dol_htmlentitiesbr($this->phone_perso):$this->phone_perso,
234
			'__PHONEMOBILE__'=>$msgishtml?dol_htmlentitiesbr($this->phone_mobile):$this->phone_mobile,
235
		);
236
237
		complete_substitutions_array($substitutionarray, $langs, $this);
238
239
		return make_substitutions($text, $substitutionarray, $langs);
240
	}
241
242
243
	/**
244
	 *	Return translated label by the nature of a adherent (physical or moral)
245
	 *
246
	 *	@param	string		$morphy		Nature of the adherent (physical or moral)
247
	 *	@return	string					Label
248
	 */
249
	function getmorphylib($morphy='')
250
	{
251
		global $langs;
252
		if (! $morphy) { $morphy=$this->morphy; }
253
		if ($morphy == 'phy') { return $langs->trans("Physical"); }
254
		if ($morphy == 'mor') { return $langs->trans("Moral"); }
255
		return $morphy;
256
	}
257
258
	/**
259
	 *	Create a member into database
260
	 *
261
	 *	@param	User	$user        	Objet user qui demande la creation
262
	 *	@param  int		$notrigger		1 ne declenche pas les triggers, 0 sinon
263
	 *	@return	int						<0 if KO, >0 if OK
264
	 */
265
	function create($user,$notrigger=0)
266
	{
267
		global $conf,$langs;
268
269
		$error=0;
270
271
		$now=dol_now();
272
273
		// Clean parameters
274
		$this->import_key = trim($this->import_key);
275
276
		// Check parameters
277
		if (! empty($conf->global->ADHERENT_MAIL_REQUIRED) && ! isValidEMail($this->email))
278
		{
279
			$langs->load("errors");
280
			$this->error = $langs->trans("ErrorBadEMail",$this->email);
281
			return -1;
282
		}
283
		if (! $this->datec) $this->datec=$now;
284
		if (empty($conf->global->ADHERENT_LOGIN_NOT_REQUIRED))
285
		{
286
			if (empty($this->login))
287
			{
288
				$this->error = $langs->trans("ErrorWrongValueForParameterX","Login");
289
				return -1;
290
			}
291
		}
292
293
		$this->db->begin();
294
295
		// Insert member
296
		$sql = "INSERT INTO ".MAIN_DB_PREFIX."adherent";
297
		$sql.= " (datec,login,fk_user_author,fk_user_mod,fk_user_valid,morphy,fk_adherent_type,entity,import_key)";
298
		$sql.= " VALUES (";
299
		$sql.= " '".$this->db->idate($this->datec)."'";
300
		$sql.= ", ".($this->login?"'".$this->db->escape($this->login)."'":"null");
301
		$sql.= ", ".($user->id>0?$user->id:"null");	// Can be null because member can be created by a guest or a script
302
		$sql.= ", null, null, '".$this->db->escape($this->morphy)."'";
303
		$sql.= ", ".$this->typeid;
304
		$sql.= ", ".$conf->entity;
305
		$sql.= ", ".(! empty($this->import_key) ? "'".$this->db->escape($this->import_key)."'":"null");
306
		$sql.= ")";
307
308
		dol_syslog(get_class($this)."::create", LOG_DEBUG);
309
		$result = $this->db->query($sql);
310
		if ($result)
311
		{
312
			$id = $this->db->last_insert_id(MAIN_DB_PREFIX."adherent");
313
			if ($id > 0)
314
			{
315
				$this->id=$id;
316
				$this->ref=(string) $id;
317
318
				// Update minor fields
319
				$result=$this->update($user,1,1,0,0,'add'); // nosync is 1 to avoid update data of user
320
				if ($result < 0)
321
				{
322
					$this->db->rollback();
323
					return -1;
324
				}
325
326
				// Add link to user
327
				if ($this->user_id)
328
				{
329
					// Add link to user
330
					$sql = "UPDATE ".MAIN_DB_PREFIX."user SET";
331
					$sql.= " fk_member = ".$this->id;
332
					$sql.= " WHERE rowid = ".$this->user_id;
333
					dol_syslog(get_class($this)."::create", LOG_DEBUG);
334
					$resql = $this->db->query($sql);
335
					if (! $resql)
336
					{
337
						$this->error='Failed to update user to make link with member';
338
						$this->db->rollback();
339
						return -4;
340
					}
341
				}
342
343
				if (! $notrigger)
344
				{
345
					// Call trigger
346
					$result=$this->call_trigger('MEMBER_CREATE',$user);
347
					if ($result < 0) { $error++; }
348
					// End call triggers
349
				}
350
351
				if (count($this->errors))
352
				{
353
					dol_syslog(get_class($this)."::create ".implode(',',$this->errors), LOG_ERR);
354
					$this->db->rollback();
355
					return -3;
356
				}
357
				else
358
				{
359
					$this->db->commit();
360
					return $this->id;
361
				}
362
			}
363
			else
364
			{
365
				$this->error='Failed to get last insert id';
366
				dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
367
				$this->db->rollback();
368
				return -2;
369
			}
370
		}
371
		else
372
		{
373
			$this->error=$this->db->error();
374
			$this->db->rollback();
375
			return -1;
376
		}
377
	}
378
379
380
	/**
381
	 *	Update a member in database (standard information and password)
382
	 *
383
	 *	@param	User	$user				User making update
384
	 *	@param	int		$notrigger			1=disable trigger UPDATE (when called by create)
385
	 *	@param	int		$nosyncuser			0=Synchronize linked user (standard info), 1=Do not synchronize linked user
386
	 *	@param	int		$nosyncuserpass		0=Synchronize linked user (password), 1=Do not synchronize linked user
387
	 *	@param	int		$nosyncthirdparty	0=Synchronize linked thirdparty (standard info), 1=Do not synchronize linked thirdparty
388
	 * 	@param	string	$action				Current action for hookmanager
389
	 * 	@return	int							<0 if KO, >0 if OK
390
	 */
391
	function update($user,$notrigger=0,$nosyncuser=0,$nosyncuserpass=0,$nosyncthirdparty=0,$action='update')
392
	{
393
		global $conf, $langs, $hookmanager;
394
395
		$nbrowsaffected=0;
396
		$error=0;
397
398
		dol_syslog(get_class($this)."::update notrigger=".$notrigger.", nosyncuser=".$nosyncuser.", nosyncuserpass=".$nosyncuserpass." nosyncthirdparty=".$nosyncthirdparty.", email=".$this->email);
399
400
		// Clean parameters
401
		$this->lastname=trim($this->lastname)?trim($this->lastname):trim($this->lastname);
402
		$this->firstname=trim($this->firstname)?trim($this->firstname):trim($this->firstname);
403
		$this->address=($this->address?$this->address:$this->address);
404
		$this->zip=($this->zip?$this->zip:$this->zip);
405
		$this->town=($this->town?$this->town:$this->town);
406
		$this->country_id=($this->country_id > 0?$this->country_id:$this->country_id);
407
		$this->state_id=($this->state_id > 0?$this->state_id:$this->state_id);
408
		if (! empty($conf->global->MAIN_FIRST_TO_UPPER)) $this->lastname=ucwords(trim($this->lastname));
409
		if (! empty($conf->global->MAIN_FIRST_TO_UPPER)) $this->firstname=ucwords(trim($this->firstname));
410
		$this->note_public=($this->note_public?$this->note_public:$this->note_public);
411
		$this->note_private=($this->note_private?$this->note_private:$this->note_private);
412
413
		// Check parameters
414
		if (! empty($conf->global->ADHERENT_MAIL_REQUIRED) && ! isValidEMail($this->email))
415
		{
416
			$langs->load("errors");
417
			$this->error = $langs->trans("ErrorBadEMail",$this->email);
418
			return -1;
419
		}
420
421
		$this->db->begin();
422
423
		$sql = "UPDATE ".MAIN_DB_PREFIX."adherent SET";
424
		$sql.= " civility = ".($this->civility_id?"'".$this->db->escape($this->civility_id)."'":"null");
425
		$sql.= ", firstname = ".($this->firstname?"'".$this->db->escape($this->firstname)."'":"null");
426
		$sql.= ", lastname = ".($this->lastname?"'".$this->db->escape($this->lastname)."'":"null");
427
		$sql.= ", login = ".($this->login?"'".$this->db->escape($this->login)."'":"null");
428
		$sql.= ", societe = ".($this->societe?"'".$this->db->escape($this->societe)."'":"null");
429
		$sql.= ", fk_soc = ".($this->fk_soc > 0?$this->db->escape($this->fk_soc):"null");
430
		$sql.= ", address = ".($this->address?"'".$this->db->escape($this->address)."'":"null");
431
		$sql.= ", zip = ".($this->zip?"'".$this->db->escape($this->zip)."'":"null");
432
		$sql.= ", town = ".($this->town?"'".$this->db->escape($this->town)."'":"null");
433
		$sql.= ", country = ".($this->country_id>0?$this->db->escape($this->country_id):"null");
434
		$sql.= ", state_id = ".($this->state_id>0?$this->db->escape($this->state_id):"null");
435
		$sql.= ", email = '".$this->db->escape($this->email)."'";
436
		$sql.= ", skype = '".$this->db->escape($this->skype)."'";
437
		$sql.= ", phone = ".($this->phone?"'".$this->db->escape($this->phone)."'":"null");
438
		$sql.= ", phone_perso = ".($this->phone_perso?"'".$this->db->escape($this->phone_perso)."'":"null");
439
		$sql.= ", phone_mobile = ".($this->phone_mobile?"'".$this->db->escape($this->phone_mobile)."'":"null");
440
		$sql.= ", note_private = ".($this->note_private?"'".$this->db->escape($this->note_private)."'":"null");
441
		$sql.= ", note_public = ".($this->note_public?"'".$this->db->escape($this->note_public)."'":"null");
442
		$sql.= ", photo = ".($this->photo?"'".$this->db->escape($this->photo)."'":"null");
443
		$sql.= ", public = '".$this->db->escape($this->public)."'";
444
		$sql.= ", statut = ".$this->db->escape($this->statut);
445
		$sql.= ", fk_adherent_type = ".$this->db->escape($this->typeid);
446
		$sql.= ", morphy = '".$this->db->escape($this->morphy)."'";
447
		$sql.= ", birth = ".($this->birth?"'".$this->db->idate($this->birth)."'":"null");
448
		if ($this->datefin)   $sql.= ", datefin = '".$this->db->idate($this->datefin)."'";		// Must be modified only when deleting a subscription
449
		if ($this->datevalid) $sql.= ", datevalid = '".$this->db->idate($this->datevalid)."'";	// Must be modified only when validating a member
450
		$sql.= ", fk_user_mod = ".($user->id>0?$user->id:'null');	// Can be null because member can be create by a guest
451
		$sql.= " WHERE rowid = ".$this->id;
452
453
		dol_syslog(get_class($this)."::update update member", LOG_DEBUG);
454
		$resql = $this->db->query($sql);
455
		if ($resql)
456
		{
457
			unset($this->country_code);
458
			unset($this->country);
459
			unset($this->state_code);
460
			unset($this->state);
461
462
			$nbrowsaffected+=$this->db->affected_rows($resql);
463
464
			$action='update';
465
466
			// Actions on extra fields
467
			if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) // For avoid conflicts if trigger used
468
			{
469
				$result=$this->insertExtraFields();
470
				if ($result < 0)
471
				{
472
					$error++;
473
				}
474
			}
475
476
			// Update password
477
			if (! $error && $this->pass)
478
			{
479
				dol_syslog(get_class($this)."::update update password");
480
				if ($this->pass != $this->pass_indatabase && $this->pass != $this->pass_indatabase_crypted)
481
				{
482
					$isencrypted = empty($conf->global->DATABASE_PWD_ENCRYPTED)?0:1;
483
484
					// If password to set differs from the one found into database
485
					$result=$this->setPassword($user,$this->pass,$isencrypted,$notrigger,$nosyncuserpass);
486
					if (! $nbrowsaffected) $nbrowsaffected++;
487
				}
488
			}
489
490
			// Remove links to user and replace with new one
491
			if (! $error)
492
			{
493
				dol_syslog(get_class($this)."::update update link to user");
494
				$sql = "UPDATE ".MAIN_DB_PREFIX."user SET fk_member = NULL WHERE fk_member = ".$this->id;
495
				dol_syslog(get_class($this)."::update", LOG_DEBUG);
496
				$resql = $this->db->query($sql);
497
				if (! $resql) { $this->error=$this->db->error(); $this->db->rollback(); return -5; }
498
				// If there is a user linked to this member
499
				if ($this->user_id > 0)
500
				{
501
					$sql = "UPDATE ".MAIN_DB_PREFIX."user SET fk_member = ".$this->id." WHERE rowid = ".$this->user_id;
502
					dol_syslog(get_class($this)."::update", LOG_DEBUG);
503
					$resql = $this->db->query($sql);
504
					if (! $resql) { $this->error=$this->db->error(); $this->db->rollback(); return -5; }
505
				}
506
			}
507
508
			if (! $error && $nbrowsaffected)	// If something has change in main data
509
			{
510
				// Update information on linked user if it is an update
511
				if (! $error && $this->user_id > 0 && ! $nosyncuser)
512
				{
513
					require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
514
515
					dol_syslog(get_class($this)."::update update linked user");
516
517
					$luser=new User($this->db);
518
					$result=$luser->fetch($this->user_id);
519
520
					if ($result >= 0)
521
					{
522
						//var_dump($this->user_login);exit;
523
						//var_dump($this->login);exit;
524
525
						// 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.
526
						if (empty($conf->global->ADHERENT_LOGIN_NOT_REQUIRED)) $luser->login=$this->login;
527
528
						$luser->civility_id=$this->civility_id;
529
						$luser->firstname=$this->firstname;
530
						$luser->lastname=$this->lastname;
531
						$luser->pass=$this->pass;
532
						$luser->societe_id=$this->societe;
533
534
						$luser->email=$this->email;
535
						$luser->skype=$this->skype;
536
						$luser->office_phone=$this->phone;
537
						$luser->user_mobile=$this->phone_mobile;
538
539
						$luser->fk_member=$this->id;
540
541
						$result=$luser->update($user,0,1,1);	// Use nosync to 1 to avoid cyclic updates
542
						if ($result < 0)
543
						{
544
							$this->error=$luser->error;
545
							dol_syslog(get_class($this)."::update ".$this->error,LOG_ERR);
546
							$error++;
547
						}
548
					}
549
					else
550
					{
551
						$this->error=$luser->error;
552
						$error++;
553
					}
554
				}
555
556
				// Update information on linked thirdparty if it is an update
557
				if (! $error && $this->fk_soc > 0 && ! $nosyncthirdparty)
558
				{
559
					require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
560
561
					dol_syslog(get_class($this)."::update update linked thirdparty");
562
563
					// This member is linked with a thirdparty, so we also update thirdparty informations
564
					// if this is an update.
565
					$lthirdparty=new Societe($this->db);
566
					$result=$lthirdparty->fetch($this->fk_soc);
567
568
					if ($result >= 0)
569
					{
570
						$lthirdparty->address=$this->address;
571
						$lthirdparty->zip=$this->zip;
572
						$lthirdparty->town=$this->town;
573
						$lthirdparty->email=$this->email;
574
						$lthirdparty->skype=$this->skype;
575
						$lthirdparty->phone=$this->phone;
576
						$lthirdparty->state_id=$this->state_id;
577
						$lthirdparty->country_id=$this->country_id;
578
						$lthirdparty->country_id=$this->country_id;
579
						//$lthirdparty->phone_mobile=$this->phone_mobile;
580
581
						$result=$lthirdparty->update($this->fk_soc,$user,0,1,1,'update');	// Use sync to 0 to avoid cyclic updates
582
						if ($result < 0)
583
						{
584
							$this->error=$lthirdparty->error;
585
							dol_syslog(get_class($this)."::update ".$this->error,LOG_ERR);
586
							$error++;
587
						}
588
					}
589
					else
590
					{
591
						$this->error=$lthirdparty->error;
592
						$error++;
593
					}
594
				}
595
596
				if (! $error && ! $notrigger)
597
				{
598
					// Call trigger
599
					$result=$this->call_trigger('MEMBER_MODIFY',$user);
600
					if ($result < 0) { $error++; }
601
					// End call triggers
602
				}
603
			}
604
605
			if (! $error)
606
			{
607
				$this->db->commit();
608
				return $nbrowsaffected;
609
			}
610
			else
611
			{
612
				$this->db->rollback();
613
				return -1;
614
			}
615
		}
616
		else
617
		{
618
			$this->db->rollback();
619
			$this->error=$this->db->lasterror();
620
			return -2;
621
		}
622
	}
623
624
625
	/**
626
	 *	Update denormalized last subscription date.
627
	 * 	This function is called when we delete a subscription for example.
628
	 *
629
	 *	@param	User	$user			User making change
630
	 *	@return	int						<0 if KO, >0 if OK
631
	 */
632
	function update_end_date($user)
633
	{
634
		$this->db->begin();
635
636
		// Search for last subscription id and end date
637
		$sql = "SELECT rowid, datec as dateop, dateadh as datedeb, datef as datefin";
638
		$sql.= " FROM ".MAIN_DB_PREFIX."subscription";
639
		$sql.= " WHERE fk_adherent=".$this->id;
640
		$sql.= " ORDER by dateadh DESC";	// Sort by start subscription date
641
642
		dol_syslog(get_class($this)."::update_end_date", LOG_DEBUG);
643
		$resql=$this->db->query($sql);
644
		if ($resql)
645
		{
646
			$obj=$this->db->fetch_object($resql);
647
			$dateop=$this->db->jdate($obj->dateop);
648
			$datedeb=$this->db->jdate($obj->datedeb);
649
			$datefin=$this->db->jdate($obj->datefin);
650
651
			$sql = "UPDATE ".MAIN_DB_PREFIX."adherent SET";
652
			$sql.= " datefin=".($datefin != '' ? "'".$this->db->idate($datefin)."'" : "null");
653
			$sql.= " WHERE rowid = ".$this->id;
654
655
			dol_syslog(get_class($this)."::update_end_date", LOG_DEBUG);
656
			$resql=$this->db->query($sql);
657
			if ($resql)
658
			{
659
				$this->last_subscription_date=$dateop;
660
				$this->last_subscription_date_start=$datedeb;
661
				$this->last_subscription_date_end=$datefin;
662
				$this->datefin=$datefin;
663
				$this->db->commit();
664
				return 1;
665
			}
666
			else
667
			{
668
				$this->db->rollback();
669
				return -1;
670
			}
671
		}
672
		else
673
		{
674
			$this->error=$this->db->lasterror();
675
			$this->db->rollback();
676
			return -1;
677
		}
678
679
	}
680
681
	/**
682
	 *  Fonction qui supprime l'adherent et les donnees associees
683
	 *
684
	 *  @param	int		$rowid		Id of member to delete
685
	 *	@param	User		$user		User object
686
	 *	@param	int		$notrigger	1=Does not execute triggers, 0= execute triggers
687
	 *  @return	int					<0 if KO, 0=nothing to do, >0 if OK
688
	 */
689
	function delete($rowid, $user, $notrigger=0)
690
	{
691
		global $conf, $langs;
692
693
		$result = 0;
694
		$error=0;
695
		$errorflag=0;
696
697
		// Check parameters
698
		if (empty($rowid)) $rowid=$this->id;
699
700
		$this->db->begin();
701
702
		if (! $error && ! $notrigger)
703
		{
704
			// Call trigger
705
			$result=$this->call_trigger('MEMBER_DELETE',$user);
706
			if ($result < 0) $error++;
707
			// End call triggers
708
		}
709
710
		// Remove category
711
		$sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_member WHERE fk_member = ".$rowid;
712
		dol_syslog(get_class($this)."::delete", LOG_DEBUG);
713
		$resql=$this->db->query($sql);
714
		if (! $resql)
715
		{
716
			$error++;
717
			$this->error .= $this->db->lasterror();
718
			$errorflag=-1;
719
		}
720
721
		// Remove subscription
722
		if (! $error)
723
		{
724
			$sql = "DELETE FROM ".MAIN_DB_PREFIX."subscription WHERE fk_adherent = ".$rowid;
725
			dol_syslog(get_class($this)."::delete", LOG_DEBUG);
726
			$resql=$this->db->query($sql);
727
			if (! $resql)
728
			{
729
				$error++;
730
				$this->error .= $this->db->lasterror();
731
				$errorflag=-2;
732
			}
733
		}
734
735
		// Remove linked user
736
		if (! $error)
737
		{
738
			$ret=$this->setUserId(0);
739
			if ($ret < 0)
740
			{
741
				$error++;
742
				$this->error .= $this->db->lasterror();
743
				$errorflag=-3;
744
			}
745
		}
746
747
		// Removed extrafields
748
		if (! $error)
749
		{
750
			if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) // For avoid conflicts if trigger used
751
			{
752
				$result=$this->deleteExtraFields();
753
				if ($result < 0)
754
				{
755
					$error++;
756
					$errorflag=-4;
757
					dol_syslog(get_class($this)."::delete erreur ".$errorflag." ".$this->error, LOG_ERR);
758
				}
759
			}
760
		}
761
762
		// Remove adherent
763
		if (! $error)
764
		{
765
			$sql = "DELETE FROM ".MAIN_DB_PREFIX."adherent WHERE rowid = ".$rowid;
766
			dol_syslog(get_class($this)."::delete", LOG_DEBUG);
767
			$resql=$this->db->query($sql);
768
			if (! $resql)
769
			{
770
				$error++;
771
				$this->error .= $this->db->lasterror();
772
				$errorflag=-5;
773
			}
774
		}
775
776
		if (! $error)
777
		{
778
			$this->db->commit();
779
			return 1;
780
		}
781
		else
782
		{
783
			$this->db->rollback();
784
			return $errorflag;
785
		}
786
	}
787
788
789
	/**
790
	 *    Change password of a user
791
	 *
792
	 *    @param	User	$user           Object user de l'utilisateur qui fait la modification
793
	 *    @param 	string	$password       New password (to generate if empty)
794
	 *    @param    int		$isencrypted    0 ou 1 si il faut crypter le mot de passe en base (0 par defaut)
795
	 *	  @param	int		$notrigger		1=Ne declenche pas les triggers
796
	 *    @param	int		$nosyncuser		Do not synchronize linked user
797
	 *    @return   string           		If OK return clear password, 0 if no change, < 0 if error
798
	 */
799
	function setPassword($user, $password='', $isencrypted=0, $notrigger=0, $nosyncuser=0)
800
	{
801
		global $conf, $langs;
802
803
		$error=0;
804
805
		dol_syslog(get_class($this)."::setPassword user=".$user->id." password=".preg_replace('/./i','*',$password)." isencrypted=".$isencrypted);
806
807
		// If new password not provided, we generate one
808
		if (! $password)
809
		{
810
			require_once DOL_DOCUMENT_ROOT.'/core/lib/security2.lib.php';
811
			$password=getRandomPassword(false);
812
		}
813
814
		// Crypt password
815
		$password_crypted = dol_hash($password);
816
817
		$password_indatabase = '';
818
		if (! $isencrypted)
819
		{
820
			$password_indatabase = $password;
821
		}
822
823
		$this->db->begin();
824
825
		// Mise a jour
826
		$sql = "UPDATE ".MAIN_DB_PREFIX."adherent";
827
		$sql.= " SET pass_crypted = '".$this->db->escape($password_crypted)."'";
828
		//if (! empty($conf->global->DATABASE_PWD_ENCRYPTED))
829
		if ($isencrypted)
830
		{
831
			$sql.= ", pass = null";
832
		}
833
		else
834
		{
835
			$sql.= ", pass = '".$this->db->escape($password_indatabase)."'";
836
		}
837
		$sql.= " WHERE rowid = ".$this->id;
838
839
		//dol_syslog("Adherent::Password sql=hidden");
840
		dol_syslog(get_class($this)."::setPassword", LOG_DEBUG);
841
		$result = $this->db->query($sql);
842
		if ($result)
843
		{
844
			$nbaffectedrows=$this->db->affected_rows($result);
845
846
			if ($nbaffectedrows)
847
			{
848
				$this->pass=$password;
849
				$this->pass_indatabase=$password_indatabase;
850
				$this->pass_indatabase_crypted=$password_crypted;
851
852
				if ($this->user_id && ! $nosyncuser)
853
				{
854
					require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
855
856
					// This member is linked with a user, so we also update users informations
857
					// if this is an update.
858
					$luser=new User($this->db);
859
					$result=$luser->fetch($this->user_id);
860
861
					if ($result >= 0)
862
					{
863
						$result=$luser->setPassword($user,$this->pass,0,0,1);
864
						if ($result < 0)
865
						{
866
							$this->error=$luser->error;
867
							dol_syslog(get_class($this)."::setPassword ".$this->error,LOG_ERR);
868
							$error++;
869
						}
870
					}
871
					else
872
					{
873
						$this->error=$luser->error;
874
						$error++;
875
					}
876
				}
877
878
				if (! $error && ! $notrigger)
879
				{
880
					// Call trigger
881
					$result=$this->call_trigger('MEMBER_NEW_PASSWORD',$user);
882
					if ($result < 0) { $error++; $this->db->rollback(); return -1; }
883
					// End call triggers
884
				}
885
886
				$this->db->commit();
887
				return $this->pass;
888
			}
889
			else
890
			{
891
				$this->db->rollback();
892
				return 0;
893
			}
894
		}
895
		else
896
		{
897
			$this->db->rollback();
898
			dol_print_error($this->db);
899
			return -1;
900
		}
901
	}
902
903
904
	/**
905
	 *    Set link to a user
906
	 *
907
	 *    @param     int	$userid        	Id of user to link to
908
	 *    @return    int					1=OK, -1=KO
909
	 */
910
	function setUserId($userid)
911
	{
912
		global $conf, $langs;
913
914
		$this->db->begin();
915
916
		// If user is linked to this member, remove old link to this member
917
		$sql = "UPDATE ".MAIN_DB_PREFIX."user SET fk_member = NULL WHERE fk_member = ".$this->id;
918
		dol_syslog(get_class($this)."::setUserId", LOG_DEBUG);
919
		$resql = $this->db->query($sql);
920
		if (! $resql) { $this->error=$this->db->error(); $this->db->rollback(); return -1; }
921
922
		// Set link to user
923
		if ($userid > 0)
924
		{
925
			$sql = "UPDATE ".MAIN_DB_PREFIX."user SET fk_member = ".$this->id;
926
			$sql.= " WHERE rowid = ".$userid;
927
			dol_syslog(get_class($this)."::setUserId", LOG_DEBUG);
928
			$resql = $this->db->query($sql);
929
			if (! $resql) { $this->error=$this->db->error(); $this->db->rollback(); return -2; }
930
		}
931
932
		$this->db->commit();
933
934
		return 1;
935
	}
936
937
938
	/**
939
	 *    Set link to a third party
940
	 *
941
	 *    @param     int	$thirdpartyid		Id of user to link to
942
	 *    @return    int						1=OK, -1=KO
943
	 */
944
	function setThirdPartyId($thirdpartyid)
945
	{
946
		global $conf, $langs;
947
948
		$this->db->begin();
949
950
		// Remove link to third party onto any other members
951
		if ($thirdpartyid > 0)
952
		{
953
			$sql = "UPDATE ".MAIN_DB_PREFIX."adherent SET fk_soc = null";
954
			$sql.= " WHERE fk_soc = '".$thirdpartyid."'";
955
			$sql.= " AND entity = ".$conf->entity;
956
			dol_syslog(get_class($this)."::setThirdPartyId", LOG_DEBUG);
957
			$resql = $this->db->query($sql);
958
		}
959
960
		// Add link to third party for current member
961
		$sql = "UPDATE ".MAIN_DB_PREFIX."adherent SET fk_soc = ".($thirdpartyid>0 ? $thirdpartyid : 'null');
962
		$sql.= " WHERE rowid = ".$this->id;
963
964
		dol_syslog(get_class($this)."::setThirdPartyId", LOG_DEBUG);
965
		$resql = $this->db->query($sql);
966
		if ($resql)
967
		{
968
			$this->db->commit();
969
			return 1;
970
		}
971
		else
972
		{
973
			$this->error=$this->db->error();
974
			$this->db->rollback();
975
			return -1;
976
		}
977
	}
978
979
980
	/**
981
	 *	Method to load member from its login
982
	 *
983
	 *	@param	string	$login		login of member
984
	 *	@return	void
985
	 */
986
	function fetch_login($login)
987
	{
988
		global $conf;
989
990
		$sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."adherent";
991
		$sql.= " WHERE login='".$this->db->escape($login)."'";
992
		$sql.= " AND entity = ".$conf->entity;
993
994
		$resql=$this->db->query($sql);
995
		if ($resql)
996
		{
997
			if ($this->db->num_rows($resql))
998
			{
999
				$obj = $this->db->fetch_object($resql);
1000
				$this->fetch($obj->rowid);
1001
			}
1002
		}
1003
		else
1004
		{
1005
			dol_print_error($this->db);
1006
		}
1007
	}
1008
1009
	/**
1010
	 *	Method to load member from its name
1011
	 *
1012
	 *	@param	string	$firstname	Firstname
1013
	 *	@param	string	$lastname	Lastname
1014
	 *	@return	void
1015
	 */
1016
	function fetch_name($firstname,$lastname)
1017
	{
1018
		global $conf;
1019
1020
		$sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."adherent";
1021
		$sql.= " WHERE firstname='".$this->db->escape($firstname)."'";
1022
		$sql.= " AND lastname='".$this->db->escape($lastname)."'";
1023
		$sql.= " AND entity = ".$conf->entity;
1024
1025
		$resql=$this->db->query($sql);
1026
		if ($resql)
1027
		{
1028
			if ($this->db->num_rows($resql))
1029
			{
1030
				$obj = $this->db->fetch_object($resql);
1031
				$this->fetch($obj->rowid);
1032
			}
1033
		}
1034
		else
1035
		{
1036
			dol_print_error($this->db);
1037
		}
1038
	}
1039
1040
	/**
1041
	 *	Load member from database
1042
	 *
1043
	 *	@param	int		$rowid      			Id of object to load
1044
	 * 	@param	string	$ref					To load member from its ref
1045
	 * 	@param	int		$fk_soc					To load member from its link to third party
1046
	 * 	@param	string	$ref_ext				External reference
1047
	 *  @param	bool	$fetch_optionals		To load optionals (extrafields)
1048
	 *  @param	bool	$fetch_subscriptions	To load member subscriptions
1049
	 *	@return int								>0 if OK, 0 if not found, <0 if KO
1050
	 */
1051
	function fetch($rowid,$ref='',$fk_soc='',$ref_ext='',$fetch_optionals=true,$fetch_subscriptions=true)
1052
	{
1053
		global $langs;
1054
1055
		$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,";
1056
		$sql.= " d.note_public,";
1057
		$sql.= " d.email, d.skype, d.phone, d.phone_perso, d.phone_mobile, d.login, d.pass, d.pass_crypted,";
1058
		$sql.= " d.photo, d.fk_adherent_type, d.morphy, d.entity,";
1059
		$sql.= " d.datec as datec,";
1060
		$sql.= " d.tms as datem,";
1061
		$sql.= " d.datefin as datefin,";
1062
		$sql.= " d.birth as birthday,";
1063
		$sql.= " d.datevalid as datev,";
1064
		$sql.= " d.country,";
1065
		$sql.= " d.state_id,";
1066
		$sql.= " d.model_pdf,";
1067
		$sql.= " c.rowid as country_id, c.code as country_code, c.label as country,";
1068
		$sql.= " dep.nom as state, dep.code_departement as state_code,";
1069
		$sql.= " t.libelle as type, t.subscription as subscription,";
1070
		$sql.= " u.rowid as user_id, u.login as user_login";
1071
		$sql.= " FROM ".MAIN_DB_PREFIX."adherent_type as t, ".MAIN_DB_PREFIX."adherent as d";
1072
		$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."c_country as c ON d.country = c.rowid";
1073
		$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."c_departements as dep ON d.state_id = dep.rowid";
1074
		$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."user as u ON d.rowid = u.fk_member";
1075
		$sql.= " WHERE d.fk_adherent_type = t.rowid";
1076
		if ($rowid) $sql.= " AND d.rowid=".$rowid;
1077
		elseif ($ref || $fk_soc) {
1078
			$sql.= " AND d.entity IN (".getEntity('adherent').")";
1079
			if ($ref) $sql.= " AND d.rowid='".$this->db->escape($ref)."'";
1080
			elseif ($fk_soc > 0) $sql.= " AND d.fk_soc=".$fk_soc;
1081
		}
1082
		elseif ($ref_ext)
1083
		{
1084
			$sql.= " AND d.ref_ext='".$this->db->escape($ref_ext)."'";
1085
		}
1086
1087
		dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
1088
		$resql=$this->db->query($sql);
1089
		if ($resql)
1090
		{
1091
			if ($this->db->num_rows($resql))
1092
			{
1093
				$obj = $this->db->fetch_object($resql);
1094
1095
				$this->entity			= $obj->entity;
1096
				$this->ref				= $obj->rowid;
1097
				$this->id				= $obj->rowid;
1098
				$this->ref_ext			= $obj->ref_ext;
1099
				$this->civility_id		= $obj->civility_id;
1100
				$this->firstname		= $obj->firstname;
1101
				$this->lastname			= $obj->lastname;
1102
				$this->login			= $obj->login;
1103
				$this->societe			= $obj->company;
1104
				$this->company			= $obj->company;
1105
				$this->fk_soc			= $obj->fk_soc;
1106
				$this->address			= $obj->address;
1107
				$this->zip				= $obj->zip;
1108
				$this->town				= $obj->town;
1109
1110
				$this->pass				= $obj->pass;
1111
				$this->pass_indatabase  = $obj->pass;
1112
				$this->pass_indatabase_crypted = $obj->pass_crypted;
1113
1114
				$this->state_id			= $obj->state_id;
1115
				$this->state_code		= $obj->state_id?$obj->state_code:'';
1116
				$this->state			= $obj->state_id?$obj->state:'';
1117
1118
				$this->country_id		= $obj->country_id;
1119
				$this->country_code		= $obj->country_code;
1120
				if ($langs->trans("Country".$obj->country_code) != "Country".$obj->country_code)
1121
					$this->country = $langs->transnoentitiesnoconv("Country".$obj->country_code);
1122
				else
1123
					$this->country=$obj->country;
1124
1125
				$this->phone			= $obj->phone;
1126
				$this->phone_perso		= $obj->phone_perso;
1127
				$this->phone_mobile		= $obj->phone_mobile;
1128
				$this->email			= $obj->email;
1129
				$this->skype			= $obj->skype;
1130
1131
				$this->photo			= $obj->photo;
1132
				$this->statut			= $obj->statut;
1133
				$this->public			= $obj->public;
1134
1135
				$this->datec			= $this->db->jdate($obj->datec);
1136
				$this->datem			= $this->db->jdate($obj->datem);
1137
				$this->datefin			= $this->db->jdate($obj->datefin);
1138
				$this->datevalid		= $this->db->jdate($obj->datev);
1139
				$this->birth			= $this->db->jdate($obj->birthday);
1140
1141
				$this->note_private		= $obj->note_private;
1142
				$this->note_public		= $obj->note_public;
1143
				$this->morphy			= $obj->morphy;
1144
1145
				$this->typeid			= $obj->fk_adherent_type;
1146
				$this->type				= $obj->type;
1147
				$this->need_subscription 	= $obj->subscription;
1148
1149
				$this->user_id			= $obj->user_id;
1150
				$this->user_login		= $obj->user_login;
1151
1152
				$this->model_pdf        = $obj->model_pdf;
1153
1154
				// Retreive all extrafield
1155
				// fetch optionals attributes and labels
1156
				if ($fetch_optionals) {
1157
					$this->fetch_optionals();
1158
				}
1159
1160
				// Load other properties
1161
				if ($fetch_subscriptions) {
1162
					$result=$this->fetch_subscriptions();
1163
				}
1164
1165
				return $this->id;
1166
			}
1167
			else
1168
			{
1169
				return 0;
1170
			}
1171
		}
1172
		else
1173
		{
1174
			$this->error=$this->db->lasterror();
1175
			return -1;
1176
		}
1177
	}
1178
1179
1180
	/**
1181
	 *	Fonction qui recupere pour un adherent les parametres
1182
	 *				first_subscription_date
1183
	 *				first_subscription_amount
1184
	 *				last_subscription_date
1185
	 *				last_subscription_amount
1186
	 *
1187
	 *	@return		int			<0 si KO, >0 si OK
1188
	 */
1189
	function fetch_subscriptions()
1190
	{
1191
		global $langs;
1192
1193
		require_once DOL_DOCUMENT_ROOT.'/adherents/class/subscription.class.php';
1194
1195
		$sql = "SELECT c.rowid, c.fk_adherent, c.subscription, c.note, c.fk_bank,";
1196
		$sql.= " c.tms as datem,";
1197
		$sql.= " c.datec as datec,";
1198
		$sql.= " c.dateadh as dateh,";
1199
		$sql.= " c.datef as datef";
1200
		$sql.= " FROM ".MAIN_DB_PREFIX."subscription as c";
1201
		$sql.= " WHERE c.fk_adherent = ".$this->id;
1202
		$sql.= " ORDER BY c.dateadh";
1203
		dol_syslog(get_class($this)."::fetch_subscriptions", LOG_DEBUG);
1204
1205
		$resql=$this->db->query($sql);
1206
		if ($resql)
1207
		{
1208
			$this->subscriptions=array();
1209
1210
			$i=0;
1211
			while ($obj = $this->db->fetch_object($resql))
1212
			{
1213
				if ($i==0)
1214
				{
1215
					$this->first_subscription_date=$obj->dateh;
1216
					$this->first_subscription_amount=$obj->subscription;
1217
				}
1218
				$this->last_subscription_date=$obj->dateh;
1219
				$this->last_subscription_amount=$obj->subscription;
1220
1221
				$subscription=new Subscription($this->db);
1222
				$subscription->id=$obj->rowid;
1223
				$subscription->fk_adherent=$obj->fk_adherent;
1224
				$subscription->amount=$obj->subscription;
1225
				$subscription->note=$obj->note;
1226
				$subscription->fk_bank=$obj->fk_bank;
1227
				$subscription->datem=$this->db->jdate($obj->datem);
1228
				$subscription->datec=$this->db->jdate($obj->datec);
1229
				$subscription->dateh=$this->db->jdate($obj->dateh);
1230
				$subscription->datef=$this->db->jdate($obj->datef);
1231
1232
				$this->subscriptions[]=$subscription;
1233
1234
				$i++;
1235
			}
1236
			return 1;
1237
		}
1238
		else
1239
		{
1240
			$this->error=$this->db->error().' sql='.$sql;
1241
			return -1;
1242
		}
1243
	}
1244
1245
1246
	/**
1247
	 *	Insert subscription into database and eventually add links to banks, mailman, etc...
1248
	 *
1249
	 *	@param	int	        $date        		Date of effect of subscription
1250
	 *	@param	double		$amount     		Amount of subscription (0 accepted for some members)
1251
	 *	@param	int			$accountid			Id bank account
1252
	 *	@param	string		$operation			Type of payment (if Id bank account provided). Example: 'CB', ...
1253
	 *	@param	string		$label				Label operation (if Id bank account provided)
1254
	 *	@param	string		$num_chq			Numero cheque (if Id bank account provided)
1255
	 *	@param	string		$emetteur_nom		Name of cheque writer
1256
	 *	@param	string		$emetteur_banque	Name of bank of cheque
1257
	 *	@param	int     	$datesubend			Date end subscription
1258
	 *	@return int         					rowid of record added, <0 if KO
1259
	 */
1260
	function subscription($date, $amount, $accountid=0, $operation='', $label='', $num_chq='', $emetteur_nom='', $emetteur_banque='', $datesubend=0)
1261
	{
1262
		global $conf,$langs,$user;
1263
1264
		require_once DOL_DOCUMENT_ROOT.'/adherents/class/subscription.class.php';
1265
1266
		$error=0;
1267
1268
		// Clean parameters
1269
		if (! $amount) $amount=0;
1270
1271
		$this->db->begin();
1272
1273
		if ($datesubend)
1274
		{
1275
			$datefin=$datesubend;
1276
		}
1277
		else
1278
		{
1279
			// If no end date, end date = date + 1 year - 1 day
1280
			$datefin = dol_time_plus_duree($date,1,'y');
1281
			$datefin = dol_time_plus_duree($datefin,-1,'d');
1282
		}
1283
1284
		// Create subscription
1285
		$subscription=new Subscription($this->db);
1286
		$subscription->fk_adherent=$this->id;
1287
		$subscription->dateh=$date;		// Date of new subscription
1288
		$subscription->datef=$datefin;	// End data of new subscription
1289
		$subscription->amount=$amount;
1290
		$subscription->note=$label;		// deprecated
1291
		$subscription->note_public=$label;
1292
1293
		$rowid=$subscription->create($user);
1294
		if ($rowid > 0)
1295
		{
1296
			// Update denormalized subscription end date (read database subscription to find values)
1297
			// This will also update this->datefin
1298
			$result=$this->update_end_date($user);
1299
			if ($result > 0)
1300
			{
1301
				// Change properties of object (used by triggers)
1302
				$this->last_subscription_date=dol_now();
1303
				$this->last_subscription_amount=$amount;
1304
				$this->last_subscription_date_start=$date;
1305
				$this->last_subscription_date_end=$datefin;
1306
			}
1307
1308
			if (! $error)
1309
			{
1310
				$this->db->commit();
1311
				return $rowid;
1312
			}
1313
			else
1314
			{
1315
				$this->db->rollback();
1316
				return -2;
1317
			}
1318
		}
1319
		else
1320
		{
1321
			$this->error=$subscription->error;
1322
			$this->errors=$subscription->errors;
1323
			$this->db->rollback();
1324
			return -1;
1325
		}
1326
	}
1327
1328
1329
	/**
1330
	 *	Do complementary actions after subscription recording.
1331
	 *
1332
	 *	@param	int			$subscriptionid			Id of created subscription
1333
	 *  @param	string		$option					Which action ('bankdirect', 'bankviainvoice', 'invoiceonly', ...)
1334
	 *	@param	int			$accountid				Id bank account
1335
	 *	@param	int			$datesubscription		Date of subscription
1336
	 *	@param	int			$paymentdate			Date of payment
1337
	 *	@param	string		$operation				Code of type of operation (if Id bank account provided). Example 'CB', ...
1338
	 *	@param	string		$label					Label operation (if Id bank account provided)
1339
	 *	@param	double		$amount     			Amount of subscription (0 accepted for some members)
1340
	 *	@param	string		$num_chq				Numero cheque (if Id bank account provided)
1341
	 *	@param	string		$emetteur_nom			Name of cheque writer
1342
	 *	@param	string		$emetteur_banque		Name of bank of cheque
1343
	 *  @param	string		$autocreatethirdparty	Auto create new thirdparty if member not linked to a thirdparty and we request an option that generate invoice.
1344
	 *	@return int									<0 if KO, >0 if OK
1345
	 */
1346
	function subscriptionComplementaryActions($subscriptionid, $option, $accountid, $datesubscription, $paymentdate, $operation, $label, $amount, $num_chq, $emetteur_nom='', $emetteur_banque='', $autocreatethirdparty=0)
1347
	{
1348
		global $conf, $langs, $user, $mysoc;
1349
1350
		$error = 0;
1351
1352
		$this->invoice = null;	// This will contains invoice if an invoice is created
1353
1354
		dol_syslog("subscriptionComplementaryActions subscriptionid=".$subscriptionid." option=".$option." accountid=".$accountid." datesubscription=".$datesubscription." paymentdate=".$paymentdate." label=".$label." amount=".$amount." num_chq=".$num_chq." autocreatethirdparty=".$autocreatethirdparty);
1355
1356
		// Insert into bank account directlty (if option choosed for) + link to llx_subscription if option is 'bankdirect'
1357
		if ($option == 'bankdirect' && $accountid)
1358
		{
1359
			require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
1360
1361
			$acct=new Account($this->db);
1362
			$result=$acct->fetch($accountid);
1363
1364
			$dateop=$paymentdate;
1365
1366
			$insertid=$acct->addline($dateop, $operation, $label, $amount, $num_chq, '', $user, $emetteur_nom, $emetteur_banque);
1367
			if ($insertid > 0)
1368
			{
1369
				$inserturlid=$acct->add_url_line($insertid, $this->id, DOL_URL_ROOT.'/adherents/card.php?rowid=', $this->getFullname($langs), 'member');
1370
				if ($inserturlid > 0)
1371
				{
1372
					// Update table subscription
1373
					$sql ="UPDATE ".MAIN_DB_PREFIX."subscription SET fk_bank=".$insertid;
1374
					$sql.=" WHERE rowid=".$subscriptionid;
1375
1376
					dol_syslog("subscription::subscription", LOG_DEBUG);
1377
					$resql = $this->db->query($sql);
1378
					if (! $resql)
1379
					{
1380
						$error++;
1381
						$this->error=$this->db->lasterror();
1382
						$this->errors[]=$this->error;
1383
					}
1384
				}
1385
				else
1386
				{
1387
					$error++;
1388
					$this->error=$acct->error;
1389
					$this->errors=$acct->errors;
1390
				}
1391
			}
1392
			else
1393
			{
1394
				$error++;
1395
				$this->error=$acct->error;
1396
				$this->errors=$acct->errors;
1397
			}
1398
		}
1399
1400
		// If option choosed, we create invoice
1401
		if (($option == 'bankviainvoice' && $accountid) || $option == 'invoiceonly')
1402
		{
1403
			require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
1404
			require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/paymentterm.class.php';
1405
1406
			$invoice=new Facture($this->db);
1407
			$customer=new Societe($this->db);
1408
1409
			if (! $error)
1410
			{
1411
				if (! ($this->fk_soc > 0))	// If not yet linked to a company
1412
				{
1413
					if ($autocreatethirdparty)
1414
					{
1415
						// Create a linked thirdparty to member
1416
						$companyalias='';
1417
						$fullname = $this->getFullName($langs);
1418
1419
						if ($this->morphy == 'mor')
1420
						{
1421
							$companyname=$this->societe;
1422
							if (! empty($fullname)) $companyalias=$fullname;
1423
						}
1424
						else
1425
						{
1426
							$companyname=$fullname;
1427
							if (! empty($this->societe)) $companyalias=$this->societe;
1428
						}
1429
1430
						$result=$customer->create_from_member($this, $companyname, $companyalias);
1431
						if ($result < 0)
1432
						{
1433
							$this->error = $customer->error;
1434
							$this->errors = $customer->errors;
1435
							$error++;
1436
						}
1437
						else
1438
						{
1439
							$this->fk_soc = $result;
1440
						}
1441
					}
1442
					else
1443
					{
1444
						 $langs->load("errors");
1445
						 $this->error=$langs->trans("ErrorMemberNotLinkedToAThirpartyLinkOrCreateFirst");
1446
						 $this->errors[]=$this->error;
1447
						 $error++;
1448
					}
1449
				}
1450
			}
1451
			if (! $error)
1452
			{
1453
				$result=$customer->fetch($this->fk_soc);
1454
				if ($result <= 0)
1455
				{
1456
					$this->error=$customer->error;
1457
					$this->errors=$customer->errors;
1458
					$error++;
1459
				}
1460
			}
1461
1462
			if (! $error)
1463
			{
1464
				// Create draft invoice
1465
				$invoice->type=Facture::TYPE_STANDARD;
1466
				$invoice->cond_reglement_id=$customer->cond_reglement_id;
1467
				if (empty($invoice->cond_reglement_id))
1468
				{
1469
					$paymenttermstatic=new PaymentTerm($this->db);
1470
					$invoice->cond_reglement_id=$paymenttermstatic->getDefaultId();
1471
					if (empty($invoice->cond_reglement_id))
1472
					{
1473
						$error++;
1474
						$this->error='ErrorNoPaymentTermRECEPFound';
1475
						$this->errors[]=$this->error;
1476
					}
1477
				}
1478
				$invoice->socid=$this->fk_soc;
1479
				$invoice->date=$datesubscription;
1480
1481
				// Possibility to add external linked objects with hooks
1482
				$invoice->linked_objects['subscription'] = $subscriptionid;
1483
				if (! empty($_POST['other_linked_objects']) && is_array($_POST['other_linked_objects']))
1484
				{
1485
					$invoice->linked_objects = array_merge($invoice->linked_objects, $_POST['other_linked_objects']);
1486
				}
1487
1488
				$result=$invoice->create($user);
1489
				if ($result <= 0)
1490
				{
1491
					$this->error=$invoice->error;
1492
					$this->errors=$invoice->errors;
1493
					$error++;
1494
				}
1495
				else
1496
				{
1497
					$this->invoice = $invoice;
1498
				}
1499
			}
1500
1501
			if (! $error)
1502
			{
1503
				// Add line to draft invoice
1504
				$idprodsubscription=0;
1505
				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;
1506
1507
				$vattouse=0;
1508
				if (isset($conf->global->ADHERENT_VAT_FOR_SUBSCRIPTIONS) && $conf->global->ADHERENT_VAT_FOR_SUBSCRIPTIONS == 'defaultforfoundationcountry')
1509
				{
1510
					$vattouse=get_default_tva($mysoc, $mysoc, $idprodsubscription);
1511
				}
1512
				//print xx".$vattouse." - ".$mysoc." - ".$customer;exit;
1513
				$result=$invoice->addline($label,0,1,$vattouse,0,0,$idprodsubscription,0,$datesubscription,'',0,0,'','TTC',$amount,1);
1514
				if ($result <= 0)
1515
				{
1516
					$this->error=$invoice->error;
1517
					$this->errors=$invoice->errors;
1518
					$error++;
1519
				}
1520
			}
1521
1522
			if (! $error)
1523
			{
1524
				// Validate invoice
1525
				$result=$invoice->validate($user);
1526
				if ($result <= 0)
1527
				{
1528
					$this->error=$invoice->error;
1529
					$this->errors=$invoice->errors;
1530
					$error++;
1531
				}
1532
			}
1533
1534
			if (! $error)
1535
			{
1536
				// TODO Link invoice with subscription ?
1537
			}
1538
1539
			// Add payment onto invoice
1540
			if (! $error && $option == 'bankviainvoice' && $accountid)
1541
			{
1542
				require_once DOL_DOCUMENT_ROOT.'/compta/paiement/class/paiement.class.php';
1543
				require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
1544
				require_once DOL_DOCUMENT_ROOT.'/core/lib/functions.lib.php';
1545
1546
				$amounts = array();
1547
				$amounts[$invoice->id] = price2num($amount);
1548
1549
				$paiement = new Paiement($this->db);
1550
				$paiement->datepaye     = $paymentdate;
1551
				$paiement->amounts      = $amounts;
1552
				$paiement->paiementid   = dol_getIdFromCode($this->db,$operation,'c_paiement','code','id',1);
1553
				$paiement->num_paiement = $num_chq;
1554
				$paiement->note         = $label;
1555
				$paiement->note_public  = $label;
1556
1557
				if (! $error)
1558
				{
1559
					// Create payment line for invoice
1560
					$paiement_id = $paiement->create($user);
1561
					if (! $paiement_id > 0)
1562
					{
1563
						$this->error=$paiement->error;
1564
						$this->errors=$paiement->errors;
1565
						$error++;
1566
					}
1567
				}
1568
1569
				if (! $error)
1570
				{
1571
					// Add transaction into bank account
1572
					$bank_line_id=$paiement->addPaymentToBank($user,'payment','(SubscriptionPayment)',$accountid,$emetteur_nom,$emetteur_banque);
1573
					if (! ($bank_line_id > 0))
1574
					{
1575
						$this->error=$paiement->error;
1576
						$this->errors=$paiement->errors;
1577
						$error++;
1578
					}
1579
				}
1580
1581
				if (! $error && !empty($bank_line_id))
1582
				{
1583
					// Update fk_bank into subscription table
1584
					$sql = 'UPDATE '.MAIN_DB_PREFIX.'subscription SET fk_bank='.$bank_line_id;
1585
					$sql.= ' WHERE rowid='.$subscriptionid;
1586
1587
					$result = $this->db->query($sql);
1588
					if (! $result)
1589
					{
1590
						$error++;
1591
					}
1592
				}
1593
1594
				if (! $error)
1595
				{
1596
					// Set invoice as paid
1597
					$invoice->set_paid($user);
1598
				}
1599
			}
1600
1601
			if (! $error)
1602
			{
1603
				// Define output language
1604
				$outputlangs = $langs;
1605
				$newlang = '';
1606
				$lang_id=GETPOST('lang_id');
1607
				if ($conf->global->MAIN_MULTILANGS && empty($newlang) && ! empty($lang_id))
1608
					$newlang = $lang_id;
1609
				if ($conf->global->MAIN_MULTILANGS && empty($newlang))
1610
					$newlang = $customer->default_lang;
1611
				if (! empty($newlang)) {
1612
					$outputlangs = new Translate("", $conf);
1613
					$outputlangs->setDefaultLang($newlang);
1614
				}
1615
				// Generate PDF (whatever is option MAIN_DISABLE_PDF_AUTOUPDATE) so we can include it into email
1616
				//if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE))
1617
1618
				$invoice->generateDocument($invoice->modelpdf, $outputlangs);
1619
			}
1620
		}
1621
1622
		if ($error)
1623
		{
1624
			return -1;
1625
		}
1626
		else
1627
		{
1628
			return 1;
1629
		}
1630
	}
1631
1632
1633
	/**
1634
	 *		Function that validate a member
1635
	 *
1636
	 *		@param	User	$user		user adherent qui valide
1637
	 *		@return	int					<0 if KO, 0 if nothing done, >0 if OK
1638
	 */
1639
	function validate($user)
1640
	{
1641
		global $langs,$conf;
1642
1643
		$error=0;
1644
		$now=dol_now();
1645
1646
		// Check parameters
1647
		if ($this->statut == 1)
1648
		{
1649
			dol_syslog(get_class($this)."::validate statut of member does not allow this", LOG_WARNING);
1650
			return 0;
1651
		}
1652
1653
		$this->db->begin();
1654
1655
		$sql = "UPDATE ".MAIN_DB_PREFIX."adherent SET";
1656
		$sql.= " statut = 1";
1657
		$sql.= ", datevalid = '".$this->db->idate($now)."'";
1658
		$sql.= ", fk_user_valid=".$user->id;
1659
		$sql.= " WHERE rowid = ".$this->id;
1660
1661
		dol_syslog(get_class($this)."::validate", LOG_DEBUG);
1662
		$result = $this->db->query($sql);
1663
		if ($result)
1664
		{
1665
			$this->statut=1;
1666
1667
			// Call trigger
1668
			$result=$this->call_trigger('MEMBER_VALIDATE',$user);
1669
			if ($result < 0) { $error++; $this->db->rollback(); return -1; }
1670
			// End call triggers
1671
1672
			$this->datevalid = $now;
1673
1674
			$this->db->commit();
1675
			return 1;
1676
		}
1677
		else
1678
		{
1679
			$this->error=$this->db->error();
1680
			$this->db->rollback();
1681
			return -1;
1682
		}
1683
	}
1684
1685
1686
	/**
1687
	 *		Fonction qui resilie un adherent
1688
	 *
1689
	 *		@param	User	$user		User making change
1690
	 *		@return	int					<0 if KO, >0 if OK
1691
	 */
1692
	function resiliate($user)
1693
	{
1694
		global $langs,$conf;
1695
1696
		$error=0;
1697
1698
		// Check paramaters
1699
		if ($this->statut == 0)
1700
		{
1701
			dol_syslog(get_class($this)."::resiliate statut of member does not allow this", LOG_WARNING);
1702
			return 0;
1703
		}
1704
1705
		$this->db->begin();
1706
1707
		$sql = "UPDATE ".MAIN_DB_PREFIX."adherent SET";
1708
		$sql.= " statut = 0";
1709
		$sql.= ", fk_user_valid=".$user->id;
1710
		$sql.= " WHERE rowid = ".$this->id;
1711
1712
		$result = $this->db->query($sql);
1713
		if ($result)
1714
		{
1715
			$this->statut=0;
1716
1717
			// Call trigger
1718
			$result=$this->call_trigger('MEMBER_RESILIATE',$user);
1719
			if ($result < 0) { $error++; $this->db->rollback(); return -1; }
1720
			// End call triggers
1721
1722
			$this->db->commit();
1723
			return 1;
1724
		}
1725
		else
1726
		{
1727
			$this->error=$this->db->error();
1728
			$this->db->rollback();
1729
			return -1;
1730
		}
1731
	}
1732
1733
1734
	/**
1735
	 *  Function to add member into external tools mailing-list, spip, etc.
1736
	 *
1737
	 *  @return		int		<0 if KO, >0 if OK
1738
	 */
1739
	function add_to_abo()
1740
	{
1741
		global $conf,$langs;
1742
1743
		include_once DOL_DOCUMENT_ROOT.'/mailmanspip/class/mailmanspip.class.php';
1744
		$mailmanspip=new MailmanSpip($this->db);
1745
1746
		$err=0;
1747
1748
		// mailman
1749
		if (! empty($conf->global->ADHERENT_USE_MAILMAN) && ! empty($conf->mailmanspip->enabled))
1750
		{
1751
			$result=$mailmanspip->add_to_mailman($this);
1752
1753
			if ($result < 0)
1754
			{
1755
				if (! empty($mailmanspip->error)) $this->errors[]=$mailmanspip->error;
1756
				$err+=1;
1757
			}
1758
			foreach ($mailmanspip->mladded_ko as $tmplist => $tmpemail)
1759
			{
1760
				$langs->load("errors");
1761
				$this->errors[]=$langs->trans("ErrorFailedToAddToMailmanList",$tmpemail,$tmplist);
1762
			}
1763
			foreach ($mailmanspip->mladded_ok as $tmplist => $tmpemail)
1764
			{
1765
				$langs->load("mailmanspip");
1766
				$this->mesgs[]=$langs->trans("SuccessToAddToMailmanList",$tmpemail,$tmplist);
1767
			}
1768
		}
1769
1770
		// spip
1771
		if (! empty($conf->global->ADHERENT_USE_SPIP) && ! empty($conf->mailmanspip->enabled))
1772
		{
1773
			$result=$mailmanspip->add_to_spip($this);
1774
			if ($result < 0)
1775
			{
1776
				$this->errors[]=$mailmanspip->error;
1777
				$err+=1;
1778
			}
1779
		}
1780
		if ($err)
1781
		{
1782
			return -$err;
1783
		}
1784
		else
1785
		{
1786
			return 1;
1787
		}
1788
	}
1789
1790
1791
	/**
1792
	 *  Function to delete a member from external tools like mailing-list, spip, etc.
1793
	 *
1794
	 *  @return     int     <0 if KO, >0 if OK
1795
	 */
1796
	function del_to_abo()
1797
	{
1798
		global $conf,$langs;
1799
1800
		include_once DOL_DOCUMENT_ROOT.'/mailmanspip/class/mailmanspip.class.php';
1801
		$mailmanspip=new MailmanSpip($this->db);
1802
1803
		$err=0;
1804
1805
		// mailman
1806
		if (! empty($conf->global->ADHERENT_USE_MAILMAN))
1807
		{
1808
			$result=$mailmanspip->del_to_mailman($this);
1809
			if ($result < 0)
1810
			{
1811
				if (! empty($mailmanspip->error)) $this->errors[]=$mailmanspip->error;
1812
				$err+=1;
1813
			}
1814
1815
			foreach ($mailmanspip->mlremoved_ko as $tmplist => $tmpemail)
1816
			{
1817
				$langs->load("errors");
1818
				$this->errors[]=$langs->trans("ErrorFailedToRemoveToMailmanList",$tmpemail,$tmplist);
1819
			}
1820
			foreach ($mailmanspip->mlremoved_ok as $tmplist => $tmpemail)
1821
			{
1822
				$langs->load("mailmanspip");
1823
				$this->mesgs[]=$langs->trans("SuccessToRemoveToMailmanList",$tmpemail,$tmplist);
1824
			}
1825
		}
1826
1827
		if ($conf->global->ADHERENT_USE_SPIP && ! empty($conf->mailmanspip->enabled))
1828
		{
1829
			$result=$mailmanspip->del_to_spip($this);
1830
			if ($result < 0)
1831
			{
1832
				$this->errors[]=$mailmanspip->error;
1833
				$err+=1;
1834
			}
1835
		}
1836
		if ($err)
1837
		{
1838
			// error
1839
			return -$err;
1840
		}
1841
		else
1842
		{
1843
			return 1;
1844
		}
1845
	}
1846
1847
1848
	/**
1849
	 *    Return civility label of a member
1850
	 *
1851
	 *    @return   string              	Translated name of civility (translated with transnoentitiesnoconv)
1852
	 */
1853
	function getCivilityLabel()
1854
	{
1855
		global $langs;
1856
		$langs->load("dict");
1857
1858
		$code=(empty($this->civility_id)?'':$this->civility_id);
1859
		if (empty($code)) return '';
1860
		return $langs->getLabelFromKey($this->db, "Civility".$code, "c_civility", "code", "label", $code);
1861
	}
1862
1863
	/**
1864
	 *  Return clicable name (with picto eventually)
1865
	 *
1866
	 *	@param	int		$withpictoimg				0=No picto, 1=Include picto into link, 2=Only picto, -1=Include photo into link, -2=Only picto photo, -3=Only photo very small)
1867
	 *	@param	int		$maxlen						length max label
1868
	 *	@param	string	$option						Page for link ('card', 'category', 'subscription', ...)
1869
	 *  @param  string  $mode           			''=Show firstname+lastname as label (using default order), 'firstname'=Show only firstname, 'login'=Show login, 'ref'=Show ref
1870
	 *  @param  string  $morecss        			Add more css on link
1871
	 *  @param  int     $save_lastsearch_value    	-1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking
1872
	 *	@return	string								Chaine avec URL
1873
	 */
1874
	function getNomUrl($withpictoimg=0, $maxlen=0, $option='card', $mode='', $morecss='', $save_lastsearch_value=-1)
1875
	{
1876
		global $conf, $langs;
1877
1878
		if (! empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER) && $withpictoimg) $withpictoimg=0;
1879
1880
		$notooltip=0;
1881
1882
		$result=''; $label='';
1883
		$link=''; $linkstart=''; $linkend='';
1884
1885
		if (! empty($this->photo))
1886
		{
1887
			$label.= '<div class="photointooltip">';
1888
			$label.= Form::showphoto('memberphoto', $this, 80, 0, 0, 'photowithmargin photologintooltip', 'small', 0, 1);
1889
			$label.= '</div><div style="clear: both;"></div>';
1890
		}
1891
1892
		$label.= '<div class="centpercent">';
1893
		$label.= '<u>' . $langs->trans("Member") . '</u>';
1894
		if (! empty($this->ref))
1895
			$label.= '<br><b>' . $langs->trans('Ref') . ':</b> ' . $this->ref;
1896
		if (! empty($this->firstname) || ! empty($this->lastname))
1897
			$label.= '<br><b>' . $langs->trans('Name') . ':</b> ' . $this->getFullName($langs);
1898
		if (! empty($this->societe))
1899
			$label.= '<br><b>' . $langs->trans('Company') . ':</b> ' . $this->societe;
1900
		$label.='</div>';
1901
1902
		$url = DOL_URL_ROOT.'/adherents/card.php?rowid='.$this->id;
1903
		if ($option == 'subscription')
1904
		{
1905
			$url = DOL_URL_ROOT.'/adherents/subscription.php?rowid='.$this->id;
1906
		}
1907
1908
		if ($option != 'nolink')
1909
		{
1910
			// Add param to save lastsearch_values or not
1911
			$add_save_lastsearch_values=($save_lastsearch_value == 1 ? 1 : 0);
1912
			if ($save_lastsearch_value == -1 && preg_match('/list\.php/',$_SERVER["PHP_SELF"])) $add_save_lastsearch_values=1;
1913
			if ($add_save_lastsearch_values) $url.='&save_lastsearch_values=1';
1914
		}
1915
1916
		$link = '<a href="'.$url.'"';
1917
		$linkclose="";
1918
		if (empty($notooltip))
1919
		{
1920
			if (! empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER))
1921
			{
1922
				$langs->load("users");
1923
				$label=$langs->trans("ShowUser");
1924
				$linkclose.=' alt="'.dol_escape_htmltag($label, 1).'"';
1925
			}
1926
			$linkclose.= ' title="'.dol_escape_htmltag($label, 1).'"';
1927
			$linkclose.= ' class="classfortooltip'.($morecss?' '.$morecss:'').'"';
1928
		}
1929
1930
		$link.=$linkclose.'>';
1931
		$linkend='</a>';
1932
1933
		//if ($withpictoimg == -1) $result.='<div class="nowrap">';
1934
		$result.=$link;
1935
		if ($withpictoimg)
1936
		{
1937
			$paddafterimage='';
1938
			if (abs($withpictoimg) == 1) $paddafterimage='style="margin-right: 3px;"';
1939
			// Only picto
1940
			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>';
1941
			// Picto must be a photo
1942
			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>';
1943
			$result.=$picto;
1944
		}
1945
		if ($withpictoimg > -2 && $withpictoimg != 2)
1946
		{
1947
			if (empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) $result.='<div class="inline-block nopadding valignmiddle'.((! isset($this->statut) || $this->statut)?'':' strikefordisabled').($morecss?' usertext'.$morecss:'').'">';
1948
			if ($mode == 'login') $result.=dol_trunc($this->login, $maxlen);
1949
			elseif ($mode == 'ref') $result.=$this->id;
1950
			else $result.=$this->getFullName($langs,'',($mode == 'firstname' ? 2 : -1),$maxlen);
1951
			if (empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) $result.='</div>';
1952
		}
1953
		$result.=$linkend;
1954
		//if ($withpictoimg == -1) $result.='</div>';
1955
1956
		return $result;
1957
	}
1958
1959
	/**
1960
	 *  Retourne le libelle du statut d'un adherent (brouillon, valide, resilie)
1961
	 *
1962
	 *  @param	int		$mode       0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
1963
	 *  @return string				Label
1964
	 */
1965
	function getLibStatut($mode=0)
1966
	{
1967
		return $this->LibStatut($this->statut,$this->need_subscription,$this->datefin,$mode);
1968
	}
1969
1970
	/**
1971
	 *  Renvoi le libelle d'un statut donne
1972
	 *
1973
	 *  @param	int			$statut      			Id statut
1974
	 *	@param	int			$need_subscription		1 if member type need subscription, 0 otherwise
1975
	 *	@param	int     	$date_end_subscription	Date fin adhesion
1976
	 *  @param  int			$mode        			0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
1977
	 *  @return string      						Label
1978
	 */
1979
	function LibStatut($statut,$need_subscription,$date_end_subscription,$mode=0)
1980
	{
1981
		global $langs;
1982
		$langs->load("members");
1983
		if ($mode == 0)
1984
		{
1985
			if ($statut == -1) return $langs->trans("MemberStatusDraft");
1986
			if ($statut >= 1)
1987
			{
1988
				if (! $date_end_subscription)            return $langs->trans("MemberStatusActive");
1989
				elseif ($date_end_subscription < time()) return $langs->trans("MemberStatusActiveLate");
1990
				else                                     return $langs->trans("MemberStatusPaid");
1991
			}
1992
			if ($statut == 0)  return $langs->trans("MemberStatusResiliated");
1993
		}
1994
		if ($mode == 1)
1995
		{
1996
			if ($statut == -1) return $langs->trans("MemberStatusDraftShort");
1997
			if ($statut >= 1)
1998
			{
1999
				if (! $date_end_subscription)            return $langs->trans("MemberStatusActiveShort");
2000
				elseif ($date_end_subscription < time()) return $langs->trans("MemberStatusActiveLateShort");
2001
				else                                     return $langs->trans("MemberStatusPaidShort");
2002
			}
2003
			if ($statut == 0)  return $langs->trans("MemberStatusResiliatedShort");
2004
		}
2005
		if ($mode == 2)
2006
		{
2007
			if ($statut == -1) return img_picto($langs->trans('MemberStatusDraft'),'statut0').' '.$langs->trans("MemberStatusDraftShort");
2008
			if ($statut >= 1)
2009
			{
2010
				if (! $date_end_subscription)            return img_picto($langs->trans('MemberStatusActive'),'statut1').' '.$langs->trans("MemberStatusActiveShort");
2011
				elseif ($date_end_subscription < time()) return img_picto($langs->trans('MemberStatusActiveLate'),'statut3').' '.$langs->trans("MemberStatusActiveLateShort");
2012
				else                                     return img_picto($langs->trans('MemberStatusPaid'),'statut4').' '.$langs->trans("MemberStatusPaidShort");
2013
			}
2014
			if ($statut == 0)  return img_picto($langs->trans('MemberStatusResiliated'),'statut5').' '.$langs->trans("MemberStatusResiliatedShort");
2015
		}
2016
		if ($mode == 3)
2017
		{
2018
			if ($statut == -1) return img_picto($langs->trans('MemberStatusDraft'),'statut0');
2019
			if ($statut >= 1)
2020
			{
2021
				if (! $date_end_subscription)            return img_picto($langs->trans('MemberStatusActive'),'statut1');
2022
				elseif ($date_end_subscription < time()) return img_picto($langs->trans('MemberStatusActiveLate'),'statut3');
2023
				else                                     return img_picto($langs->trans('MemberStatusPaid'),'statut4');
2024
			}
2025
			if ($statut == 0)  return img_picto($langs->trans('MemberStatusResiliated'),'statut5');
2026
		}
2027
		if ($mode == 4)
2028
		{
2029
			if ($statut == -1) return img_picto($langs->trans('MemberStatusDraft'),'statut0').' '.$langs->trans("MemberStatusDraft");
2030
			if ($statut >= 1)
2031
			{
2032
				if (! $date_end_subscription)            return img_picto($langs->trans('MemberStatusActive'),'statut1').' '.$langs->trans("MemberStatusActive");
2033
				elseif ($date_end_subscription < time()) return img_picto($langs->trans('MemberStatusActiveLate'),'statut3').' '.$langs->trans("MemberStatusActiveLate");
2034
				else                                     return img_picto($langs->trans('MemberStatusPaid'),'statut4').' '.$langs->trans("MemberStatusPaid");
2035
			}
2036
			if ($statut == 0)  return img_picto($langs->trans('MemberStatusResiliated'),'statut5').' '.$langs->trans("MemberStatusResiliated");
2037
		}
2038
		if ($mode == 5)
2039
		{
2040
			if ($statut == -1) return $langs->trans("MemberStatusDraft").' '.img_picto($langs->trans('MemberStatusDraft'),'statut0');
2041
			if ($statut >= 1)
2042
			{
2043
				if (! $date_end_subscription)            return '<span class="hideonsmartphone">'.$langs->trans("MemberStatusActiveShort").' </span>'.img_picto($langs->trans('MemberStatusActive'),'statut1');
2044
				elseif ($date_end_subscription < time()) return '<span class="hideonsmartphone">'.$langs->trans("MemberStatusActiveLateShort").' </span>'.img_picto($langs->trans('MemberStatusActiveLate'),'statut3');
2045
				else                                     return '<span class="hideonsmartphone">'.$langs->trans("MemberStatusPaidShort").' </span>'.img_picto($langs->trans('MemberStatusPaid'),'statut4');
2046
			}
2047
			if ($statut == 0)  return '<span class="hideonsmartphone">'.$langs->trans("MemberStatusResiliated").' </span>'.img_picto($langs->trans('MemberStatusResiliated'),'statut5');
2048
		}
2049
		if ($mode == 6)
2050
		{
2051
			if ($statut == -1) return $langs->trans("MemberStatusDraft").' '.img_picto($langs->trans('MemberStatusDraft'),'statut0');
2052
			if ($statut >= 1)
2053
			{
2054
				if (! $date_end_subscription)            return '<span class="hideonsmartphone">'.$langs->trans("MemberStatusActive").' </span>'.img_picto($langs->trans('MemberStatusActive'),'statut1');
2055
				elseif ($date_end_subscription < time()) return '<span class="hideonsmartphone">'.$langs->trans("MemberStatusActiveLate").' </span>'.img_picto($langs->trans('MemberStatusActiveLate'),'statut3');
2056
				else                                     return '<span class="hideonsmartphone">'.$langs->trans("MemberStatusPaid").' </span>'.img_picto($langs->trans('MemberStatusPaid'),'statut4');
2057
			}
2058
			if ($statut == 0)  return '<span class="hideonsmartphone">'.$langs->trans("MemberStatusResiliated").' </span>'.img_picto($langs->trans('MemberStatusResiliated'),'statut5');
2059
		}
2060
	}
2061
2062
2063
	/**
2064
	 *      Charge indicateurs this->nb de tableau de bord
2065
	 *
2066
	 *      @return     int         <0 if KO, >0 if OK
2067
	 */
2068
	function load_state_board()
2069
	{
2070
		global $conf;
2071
2072
		$this->nb=array();
2073
2074
		$sql = "SELECT count(a.rowid) as nb";
2075
		$sql.= " FROM ".MAIN_DB_PREFIX."adherent as a";
2076
		$sql.= " WHERE a.statut > 0";
2077
		$sql.= " AND a.entity IN (".getEntity('adherent').")";
2078
2079
		$resql=$this->db->query($sql);
2080
		if ($resql)
2081
		{
2082
			while ($obj=$this->db->fetch_object($resql))
2083
			{
2084
				$this->nb["members"]=$obj->nb;
2085
			}
2086
			$this->db->free($resql);
2087
			return 1;
2088
		}
2089
		else
2090
		{
2091
			dol_print_error($this->db);
2092
			$this->error=$this->db->error();
2093
			return -1;
2094
		}
2095
2096
	}
2097
2098
	/**
2099
	 *      Load indicators for dashboard (this->nbtodo and this->nbtodolate)
2100
	 *
2101
	 *      @param	User	$user   		Objet user
2102
	 *      @return WorkboardResponse|int 	<0 if KO, WorkboardResponse if OK
2103
	 */
2104
	function load_board($user)
2105
	{
2106
		global $conf, $langs;
2107
2108
		if ($user->societe_id) return -1;   // protection pour eviter appel par utilisateur externe
2109
2110
		$now=dol_now();
2111
2112
		$sql = "SELECT a.rowid, a.datefin, a.statut";
2113
		$sql.= " FROM ".MAIN_DB_PREFIX."adherent as a";
2114
		$sql.= " WHERE a.statut = 1";
2115
		$sql.= " AND a.entity IN (".getEntity('adherent').")";
2116
		$sql.= " AND (a.datefin IS NULL or a.datefin < '".$this->db->idate($now)."')";
2117
2118
		$resql=$this->db->query($sql);
2119
		if ($resql)
2120
		{
2121
			$langs->load("members");
2122
2123
			$response = new WorkboardResponse();
2124
			$response->warning_delay=$conf->adherent->subscription->warning_delay/60/60/24;
2125
			$response->label=$langs->trans("MembersWithSubscriptionToReceive");
2126
			$response->url=DOL_URL_ROOT.'/adherents/list.php?mainmenu=members&amp;statut=1&amp;filter=outofdate';
2127
			$response->img=img_object('',"user");
2128
2129
			$adherentstatic = new Adherent($this->db);
2130
2131
			while ($obj=$this->db->fetch_object($resql))
2132
			{
2133
				$response->nbtodo++;
2134
2135
				$adherentstatic->datefin = $this->db->jdate($obj->datefin);
2136
				$adherentstatic->statut = $obj->statut;
2137
2138
				if ($adherentstatic->hasDelay()) {
2139
					$response->nbtodolate++;
2140
				}
2141
			}
2142
2143
			return $response;
2144
		}
2145
		else
2146
		{
2147
			dol_print_error($this->db);
2148
			$this->error=$this->db->error();
2149
			return -1;
2150
		}
2151
	}
2152
2153
2154
	/**
2155
	 *  Create a document onto disk according to template module.
2156
	 *
2157
	 *  @param	    string		$modele			Force template to use ('' to not force)
2158
	 *  @param		Translate	$outputlangs	objet lang a utiliser pour traduction
2159
	 *  @param      int			$hidedetails    Hide details of lines
2160
	 *  @param      int			$hidedesc       Hide description
2161
	 *  @param      int			$hideref        Hide ref
2162
	 *  @return     int         				0 if KO, 1 if OK
2163
	 */
2164
	public function generateDocument($modele, $outputlangs, $hidedetails=0, $hidedesc=0, $hideref=0)
2165
	{
2166
		global $conf,$langs;
2167
2168
		$langs->load("orders");
2169
2170
		if (! dol_strlen($modele)) {
2171
2172
			$modele = 'standard';
2173
2174
			if ($this->modelpdf) {
2175
				$modele = $this->modelpdf;
2176
			} elseif (! empty($conf->global->ADHERENT_ADDON_PDF)) {
2177
				$modele = $conf->global->ADHERENT_ADDON_PDF;
2178
			}
2179
		}
2180
2181
		$modelpath = "core/modules/member/doc/";
2182
2183
		return $this->commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref);
2184
	}
2185
2186
2187
	/**
2188
	 *  Initialise an instance with random values.
2189
	 *  Used to build previews or test instances.
2190
	 *	id must be 0 if object instance is a specimen.
2191
	 *
2192
	 *  @return	void
2193
	 */
2194
	function initAsSpecimen()
2195
	{
2196
		global $user,$langs;
2197
2198
		// Initialise parametres
2199
		$this->id=0;
2200
		$this->specimen=1;
2201
		$this->civility_id = 0;
2202
		$this->lastname = 'DOLIBARR';
2203
		$this->firstname = 'SPECIMEN';
2204
		$this->login='dolibspec';
2205
		$this->pass='dolibspec';
2206
		$this->societe = 'Societe ABC';
2207
		$this->address = '61 jump street';
2208
		$this->zip = '75000';
2209
		$this->town = 'Paris';
2210
		$this->country_id = 1;
2211
		$this->country_code = 'FR';
2212
		$this->country = 'France';
2213
		$this->morphy = 1;
2214
		$this->email = '[email protected]';
2215
		$this->skype = 'tom.hanson';
2216
		$this->phone        = '0999999999';
2217
		$this->phone_perso  = '0999999998';
2218
		$this->phone_mobile = '0999999997';
2219
		$this->note_private='No comment';
2220
		$this->birth=time();
2221
		$this->photo='';
2222
		$this->public=1;
2223
		$this->statut=0;
2224
2225
		$this->datefin=time();
2226
		$this->datevalid=time();
2227
2228
		$this->typeid=1;				// Id type adherent
2229
		$this->type='Type adherent';	// Libelle type adherent
2230
		$this->need_subscription=0;
2231
2232
		$this->first_subscription_date=time();
2233
		$this->first_subscription_amount=10;
2234
		$this->last_subscription_date=time();
2235
		$this->last_subscription_amount=10;
2236
	}
2237
2238
2239
	/**
2240
	 *	Retourne chaine DN complete dans l'annuaire LDAP pour l'objet
2241
	 *
2242
	 *	@param	array	$info		Info array loaded by _load_ldap_info
2243
	 *	@param	int		$mode		0=Return full DN (uid=qqq,ou=xxx,dc=aaa,dc=bbb)
2244
	 *								1=Return DN without key inside (ou=xxx,dc=aaa,dc=bbb)
2245
	 *								2=Return key only (uid=qqq)
2246
	 *	@return	string				DN
2247
	 */
2248
	function _load_ldap_dn($info,$mode=0)
2249
	{
2250
		global $conf;
2251
		$dn='';
2252
		if ($mode==0) $dn=$conf->global->LDAP_KEY_MEMBERS."=".$info[$conf->global->LDAP_KEY_MEMBERS].",".$conf->global->LDAP_MEMBER_DN;
2253
		if ($mode==1) $dn=$conf->global->LDAP_MEMBER_DN;
2254
		if ($mode==2) $dn=$conf->global->LDAP_KEY_MEMBERS."=".$info[$conf->global->LDAP_KEY_MEMBERS];
2255
		return $dn;
2256
	}
2257
2258
2259
	/**
2260
	 *	Initialise tableau info (tableau des attributs LDAP)
2261
	 *
2262
	 *	@return		array		Tableau info des attributs
2263
	 */
2264
	function _load_ldap_info()
2265
	{
2266
		global $conf,$langs;
2267
2268
		$info=array();
2269
		$keymodified=false;
2270
2271
		// Object classes
2272
		$info["objectclass"]=explode(',',$conf->global->LDAP_MEMBER_OBJECT_CLASS);
2273
2274
		$this->fullname=$this->getFullName($langs);
2275
2276
		// For avoid ldap error when firstname and lastname are empty
2277
		if ($this->morphy == 'mor' && (empty($this->fullname) || $this->fullname == $this->societe)) {
2278
			$this->fullname = $this->societe;
2279
			$this->lastname = $this->societe;
2280
		}
2281
2282
		// Possible LDAP KEY (constname => varname)
2283
		$ldapkey = array(
2284
			'LDAP_MEMBER_FIELD_FULLNAME'		=> 'fullname',
2285
			'LDAP_MEMBER_FIELD_NAME'			=> 'lastname',
2286
			'LDAP_MEMBER_FIELD_LOGIN'		=> 'login',
2287
			'LDAP_MEMBER_FIELD_LOGIN_SAMBA'	=> 'login',
2288
			'LDAP_MEMBER_FIELD_MAIL'			=> 'email'
2289
		);
2290
2291
		// Member
2292
		foreach ($ldapkey as $constname => $varname)
2293
		{
2294
			if (! empty($this->$varname) && ! empty($conf->global->$constname))
2295
			{
2296
				$info[$conf->global->$constname] = $this->$varname;
2297
2298
				// Check if it is the LDAP key and if its value has been changed
2299
				if (! empty($conf->global->LDAP_KEY_MEMBERS) && $conf->global->LDAP_KEY_MEMBERS == $conf->global->$constname)
2300
				{
2301
					if (! empty($this->oldcopy) && $this->$varname != $this->oldcopy->$varname) $keymodified=true; // For check if LDAP key has been modified
2302
				}
2303
			}
2304
		}
2305
		if ($this->firstname && ! empty($conf->global->LDAP_MEMBER_FIELD_FIRSTNAME))			$info[$conf->global->LDAP_MEMBER_FIELD_FIRSTNAME] = $this->firstname;
2306
		if ($this->poste && ! empty($conf->global->LDAP_MEMBER_FIELD_TITLE))					$info[$conf->global->LDAP_MEMBER_FIELD_TITLE] = $this->poste;
2307
		if ($this->societe && ! empty($conf->global->LDAP_MEMBER_FIELD_COMPANY))				$info[$conf->global->LDAP_MEMBER_FIELD_COMPANY] = $this->societe;
2308
		if ($this->address && ! empty($conf->global->LDAP_MEMBER_FIELD_ADDRESS))				$info[$conf->global->LDAP_MEMBER_FIELD_ADDRESS] = $this->address;
2309
		if ($this->zip && ! empty($conf->global->LDAP_MEMBER_FIELD_ZIP))						$info[$conf->global->LDAP_MEMBER_FIELD_ZIP] = $this->zip;
2310
		if ($this->town && ! empty($conf->global->LDAP_MEMBER_FIELD_TOWN))						$info[$conf->global->LDAP_MEMBER_FIELD_TOWN] = $this->town;
2311
		if ($this->country_code && ! empty($conf->global->LDAP_MEMBER_FIELD_COUNTRY))			$info[$conf->global->LDAP_MEMBER_FIELD_COUNTRY] = $this->country_code;
2312
		if ($this->skype && ! empty($conf->global->LDAP_MEMBER_FIELD_SKYPE))					$info[$conf->global->LDAP_MEMBER_FIELD_SKYPE] = $this->skype;
2313
		if ($this->phone && ! empty($conf->global->LDAP_MEMBER_FIELD_PHONE))					$info[$conf->global->LDAP_MEMBER_FIELD_PHONE] = $this->phone;
2314
		if ($this->phone_perso && ! empty($conf->global->LDAP_MEMBER_FIELD_PHONE_PERSO))		$info[$conf->global->LDAP_MEMBER_FIELD_PHONE_PERSO] = $this->phone_perso;
2315
		if ($this->phone_mobile && ! empty($conf->global->LDAP_MEMBER_FIELD_MOBILE))			$info[$conf->global->LDAP_MEMBER_FIELD_MOBILE] = $this->phone_mobile;
2316
		if ($this->fax && ! empty($conf->global->LDAP_MEMBER_FIELD_FAX))						$info[$conf->global->LDAP_MEMBER_FIELD_FAX] = $this->fax;
2317
		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);
2318
		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);
2319
		if ($this->birth && ! empty($conf->global->LDAP_MEMBER_FIELD_BIRTHDATE))				$info[$conf->global->LDAP_MEMBER_FIELD_BIRTHDATE] = dol_print_date($this->birth,'dayhourldap');
2320
		if (isset($this->statut) && ! empty($conf->global->LDAP_FIELD_MEMBER_STATUS))			$info[$conf->global->LDAP_FIELD_MEMBER_STATUS] = $this->statut;
2321
		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');
2322
2323
		// When password is modified
2324
		if (! empty($this->pass))
2325
		{
2326
			if (! empty($conf->global->LDAP_MEMBER_FIELD_PASSWORD))				$info[$conf->global->LDAP_MEMBER_FIELD_PASSWORD] = $this->pass;	// this->pass = mot de passe non crypte
2327
			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)
2328
		}
2329
		// Set LDAP password if possible
2330
		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
2331
		{
2332
			if (! empty($conf->global->DATABASE_PWD_ENCRYPTED))
2333
			{
2334
				// Just for the default MD5 !
2335
				if (empty($conf->global->MAIN_SECURITY_HASH_ALGO))
2336
				{
2337
					if ($this->pass_indatabase_crypted && ! empty($conf->global->LDAP_MEMBER_FIELD_PASSWORD_CRYPTED))	{
2338
						// Create OpenLDAP MD5 password from Dolibarr MD5 password
2339
						// Note: This suppose that "pass_indatabase_crypted" is a md5 (guaranted by the previous test if "(empty($conf->global->MAIN_SECURITY_HASH_ALGO))"
2340
						$info[$conf->global->LDAP_MEMBER_FIELD_PASSWORD_CRYPTED] = '{md5}'.base64_encode(hex2bin($this->pass_indatabase_crypted));
2341
					}
2342
				}
2343
			}
2344
			// Use $this->pass_indatabase value if exists
2345
			else if (! empty($this->pass_indatabase))
2346
			{
2347
				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
2348
				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
2349
			}
2350
		}
2351
2352
		// Subscriptions
2353
		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');
2354
		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;
2355
		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');
2356
		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;
2357
2358
		return $info;
2359
	}
2360
2361
2362
	/**
2363
	 *      Charge les informations d'ordre info dans l'objet adherent
2364
	 *
2365
	 *      @param  int		$id       Id of member to load
2366
	 *      @return	void
2367
	 */
2368
	function info($id)
2369
	{
2370
		$sql = 'SELECT a.rowid, a.datec as datec,';
2371
		$sql.= ' a.datevalid as datev,';
2372
		$sql.= ' a.tms as datem,';
2373
		$sql.= ' a.fk_user_author, a.fk_user_valid, a.fk_user_mod';
2374
		$sql.= ' FROM '.MAIN_DB_PREFIX.'adherent as a';
2375
		$sql.= ' WHERE a.rowid = '.$id;
2376
2377
		dol_syslog(get_class($this)."::info", LOG_DEBUG);
2378
		$result=$this->db->query($sql);
2379
		if ($result)
2380
		{
2381
			if ($this->db->num_rows($result))
2382
			{
2383
				$obj = $this->db->fetch_object($result);
2384
				$this->id = $obj->rowid;
2385
				if ($obj->fk_user_author)
2386
				{
2387
					$cuser = new User($this->db);
2388
					$cuser->fetch($obj->fk_user_author);
2389
					$this->user_creation   = $cuser;
2390
				}
2391
2392
				if ($obj->fk_user_valid)
2393
				{
2394
					$vuser = new User($this->db);
2395
					$vuser->fetch($obj->fk_user_valid);
2396
					$this->user_validation = $vuser;
2397
				}
2398
2399
				if ($obj->fk_user_mod)
2400
				{
2401
					$muser = new User($this->db);
2402
					$muser->fetch($obj->fk_user_mod);
2403
					$this->user_modification = $muser;
2404
				}
2405
2406
				$this->date_creation     = $this->db->jdate($obj->datec);
2407
				$this->date_validation   = $this->db->jdate($obj->datev);
2408
				$this->date_modification = $this->db->jdate($obj->datem);
2409
			}
2410
2411
			$this->db->free($result);
2412
2413
		}
2414
		else
2415
		{
2416
			dol_print_error($this->db);
2417
		}
2418
	}
2419
2420
	/**
2421
	 *  Return number of mass Emailing received by this member with its email
2422
	 *
2423
	 *  @return       int     Number of EMailings
2424
	 */
2425
	function getNbOfEMailings()
2426
	{
2427
		$sql = "SELECT count(mc.email) as nb";
2428
		$sql.= " FROM ".MAIN_DB_PREFIX."mailing_cibles as mc";
2429
		$sql.= " WHERE mc.email = '".$this->db->escape($this->email)."'";
2430
		$sql.= " AND mc.statut NOT IN (-1,0)";      // -1 erreur, 0 non envoye, 1 envoye avec succes
2431
2432
		$resql=$this->db->query($sql);
2433
		if ($resql)
2434
		{
2435
			$obj = $this->db->fetch_object($resql);
2436
			$nb=$obj->nb;
2437
2438
			$this->db->free($resql);
2439
			return $nb;
2440
		}
2441
		else
2442
		{
2443
			$this->error=$this->db->error();
2444
			return -1;
2445
		}
2446
	}
2447
2448
	/**
2449
	 * Sets object to supplied categories.
2450
	 *
2451
	 * Deletes object from existing categories not supplied.
2452
	 * Adds it to non existing supplied categories.
2453
	 * Existing categories are left untouch.
2454
	 *
2455
	 * @param int[]|int $categories Category or categories IDs
2456
	 */
2457
	public function setCategories($categories)
2458
	{
2459
		// Handle single category
2460
		if (!is_array($categories)) {
2461
			$categories = array($categories);
2462
		}
2463
2464
		// Get current categories
2465
		require_once DOL_DOCUMENT_ROOT . '/categories/class/categorie.class.php';
2466
		$c = new Categorie($this->db);
2467
		$existing = $c->containing($this->id, Categorie::TYPE_MEMBER, 'id');
2468
2469
		// Diff
2470
		if (is_array($existing)) {
2471
			$to_del = array_diff($existing, $categories);
2472
			$to_add = array_diff($categories, $existing);
2473
		} else {
2474
			$to_del = array(); // Nothing to delete
2475
			$to_add = $categories;
2476
		}
2477
2478
		// Process
2479
		foreach ($to_del as $del) {
2480
			if ($c->fetch($del) > 0) {
2481
				$c->del_type($this, 'member');
2482
			}
2483
		}
2484
		foreach ($to_add as $add) {
2485
			if ($c->fetch($add) > 0) {
2486
				$c->add_type($this, 'member');
2487
			}
2488
		}
2489
2490
		return;
2491
	}
2492
2493
	/**
2494
	 * Function used to replace a thirdparty id with another one.
2495
	 *
2496
	 * @param DoliDB 	$db 			Database handler
2497
	 * @param int 		$origin_id 		Old thirdparty id
2498
	 * @param int 		$dest_id 		New thirdparty id
2499
	 * @return bool
2500
	 */
2501
	public static function replaceThirdparty($db, $origin_id, $dest_id)
2502
	{
2503
		$tables = array(
2504
			'adherent'
2505
		);
2506
2507
		return CommonObject::commonReplaceThirdparty($db, $origin_id, $dest_id, $tables);
2508
	}
2509
2510
	/**
2511
	 * Return if a member is late (subscription late) or not
2512
	 *
2513
	 * @return boolean     True if late, False if not late
2514
	 */
2515
	public function hasDelay()
2516
	{
2517
		global $conf;
2518
2519
		//Only valid members
2520
		if ($this->statut <= 0) return false;
2521
		if (! $this->datefin) return false;
2522
2523
		$now = dol_now();
2524
2525
		return $this->datefin < ($now - $conf->adherent->subscription->warning_delay);
2526
	}
2527
2528
2529
2530
	/**
2531
	 * Send reminders by emails before subscription end
2532
	 * CAN BE A CRON TASK
2533
	 *
2534
	 * @param	int			$daysbeforeend		Nb of days before end of subscription (negative number = after subscription)
2535
	 * @return	int								0 if OK, <>0 if KO (this function is used also by cron so only 0 is OK)
2536
	 */
2537
	public function sendReminderForExpiredSubscription($daysbeforeend=10)
2538
	{
2539
		global $conf, $langs, $mysoc, $user;
2540
2541
		$error = 0;
2542
		$this->output = '';
2543
		$this->error='';
2544
2545
		$blockingerrormsg = '';
2546
2547
		/*if (empty($conf->global->MEMBER_REMINDER_EMAIL))
2548
		{
2549
			$langs->load("agenda");
2550
			$this->output = $langs->trans('EventRemindersByEmailNotEnabled', $langs->transnoentitiesnoconv("Adherent"));
2551
			return 0;
2552
		}*/
2553
2554
		$now = dol_now();
2555
2556
		dol_syslog(__METHOD__, LOG_DEBUG);
2557
2558
		$tmp=dol_getdate($now);
2559
		$datetosearchfor = dol_time_plus_duree(dol_mktime(0, 0, 0, $tmp['mon'], $tmp['mday'], $tmp['year']), -1 * $daysbeforeend, 'd');
2560
2561
		$sql = 'SELECT rowid FROM '.MAIN_DB_PREFIX.'adherent';
2562
		$sql.= " WHERE datefin = '".$this->db->idate($datetosearchfor)."'";
2563
2564
		$resql = $this->db->query($sql);
2565
		if ($resql)
2566
		{
2567
			$num_rows = $this->db->num_rows($resql);
2568
2569
			include_once DOL_DOCUMENT_ROOT.'/core/class/html.formmail.class.php';
2570
			$adherent = new Adherent($this->db);
2571
			$formmail = new FormMail($this->db);
2572
2573
			$i=0;
2574
			$nbok = 0;
2575
			$nbko = 0;
2576
			while ($i < $num_rows)
2577
			{
2578
				$obj = $this->db->fetch_object($resql);
2579
2580
				$adherent->fetch($obj->rowid);
2581
2582
				if (empty($adherent->email))
2583
				{
2584
					$nbko++;
2585
				}
2586
				else
2587
				{
2588
					$adherent->fetch_thirdparty();
2589
2590
					// Send reminder email
2591
					$outputlangs = new Translate('', $conf);
2592
					$outputlangs->setDefaultLang(empty($adherent->thirdparty->default_lang) ? $mysoc->default_lang : $adherent->thirdparty->default_lang);
2593
					$outputlangs->loadLangs(array("main", "members"));
2594
2595
					$arraydefaultmessage=null;
2596
					$labeltouse = $conf->global->ADHERENT_EMAIL_TEMPLATE_REMIND_EXPIRATION;
2597
2598
					if (! empty($labeltouse)) $arraydefaultmessage=$formmail->getEMailTemplate($this->db, 'member', $user, $outputlangs, 0, 1, $labeltouse);
2599
2600
					if (! empty($labeltouse) && is_object($arraydefaultmessage) && $arraydefaultmessage->id > 0)
2601
					{
2602
						$substitutionarray=getCommonSubstitutionArray($outputlangs, 0, null, $adherent);
2603
						//if (is_array($adherent->thirdparty)) $substitutionarraycomp = ...
2604
						complete_substitutions_array($substitutionarray, $outputlangs, $adherent);
2605
2606
						$subject = make_substitutions($arraydefaultmessage->topic, $substitutionarray, $outputlangs);
2607
						$msg     = make_substitutions($arraydefaultmessage->content, $substitutionarray, $outputlangs);
2608
						$from = $conf->global->ADHERENT_MAIL_FROM;
2609
						$to = $adherent->email;
2610
2611
						include_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
2612
						$cmail = new CMailFile($subject, $to, $from, $msg, array(), array(), array(), '', '', 0, 1);
2613
						$result = $cmail->sendfile();
2614
						if (! $result)
2615
						{
2616
							$error++;
2617
							$this->error = $cmail->error;
2618
							$this->errors += $cmail->errors;
2619
							$nbko++;
2620
						}
2621
						else
2622
						{
2623
							$nbok++;
2624
						}
2625
					}
2626
					else
2627
					{
2628
						$blockingerrormsg="Can't find email template, defined into member module setup, to use for reminding";
2629
						$nbko++;
2630
						break;
2631
					}
2632
				}
2633
2634
				$i++;
2635
			}
2636
		}
2637
		else
2638
		{
2639
			$this->error = $this->db->lasterror();
2640
			return 1;
2641
		}
2642
2643
		if ($blockingerrormsg)
2644
		{
2645
			$this->error = $blockingerrormsg;
2646
			return 1;
2647
		}
2648
		else
2649
		{
2650
			$this->output = 'Found '.($nbok + $nbko).' members to send reminder to.';
2651
			$this->output.= ' Send email successfuly to '.$nbok.' members';
2652
			if ($nbko) $this->output.= ' - Canceled for '.$nbko.' member (no email or email sending error)';
2653
		}
2654
2655
		return 0;
2656
	}
2657
2658
}
2659