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