modStock.class.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. <?php
  2. /* Copyright (C) 2003-2006 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2008 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005-2009 Regis Houssin <regis.houssin@inodbox.com>
  5. * Copyright (C) 2012 Juanjo Menent <jmenent@2byte.es>
  6. * Copyright (C) 2021 Ferran Marcet <fmarcet@2byte.es>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. */
  21. /**
  22. * \defgroup stock Module stocks
  23. * \brief Module pour gerer la tenue de stocks produits
  24. * \file htdocs/core/modules/modStock.class.php
  25. * \ingroup stock
  26. * \brief Description and activation file for the module Stock
  27. */
  28. include_once DOL_DOCUMENT_ROOT.'/core/modules/DolibarrModules.class.php';
  29. /**
  30. * Class to describe and enable module Stock
  31. */
  32. class modStock extends DolibarrModules
  33. {
  34. /**
  35. * Constructor. Define names, constants, directories, boxes, permissions
  36. *
  37. * @param DoliDB $db Database handler
  38. */
  39. public function __construct($db)
  40. {
  41. global $conf, $langs;
  42. $this->db = $db;
  43. $this->numero = 52;
  44. $this->family = "products";
  45. $this->module_position = '39';
  46. // Module label (no space allowed), used if translation string 'ModuleXXXName' not found (where XXX is value of numeric property 'numero' of module)
  47. $this->name = preg_replace('/^mod/i', '', get_class($this));
  48. $this->description = "Gestion des stocks";
  49. // Possible values for version are: 'development', 'experimental', 'dolibarr' or version
  50. $this->version = 'dolibarr';
  51. $this->const_name = 'MAIN_MODULE_'.strtoupper($this->name);
  52. $this->picto = 'stock';
  53. // Data directories to create when module is enabled
  54. $this->dirs = array("/stock/temp");
  55. $this->config_page_url = array("stock.php");
  56. // Dependencies
  57. $this->hidden = false; // A condition to hide module
  58. $this->depends = array("modProduct"); // List of module class names as string that must be enabled if this module is enabled
  59. $this->requiredby = array("modProductBatch"); // List of module ids to disable if this one is disabled
  60. $this->conflictwith = array(); // List of module class names as string this module is in conflict with
  61. $this->phpmin = array(5, 6); // Minimum version of PHP required by module
  62. $this->langfiles = array("stocks");
  63. // Constants
  64. $this->const = array();
  65. $r = 0;
  66. $this->const[$r] = array('STOCK_ALLOW_NEGATIVE_TRANSFER', 'chaine', '1', '', 1);
  67. $r++;
  68. $this->const[$r][0] = "STOCK_ADDON_PDF";
  69. $this->const[$r][1] = "chaine";
  70. $this->const[$r][2] = "standard";
  71. $this->const[$r][3] = 'Name of PDF model of stock';
  72. $this->const[$r][4] = 0;
  73. $r++;
  74. $this->const[$r][0] = "MOUVEMENT_ADDON_PDF";
  75. $this->const[$r][1] = "chaine";
  76. $this->const[$r][2] = "stdmovement";
  77. $this->const[$r][3] = 'Name of PDF model of stock mouvement';
  78. $this->const[$r][4] = 0;
  79. $r++;
  80. $this->const[$r][0] = "STOCK_ADDON_PDF_ODT_PATH";
  81. $this->const[$r][1] = "chaine";
  82. $this->const[$r][2] = "DOL_DATA_ROOT/doctemplates/stocks";
  83. $this->const[$r][3] = "";
  84. $this->const[$r][4] = 0;
  85. $r++;
  86. $this->const[$r][0] = "MOUVEMENT_ADDON_PDF_ODT_PATH";
  87. $this->const[$r][1] = "chaine";
  88. $this->const[$r][2] = "DOL_DATA_ROOT/doctemplates/stocks/mouvements";
  89. $this->const[$r][3] = "";
  90. $this->const[$r][4] = 0;
  91. // Boxes
  92. $this->boxes = array();
  93. // Permissions
  94. $this->rights = array();
  95. $this->rights_class = 'stock';
  96. $this->rights[0][0] = 1001;
  97. $this->rights[0][1] = 'Lire les stocks';
  98. $this->rights[0][2] = 'r';
  99. $this->rights[0][3] = 0;
  100. $this->rights[0][4] = 'lire';
  101. $this->rights[0][5] = '';
  102. $this->rights[1][0] = 1002;
  103. $this->rights[1][1] = 'Creer/Modifier les stocks';
  104. $this->rights[1][2] = 'w';
  105. $this->rights[1][3] = 0;
  106. $this->rights[1][4] = 'creer';
  107. $this->rights[1][5] = '';
  108. $this->rights[2][0] = 1003;
  109. $this->rights[2][1] = 'Supprimer les stocks';
  110. $this->rights[2][2] = 'd';
  111. $this->rights[2][3] = 0;
  112. $this->rights[2][4] = 'supprimer';
  113. $this->rights[2][5] = '';
  114. $this->rights[3][0] = 1004;
  115. $this->rights[3][1] = 'Lire mouvements de stocks';
  116. $this->rights[3][2] = 'r';
  117. $this->rights[3][3] = 0;
  118. $this->rights[3][4] = 'mouvement';
  119. $this->rights[3][5] = 'lire';
  120. $this->rights[4][0] = 1005;
  121. $this->rights[4][1] = 'Creer/modifier mouvements de stocks';
  122. $this->rights[4][2] = 'w';
  123. $this->rights[4][3] = 0;
  124. $this->rights[4][4] = 'mouvement';
  125. $this->rights[4][5] = 'creer';
  126. $this->rights[5][0] = 1011;
  127. $this->rights[5][1] = 'inventoryReadPermission'; // Permission label
  128. $this->rights[5][3] = 0; // Permission by default for new user (0/1)
  129. $this->rights[5][4] = 'inventory_advance'; // In php code, permission will be checked by test if ($user->rights->permkey->level1->level2)
  130. $this->rights[5][5] = 'read'; // In php code, permission will be checked by test if ($user->rights->permkey->level1->level2)
  131. $this->rights[6][0] = 1012;
  132. $this->rights[6][1] = 'inventoryCreatePermission'; // Permission label
  133. $this->rights[6][3] = 0; // Permission by default for new user (0/1)
  134. $this->rights[6][4] = 'inventory_advance'; // In php code, permission will be checked by test if ($user->rights->permkey->level1->level2)
  135. $this->rights[6][5] = 'write'; // In php code, permission will be checked by test if ($user->rights->permkey->level1->level2)
  136. if ($conf->global->MAIN_FEATURES_LEVEL >= 2) {
  137. $this->rights[8][0] = 1014;
  138. $this->rights[8][1] = 'inventoryValidatePermission'; // Permission label
  139. $this->rights[8][3] = 0; // Permission by default for new user (0/1)
  140. $this->rights[8][4] = 'inventory_advance'; // In php code, permission will be checked by test if ($user->rights->permkey->level1->level2)
  141. $this->rights[8][5] = 'validate'; // In php code, permission will be checked by test if ($user->rights->permkey->level1->level2)
  142. $this->rights[9][0] = 1015;
  143. $this->rights[9][1] = 'inventoryChangePMPPermission'; // Permission label
  144. $this->rights[9][3] = 0; // Permission by default for new user (0/1)
  145. $this->rights[9][4] = 'inventory_advance'; // In php code, permission will be checked by test if ($user->rights->permkey->level1->level2)
  146. $this->rights[9][5] = 'changePMP'; // In php code, permission will be checked by test if ($user->rights->permkey->level1->level2)
  147. $this->rights[10][0] = 1016;
  148. $this->rights[10][1] = 'inventoryDeletePermission'; // Permission label
  149. $this->rights[10][3] = 0; // Permission by default for new user (0/1)
  150. $this->rights[10][4] = 'inventory_advance'; // In php code, permission will be checked by test if ($user->rights->permkey->level1->level2)
  151. $this->rights[10][5] = 'delete'; // In php code, permission will be checked by test if ($user->rights->permkey->level1->level2)
  152. }
  153. // Main menu entries
  154. $this->menu = array(); // List of menus to add
  155. $r = 0;
  156. // Menus
  157. //-------
  158. $this->menu = 1; // This module add menu entries. They are coded into menu manager.
  159. // Exports
  160. //--------
  161. $r = 0;
  162. // Export warehouses
  163. $r++;
  164. $this->export_code[$r] = $this->rights_class.'_emplacement';
  165. $this->export_label[$r] = "Warehouses"; // Translation key (used only if key ExportDataset_xxx_z not found)
  166. $this->export_icon[$r] = "warehouse";
  167. $this->export_permission[$r] = array(array("stock", "lire"));
  168. $this->export_fields_array[$r] = array(
  169. 'e.rowid'=>'IdWarehouse', 'e.ref'=>'LocationSummary', 'e.description'=>'DescWareHouse', 'e.lieu'=>'LieuWareHouse', 'e.address'=>'Address', 'e.zip'=>'Zip', 'e.town'=>'Town',
  170. 'd.code_departement'=>'Departement', 'c.code'=>'CountryCode',
  171. 'e.phone'=>'Phone', 'e.fax'=>'Fax', 'e.statut'=>'Status', 'pe.rowid'=>'ParentWarehouse', 'pe.ref'=>'LocationSummary'
  172. );
  173. $this->export_TypeFields_array[$r] = array(
  174. 'e.ref'=>'Text', 'e.description'=>'Text', 'e.lieu'=>'Text', 'e.address'=>'Text', 'e.zip'=>'Text', 'e.town'=>'Text',
  175. 'd.code_departement'=>'List:c_departements:code_departement:code_departement:', 'c.code'=>'List:c_country:code:code:',
  176. 'e.phone'=>'Text', 'e.fax'=>'Text', 'e.statut'=>'Text', 'pe.rowid'=>'List:entrepot:ref:rowid:stock', 'pe.ref'=>'Text'
  177. );
  178. $this->export_entities_array[$r] = array(); // We define here only fields that use another icon that the one defined into export_icon
  179. $this->export_aggregate_array[$r] = array();
  180. $keyforselect = 'warehouse';
  181. $keyforelement = 'warehouse';
  182. $keyforaliasextra = 'extra';
  183. include DOL_DOCUMENT_ROOT.'/core/extrafieldsinexport.inc.php';
  184. $this->export_sql_start[$r] = 'SELECT DISTINCT ';
  185. $this->export_sql_end[$r] = ' FROM '.MAIN_DB_PREFIX.'entrepot as e';
  186. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_departements as d ON d.rowid = e.fk_departement';
  187. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_country as c ON c.rowid = e.fk_pays';
  188. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'entrepot as pe ON pe.rowid = e.fk_parent';
  189. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'entrepot_extrafields as extra ON extra.fk_object = e.rowid';
  190. $this->export_sql_end[$r] .= ' WHERE e.entity IN ('.getEntity('stock').')';
  191. // Export stock (without batch number)
  192. $r++;
  193. $this->export_code[$r] = $this->rights_class.'_emplacement_product';
  194. $this->export_label[$r] = "Stocks"; // Translation key (used only if key ExportDataset_xxx_z not found)
  195. $this->export_icon[$r] = "warehouse";
  196. $this->export_permission[$r] = array(array("stock", "lire"));
  197. $this->export_fields_array[$r] = array(
  198. 'e.rowid'=>'IdWarehouse', 'e.ref'=>'LocationSummary', 'e.description'=>'DescWareHouse', 'e.lieu'=>'LieuWareHouse', 'e.address'=>'Address', 'e.zip'=>'Zip', 'e.town'=>'Town',
  199. 'p.rowid'=>"ProductId", 'p.ref'=>"Ref", 'p.fk_product_type'=>"Type", 'p.label'=>"Label", 'p.description'=>"Description", 'p.note'=>"Note",
  200. 'p.price'=>"Price", 'p.tva_tx'=>'VAT', 'p.tosell'=>"OnSell", 'p.tobuy'=>'OnBuy', 'p.duration'=>"Duration",
  201. 'p.datec'=>'DateCreation', 'p.tms'=>'DateModification', 'p.pmp'=>'PMPValue', 'p.cost_price'=>'CostPrice',
  202. 'p.seuil_stock_alerte'=>'StockLimit',
  203. );
  204. $this->export_TypeFields_array[$r] = array(
  205. 'e.rowid'=>'List:entrepot:ref::stock', 'e.ref'=>'Text', 'e.lieu'=>'Text', 'e.address'=>'Text', 'e.zip'=>'Text', 'e.town'=>'Text',
  206. 'p.rowid'=>"Numeric", 'p.ref'=>"Text", 'p.fk_product_type'=>"Text", 'p.label'=>"Text", 'p.description'=>"Text", 'p.note'=>"Text",
  207. 'p.price'=>"Numeric", 'p.tva_tx'=>'Numeric', 'p.tosell'=>"Boolean", 'p.tobuy'=>"Boolean", 'p.duration'=>"Duree",
  208. 'p.datec'=>'Date', 'p.tms'=>'Date', 'p.pmp'=>'Numeric', 'p.cost_price'=>'Numeric',
  209. 'ps.reel'=>'Numeric',
  210. 'p.seuil_stock_alerte'=>'Numeric',
  211. );
  212. $this->export_entities_array[$r] = array(
  213. 'p.rowid'=>"product", 'p.ref'=>"product", 'p.fk_product_type'=>"product", 'p.label'=>"product", 'p.description'=>"product", 'p.note'=>"product",
  214. 'p.price'=>"product", 'p.tva_tx'=>'product', 'p.tosell'=>"product", 'p.tobuy'=>"product", 'p.duration'=>"product",
  215. 'p.datec'=>'product', 'p.tms'=>'product', 'p.pmp'=>'product', 'p.cost_price'=>'product',
  216. 'ps.reel'=>'stock',
  217. 'p.seuil_stock_alerte'=>'product',
  218. ); // We define here only fields that use another icon that the one defined into export_icon
  219. $this->export_aggregate_array[$r] = array('ps.reel'=>'SUM'); // TODO Not used yet
  220. $this->export_dependencies_array[$r] = array('stock'=>array('p.rowid', 'e.rowid')); // We must keep this until the aggregate_array is used. To have a unique key, if we ask a field of a child, to avoid the DISTINCT to discard them.
  221. $keyforselect = 'product';
  222. $keyforelement = 'product';
  223. $keyforaliasextra = 'extra';
  224. include DOL_DOCUMENT_ROOT.'/core/extrafieldsinexport.inc.php';
  225. $this->export_fields_array[$r] = array_merge($this->export_fields_array[$r], array('ps.reel'=>'Stock'));
  226. $this->export_sql_start[$r] = 'SELECT DISTINCT ';
  227. $this->export_sql_end[$r] = ' FROM '.MAIN_DB_PREFIX.'product as p LEFT JOIN '.MAIN_DB_PREFIX.'product_extrafields as extra ON extra.fk_object = p.rowid, '.MAIN_DB_PREFIX.'product_stock as ps, '.MAIN_DB_PREFIX.'entrepot as e';
  228. $this->export_sql_end[$r] .= ' WHERE p.rowid = ps.fk_product AND ps.fk_entrepot = e.rowid';
  229. $this->export_sql_end[$r] .= ' AND e.entity IN ('.getEntity('stock').')';
  230. // Export stock including batch number
  231. if (!empty($conf->productbatch->enabled)) {
  232. $langs->load("productbatch");
  233. // This request is same than previous but without field ps.stock (real stock in warehouse) and with link to subtable productbatch
  234. $r++;
  235. $this->export_code[$r] = $this->rights_class.'_emplacement_product_lot';
  236. $this->export_label[$r] = "StocksWithBatch"; // Translation key (used only if key ExportDataset_xxx_z not found)
  237. $this->export_icon[$r] = "warehouse";
  238. $this->export_permission[$r] = array(array("stock", "lire"));
  239. $this->export_fields_array[$r] = array(
  240. 'e.rowid'=>'IdWarehouse', 'e.ref'=>'LocationSummary', 'e.description'=>'DescWareHouse', 'e.lieu'=>'LieuWareHouse', 'e.address'=>'Address', 'e.zip'=>'Zip', 'e.town'=>'Town',
  241. 'p.rowid'=>"ProductId", 'p.ref'=>"Ref", 'p.fk_product_type'=>"Type", 'p.label'=>"Label", 'p.description'=>"Description", 'p.note'=>"Note",
  242. 'p.price'=>"Price", 'p.tva_tx'=>'VAT', 'p.tosell'=>"OnSell", 'p.tobuy'=>'OnBuy', 'p.duration'=>"Duration",
  243. 'p.datec'=>'DateCreation', 'p.tms'=>'DateModification', 'p.pmp'=>'PMPValue', 'p.cost_price'=>'CostPrice',
  244. 'pb.rowid'=>'Id', 'pb.batch'=>'Batch', 'pb.qty'=>'Qty',
  245. 'pl.eatby'=>'EatByDate', 'pl.sellby'=>'SellByDate'
  246. );
  247. $this->export_TypeFields_array[$r] = array(
  248. 'e.rowid'=>'List:entrepot:ref::stock', 'e.ref'=>'Text', 'e.lieu'=>'Text', 'e.description'=>'Text', 'e.address'=>'Text', 'e.zip'=>'Text', 'e.town'=>'Text',
  249. 'p.rowid'=>"Numeric", 'p.ref'=>"Text", 'p.fk_product_type'=>"Text", 'p.label'=>"Text", 'p.description'=>"Text", 'p.note'=>"Text",
  250. 'p.price'=>"Numeric", 'p.tva_tx'=>'Numeric', 'p.tosell'=>"Boolean", 'p.tobuy'=>"Boolean", 'p.duration'=>"Duree",
  251. 'p.datec'=>'DateCreation', 'p.tms'=>'DateModification', 'p.pmp'=>'PMPValue', 'p.cost_price'=>'CostPrice',
  252. 'pb.batch'=>'Text', 'pb.qty'=>'Numeric',
  253. 'pl.eatby'=>'Date', 'pl.sellby'=>'Date'
  254. );
  255. $this->export_entities_array[$r] = array(
  256. 'p.rowid'=>"product", 'p.ref'=>"product", 'p.fk_product_type'=>"product", 'p.label'=>"product", 'p.description'=>"product", 'p.note'=>"product",
  257. 'p.price'=>"product", 'p.tva_tx'=>'product', 'p.tosell'=>"product", 'p.tobuy'=>"product", 'p.duration'=>"product",
  258. 'p.datec'=>'product', 'p.tms'=>'product', 'p.pmp'=>'product', 'p.cost_price'=>'product',
  259. 'pb.rowid'=>'batch', 'pb.batch'=>'batch', 'pb.qty'=>'batch',
  260. 'pl.eatby'=>'batch', 'pl.sellby'=>'batch'
  261. ); // We define here only fields that use another icon that the one defined into export_icon
  262. $this->export_aggregate_array[$r] = array('ps.reel'=>'SUM'); // TODO Not used yet
  263. $this->export_dependencies_array[$r] = array('stockbatch'=>array('pb.rowid'), 'batch'=>array('pb.rowid')); // We must keep this until the aggregate_array is used. To add unique key if we ask a field of a child to avoid the DISTINCT to discard them.
  264. $keyforselect = 'product_lot';
  265. $keyforelement = 'batch';
  266. $keyforaliasextra = 'extra';
  267. include DOL_DOCUMENT_ROOT.'/core/extrafieldsinexport.inc.php';
  268. $this->export_sql_start[$r] = 'SELECT DISTINCT ';
  269. $this->export_sql_end[$r] = ' FROM '.MAIN_DB_PREFIX.'product_batch as pb';
  270. $this->export_sql_end[$r] .= ' INNER JOIN '.MAIN_DB_PREFIX.'product_stock as ps ON ps.rowid = pb.fk_product_stock';
  271. $this->export_sql_end[$r] .= ' INNER JOIN '.MAIN_DB_PREFIX.'product as p ON p.rowid = ps.fk_product';
  272. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product_lot as pl ON pl.fk_product = p.rowid AND pl.batch = pb.batch';
  273. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product_lot_extrafields as extra ON extra.fk_object = pl.rowid,';
  274. $this->export_sql_end[$r] .= ' '.MAIN_DB_PREFIX.'entrepot as e';
  275. $this->export_sql_end[$r] .= ' WHERE ps.fk_entrepot = e.rowid';
  276. $this->export_sql_end[$r] .= ' AND e.entity IN ('.getEntity('stock').')';
  277. }
  278. // Export of stock movements
  279. $r++;
  280. $this->export_code[$r] = $this->rights_class.'_movement';
  281. $this->export_label[$r] = "StockMovements"; // Translation key (used only if key ExportDataset_xxx_z not found)
  282. $this->export_icon[$r] = "movement";
  283. $this->export_permission[$r] = array(array("stock", "lire"));
  284. $this->export_fields_array[$r] = array(
  285. 'sm.rowid'=>'MovementId', 'sm.value'=>'Qty', 'sm.datem'=>'DateMovement', 'sm.label'=>'MovementLabel', 'sm.inventorycode'=>'InventoryCode',
  286. 'e.rowid'=>'IdWarehouse', 'e.ref'=>'LocationSummary', 'e.description'=>'DescWareHouse', 'e.lieu'=>'LieuWareHouse', 'e.address'=>'Address', 'e.zip'=>'Zip', 'e.town'=>'Town',
  287. 'p.rowid'=>"ProductId", 'p.ref'=>"Ref", 'p.fk_product_type'=>"Type", 'p.label'=>"Label", 'p.description'=>"Description", 'p.note'=>"Note",
  288. 'p.price'=>"Price", 'p.tva_tx'=>'VAT', 'p.tosell'=>"OnSell", 'p.tobuy'=>'OnBuy', 'p.duration'=>"Duration", 'p.datec'=>'DateCreation', 'p.tms'=>'DateModification'
  289. );
  290. $this->export_TypeFields_array[$r] = array(
  291. 'sm.rowid'=>'Numeric', 'sm.value'=>'Numeric', 'sm.datem'=>'Date', 'sm.batch'=>'Text', 'sm.label'=>'Text', 'sm.inventorycode'=>'Text',
  292. 'e.rowid'=>'List:entrepot:ref::stock', 'e.ref'=>'Text', 'e.description'=>'Text', 'e.lieu'=>'Text', 'e.address'=>'Text', 'e.zip'=>'Text', 'e.town'=>'Text',
  293. 'p.rowid'=>"Numeric", 'p.ref'=>"Text", 'p.fk_product_type'=>"Text", 'p.label'=>"Text", 'p.description'=>"Text", 'p.note'=>"Text",
  294. 'p.price'=>"Numeric", 'p.tva_tx'=>'Numeric', 'p.tosell'=>"Boolean", 'p.tobuy'=>"Boolean", 'p.duration'=>"Duree", 'p.datec'=>'Date', 'p.tms'=>'Date'
  295. );
  296. $this->export_entities_array[$r] = array(
  297. 'e.rowid'=>'warehouse', 'e.ref'=>'warehouse', 'e.description'=>'warehouse', 'e.lieu'=>'warehouse', 'e.address'=>'warehouse', 'e.zip'=>'warehouse', 'e.town'=>'warehouse',
  298. 'p.rowid'=>"product", 'p.ref'=>"product", 'p.fk_product_type'=>"product", 'p.label'=>"product", 'p.description'=>"product", 'p.note'=>"product",
  299. 'p.price'=>"product", 'p.tva_tx'=>'product', 'p.tosell'=>"product", 'p.tobuy'=>"product", 'p.duration'=>"product", 'p.datec'=>'product', 'p.tms'=>'product'
  300. ); // We define here only fields that use another icon that the one defined into export_icon
  301. if (!empty($conf->productbatch->enabled)) {
  302. $this->export_fields_array[$r]['sm.batch'] = 'Batch';
  303. $this->export_TypeFields_array[$r]['sm.batch'] = 'Text';
  304. $this->export_entities_array[$r]['sm.batch'] = 'movement';
  305. }
  306. $this->export_aggregate_array[$r] = array('sm.value'=>'SUM'); // TODO Not used yet
  307. $this->export_dependencies_array[$r] = array('movement'=>array('sm.rowid')); // We must keep this until the aggregate_array is used. To add unique key if we ask a field of a child to avoid the DISTINCT to discard them.
  308. $this->export_sql_start[$r] = 'SELECT DISTINCT ';
  309. $this->export_sql_end[$r] = ' FROM '.MAIN_DB_PREFIX.'product as p, '.MAIN_DB_PREFIX.'stock_mouvement as sm, '.MAIN_DB_PREFIX.'entrepot as e';
  310. $this->export_sql_end[$r] .= ' WHERE p.rowid = sm.fk_product AND sm.fk_entrepot = e.rowid';
  311. $this->export_sql_end[$r] .= ' AND e.entity IN ('.getEntity('stock').')';
  312. // Imports
  313. //--------
  314. $r = 0;
  315. // Import warehouses
  316. $r++;
  317. $this->import_code[$r] = $this->rights_class.'_'.$r;
  318. $this->import_label[$r] = "Warehouses"; // Translation key
  319. $this->import_icon[$r] = "warehouse";
  320. $this->import_entities_array[$r] = array(); // We define here only fields that use another icon that the one defined into import_icon
  321. $this->import_tables_array[$r] = array('e'=>MAIN_DB_PREFIX.'entrepot');
  322. $this->import_tables_creator_array[$r] = array('e'=>'fk_user_author');
  323. $this->import_fields_array[$r] = array('e.ref'=>"LocationSummary*",
  324. 'e.description'=>"DescWareHouse",
  325. 'e.lieu'=>"LieuWareHouse",
  326. 'e.address'=>"Address",
  327. 'e.zip'=>'Zip',
  328. 'e.fk_departement'=>'StateCode',
  329. 'e.fk_pays'=>'CountryCode',
  330. 'e.phone'=>'Phone',
  331. 'e.fax'=>'Fax',
  332. 'e.statut'=>'Status',
  333. 'e.fk_parent'=>'ParentWarehouse'
  334. );
  335. $this->import_convertvalue_array[$r] = array(
  336. 'e.fk_departement'=>array('rule'=>'fetchidfromcodeid', 'classfile'=>'/core/class/cstate.class.php', 'class'=>'Cstate', 'method'=>'fetch', 'dict'=>'DictionaryStateCode'),
  337. 'e.fk_pays'=>array('rule'=>'fetchidfromcodeid', 'classfile'=>'/core/class/ccountry.class.php', 'class'=>'Ccountry', 'method'=>'fetch', 'dict'=>'DictionaryCountry'),
  338. 'e.fk_parent'=>array('rule'=>'fetchidfromref', 'classfile'=>'/product/stock/class/entrepot.class.php', 'class'=>'Entrepot', 'method'=>'fetch', 'element'=>'ref')
  339. );
  340. $this->import_regex_array[$r] = array('e.statut'=>'^[0|1]');
  341. $this->import_examplevalues_array[$r] = array('e.ref'=>"ALM001",
  342. 'e.description'=>"Central Warehouse",
  343. 'e.lieu'=>"Central",
  344. 'e.address'=>"Route 66",
  345. 'e.zip'=>'28080',
  346. 'e.fk_departement'=>'matches field "code_departement" in table "'.MAIN_DB_PREFIX.'c_departements"',
  347. 'e.fk_pays'=>'US/FR/DE etc. matches field "code" in table "'.MAIN_DB_PREFIX.'c_country"',
  348. 'e.phone'=>'(+33)(0)123456789',
  349. 'e.fax'=>'(+33)(0)123456790',
  350. 'e.statut'=>'1',
  351. 'e.fk_parent'=>'id or ref of warehouse'
  352. );
  353. $this->import_updatekeys_array[$r] = array('p.ref'=>'Ref');
  354. // Import stocks
  355. $r++;
  356. $this->import_code[$r] = $this->rights_class.'_'.$r;
  357. $this->import_label[$r] = "Stocks"; // Translation key
  358. $this->import_icon[$r] = "stock";
  359. $this->import_entities_array[$r] = array(); // We define here only fields that use another icon that the one defined into import_icon
  360. $this->import_tables_array[$r] = array('ps'=>MAIN_DB_PREFIX.'product_stock');
  361. $this->import_fields_array[$r] = array('ps.fk_product'=>"Product*", 'ps.fk_entrepot'=>"Warehouse*", 'ps.reel'=>"Stock*");
  362. $this->import_convertvalue_array[$r] = array(
  363. 'ps.fk_product'=>array('rule'=>'fetchidfromref', 'classfile'=>'/product/class/product.class.php', 'class'=>'Product', 'method'=>'fetch', 'element'=>'product'),
  364. 'ps.fk_entrepot'=>array('rule'=>'fetchidfromref', 'classfile'=>'/product/stock/class/entrepot.class.php', 'class'=>'Entrepot', 'method'=>'fetch', 'element'=>'ref')
  365. );
  366. $this->import_examplevalues_array[$r] = array(
  367. 'ps.fk_product'=>"id or ref of product", 'ps.fk_entrepot'=>"id or ref of warehouse", 'ps.reel'=>"10"
  368. );
  369. $this->import_updatekeys_array[$r] = array('ps.fk_product'=>'Product', 'ps.fk_entrepot'=>"Warehouse");
  370. $this->import_run_sql_after_array[$r] = array( // Because we may change data that are denormalized, we must update dernormalized data after.
  371. 'UPDATE '.MAIN_DB_PREFIX.'product as p SET p.stock = (SELECT SUM(ps.reel) FROM '.MAIN_DB_PREFIX.'product_stock ps WHERE ps.fk_product = p.rowid);'
  372. );
  373. }
  374. /**
  375. * Function called when module is enabled.
  376. * The init function add constants, boxes, permissions and menus (defined in constructor) into Dolibarr database.
  377. * It also creates data directories
  378. *
  379. * @param string $options Options when enabling module ('', 'noboxes')
  380. * @return int 1 if OK, 0 if KO
  381. */
  382. public function init($options = '')
  383. {
  384. global $conf, $langs;
  385. $result = $this->_load_tables('/install/mysql/', 'stock');
  386. if ($result < 0) {
  387. 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')
  388. }
  389. // Permissions
  390. $this->remove($options);
  391. //ODT template
  392. $src = DOL_DOCUMENT_ROOT.'/install/doctemplates/stocks/template_warehouse.odt';
  393. $dirodt = DOL_DATA_ROOT.'/doctemplates/stocks';
  394. $dest = $dirodt.'/template_warehouse.odt';
  395. if (file_exists($src) && !file_exists($dest)) {
  396. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  397. dol_mkdir($dirodt);
  398. $result = dol_copy($src, $dest, 0, 0);
  399. if ($result < 0) {
  400. $langs->load("errors");
  401. $this->error = $langs->trans('ErrorFailToCopyFile', $src, $dest);
  402. return 0;
  403. }
  404. }
  405. $sql = array();
  406. $sql = array(
  407. "DELETE FROM ".MAIN_DB_PREFIX."document_model WHERE nom = '".$this->db->escape($this->const[1][2])."' AND type = 'stock' AND entity = ".((int) $conf->entity),
  408. "INSERT INTO ".MAIN_DB_PREFIX."document_model (nom, type, entity) VALUES('".$this->db->escape($this->const[1][2])."','stock',".((int) $conf->entity).")",
  409. "DELETE FROM ".MAIN_DB_PREFIX."document_model WHERE nom = '".$this->db->escape($this->const[2][2])."' AND type = 'mouvement' AND entity = ".((int) $conf->entity),
  410. "INSERT INTO ".MAIN_DB_PREFIX."document_model (nom, type, entity) VALUES('".$this->db->escape($this->const[2][2])."','mouvement',".((int) $conf->entity).")",
  411. );
  412. return $this->_init($sql, $options);
  413. }
  414. }