societeaccount.class.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  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. /**
  21. * \file societe/class/societeaccount.class.php
  22. * \ingroup societe
  23. * \brief This file is a CRUD class file for SocieteAccount (Create/Read/Update/Delete)
  24. */
  25. // Put here all includes required by your class file
  26. require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
  27. //require_once DOL_DOCUMENT_ROOT . '/societe/class/societe.class.php';
  28. //require_once DOL_DOCUMENT_ROOT . '/product/class/product.class.php';
  29. /**
  30. * Class for SocieteAccount
  31. */
  32. class SocieteAccount extends CommonObject
  33. {
  34. /**
  35. * @var string ID to identify managed object
  36. */
  37. public $element = 'societeaccount';
  38. /**
  39. * @var string Name of table without prefix where object is stored
  40. */
  41. public $table_element = 'societe_account';
  42. /**
  43. * @var array Does societeaccount support multicompany module ? 0=No test on entity, 1=Test with field entity, 2=Test with link by societe
  44. */
  45. public $ismultientitymanaged = 0;
  46. /**
  47. * @var string String with name of icon for societeaccount. Must be the part after the 'object_' into object_myobject.png
  48. */
  49. public $picto = 'lock';
  50. /**
  51. * 'type' if the field format.
  52. * 'label' the translation key.
  53. * 'enabled' is a condition when the field must be managed.
  54. * 'visible' says if field is visible in list (Examples: 0=Not visible, 1=Visible on list and create/update/view forms, 2=Visible on list only. Using a negative value means field is not shown by default on list but can be selected for viewing)
  55. * 'notnull' is set to 1 if not null in database. Set to -1 if we must set data to null if empty ('' or 0).
  56. * 'index' if we want an index in database.
  57. * 'foreignkey'=>'tablename.field' if the field is a foreign key (it is recommanded to name the field fk_...).
  58. * 'position' is the sort order of field.
  59. * 'searchall' is 1 if we want to search in this field when making a search from the quick search button.
  60. * 'isameasure' must be set to 1 if you want to have a total on list for this field. Field type must be summable like integer or double(24,8).
  61. * 'help' is a string visible as a tooltip on field
  62. * 'comment' is not used. You can store here any text of your choice. It is not used by application.
  63. * 'default' is a default value for creation (can still be replaced by the global setup of default values)
  64. * 'showoncombobox' if field must be shown into the label of combobox
  65. */
  66. // BEGIN MODULEBUILDER PROPERTIES
  67. /**
  68. * @var array Array with all fields and their property. Do not use it as a static var. It may be modified by constructor.
  69. */
  70. public $fields = array(
  71. 'rowid' => array('type'=>'integer', 'label'=>'TechnicalID', 'visible'=>-2, 'enabled'=>1, 'position'=>1, 'notnull'=>1, 'index'=>1, 'comment'=>'Id',),
  72. 'entity' => array('type'=>'integer', 'label'=>'Entity', 'visible'=>0, 'enabled'=>1, 'position'=>5, 'default'=>1),
  73. 'login' => array('type'=>'varchar(64)', 'label'=>'Login', 'visible'=>1, 'enabled'=>1, 'notnull'=>1, 'position'=>10, 'showoncombobox'=>1),
  74. 'pass_encoding' => array('type'=>'varchar(24)', 'label'=>'PassEncoding', 'visible'=>0, 'enabled'=>1, 'position'=>30),
  75. 'pass_crypted' => array('type'=>'varchar(128)', 'label'=>'Password', 'visible'=>1, 'enabled'=>1, 'position'=>31, 'notnull'=>1),
  76. 'pass_temp' => array('type'=>'varchar(128)', 'label'=>'Temp', 'visible'=>0, 'enabled'=>0, 'position'=>32, 'notnull'=>-1,),
  77. 'fk_soc' => array('type'=>'integer:Societe:societe/class/societe.class.php', 'label'=>'ThirdParty', 'visible'=>1, 'enabled'=>1, 'position'=>40, 'notnull'=>-1, 'index'=>1),
  78. 'fk_website' => array('type'=>'integer:Website:website/class/website.class.php', 'label'=>'WebSite', 'visible'=>1, 'enabled'=>1, 'position'=>42, 'notnull'=>-1, 'index'=>1),
  79. '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'),
  80. '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'),
  81. '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)',),
  82. 'date_last_login' => array('type'=>'datetime', 'label'=>'LastConnexion', 'visible'=>2, 'enabled'=>1, 'position'=>50, 'notnull'=>0,),
  83. 'date_previous_login' => array('type'=>'datetime', 'label'=>'PreviousConnexion', 'visible'=>2, 'enabled'=>1, 'position'=>51, 'notnull'=>0,),
  84. //'note_public' => array('type'=>'text', 'label'=>'NotePublic', 'visible'=>-1, 'enabled'=>1, 'position'=>45, 'notnull'=>-1,),
  85. 'note_private' => array('type'=>'text', 'label'=>'NotePrivate', 'visible'=>-1, 'enabled'=>1, 'position'=>46, 'notnull'=>-1,),
  86. 'date_creation' => array('type'=>'datetime', 'label'=>'DateCreation', 'visible'=>-2, 'enabled'=>1, 'position'=>500, 'notnull'=>1,),
  87. 'tms' => array('type'=>'timestamp', 'label'=>'DateModification', 'visible'=>-2, 'enabled'=>1, 'position'=>500, 'notnull'=>1,),
  88. 'fk_user_creat' => array('type'=>'integer', 'label'=>'UserAuthor', 'visible'=>-2, 'enabled'=>1, 'position'=>500, 'notnull'=>1,),
  89. 'fk_user_modif' => array('type'=>'integer', 'label'=>'UserModif', 'visible'=>-2, 'enabled'=>1, 'position'=>500, 'notnull'=>-1,),
  90. 'import_key' => array('type'=>'varchar(14)', 'label'=>'ImportId', 'visible'=>-2, 'enabled'=>1, 'position'=>1000, 'notnull'=>-1, 'index'=>1,),
  91. 'status' => array('type'=>'integer', 'label'=>'Status', 'visible'=>1, 'enabled'=>1, 'position'=>1000, 'notnull'=>1, 'index'=>1, 'default'=>1, 'arrayofkeyval'=>array('1'=>'Active', '0'=>'Disabled')),
  92. );
  93. /**
  94. * @var int ID
  95. */
  96. public $rowid;
  97. /**
  98. * @var int Entity
  99. */
  100. public $entity;
  101. public $key_account;
  102. public $login;
  103. public $pass_encoding;
  104. public $pass_crypted;
  105. public $pass_temp;
  106. /**
  107. * @var int Thirdparty ID
  108. */
  109. public $fk_soc;
  110. public $site;
  111. public $site_account;
  112. /**
  113. * @var integer|string date_last_login
  114. */
  115. public $date_last_login;
  116. public $date_previous_login;
  117. public $note_private;
  118. /**
  119. * @var integer|string date_creation
  120. */
  121. public $date_creation;
  122. public $tms;
  123. /**
  124. * @var int ID
  125. */
  126. public $fk_user_creat;
  127. /**
  128. * @var int ID
  129. */
  130. public $fk_user_modif;
  131. public $import_key;
  132. /**
  133. * @var int Status
  134. */
  135. public $status;
  136. // END MODULEBUILDER PROPERTIES
  137. /**
  138. * Constructor
  139. *
  140. * @param DoliDb $db Database handler
  141. */
  142. public function __construct(DoliDB $db)
  143. {
  144. global $conf;
  145. $this->db = $db;
  146. if (empty($conf->global->MAIN_SHOW_TECHNICAL_ID)) {
  147. $this->fields['rowid']['visible'] = 0;
  148. }
  149. }
  150. /**
  151. * Create object into database
  152. *
  153. * @param User $user User that creates
  154. * @param bool $notrigger false=launch triggers after, true=disable triggers
  155. * @return int <0 if KO, Id of created object if OK
  156. */
  157. public function create(User $user, $notrigger = false)
  158. {
  159. return $this->createCommon($user, $notrigger);
  160. }
  161. /**
  162. * Clone and object into another one
  163. *
  164. * @param User $user User that creates
  165. * @param int $fromid Id of object to clone
  166. * @return mixed New object created, <0 if KO
  167. */
  168. public function createFromClone(User $user, $fromid)
  169. {
  170. global $hookmanager, $langs;
  171. $error = 0;
  172. dol_syslog(__METHOD__, LOG_DEBUG);
  173. $object = new self($this->db);
  174. $this->db->begin();
  175. // Load source object
  176. $object->fetchCommon($fromid);
  177. // Reset some properties
  178. unset($object->id);
  179. unset($object->fk_user_creat);
  180. unset($object->import_key);
  181. // Clear fields
  182. $object->ref = "copy_of_".$object->ref;
  183. $object->title = $langs->trans("CopyOf")." ".$object->title;
  184. // ...
  185. // Create clone
  186. $object->context['createfromclone'] = 'createfromclone';
  187. $result = $object->createCommon($user);
  188. if ($result < 0) {
  189. $error++;
  190. $this->error = $object->error;
  191. $this->errors = $object->errors;
  192. }
  193. unset($object->context['createfromclone']);
  194. // End
  195. if (!$error) {
  196. $this->db->commit();
  197. return $object;
  198. } else {
  199. $this->db->rollback();
  200. return -1;
  201. }
  202. }
  203. /**
  204. * Load object in memory from the database
  205. *
  206. * @param int $id Id object
  207. * @param string $ref Ref
  208. * @return int <0 if KO, 0 if not found, >0 if OK
  209. */
  210. public function fetch($id, $ref = null)
  211. {
  212. $result = $this->fetchCommon($id, $ref);
  213. if ($result > 0 && !empty($this->table_element_line)) {
  214. $this->fetchLines();
  215. }
  216. return $result;
  217. }
  218. /**
  219. * Load object lines in memory from the database
  220. *
  221. * @return int <0 if KO, 0 if not found, >0 if OK
  222. */
  223. public function fetchLines()
  224. {
  225. $this->lines = array();
  226. // Load lines with object societeAccountLine
  227. return count($this->lines) ? 1 : 0;
  228. }
  229. /**
  230. * Try to find the external customer id of a thirdparty for another site/system.
  231. *
  232. * @param int $id Id of third party
  233. * @param string $site Site (example: 'stripe', '...')
  234. * @param int $status Status (0=test, 1=live)
  235. * @param string $site_account Value to use to identify with account to use on site when site can offer several accounts. For example: 'pk_live_123456' when using Stripe service.
  236. * @return string Stripe customer ref 'cu_xxxxxxxxxxxxx' or ''
  237. * @see getThirdPartyID()
  238. */
  239. public function getCustomerAccount($id, $site, $status = 0, $site_account = '')
  240. {
  241. $sql = "SELECT sa.key_account as key_account, sa.entity";
  242. $sql .= " FROM ".MAIN_DB_PREFIX."societe_account as sa";
  243. $sql .= " WHERE sa.fk_soc = ".((int) $id);
  244. $sql .= " AND sa.entity IN (".getEntity('societe').")";
  245. $sql .= " AND sa.site = '".$this->db->escape($site)."' AND sa.status = ".((int) $status);
  246. $sql .= " AND sa.key_account IS NOT NULL AND sa.key_account <> ''";
  247. $sql .= " AND (sa.site_account = '' OR sa.site_account IS NULL OR sa.site_account = '".$this->db->escape($site_account)."')";
  248. $sql .= " ORDER BY sa.site_account DESC"; // To get the entry with a site_account defined in priority
  249. 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);
  250. $result = $this->db->query($sql);
  251. if ($result) {
  252. if ($this->db->num_rows($result)) {
  253. $obj = $this->db->fetch_object($result);
  254. $key = $obj->key_account;
  255. } else {
  256. $key = '';
  257. }
  258. } else {
  259. $key = '';
  260. }
  261. return $key;
  262. }
  263. /**
  264. * Try to find the thirdparty id from an another site/system external id.
  265. *
  266. * @param string $id Id of customer in external system (example: 'cu_xxxxxxxxxxxxx', ...)
  267. * @param string $site Site (example: 'stripe', '...')
  268. * @param int $status Status (0=test, 1=live)
  269. * @return int Id of third party
  270. * @see getCustomerAccount()
  271. */
  272. public function getThirdPartyID($id, $site, $status = 0)
  273. {
  274. $socid = 0;
  275. $sql = "SELECT sa.fk_soc as fk_soc, sa.key_account, sa.entity";
  276. $sql .= " FROM ".MAIN_DB_PREFIX."societe_account as sa";
  277. $sql .= " WHERE sa.key_account = '".$this->db->escape($id)."'";
  278. $sql .= " AND sa.entity IN (".getEntity('societe').")";
  279. $sql .= " AND sa.site = '".$this->db->escape($site)."' AND sa.status = ".((int) $status);
  280. $sql .= " AND sa.fk_soc > 0";
  281. dol_syslog(get_class($this)."::getCustomerAccount Try to find the first thirdparty id for ".$site." for external id=".$id, LOG_DEBUG);
  282. $result = $this->db->query($sql);
  283. if ($result) {
  284. if ($this->db->num_rows($result)) {
  285. $obj = $this->db->fetch_object($result);
  286. $socid = $obj->fk_soc;
  287. }
  288. }
  289. return $socid;
  290. }
  291. /**
  292. * Update object into database
  293. *
  294. * @param User $user User that modifies
  295. * @param bool $notrigger false=launch triggers after, true=disable triggers
  296. * @return int <0 if KO, >0 if OK
  297. */
  298. public function update(User $user, $notrigger = false)
  299. {
  300. return $this->updateCommon($user, $notrigger);
  301. }
  302. /**
  303. * Delete object in database
  304. *
  305. * @param User $user User that deletes
  306. * @param bool $notrigger false=launch triggers after, true=disable triggers
  307. * @return int <0 if KO, >0 if OK
  308. */
  309. public function delete(User $user, $notrigger = false)
  310. {
  311. return $this->deleteCommon($user, $notrigger);
  312. }
  313. /**
  314. * Return a link to the object card (with optionaly the picto)
  315. *
  316. * @param int $withpicto Include picto in link (0=No picto, 1=Include picto into link, 2=Only picto)
  317. * @param string $option On what the link point to ('nolink', ...)
  318. * @param int $notooltip 1=Disable tooltip
  319. * @param string $morecss Add more css on link
  320. * @param int $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking
  321. * @return string String with URL
  322. */
  323. public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
  324. {
  325. global $db, $conf, $langs;
  326. global $dolibarr_main_authentication, $dolibarr_main_demo;
  327. global $menumanager;
  328. if (!empty($conf->dol_no_mouse_hover)) {
  329. $notooltip = 1; // Force disable tooltips
  330. }
  331. $result = '';
  332. $this->ref = $this->login;
  333. $label = '<u>'.$langs->trans("WebsiteAccount").'</u>';
  334. $label .= '<br>';
  335. $label .= '<b>'.$langs->trans('Login').':</b> '.$this->ref;
  336. //$label.= '<b>' . $langs->trans('WebSite') . ':</b> ' . $this->ref;
  337. $url = DOL_URL_ROOT.'/website/websiteaccount_card.php?id='.$this->id;
  338. if ($option != 'nolink') {
  339. // Add param to save lastsearch_values or not
  340. $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
  341. if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
  342. $add_save_lastsearch_values = 1;
  343. }
  344. if ($add_save_lastsearch_values) {
  345. $url .= '&save_lastsearch_values=1';
  346. }
  347. }
  348. $linkclose = '';
  349. if (empty($notooltip)) {
  350. if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
  351. $label = $langs->trans("WebsiteAccount");
  352. $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
  353. }
  354. $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
  355. $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
  356. } else {
  357. $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
  358. }
  359. $linkstart = '<a href="'.$url.'"';
  360. $linkstart .= $linkclose.'>';
  361. $linkend = '</a>';
  362. $result .= $linkstart;
  363. if ($withpicto) {
  364. $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);
  365. }
  366. if ($withpicto != 2) {
  367. $result .= $this->ref;
  368. }
  369. $result .= $linkend;
  370. return $result;
  371. }
  372. /**
  373. * Retourne le libelle du status d'un user (actif, inactif)
  374. *
  375. * @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
  376. * @return string Label of status
  377. */
  378. public function getLibStatut($mode = 0)
  379. {
  380. return $this->LibStatut($this->status, $mode);
  381. }
  382. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  383. /**
  384. * Return the status
  385. *
  386. * @param int $status Id status
  387. * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto, 6=Long label + Picto
  388. * @return string Label of status
  389. */
  390. public static function LibStatut($status, $mode = 0)
  391. {
  392. // phpcs:enable
  393. global $langs;
  394. if ($mode == 0) {
  395. $prefix = '';
  396. if ($status == 1) {
  397. return $langs->trans('Enabled');
  398. } elseif ($status == 0) {
  399. return $langs->trans('Disabled');
  400. }
  401. } elseif ($mode == 1) {
  402. if ($status == 1) {
  403. return $langs->trans('Enabled');
  404. } elseif ($status == 0) {
  405. return $langs->trans('Disabled');
  406. }
  407. } elseif ($mode == 2) {
  408. if ($status == 1) {
  409. return img_picto($langs->trans('Enabled'), 'statut4').' '.$langs->trans('Enabled');
  410. } elseif ($status == 0) {
  411. return img_picto($langs->trans('Disabled'), 'statut5').' '.$langs->trans('Disabled');
  412. }
  413. } elseif ($mode == 3) {
  414. if ($status == 1) {
  415. return img_picto($langs->trans('Enabled'), 'statut4');
  416. } elseif ($status == 0) {
  417. return img_picto($langs->trans('Disabled'), 'statut5');
  418. }
  419. } elseif ($mode == 4) {
  420. if ($status == 1) {
  421. return img_picto($langs->trans('Enabled'), 'statut4').' '.$langs->trans('Enabled');
  422. } elseif ($status == 0) {
  423. return img_picto($langs->trans('Disabled'), 'statut5').' '.$langs->trans('Disabled');
  424. }
  425. } elseif ($mode == 5) {
  426. if ($status == 1) {
  427. return $langs->trans('Enabled').' '.img_picto($langs->trans('Enabled'), 'statut4');
  428. } elseif ($status == 0) {
  429. return $langs->trans('Disabled').' '.img_picto($langs->trans('Disabled'), 'statut5');
  430. }
  431. } elseif ($mode == 6) {
  432. if ($status == 1) {
  433. return $langs->trans('Enabled').' '.img_picto($langs->trans('Enabled'), 'statut4');
  434. } elseif ($status == 0) {
  435. return $langs->trans('Disabled').' '.img_picto($langs->trans('Disabled'), 'statut5');
  436. }
  437. }
  438. }
  439. /**
  440. * Charge les informations d'ordre info dans l'objet commande
  441. *
  442. * @param int $id Id of order
  443. * @return void
  444. */
  445. public function info($id)
  446. {
  447. $sql = 'SELECT rowid, date_creation as datec, tms as datem,';
  448. $sql .= ' fk_user_creat, fk_user_modif';
  449. $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
  450. $sql .= ' WHERE t.rowid = '.((int) $id);
  451. $result = $this->db->query($sql);
  452. if ($result) {
  453. if ($this->db->num_rows($result)) {
  454. $obj = $this->db->fetch_object($result);
  455. $this->id = $obj->rowid;
  456. $this->user_creation_id = $obj->fk_user_creat;
  457. $this->user_modification_id = $obj->fk_user_modif;
  458. $this->date_creation = $this->db->jdate($obj->datec);
  459. $this->date_modification = empty($obj->datem) ? '' : $this->db->jdate($obj->datem);
  460. }
  461. $this->db->free($result);
  462. } else {
  463. dol_print_error($this->db);
  464. }
  465. }
  466. /**
  467. * Initialise object with example values
  468. * Id must be 0 if object instance is a specimen
  469. *
  470. * @return void
  471. */
  472. public function initAsSpecimen()
  473. {
  474. $this->initAsSpecimenCommon();
  475. }
  476. }