dolibarr 22.0.5
societeaccount.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2007-2017 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2014-2016 Juanjo Menent <jmenent@2byte.es>
4 * Copyright (C) 2015 Florian Henry <florian.henry@open-concept.pro>
5 * Copyright (C) 2015 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
6 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
7 * Copyright (C) 2024-2025 MDW <mdeweerd@users.noreply.github.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 */
22
29// Put here all includes required by your class file
30require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
31//require_once DOL_DOCUMENT_ROOT . '/societe/class/societe.class.php';
32//require_once DOL_DOCUMENT_ROOT . '/product/class/product.class.php';
33
34
39{
43 public $element = 'societeaccount';
44
48 public $table_element = 'societe_account';
49
53 public $picto = 'lock';
54
55
95 // BEGIN MODULEBUILDER PROPERTIES
100 public $fields = array(
101 'rowid' => array('type' => 'integer', 'label' => 'TechnicalID', 'visible' => -2, 'enabled' => 1, 'position' => 1, 'notnull' => 1, 'index' => 1, 'comment' => 'Id',),
102 'entity' => array('type' => 'integer', 'label' => 'Entity', 'visible' => 0, 'enabled' => 1, 'position' => 5, 'default' => '1'),
103 'login' => array('type' => 'varchar(64)', 'label' => 'Login', 'visible' => 1, 'enabled' => 1, 'notnull' => 1, 'position' => 10, 'showoncombobox' => 1, 'autofocusoncreate' => 1, 'csslist' => 'tdoverflowmax150'),
104 'pass_encoding' => array('type' => 'varchar(24)', 'label' => 'PassEncoding', 'visible' => 0, 'enabled' => 1, 'position' => 30),
105 'pass_crypted' => array('type' => 'password', 'label' => 'Password', 'visible' => -1, 'enabled' => 1, 'position' => 31, 'notnull' => 1),
106 'pass_temp' => array('type' => 'varchar(128)', 'label' => 'Temp', 'visible' => 0, 'enabled' => 0, 'position' => 32, 'notnull' => -1,),
107 'fk_soc' => array('type' => 'integer:Societe:societe/class/societe.class.php', 'label' => 'ThirdParty', 'visible' => -6, 'enabled' => 1, 'position' => 40, 'notnull' => -1, 'index' => 1, 'picto' => 'company', 'css' => 'maxwidth300 widthcentpercentminusxx'),
108 'site' => array('type' => 'varchar(128)', 'label' => 'WebsiteTypeLabel', 'visible' => 0, 'enabled' => 0, 'position' => 41, 'notnull' => 1, 'default' => '', 'help' => 'Name of the website or service if this is account on an external website or service', 'csslist' => 'tdoverflowmax150', 'arrayofkeyval' => array(/* For static analysis, filled in constructor */)),
109 'fk_website' => array('type' => 'integer:Website:website/class/website.class.php', 'label' => 'WebSite', 'visible' => 0, 'enabled' => 0, 'position' => 42, 'notnull' => -1, 'index' => 1, 'picto' => 'website', 'css' => 'maxwidth300 widthcentpercentminusxx', 'csslist' => 'tdoverflowmax150'),
110 'site_account' => array('type' => 'varchar(128)', 'label' => 'ExternalSiteAccount', 'visible' => 0, 'enabled' => 1, 'position' => 44, 'help' => 'A key to identify the account on external web site if this is an account on an external website'),
111 'key_account' => array('type' => 'varchar(128)', 'label' => 'KeyAccount', 'visible' => 0, 'enabled' => 1, 'position' => 48, 'notnull' => 0, 'index' => 1, 'searchall' => 1, 'comment' => 'The id of third party in the external web site (for site_account if site_account defined)',),
112 'date_last_login' => array('type' => 'datetime', 'label' => 'LastConnexion', 'visible' => 2, 'enabled' => 1, 'position' => 50, 'notnull' => 0, 'css' => 'nowraponall'),
113 'date_previous_login' => array('type' => 'datetime', 'label' => 'PreviousConnexion', 'visible' => 2, 'enabled' => 1, 'position' => 51, 'notnull' => 0, 'css' => 'nowraponall'),
114 'date_last_reset_password' => array('type' => 'datetime', 'label' => 'PreviousResetPassword', 'visible' => 0, 'enabled' => 1, 'position' => 53, 'notnull' => 0, 'css' => 'nowraponall'),
115 //'note_public' => array('type'=>'html', 'label'=>'NotePublic', 'visible'=>-1, 'enabled'=>1, 'position'=>45, 'notnull'=>-1,),
116 'note_private' => array('type' => 'html', 'label' => 'NotePrivate', 'visible' => -1, 'enabled' => 1, 'position' => 46, 'notnull' => -1,),
117 'date_creation' => array('type' => 'datetime', 'label' => 'DateCreation', 'visible' => -2, 'enabled' => 1, 'position' => 500, 'notnull' => 1,),
118 'tms' => array('type' => 'timestamp', 'label' => 'DateModification', 'visible' => -2, 'enabled' => 1, 'position' => 500, 'notnull' => 1,),
119 'fk_user_creat' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'UserAuthor', 'visible' => -2, 'enabled' => 1, 'position' => 500, 'notnull' => 1,),
120 'fk_user_modif' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'UserModif', 'visible' => -2, 'enabled' => 1, 'position' => 500, 'notnull' => -1,),
121 'import_key' => array('type' => 'varchar(14)', 'label' => 'ImportId', 'visible' => -2, 'enabled' => 1, 'position' => 1000, 'notnull' => -1, 'index' => 1,),
122 'status' => array('type' => 'integer', 'label' => 'Status', 'visible' => 1, 'enabled' => 1, 'position' => 1000, 'notnull' => 1, 'index' => 1, 'default' => '1', 'arrayofkeyval' => array(1 => 'Active', 0 => 'Disabled')),
123 );
124
128 public $rowid;
129
133 public $entity;
134
138 public $key_account;
142 public $login;
146 public $pass_encoding;
150 public $pass_crypted;
154 public $pass;
158 public $pass_temp;
159
163 public $fk_soc;
164
168 public $site;
169
173 public $fk_website;
174
178 public $site_account;
179
183 public $date_last_login;
184
188 public $date_last_reset_password;
192 public $date_previous_login;
196 public $note_private;
197
201 public $fk_user_creat;
202
206 public $fk_user_modif;
207
211 public $import_key;
212
216 public $status;
217
218 // END MODULEBUILDER PROPERTIES
219
220
221 const STATUS_ENABLED = 1;
222 const STATUS_DISABLED = 0;
223
224
230 public function __construct(DoliDB $db)
231 {
232 global $langs;
233
234 $this->db = $db;
235
236 $this->ismultientitymanaged = 0;
237
238 if (!getDolGlobalString('MAIN_SHOW_TECHNICAL_ID')) {
239 $this->fields['rowid']['visible'] = 0;
240 }
241
242 // add site type list and set visible
243 $site_type_list = array();
244
245 if (isModEnabled('webportal')) {
246 $this->fields['site']['visible'] = 1;
247 $this->fields['site']['enabled'] = 1;
248 $site_type_list['dolibarr_portal'] = $langs->trans('WebsiteTypeDolibarrPortal');
249 }
250
251 if (isModEnabled('website')) {
252 $this->fields['fk_website']['visible'] = 1;
253 $this->fields['fk_website']['enabled'] = 1;
254 $this->fields['site']['visible'] = 1;
255 $this->fields['site']['enabled'] = 1;
256 $site_type_list['dolibarr_website'] = $langs->trans('WebsiteTypeDolibarrWebsite');
257 }
258
259 $this->fields['site']['arrayofkeyval'] = $site_type_list;
260 }
261
269 public function create(User $user, $notrigger = 0)
270 {
271 global $langs;
272
273 if ($this->site == 'dolibarr_website') {
274 if ((int) $this->fk_website <= 0) {
275 $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Website"));
276 return -1;
277 }
278 }
279
280 if ($this->site == 'dolibarr_portal') {
281 // Check that entry does not already exists
282 $sql = "SELECT sa.rowid, sa.entity";
283 $sql .= " FROM ".MAIN_DB_PREFIX."societe_account as sa";
284 $sql .= " WHERE sa.login = '".$this->db->escape($this->login)."'";
285 $sql .= " AND sa.entity IN (".getEntity('societe').")";
286 $sql .= " AND sa.site = 'dolibarr_portal'";
287 $sql .= " ORDER BY sa.rowid DESC"; // To always get the same order
288
289 $result = $this->db->query($sql);
290 if ($result) {
291 if ($this->db->num_rows($result)) {
292 $this->error = 'ErrorLoginAlreadyExists';
293 return -2;
294 }
295 }
296 }
297
298 return $this->createCommon($user, $notrigger);
299 }
300
308 public function createFromClone(User $user, $fromid)
309 {
310 $error = 0;
311
312 dol_syslog(__METHOD__, LOG_DEBUG);
313
314 $object = new self($this->db);
315
316 $this->db->begin();
317
318 // Load source object
319 $object->fetchCommon($fromid);
320 // Reset some properties
321 unset($object->id);
322 unset($object->fk_user_creat);
323 unset($object->import_key);
324
325 // Clear fields
326 $object->ref = "copy_of_".$object->ref;
327 // $object->title = $langs->trans("CopyOf")." ".$object->title;
328
329 // Create clone
330 $object->context['createfromclone'] = 'createfromclone';
331 $result = $object->createCommon($user);
332 if ($result < 0) {
333 $error++;
334 $this->error = $object->error;
335 $this->errors = $object->errors;
336 }
337
338 unset($object->context['createfromclone']);
339
340 // End
341 if (!$error) {
342 $this->db->commit();
343 return $object;
344 } else {
345 $this->db->rollback();
346 return -1;
347 }
348 }
349
357 public function fetch($id, $ref = null)
358 {
359 $result = $this->fetchCommon($id, $ref);
360 if ($result > 0 && !empty($this->table_element_line)) {
361 $this->fetchLines();
362 }
363 return $result;
364 }
365
371 public function fetchLines()
372 {
373 $this->lines = array();
374
375 // Load lines with object societeAccountLine
376
377 return count($this->lines) ? 1 : 0;
378 }
379
391 public function getCustomerAccount($id, $site, $status = 0, $site_account = '', $fk_website = 0)
392 {
393 $sql = "SELECT sa.key_account as key_account, sa.entity";
394 $sql .= " FROM ".MAIN_DB_PREFIX."societe_account as sa";
395 $sql .= " WHERE sa.fk_soc = ".((int) $id);
396 $sql .= " AND sa.entity IN (".getEntity('societe').")";
397 $sql .= " AND sa.site = '".$this->db->escape($site)."'";
398 if ($fk_website > 0) {
399 $sql .= " AND sa.fk_website = ".((int) $fk_website);
400 }
401 if ($status >= 0) {
402 $sql .= " AND sa.status = ".((int) $status);
403 }
404 $sql .= " AND sa.key_account IS NOT NULL AND sa.key_account <> ''";
405 $sql .= " AND (sa.site_account = '' OR sa.site_account IS NULL OR sa.site_account = '".$this->db->escape($site_account)."')";
406 $sql .= " ORDER BY sa.site_account DESC, sa.rowid DESC"; // To get the entry with a site_account defined in priority
407
408 dol_syslog(get_class($this)."::getCustomerAccount Try to find the first system customer id for ".$site." of thirdparty id=".$id." (example: cus_.... for stripe)", LOG_DEBUG);
409 $result = $this->db->query($sql);
410 if ($result) {
411 if ($this->db->num_rows($result)) {
412 $obj = $this->db->fetch_object($result);
413 $key = $obj->key_account;
414 } else {
415 $key = '';
416 }
417 } else {
418 $key = '';
419 }
420
421 return $key;
422 }
423
433 public function getThirdPartyID($id, $site, $status = 0)
434 {
435 $socid = 0;
436
437 $sql = "SELECT sa.fk_soc as fk_soc, sa.key_account, sa.entity";
438 $sql .= " FROM ".MAIN_DB_PREFIX."societe_account as sa";
439 $sql .= " WHERE sa.key_account = '".$this->db->escape($id)."'";
440 $sql .= " AND sa.entity IN (".getEntity('societe').")";
441 $sql .= " AND sa.site = '".$this->db->escape($site)."' AND sa.status = ".((int) $status);
442 $sql .= " AND sa.fk_soc > 0";
443 $sql .= " ORDER BY sa.site_account DESC, sa.rowid DESC"; // To get the entry with a site_account defined in priority
444
445 dol_syslog(get_class($this)."::getCustomerAccount Try to find the first thirdparty id for ".$site." for external id=".$id, LOG_DEBUG);
446 $result = $this->db->query($sql);
447 if ($result) {
448 if ($this->db->num_rows($result)) {
449 $obj = $this->db->fetch_object($result);
450 $socid = $obj->fk_soc;
451 }
452 }
453
454 return $socid;
455 }
456
464 public function update(User $user, $notrigger = 0)
465 {
466 return $this->updateCommon($user, $notrigger);
467 }
468
476 public function delete(User $user, $notrigger = 0)
477 {
478 return $this->deleteCommon($user, $notrigger);
479 }
480
488 public function getTooltipContentArray($params)
489 {
490 global $langs;
491
492 $langs->loadLangs(['companies, commercial', 'website']);
493
494 $datas = [];
495 $option = $params['option'] ?? '';
496
497 $datas['picto'] = '<u>'.$langs->trans("WebsiteAccount").'</u>';
498 $datas['login'] = '<br><b>'.$langs->trans('Login').':</b> '.$this->login;
499
500 return $datas;
501 }
502
513 public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
514 {
515 global $conf, $langs;
516 global $dolibarr_main_authentication, $dolibarr_main_demo;
517 global $menumanager;
518
519 if (!empty($conf->dol_no_mouse_hover)) {
520 $notooltip = 1; // Force disable tooltips
521 }
522
523 $result = '';
524
525 $this->ref = $this->login;
526
527 $label = '<u>'.$langs->trans("WebsiteAccount").'</u>';
528 $label .= '<br>';
529 $label .= '<b>'.$langs->trans('Login').':</b> '.$this->ref;
530 //$label.= '<b>' . $langs->trans('WebSite') . ':</b> ' . $this->ref;
531
532 $url = DOL_URL_ROOT.'/website/websiteaccount_card.php?id='.$this->id;
533
534 if ($option != 'nolink') {
535 // Add param to save lastsearch_values or not
536 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
537 if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
538 $add_save_lastsearch_values = 1;
539 }
540 if ($add_save_lastsearch_values) {
541 $url .= '&save_lastsearch_values=1';
542 }
543 }
544
545 $linkclose = '';
546 $classfortooltip = 'classfortooltip';
547 $dataparams = '';
548 if (getDolGlobalInt('MAIN_ENABLE_AJAX_TOOLTIP')) {
549 $params = [
550 'id' => $this->id,
551 'objecttype' => $this->element,
552 'option' => $option,
553 ];
554 $classfortooltip = 'classforajaxtooltip';
555 $dataparams = ' data-params="'.dol_escape_htmltag(json_encode($params)).'"';
556 $label = '';
557 }
558
559 if (empty($notooltip)) {
560 if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
561 $label = $langs->trans("WebsiteAccount");
562 $linkclose .= ' alt="'.dolPrintHTMLForAttribute($label).'"';
563 }
564 $linkclose .= ($label ? ' title="'.dolPrintHTMLForAttribute($label).'"' : ' title="tocomplete"');
565 $linkclose .= $dataparams.' class="'.$classfortooltip.($morecss ? ' '.$morecss : '').'"';
566 } else {
567 $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
568 }
569
570 $linkstart = '<a href="'.$url.'"';
571 $linkstart .= $linkclose.'>';
572 $linkend = '</a>';
573
574 $result .= $linkstart;
575 if ($withpicto) {
576 $result .= img_object(($notooltip ? '' : $label), ($this->picto ? $this->picto : 'generic'), ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : $dataparams.' class="'.(($withpicto != 2) ? 'paddingright ' : '').$classfortooltip.'"'), 0, 0, $notooltip ? 0 : 1);
577 }
578 if ($withpicto != 2) {
579 $result .= $this->ref;
580 }
581 $result .= $linkend;
582
583 return $result;
584 }
585
592 public function getLibStatut($mode = 0)
593 {
594 return $this->LibStatut($this->status, $mode);
595 }
596
597 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
605 public function LibStatut($status, $mode = 0)
606 {
607 // phpcs:enable
608 if (is_null($status)) {
609 return '';
610 }
611
612 if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
613 global $langs;
614 //$langs->load("mymodule@mymodule");
615 $this->labelStatus[self::STATUS_ENABLED] = $langs->transnoentitiesnoconv('Enabled');
616 $this->labelStatus[self::STATUS_DISABLED] = $langs->transnoentitiesnoconv('Disabled');
617 $this->labelStatusShort[self::STATUS_ENABLED] = $langs->transnoentitiesnoconv('Enabled');
618 $this->labelStatusShort[self::STATUS_DISABLED] = $langs->transnoentitiesnoconv('Disabled');
619 }
620
621 $statusType = 'status4';
622 //if ($status == self::STATUS_VALIDATED) $statusType = 'status1';
623 if ($status == self::STATUS_DISABLED) {
624 $statusType = 'status6';
625 }
626
627 return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
628 }
629
636 public function info($id)
637 {
638 $sql = 'SELECT rowid, date_creation as datec, tms as datem,';
639 $sql .= ' fk_user_creat, fk_user_modif';
640 $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
641 $sql .= ' WHERE t.rowid = '.((int) $id);
642 $result = $this->db->query($sql);
643 if ($result) {
644 if ($this->db->num_rows($result)) {
645 $obj = $this->db->fetch_object($result);
646 $this->id = $obj->rowid;
647
648 $this->user_creation_id = $obj->fk_user_creat;
649 $this->user_modification_id = $obj->fk_user_modif;
650 $this->date_creation = $this->db->jdate($obj->datec);
651 $this->date_modification = empty($obj->datem) ? '' : $this->db->jdate($obj->datem);
652 }
653
654 $this->db->free($result);
655 } else {
656 dol_print_error($this->db);
657 }
658 }
659
666 public function initAsSpecimen()
667 {
668 return $this->initAsSpecimenCommon();
669 }
670}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:67
$object ref
Definition info.php:90
Parent class of all other business classes (invoices, contracts, proposals, orders,...
createCommon(User $user, $notrigger=0)
Create object in the database.
updateCommon(User $user, $notrigger=0)
Update object into database.
initAsSpecimenCommon()
Initialise object with example values Id must be 0 if object instance is a specimen.
fetchCommon($id, $ref=null, $morewhere='', $noextrafields=0)
Load object in memory from the database.
deleteCommon(User $user, $notrigger=0, $forcechilddeletion=0)
Delete object in database.
Class to manage Dolibarr database access.
Class for SocieteAccount.
initAsSpecimen()
Initialise object with example values Id must be 0 if object instance is a specimen.
fetch($id, $ref=null)
Load object in memory from the database.
info($id)
Charge les information d'ordre info dans l'objet commande.
getLibStatut($mode=0)
Return the label of a given status.
getNomUrl($withpicto=0, $option='', $notooltip=0, $morecss='', $save_lastsearch_value=-1)
Return a link to the object card (with optionally the picto)
getTooltipContentArray($params)
getTooltipContentArray
getCustomerAccount($id, $site, $status=0, $site_account='', $fk_website=0)
Try to find the external customer id of a third party for another site/system.
fetchLines()
Load object lines in memory from the database.
createFromClone(User $user, $fromid)
Clone an object into another one.
__construct(DoliDB $db)
Constructor.
update(User $user, $notrigger=0)
Update object into database.
LibStatut($status, $mode=0)
Return the status.
getThirdPartyID($id, $site, $status=0)
Try to find the thirdparty id from an another site/system external id.
create(User $user, $notrigger=0)
Create object into database.
Class to manage Dolibarr users.
print $langs trans("Ref").' m titre as m m statut as status
Or an array listing all the potential status of the object: array: int of the status => translated la...
Definition index.php:171
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $allowothertags=array())
Show a picto called object_picto (generic function)
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
dolGetStatus($statusLabel='', $statusLabelShort='', $html='', $statusType='status0', $displayMode=0, $url='', $params=array())
Output the badge of a status.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79
global $dolibarr_main_demo
Definition index.php:57