modMultiCompany.class.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. <?php
  2. /* Copyright (C) 2009-2023 Regis Houssin <regis.houssin@inodbox.com>
  3. * Copyright (C) 2011 Herve Prot <herve.prot@symeos.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. /**
  20. * \defgroup multicompany Module multicompany
  21. * \brief Descriptor file for module multicompany
  22. * \file /multicompany/core/modules/modMultiCompany.class.php
  23. * \ingroup multicompany
  24. * \brief Description and activation file for module MultiCompany
  25. */
  26. include_once DOL_DOCUMENT_ROOT . '/core/modules/DolibarrModules.class.php';
  27. /**
  28. * \class modMultiCompany
  29. * \brief Description and activation class for module MultiCompany
  30. */
  31. class modMultiCompany extends DolibarrModules
  32. {
  33. /**
  34. * Constructor.
  35. *
  36. * @param DoliDB $db Database handler
  37. */
  38. function __construct($db)
  39. {
  40. global $langs;
  41. $this->db = $db;
  42. // Id for module (must be unique).
  43. // Use here a free id (See in Home -> System information -> Dolibarr for list of used modules id).
  44. $this->numero = 5000;
  45. // Key text used to identify module (for permissions, menus, etc...)
  46. $this->rights_class = 'multicompany';
  47. // Family can be 'crm','financial','hr','projects','products','ecm','technic','other'
  48. // It is used to group modules in module setup page
  49. $this->family = "base";
  50. // Gives the possibility to the module, to provide his own family info and position of this family.
  51. $this->familyinfo = array(
  52. 'core' => array(
  53. 'position' => '001',
  54. 'label' => $langs->trans("iNodbox")
  55. )
  56. );
  57. // Module position in the family
  58. $this->module_position = 1;
  59. // Module label (no space allowed), used if translation string 'ModuleXXXName' not found (where XXX is value of numeric property 'numero' of module)
  60. $this->name = preg_replace('/^mod/i','',get_class($this));
  61. // Module description, used if translation string 'ModuleXXXDesc' not found (where XXX is value of numeric property 'numero' of module)
  62. $this->description = "Multi-Company Management";
  63. //$this->descriptionlong = "A very lon description. Can be a full HTML content";
  64. $this->editor_name = 'Régis Houssin';
  65. $this->editor_url = 'https://www.inodbox.com';
  66. // Can be enabled / disabled only in the main company with superadmin account
  67. $this->core_enabled = 1;
  68. // Possible values for version are: 'development', 'experimental', 'dolibarr' or version
  69. $this->version = '16.0.1';
  70. //Url to the file with your last numberversion of this module
  71. $this->url_last_version = 'https://www.inodbox.com/versionmodules/multicompany16.txt';
  72. // Key used in llx_const table to save module status enabled/disabled (where MYMODULE is value of property name of module in uppercase)
  73. $this->const_name = 'MAIN_MODULE_'.strtoupper($this->name);
  74. // Name of png file (without png) used for this module.
  75. // Png file must be in theme/yourtheme/img directory under name object_pictovalue.png.
  76. $this->picto='multicompany@multicompany';
  77. // Data directories to create when module is enabled.
  78. $this->dirs = array();
  79. // Config pages. Put here list of php page names stored in admmin directory used to setup module.
  80. $this->config_page_url = array("multicompany.php@multicompany");
  81. // Defined all module parts (triggers, login, substitutions, menus, css, etc...)
  82. $this->module_parts = array(
  83. 'login' => array(
  84. 'data' => 1,
  85. 'entity' => '0'
  86. ),
  87. 'triggers' => array(
  88. 'data' => 1,
  89. 'entity' => '0'
  90. ),
  91. 'hooks' => array(
  92. 'data' => array(
  93. 'login',
  94. 'main',
  95. 'mainloginpage',
  96. 'upgrade',
  97. 'upgrade2',
  98. 'cashdeskloginpage',
  99. 'passwordforgottenpage',
  100. 'toprightmenu',
  101. 'adminmodules',
  102. 'admincompany',
  103. 'commonobject',
  104. 'thirdpartiesindex',
  105. 'commercialindex',
  106. 'globalcard',
  107. 'thirdpartycard',
  108. 'thirdpartycontact',
  109. 'thirdpartylist',
  110. 'customerlist',
  111. 'prospectlist',
  112. 'supplierlist',
  113. 'usercard',
  114. 'userperms',
  115. 'groupcard',
  116. 'groupperms',
  117. 'userlist',
  118. 'userhome',
  119. 'userdao',
  120. 'contactlist',
  121. 'contactprospectlist',
  122. 'contactcustomerlist',
  123. 'contactsupplierlist',
  124. 'contactotherlist',
  125. 'productindex',
  126. 'productcard',
  127. 'productservicelist',
  128. 'pricesuppliercard',
  129. 'productreassortlist',
  130. 'propalcard',
  131. 'propallist',
  132. 'ordercard',
  133. 'orderlist',
  134. 'invoicecard',
  135. 'invoicelist',
  136. 'warehousecard',
  137. 'stocklist',
  138. 'memberlist',
  139. 'membercard',
  140. 'projectlist',
  141. 'projectcard',
  142. 'accountancyadminaccount'
  143. ),
  144. 'entity' => '0'
  145. ),
  146. 'css' => array(
  147. 'data' => '/multicompany/css/multicompany.css.php',
  148. 'entity' => '0'
  149. ),
  150. 'js' => array(
  151. 'data' => '/multicompany/inc/multiselect/js/multiselect.min.js',
  152. 'entity' => '0'
  153. ),
  154. 'substitutions' => array(
  155. 'data' => '/multicompany/core/substitutions/',
  156. 'entity' => '0'
  157. )
  158. );
  159. // Dependencies
  160. $this->depends = array(); // List of modules id that must be enabled if this module is enabled
  161. $this->requiredby = array(); // List of modules id to disable if this one is disabled
  162. $this->phpmin = array(5,6); // Minimum version of PHP required by module
  163. $this->need_dolibarr_version = array(13,0,0); // Minimum version of Dolibarr required by module
  164. $this->langfiles = array("multicompany@multicompany");
  165. // Constants
  166. // List of particular constants to add when module is enabled
  167. $this->const=array(
  168. 1 => array('MULTICOMPANY_MAIN_VERSION', 'chaine', $this->version, '', 0, 'multicompany', 1),
  169. 2 => array('MULTICOMPANY_EXTERNAL_MODULES_SHARING', 'chaine', '', '', 0, 'multicompany', 0),
  170. //3 => array('MULTICOMPANY_NO_TOP_MENU_ENTITY_LABEL', 'chaine', 1, '', 0, 'multicompany', 0),
  171. 4 => array('MULTICOMPANY_PMP_PER_ENTITY_ENABLED', 'chaine', 1, '', 0, 'multicompany', 0)
  172. );
  173. // Boxes
  174. $this->boxes = array(); // List of boxes
  175. $r=0;
  176. // Permissions
  177. $this->rights = array();
  178. $r=0;
  179. $r++;
  180. $this->rights[$r][0] = 5001;
  181. $this->rights[$r][1] = 'Read entities (For superadmin users)';
  182. $this->rights[$r][2] = 'r';
  183. $this->rights[$r][3] = 0;
  184. $this->rights[$r][4] = 'read';
  185. $r++;
  186. $this->rights[$r][0] = 5002;
  187. $this->rights[$r][1] = 'Create/modify entities (For superadmin users)';
  188. $this->rights[$r][2] = 'w';
  189. $this->rights[$r][3] = 0;
  190. $this->rights[$r][4] = 'write';
  191. $r++;
  192. $this->rights[$r][0] = 5003;
  193. $this->rights[$r][1] = 'Delete entities (For superadmin users)';
  194. $this->rights[$r][2] = 'd';
  195. $this->rights[$r][3] = 0;
  196. $this->rights[$r][4] = 'delete';
  197. // Thirdparty sharing (501x)
  198. $r++;
  199. $this->rights[$r][0] = 5011;
  200. $this->rights[$r][1] = 'Read shared third parties';
  201. $this->rights[$r][2] = 'r';
  202. $this->rights[$r][3] = 0;
  203. $this->rights[$r][4] = 'thirdparty';
  204. $this->rights[$r][5] = 'read';
  205. $r++;
  206. $this->rights[$r][0] = 5012;
  207. $this->rights[$r][1] = 'Create/modify shared third parties';
  208. $this->rights[$r][2] = 'w';
  209. $this->rights[$r][3] = 0;
  210. $this->rights[$r][4] = 'thirdparty';
  211. $this->rights[$r][5] = 'write';
  212. $r++;
  213. $this->rights[$r][0] = 5013;
  214. $this->rights[$r][1] = 'Delete shared third parties';
  215. $this->rights[$r][2] = 'd';
  216. $this->rights[$r][3] = 0;
  217. $this->rights[$r][4] = 'thirdparty';
  218. $this->rights[$r][5] = 'delete';
  219. // Contact sharing (502x)
  220. $r++;
  221. $this->rights[$r][0] = 5021;
  222. $this->rights[$r][1] = 'Read shared contacts';
  223. $this->rights[$r][2] = 'r';
  224. $this->rights[$r][3] = 0;
  225. $this->rights[$r][4] = 'contact';
  226. $this->rights[$r][5] = 'read';
  227. $r++;
  228. $this->rights[$r][0] = 5022;
  229. $this->rights[$r][1] = 'Create/modify shared contacts';
  230. $this->rights[$r][2] = 'w';
  231. $this->rights[$r][3] = 0;
  232. $this->rights[$r][4] = 'contact';
  233. $this->rights[$r][5] = 'write';
  234. $r++;
  235. $this->rights[$r][0] = 5023;
  236. $this->rights[$r][1] = 'Delete shared contacts';
  237. $this->rights[$r][2] = 'd';
  238. $this->rights[$r][3] = 0;
  239. $this->rights[$r][4] = 'contact';
  240. $this->rights[$r][5] = 'delete';
  241. // Product/service sharing (503x)
  242. $r++;
  243. $this->rights[$r][0] = 5031;
  244. $this->rights[$r][1] = 'Read shared products/services';
  245. $this->rights[$r][2] = 'r';
  246. $this->rights[$r][3] = 0;
  247. $this->rights[$r][4] = 'product';
  248. $this->rights[$r][5] = 'read';
  249. $r++;
  250. $this->rights[$r][0] = 5032;
  251. $this->rights[$r][1] = 'Create/modify shared products/services';
  252. $this->rights[$r][2] = 'w';
  253. $this->rights[$r][3] = 0;
  254. $this->rights[$r][4] = 'product';
  255. $this->rights[$r][5] = 'write';
  256. $r++;
  257. $this->rights[$r][0] = 5033;
  258. $this->rights[$r][1] = 'Delete shared products/services';
  259. $this->rights[$r][2] = 'd';
  260. $this->rights[$r][3] = 0;
  261. $this->rights[$r][4] = 'product';
  262. $this->rights[$r][5] = 'delete';
  263. // Proposal sharing (504x)
  264. $r++;
  265. $this->rights[$r][0] = 5041;
  266. $this->rights[$r][1] = 'Read shared customer proposals';
  267. $this->rights[$r][2] = 'r';
  268. $this->rights[$r][3] = 0;
  269. $this->rights[$r][4] = 'propal';
  270. $this->rights[$r][5] = 'read';
  271. $r++;
  272. $this->rights[$r][0] = 5042;
  273. $this->rights[$r][1] = 'Create/modify shared customer proposals';
  274. $this->rights[$r][2] = 'w';
  275. $this->rights[$r][3] = 0;
  276. $this->rights[$r][4] = 'propal';
  277. $this->rights[$r][5] = 'write';
  278. $r++;
  279. $this->rights[$r][0] = 5043;
  280. $this->rights[$r][1] = 'Validate shared customer proposals';
  281. $this->rights[$r][2] = 'd';
  282. $this->rights[$r][3] = 0;
  283. $this->rights[$r][4] = 'propal_advance';
  284. $this->rights[$r][5] = 'validate';
  285. $r++;
  286. $this->rights[$r][0] = 5044;
  287. $this->rights[$r][1] = 'Send shared customer proposals';
  288. $this->rights[$r][2] = 'd';
  289. $this->rights[$r][3] = 0;
  290. $this->rights[$r][4] = 'propal_advance';
  291. $this->rights[$r][5] = 'send';
  292. $r++;
  293. $this->rights[$r][0] = 5045;
  294. $this->rights[$r][1] = 'Close shared customer proposals';
  295. $this->rights[$r][2] = 'd';
  296. $this->rights[$r][3] = 0;
  297. $this->rights[$r][4] = 'propal';
  298. $this->rights[$r][5] = 'close';
  299. $r++;
  300. $this->rights[$r][0] = 5046;
  301. $this->rights[$r][1] = 'Delete shared customer proposals';
  302. $this->rights[$r][2] = 'd';
  303. $this->rights[$r][3] = 0;
  304. $this->rights[$r][4] = 'propal';
  305. $this->rights[$r][5] = 'delete';
  306. // Order sharing (505x)
  307. $r++;
  308. $this->rights[$r][0] = 5051;
  309. $this->rights[$r][1] = 'Read shared customer orders';
  310. $this->rights[$r][2] = 'r';
  311. $this->rights[$r][3] = 0;
  312. $this->rights[$r][4] = 'order';
  313. $this->rights[$r][5] = 'read';
  314. $r++;
  315. $this->rights[$r][0] = 5052;
  316. $this->rights[$r][1] = 'Close shared customer orders';
  317. $this->rights[$r][2] = 'w';
  318. $this->rights[$r][3] = 0;
  319. $this->rights[$r][4] = 'order';
  320. $this->rights[$r][5] = 'write';
  321. $r++;
  322. $this->rights[$r][0] = 5054;
  323. $this->rights[$r][1] = 'Validate shared customer orders';
  324. $this->rights[$r][2] = 'd';
  325. $this->rights[$r][3] = 0;
  326. $this->rights[$r][4] = 'order_advance';
  327. $this->rights[$r][5] = 'validate';
  328. $r++;
  329. $this->rights[$r][0] = 5056;
  330. $this->rights[$r][1] = 'Send shared customer orders';
  331. $this->rights[$r][2] = 'd';
  332. $this->rights[$r][3] = 0;
  333. $this->rights[$r][4] = 'order_advance';
  334. $this->rights[$r][5] = 'send';
  335. $r++;
  336. $this->rights[$r][0] = 5057;
  337. $this->rights[$r][1] = 'Close shared customer orders';
  338. $this->rights[$r][2] = 'd';
  339. $this->rights[$r][3] = 0;
  340. $this->rights[$r][4] = 'order';
  341. $this->rights[$r][5] = 'close';
  342. $r++;
  343. $this->rights[$r][0] = 5058;
  344. $this->rights[$r][1] = 'Cancel shared customer orders';
  345. $this->rights[$r][2] = 'd';
  346. $this->rights[$r][3] = 0;
  347. $this->rights[$r][4] = 'order_advance';
  348. $this->rights[$r][5] = 'cancel';
  349. $r++;
  350. $this->rights[$r][0] = 5059;
  351. $this->rights[$r][1] = 'Delete shared customer orders';
  352. $this->rights[$r][2] = 'd';
  353. $this->rights[$r][3] = 0;
  354. $this->rights[$r][4] = 'order';
  355. $this->rights[$r][5] = 'delete';
  356. // Invoice sharing (506x)
  357. $r++;
  358. $this->rights[$r][0] = 5061;
  359. $this->rights[$r][1] = 'Read shared customer invoices';
  360. $this->rights[$r][2] = 'a';
  361. $this->rights[$r][3] = 0;
  362. $this->rights[$r][4] = 'invoice';
  363. $this->rights[$r][5] = 'read';
  364. $r++;
  365. $this->rights[$r][0] = 5062;
  366. $this->rights[$r][1] = 'Create/modify shared customer invoices';
  367. $this->rights[$r][2] = 'a';
  368. $this->rights[$r][3] = 0;
  369. $this->rights[$r][4] = 'invoice';
  370. $this->rights[$r][5] = 'write';
  371. // There is a particular permission for unvalidate because this may be not forbidden by some laws
  372. $r++;
  373. $this->rights[$r][0] = 5063;
  374. $this->rights[$r][1] = 'Devalidate shared customer invoices';
  375. $this->rights[$r][2] = 'a';
  376. $this->rights[$r][3] = 0;
  377. $this->rights[$r][4] = 'invoice_advance';
  378. $this->rights[$r][5] = 'unvalidate';
  379. $r++;
  380. $this->rights[$r][0] = 5064;
  381. $this->rights[$r][1] = 'Validate shared customer invoices';
  382. $this->rights[$r][2] = 'a';
  383. $this->rights[$r][3] = 0;
  384. $this->rights[$r][4] = 'invoice_advance';
  385. $this->rights[$r][5] = 'validate';
  386. $r++;
  387. $this->rights[$r][0] = 5065;
  388. $this->rights[$r][1] = 'Send shared customer invoices';
  389. $this->rights[$r][2] = 'a';
  390. $this->rights[$r][3] = 0;
  391. $this->rights[$r][4] = 'invoice_advance';
  392. $this->rights[$r][5] = 'send';
  393. $r++;
  394. $this->rights[$r][0] = 5066;
  395. $this->rights[$r][1] = 'Issue payments on shared customer invoices';
  396. $this->rights[$r][2] = 'a';
  397. $this->rights[$r][3] = 0;
  398. $this->rights[$r][4] = 'invoice';
  399. $this->rights[$r][5] = 'payment';
  400. $r++;
  401. $this->rights[$r][0] = 5067;
  402. $this->rights[$r][1] = 'Re-open a fully paid shared customer invoices';
  403. $this->rights[$r][2] = 'r';
  404. $this->rights[$r][3] = 0;
  405. $this->rights[$r][4] = 'invoice_advance';
  406. $this->rights[$r][5] = 'reopen';
  407. $r++;
  408. $this->rights[$r][0] = 5069;
  409. $this->rights[$r][1] = 'Delete shared customer invoices';
  410. $this->rights[$r][2] = 'a';
  411. $this->rights[$r][3] = 0;
  412. $this->rights[$r][4] = 'invoice';
  413. $this->rights[$r][5] = 'delete';
  414. // Main menu entries
  415. $this->menus = array(); // List of menus to add
  416. $r=0;
  417. }
  418. /**
  419. * Function called when module is enabled.
  420. * The init function add constants, boxes, permissions and menus (defined in constructor) into Dolibarr database.
  421. * It also creates data directories.
  422. *
  423. * @param string $options Options when enabling module ('', 'newboxdefonly', 'noboxes')
  424. * 'noboxes' = Do not insert boxes
  425. * 'newboxdefonly' = For boxes, insert def of boxes only and not boxes activation
  426. * @param int $force_entity Force current entity
  427. * @return int 1 if OK, 0 if KO
  428. */
  429. function init($options = '', $force_entity = null)
  430. {
  431. $sql = array();
  432. $result=$this->load_tables();
  433. $result=$this->setSuperAdmin();
  434. $result=$this->setFirstEntity();
  435. return $this->_init($sql, $options, $force_entity);
  436. }
  437. /**
  438. * Function called when module is disabled.
  439. * Remove from database constants, boxes and permissions from Dolibarr database.
  440. * Data directories are not deleted.
  441. *
  442. * @param string $options Options when enabling module ('', 'noboxes')
  443. * @return int 1 if OK, 0 if KO
  444. */
  445. function remove($options = '')
  446. {
  447. $sql = array(
  448. "DELETE FROM ".MAIN_DB_PREFIX."const WHERE name = ".$this->db->encrypt('MAIN_MODULE_MULTICOMPANY_CSS', 1),
  449. "DELETE FROM ".MAIN_DB_PREFIX."const WHERE name = ".$this->db->encrypt('MAIN_MODULE_MULTICOMPANY_LOGIN', 1)
  450. );
  451. return $this->_remove($sql, $options);
  452. }
  453. /**
  454. * Create tables and keys required by module
  455. * This function is called by this->init.
  456. * @return int <=0 if KO, >0 if OK
  457. */
  458. function load_tables()
  459. {
  460. return $this->_load_tables('/multicompany/sql/');
  461. }
  462. /**
  463. * Set the first entity
  464. *
  465. * @return int
  466. */
  467. function setSuperAdmin()
  468. {
  469. global $user;
  470. $sql = "SELECT count(rowid) FROM ".MAIN_DB_PREFIX."user";
  471. $sql.= " WHERE admin = 1 AND entity = 0";
  472. $resql = $this->db->query($sql);
  473. if ($resql) {
  474. $num = $this->db->fetch_array($resql);
  475. } else {
  476. dol_print_error($this->db);
  477. }
  478. $this->db->free($resql);
  479. if (empty($num[0])) {
  480. $this->db->begin();
  481. $sql = "UPDATE ".MAIN_DB_PREFIX."user SET entity = 0";
  482. $sql.= " WHERE admin = 1 AND rowid IN (1, ".$user->id.")";
  483. if ($this->db->query($sql)) {
  484. $this->db->commit();
  485. return 1;
  486. } else {
  487. $this->error = $this->db->lasterror;
  488. $this->db->rollback();
  489. return -1;
  490. }
  491. } else {
  492. return 0;
  493. }
  494. }
  495. /**
  496. * Set the first entity
  497. *
  498. * @return int
  499. */
  500. function setFirstEntity()
  501. {
  502. global $user, $langs;
  503. $langs->load('multicompany@multicompany');
  504. $sql = 'SELECT count(rowid) FROM '.MAIN_DB_PREFIX.'entity';
  505. $resql = $this->db->query($sql);
  506. if ($resql) {
  507. $num = $this->db->fetch_array($resql);
  508. } else {
  509. dol_print_error($this->db);
  510. }
  511. $this->db->free($resql);
  512. if (empty($num[0])) {
  513. $this->db->begin();
  514. $now = dol_now();
  515. $label = $langs->trans("MasterEntity");
  516. $description = $langs->trans("MasterEntityDesc");
  517. $sql = "INSERT INTO ".MAIN_DB_PREFIX."entity (";
  518. $sql.= "label";
  519. $sql.= ", description";
  520. $sql.= ", datec";
  521. $sql.= ", fk_user_creat";
  522. $sql.= ") VALUES (";
  523. $sql.= "'".$this->db->escape($label)."'";
  524. $sql.= ", '".$this->db->escape($description)."'";
  525. $sql.= ", '".$this->db->idate($now)."'";
  526. $sql.= ", ".$user->id;
  527. $sql.= ")";
  528. if ($this->db->query($sql)) {
  529. $this->db->commit();
  530. return 1;
  531. } else {
  532. $this->error = $this->db->lasterror;
  533. $this->db->rollback();
  534. return -1;
  535. }
  536. } else {
  537. return 0;
  538. }
  539. }
  540. }