dolibarr  16.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  *
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)) {
176  $this->fields['rowid']['visible'] = 0;
177  }
178  }
179 
187  public function create(User $user, $notrigger = false)
188  {
189  return $this->createCommon($user, $notrigger);
190  }
191 
199  public function createFromClone(User $user, $fromid)
200  {
201  global $hookmanager, $langs;
202  $error = 0;
203 
204  dol_syslog(__METHOD__, LOG_DEBUG);
205 
206  $object = new self($this->db);
207 
208  $this->db->begin();
209 
210  // Load source object
211  $object->fetchCommon($fromid);
212  // Reset some properties
213  unset($object->id);
214  unset($object->fk_user_creat);
215  unset($object->import_key);
216 
217  // Clear fields
218  $object->ref = "copy_of_".$object->ref;
219  $object->title = $langs->trans("CopyOf")." ".$object->title;
220  // ...
221 
222  // Create clone
223  $object->context['createfromclone'] = 'createfromclone';
224  $result = $object->createCommon($user);
225  if ($result < 0) {
226  $error++;
227  $this->error = $object->error;
228  $this->errors = $object->errors;
229  }
230 
231  unset($object->context['createfromclone']);
232 
233  // End
234  if (!$error) {
235  $this->db->commit();
236  return $object;
237  } else {
238  $this->db->rollback();
239  return -1;
240  }
241  }
242 
250  public function fetch($id, $ref = null)
251  {
252  $result = $this->fetchCommon($id, $ref);
253  if ($result > 0 && !empty($this->table_element_line)) {
254  $this->fetchLines();
255  }
256  return $result;
257  }
258 
264  public function fetchLines()
265  {
266  $this->lines = array();
267 
268  // Load lines with object societeAccountLine
269 
270  return count($this->lines) ? 1 : 0;
271  }
272 
283  public function getCustomerAccount($id, $site, $status = 0, $site_account = '')
284  {
285  $sql = "SELECT sa.key_account as key_account, sa.entity";
286  $sql .= " FROM ".MAIN_DB_PREFIX."societe_account as sa";
287  $sql .= " WHERE sa.fk_soc = ".((int) $id);
288  $sql .= " AND sa.entity IN (".getEntity('societe').")";
289  $sql .= " AND sa.site = '".$this->db->escape($site)."' AND sa.status = ".((int) $status);
290  $sql .= " AND sa.key_account IS NOT NULL AND sa.key_account <> ''";
291  $sql .= " AND (sa.site_account = '' OR sa.site_account IS NULL OR sa.site_account = '".$this->db->escape($site_account)."')";
292  $sql .= " ORDER BY sa.site_account DESC"; // To get the entry with a site_account defined in priority
293 
294  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);
295  $result = $this->db->query($sql);
296  if ($result) {
297  if ($this->db->num_rows($result)) {
298  $obj = $this->db->fetch_object($result);
299  $key = $obj->key_account;
300  } else {
301  $key = '';
302  }
303  } else {
304  $key = '';
305  }
306 
307  return $key;
308  }
309 
319  public function getThirdPartyID($id, $site, $status = 0)
320  {
321  $socid = 0;
322 
323  $sql = "SELECT sa.fk_soc as fk_soc, sa.key_account, sa.entity";
324  $sql .= " FROM ".MAIN_DB_PREFIX."societe_account as sa";
325  $sql .= " WHERE sa.key_account = '".$this->db->escape($id)."'";
326  $sql .= " AND sa.entity IN (".getEntity('societe').")";
327  $sql .= " AND sa.site = '".$this->db->escape($site)."' AND sa.status = ".((int) $status);
328  $sql .= " AND sa.fk_soc > 0";
329 
330  dol_syslog(get_class($this)."::getCustomerAccount Try to find the first thirdparty id for ".$site." for external id=".$id, LOG_DEBUG);
331  $result = $this->db->query($sql);
332  if ($result) {
333  if ($this->db->num_rows($result)) {
334  $obj = $this->db->fetch_object($result);
335  $socid = $obj->fk_soc;
336  }
337  }
338 
339  return $socid;
340  }
341 
349  public function update(User $user, $notrigger = false)
350  {
351  return $this->updateCommon($user, $notrigger);
352  }
353 
361  public function delete(User $user, $notrigger = false)
362  {
363  return $this->deleteCommon($user, $notrigger);
364  }
365 
376  public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
377  {
378  global $db, $conf, $langs;
379  global $dolibarr_main_authentication, $dolibarr_main_demo;
380  global $menumanager;
381 
382  if (!empty($conf->dol_no_mouse_hover)) {
383  $notooltip = 1; // Force disable tooltips
384  }
385 
386  $result = '';
387 
388  $this->ref = $this->login;
389 
390  $label = '<u>'.$langs->trans("WebsiteAccount").'</u>';
391  $label .= '<br>';
392  $label .= '<b>'.$langs->trans('Login').':</b> '.$this->ref;
393  //$label.= '<b>' . $langs->trans('WebSite') . ':</b> ' . $this->ref;
394 
395  $url = DOL_URL_ROOT.'/website/websiteaccount_card.php?id='.$this->id;
396 
397  if ($option != 'nolink') {
398  // Add param to save lastsearch_values or not
399  $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
400  if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
401  $add_save_lastsearch_values = 1;
402  }
403  if ($add_save_lastsearch_values) {
404  $url .= '&save_lastsearch_values=1';
405  }
406  }
407 
408  $linkclose = '';
409  if (empty($notooltip)) {
410  if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
411  $label = $langs->trans("WebsiteAccount");
412  $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
413  }
414  $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
415  $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
416  } else {
417  $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
418  }
419 
420  $linkstart = '<a href="'.$url.'"';
421  $linkstart .= $linkclose.'>';
422  $linkend = '</a>';
423 
424  $result .= $linkstart;
425  if ($withpicto) {
426  $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);
427  }
428  if ($withpicto != 2) {
429  $result .= $this->ref;
430  }
431  $result .= $linkend;
432 
433  return $result;
434  }
435 
442  public function getLibStatut($mode = 0)
443  {
444  return $this->LibStatut($this->status, $mode);
445  }
446 
447  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
455  public static function LibStatut($status, $mode = 0)
456  {
457  // phpcs:enable
458  global $langs;
459 
460  if ($mode == 0) {
461  $prefix = '';
462  if ($status == 1) {
463  return $langs->trans('Enabled');
464  } elseif ($status == 0) {
465  return $langs->trans('Disabled');
466  }
467  } elseif ($mode == 1) {
468  if ($status == 1) {
469  return $langs->trans('Enabled');
470  } elseif ($status == 0) {
471  return $langs->trans('Disabled');
472  }
473  } elseif ($mode == 2) {
474  if ($status == 1) {
475  return img_picto($langs->trans('Enabled'), 'statut4').' '.$langs->trans('Enabled');
476  } elseif ($status == 0) {
477  return img_picto($langs->trans('Disabled'), 'statut5').' '.$langs->trans('Disabled');
478  }
479  } elseif ($mode == 3) {
480  if ($status == 1) {
481  return img_picto($langs->trans('Enabled'), 'statut4');
482  } elseif ($status == 0) {
483  return img_picto($langs->trans('Disabled'), 'statut5');
484  }
485  } elseif ($mode == 4) {
486  if ($status == 1) {
487  return img_picto($langs->trans('Enabled'), 'statut4').' '.$langs->trans('Enabled');
488  } elseif ($status == 0) {
489  return img_picto($langs->trans('Disabled'), 'statut5').' '.$langs->trans('Disabled');
490  }
491  } elseif ($mode == 5) {
492  if ($status == 1) {
493  return $langs->trans('Enabled').' '.img_picto($langs->trans('Enabled'), 'statut4');
494  } elseif ($status == 0) {
495  return $langs->trans('Disabled').' '.img_picto($langs->trans('Disabled'), 'statut5');
496  }
497  } elseif ($mode == 6) {
498  if ($status == 1) {
499  return $langs->trans('Enabled').' '.img_picto($langs->trans('Enabled'), 'statut4');
500  } elseif ($status == 0) {
501  return $langs->trans('Disabled').' '.img_picto($langs->trans('Disabled'), 'statut5');
502  }
503  }
504  }
505 
512  public function info($id)
513  {
514  $sql = 'SELECT rowid, date_creation as datec, tms as datem,';
515  $sql .= ' fk_user_creat, fk_user_modif';
516  $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
517  $sql .= ' WHERE t.rowid = '.((int) $id);
518  $result = $this->db->query($sql);
519  if ($result) {
520  if ($this->db->num_rows($result)) {
521  $obj = $this->db->fetch_object($result);
522  $this->id = $obj->rowid;
523 
524 
525  $this->user_creation_id = $obj->fk_user_creat;
526  $this->user_modification_id = $obj->fk_user_modif;
527  $this->date_creation = $this->db->jdate($obj->datec);
528  $this->date_modification = empty($obj->datem) ? '' : $this->db->jdate($obj->datem);
529  }
530 
531  $this->db->free($result);
532  } else {
533  dol_print_error($this->db);
534  }
535  }
536 
543  public function initAsSpecimen()
544  {
545  $this->initAsSpecimenCommon();
546  }
547 }
CommonObject\deleteCommon
deleteCommon(User $user, $notrigger=false, $forcechilddeletion=0)
Delete object in database.
Definition: commonobject.class.php:9406
db
$conf db
API class for accounts.
Definition: inc.php:41
CommonObject\fetchCommon
fetchCommon($id, $ref=null, $morewhere='')
Load object in memory from the database.
Definition: commonobject.class.php:9202
SocieteAccount\LibStatut
static LibStatut($status, $mode=0)
Return the status.
Definition: societeaccount.class.php:455
DoliDB
Class to manage Dolibarr database access.
Definition: DoliDB.class.php:30
SocieteAccount\getCustomerAccount
getCustomerAccount($id, $site, $status=0, $site_account='')
Try to find the external customer id of a thirdparty for another site/system.
Definition: societeaccount.class.php:283
dol_print_error
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
Definition: functions.lib.php:4844
SocieteAccount
Class for SocieteAccount.
Definition: societeaccount.class.php:35
SocieteAccount\create
create(User $user, $notrigger=false)
Create object into database.
Definition: societeaccount.class.php:187
ref
$object ref
Definition: info.php:77
SocieteAccount\__construct
__construct(DoliDB $db)
Constructor.
Definition: societeaccount.class.php:169
SocieteAccount\update
update(User $user, $notrigger=false)
Update object into database.
Definition: societeaccount.class.php:349
CommonObject\initAsSpecimenCommon
initAsSpecimenCommon()
Initialise object with example values Id must be 0 if object instance is a specimen.
Definition: commonobject.class.php:9733
CommonObject
Parent class of all other business classes (invoices, contracts, proposals, orders,...
Definition: commonobject.class.php:44
SocieteAccount\fetch
fetch($id, $ref=null)
Load object in memory from the database.
Definition: societeaccount.class.php:250
img_picto
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
Definition: functions.lib.php:3880
SocieteAccount\fetchLines
fetchLines()
Load object lines in memory from the database.
Definition: societeaccount.class.php:264
CommonObject\createCommon
createCommon(User $user, $notrigger=false)
Create object into database.
Definition: commonobject.class.php:9035
SocieteAccount\getThirdPartyID
getThirdPartyID($id, $site, $status=0)
Try to find the thirdparty id from an another site/system external id.
Definition: societeaccount.class.php:319
SocieteAccount\createFromClone
createFromClone(User $user, $fromid)
Clone and object into another one.
Definition: societeaccount.class.php:199
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1603
CommonObject\updateCommon
updateCommon(User $user, $notrigger=false)
Update object into database.
Definition: commonobject.class.php:9308
User
Class to manage Dolibarr users.
Definition: user.class.php:44
SocieteAccount\initAsSpecimen
initAsSpecimen()
Initialise object with example values Id must be 0 if object instance is a specimen.
Definition: societeaccount.class.php:543
SocieteAccount\info
info($id)
Charge les informations d'ordre info dans l'objet commande.
Definition: societeaccount.class.php:512
img_object
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
Definition: functions.lib.php:4211
SocieteAccount\getNomUrl
getNomUrl($withpicto=0, $option='', $notooltip=0, $morecss='', $save_lastsearch_value=-1)
Return a link to the object card (with optionaly the picto)
Definition: societeaccount.class.php:376
SocieteAccount\getLibStatut
getLibStatut($mode=0)
Retourne le libelle du status d'un user (actif, inactif)
Definition: societeaccount.class.php:442