dolibarr  19.0.0-dev
modStock.class.php
Go to the documentation of this file.
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 
30 include_once DOL_DOCUMENT_ROOT.'/core/modules/DolibarrModules.class.php';
31 
32 
37 {
43  public function __construct($db)
44  {
45  global $conf, $langs;
46 
47  $this->db = $db;
48  $this->numero = 52;
49 
50  $this->family = "products";
51  $this->module_position = '39';
52  // Module label (no space allowed), used if translation string 'ModuleXXXName' not found (where XXX is value of numeric property 'numero' of module)
53  $this->name = preg_replace('/^mod/i', '', get_class($this));
54  $this->description = "Gestion des stocks";
55 
56  // Possible values for version are: 'development', 'experimental', 'dolibarr' or version
57  $this->version = 'dolibarr';
58 
59  $this->const_name = 'MAIN_MODULE_'.strtoupper($this->name);
60  $this->picto = 'stock';
61 
62  // Data directories to create when module is enabled
63  $this->dirs = array("/stock/temp");
64 
65  $this->config_page_url = array("stock.php");
66 
67  // Dependencies
68  $this->hidden = false; // A condition to hide module
69  $this->depends = array("modProduct"); // List of module class names as string that must be enabled if this module is enabled
70  $this->requiredby = array("modProductBatch"); // List of module ids to disable if this one is disabled
71  $this->conflictwith = array(); // List of module class names as string this module is in conflict with
72  $this->phpmin = array(7, 0); // Minimum version of PHP required by module
73  $this->langfiles = array("stocks");
74 
75  // Constants
76  $this->const = array();
77  $r = 0;
78 
79  $this->const[$r] = array('STOCK_ALLOW_NEGATIVE_TRANSFER', 'chaine', '1', '', 1);
80 
81  $r++;
82  $this->const[$r][0] = "STOCK_ADDON_PDF";
83  $this->const[$r][1] = "chaine";
84  $this->const[$r][2] = "standard";
85  $this->const[$r][3] = 'Name of PDF model of stock';
86  $this->const[$r][4] = 0;
87 
88  $r++;
89  $this->const[$r][0] = "MOUVEMENT_ADDON_PDF";
90  $this->const[$r][1] = "chaine";
91  $this->const[$r][2] = "stdmovement";
92  $this->const[$r][3] = 'Name of PDF model of stock mouvement';
93  $this->const[$r][4] = 0;
94 
95  $r++;
96  $this->const[$r][0] = "STOCK_ADDON_PDF_ODT_PATH";
97  $this->const[$r][1] = "chaine";
98  $this->const[$r][2] = "DOL_DATA_ROOT/doctemplates/stocks";
99  $this->const[$r][3] = "";
100  $this->const[$r][4] = 0;
101 
102  $r++;
103  $this->const[$r][0] = "MOUVEMENT_ADDON_PDF_ODT_PATH";
104  $this->const[$r][1] = "chaine";
105  $this->const[$r][2] = "DOL_DATA_ROOT/doctemplates/stocks/mouvements";
106  $this->const[$r][3] = "";
107  $this->const[$r][4] = 0;
108 
109  // Boxes
110  $this->boxes = array();
111 
112  // Permissions
113  $this->rights = array();
114  $this->rights_class = 'stock';
115 
116  $this->rights[0][0] = 1001;
117  $this->rights[0][1] = 'Lire les stocks';
118  $this->rights[0][2] = 'r';
119  $this->rights[0][3] = 0;
120  $this->rights[0][4] = 'lire';
121  $this->rights[0][5] = '';
122 
123  $this->rights[1][0] = 1002;
124  $this->rights[1][1] = 'Creer/Modifier les stocks';
125  $this->rights[1][2] = 'w';
126  $this->rights[1][3] = 0;
127  $this->rights[1][4] = 'creer';
128  $this->rights[1][5] = '';
129 
130  $this->rights[2][0] = 1003;
131  $this->rights[2][1] = 'Supprimer les stocks';
132  $this->rights[2][2] = 'd';
133  $this->rights[2][3] = 0;
134  $this->rights[2][4] = 'supprimer';
135  $this->rights[2][5] = '';
136 
137  $this->rights[3][0] = 1004;
138  $this->rights[3][1] = 'Lire mouvements de stocks';
139  $this->rights[3][2] = 'r';
140  $this->rights[3][3] = 0;
141  $this->rights[3][4] = 'mouvement';
142  $this->rights[3][5] = 'lire';
143 
144  $this->rights[4][0] = 1005;
145  $this->rights[4][1] = 'Creer/modifier mouvements de stocks';
146  $this->rights[4][2] = 'w';
147  $this->rights[4][3] = 0;
148  $this->rights[4][4] = 'mouvement';
149  $this->rights[4][5] = 'creer';
150 
151  $this->rights[5][0] = 1011;
152  $this->rights[5][1] = 'inventoryReadPermission'; // Permission label
153  $this->rights[5][3] = 0; // Permission by default for new user (0/1)
154  $this->rights[5][4] = 'inventory_advance'; // In php code, permission will be checked by test if ($user->rights->permkey->level1->level2)
155  $this->rights[5][5] = 'read'; // In php code, permission will be checked by test if ($user->rights->permkey->level1->level2)
156 
157  $this->rights[6][0] = 1012;
158  $this->rights[6][1] = 'inventoryCreatePermission'; // Permission label
159  $this->rights[6][3] = 0; // Permission by default for new user (0/1)
160  $this->rights[6][4] = 'inventory_advance'; // In php code, permission will be checked by test if ($user->rights->permkey->level1->level2)
161  $this->rights[6][5] = 'write'; // In php code, permission will be checked by test if ($user->rights->permkey->level1->level2)
162 
163  if ($conf->global->MAIN_FEATURES_LEVEL >= 2) {
164  $this->rights[8][0] = 1014;
165  $this->rights[8][1] = 'inventoryValidatePermission'; // Permission label
166  $this->rights[8][3] = 0; // Permission by default for new user (0/1)
167  $this->rights[8][4] = 'inventory_advance'; // In php code, permission will be checked by test if ($user->rights->permkey->level1->level2)
168  $this->rights[8][5] = 'validate'; // In php code, permission will be checked by test if ($user->rights->permkey->level1->level2)
169 
170  $this->rights[9][0] = 1015;
171  $this->rights[9][1] = 'inventoryChangePMPPermission'; // Permission label
172  $this->rights[9][3] = 0; // Permission by default for new user (0/1)
173  $this->rights[9][4] = 'inventory_advance'; // In php code, permission will be checked by test if ($user->rights->permkey->level1->level2)
174  $this->rights[9][5] = 'changePMP'; // In php code, permission will be checked by test if ($user->rights->permkey->level1->level2)
175 
176  $this->rights[10][0] = 1016;
177  $this->rights[10][1] = 'inventoryDeletePermission'; // Permission label
178  $this->rights[10][3] = 0; // Permission by default for new user (0/1)
179  $this->rights[10][4] = 'inventory_advance'; // In php code, permission will be checked by test if ($user->rights->permkey->level1->level2)
180  $this->rights[10][5] = 'delete'; // In php code, permission will be checked by test if ($user->rights->permkey->level1->level2)
181  }
182 
183  // Main menu entries
184  $this->menu = array(); // List of menus to add
185  $r = 0;
186 
187  // Menus
188  //-------
189  $this->menu = 1; // This module add menu entries. They are coded into menu manager.
190 
191 
192  // Exports
193  //--------
194  $r = 0;
195 
196  // Export warehouses
197  $r++;
198  $this->export_code[$r] = $this->rights_class.'_emplacement';
199  $this->export_label[$r] = "Warehouses"; // Translation key (used only if key ExportDataset_xxx_z not found)
200  $this->export_icon[$r] = "warehouse";
201  $this->export_permission[$r] = array(array("stock", "lire"));
202  $this->export_fields_array[$r] = array(
203  'e.rowid'=>'IdWarehouse', 'e.ref'=>'LocationSummary', 'e.description'=>'DescWareHouse', 'e.lieu'=>'LieuWareHouse', 'e.address'=>'Address', 'e.zip'=>'Zip', 'e.town'=>'Town',
204  'd.code_departement'=>'Departement', 'c.code'=>'CountryCode',
205  'e.phone'=>'Phone', 'e.fax'=>'Fax', 'e.statut'=>'Status', 'pe.rowid'=>'ParentWarehouse', 'pe.ref'=>'LocationSummary'
206  );
207  $this->export_TypeFields_array[$r] = array(
208  'e.ref'=>'Text', 'e.description'=>'Text', 'e.lieu'=>'Text', 'e.address'=>'Text', 'e.zip'=>'Text', 'e.town'=>'Text',
209  'd.code_departement'=>'List:c_departements:code_departement:code_departement:', 'c.code'=>'List:c_country:code:code:',
210  'e.phone'=>'Text', 'e.fax'=>'Text', 'e.statut'=>'Text', 'pe.rowid'=>'List:entrepot:ref:rowid:stock', 'pe.ref'=>'Text'
211  );
212  $this->export_entities_array[$r] = array(); // We define here only fields that use another icon that the one defined into export_icon
213  $this->export_aggregate_array[$r] = array(); // TODO Not used yet
214  $keyforselect = 'warehouse';
215  $keyforelement = 'warehouse';
216  $keyforaliasextra = 'extra';
217  include DOL_DOCUMENT_ROOT.'/core/extrafieldsinexport.inc.php';
218 
219  $this->export_sql_start[$r] = 'SELECT DISTINCT ';
220  $this->export_sql_end[$r] = ' FROM '.MAIN_DB_PREFIX.'entrepot as e';
221  $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_departements as d ON d.rowid = e.fk_departement';
222  $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_country as c ON c.rowid = e.fk_pays';
223  $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'entrepot as pe ON pe.rowid = e.fk_parent';
224  $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'entrepot_extrafields as extra ON extra.fk_object = e.rowid';
225  $this->export_sql_end[$r] .= ' WHERE e.entity IN ('.getEntity('stock').')';
226 
227  // Export stock (without batch number)
228  $r++;
229  $this->export_code[$r] = $this->rights_class.'_emplacement_product';
230  $this->export_label[$r] = "Stocks"; // Translation key (used only if key ExportDataset_xxx_z not found)
231  $this->export_icon[$r] = "warehouse";
232  $this->export_permission[$r] = array(array("stock", "lire"));
233  $this->export_fields_array[$r] = array(
234  'e.rowid'=>'IdWarehouse', 'e.ref'=>'LocationSummary', 'e.description'=>'DescWareHouse', 'e.lieu'=>'LieuWareHouse', 'e.address'=>'Address', 'e.zip'=>'Zip', 'e.town'=>'Town',
235  'p.rowid'=>"ProductId", 'p.ref'=>"Ref", 'p.fk_product_type'=>"Type", 'p.label'=>"Label", 'p.description'=>"Description", 'p.note'=>"Note",
236  'p.price'=>"Price", 'p.tva_tx'=>'VAT', 'p.tosell'=>"OnSell", 'p.tobuy'=>'OnBuy', 'p.duration'=>"Duration",
237  'p.datec'=>'DateCreation', 'p.tms'=>'DateModification', 'p.pmp'=>'PMPValue', 'p.cost_price'=>'CostPrice',
238  'p.seuil_stock_alerte'=>'StockLimit',
239  );
240  if (isModEnabled('barcode')) {
241  $this->export_fields_array[$r] = array_merge($this->export_fields_array[$r], array('p.barcode'=>'BarCode'));
242  }
243  $this->export_TypeFields_array[$r] = array(
244  'e.rowid'=>'List:entrepot:ref::stock', 'e.ref'=>'Text', 'e.lieu'=>'Text', 'e.address'=>'Text', 'e.zip'=>'Text', 'e.town'=>'Text',
245  'p.rowid'=>"Numeric", 'p.ref'=>"Text", 'p.fk_product_type'=>"Text", 'p.label'=>"Text", 'p.description'=>"Text", 'p.note'=>"Text",
246  'p.price'=>"Numeric", 'p.tva_tx'=>'Numeric', 'p.tosell'=>"Boolean", 'p.tobuy'=>"Boolean", 'p.duration'=>"Duree",
247  'p.datec'=>'Date', 'p.tms'=>'Date', 'p.pmp'=>'Numeric', 'p.cost_price'=>'Numeric',
248  'ps.reel'=>'Numeric',
249  'p.seuil_stock_alerte'=>'Numeric',
250  );
251  if (isModEnabled('barcode')) {
252  $this->export_TypeFields_array[$r] = array_merge($this->export_TypeFields_array[$r], array('p.barcode'=>'Text'));
253  }
254  $this->export_entities_array[$r] = array(
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",
257  'p.datec'=>'product', 'p.tms'=>'product', 'p.pmp'=>'product', 'p.cost_price'=>'product',
258  'ps.reel'=>'stock',
259  'p.seuil_stock_alerte'=>'product',
260  ); // We define here only fields that use another icon that the one defined into export_icon
261  if (isModEnabled('barcode')) {
262  $this->export_entities_array[$r] = array_merge($this->export_entities_array[$r], array('p.barcode'=>'product'));
263  }
264  $this->export_aggregate_array[$r] = array('ps.reel'=>'SUM'); // TODO Not used yet
265  $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.
266  $keyforselect = 'product';
267  $keyforelement = 'product';
268  $keyforaliasextra = 'extra';
269  include DOL_DOCUMENT_ROOT.'/core/extrafieldsinexport.inc.php';
270  $this->export_fields_array[$r] = array_merge($this->export_fields_array[$r], array('ps.reel'=>'Stock'));
271 
272  $this->export_sql_start[$r] = 'SELECT DISTINCT ';
273  $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';
274  $this->export_sql_end[$r] .= ' WHERE p.rowid = ps.fk_product AND ps.fk_entrepot = e.rowid';
275  $this->export_sql_end[$r] .= ' AND e.entity IN ('.getEntity('stock').')';
276 
277  // Export stock including batch number
278  if (isModEnabled('productbatch')) {
279  $langs->load("productbatch");
280 
281  // This request is same than previous but without field ps.stock (real stock in warehouse) and with link to subtable productbatch
282  $r++;
283 
284  $this->export_code[$r] = $this->rights_class.'_emplacement_product_lot';
285  $this->export_label[$r] = "StocksWithBatch"; // Translation key (used only if key ExportDataset_xxx_z not found)
286  $this->export_icon[$r] = "warehouse";
287  $this->export_permission[$r] = array(array("stock", "lire"));
288  $this->export_fields_array[$r] = array(
289  'e.rowid'=>'IdWarehouse', 'e.ref'=>'LocationSummary', 'e.description'=>'DescWareHouse', 'e.lieu'=>'LieuWareHouse', 'e.address'=>'Address', 'e.zip'=>'Zip', 'e.town'=>'Town',
290  'p.rowid'=>"ProductId", 'p.ref'=>"Ref", 'p.fk_product_type'=>"Type", 'p.label'=>"Label", 'p.description'=>"Description", 'p.note'=>"Note",
291  'p.price'=>"Price", 'p.tva_tx'=>'VAT', 'p.tosell'=>"OnSell", 'p.tobuy'=>'OnBuy', 'p.duration'=>"Duration",
292  'p.datec'=>'DateCreation', 'p.tms'=>'DateModification', 'p.pmp'=>'PMPValue', 'p.cost_price'=>'CostPrice',
293  'pb.rowid'=>'Id', 'pb.batch'=>'Batch', 'pb.qty'=>'Qty',
294  'pl.eatby'=>'EatByDate', 'pl.sellby'=>'SellByDate'
295  );
296  if (isModEnabled('barcode')) {
297  $this->export_fields_array[$r] = array_merge($this->export_fields_array[$r], array('p.barcode'=>'BarCode'));
298  }
299  $this->export_TypeFields_array[$r] = array(
300  'e.rowid'=>'List:entrepot:ref::stock', 'e.ref'=>'Text', 'e.lieu'=>'Text', 'e.description'=>'Text', 'e.address'=>'Text', 'e.zip'=>'Text', 'e.town'=>'Text',
301  'p.rowid'=>"Numeric", 'p.ref'=>"Text", 'p.fk_product_type'=>"Text", 'p.label'=>"Text", 'p.description'=>"Text", 'p.note'=>"Text",
302  'p.price'=>"Numeric", 'p.tva_tx'=>'Numeric', 'p.tosell'=>"Boolean", 'p.tobuy'=>"Boolean", 'p.duration'=>"Duree",
303  'p.datec'=>'DateCreation', 'p.tms'=>'DateModification', 'p.pmp'=>'PMPValue', 'p.cost_price'=>'CostPrice',
304  'pb.batch'=>'Text', 'pb.qty'=>'Numeric',
305  'pl.eatby'=>'Date', 'pl.sellby'=>'Date'
306  );
307  if (isModEnabled('barcode')) {
308  $this->export_TypeFields_array[$r] = array_merge($this->export_TypeFields_array[$r], array('p.barcode'=>'Text'));
309  }
310  $this->export_entities_array[$r] = array(
311  'p.rowid'=>"product", 'p.ref'=>"product", 'p.fk_product_type'=>"product", 'p.label'=>"product", 'p.description'=>"product", 'p.note'=>"product",
312  'p.price'=>"product", 'p.tva_tx'=>'product', 'p.tosell'=>"product", 'p.tobuy'=>"product", 'p.duration'=>"product",
313  'p.datec'=>'product', 'p.tms'=>'product', 'p.pmp'=>'product', 'p.cost_price'=>'product',
314  'pb.rowid'=>'batch', 'pb.batch'=>'batch', 'pb.qty'=>'batch',
315  'pl.eatby'=>'batch', 'pl.sellby'=>'batch'
316  ); // We define here only fields that use another icon that the one defined into export_icon
317  if (isModEnabled('barcode')) {
318  $this->export_entities_array[$r] = array_merge($this->export_entities_array[$r], array('p.barcode'=>'product'));
319  }
320  $this->export_aggregate_array[$r] = array('ps.reel'=>'SUM'); // TODO Not used yet
321  $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.
322  $keyforselect = 'product_lot';
323  $keyforelement = 'batch';
324  $keyforaliasextra = 'extra';
325  include DOL_DOCUMENT_ROOT.'/core/extrafieldsinexport.inc.php';
326 
327  $this->export_sql_start[$r] = 'SELECT DISTINCT ';
328  $this->export_sql_end[$r] = ' FROM '.MAIN_DB_PREFIX.'product_batch as pb';
329  $this->export_sql_end[$r] .= ' INNER JOIN '.MAIN_DB_PREFIX.'product_stock as ps ON ps.rowid = pb.fk_product_stock';
330  $this->export_sql_end[$r] .= ' INNER JOIN '.MAIN_DB_PREFIX.'product as p ON p.rowid = ps.fk_product';
331  $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';
332  $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product_lot_extrafields as extra ON extra.fk_object = pl.rowid,';
333  $this->export_sql_end[$r] .= ' '.MAIN_DB_PREFIX.'entrepot as e';
334  $this->export_sql_end[$r] .= ' WHERE ps.fk_entrepot = e.rowid';
335  $this->export_sql_end[$r] .= ' AND e.entity IN ('.getEntity('stock').')';
336  }
337 
338  // Export of stock movements
339  $r++;
340  $this->export_code[$r] = $this->rights_class.'_movement';
341  $this->export_label[$r] = "StockMovements"; // Translation key (used only if key ExportDataset_xxx_z not found)
342  $this->export_icon[$r] = "movement";
343  $this->export_permission[$r] = array(array("stock", "lire"));
344  $this->export_fields_array[$r] = array(
345  'sm.rowid'=>'MovementId', 'sm.value'=>'Qty', 'sm.datem'=>'DateMovement', 'sm.label'=>'MovementLabel', 'sm.inventorycode'=>'InventoryCode',
346  'e.rowid'=>'IdWarehouse', 'e.ref'=>'LocationSummary', 'e.description'=>'DescWareHouse', 'e.lieu'=>'LieuWareHouse', 'e.address'=>'Address', 'e.zip'=>'Zip', 'e.town'=>'Town',
347  'p.rowid'=>"ProductId", 'p.ref'=>"Ref", 'p.fk_product_type'=>"Type", 'p.label'=>"Label", 'p.description'=>"Description", 'p.note'=>"Note",
348  'p.price'=>"Price", 'p.tva_tx'=>'VAT', 'p.tosell'=>"OnSell", 'p.tobuy'=>'OnBuy', 'p.duration'=>"Duration", 'p.datec'=>'DateCreation', 'p.tms'=>'DateModification'
349  );
350  if (isModEnabled('barcode')) {
351  $this->export_fields_array[$r] = array_merge($this->export_fields_array[$r], array('p.barcode'=>'BarCode'));
352  }
353  $this->export_TypeFields_array[$r] = array(
354  'sm.rowid'=>'Numeric', 'sm.value'=>'Numeric', 'sm.datem'=>'Date', 'sm.batch'=>'Text', 'sm.label'=>'Text', 'sm.inventorycode'=>'Text',
355  'e.rowid'=>'List:entrepot:ref::stock', 'e.ref'=>'Text', 'e.description'=>'Text', 'e.lieu'=>'Text', 'e.address'=>'Text', 'e.zip'=>'Text', 'e.town'=>'Text',
356  'p.rowid'=>"Numeric", 'p.ref'=>"Text", 'p.fk_product_type'=>"Text", 'p.label'=>"Text", 'p.description'=>"Text", 'p.note'=>"Text",
357  'p.price'=>"Numeric", 'p.tva_tx'=>'Numeric', 'p.tosell'=>"Boolean", 'p.tobuy'=>"Boolean", 'p.duration'=>"Duree", 'p.datec'=>'Date', 'p.tms'=>'Date'
358  );
359  if (isModEnabled('barcode')) {
360  $this->export_TypeFields_array[$r] = array_merge($this->export_TypeFields_array[$r], array('p.barcode'=>'Text'));
361  }
362  $this->export_entities_array[$r] = array(
363  'e.rowid'=>'warehouse', 'e.ref'=>'warehouse', 'e.description'=>'warehouse', 'e.lieu'=>'warehouse', 'e.address'=>'warehouse', 'e.zip'=>'warehouse', 'e.town'=>'warehouse',
364  'p.rowid'=>"product", 'p.ref'=>"product", 'p.fk_product_type'=>"product", 'p.label'=>"product", 'p.description'=>"product", 'p.note'=>"product",
365  'p.price'=>"product", 'p.tva_tx'=>'product', 'p.tosell'=>"product", 'p.tobuy'=>"product", 'p.duration'=>"product", 'p.datec'=>'product', 'p.tms'=>'product'
366  ); // We define here only fields that use another icon that the one defined into export_icon
367  if (isModEnabled('productbatch')) {
368  $this->export_fields_array[$r]['sm.batch'] = 'Batch';
369  $this->export_TypeFields_array[$r]['sm.batch'] = 'Text';
370  $this->export_entities_array[$r]['sm.batch'] = 'movement';
371  }
372  if (isModEnabled('barcode')) {
373  $this->export_entities_array[$r] = array_merge($this->export_entities_array[$r], array('p.barcode'=>'product'));
374  }
375  $this->export_aggregate_array[$r] = array('sm.value'=>'SUM'); // TODO Not used yet
376  $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.
377 
378  $this->export_sql_start[$r] = 'SELECT DISTINCT ';
379  $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';
380  $this->export_sql_end[$r] .= ' WHERE p.rowid = sm.fk_product AND sm.fk_entrepot = e.rowid';
381  $this->export_sql_end[$r] .= ' AND e.entity IN ('.getEntity('stock').')';
382 
383  // Imports
384  //--------
385 
386  $r = 0;
387 
388  // Import warehouses
389  $r++;
390  $this->import_code[$r] = $this->rights_class.'_'.$r;
391  $this->import_label[$r] = "Warehouses"; // Translation key
392  $this->import_icon[$r] = "warehouse";
393  $this->import_entities_array[$r] = array(); // We define here only fields that use another icon that the one defined into import_icon
394  $this->import_tables_array[$r] = array('e'=>MAIN_DB_PREFIX.'entrepot');
395  $this->import_tables_creator_array[$r] = array('e'=>'fk_user_author');
396  $this->import_fields_array[$r] = array('e.ref'=>"LocationSummary*",
397  'e.description'=>"DescWareHouse",
398  'e.lieu'=>"LieuWareHouse",
399  'e.address'=>"Address",
400  'e.zip'=>'Zip',
401  'e.fk_departement'=>'StateCode',
402  'e.fk_pays'=>'CountryCode',
403  'e.phone'=>'Phone',
404  'e.fax'=>'Fax',
405  'e.statut'=>'Status',
406  'e.fk_parent'=>'ParentWarehouse'
407  );
408 
409  $this->import_convertvalue_array[$r] = array(
410  'e.fk_departement'=>array('rule'=>'fetchidfromcodeid', 'classfile'=>'/core/class/cstate.class.php', 'class'=>'Cstate', 'method'=>'fetch', 'dict'=>'DictionaryStateCode'),
411  'e.fk_pays'=>array('rule'=>'fetchidfromcodeid', 'classfile'=>'/core/class/ccountry.class.php', 'class'=>'Ccountry', 'method'=>'fetch', 'dict'=>'DictionaryCountry'),
412  'e.fk_parent'=>array('rule'=>'fetchidfromref', 'classfile'=>'/product/stock/class/entrepot.class.php', 'class'=>'Entrepot', 'method'=>'fetch', 'element'=>'ref')
413  );
414  $this->import_regex_array[$r] = array('e.statut'=>'^[0|1]');
415  $this->import_examplevalues_array[$r] = array('e.ref'=>"ALM001",
416  'e.description'=>"Central Warehouse",
417  'e.lieu'=>"Central",
418  'e.address'=>"Route 66",
419  'e.zip'=>'28080',
420  'e.fk_departement'=>'matches field "code_departement" in table "'.MAIN_DB_PREFIX.'c_departements"',
421  'e.fk_pays'=>'US/FR/DE etc. matches field "code" in table "'.MAIN_DB_PREFIX.'c_country"',
422  'e.phone'=>'(+33)(0)123456789',
423  'e.fax'=>'(+33)(0)123456790',
424  'e.statut'=>'1',
425  'e.fk_parent'=>'id or ref of warehouse'
426  );
427  $this->import_updatekeys_array[$r] = array('p.ref'=>'Ref');
428 
429  // Import stocks
430  $r++;
431  $this->import_code[$r] = $this->rights_class.'_'.$r;
432  $this->import_label[$r] = "Stocks"; // Translation key
433  $this->import_icon[$r] = "stock";
434  $this->import_entities_array[$r] = array(); // We define here only fields that use another icon that the one defined into import_icon
435  $this->import_tables_array[$r] = array('ps'=>MAIN_DB_PREFIX.'product_stock');
436  $this->import_fields_array[$r] = array('ps.fk_product'=>"Product*", 'ps.fk_entrepot'=>"Warehouse*", 'ps.reel'=>"Stock*");
437 
438  $this->import_convertvalue_array[$r] = array(
439  'ps.fk_product'=>array('rule'=>'fetchidfromref', 'classfile'=>'/product/class/product.class.php', 'class'=>'Product', 'method'=>'fetch', 'element'=>'product'),
440  'ps.fk_entrepot'=>array('rule'=>'fetchidfromref', 'classfile'=>'/product/stock/class/entrepot.class.php', 'class'=>'Entrepot', 'method'=>'fetch', 'element'=>'ref')
441  );
442  $this->import_examplevalues_array[$r] = array(
443  'ps.fk_product'=>"id or ref of product", 'ps.fk_entrepot'=>"id or ref of warehouse", 'ps.reel'=>"10"
444  );
445  $this->import_updatekeys_array[$r] = array('ps.fk_product'=>'Product', 'ps.fk_entrepot'=>"Warehouse");
446  $this->import_run_sql_after_array[$r] = array( // Because we may change data that are denormalized, we must update dernormalized data after.
447  '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);'
448  );
449  }
450 
451 
460  public function init($options = '')
461  {
462  global $conf, $langs;
463 
464  $result = $this->_load_tables('/install/mysql/', 'stock');
465  if ($result < 0) {
466  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')
467  }
468 
469  // Permissions
470  $this->remove($options);
471 
472  //ODT template
473  $src = DOL_DOCUMENT_ROOT.'/install/doctemplates/stocks/template_warehouse.odt';
474  $dirodt = DOL_DATA_ROOT.'/doctemplates/stocks';
475  $dest = $dirodt.'/template_warehouse.odt';
476 
477  if (file_exists($src) && !file_exists($dest)) {
478  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
479  dol_mkdir($dirodt);
480  $result = dol_copy($src, $dest, 0, 0);
481  if ($result < 0) {
482  $langs->load("errors");
483  $this->error = $langs->trans('ErrorFailToCopyFile', $src, $dest);
484  return 0;
485  }
486  }
487 
488  $sql = array();
489 
490  $sql = array(
491  "DELETE FROM ".MAIN_DB_PREFIX."document_model WHERE nom = '".$this->db->escape($this->const[1][2])."' AND type = 'stock' AND entity = ".((int) $conf->entity),
492  "INSERT INTO ".MAIN_DB_PREFIX."document_model (nom, type, entity) VALUES('".$this->db->escape($this->const[1][2])."','stock',".((int) $conf->entity).")",
493  "DELETE FROM ".MAIN_DB_PREFIX."document_model WHERE nom = '".$this->db->escape($this->const[2][2])."' AND type = 'mouvement' AND entity = ".((int) $conf->entity),
494  "INSERT INTO ".MAIN_DB_PREFIX."document_model (nom, type, entity) VALUES('".$this->db->escape($this->const[2][2])."','mouvement',".((int) $conf->entity).")",
495  );
496 
497  return $this->_init($sql, $options);
498  }
499 }
Class DolibarrModules.
_init($array_sql, $options='')
Enables a module.
_load_tables($reldir, $onlywithsuffix='')
Create tables and keys required by module:
Class to describe and enable module Stock.
init($options='')
Function called when module is enabled.
__construct($db)
Constructor.
if(isModEnabled('facture') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') && $user->hasRight('don', 'lire')) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $sql
Social contributions to pay.
Definition: index.php:746
print *****$script_file(".$version.") pid cd cd cd description as description
Only used if Module[ID]Desc translation string is not found.
dol_copy($srcfile, $destfile, $newmask=0, $overwriteifexists=1, $testvirus=0, $indexdatabase=0)
Copy a file to another file.
Definition: files.lib.php:717
isModEnabled($module)
Is Dolibarr module enabled.
dol_mkdir($dir, $dataroot='', $newmask='')
Creation of a directory (this can create recursive subdir)
$conf db name
Only used if Module[ID]Name translation string is not found.
Definition: repair.php:123