dolibarr  13.0.2
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  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <https://www.gnu.org/licenses/>.
19  */
20 
27 // Put here all includes required by your class file
28 require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
29 //require_once DOL_DOCUMENT_ROOT . '/societe/class/societe.class.php';
30 //require_once DOL_DOCUMENT_ROOT . '/product/class/product.class.php';
31 
36 {
40  public $element = 'societeaccount';
41 
45  public $table_element = 'societe_account';
46 
50  public $ismultientitymanaged = 0;
51 
55  public $picto = 'lock';
56 
57 
75  // BEGIN MODULEBUILDER PROPERTIES
79  public $fields = array(
80  'rowid' => array('type'=>'integer', 'label'=>'TechnicalID', 'visible'=>-2, 'enabled'=>1, 'position'=>1, 'notnull'=>1, 'index'=>1, 'comment'=>'Id',),
81  'entity' => array('type'=>'integer', 'label'=>'Entity', 'visible'=>0, 'enabled'=>1, 'position'=>5, 'default'=>1),
82  'login' => array('type'=>'varchar(64)', 'label'=>'Login', 'visible'=>1, 'enabled'=>1, 'notnull'=>1, 'position'=>10, 'showoncombobox'=>1),
83  'pass_encoding' => array('type'=>'varchar(24)', 'label'=>'PassEncoding', 'visible'=>0, 'enabled'=>1, 'position'=>30),
84  'pass_crypted' => array('type'=>'varchar(128)', 'label'=>'Password', 'visible'=>1, 'enabled'=>1, 'position'=>31, 'notnull'=>1),
85  'pass_temp' => array('type'=>'varchar(128)', 'label'=>'Temp', 'visible'=>0, 'enabled'=>0, 'position'=>32, 'notnull'=>-1,),
86  'fk_soc' => array('type'=>'integer:Societe:societe/class/societe.class.php', 'label'=>'ThirdParty', 'visible'=>1, 'enabled'=>1, 'position'=>40, 'notnull'=>-1, 'index'=>1),
87  'fk_website' => array('type'=>'integer:Website:website/class/website.class.php', 'label'=>'WebSite', 'visible'=>1, 'enabled'=>1, 'position'=>42, 'notnull'=>-1, 'index'=>1),
88  'site' => array('type'=>'varchar(128)', 'label'=>'ExternalSite', 'visible'=>0, 'enabled'=>1, 'position'=>43, 'help'=>'Name of the website or service if this is account on an external website or service'),
89  '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'),
90  '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)',),
91  'date_last_login' => array('type'=>'datetime', 'label'=>'LastConnexion', 'visible'=>2, 'enabled'=>1, 'position'=>50, 'notnull'=>0,),
92  'date_previous_login' => array('type'=>'datetime', 'label'=>'PreviousConnexion', 'visible'=>2, 'enabled'=>1, 'position'=>51, 'notnull'=>0,),
93  //'note_public' => array('type'=>'text', 'label'=>'NotePublic', 'visible'=>-1, 'enabled'=>1, 'position'=>45, 'notnull'=>-1,),
94  'note_private' => array('type'=>'text', 'label'=>'NotePrivate', 'visible'=>-1, 'enabled'=>1, 'position'=>46, 'notnull'=>-1,),
95  'date_creation' => array('type'=>'datetime', 'label'=>'DateCreation', 'visible'=>-2, 'enabled'=>1, 'position'=>500, 'notnull'=>1,),
96  'tms' => array('type'=>'timestamp', 'label'=>'DateModification', 'visible'=>-2, 'enabled'=>1, 'position'=>500, 'notnull'=>1,),
97  'fk_user_creat' => array('type'=>'integer', 'label'=>'UserAuthor', 'visible'=>-2, 'enabled'=>1, 'position'=>500, 'notnull'=>1,),
98  'fk_user_modif' => array('type'=>'integer', 'label'=>'UserModif', 'visible'=>-2, 'enabled'=>1, 'position'=>500, 'notnull'=>-1,),
99  'import_key' => array('type'=>'varchar(14)', 'label'=>'ImportId', 'visible'=>-2, 'enabled'=>1, 'position'=>1000, 'notnull'=>-1, 'index'=>1,),
100  'status' => array('type'=>'integer', 'label'=>'Status', 'visible'=>1, 'enabled'=>1, 'position'=>1000, 'notnull'=>1, 'index'=>1, 'default'=>1, 'arrayofkeyval'=>array('1'=>'Active', '0'=>'Disabled')),
101  );
102 
106  public $rowid;
107 
111  public $entity;
112 
113  public $key_account;
114  public $login;
115  public $pass_encoding;
116  public $pass_crypted;
117  public $pass_temp;
118 
122  public $fk_soc;
123 
124  public $site;
125  public $site_account;
126 
130  public $date_last_login;
131 
132 
133  public $date_previous_login;
134  public $note_private;
135 
139  public $date_creation;
140 
141 
142  public $tms;
143 
147  public $fk_user_creat;
148 
152  public $fk_user_modif;
153 
154  public $import_key;
155 
159  public $status;
160 
161  // END MODULEBUILDER PROPERTIES
162 
163 
169  public function __construct(DoliDB $db)
170  {
171  global $conf;
172 
173  $this->db = $db;
174 
175  if (empty($conf->global->MAIN_SHOW_TECHNICAL_ID)) $this->fields['rowid']['visible'] = 0;
176  }
177 
185  public function create(User $user, $notrigger = false)
186  {
187  return $this->createCommon($user, $notrigger);
188  }
189 
197  public function createFromClone(User $user, $fromid)
198  {
199  global $hookmanager, $langs;
200  $error = 0;
201 
202  dol_syslog(__METHOD__, LOG_DEBUG);
203 
204  $object = new self($this->db);
205 
206  $this->db->begin();
207 
208  // Load source object
209  $object->fetchCommon($fromid);
210  // Reset some properties
211  unset($object->id);
212  unset($object->fk_user_creat);
213  unset($object->import_key);
214 
215  // Clear fields
216  $object->ref = "copy_of_".$object->ref;
217  $object->title = $langs->trans("CopyOf")." ".$object->title;
218  // ...
219 
220  // Create clone
221  $object->context['createfromclone'] = 'createfromclone';
222  $result = $object->createCommon($user);
223  if ($result < 0) {
224  $error++;
225  $this->error = $object->error;
226  $this->errors = $object->errors;
227  }
228 
229  unset($object->context['createfromclone']);
230 
231  // End
232  if (!$error) {
233  $this->db->commit();
234  return $object;
235  } else {
236  $this->db->rollback();
237  return -1;
238  }
239  }
240 
248  public function fetch($id, $ref = null)
249  {
250  $result = $this->fetchCommon($id, $ref);
251  if ($result > 0 && !empty($this->table_element_line)) $this->fetchLines();
252  return $result;
253  }
254 
260  public function fetchLines()
261  {
262  $this->lines = array();
263 
264  // Load lines with object societeAccountLine
265 
266  return count($this->lines) ? 1 : 0;
267  }
268 
279  public function getCustomerAccount($id, $site, $status = 0, $site_account = '')
280  {
281  $sql = "SELECT sa.key_account as key_account, sa.entity";
282  $sql .= " FROM ".MAIN_DB_PREFIX."societe_account as sa";
283  $sql .= " WHERE sa.fk_soc = ".$id;
284  $sql .= " AND sa.entity IN (".getEntity('societe').")";
285  $sql .= " AND sa.site = '".$this->db->escape($site)."' AND sa.status = ".((int) $status);
286  $sql .= " AND sa.key_account IS NOT NULL AND sa.key_account <> ''";
287  $sql .= " AND (sa.site_account = '' OR sa.site_account IS NULL OR sa.site_account = '".$this->db->escape($site_account)."')";
288  $sql .= " ORDER BY sa.site_account DESC"; // To get the entry with a site_account defined in priority
289 
290  dol_syslog(get_class($this)."::getCustomerAccount Try to find the first system customer id for ".$site." of thirdparty id=".$id." (exemple: cus_.... for stripe)", LOG_DEBUG);
291  $result = $this->db->query($sql);
292  if ($result) {
293  if ($this->db->num_rows($result)) {
294  $obj = $this->db->fetch_object($result);
295  $key = $obj->key_account;
296  } else {
297  $key = '';
298  }
299  } else {
300  $key = '';
301  }
302 
303  return $key;
304  }
305 
315  public function getThirdPartyID($id, $site, $status = 0)
316  {
317  $socid = 0;
318 
319  $sql = "SELECT sa.fk_soc as fk_soc, sa.key_account, sa.entity";
320  $sql .= " FROM ".MAIN_DB_PREFIX."societe_account as sa";
321  $sql .= " WHERE sa.key_account = '".$this->db->escape($id)."'";
322  $sql .= " AND sa.entity IN (".getEntity('societe').")";
323  $sql .= " AND sa.site = '".$this->db->escape($site)."' AND sa.status = ".((int) $status);
324  $sql .= " AND sa.fk_soc > 0";
325 
326  dol_syslog(get_class($this)."::getCustomerAccount Try to find the first thirdparty id for ".$site." for external id=".$id, LOG_DEBUG);
327  $result = $this->db->query($sql);
328  if ($result) {
329  if ($this->db->num_rows($result)) {
330  $obj = $this->db->fetch_object($result);
331  $socid = $obj->fk_soc;
332  }
333  }
334 
335  return $socid;
336  }
337 
345  public function update(User $user, $notrigger = false)
346  {
347  return $this->updateCommon($user, $notrigger);
348  }
349 
357  public function delete(User $user, $notrigger = false)
358  {
359  return $this->deleteCommon($user, $notrigger);
360  }
361 
372  public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
373  {
374  global $db, $conf, $langs;
375  global $dolibarr_main_authentication, $dolibarr_main_demo;
376  global $menumanager;
377 
378  if (!empty($conf->dol_no_mouse_hover)) $notooltip = 1; // Force disable tooltips
379 
380  $result = '';
381 
382  $this->ref = $this->login;
383 
384  $label = '<u>'.$langs->trans("WebsiteAccount").'</u>';
385  $label .= '<br>';
386  $label .= '<b>'.$langs->trans('Login').':</b> '.$this->ref;
387  //$label.= '<b>' . $langs->trans('WebSite') . ':</b> ' . $this->ref;
388 
389  $url = dol_buildpath('/website/websiteaccount_card.php', 1).'?id='.$this->id;
390 
391  if ($option != 'nolink')
392  {
393  // Add param to save lastsearch_values or not
394  $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
395  if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) $add_save_lastsearch_values = 1;
396  if ($add_save_lastsearch_values) $url .= '&save_lastsearch_values=1';
397  }
398 
399  $linkclose = '';
400  if (empty($notooltip))
401  {
402  if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER))
403  {
404  $label = $langs->trans("WebsiteAccount");
405  $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
406  }
407  $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
408  $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
409  } else $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
410 
411  $linkstart = '<a href="'.$url.'"';
412  $linkstart .= $linkclose.'>';
413  $linkend = '</a>';
414 
415  $result .= $linkstart;
416  if ($withpicto) $result .= img_object(($notooltip ? '' : $label), ($this->picto ? $this->picto : 'generic'), ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
417  if ($withpicto != 2) $result .= $this->ref;
418  $result .= $linkend;
419 
420  return $result;
421  }
422 
429  public function getLibStatut($mode = 0)
430  {
431  return $this->LibStatut($this->status, $mode);
432  }
433 
434  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
442  public static function LibStatut($status, $mode = 0)
443  {
444  // phpcs:enable
445  global $langs;
446 
447  if ($mode == 0)
448  {
449  $prefix = '';
450  if ($status == 1) return $langs->trans('Enabled');
451  elseif ($status == 0) return $langs->trans('Disabled');
452  } elseif ($mode == 1)
453  {
454  if ($status == 1) return $langs->trans('Enabled');
455  elseif ($status == 0) return $langs->trans('Disabled');
456  } elseif ($mode == 2)
457  {
458  if ($status == 1) return img_picto($langs->trans('Enabled'), 'statut4').' '.$langs->trans('Enabled');
459  elseif ($status == 0) return img_picto($langs->trans('Disabled'), 'statut5').' '.$langs->trans('Disabled');
460  } elseif ($mode == 3)
461  {
462  if ($status == 1) return img_picto($langs->trans('Enabled'), 'statut4');
463  elseif ($status == 0) return img_picto($langs->trans('Disabled'), 'statut5');
464  } elseif ($mode == 4)
465  {
466  if ($status == 1) return img_picto($langs->trans('Enabled'), 'statut4').' '.$langs->trans('Enabled');
467  elseif ($status == 0) return img_picto($langs->trans('Disabled'), 'statut5').' '.$langs->trans('Disabled');
468  } elseif ($mode == 5)
469  {
470  if ($status == 1) return $langs->trans('Enabled').' '.img_picto($langs->trans('Enabled'), 'statut4');
471  elseif ($status == 0) return $langs->trans('Disabled').' '.img_picto($langs->trans('Disabled'), 'statut5');
472  } elseif ($mode == 6)
473  {
474  if ($status == 1) return $langs->trans('Enabled').' '.img_picto($langs->trans('Enabled'), 'statut4');
475  elseif ($status == 0) return $langs->trans('Disabled').' '.img_picto($langs->trans('Disabled'), 'statut5');
476  }
477  }
478 
485  public function info($id)
486  {
487  $sql = 'SELECT rowid, date_creation as datec, tms as datem,';
488  $sql .= ' fk_user_creat, fk_user_modif';
489  $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
490  $sql .= ' WHERE t.rowid = '.$id;
491  $result = $this->db->query($sql);
492  if ($result)
493  {
494  if ($this->db->num_rows($result))
495  {
496  $obj = $this->db->fetch_object($result);
497  $this->id = $obj->rowid;
498  if ($obj->fk_user_author)
499  {
500  $cuser = new User($this->db);
501  $cuser->fetch($obj->fk_user_author);
502  $this->user_creation = $cuser;
503  }
504 
505  if ($obj->fk_user_valid)
506  {
507  $vuser = new User($this->db);
508  $vuser->fetch($obj->fk_user_valid);
509  $this->user_validation = $vuser;
510  }
511 
512  if ($obj->fk_user_cloture)
513  {
514  $cluser = new User($this->db);
515  $cluser->fetch($obj->fk_user_cloture);
516  $this->user_cloture = $cluser;
517  }
518 
519  $this->date_creation = $this->db->jdate($obj->datec);
520  $this->date_modification = $this->db->jdate($obj->datem);
521  $this->date_validation = $this->db->jdate($obj->datev);
522  }
523 
524  $this->db->free($result);
525  } else {
526  dol_print_error($this->db);
527  }
528  }
529 
536  public function initAsSpecimen()
537  {
538  $this->initAsSpecimenCommon();
539  }
540 }
__construct(DoliDB $db)
Constructor.
deleteCommon(User $user, $notrigger=false, $forcechilddeletion=0)
Delete object in database.
fetch($id, $ref=null)
Load object in memory from the database.
static LibStatut($status, $mode=0)
Return the status.
initAsSpecimen()
Initialise object with example values Id must be 0 if object instance is a specimen.
foreach($object->fields as $key=> $val) if(is_array($extrafields->attributes[$object->table_element]['label'])&&count($extrafields->attributes[$object->table_element]['label']) > 0) $object fields
Class to manage Dolibarr users.
Definition: user.class.php:44
Class to manage Dolibarr database access.
initAsSpecimenCommon()
Initialise object with example values Id must be 0 if object instance is a specimen.
createCommon(User $user, $notrigger=false)
Create object into database.
Class for SocieteAccount.
create(User $user, $notrigger=false)
Create object into database.
getThirdPartyID($id, $site, $status=0)
Try to find the thirdparty id from an another site/system external id.
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
$conf db
API class for accounts.
Definition: inc.php:54
getNomUrl($withpicto=0, $option= '', $notooltip=0, $morecss= '', $save_lastsearch_value=-1)
Return a link to the object card (with optionaly the picto)
update(User $user, $notrigger=false)
Update object into database.
img_picto($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt= '', $morecss= '', $marginleftonlyshort=2)
Show picto whatever it&#39;s its name (generic function)
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
img_object($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
updateCommon(User $user, $notrigger=false)
Update object into database.
createFromClone(User $user, $fromid)
Clone and object into another one.
print $_SERVER["PHP_SELF"]
Edit parameters.
getCustomerAccount($id, $site, $status=0, $site_account= '')
Try to find the external customer id of a thirdparty for another site/system.
dol_print_error($db= '', $error= '', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
fetchLines()
Load object lines in memory from the database.
getLibStatut($mode=0)
Retourne le libelle du status d&#39;un user (actif, inactif)
info($id)
Charge les informations d&#39;ordre info dans l&#39;objet commande.
Parent class of all other business classes (invoices, contracts, proposals, orders, ...)
fetchCommon($id, $ref=null, $morewhere= '')
Load object in memory from the database.