dolibarr  7.0.0-beta
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@capnetworks.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 
29 include_once DOL_DOCUMENT_ROOT .'/core/modules/DolibarrModules.class.php';
30 
31 
36 {
37 
43  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 = 40;
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->special = 0;
61  $this->picto='stock';
62 
63  // Data directories to create when module is enabled
64  $this->dirs = array();
65 
66  $this->config_page_url = array("stock.php");
67 
68  // Dependencies
69  $this->depends = array("modProduct");
70  $this->requiredby = array("modProductBatch");
71  $this->langfiles = array("stocks");
72 
73  // Constants
74  $this->const = array(
75  0=>array('STOCK_ALLOW_NEGATIVE_TRANSFER','chaine','1','',1)
76  );
77 
78  // Boxes
79  $this->boxes = array();
80 
81  // Permissions
82  $this->rights = array();
83  $this->rights_class = 'stock';
84 
85  $this->rights[0][0] = 1001;
86  $this->rights[0][1] = 'Lire les stocks';
87  $this->rights[0][2] = 'r';
88  $this->rights[0][3] = 0;
89  $this->rights[0][4] = 'lire';
90  $this->rights[0][5] = '';
91 
92  $this->rights[1][0] = 1002;
93  $this->rights[1][1] = 'Creer/Modifier les stocks';
94  $this->rights[1][2] = 'w';
95  $this->rights[1][3] = 0;
96  $this->rights[1][4] = 'creer';
97  $this->rights[1][5] = '';
98 
99  $this->rights[2][0] = 1003;
100  $this->rights[2][1] = 'Supprimer les stocks';
101  $this->rights[2][2] = 'd';
102  $this->rights[2][3] = 0;
103  $this->rights[2][4] = 'supprimer';
104  $this->rights[2][5] = '';
105 
106  $this->rights[3][0] = 1004;
107  $this->rights[3][1] = 'Lire mouvements de stocks';
108  $this->rights[3][2] = 'r';
109  $this->rights[3][3] = 0;
110  $this->rights[3][4] = 'mouvement';
111  $this->rights[3][5] = 'lire';
112 
113  $this->rights[4][0] = 1005;
114  $this->rights[4][1] = 'Creer/modifier mouvements de stocks';
115  $this->rights[4][2] = 'w';
116  $this->rights[4][3] = 0;
117  $this->rights[4][4] = 'mouvement';
118  $this->rights[4][5] = 'creer';
119 
120  if ($conf->global->MAIN_FEATURES_LEVEL >= 2) {
121 
122  $this->rights[5][0] = 1011;
123  $this->rights[5][1] = 'inventoryReadPermission'; // Permission label
124  $this->rights[5][3] = 0; // Permission by default for new user (0/1)
125  $this->rights[5][4] = 'advance_inventory'; // In php code, permission will be checked by test if ($user->rights->permkey->level1->level2)
126  $this->rights[5][5] = 'read'; // In php code, permission will be checked by test if ($user->rights->permkey->level1->level2)
127 
128  $this->rights[6][0] = 1012;
129  $this->rights[6][1] = 'inventoryCreatePermission'; // Permission label
130  $this->rights[6][3] = 0; // Permission by default for new user (0/1)
131  $this->rights[6][4] = 'advance_inventory'; // In php code, permission will be checked by test if ($user->rights->permkey->level1->level2)
132  $this->rights[6][5] = 'create'; // In php code, permission will be checked by test if ($user->rights->permkey->level1->level2)
133 
134  $this->rights[7][0] = 1013;
135  $this->rights[7][1] = 'inventoryWritePermission'; // Permission label
136  $this->rights[7][3] = 0; // Permission by default for new user (0/1)
137  $this->rights[7][4] = 'advance_inventory'; // In php code, permission will be checked by test if ($user->rights->permkey->level1->level2)
138  $this->rights[7][5] = 'write'; // In php code, permission will be checked by test if ($user->rights->permkey->level1->level2)
139 
140  $this->rights[8][0] = 1014;
141  $this->rights[8][1] = 'inventoryValidatePermission'; // Permission label
142  $this->rights[8][3] = 0; // Permission by default for new user (0/1)
143  $this->rights[8][4] = 'advance_inventory'; // In php code, permission will be checked by test if ($user->rights->permkey->level1->level2)
144  $this->rights[8][5] = 'validate'; // In php code, permission will be checked by test if ($user->rights->permkey->level1->level2)
145 
146  $this->rights[9][0] = 1015;
147  $this->rights[9][1] = 'inventoryChangePMPPermission'; // Permission label
148  $this->rights[9][3] = 0; // Permission by default for new user (0/1)
149  $this->rights[9][4] = 'advance_inventory'; // In php code, permission will be checked by test if ($user->rights->permkey->level1->level2)
150  $this->rights[9][5] = 'changePMP'; // In php code, permission will be checked by test if ($user->rights->permkey->level1->level2)
151 
152  }
153 
154  // Main menu entries
155  $this->menu = array(); // List of menus to add
156  $r=0;
157 
158  // Menus
159  //-------
160  $this->menu = 1; // This module add menu entries. They are coded into menu manager.
161 
162 
163  // Exports
164  //--------
165  $r=0;
166 
167  $r++;
168  $this->export_code[$r]=$this->rights_class;
169  $this->export_label[$r]="WarehousesAndProducts"; // Translation key (used only if key ExportDataset_xxx_z not found)
170  $this->export_permission[$r]=array(array("stock","lire"));
171  $this->export_fields_array[$r]=array('e.rowid'=>'IdWarehouse','e.ref'=>'LocationSummary','e.description'=>'DescWareHouse','e.lieu'=>'LieuWareHouse','e.address'=>'Address','e.zip'=>'Zip','e.town'=>'Town','p.rowid'=>"ProductId",'p.ref'=>"Ref",'p.fk_product_type'=>"Type",'p.label'=>"Label",'p.description'=>"Description",'p.note'=>"Note",'p.price'=>"Price",'p.tva_tx'=>'VAT','p.tosell'=>"OnSell",'p.tobuy'=>'OnBuy','p.duration'=>"Duration",'p.datec'=>'DateCreation','p.tms'=>'DateModification','p.pmp'=>'PMPValue','p.cost_price'=>'CostPrice','ps.reel'=>'Stock');
172  $this->export_TypeFields_array[$r]=array('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",'p.ref'=>"Text",'p.fk_product_type'=>"Text",'p.label'=>"Text",'p.description'=>"Text",'p.note'=>"Text",'p.price'=>"Numeric",'p.tva_tx'=>'Numeric','p.tosell'=>"Boolean",'p.tobuy'=>"Boolean",'p.duration'=>"Duree",'p.datec'=>'Date','p.tms'=>'Date','p.pmp'=>'Numeric','p.cost_price'=>'Numeric','ps.reel'=>'Numeric');
173  $this->export_entities_array[$r]=array('e.rowid'=>'warehouse','e.ref'=>'warehouse','e.description'=>'warehouse','e.lieu'=>'warehouse','e.address'=>'warehouse','e.zip'=>'warehouse','e.town'=>'warehouse','p.rowid'=>"product",'p.ref'=>"product",'p.fk_product_type'=>"product",'p.label'=>"product",'p.description'=>"product",'p.note'=>"product",'p.price'=>"product",'p.tva_tx'=>'product','p.tosell'=>"product",'p.tobuy'=>"product",'p.duration'=>"product",'p.datec'=>'product','p.tms'=>'product','p.pmp'=>'product','p.cost_price'=>'product','ps.reel'=>'stock');
174  $this->export_aggregate_array[$r]=array('ps.reel'=>'SUM'); // TODO Not used yet
175  $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.
176  $this->export_sql_start[$r]='SELECT DISTINCT ';
177  $this->export_sql_end[$r] =' FROM '.MAIN_DB_PREFIX.'product as p, '.MAIN_DB_PREFIX.'product_stock as ps, '.MAIN_DB_PREFIX.'entrepot as e';
178  $this->export_sql_end[$r] .=' WHERE p.rowid = ps.fk_product AND ps.fk_entrepot = e.rowid';
179  $this->export_sql_end[$r] .=' AND e.entity IN ('.getEntity('stock').')';
180 
181  if ($conf->productbatch->enabled)
182  {
183  $langs->load("productbatch");
184 
185  // This request is same than previous but without field ps.stock (real stock in warehouse) and with link to subtable productbatch
186  $r++;
187 
188  $this->export_code[$r]=$this->rights_class.'_lot';
189  $this->export_label[$r]="WarehousesAndProductsBatchDetail"; // Translation key (used only if key ExportDataset_xxx_z not found)
190  $this->export_permission[$r]=array(array("stock","lire"));
191  $this->export_fields_array[$r]=array('e.rowid'=>'IdWarehouse','e.ref'=>'LocationSummary','e.description'=>'DescWareHouse','e.lieu'=>'LieuWareHouse','e.address'=>'Address','e.zip'=>'Zip','e.town'=>'Town','p.rowid'=>"ProductId",'p.ref'=>"Ref",'p.fk_product_type'=>"Type",'p.label'=>"Label",'p.description'=>"Description",'p.note'=>"Note",'p.price'=>"Price",'p.tva_tx'=>'VAT','p.tosell'=>"OnSell",'p.tobuy'=>'OnBuy','p.duration'=>"Duration",'p.datec'=>'DateCreation','p.tms'=>'DateModification','pb.rowid'=>'Id','pb.batch'=>'Batch','pb.qty'=>'Qty','pl.eatby'=>'EatByDate','pl.sellby'=>'SellByDate');
192  $this->export_TypeFields_array[$r]=array('e.rowid'=>'List:entrepot:ref','e.ref'=>'Text','e.lieu'=>'Text','e.description'=>'Text','e.address'=>'Text','e.zip'=>'Text','e.town'=>'Text','p.rowid'=>"List:product:label",'p.ref'=>"Text",'p.fk_product_type'=>"Text",'p.label'=>"Text",'p.description'=>"Text",'p.note'=>"Text",'p.price'=>"Numeric",'p.tva_tx'=>'Numeric','p.tosell'=>"Boolean",'p.tobuy'=>"Boolean",'p.duration'=>"Duree",'p.datec'=>'Date','p.tms'=>'Date','pb.batch'=>'Text','pb.qty'=>'Numeric','pl.eatby'=>'Date','pl.sellby'=>'Date');
193  $this->export_entities_array[$r]=array('e.rowid'=>'warehouse','e.ref'=>'warehouse','e.description'=>'warehouse','e.lieu'=>'warehouse','e.address'=>'warehouse','e.zip'=>'warehouse','e.town'=>'warehouse','p.rowid'=>"product",'p.ref'=>"product",'p.fk_product_type'=>"product",'p.label'=>"product",'p.description'=>"product",'p.note'=>"product",'p.price'=>"product",'p.tva_tx'=>'product','p.tosell'=>"product",'p.tobuy'=>"product",'p.duration'=>"product",'p.datec'=>'product','p.tms'=>'product','pb.rowid'=>'stockbatch','pb.batch'=>'stockbatch','pb.qty'=>'stockbatch','pl.eatby'=>'batch','pl.sellby'=>'batch');
194  $this->export_aggregate_array[$r]=array('ps.reel'=>'SUM'); // TODO Not used yet
195  $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.
196  $keyforselect='product_lot'; $keyforelement='batch'; $keyforaliasextra='extra';
197  include DOL_DOCUMENT_ROOT.'/core/extrafieldsinexport.inc.php';
198  $this->export_sql_start[$r]='SELECT DISTINCT ';
199  $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)';
200  $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';
201  $this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'product_lot_extrafields as extra ON extra.fk_object = pl.rowid,';
202  $this->export_sql_end[$r] .=' '.MAIN_DB_PREFIX.'entrepot as e';
203  $this->export_sql_end[$r] .=' WHERE p.rowid = ps.fk_product AND ps.fk_entrepot = e.rowid AND ps.rowid = pb.fk_product_stock';
204  $this->export_sql_end[$r] .=' AND e.entity IN ('.getEntity('stock').')';
205  }
206 
207  $r++;
208  $this->export_code[$r]=$this->rights_class.'_movement';
209  $this->export_label[$r]="StockMovements"; // Translation key (used only if key ExportDataset_xxx_z not found)
210  $this->export_permission[$r]=array(array("stock","lire"));
211  $this->export_fields_array[$r]=array('e.rowid'=>'IdWarehouse','e.ref'=>'LocationSummary','e.description'=>'DescWareHouse','e.lieu'=>'LieuWareHouse','e.address'=>'Address','e.zip'=>'Zip','e.town'=>'Town','p.rowid'=>"ProductId",'p.ref'=>"Ref",'p.fk_product_type'=>"Type",'p.label'=>"Label",'p.description'=>"Description",'p.note'=>"Note",'p.price'=>"Price",'p.tva_tx'=>'VAT','p.tosell'=>"OnSell",'p.tobuy'=>'OnBuy','p.duration'=>"Duration",'p.datec'=>'DateCreation','p.tms'=>'DateModification','sm.rowid'=>'MovementId','sm.value'=>'Qty','sm.datem'=>'DateMovement','sm.label'=>'LabelMovement','sm.inventorycode'=>'InventoryCode');
212  $this->export_TypeFields_array[$r]=array('e.rowid'=>'List:entrepot:ref','e.ref'=>'Text','e.description'=>'Text','e.lieu'=>'Text','e.address'=>'Text','e.zip'=>'Text','e.town'=>'Text','p.rowid'=>"List:product:label",'p.ref'=>"Text",'p.fk_product_type'=>"Text",'p.label'=>"Text",'p.description'=>"Text",'p.note'=>"Text",'p.price'=>"Numeric",'p.tva_tx'=>'Numeric','p.tosell'=>"Boolean",'p.tobuy'=>"Boolean",'p.duration'=>"Duree",'p.datec'=>'Date','p.tms'=>'Date','sm.rowid'=>'Numeric','sm.value'=>'Numeric','sm.datem'=>'Date','sm.batch'=>'Text','sm.label'=>'Text','sm.inventorycode'=>'Text');
213  $this->export_entities_array[$r]=array('e.rowid'=>'warehouse','e.ref'=>'warehouse','e.description'=>'warehouse','e.lieu'=>'warehouse','e.address'=>'warehouse','e.zip'=>'warehouse','e.town'=>'warehouse','p.rowid'=>"product",'p.ref'=>"product",'p.fk_product_type'=>"product",'p.label'=>"product",'p.description'=>"product",'p.note'=>"product",'p.price'=>"product",'p.tva_tx'=>'product','p.tosell'=>"product",'p.tobuy'=>"product",'p.duration'=>"product",'p.datec'=>'product','p.tms'=>'product','sm.rowid'=>'movement','sm.value'=>'movement','sm.datem'=>'movement','sm.label'=>'movement','sm.inventorycode'=>'movement');
214  if ($conf->productbatch->enabled)
215  {
216  $this->export_fields_array[$r]['sm.batch']='Batch';
217  $this->export_TypeFields_array[$r]['sm.batch']='Text';
218  $this->export_entities_array[$r]['sm.batch']='movement';
219  }
220  $this->export_aggregate_array[$r]=array('sm.value'=>'SUM'); // TODO Not used yet
221  $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.
222  $this->export_sql_start[$r]='SELECT DISTINCT ';
223  $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';
224  $this->export_sql_end[$r] .=' WHERE p.rowid = sm.fk_product AND sm.fk_entrepot = e.rowid';
225  $this->export_sql_end[$r] .=' AND e.entity IN ('.getEntity('stock').')';
226 
227  // Imports
228  //--------
229 
230  $r=0;
231 
232  // Import warehouses
233  $r++;
234  $this->import_code[$r]=$this->rights_class.'_'.$r;
235  $this->import_label[$r]="Warehouses"; // Translation key
236  $this->import_icon[$r]=$this->picto;
237  $this->import_entities_array[$r]=array(); // We define here only fields that use another icon that the one defined into import_icon
238  $this->import_tables_array[$r]=array('e'=>MAIN_DB_PREFIX.'entrepot');
239  $this->import_tables_creator_array[$r]=array('e'=>'fk_user_author');
240  $this->import_fields_array[$r]=array('e.ref'=>"LocationSummary*",
241  'e.description'=>"DescWareHouse",'e.lieu'=>"LieuWareHouse",
242  'e.address'=>"Address",'e.zip'=>'Zip','e.fk_pays'=>'CountryCode',
243  'e.statut'=>'Status'
244  );
245 
246  $this->import_convertvalue_array[$r]=array(
247  'e.fk_pays'=>array('rule'=>'fetchidfromcodeid','classfile'=>'/core/class/ccountry.class.php','class'=>'Ccountry','method'=>'fetch','dict'=>'DictionaryCountry')
248  );
249  $this->import_regex_array[$r]=array('e.statut'=>'^[0|1]');
250  $this->import_examplevalues_array[$r]=array('e.ref'=>"ALM001",
251  'e.description'=>"Central Warehouse",'e.lieu'=>"Central",
252  'e.address'=>"Route 66",'e.zip'=>'28080','e.fk_pays'=>'US',
253  'e.statut'=>'1');
254 
255  // Import stocks
256  $r++;
257  $this->import_code[$r]=$this->rights_class.'_'.$r;
258  $this->import_label[$r]="Stocks"; // Translation key
259  $this->import_icon[$r]=$this->picto;
260  $this->import_entities_array[$r]=array(); // We define here only fields that use another icon that the one defined into import_icon
261  $this->import_tables_array[$r]=array('ps'=>MAIN_DB_PREFIX.'product_stock');
262  $this->import_fields_array[$r]=array('ps.fk_product'=>"Product*",'ps.fk_entrepot'=>"Warehouse*",'ps.reel'=>"Stock*");
263 
264  $this->import_convertvalue_array[$r]=array(
265  'ps.fk_product'=>array('rule'=>'fetchidfromref','classfile'=>'/product/class/product.class.php','class'=>'Product','method'=>'fetch','element'=>'product'),
266  'ps.fk_entrepot'=>array('rule'=>'fetchidfromref','classfile'=>'/product/stock/class/entrepot.class.php','class'=>'Entrepot','method'=>'fetch','element'=>'ref')
267  );
268  $this->import_examplevalues_array[$r]=array(
269  'ps.fk_product'=>"PREF123456",'ps.fk_entrepot'=>"ALM001",'ps.reel'=>"10"
270  );
271  $this->import_run_sql_after_array[$r]=array( // Because we may change data that are denormalized, we must update dernormalized data after.
272  'UPDATE llx_product p SET p.stock= (SELECT SUM(ps.reel) FROM llx_product_stock ps WHERE ps.fk_product = p.rowid);'
273  );
274 
275  }
276 }
Class DolibarrModules.
</td >< tdclass="liste_titre"align="right"></td ></tr >< trclass="liste_titre">< inputtype="checkbox"onClick="toggle(this)"/> Ref p ref Label p label Duration p duration warehouseinternal SELECT description FROM product_lang WHERE qty< br > qty qty qty StockTooLow img yes disabled img no img no< trclass="oddeven">< td >< inputtype="checkbox"class="check"name="'.$i.'"'.$disabled.'></td >< td >< inputtype="checkbox"class="check"name="choose'.$i.'"></td >< tdclass="nowrap"></td >< td >< inputtype="hidden"name="desc'.$i.'"value="'.dol_escape_htmltag($objp-> description
Only used if Module[ID]Desc translation string is not found.
Definition: replenish.php:554
__construct($db)
Constructor.
Class to describe and enable module Stock.
$conf db name
Only used if Module[ID]Name translation string is not found.
Definition: repair.php:104