modStock.class.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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 <https://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. public function __construct($db)
  39. {
  40. global $conf, $langs;
  41. $this->db = $db;
  42. $this->numero = 52;
  43. $this->family = "products";
  44. $this->module_position = '39';
  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. {
  127. $this->rights[5][0] = 1011;
  128. $this->rights[5][1] = 'inventoryReadPermission'; // Permission label
  129. $this->rights[5][3] = 0; // Permission by default for new user (0/1)
  130. $this->rights[5][4] = 'inventory_advance'; // In php code, permission will be checked by test if ($user->rights->permkey->level1->level2)
  131. $this->rights[5][5] = 'read'; // In php code, permission will be checked by test if ($user->rights->permkey->level1->level2)
  132. $this->rights[6][0] = 1012;
  133. $this->rights[6][1] = 'inventoryCreatePermission'; // Permission label
  134. $this->rights[6][3] = 0; // Permission by default for new user (0/1)
  135. $this->rights[6][4] = 'inventory_advance'; // In php code, permission will be checked by test if ($user->rights->permkey->level1->level2)
  136. $this->rights[6][5] = 'write'; // In php code, permission will be checked by test if ($user->rights->permkey->level1->level2)
  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. }
  148. // Main menu entries
  149. $this->menu = array(); // List of menus to add
  150. $r = 0;
  151. // Menus
  152. //-------
  153. $this->menu = 1; // This module add menu entries. They are coded into menu manager.
  154. // Exports
  155. //--------
  156. $r = 0;
  157. // Export stock (without batch number)
  158. $r++;
  159. $this->export_code[$r] = $this->rights_class;
  160. $this->export_label[$r] = "Stocks"; // Translation key (used only if key ExportDataset_xxx_z not found)
  161. $this->export_icon[$r] = "warehouse";
  162. $this->export_permission[$r] = array(array("stock", "lire"));
  163. $this->export_fields_array[$r] = array(
  164. 'e.rowid'=>'IdWarehouse', 'e.ref'=>'LocationSummary', 'e.description'=>'DescWareHouse', 'e.lieu'=>'LieuWareHouse', 'e.address'=>'Address', 'e.zip'=>'Zip', 'e.town'=>'Town',
  165. 'p.rowid'=>"ProductId", 'p.ref'=>"Ref", 'p.fk_product_type'=>"Type", 'p.label'=>"Label", 'p.description'=>"Description", 'p.note'=>"Note",
  166. 'p.price'=>"Price", 'p.tva_tx'=>'VAT', 'p.tosell'=>"OnSell", 'p.tobuy'=>'OnBuy', 'p.duration'=>"Duration",
  167. 'p.datec'=>'DateCreation', 'p.tms'=>'DateModification', 'p.pmp'=>'PMPValue', 'p.cost_price'=>'CostPrice',
  168. );
  169. $this->export_TypeFields_array[$r] = array(
  170. 'e.rowid'=>'List:entrepot:ref::stock', 'e.ref'=>'Text', 'e.lieu'=>'Text', 'e.address'=>'Text', 'e.zip'=>'Text', 'e.town'=>'Text',
  171. 'p.rowid'=>"List:product:label::product", 'p.ref'=>"Text", 'p.fk_product_type'=>"Text", 'p.label'=>"Text", 'p.description'=>"Text", 'p.note'=>"Text",
  172. 'p.price'=>"Numeric", 'p.tva_tx'=>'Numeric', 'p.tosell'=>"Boolean", 'p.tobuy'=>"Boolean", 'p.duration'=>"Duree",
  173. 'p.datec'=>'Date', 'p.tms'=>'Date', 'p.pmp'=>'Numeric', 'p.cost_price'=>'Numeric',
  174. 'ps.reel'=>'Numeric'
  175. );
  176. $this->export_entities_array[$r] = array(
  177. 'p.rowid'=>"product", 'p.ref'=>"product", 'p.fk_product_type'=>"product", 'p.label'=>"product", 'p.description'=>"product", 'p.note'=>"product",
  178. 'p.price'=>"product", 'p.tva_tx'=>'product', 'p.tosell'=>"product", 'p.tobuy'=>"product", 'p.duration'=>"product",
  179. 'p.datec'=>'product', 'p.tms'=>'product', 'p.pmp'=>'product', 'p.cost_price'=>'product',
  180. 'ps.reel'=>'stock'
  181. ); // We define here only fields that use another icon that the one defined into export_icon
  182. $this->export_aggregate_array[$r] = array('ps.reel'=>'SUM'); // TODO Not used yet
  183. $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.
  184. $keyforselect = 'product'; $keyforelement = 'product'; $keyforaliasextra = 'extra';
  185. include DOL_DOCUMENT_ROOT.'/core/extrafieldsinexport.inc.php';
  186. $this->export_fields_array[$r] = array_merge($this->export_fields_array[$r], array('ps.reel'=>'Stock'));
  187. $this->export_sql_start[$r] = 'SELECT DISTINCT ';
  188. $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';
  189. $this->export_sql_end[$r] .= ' WHERE p.rowid = ps.fk_product AND ps.fk_entrepot = e.rowid';
  190. $this->export_sql_end[$r] .= ' AND e.entity IN ('.getEntity('stock').')';
  191. // Export stock including batch number
  192. if ($conf->productbatch->enabled)
  193. {
  194. $langs->load("productbatch");
  195. // This request is same than previous but without field ps.stock (real stock in warehouse) and with link to subtable productbatch
  196. $r++;
  197. $this->export_code[$r] = $this->rights_class.'_lot';
  198. $this->export_label[$r] = "StocksWithBatch"; // Translation key (used only if key ExportDataset_xxx_z not found)
  199. $this->export_icon[$r] = "warehouse";
  200. $this->export_permission[$r] = array(array("stock", "lire"));
  201. $this->export_fields_array[$r] = array(
  202. 'e.rowid'=>'IdWarehouse', 'e.ref'=>'LocationSummary', 'e.description'=>'DescWareHouse', 'e.lieu'=>'LieuWareHouse', 'e.address'=>'Address', 'e.zip'=>'Zip', 'e.town'=>'Town',
  203. 'p.rowid'=>"ProductId", 'p.ref'=>"Ref", 'p.fk_product_type'=>"Type", 'p.label'=>"Label", 'p.description'=>"Description", 'p.note'=>"Note",
  204. 'p.price'=>"Price", 'p.tva_tx'=>'VAT', 'p.tosell'=>"OnSell", 'p.tobuy'=>'OnBuy', 'p.duration'=>"Duration",
  205. 'p.datec'=>'DateCreation', 'p.tms'=>'DateModification', 'p.pmp'=>'PMPValue', 'p.cost_price'=>'CostPrice',
  206. 'pb.rowid'=>'Id', 'pb.batch'=>'Batch', 'pb.qty'=>'Qty',
  207. 'pl.eatby'=>'EatByDate', 'pl.sellby'=>'SellByDate'
  208. );
  209. $this->export_TypeFields_array[$r] = array(
  210. 'e.rowid'=>'List:entrepot:ref::stock', 'e.ref'=>'Text', 'e.lieu'=>'Text', 'e.description'=>'Text', 'e.address'=>'Text', 'e.zip'=>'Text', 'e.town'=>'Text',
  211. 'p.rowid'=>"List:product:label::product", 'p.ref'=>"Text", 'p.fk_product_type'=>"Text", 'p.label'=>"Text", 'p.description'=>"Text", 'p.note'=>"Text",
  212. 'p.price'=>"Numeric", 'p.tva_tx'=>'Numeric', 'p.tosell'=>"Boolean", 'p.tobuy'=>"Boolean", 'p.duration'=>"Duree",
  213. 'p.datec'=>'DateCreation', 'p.tms'=>'DateModification', 'p.pmp'=>'PMPValue', 'p.cost_price'=>'CostPrice',
  214. 'pb.batch'=>'Text', 'pb.qty'=>'Numeric',
  215. 'pl.eatby'=>'Date', 'pl.sellby'=>'Date'
  216. );
  217. $this->export_entities_array[$r] = array(
  218. 'p.rowid'=>"product", 'p.ref'=>"product", 'p.fk_product_type'=>"product", 'p.label'=>"product", 'p.description'=>"product", 'p.note'=>"product",
  219. 'p.price'=>"product", 'p.tva_tx'=>'product', 'p.tosell'=>"product", 'p.tobuy'=>"product", 'p.duration'=>"product",
  220. 'p.datec'=>'product', 'p.tms'=>'product', 'p.pmp'=>'product', 'p.cost_price'=>'product',
  221. 'pb.rowid'=>'batch', 'pb.batch'=>'batch', 'pb.qty'=>'batch',
  222. 'pl.eatby'=>'batch', 'pl.sellby'=>'batch'
  223. ); // We define here only fields that use another icon that the one defined into export_icon
  224. $this->export_aggregate_array[$r] = array('ps.reel'=>'SUM'); // TODO Not used yet
  225. $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.
  226. $keyforselect = 'product_lot'; $keyforelement = 'batch'; $keyforaliasextra = 'extra';
  227. include DOL_DOCUMENT_ROOT.'/core/extrafieldsinexport.inc.php';
  228. $this->export_sql_start[$r] = 'SELECT DISTINCT ';
  229. $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)';
  230. $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';
  231. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product_lot_extrafields as extra ON extra.fk_object = pl.rowid,';
  232. $this->export_sql_end[$r] .= ' '.MAIN_DB_PREFIX.'entrepot as e';
  233. $this->export_sql_end[$r] .= ' WHERE p.rowid = ps.fk_product AND ps.fk_entrepot = e.rowid AND ps.rowid = pb.fk_product_stock';
  234. $this->export_sql_end[$r] .= ' AND e.entity IN ('.getEntity('stock').')'; }
  235. // Export of stock movements
  236. $r++;
  237. $this->export_code[$r] = $this->rights_class.'_movement';
  238. $this->export_label[$r] = "StockMovements"; // Translation key (used only if key ExportDataset_xxx_z not found)
  239. $this->export_icon[$r] = "movement";
  240. $this->export_permission[$r] = array(array("stock", "lire"));
  241. $this->export_fields_array[$r] = array(
  242. 'sm.rowid'=>'MovementId', 'sm.value'=>'Qty', 'sm.datem'=>'DateMovement', 'sm.label'=>'MovementLabel', 'sm.inventorycode'=>'InventoryCode',
  243. 'e.rowid'=>'IdWarehouse', 'e.ref'=>'LocationSummary', 'e.description'=>'DescWareHouse', 'e.lieu'=>'LieuWareHouse', 'e.address'=>'Address', 'e.zip'=>'Zip', 'e.town'=>'Town',
  244. 'p.rowid'=>"ProductId", 'p.ref'=>"Ref", 'p.fk_product_type'=>"Type", 'p.label'=>"Label", 'p.description'=>"Description", 'p.note'=>"Note",
  245. 'p.price'=>"Price", 'p.tva_tx'=>'VAT', 'p.tosell'=>"OnSell", 'p.tobuy'=>'OnBuy', 'p.duration'=>"Duration", 'p.datec'=>'DateCreation', 'p.tms'=>'DateModification'
  246. );
  247. $this->export_TypeFields_array[$r] = array(
  248. 'sm.rowid'=>'Numeric', 'sm.value'=>'Numeric', 'sm.datem'=>'Date', 'sm.batch'=>'Text', 'sm.label'=>'Text', 'sm.inventorycode'=>'Text',
  249. 'e.rowid'=>'List:entrepot:ref::stock', 'e.ref'=>'Text', 'e.description'=>'Text', 'e.lieu'=>'Text', 'e.address'=>'Text', 'e.zip'=>'Text', 'e.town'=>'Text',
  250. 'p.rowid'=>"List:product:label::product", 'p.ref'=>"Text", 'p.fk_product_type'=>"Text", 'p.label'=>"Text", 'p.description'=>"Text", 'p.note'=>"Text",
  251. 'p.price'=>"Numeric", 'p.tva_tx'=>'Numeric', 'p.tosell'=>"Boolean", 'p.tobuy'=>"Boolean", 'p.duration'=>"Duree", 'p.datec'=>'Date', 'p.tms'=>'Date'
  252. );
  253. $this->export_entities_array[$r] = array(
  254. 'e.rowid'=>'warehouse', 'e.ref'=>'warehouse', 'e.description'=>'warehouse', 'e.lieu'=>'warehouse', 'e.address'=>'warehouse', 'e.zip'=>'warehouse', 'e.town'=>'warehouse',
  255. 'p.rowid'=>"product", 'p.ref'=>"product", 'p.fk_product_type'=>"product", 'p.label'=>"product", 'p.description'=>"product", 'p.note'=>"product",
  256. 'p.price'=>"product", 'p.tva_tx'=>'product', 'p.tosell'=>"product", 'p.tobuy'=>"product", 'p.duration'=>"product", 'p.datec'=>'product', 'p.tms'=>'product'
  257. ); // We define here only fields that use another icon that the one defined into export_icon
  258. if ($conf->productbatch->enabled)
  259. {
  260. $this->export_fields_array[$r]['sm.batch'] = 'Batch';
  261. $this->export_TypeFields_array[$r]['sm.batch'] = 'Text';
  262. $this->export_entities_array[$r]['sm.batch'] = 'movement';
  263. }
  264. $this->export_aggregate_array[$r] = array('sm.value'=>'SUM'); // TODO Not used yet
  265. $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.
  266. $this->export_sql_start[$r] = 'SELECT DISTINCT ';
  267. $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';
  268. $this->export_sql_end[$r] .= ' WHERE p.rowid = sm.fk_product AND sm.fk_entrepot = e.rowid';
  269. $this->export_sql_end[$r] .= ' AND e.entity IN ('.getEntity('stock').')';
  270. // Imports
  271. //--------
  272. $r = 0;
  273. // Import warehouses
  274. $r++;
  275. $this->import_code[$r] = $this->rights_class.'_'.$r;
  276. $this->import_label[$r] = "Warehouses"; // Translation key
  277. $this->import_icon[$r] = "warehouse";
  278. $this->import_entities_array[$r] = array(); // We define here only fields that use another icon that the one defined into import_icon
  279. $this->import_tables_array[$r] = array('e'=>MAIN_DB_PREFIX.'entrepot');
  280. $this->import_tables_creator_array[$r] = array('e'=>'fk_user_author');
  281. $this->import_fields_array[$r] = array('e.ref'=>"LocationSummary*",
  282. 'e.description'=>"DescWareHouse",
  283. 'e.lieu'=>"LieuWareHouse",
  284. 'e.address'=>"Address",
  285. 'e.zip'=>'Zip',
  286. 'e.fk_departement'=>'StateCode',
  287. 'e.fk_pays'=>'CountryCode',
  288. 'e.phone'=>'Phone',
  289. 'e.fax'=>'Fax',
  290. 'e.statut'=>'Status',
  291. 'e.fk_parent'=>'ParentWarehouse'
  292. );
  293. $this->import_convertvalue_array[$r] = array(
  294. 'e.fk_departement'=>array('rule'=>'fetchidfromcodeid', 'classfile'=>'/core/class/cstate.class.php', 'class'=>'Cstate', 'method'=>'fetch', 'dict'=>'DictionaryStateCode'),
  295. 'e.fk_pays'=>array('rule'=>'fetchidfromcodeid', 'classfile'=>'/core/class/ccountry.class.php', 'class'=>'Ccountry', 'method'=>'fetch', 'dict'=>'DictionaryCountry'),
  296. 'e.fk_parent'=>array('rule'=>'fetchidfromref', 'classfile'=>'/product/stock/class/entrepot.class.php', 'class'=>'Entrepot', 'method'=>'fetch', 'element'=>'ref')
  297. );
  298. $this->import_regex_array[$r] = array('e.statut'=>'^[0|1]');
  299. $this->import_examplevalues_array[$r] = array('e.ref'=>"ALM001",
  300. 'e.description'=>"Central Warehouse",
  301. 'e.lieu'=>"Central",
  302. 'e.address'=>"Route 66",
  303. 'e.zip'=>'28080',
  304. 'e.fk_departement'=>'matches field "code_departement" in table "'.MAIN_DB_PREFIX.'c_departements"',
  305. 'e.fk_pays'=>'US/FR/DE etc. matches field "code" in table "'.MAIN_DB_PREFIX.'c_country"',
  306. 'e.phone'=>'(+33)(0)123456789',
  307. 'e.fax'=>'(+33)(0)123456790',
  308. 'e.statut'=>'1',
  309. 'e.fk_parent'=>'id or ref in this table'
  310. );
  311. $this->import_updatekeys_array[$r] = array('p.ref'=>'Ref');
  312. // Import stocks
  313. $r++;
  314. $this->import_code[$r] = $this->rights_class.'_'.$r;
  315. $this->import_label[$r] = "Stocks"; // Translation key
  316. $this->import_icon[$r] = "stock";
  317. $this->import_entities_array[$r] = array(); // We define here only fields that use another icon that the one defined into import_icon
  318. $this->import_tables_array[$r] = array('ps'=>MAIN_DB_PREFIX.'product_stock');
  319. $this->import_fields_array[$r] = array('ps.fk_product'=>"Product*", 'ps.fk_entrepot'=>"Warehouse*", 'ps.reel'=>"Stock*");
  320. $this->import_convertvalue_array[$r] = array(
  321. 'ps.fk_product'=>array('rule'=>'fetchidfromref', 'classfile'=>'/product/class/product.class.php', 'class'=>'Product', 'method'=>'fetch', 'element'=>'product'),
  322. 'ps.fk_entrepot'=>array('rule'=>'fetchidfromref', 'classfile'=>'/product/stock/class/entrepot.class.php', 'class'=>'Entrepot', 'method'=>'fetch', 'element'=>'ref')
  323. );
  324. $this->import_examplevalues_array[$r] = array(
  325. 'ps.fk_product'=>"id or ref", 'ps.fk_entrepot'=>"id or ref", 'ps.reel'=>"10"
  326. );
  327. $this->import_updatekeys_array[$r] = array('ps.fk_product'=>'Product', 'ps.fk_entrepot'=>"Warehouse");
  328. $this->import_run_sql_after_array[$r] = array( // Because we may change data that are denormalized, we must update dernormalized data after.
  329. 'UPDATE '.MAIN_DB_PREFIX.'product p SET p.stock= (SELECT SUM(ps.reel) FROM '.MAIN_DB_PREFIX.'product_stock ps WHERE ps.fk_product = p.rowid);'
  330. );
  331. }
  332. /**
  333. * Function called when module is enabled.
  334. * The init function add constants, boxes, permissions and menus (defined in constructor) into Dolibarr database.
  335. * It also creates data directories
  336. *
  337. * @param string $options Options when enabling module ('', 'noboxes')
  338. * @return int 1 if OK, 0 if KO
  339. */
  340. public function init($options = '')
  341. {
  342. global $conf, $langs;
  343. // Permissions
  344. $this->remove($options);
  345. //ODT template
  346. $src = DOL_DOCUMENT_ROOT.'/install/doctemplates/stock/template_stock.odt';
  347. $dirodt = DOL_DATA_ROOT.'/doctemplates/stock';
  348. $dest = $dirodt.'/template_stock.odt';
  349. if (file_exists($src) && !file_exists($dest))
  350. {
  351. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  352. dol_mkdir($dirodt);
  353. $result = dol_copy($src, $dest, 0, 0);
  354. if ($result < 0)
  355. {
  356. $langs->load("errors");
  357. $this->error = $langs->trans('ErrorFailToCopyFile', $src, $dest);
  358. return 0;
  359. }
  360. }
  361. $sql = array();
  362. $sql = array(
  363. "DELETE FROM ".MAIN_DB_PREFIX."document_model WHERE nom = '".$this->db->escape($this->const[1][2])."' AND type = 'stock' AND entity = ".$conf->entity,
  364. "INSERT INTO ".MAIN_DB_PREFIX."document_model (nom, type, entity) VALUES('".$this->db->escape($this->const[1][2])."','stock',".$conf->entity.")",
  365. "DELETE FROM ".MAIN_DB_PREFIX."document_model WHERE nom = '".$this->db->escape($this->const[2][2])."' AND type = 'mouvement' AND entity = ".$conf->entity,
  366. "INSERT INTO ".MAIN_DB_PREFIX."document_model (nom, type, entity) VALUES('".$this->db->escape($this->const[2][2])."','mouvement',".$conf->entity.")",
  367. );
  368. return $this->_init($sql, $options);
  369. }
  370. }