dolibarr  20.0.0-alpha
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 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
30 require_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 
38 {
42  public $element = 'societeaccount';
43 
47  public $table_element = 'societe_account';
48 
53  public $ismultientitymanaged = 0;
54 
58  public $picto = 'lock';
59 
60 
78  // BEGIN MODULEBUILDER PROPERTIES
83  public $fields = array(
84  'rowid' => array('type' => 'integer', 'label' => 'TechnicalID', 'visible' => -2, 'enabled' => 1, 'position' => 1, 'notnull' => 1, 'index' => 1, 'comment' => 'Id',),
85  'entity' => array('type' => 'integer', 'label' => 'Entity', 'visible' => 0, 'enabled' => 1, 'position' => 5, 'default' => 1),
86  'login' => array('type' => 'varchar(64)', 'label' => 'Login', 'visible' => 1, 'enabled' => 1, 'notnull' => 1, 'position' => 10, 'showoncombobox' => 1),
87  'pass_encoding' => array('type' => 'varchar(24)', 'label' => 'PassEncoding', 'visible' => 0, 'enabled' => 1, 'position' => 30),
88  'pass_crypted' => array('type' => 'password', 'label' => 'Password', 'visible' => -1, 'enabled' => 1, 'position' => 31, 'notnull' => 1),
89  'pass_temp' => array('type' => 'varchar(128)', 'label' => 'Temp', 'visible' => 0, 'enabled' => 0, 'position' => 32, 'notnull' => -1,),
90  'fk_soc' => array('type' => 'integer:Societe:societe/class/societe.class.php', 'label' => 'ThirdParty', 'visible' => 1, 'enabled' => 1, 'position' => 40, 'notnull' => -1, 'index' => 1, 'picto' => 'company', 'css' => 'maxwidth300 widthcentpercentminusxx'),
91  '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', 'arrayofkeyval' => array( /* For static analysis, filled in constructor */)),
92  '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'),
93  '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'),
94  '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)',),
95  'date_last_login' => array('type' => 'datetime', 'label' => 'LastConnexion', 'visible' => 2, 'enabled' => 1, 'position' => 50, 'notnull' => 0,),
96  'date_previous_login' => array('type' => 'datetime', 'label' => 'PreviousConnexion', 'visible' => 2, 'enabled' => 1, 'position' => 51, 'notnull' => 0,),
97  //'note_public' => array('type'=>'html', 'label'=>'NotePublic', 'visible'=>-1, 'enabled'=>1, 'position'=>45, 'notnull'=>-1,),
98  'note_private' => array('type' => 'html', 'label' => 'NotePrivate', 'visible' => -1, 'enabled' => 1, 'position' => 46, 'notnull' => -1,),
99  'date_creation' => array('type' => 'datetime', 'label' => 'DateCreation', 'visible' => -2, 'enabled' => 1, 'position' => 500, 'notnull' => 1,),
100  'tms' => array('type' => 'timestamp', 'label' => 'DateModification', 'visible' => -2, 'enabled' => 1, 'position' => 500, 'notnull' => 1,),
101  'fk_user_creat' => array('type' => 'integer', 'label' => 'UserAuthor', 'visible' => -2, 'enabled' => 1, 'position' => 500, 'notnull' => 1,),
102  'fk_user_modif' => array('type' => 'integer', 'label' => 'UserModif', 'visible' => -2, 'enabled' => 1, 'position' => 500, 'notnull' => -1,),
103  'import_key' => array('type' => 'varchar(14)', 'label' => 'ImportId', 'visible' => -2, 'enabled' => 1, 'position' => 1000, 'notnull' => -1, 'index' => 1,),
104  'status' => array('type' => 'integer', 'label' => 'Status', 'visible' => 1, 'enabled' => 1, 'position' => 1000, 'notnull' => 1, 'index' => 1, 'default' => 1, 'arrayofkeyval' => array('1' => 'Active', '0' => 'Disabled')),
105  );
106 
110  public $rowid;
111 
115  public $entity;
116 
117  public $key_account;
118  public $login;
119  public $pass_encoding;
120  public $pass_crypted;
121  public $pass_temp;
122 
126  public $fk_soc;
127 
128  public $site;
129  public $site_account;
130 
134  public $date_last_login;
135 
136 
137  public $date_previous_login;
138  public $note_private;
139 
143  public $date_creation;
144 
148  public $fk_user_creat;
149 
153  public $fk_user_modif;
154 
155  public $import_key;
156 
160  public $status;
161 
162  // END MODULEBUILDER PROPERTIES
163 
164 
165  const STATUS_ENABLED = 1;
166  const STATUS_DISABLED = 0;
167 
168 
174  public function __construct(DoliDB $db)
175  {
176  global $langs;
177 
178  $this->db = $db;
179 
180  if (!getDolGlobalString('MAIN_SHOW_TECHNICAL_ID')) {
181  $this->fields['rowid']['visible'] = 0;
182  }
183 
184  // add site type list and set visible
185  $site_type_list = array();
186  if (isModEnabled('website')) {
187  $this->fields['fk_website']['visible'] = 1;
188  $this->fields['fk_website']['enabled'] = 1;
189  $this->fields['site']['visible'] = 1;
190  $this->fields['site']['enabled'] = 1;
191  $site_type_list['dolibarr_website'] = $langs->trans('WebsiteTypeDolibarrWebsite');
192  }
193  if (isModEnabled('webportal')) {
194  $this->fields['site']['visible'] = 1;
195  $this->fields['site']['enabled'] = 1;
196  $site_type_list['dolibarr_portal'] = $langs->trans('WebsiteTypeDolibarrPortal');
197  }
198  $this->fields['site']['arrayofkeyval'] = $site_type_list;
199  }
200 
208  public function create(User $user, $notrigger = 0)
209  {
210  return $this->createCommon($user, $notrigger);
211  }
212 
220  public function createFromClone(User $user, $fromid)
221  {
222  global $hookmanager, $langs;
223  $error = 0;
224 
225  dol_syslog(__METHOD__, LOG_DEBUG);
226 
227  $object = new self($this->db);
228 
229  $this->db->begin();
230 
231  // Load source object
232  $object->fetchCommon($fromid);
233  // Reset some properties
234  unset($object->id);
235  unset($object->fk_user_creat);
236  unset($object->import_key);
237 
238  // Clear fields
239  $object->ref = "copy_of_".$object->ref;
240  // $object->title = $langs->trans("CopyOf")." ".$object->title;
241 
242  // Create clone
243  $object->context['createfromclone'] = 'createfromclone';
244  $result = $object->createCommon($user);
245  if ($result < 0) {
246  $error++;
247  $this->error = $object->error;
248  $this->errors = $object->errors;
249  }
250 
251  unset($object->context['createfromclone']);
252 
253  // End
254  if (!$error) {
255  $this->db->commit();
256  return $object;
257  } else {
258  $this->db->rollback();
259  return -1;
260  }
261  }
262 
270  public function fetch($id, $ref = null)
271  {
272  $result = $this->fetchCommon($id, $ref);
273  if ($result > 0 && !empty($this->table_element_line)) {
274  $this->fetchLines();
275  }
276  return $result;
277  }
278 
284  public function fetchLines()
285  {
286  $this->lines = array();
287 
288  // Load lines with object societeAccountLine
289 
290  return count($this->lines) ? 1 : 0;
291  }
292 
303  public function getCustomerAccount($id, $site, $status = 0, $site_account = '')
304  {
305  $sql = "SELECT sa.key_account as key_account, sa.entity";
306  $sql .= " FROM ".MAIN_DB_PREFIX."societe_account as sa";
307  $sql .= " WHERE sa.fk_soc = ".((int) $id);
308  $sql .= " AND sa.entity IN (".getEntity('societe').")";
309  $sql .= " AND sa.site = '".$this->db->escape($site)."' AND sa.status = ".((int) $status);
310  $sql .= " AND sa.key_account IS NOT NULL AND sa.key_account <> ''";
311  $sql .= " AND (sa.site_account = '' OR sa.site_account IS NULL OR sa.site_account = '".$this->db->escape($site_account)."')";
312  $sql .= " ORDER BY sa.site_account DESC"; // To get the entry with a site_account defined in priority
313 
314  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);
315  $result = $this->db->query($sql);
316  if ($result) {
317  if ($this->db->num_rows($result)) {
318  $obj = $this->db->fetch_object($result);
319  $key = $obj->key_account;
320  } else {
321  $key = '';
322  }
323  } else {
324  $key = '';
325  }
326 
327  return $key;
328  }
329 
339  public function getThirdPartyID($id, $site, $status = 0)
340  {
341  $socid = 0;
342 
343  $sql = "SELECT sa.fk_soc as fk_soc, sa.key_account, sa.entity";
344  $sql .= " FROM ".MAIN_DB_PREFIX."societe_account as sa";
345  $sql .= " WHERE sa.key_account = '".$this->db->escape($id)."'";
346  $sql .= " AND sa.entity IN (".getEntity('societe').")";
347  $sql .= " AND sa.site = '".$this->db->escape($site)."' AND sa.status = ".((int) $status);
348  $sql .= " AND sa.fk_soc > 0";
349 
350  dol_syslog(get_class($this)."::getCustomerAccount Try to find the first thirdparty id for ".$site." for external id=".$id, LOG_DEBUG);
351  $result = $this->db->query($sql);
352  if ($result) {
353  if ($this->db->num_rows($result)) {
354  $obj = $this->db->fetch_object($result);
355  $socid = $obj->fk_soc;
356  }
357  }
358 
359  return $socid;
360  }
361 
369  public function update(User $user, $notrigger = 0)
370  {
371  return $this->updateCommon($user, $notrigger);
372  }
373 
381  public function delete(User $user, $notrigger = 0)
382  {
383  return $this->deleteCommon($user, $notrigger);
384  }
385 
392  public function getTooltipContentArray($params)
393  {
394  global $conf, $langs, $user;
395 
396  $langs->loadLangs(['companies, commercial']);
397 
398  $datas = [];
399  $option = $params['option'] ?? '';
400 
401  $datas['picto'] = '<u>'.$langs->trans("WebsiteAccount").'</u>';
402  $datas['login'] = '<br><b>'.$langs->trans('Login').':</b> '.$this->login;
403 
404  return $datas;
405  }
406 
417  public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
418  {
419  global $db, $conf, $langs;
420  global $dolibarr_main_authentication, $dolibarr_main_demo;
421  global $menumanager;
422 
423  if (!empty($conf->dol_no_mouse_hover)) {
424  $notooltip = 1; // Force disable tooltips
425  }
426 
427  $result = '';
428 
429  $this->ref = $this->login;
430 
431  $label = '<u>'.$langs->trans("WebsiteAccount").'</u>';
432  $label .= '<br>';
433  $label .= '<b>'.$langs->trans('Login').':</b> '.$this->ref;
434  //$label.= '<b>' . $langs->trans('WebSite') . ':</b> ' . $this->ref;
435 
436  $url = DOL_URL_ROOT.'/website/websiteaccount_card.php?id='.$this->id;
437 
438  if ($option != 'nolink') {
439  // Add param to save lastsearch_values or not
440  $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
441  if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
442  $add_save_lastsearch_values = 1;
443  }
444  if ($add_save_lastsearch_values) {
445  $url .= '&save_lastsearch_values=1';
446  }
447  }
448 
449  $linkclose = '';
450  $classfortooltip = 'classfortooltip';
451  $dataparams = '';
452  if (getDolGlobalInt('MAIN_ENABLE_AJAX_TOOLTIP')) {
453  $params = [
454  'id' => $this->id,
455  'objecttype' => $this->element,
456  'option' => $option,
457  ];
458  $classfortooltip = 'classforajaxtooltip';
459  $dataparams = ' data-params="'.dol_escape_htmltag(json_encode($params)).'"';
460  $label = '';
461  }
462 
463  if (empty($notooltip)) {
464  if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
465  $label = $langs->trans("WebsiteAccount");
466  $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
467  }
468  $linkclose .= ($label ? ' title="'.dol_escape_htmltag($label, 1).'"' : ' title="tocomplete"');
469  $linkclose .= $dataparams.' class="'.$classfortooltip.($morecss ? ' '.$morecss : '').'"';
470  } else {
471  $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
472  }
473 
474  $linkstart = '<a href="'.$url.'"';
475  $linkstart .= $linkclose.'>';
476  $linkend = '</a>';
477 
478  $result .= $linkstart;
479  if ($withpicto) {
480  $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);
481  }
482  if ($withpicto != 2) {
483  $result .= $this->ref;
484  }
485  $result .= $linkend;
486 
487  return $result;
488  }
489 
496  public function getLibStatut($mode = 0)
497  {
498  return $this->LibStatut($this->status, $mode);
499  }
500 
501  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
509  public function LibStatut($status, $mode = 0)
510  {
511  // phpcs:enable
512  if (is_null($status)) {
513  return '';
514  }
515 
516  if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
517  global $langs;
518  //$langs->load("mymodule@mymodule");
519  $this->labelStatus[self::STATUS_ENABLED] = $langs->transnoentitiesnoconv('Enabled');
520  $this->labelStatus[self::STATUS_DISABLED] = $langs->transnoentitiesnoconv('Disabled');
521  $this->labelStatusShort[self::STATUS_ENABLED] = $langs->transnoentitiesnoconv('Enabled');
522  $this->labelStatusShort[self::STATUS_DISABLED] = $langs->transnoentitiesnoconv('Disabled');
523  }
524 
525  $statusType = 'status4';
526  //if ($status == self::STATUS_VALIDATED) $statusType = 'status1';
527  if ($status == self::STATUS_DISABLED) {
528  $statusType = 'status6';
529  }
530 
531  return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
532  }
533 
540  public function info($id)
541  {
542  $sql = 'SELECT rowid, date_creation as datec, tms as datem,';
543  $sql .= ' fk_user_creat, fk_user_modif';
544  $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
545  $sql .= ' WHERE t.rowid = '.((int) $id);
546  $result = $this->db->query($sql);
547  if ($result) {
548  if ($this->db->num_rows($result)) {
549  $obj = $this->db->fetch_object($result);
550  $this->id = $obj->rowid;
551 
552  $this->user_creation_id = $obj->fk_user_creat;
553  $this->user_modification_id = $obj->fk_user_modif;
554  $this->date_creation = $this->db->jdate($obj->datec);
555  $this->date_modification = empty($obj->datem) ? '' : $this->db->jdate($obj->datem);
556  }
557 
558  $this->db->free($result);
559  } else {
560  dol_print_error($this->db);
561  }
562  }
563 
570  public function initAsSpecimen()
571  {
572  return $this->initAsSpecimenCommon();
573  }
574 }
if($user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition: card.php:58
print $langs trans("AuditedSecurityEvents").'</strong >< span class="opacitymedium"></span >< br > status
Or an array listing all the potential status of the object: array: int of the status => translated la...
Definition: security.php:604
$object ref
Definition: info.php:79
Parent class of all other business classes (invoices, contracts, proposals, orders,...
createCommon(User $user, $notrigger=0)
Create object into 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
fetchLines()
Load object lines in memory from the database.
createFromClone(User $user, $fromid)
Clone and object into another one.
__construct(DoliDB $db)
Constructor.
update(User $user, $notrigger=0)
Update object into database.
getCustomerAccount($id, $site, $status=0, $site_account='')
Try to find the external customer id of a thirdparty for another site/system.
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.
Definition: user.class.php:50
if(isModEnabled('invoice') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&!getDolGlobalString('MAIN_USE_NEW_SUPPLIERMOD') && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') && $user->hasRight('don', 'lire')) if(isModEnabled('tax') && $user->hasRight('tax', 'charges', 'lire')) if(isModEnabled('invoice') &&isModEnabled('order') && $user->hasRight("commande", "lire") &&!getDolGlobalString('WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER')) $sql
Social contributions to pay.
Definition: index.php:744
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0)
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 dolibarr global constant string value.
isModEnabled($module)
Is Dolibarr module enabled.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.