modStock.class.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. *
  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 <http://www.gnu.org/licenses/>.
  19. */
  20. /**
  21. * \defgroup stock Module stocks
  22. * \brief Module pour gerer la tenue de stocks produits
  23. * \file htdocs/core/modules/modStock.class.php
  24. * \ingroup stock
  25. * \brief Fichier de description et activation du module Stock
  26. */
  27. include_once DOL_DOCUMENT_ROOT .'/core/modules/DolibarrModules.class.php';
  28. /**
  29. * Class to describe and enable module Stock
  30. */
  31. class modStock extends DolibarrModules
  32. {
  33. /**
  34. * Constructor. Define names, constants, directories, boxes, permissions
  35. *
  36. * @param DoliDB $db Database handler
  37. */
  38. function __construct($db)
  39. {
  40. global $conf, $langs;
  41. $this->db = $db;
  42. $this->numero = 52;
  43. $this->family = "products";
  44. $this->module_position = '40';
  45. // Module label (no space allowed), used if translation string 'ModuleXXXName' not found (where XXX is value of numeric property 'numero' of module)
  46. $this->name = preg_replace('/^mod/i','',get_class($this));
  47. $this->description = "Gestion des stocks";
  48. // Possible values for version are: 'development', 'experimental', 'dolibarr' or version
  49. $this->version = 'dolibarr';
  50. $this->const_name = 'MAIN_MODULE_'.strtoupper($this->name);
  51. $this->picto='stock';
  52. // Data directories to create when module is enabled
  53. $this->dirs = array("/stock/temp");
  54. $this->config_page_url = array("stock.php");
  55. // Dependencies
  56. $this->hidden = false; // A condition to hide module
  57. $this->depends = array("modProduct"); // List of module class names as string that must be enabled if this module is enabled
  58. $this->requiredby = array("modProductBatch"); // List of module ids to disable if this one is disabled
  59. $this->conflictwith = array(); // List of module class names as string this module is in conflict with
  60. $this->phpmin = array(5,4); // Minimum version of PHP required by module
  61. $this->langfiles = array("stocks");
  62. // Constants
  63. $this->const = array();
  64. $r=0;
  65. $this->const[$r] = array('STOCK_ALLOW_NEGATIVE_TRANSFER','chaine','1','',1);
  66. $r++;
  67. $this->const[$r][0] = "STOCK_ADDON_PDF";
  68. $this->const[$r][1] = "chaine";
  69. $this->const[$r][2] = "Standard";
  70. $this->const[$r][3] = 'Name of PDF model of stock';
  71. $this->const[$r][4] = 0;
  72. $r++;
  73. $this->const[$r][0] = "MOUVEMENT_ADDON_PDF";
  74. $this->const[$r][1] = "chaine";
  75. $this->const[$r][2] = "StdMouvement";
  76. $this->const[$r][3] = 'Name of PDF model of stock mouvement';
  77. $this->const[$r][4] = 0;
  78. $r++;
  79. $this->const[$r][0] = "STOCK_ADDON_PDF_ODT_PATH";
  80. $this->const[$r][1] = "chaine";
  81. $this->const[$r][2] = "DOL_DATA_ROOT/doctemplates/stocks";
  82. $this->const[$r][3] = "";
  83. $this->const[$r][4] = 0;
  84. $r++;
  85. $this->const[$r][0] = "MOUVEMENT_ADDON_PDF_ODT_PATH";
  86. $this->const[$r][1] = "chaine";
  87. $this->const[$r][2] = "DOL_DATA_ROOT/doctemplates/stocks/mouvements";
  88. $this->const[$r][3] = "";
  89. $this->const[$r][4] = 0;
  90. // Boxes
  91. $this->boxes = array();
  92. // Permissions
  93. $this->rights = array();
  94. $this->rights_class = 'stock';
  95. $this->rights[0][0] = 1001;
  96. $this->rights[0][1] = 'Lire les stocks';
  97. $this->rights[0][2] = 'r';
  98. $this->rights[0][3] = 0;
  99. $this->rights[0][4] = 'lire';
  100. $this->rights[0][5] = '';
  101. $this->rights[1][0] = 1002;
  102. $this->rights[1][1] = 'Creer/Modifier les stocks';
  103. $this->rights[1][2] = 'w';
  104. $this->rights[1][3] = 0;
  105. $this->rights[1][4] = 'creer';
  106. $this->rights[1][5] = '';
  107. $this->rights[2][0] = 1003;
  108. $this->rights[2][1] = 'Supprimer les stocks';
  109. $this->rights[2][2] = 'd';
  110. $this->rights[2][3] = 0;
  111. $this->rights[2][4] = 'supprimer';
  112. $this->rights[2][5] = '';
  113. $this->rights[3][0] = 1004;
  114. $this->rights[3][1] = 'Lire mouvements de stocks';
  115. $this->rights[3][2] = 'r';
  116. $this->rights[3][3] = 0;
  117. $this->rights[3][4] = 'mouvement';
  118. $this->rights[3][5] = 'lire';
  119. $this->rights[4][0] = 1005;
  120. $this->rights[4][1] = 'Creer/modifier mouvements de stocks';
  121. $this->rights[4][2] = 'w';
  122. $this->rights[4][3] = 0;
  123. $this->rights[4][4] = 'mouvement';
  124. $this->rights[4][5] = 'creer';
  125. if ($conf->global->MAIN_FEATURES_LEVEL >= 2) {
  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. $this->rights[8][0] = 1014;
  137. $this->rights[8][1] = 'inventoryValidatePermission'; // Permission label
  138. $this->rights[8][3] = 0; // Permission by default for new user (0/1)
  139. $this->rights[8][4] = 'inventory_advance'; // In php code, permission will be checked by test if ($user->rights->permkey->level1->level2)
  140. $this->rights[8][5] = 'validate'; // In php code, permission will be checked by test if ($user->rights->permkey->level1->level2)
  141. $this->rights[9][0] = 1015;
  142. $this->rights[9][1] = 'inventoryChangePMPPermission'; // Permission label
  143. $this->rights[9][3] = 0; // Permission by default for new user (0/1)
  144. $this->rights[9][4] = 'inventory_advance'; // In php code, permission will be checked by test if ($user->rights->permkey->level1->level2)
  145. $this->rights[9][5] = 'changePMP'; // In php code, permission will be checked by test if ($user->rights->permkey->level1->level2)
  146. }
  147. // Main menu entries
  148. $this->menu = array(); // List of menus to add
  149. $r=0;
  150. // Menus
  151. //-------
  152. $this->menu = 1; // This module add menu entries. They are coded into menu manager.
  153. // Exports
  154. //--------
  155. $r=0;
  156. $r++;
  157. $this->export_code[$r]=$this->rights_class;
  158. $this->export_label[$r]="WarehousesAndProducts"; // Translation key (used only if key ExportDataset_xxx_z not found)
  159. $this->export_permission[$r]=array(array("stock","lire"));
  160. $this->export_fields_array[$r]=array(
  161. 'e.rowid'=>'IdWarehouse','e.ref'=>'LocationSummary','e.description'=>'DescWareHouse','e.lieu'=>'LieuWareHouse','e.address'=>'Address',
  162. 'e.zip'=>'Zip','e.town'=>'Town','p.rowid'=>"ProductId",'p.ref'=>"Ref",'p.fk_product_type'=>"Type",'p.label'=>"Label",'p.description'=>"Description",
  163. 'p.note'=>"Note",'p.price'=>"Price",'p.tva_tx'=>'VAT','p.tosell'=>"OnSell",'p.tobuy'=>'OnBuy','p.duration'=>"Duration",'p.datec'=>'DateCreation',
  164. 'p.tms'=>'DateModification','p.pmp'=>'PMPValue','p.cost_price'=>'CostPrice'
  165. );
  166. $this->export_TypeFields_array[$r]=array(
  167. 'e.rowid'=>'List:entrepot:ref','e.ref'=>'Text','e.lieu'=>'Text','e.address'=>'Text','e.zip'=>'Text','e.town'=>'Text','p.rowid'=>"List:product:label",
  168. 'p.ref'=>"Text",'p.fk_product_type'=>"Text",'p.label'=>"Text",'p.description'=>"Text",'p.note'=>"Text",'p.price'=>"Numeric",'p.tva_tx'=>'Numeric',
  169. 'p.tosell'=>"Boolean",'p.tobuy'=>"Boolean",'p.duration'=>"Duree",'p.datec'=>'Date','p.tms'=>'Date','p.pmp'=>'Numeric','p.cost_price'=>'Numeric',
  170. 'ps.reel'=>'Numeric'
  171. );
  172. $this->export_entities_array[$r]=array(
  173. 'e.rowid'=>'warehouse','e.ref'=>'warehouse','e.description'=>'warehouse','e.lieu'=>'warehouse','e.address'=>'warehouse','e.zip'=>'warehouse',
  174. 'e.town'=>'warehouse','p.rowid'=>"product",'p.ref'=>"product",'p.fk_product_type'=>"product",'p.label'=>"product",'p.description'=>"product",
  175. 'p.note'=>"product",'p.price'=>"product",'p.tva_tx'=>'product','p.tosell'=>"product",'p.tobuy'=>"product",'p.duration'=>"product",
  176. 'p.datec'=>'product','p.tms'=>'product','p.pmp'=>'product','p.cost_price'=>'product','ps.reel'=>'stock'
  177. );
  178. $this->export_aggregate_array[$r]=array('ps.reel'=>'SUM'); // TODO Not used yet
  179. $this->export_dependencies_array[$r]=array('stock'=>array('p.rowid','e.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.
  180. $keyforselect='product'; $keyforelement='product'; $keyforaliasextra='extra';
  181. include DOL_DOCUMENT_ROOT.'/core/extrafieldsinexport.inc.php';
  182. $this->export_fields_array[$r]=array_merge($this->export_fields_array[$r],array('ps.reel'=>'Stock'));
  183. $this->export_sql_start[$r]='SELECT DISTINCT ';
  184. $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';
  185. $this->export_sql_end[$r] .=' WHERE p.rowid = ps.fk_product AND ps.fk_entrepot = e.rowid';
  186. $this->export_sql_end[$r] .=' AND e.entity IN ('.getEntity('stock').')';
  187. if ($conf->productbatch->enabled)
  188. {
  189. // Export of stock including lot number
  190. $langs->load("productbatch");
  191. // This request is same than previous but without field ps.stock (real stock in warehouse) and with link to subtable productbatch
  192. $r++;
  193. $this->export_code[$r]=$this->rights_class.'_lot';
  194. $this->export_label[$r]="WarehousesAndProductsBatchDetail"; // Translation key (used only if key ExportDataset_xxx_z not found)
  195. $this->export_permission[$r]=array(array("stock","lire"));
  196. $this->export_fields_array[$r]=array(
  197. 'e.rowid'=>'IdWarehouse','e.ref'=>'LocationSummary','e.description'=>'DescWareHouse','e.lieu'=>'LieuWareHouse','e.address'=>'Address',
  198. 'e.zip'=>'Zip','e.town'=>'Town','p.rowid'=>"ProductId",'p.ref'=>"Ref",'p.fk_product_type'=>"Type",'p.label'=>"Label",'p.description'=>"Description",
  199. 'p.note'=>"Note",'p.price'=>"Price",'p.tva_tx'=>'VAT','p.tosell'=>"OnSell",'p.tobuy'=>'OnBuy','p.duration'=>"Duration",'p.datec'=>'DateCreation',
  200. 'p.tms'=>'DateModification','pb.rowid'=>'Id','pb.batch'=>'Batch','pb.qty'=>'Qty','pl.eatby'=>'EatByDate','pl.sellby'=>'SellByDate'
  201. );
  202. $this->export_TypeFields_array[$r]=array(
  203. 'e.rowid'=>'List:entrepot:ref','e.ref'=>'Text','e.lieu'=>'Text','e.description'=>'Text','e.address'=>'Text','e.zip'=>'Text','e.town'=>'Text',
  204. 'p.rowid'=>"List:product:label",'p.ref'=>"Text",'p.fk_product_type'=>"Text",'p.label'=>"Text",'p.description'=>"Text",'p.note'=>"Text",
  205. 'p.price'=>"Numeric",'p.tva_tx'=>'Numeric','p.tosell'=>"Boolean",'p.tobuy'=>"Boolean",'p.duration'=>"Duree",'p.datec'=>'Date','p.tms'=>'Date',
  206. 'pb.batch'=>'Text','pb.qty'=>'Numeric','pl.eatby'=>'Date','pl.sellby'=>'Date'
  207. );
  208. $this->export_entities_array[$r]=array(
  209. 'e.rowid'=>'warehouse','e.ref'=>'warehouse','e.description'=>'warehouse','e.lieu'=>'warehouse','e.address'=>'warehouse','e.zip'=>'warehouse',
  210. 'e.town'=>'warehouse','p.rowid'=>"product",'p.ref'=>"product",'p.fk_product_type'=>"product",'p.label'=>"product",'p.description'=>"product",
  211. 'p.note'=>"product",'p.price'=>"product",'p.tva_tx'=>'product','p.tosell'=>"product",'p.tobuy'=>"product",'p.duration'=>"product",
  212. 'p.datec'=>'product','p.tms'=>'product','pb.rowid'=>'stockbatch','pb.batch'=>'stockbatch','pb.qty'=>'stockbatch','pl.eatby'=>'batch',
  213. 'pl.sellby'=>'batch'
  214. );
  215. $this->export_aggregate_array[$r]=array('ps.reel'=>'SUM'); // TODO Not used yet
  216. $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.
  217. $keyforselect='product_lot'; $keyforelement='batch'; $keyforaliasextra='extra';
  218. include DOL_DOCUMENT_ROOT.'/core/extrafieldsinexport.inc.php';
  219. $this->export_sql_start[$r]='SELECT DISTINCT ';
  220. $this->export_sql_end[$r] =' FROM ('.MAIN_DB_PREFIX.'product as p, '.MAIN_DB_PREFIX.'product_stock as ps, '.MAIN_DB_PREFIX.'product_batch as pb)';
  221. $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';
  222. $this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'product_lot_extrafields as extra ON extra.fk_object = pl.rowid,';
  223. $this->export_sql_end[$r] .=' '.MAIN_DB_PREFIX.'entrepot as e';
  224. $this->export_sql_end[$r] .=' WHERE p.rowid = ps.fk_product AND ps.fk_entrepot = e.rowid AND ps.rowid = pb.fk_product_stock';
  225. $this->export_sql_end[$r] .=' AND e.entity IN ('.getEntity('stock').')';
  226. }
  227. // Export of stock movement
  228. $r++;
  229. $this->export_code[$r]=$this->rights_class.'_movement';
  230. $this->export_label[$r]="StockMovements"; // Translation key (used only if key ExportDataset_xxx_z not found)
  231. $this->export_permission[$r]=array(array("stock","lire"));
  232. $this->export_fields_array[$r]=array(
  233. 'e.rowid'=>'IdWarehouse','e.ref'=>'LocationSummary','e.description'=>'DescWareHouse','e.lieu'=>'LieuWareHouse','e.address'=>'Address','e.zip'=>'Zip',
  234. 'e.town'=>'Town','p.rowid'=>"ProductId",'p.ref'=>"Ref",'p.fk_product_type'=>"Type",'p.label'=>"Label",'p.description'=>"Description",'p.note'=>"Note",
  235. 'p.price'=>"Price",'p.tva_tx'=>'VAT','p.tosell'=>"OnSell",'p.tobuy'=>'OnBuy','p.duration'=>"Duration",'p.datec'=>'DateCreation',
  236. 'p.tms'=>'DateModification','sm.rowid'=>'MovementId','sm.value'=>'Qty','sm.datem'=>'DateMovement','sm.label'=>'MovementLabel',
  237. 'sm.inventorycode'=>'InventoryCode'
  238. );
  239. $this->export_TypeFields_array[$r]=array(
  240. 'e.rowid'=>'List:entrepot:ref','e.ref'=>'Text','e.description'=>'Text','e.lieu'=>'Text','e.address'=>'Text','e.zip'=>'Text','e.town'=>'Text',
  241. 'p.rowid'=>"List:product:label",'p.ref'=>"Text",'p.fk_product_type'=>"Text",'p.label'=>"Text",'p.description'=>"Text",'p.note'=>"Text",
  242. 'p.price'=>"Numeric",'p.tva_tx'=>'Numeric','p.tosell'=>"Boolean",'p.tobuy'=>"Boolean",'p.duration'=>"Duree",'p.datec'=>'Date','p.tms'=>'Date',
  243. 'sm.rowid'=>'Numeric','sm.value'=>'Numeric','sm.datem'=>'Date','sm.batch'=>'Text','sm.label'=>'Text','sm.inventorycode'=>'Text'
  244. );
  245. $this->export_entities_array[$r]=array(
  246. 'e.rowid'=>'warehouse','e.ref'=>'warehouse','e.description'=>'warehouse','e.lieu'=>'warehouse','e.address'=>'warehouse','e.zip'=>'warehouse',
  247. 'e.town'=>'warehouse','p.rowid'=>"product",'p.ref'=>"product",'p.fk_product_type'=>"product",'p.label'=>"product",'p.description'=>"product",
  248. 'p.note'=>"product",'p.price'=>"product",'p.tva_tx'=>'product','p.tosell'=>"product",'p.tobuy'=>"product",'p.duration'=>"product",'p.datec'=>'product',
  249. 'p.tms'=>'product','sm.rowid'=>'movement','sm.value'=>'movement','sm.datem'=>'movement','sm.label'=>'movement','sm.inventorycode'=>'movement'
  250. );
  251. if ($conf->productbatch->enabled)
  252. {
  253. $this->export_fields_array[$r]['sm.batch']='Batch';
  254. $this->export_TypeFields_array[$r]['sm.batch']='Text';
  255. $this->export_entities_array[$r]['sm.batch']='movement';
  256. }
  257. $this->export_aggregate_array[$r]=array('sm.value'=>'SUM'); // TODO Not used yet
  258. $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.
  259. $this->export_sql_start[$r]='SELECT DISTINCT ';
  260. $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';
  261. $this->export_sql_end[$r] .=' WHERE p.rowid = sm.fk_product AND sm.fk_entrepot = e.rowid';
  262. $this->export_sql_end[$r] .=' AND e.entity IN ('.getEntity('stock').')';
  263. // Imports
  264. //--------
  265. $r=0;
  266. // Import warehouses
  267. $r++;
  268. $this->import_code[$r]=$this->rights_class.'_'.$r;
  269. $this->import_label[$r]="Warehouses"; // Translation key
  270. $this->import_icon[$r]=$this->picto;
  271. $this->import_entities_array[$r]=array(); // We define here only fields that use another icon that the one defined into import_icon
  272. $this->import_tables_array[$r]=array('e'=>MAIN_DB_PREFIX.'entrepot');
  273. $this->import_tables_creator_array[$r]=array('e'=>'fk_user_author');
  274. $this->import_fields_array[$r]=array('e.ref'=>"LocationSummary*",
  275. 'e.description'=>"DescWareHouse",'e.lieu'=>"LieuWareHouse",
  276. 'e.address'=>"Address",'e.zip'=>'Zip','e.fk_pays'=>'CountryCode',
  277. 'e.statut'=>'Status'
  278. );
  279. $this->import_convertvalue_array[$r]=array(
  280. 'e.fk_pays'=>array('rule'=>'fetchidfromcodeid','classfile'=>'/core/class/ccountry.class.php','class'=>'Ccountry','method'=>'fetch','dict'=>'DictionaryCountry')
  281. );
  282. $this->import_regex_array[$r]=array('e.statut'=>'^[0|1]');
  283. $this->import_examplevalues_array[$r]=array('e.ref'=>"ALM001",
  284. 'e.description'=>"Central Warehouse",'e.lieu'=>"Central",
  285. 'e.address'=>"Route 66",'e.zip'=>'28080','e.fk_pays'=>'US',
  286. 'e.statut'=>'1');
  287. // Import stocks
  288. $r++;
  289. $this->import_code[$r]=$this->rights_class.'_'.$r;
  290. $this->import_label[$r]="Stocks"; // Translation key
  291. $this->import_icon[$r]=$this->picto;
  292. $this->import_entities_array[$r]=array(); // We define here only fields that use another icon that the one defined into import_icon
  293. $this->import_tables_array[$r]=array('ps'=>MAIN_DB_PREFIX.'product_stock');
  294. $this->import_fields_array[$r]=array('ps.fk_product'=>"Product*",'ps.fk_entrepot'=>"Warehouse*",'ps.reel'=>"Stock*");
  295. $this->import_convertvalue_array[$r]=array(
  296. 'ps.fk_product'=>array('rule'=>'fetchidfromref','classfile'=>'/product/class/product.class.php','class'=>'Product','method'=>'fetch','element'=>'product'),
  297. 'ps.fk_entrepot'=>array('rule'=>'fetchidfromref','classfile'=>'/product/stock/class/entrepot.class.php','class'=>'Entrepot','method'=>'fetch','element'=>'ref')
  298. );
  299. $this->import_examplevalues_array[$r]=array(
  300. 'ps.fk_product'=>"PREF123456",'ps.fk_entrepot'=>"ALM001",'ps.reel'=>"10"
  301. );
  302. $this->import_run_sql_after_array[$r]=array( // Because we may change data that are denormalized, we must update dernormalized data after.
  303. 'UPDATE llx_product p SET p.stock= (SELECT SUM(ps.reel) FROM llx_product_stock ps WHERE ps.fk_product = p.rowid);'
  304. );
  305. }
  306. /**
  307. * Function called when module is enabled.
  308. * The init function add constants, boxes, permissions and menus (defined in constructor) into Dolibarr database.
  309. * It also creates data directories
  310. *
  311. * @param string $options Options when enabling module ('', 'noboxes')
  312. * @return int 1 if OK, 0 if KO
  313. */
  314. function init($options='')
  315. {
  316. global $conf,$langs;
  317. // Permissions
  318. $this->remove($options);
  319. //ODT template
  320. $src=DOL_DOCUMENT_ROOT.'/install/doctemplates/stock/template_stock.odt';
  321. $dirodt=DOL_DATA_ROOT.'/doctemplates/stock';
  322. $dest=$dirodt.'/template_stock.odt';
  323. if (file_exists($src) && ! file_exists($dest))
  324. {
  325. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  326. dol_mkdir($dirodt);
  327. $result=dol_copy($src,$dest,0,0);
  328. if ($result < 0)
  329. {
  330. $langs->load("errors");
  331. $this->error=$langs->trans('ErrorFailToCopyFile',$src,$dest);
  332. return 0;
  333. }
  334. }
  335. $sql = array();
  336. $sql = array(
  337. "DELETE FROM ".MAIN_DB_PREFIX."document_model WHERE nom = '".$this->db->escape($this->const[1][2])."' AND type = 'stock' AND entity = ".$conf->entity,
  338. "INSERT INTO ".MAIN_DB_PREFIX."document_model (nom, type, entity) VALUES('".$this->db->escape($this->const[1][2])."','stock',".$conf->entity.")",
  339. "DELETE FROM ".MAIN_DB_PREFIX."document_model WHERE nom = '".$this->db->escape($this->const[2][2])."' AND type = 'mouvement' AND entity = ".$conf->entity,
  340. "INSERT INTO ".MAIN_DB_PREFIX."document_model (nom, type, entity) VALUES('".$this->db->escape($this->const[2][2])."','mouvement',".$conf->entity.")",
  341. );
  342. return $this->_init($sql,$options);
  343. }
  344. }