modTakePos.class.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. <?php
  2. /* Copyright (C) 2004-2018 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2018 SuperAdmin
  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, see <https://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \defgroup takepos Module TakePos
  20. * \brief TakePos module descriptor.
  21. *
  22. * \file htdocs/core/modules/modTakePos.class.php
  23. * \ingroup takepos
  24. * \brief Description and activation file for the module TakePos
  25. */
  26. include_once DOL_DOCUMENT_ROOT.'/core/modules/DolibarrModules.class.php';
  27. include_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
  28. /**
  29. * Class to describe and enable module TakePos
  30. */
  31. class modTakePos extends DolibarrModules
  32. {
  33. /**
  34. * Constructor. Define names, constants, directories, boxes, permissions
  35. *
  36. * @param DoliDB $db Database handler
  37. */
  38. public function __construct($db)
  39. {
  40. global $langs, $conf;
  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 = 50150;
  45. // Key text used to identify module (for permissions, menus, etc...)
  46. $this->rights_class = 'takepos';
  47. // Family can be 'crm','financial','hr','projects','products','ecm','technic','interface','other'
  48. // It is used to group modules by family in module setup page
  49. $this->family = "portal";
  50. // Module position in the family on 2 digits ('01', '10', '20', ...)
  51. $this->module_position = '60';
  52. // Gives the possibility to the module, to provide his own family info and position of this family (Overwrite $this->family and $this->module_position. Avoid this)
  53. //$this->familyinfo = array('myownfamily' => array('position' => '01', 'label' => $langs->trans("MyOwnFamily")));
  54. // Module label (no space allowed), used if translation string 'ModuleTakePosName' not found (MyModue is name of module).
  55. $this->name = preg_replace('/^mod/i', '', get_class($this));
  56. // Module description, used if translation string 'ModuleTakePosDesc' not found (MyModue is name of module).
  57. $this->description = "Point of sales module (Touch Screen POS)";
  58. // Used only if file README.md and README-LL.md not found.
  59. $this->descriptionlong = "Point Of Sales (compliant with touch screen)";
  60. // Possible values for version are: 'development', 'experimental', 'dolibarr', 'dolibarr_deprecated' or a version string like 'x.y.z'
  61. $this->version = 'dolibarr';
  62. // Key used in llx_const table to save module status enabled/disabled (where TAKEPOS is value of property name of module in uppercase)
  63. $this->const_name = 'MAIN_MODULE_'.strtoupper($this->name);
  64. // Name of image file used for this module.
  65. // If file is in theme/yourtheme/img directory under name object_pictovalue.png, use this->picto='pictovalue'
  66. // If file is in module/img directory under name object_pictovalue.png, use this->picto='pictovalue@module'
  67. $this->picto = 'cash-register';
  68. // Defined all module parts (triggers, login, substitutions, menus, css, etc...)
  69. // for default path (eg: /takepos/core/xxxxx) (0=disable, 1=enable)
  70. // for specific path of parts (eg: /takepos/core/modules/barcode)
  71. // for specific css file (eg: /takepos/css/takepos.css.php)
  72. $this->module_parts = array(
  73. 'triggers' => 0, // Set this to 1 if module has its own trigger directory (core/triggers)
  74. 'login' => 0, // Set this to 1 if module has its own login method file (core/login)
  75. 'substitutions' => 1, // Set this to 1 if module has its own substitution function file (core/substitutions)
  76. 'menus' => 0, // Set this to 1 if module has its own menus handler directory (core/menus)
  77. 'theme' => 0, // Set this to 1 if module has its own theme directory (theme)
  78. 'tpl' => 0, // Set this to 1 if module overwrite template dir (core/tpl)
  79. 'barcode' => 0, // Set this to 1 if module has its own barcode directory (core/modules/barcode)
  80. 'models' => 0, // Set this to 1 if module has its own models directory (core/modules/xxx)
  81. 'hooks' => array() // Set here all hooks context managed by module. To find available hook context, make a "grep -r '>initHooks(' *" on source code. You can also set hook context 'all'
  82. );
  83. // Data directories to create when module is enabled.
  84. // Example: this->dirs = array("/takepos/temp","/takepos/subdir");
  85. $this->dirs = array();
  86. // Config pages. Put here list of php page, stored into takepos/admin directory, to use to setup module.
  87. $this->config_page_url = array("setup.php@takepos");
  88. // Dependencies
  89. $this->hidden = false; // A condition to hide module
  90. // List of module class names as string that must be enabled if this module is enabled. Example: array('always'=>array('modModuleToEnable1','modModuleToEnable2'), 'FR'=>array('modModuleToEnableFR'...))
  91. $this->depends = array('always'=>array("modBanque", "modFacture", "modProduct", "modCategorie"), 'FR'=>array('modBlockedLog'));
  92. $this->requiredby = array(); // List of module ids to disable if this one is disabled
  93. $this->conflictwith = array(); // List of module class names as string this module is in conflict with
  94. $this->langfiles = array("cashdesk");
  95. $this->phpmin = array(7, 0); // Minimum version of PHP required by module
  96. $this->need_dolibarr_version = array(4, 0); // Minimum version of Dolibarr required by module
  97. $this->warnings_activation = array('FR'=>'WarningNoteModulePOSForFrenchLaw'); // Warning to show when we activate module. array('always'='text') or array('FR'='text')
  98. $this->warnings_activation_ext = array(); // Warning to show when we activate an external module. array('always'='text') or array('FR'='textfr','ES'='textes'...)
  99. //$this->automatic_activation = array('FR'=>'TakePosWasAutomaticallyActivatedBecauseOfYourCountryChoice');
  100. //$this->always_enabled = true; // If true, can't be disabled
  101. // Constants
  102. // List of particular constants to add when module is enabled (key, 'chaine', value, desc, visible, 'current' or 'allentities', deleteonunactive)
  103. // Example: $this->const=array(0=>array('TAKEPOS_MYNEWCONST1','chaine','myvalue','This is a constant to add',1),
  104. // 1=>array('TAKEPOS_MYNEWCONST2','chaine','myvalue','This is another constant to add',0, 'current', 1)
  105. // );
  106. $this->const = array(
  107. //1=>array('TAKEPOS_MYCONSTANT', 'chaine', 'avalue', 'This is a constant to add', 1, 'allentities', 1)
  108. );
  109. if (!isset($conf->takepos) || !isset($conf->takepos->enabled)) {
  110. $conf->takepos = new stdClass();
  111. $conf->takepos->enabled = 0;
  112. }
  113. // Array to add new pages in new tabs
  114. $this->tabs = array();
  115. // Example:
  116. // $this->tabs[] = array('data'=>'objecttype:+tabname1:Title1:mylangfile@takepos:$user->rights->takepos->read:/takepos/mynewtab1.php?id=__ID__'); // To add a new tab identified by code tabname1
  117. // $this->tabs[] = array('data'=>'objecttype:+tabname2:SUBSTITUTION_Title2:mylangfile@takepos:$user->rights->othermodule->read:/takepos/mynewtab2.php?id=__ID__', // To add another new tab identified by code tabname2. Label will be result of calling all substitution functions on 'Title2' key.
  118. // $this->tabs[] = array('data'=>'objecttype:-tabname:NU:conditiontoremove'); // To remove an existing tab identified by code tabname
  119. //
  120. // Where objecttype can be
  121. // 'categories_x' to add a tab in category view (replace 'x' by type of category (0=product, 1=supplier, 2=customer, 3=member)
  122. // 'contact' to add a tab in contact view
  123. // 'contract' to add a tab in contract view
  124. // 'group' to add a tab in group view
  125. // 'intervention' to add a tab in intervention view
  126. // 'invoice' to add a tab in customer invoice view
  127. // 'invoice_supplier' to add a tab in supplier invoice view
  128. // 'member' to add a tab in fundation member view
  129. // 'opensurveypoll' to add a tab in opensurvey poll view
  130. // 'order' to add a tab in sales order view
  131. // 'order_supplier' to add a tab in supplier order view
  132. // 'payment' to add a tab in payment view
  133. // 'payment_supplier' to add a tab in supplier payment view
  134. // 'product' to add a tab in product view
  135. // 'propal' to add a tab in propal view
  136. // 'project' to add a tab in project view
  137. // 'stock' to add a tab in stock view
  138. // 'thirdparty' to add a tab in third party view
  139. // 'user' to add a tab in user view
  140. // Dictionaries
  141. $this->dictionaries = array();
  142. // Boxes/Widgets
  143. // Add here list of php file(s) stored in takepos/core/boxes that contains class to show a widget.
  144. $this->boxes = array(
  145. //0=>array('file'=>'takeposwidget1.php@takepos','note'=>'Widget provided by TakePos','enabledbydefaulton'=>'Home'),
  146. //1=>array('file'=>'takeposwidget2.php@takepos','note'=>'Widget provided by TakePos'),
  147. //2=>array('file'=>'takeposwidget3.php@takepos','note'=>'Widget provided by TakePos')
  148. );
  149. // Cronjobs (List of cron jobs entries to add when module is enabled)
  150. // unit_frequency must be 60 for minute, 3600 for hour, 86400 for day, 604800 for week
  151. $this->cronjobs = array(
  152. //0=>array('label'=>'MyJob label', 'jobtype'=>'method', 'class'=>'/takepos/class/myobject.class.php', 'objectname'=>'MyObject', 'method'=>'doScheduledJob', 'parameters'=>'', 'comment'=>'Comment', 'frequency'=>2, 'unitfrequency'=>3600, 'status'=>0, 'test'=>true)
  153. );
  154. // Example: $this->cronjobs=array(0=>array('label'=>'My label', 'jobtype'=>'method', 'class'=>'/dir/class/file.class.php', 'objectname'=>'MyClass', 'method'=>'myMethod', 'parameters'=>'param1, param2', 'comment'=>'Comment', 'frequency'=>2, 'unitfrequency'=>3600, 'status'=>0, 'test'=>true),
  155. // 1=>array('label'=>'My label', 'jobtype'=>'command', 'command'=>'', 'parameters'=>'param1, param2', 'comment'=>'Comment', 'frequency'=>1, 'unitfrequency'=>3600*24, 'status'=>0, 'test'=>true)
  156. // );
  157. // Permissions
  158. $this->rights = array(); // Permission array used by this module
  159. $r = 0;
  160. $r++;
  161. $this->rights[$r][0] = 50151;
  162. $this->rights[$r][1] = 'Use Point Of Sale (record a sale, add products, record payment)';
  163. $this->rights[$r][2] = 'a';
  164. $this->rights[$r][3] = 0;
  165. $this->rights[$r][4] = 'run';
  166. $r++;
  167. $this->rights[$r][0] = 50152;
  168. $this->rights[$r][1] = 'Can modify added sales lines (prices, discount)';
  169. $this->rights[$r][2] = 'a';
  170. $this->rights[$r][3] = 0;
  171. $this->rights[$r][4] = 'editlines';
  172. $r++;
  173. $this->rights[$r][0] = 50153;
  174. $this->rights[$r][1] = 'Edit ordered sales lines (useful only when option "Order printers" has been enabled). Allow to edit sales lines even after the order has been printed';
  175. $this->rights[$r][2] = 'a';
  176. $this->rights[$r][3] = 0;
  177. $this->rights[$r][4] = 'editorderedlines';
  178. // Main menu entries
  179. $this->menu = array(); // List of menus to add
  180. $r = 0;
  181. // Add here entries to declare new menus
  182. /* BEGIN MODULEBUILDER TOPMENU */
  183. $this->menu[$r++] = array('fk_menu'=>'', // '' if this is a top menu. For left menu, use 'fk_mainmenu=xxx' or 'fk_mainmenu=xxx,fk_leftmenu=yyy' where xxx is mainmenucode and yyy is a leftmenucode
  184. 'type'=>'top', // This is a Top menu entry
  185. 'titre'=>'PointOfSaleShort',
  186. 'mainmenu'=>'takepos',
  187. 'leftmenu'=>'',
  188. 'prefix' => img_picto('', $this->picto, 'class="pictofixedwidth"'),
  189. 'url'=>'/takepos/index.php',
  190. 'langs'=>'cashdesk', // Lang file to use (without .lang) by module. File must be in langs/code_CODE/ directory.
  191. 'position'=>1000 + $r,
  192. 'enabled'=>'isModEnabled("takepos")', // Define condition to show or hide menu entry. Use '$conf->takepos->enabled' if entry must be visible if module is enabled.
  193. 'perms'=>'$user->hasRight("takepos", "run")', // Use 'perms'=>'$user->rights->takepos->level1->level2' if you want your menu with a permission rules
  194. 'target'=>'takepos',
  195. 'user'=>2); // 0=Menu for internal users, 1=external users, 2=both
  196. /* END MODULEBUILDER TOPMENU */
  197. /* BEGIN MODULEBUILDER LEFTMENU MYOBJECT
  198. $this->menu[$r++]=array( 'fk_menu'=>'fk_mainmenu=takepos', // '' if this is a top menu. For left menu, use 'fk_mainmenu=xxx' or 'fk_mainmenu=xxx,fk_leftmenu=yyy' where xxx is mainmenucode and yyy is a leftmenucode
  199. 'type'=>'left', // This is a Left menu entry
  200. 'titre'=>'List MyObject',
  201. 'mainmenu'=>'takepos',
  202. 'leftmenu'=>'takepos_myobject_list',
  203. 'url'=>'/takepos/myobject_list.php',
  204. 'langs'=>'cashdesk', // Lang file to use (without .lang) by module. File must be in langs/code_CODE/ directory.
  205. 'position'=>1000+$r,
  206. 'enabled'=>'$conf->takepos->enabled', // Define condition to show or hide menu entry. Use '$conf->takepos->enabled' if entry must be visible if module is enabled. Use '$leftmenu==\'system\'' to show if leftmenu system is selected.
  207. 'perms'=>'1', // Use 'perms'=>'$user->rights->takepos->level1->level2' if you want your menu with a permission rules
  208. 'target'=>'',
  209. 'user'=>2); // 0=Menu for internal users, 1=external users, 2=both
  210. $this->menu[$r++]=array( 'fk_menu'=>'fk_mainmenu=takepos,fk_leftmenu=takepos', // '' if this is a top menu. For left menu, use 'fk_mainmenu=xxx' or 'fk_mainmenu=xxx,fk_leftmenu=yyy' where xxx is mainmenucode and yyy is a leftmenucode
  211. 'type'=>'left', // This is a Left menu entry
  212. 'titre'=>'New MyObject',
  213. 'mainmenu'=>'takepos',
  214. 'leftmenu'=>'takepos_myobject_new',
  215. 'url'=>'/takepos/myobject_page.php?action=create',
  216. 'langs'=>'cashdesk', // Lang file to use (without .lang) by module. File must be in langs/code_CODE/ directory.
  217. 'position'=>1000+$r,
  218. 'enabled'=>'$conf->takepos->enabled', // Define condition to show or hide menu entry. Use '$conf->takepos->enabled' if entry must be visible if module is enabled. Use '$leftmenu==\'system\'' to show if leftmenu system is selected.
  219. 'perms'=>'1', // Use 'perms'=>'$user->rights->takepos->level1->level2' if you want your menu with a permission rules
  220. 'target'=>'',
  221. 'user'=>2); // 0=Menu for internal users, 1=external users, 2=both
  222. END MODULEBUILDER LEFTMENU MYOBJECT */
  223. }
  224. /**
  225. * Function called when module is enabled.
  226. * The init function add constants, boxes, permissions and menus (defined in constructor) into Dolibarr database.
  227. * It also creates data directories
  228. *
  229. * @param string $options Options when enabling module ('', 'noboxes')
  230. * @return int 1 if OK, 0 if KO
  231. */
  232. public function init($options = '')
  233. {
  234. global $conf, $langs, $user, $mysoc;
  235. $langs->load("cashdesk");
  236. dolibarr_set_const($this->db, "TAKEPOS_PRINT_METHOD", "browser", 'chaine', 0, '', $conf->entity);
  237. // Default customer for Point of sale
  238. if (!getDolGlobalInt('CASHDESK_ID_THIRDPARTY1')) { // If a customer has already ben set into the TakePos setup page
  239. $societe = new Societe($this->db);
  240. $nametouse = $langs->trans("DefaultPOSThirdLabel");
  241. $searchcompanyid = $societe->fetch(0, $nametouse);
  242. if ($searchcompanyid == 0) {
  243. $societe->name = $nametouse;
  244. $societe->client = 1;
  245. $societe->code_client = -1;
  246. $societe->code_fournisseur = -1;
  247. $societe->note_private = "Default customer automaticaly created by Point Of Sale module activation. Can be used as the default generic customer in the Point Of Sale setup. Can also be edited or removed if you don't need a generic customer.";
  248. $searchcompanyid = $societe->create($user);
  249. }
  250. if ($searchcompanyid > 0) {
  251. // We already have or we have create a thirdparty with id = $searchcompanyid, so we link use it into setup
  252. dolibarr_set_const($this->db, "CASHDESK_ID_THIRDPARTY1", $searchcompanyid, 'chaine', 0, '', $conf->entity);
  253. } else {
  254. setEventMessages($societe->error, $societe->errors, 'errors');
  255. }
  256. }
  257. //Create category if not exists
  258. $categories = new Categorie($this->db);
  259. $cate_arbo = $categories->get_full_arbo('product', 0, 1);
  260. if (is_array($cate_arbo)) {
  261. if (!count($cate_arbo)) {
  262. $category = new Categorie($this->db);
  263. $category->label = $langs->trans("DefaultPOSCatLabel");
  264. $category->type = Categorie::TYPE_PRODUCT;
  265. $result = $category->create($user);
  266. if ($result > 0) {
  267. /* TODO Create a generic product only if there is no product yet. If 0 product, we create 1. If there is already product, it is better to show a message to ask to add product in the category */
  268. /*
  269. $product = new Product($this->db);
  270. $product->status = 1;
  271. $product->ref = "takepos";
  272. $product->label = $langs->trans("DefaultPOSProductLabel");
  273. $product->create($user);
  274. $product->setCategories($result);
  275. */
  276. } else {
  277. setEventMessages($category->error, $category->errors, 'errors');
  278. }
  279. }
  280. }
  281. //Create cash account if not exists
  282. if (!getDolGlobalInt('CASHDESK_ID_BANKACCOUNT_CASH1')) {
  283. require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
  284. $cashaccount = new Account($this->db);
  285. $searchaccountid = $cashaccount->fetch(0, "CASH-POS");
  286. if ($searchaccountid == 0) {
  287. $cashaccount->ref = "CASH-POS";
  288. $cashaccount->label = $langs->trans("DefaultCashPOSLabel");
  289. $cashaccount->courant = 2;
  290. $cashaccount->country_id = $mysoc->country_id ? $mysoc->country_id : 1;
  291. $cashaccount->date_solde = dol_now();
  292. $searchaccountid = $cashaccount->create($user);
  293. }
  294. if ($searchaccountid > 0) {
  295. dolibarr_set_const($this->db, "CASHDESK_ID_BANKACCOUNT_CASH1", $searchaccountid, 'chaine', 0, '', $conf->entity);
  296. } else {
  297. setEventMessages($cashaccount->error, $cashaccount->errors, 'errors');
  298. }
  299. }
  300. $result = $this->_load_tables('/install/mysql/', 'takepos');
  301. if ($result < 0) {
  302. return -1; // Do not activate module if error 'not allowed' returned when loading module SQL queries (the _load_table run sql with run_sql with the error allowed parameter set to 'default')
  303. }
  304. // Clean before activation
  305. $this->remove($options);
  306. $sql = array();
  307. return $this->_init($sql, $options);
  308. }
  309. /**
  310. * Function called when module is disabled.
  311. * Remove from database constants, boxes and permissions from Dolibarr database.
  312. * Data directories are not deleted
  313. *
  314. * @param string $options Options when enabling module ('', 'noboxes')
  315. * @return int 1 if OK, 0 if KO
  316. */
  317. public function remove($options = '')
  318. {
  319. $sql = array();
  320. return $this->_remove($sql, $options);
  321. }
  322. }