dolibarr 21.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@inodbox.com>
5 * Copyright (C) 2012 Juanjo Menent <jmenent@2byte.es>
6 * Copyright (C) 2021 Ferran Marcet <fmarcet@2byte.es>
7 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
8 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 */
23
32include_once DOL_DOCUMENT_ROOT.'/core/modules/DolibarrModules.class.php';
33
34
39{
45 public function __construct($db)
46 {
47 global $conf, $langs;
48
49 $this->db = $db;
50 $this->numero = 52;
51
52 $this->family = "products";
53 $this->module_position = '39';
54 // Module label (no space allowed), used if translation string 'ModuleXXXName' not found (where XXX is value of numeric property 'numero' of module)
55 $this->name = preg_replace('/^mod/i', '', get_class($this));
56 $this->description = "Gestion des stocks";
57
58 // Possible values for version are: 'development', 'experimental', 'dolibarr' or version
59 $this->version = 'dolibarr';
60
61 $this->const_name = 'MAIN_MODULE_'.strtoupper($this->name);
62 $this->picto = 'stock';
63
64 // Data directories to create when module is enabled
65 $this->dirs = array("/stock/temp");
66
67 $this->config_page_url = array("stock.php");
68
69 // Dependencies
70 $this->hidden = false; // A condition to hide module
71 $this->depends = array("modProduct"); // List of module class names as string that must be enabled if this module is enabled
72 $this->requiredby = array("modProductBatch"); // List of module ids to disable if this one is disabled
73 $this->conflictwith = array(); // List of module class names as string this module is in conflict with
74 $this->phpmin = array(7, 0); // Minimum version of PHP required by module
75 $this->langfiles = array("stocks");
76
77 // Constants
78 $this->const = array();
79 $r = 0;
80
81 $this->const[$r] = array('STOCK_ALLOW_NEGATIVE_TRANSFER', 'chaine', '1', '', 1);
82
83 $r++;
84 $this->const[$r][0] = "STOCK_ADDON_PDF";
85 $this->const[$r][1] = "chaine";
86 $this->const[$r][2] = "standard_stock";
87 $this->const[$r][3] = 'Name of PDF model of stock';
88 $this->const[$r][4] = 0;
89
90 $r++;
91 $this->const[$r][0] = "MOUVEMENT_ADDON_PDF";
92 $this->const[$r][1] = "chaine";
93 $this->const[$r][2] = "standard_movement_stock";
94 $this->const[$r][3] = 'Name of PDF model of stock movement';
95 $this->const[$r][4] = 0;
96
97 $r++;
98 $this->const[$r][0] = "STOCK_ADDON_PDF_ODT_PATH";
99 $this->const[$r][1] = "chaine";
100 $this->const[$r][2] = "DOL_DATA_ROOT/doctemplates/stocks";
101 $this->const[$r][3] = "";
102 $this->const[$r][4] = 0;
103
104 $r++;
105 $this->const[$r][0] = "MOUVEMENT_ADDON_PDF_ODT_PATH";
106 $this->const[$r][1] = "chaine";
107 $this->const[$r][2] = "DOL_DATA_ROOT/doctemplates/stocks/movements";
108 $this->const[$r][3] = "";
109 $this->const[$r][4] = 0;
110
111 // Boxes
112 $this->boxes = array();
113
114 // Permissions
115 $this->rights = array();
116 $this->rights_class = 'stock';
117
118 $r = 0;
119
120 $this->rights[$r][0] = 1001;
121 $this->rights[$r][1] = 'Read stocks';
122 $this->rights[$r][2] = 'r';
123 $this->rights[$r][3] = 0;
124 $this->rights[$r][4] = 'lire';
125 $this->rights[$r][5] = '';
126
127 $r++;
128 $this->rights[$r][0] = 1002;
129 $this->rights[$r][1] = 'Create/Modify stocks';
130 $this->rights[$r][2] = 'w';
131 $this->rights[$r][3] = 0;
132 $this->rights[$r][4] = 'creer';
133 $this->rights[$r][5] = '';
134
135 $r++;
136 $this->rights[$r][0] = 1003;
137 $this->rights[$r][1] = 'Delete stock';
138 $this->rights[$r][2] = 'd';
139 $this->rights[$r][3] = 0;
140 $this->rights[$r][4] = 'supprimer';
141 $this->rights[$r][5] = '';
142
143 $r++;
144 $this->rights[$r][0] = 1004;
145 $this->rights[$r][1] = 'Read stock movements';
146 $this->rights[$r][2] = 'r';
147 $this->rights[$r][3] = 0;
148 $this->rights[$r][4] = 'mouvement';
149 $this->rights[$r][5] = 'lire';
150
151 $r++;
152 $this->rights[$r][0] = 1005;
153 $this->rights[$r][1] = 'Create/modify stock movements';
154 $this->rights[$r][2] = 'w';
155 $this->rights[$r][3] = 0;
156 $this->rights[$r][4] = 'mouvement';
157 $this->rights[$r][5] = 'creer';
158
159 $r++;
160 $this->rights[$r][0] = 1011;
161 $this->rights[$r][1] = 'inventoryReadPermission'; // Permission label
162 $this->rights[$r][3] = 0; // Permission by default for new user (0/1)
163 $this->rights[$r][4] = 'inventory_advance'; // In php code, permission will be checked by test if ($user->rights->permkey->level1->level2)
164 $this->rights[$r][5] = 'read'; // In php code, permission will be checked by test if ($user->rights->permkey->level1->level2)
165
166 $r++;
167 $this->rights[$r][0] = 1012;
168 $this->rights[$r][1] = 'inventoryCreatePermission'; // Permission label
169 $this->rights[$r][3] = 0; // Permission by default for new user (0/1)
170 $this->rights[$r][4] = 'inventory_advance'; // In php code, permission will be checked by test if ($user->rights->permkey->level1->level2)
171 $this->rights[$r][5] = 'write'; // In php code, permission will be checked by test if ($user->rights->permkey->level1->level2)
172
173 $r++;
174 $this->rights[$r][0] = 1013;
175 $this->rights[$r][1] = 'inventoryDeletePermission'; // Permission label
176 $this->rights[$r][3] = 0; // Permission by default for new user (0/1)
177 $this->rights[$r][4] = 'inventory_advance'; // In php code, permission will be checked by test if ($user->rights->permkey->level1->level2)
178 $this->rights[$r][5] = 'delete'; // In php code, permission will be checked by test if ($user->rights->permkey->level1->level2)
179
180 if (getDolGlobalInt('MAIN_FEATURES_LEVEL') >= 2) {
181 $r++;
182 $this->rights[$r][0] = 1014;
183 $this->rights[$r][1] = 'inventoryValidatePermission'; // Permission label
184 $this->rights[$r][3] = 0; // Permission by default for new user (0/1)
185 $this->rights[$r][4] = 'inventory_advance'; // In php code, permission will be checked by test if ($user->rights->permkey->level1->level2)
186 $this->rights[$r][5] = 'validate'; // In php code, permission will be checked by test if ($user->rights->permkey->level1->level2)
187
188 $r++;
189 $this->rights[$r][0] = 1015;
190 $this->rights[$r][1] = 'inventoryChangePMPPermission'; // Permission label
191 $this->rights[$r][3] = 0; // Permission by default for new user (0/1)
192 $this->rights[$r][4] = 'inventory_advance'; // In php code, permission will be checked by test if ($user->rights->permkey->level1->level2)
193 $this->rights[$r][5] = 'changePMP'; // In php code, permission will be checked by test if ($user->rights->permkey->level1->level2)
194 }
195
196 // Main menu entries
197 $this->menu = array(); // List of menus to add
198 $r = 0;
199
200 // Menus
201 //-------
202 $this->menu = 1; // This module add menu entries. They are coded into menu manager.
203
204
205 // Exports
206 //--------
207 $r = 0;
208
209 // Export warehouses
210 $r++;
211 $this->export_code[$r] = $this->rights_class.'_emplacement';
212 $this->export_label[$r] = "Warehouses"; // Translation key (used only if key ExportDataset_xxx_z not found)
213 $this->export_icon[$r] = "warehouse";
214 $this->export_permission[$r] = array(array("stock", "lire"));
215 $this->export_fields_array[$r] = array(
216 'e.rowid' => 'IdWarehouse', 'e.ref' => 'LocationSummary', 'e.description' => 'DescWareHouse', 'e.lieu' => 'LieuWareHouse', 'e.address' => 'Address', 'e.zip' => 'Zip', 'e.town' => 'Town',
217 'd.code_departement' => 'Departement', 'c.code' => 'CountryCode',
218 'e.phone' => 'Phone', 'e.fax' => 'Fax', 'e.statut' => 'Status', 'pe.rowid' => 'ParentWarehouse', 'pe.ref' => 'LocationSummary'
219 );
220 $this->export_TypeFields_array[$r] = array(
221 'e.ref' => 'Text', 'e.description' => 'Text', 'e.lieu' => 'Text', 'e.address' => 'Text', 'e.zip' => 'Text', 'e.town' => 'Text',
222 'd.code_departement' => 'List:c_departements:code_departement:code_departement:', 'c.code' => 'List:c_country:code:code:',
223 'e.phone' => 'Text', 'e.fax' => 'Text', 'e.statut' => 'Text', 'pe.rowid' => 'List:entrepot:ref:rowid:stock', 'pe.ref' => 'Text'
224 );
225 $this->export_entities_array[$r] = array(); // We define here only fields that use another icon that the one defined into export_icon
226 $this->export_aggregate_array[$r] = array(); // TODO Not used yet
227 $keyforselect = 'warehouse';
228 $keyforelement = 'warehouse';
229 $keyforaliasextra = 'extra';
230 include DOL_DOCUMENT_ROOT.'/core/extrafieldsinexport.inc.php';
231
232 $this->export_sql_start[$r] = 'SELECT DISTINCT ';
233 $this->export_sql_end[$r] = ' FROM '.MAIN_DB_PREFIX.'entrepot as e';
234 $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_departements as d ON d.rowid = e.fk_departement';
235 $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_country as c ON c.rowid = e.fk_pays';
236 $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'entrepot as pe ON pe.rowid = e.fk_parent';
237 $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'entrepot_extrafields as extra ON extra.fk_object = e.rowid';
238 $this->export_sql_end[$r] .= ' WHERE e.entity IN ('.getEntity('stock').')';
239
240 // Export stock (without batch number)
241 $r++;
242 $this->export_code[$r] = $this->rights_class.'_emplacement_product';
243 $this->export_label[$r] = "Stocks"; // Translation key (used only if key ExportDataset_xxx_z not found)
244 $this->export_icon[$r] = "warehouse";
245 $this->export_permission[$r] = array(array("stock", "lire"));
246 $this->export_fields_array[$r] = array(
247 'e.rowid' => 'IdWarehouse', 'e.ref' => 'LocationSummary', 'e.description' => 'DescWareHouse', 'e.lieu' => 'LieuWareHouse', 'e.address' => 'Address', 'e.zip' => 'Zip', 'e.town' => 'Town',
248 'p.rowid' => "ProductId", 'p.ref' => "Ref", 'p.fk_product_type' => "Type", 'p.label' => "Label", 'p.description' => "Description", 'p.note' => "Note",
249 'p.price' => "Price", 'p.tva_tx' => 'VAT', 'p.tosell' => "OnSell", 'p.tobuy' => 'OnBuy', 'p.duration' => "Duration",
250 'p.datec' => 'DateCreation', 'p.tms' => 'DateModification', 'p.pmp' => 'PMPValue', 'p.cost_price' => 'CostPrice',
251 'p.seuil_stock_alerte' => 'StockLimit', 'p.barcode' => 'BarCode', 'bt.libelle' => 'BarcodeType',
252 );
253 if (isModEnabled('barcode')) {
254 $this->export_fields_array[$r] = array_merge($this->export_fields_array[$r], array('p.barcode' => 'BarCode'));
255 }
256 $this->export_TypeFields_array[$r] = array(
257 'e.rowid' => 'List:entrepot:ref::stock', 'e.ref' => 'Text', 'e.lieu' => 'Text', 'e.address' => 'Text', 'e.zip' => 'Text', 'e.town' => 'Text',
258 'p.rowid' => "Numeric", 'p.ref' => "Text", 'p.fk_product_type' => "Text", 'p.label' => "Text", 'p.description' => "Text", 'p.note' => "Text",
259 'p.price' => "Numeric", 'p.tva_tx' => 'Numeric', 'p.tosell' => "Boolean", 'p.tobuy' => "Boolean", 'p.duration' => "Duree",
260 'p.datec' => 'Date', 'p.tms' => 'Date', 'p.pmp' => 'Numeric', 'p.cost_price' => 'Numeric',
261 'ps.reel' => 'Numeric',
262 'p.seuil_stock_alerte' => 'Numeric', 'p.barcode' => 'Text', 'bt.libelle' => 'List:c_barcode_type:libelle',
263 );
264 if (isModEnabled('barcode')) {
265 $this->export_TypeFields_array[$r] = array_merge($this->export_TypeFields_array[$r], array('p.barcode' => 'Text'));
266 }
267 $this->export_entities_array[$r] = array(
268 'p.rowid' => "product", 'p.ref' => "product", 'p.fk_product_type' => "product", 'p.label' => "product", 'p.description' => "product", 'p.note' => "product",
269 'p.price' => "product", 'p.tva_tx' => 'product', 'p.tosell' => "product", 'p.tobuy' => "product", 'p.duration' => "product",
270 'p.datec' => 'product', 'p.tms' => 'product', 'p.pmp' => 'product', 'p.cost_price' => 'product',
271 'ps.reel' => 'stock',
272 'p.seuil_stock_alerte' => 'product', 'p.barcode' => 'product', 'bt.libelle' => 'product',
273 ); // We define here only fields that use another icon that the one defined into export_icon
274 if (isModEnabled('barcode')) {
275 $this->export_entities_array[$r] = array_merge($this->export_entities_array[$r], array('p.barcode' => 'product'));
276 }
277 $this->export_aggregate_array[$r] = array('ps.reel' => 'SUM'); // TODO Not used yet
278 $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.
279 $keyforselect = 'product';
280 $keyforelement = 'product';
281 $keyforaliasextra = 'extra';
282 include DOL_DOCUMENT_ROOT.'/core/extrafieldsinexport.inc.php';
283 $this->export_fields_array[$r] = array_merge($this->export_fields_array[$r], array('ps.reel' => 'Stock'));
284
285 $this->export_sql_start[$r] = 'SELECT DISTINCT ';
286 $this->export_sql_end[$r] = ' FROM '.MAIN_DB_PREFIX.'product as p';
287 $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product_extrafields as extra ON extra.fk_object = p.rowid';
288 $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_barcode_type as bt ON bt.rowid = p.fk_barcode_type';
289 $this->export_sql_end[$r] .= ', '.MAIN_DB_PREFIX.'product_stock as ps, '.MAIN_DB_PREFIX.'entrepot as e';
290 $this->export_sql_end[$r] .= ' WHERE p.rowid = ps.fk_product AND ps.fk_entrepot = e.rowid';
291 $this->export_sql_end[$r] .= ' AND e.entity IN ('.getEntity('stock').')';
292
293 // Export stock including batch number
294 if (isModEnabled('productbatch')) {
295 $langs->load("productbatch");
296
297 // This request is same than previous but without field ps.stock (real stock in warehouse) and with link to subtable productbatch
298 $r++;
299
300 $this->export_code[$r] = $this->rights_class.'_emplacement_product_lot';
301 $this->export_label[$r] = "StocksWithBatch"; // Translation key (used only if key ExportDataset_xxx_z not found)
302 $this->export_icon[$r] = "warehouse";
303 $this->export_permission[$r] = array(array("stock", "lire"));
304 $this->export_fields_array[$r] = array(
305 'e.rowid' => 'IdWarehouse', 'e.ref' => 'LocationSummary', 'e.description' => 'DescWareHouse', 'e.lieu' => 'LieuWareHouse', 'e.address' => 'Address', 'e.zip' => 'Zip', 'e.town' => 'Town',
306 'p.rowid' => "ProductId", 'p.ref' => "Ref", 'p.fk_product_type' => "Type", 'p.label' => "Label", 'p.description' => "Description", 'p.note' => "Note",
307 'p.price' => "Price", 'p.tva_tx' => 'VAT', 'p.tosell' => "OnSell", 'p.tobuy' => 'OnBuy', 'p.duration' => "Duration",
308 'p.datec' => 'DateCreation', 'p.tms' => 'DateModification', 'p.pmp' => 'PMPValue', 'p.cost_price' => 'CostPrice', 'lcpn.label'=>'Nature',
309 'pb.rowid' => 'Id', 'pb.batch' => 'Batch', 'pb.qty' => 'Qty',
310 'pl.eatby' => 'EatByDate', 'pl.sellby' => 'SellByDate', 'none.dateLastMovement' => 'LastMovement'
311 );
312 if (isModEnabled('barcode')) {
313 $this->export_fields_array[$r] = array_merge($this->export_fields_array[$r], array('p.barcode' => 'BarCode'));
314 }
315 $this->export_TypeFields_array[$r] = array(
316 'e.rowid' => 'List:entrepot:ref::stock', 'e.ref' => 'Text', 'e.lieu' => 'Text', 'e.description' => 'Text', 'e.address' => 'Text', 'e.zip' => 'Text', 'e.town' => 'Text',
317 'p.rowid' => "Numeric", 'p.ref' => "Text", 'p.fk_product_type' => "Text", 'p.label' => "Text", 'p.description' => "Text", 'p.note' => "Text",
318 'p.price' => "Numeric", 'p.tva_tx' => 'Numeric', 'p.tosell' => "Boolean", 'p.tobuy' => "Boolean", 'p.duration' => "Duree",
319 'p.datec' => 'Date', 'p.tms' => 'Date', 'p.pmp' => 'PMPValue', 'p.cost_price' => 'CostPrice', 'lcpn.label'=>'Text',
320 'pb.batch' => 'Text', 'pb.qty' => 'Numeric',
321 'pl.eatby' => 'Date', 'pl.sellby' => 'Date', 'none.dateLastMovement' => 'Date'
322 );
323 if (isModEnabled('barcode')) {
324 $this->export_TypeFields_array[$r] = array_merge($this->export_TypeFields_array[$r], array('p.barcode' => 'Text'));
325 }
326 $this->export_entities_array[$r] = array(
327 'p.rowid' => "product", 'p.ref' => "product", 'p.fk_product_type' => "product", 'p.label' => "product", 'p.description' => "product", 'p.note' => "product",
328 'p.price' => "product", 'p.tva_tx' => 'product', 'p.tosell' => "product", 'p.tobuy' => "product", 'p.duration' => "product",
329 'p.datec' => 'product', 'p.tms' => 'product', 'p.pmp' => 'product', 'p.cost_price' => 'product', 'lcpn.label'=>'product',
330 'pb.rowid' => 'batch', 'pb.batch' => 'batch', 'pb.qty' => 'batch', 'none.dateLastMovement' => 'movement',
331 'pl.eatby' => 'batch', 'pl.sellby' => 'batch'
332 ); // We define here only fields that use another icon that the one defined into export_icon
333 $this->export_special_array[$r] = array(
334 'none.dateLastMovement'=>array('rule'=>'compute', 'classfile'=>'/product/stock/class/mouvementstock.class.php', 'class'=>'MouvementStock', 'method'=>'getDateLastMovementProductBatch', 'method_params'=>['e_rowid', 'p_rowid', 'pb_batch']),
335 );
336 if (isModEnabled('barcode')) {
337 $this->export_entities_array[$r] = array_merge($this->export_entities_array[$r], array('p.barcode' => 'product'));
338 }
339 $this->export_aggregate_array[$r] = array('ps.reel' => 'SUM'); // TODO Not used yet
340 $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.
341 $keyforselect = 'product_lot';
342 $keyforelement = 'batch';
343 $keyforaliasextra = 'extra';
344 include DOL_DOCUMENT_ROOT.'/core/extrafieldsinexport.inc.php';
345
346 $this->export_sql_start[$r] = 'SELECT DISTINCT ';
347 $this->export_sql_end[$r] = ' FROM '.MAIN_DB_PREFIX.'product_batch as pb';
348 $this->export_sql_end[$r] .= ' INNER JOIN '.MAIN_DB_PREFIX.'product_stock as ps ON ps.rowid = pb.fk_product_stock';
349 $this->export_sql_end[$r] .= ' INNER JOIN '.MAIN_DB_PREFIX.'product as p ON p.rowid = ps.fk_product';
350 $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';
351 $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product_lot_extrafields as extra ON extra.fk_object = pl.rowid';
352 $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_product_nature as lcpn on lcpn.code = p.finished,';
353 $this->export_sql_end[$r] .= ' '.MAIN_DB_PREFIX.'entrepot as e';
354 $this->export_sql_end[$r] .= ' WHERE ps.fk_entrepot = e.rowid';
355 $this->export_sql_end[$r] .= ' AND e.entity IN ('.getEntity('stock').')';
356 }
357
358 // Export of stock movements
359 $r++;
360 $this->export_code[$r] = $this->rights_class.'_movement';
361 $this->export_label[$r] = "StockMovements"; // Translation key (used only if key ExportDataset_xxx_z not found)
362 $this->export_icon[$r] = "movement";
363 $this->export_permission[$r] = array(array("stock", "lire"));
364 $this->export_fields_array[$r] = array(
365 'sm.rowid' => 'MovementId', 'sm.value' => 'Qty', 'sm.datem' => 'DateMovement', 'sm.label' => 'MovementLabel', 'sm.inventorycode' => 'InventoryCode',
366 'e.rowid' => 'IdWarehouse', 'e.ref' => 'LocationSummary', 'e.description' => 'DescWareHouse', 'e.lieu' => 'LieuWareHouse', 'e.address' => 'Address', 'e.zip' => 'Zip', 'e.town' => 'Town',
367 'p.rowid' => "ProductId", 'p.ref' => "Ref", 'p.fk_product_type' => "Type", 'p.label' => "Label", 'p.description' => "Description", 'p.note' => "Note",
368 'p.price' => "Price", 'p.tva_tx' => 'VAT', 'p.tosell' => "OnSell", 'p.tobuy' => 'OnBuy', 'p.duration' => "Duration", 'p.datec' => 'DateCreation', 'p.tms' => 'DateModification'
369 );
370 if (isModEnabled('barcode')) {
371 $this->export_fields_array[$r] = array_merge($this->export_fields_array[$r], array('p.barcode' => 'BarCode'));
372 }
373 $this->export_TypeFields_array[$r] = array(
374 'sm.rowid' => 'Numeric', 'sm.value' => 'Numeric', 'sm.datem' => 'Date', 'sm.batch' => 'Text', 'sm.label' => 'Text', 'sm.inventorycode' => 'Text',
375 'e.rowid' => 'List:entrepot:ref::stock', 'e.ref' => 'Text', 'e.description' => 'Text', 'e.lieu' => 'Text', 'e.address' => 'Text', 'e.zip' => 'Text', 'e.town' => 'Text',
376 'p.rowid' => "Numeric", 'p.ref' => "Text", 'p.fk_product_type' => "Text", 'p.label' => "Text", 'p.description' => "Text", 'p.note' => "Text",
377 'p.price' => "Numeric", 'p.tva_tx' => 'Numeric', 'p.tosell' => "Boolean", 'p.tobuy' => "Boolean", 'p.duration' => "Duree", 'p.datec' => 'Date', 'p.tms' => 'Date'
378 );
379 if (isModEnabled('barcode')) {
380 $this->export_TypeFields_array[$r] = array_merge($this->export_TypeFields_array[$r], array('p.barcode' => 'Text'));
381 }
382 $this->export_entities_array[$r] = array(
383 'e.rowid' => 'warehouse', 'e.ref' => 'warehouse', 'e.description' => 'warehouse', 'e.lieu' => 'warehouse', 'e.address' => 'warehouse', 'e.zip' => 'warehouse', 'e.town' => 'warehouse',
384 'p.rowid' => "product", 'p.ref' => "product", 'p.fk_product_type' => "product", 'p.label' => "product", 'p.description' => "product", 'p.note' => "product",
385 'p.price' => "product", 'p.tva_tx' => 'product', 'p.tosell' => "product", 'p.tobuy' => "product", 'p.duration' => "product", 'p.datec' => 'product', 'p.tms' => 'product'
386 ); // We define here only fields that use another icon that the one defined into export_icon
387 if (isModEnabled('productbatch')) {
388 $this->export_fields_array[$r]['sm.batch'] = 'Batch';
389 $this->export_TypeFields_array[$r]['sm.batch'] = 'Text';
390 $this->export_entities_array[$r]['sm.batch'] = 'movement';
391 }
392 if (isModEnabled('barcode')) {
393 $this->export_entities_array[$r] = array_merge($this->export_entities_array[$r], array('p.barcode' => 'product'));
394 }
395 $this->export_aggregate_array[$r] = array('sm.value' => 'SUM'); // TODO Not used yet
396 $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.
397
398 $this->export_sql_start[$r] = 'SELECT DISTINCT ';
399 $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';
400 $this->export_sql_end[$r] .= ' WHERE p.rowid = sm.fk_product AND sm.fk_entrepot = e.rowid';
401 $this->export_sql_end[$r] .= ' AND e.entity IN ('.getEntity('stock').')';
402
403
404 // Export inventories
405 $r++;
406 $this->export_code[$r] = $this->rights_class.'_inventory';
407 $this->export_label[$r] = "Inventories"; // Translation key (used only if key ExportDataset_xxx_z not found)
408 $this->export_icon[$r] = "inventory";
409 $this->export_permission[$r] = array(array("stock", "lire"));
410 $this->export_fields_array[$r] = array(
411 'i.rowid' => 'InventoryId', 'i.ref' => 'InventoryRef', 'i.date_inventory' => 'DateInventory', 'i.status' => 'InventoryStatus', 'i.title' => 'InventoryTitle',
412 'id.rowid' => 'InventoryLineId', 'id.qty_view' => 'QtyViewed', 'id.qty_stock' => 'QtyStock', 'id.qty_regulated' => 'QtyRegulated',
413 'id.batch' => 'Lotserial',
414 'e.rowid' => 'IdWarehouse', 'e.ref' => 'LocationSummary', 'e.description' => 'DescWareHouse', 'e.lieu' => 'LieuWareHouse', 'e.address' => 'Address', 'e.zip' => 'Zip', 'e.town' => 'Town',
415 'p.rowid' => "ProductId", 'p.ref' => "Ref", 'p.fk_product_type' => "Type", 'p.label' => "Label", 'p.description' => "Description", 'p.note' => "Note",
416 'p.barcode' => "Barcode", 'p.price' => "Price", 'p.tva_tx' => 'VAT', 'p.tosell' => "OnSell", 'p.tobuy' => 'OnBuy', 'p.duration' => "Duration", 'p.datec' => 'DateCreation', 'p.tms' => 'DateModification'
417 );
418 if (isModEnabled('barcode')) {
419 $this->export_fields_array[$r] = array_merge($this->export_fields_array[$r], array('p.barcode' => 'BarCode'));
420 }
421 $this->export_TypeFields_array[$r] = array(
422 'id.rowid' => 'Numeric',
423 'e.rowid' => 'List:entrepot:ref::stock', 'e.ref' => 'Text', 'e.description' => 'Text', 'e.lieu' => 'Text', 'e.address' => 'Text', 'e.zip' => 'Text', 'e.town' => 'Text',
424 'p.rowid' => "Numeric", 'p.ref' => "Text", 'p.fk_product_type' => "Text", 'p.label' => "Text", 'p.description' => "Text", 'p.note' => "Text",
425 'p.barcode' => "Text", 'p.price' => "Numeric", 'p.tva_tx' => 'Numeric', 'p.tosell' => "Boolean", 'p.tobuy' => "Boolean", 'p.duration' => "Duree", 'p.datec' => 'Date', 'p.tms' => 'Date',
426 'i.rowid' => 'Numeric', 'i.ref' => 'Text', 'i.date_inventory' => 'Date', 'i.status' => 'Numeric', 'i.title' => 'Text',
427 'id.qty_view' => 'Numeric', 'id.qty_stock' => 'Numeric', 'id.batch' => 'Text',
428 'id.qty_regulated' => 'Numeric', 'id.fk_warehouse' => 'Numeric',
429 );
430 if (isModEnabled('barcode')) {
431 $this->export_TypeFields_array[$r] = array_merge($this->export_TypeFields_array[$r], array('p.barcode' => 'Text'));
432 }
433 $this->export_entities_array[$r] = array(
434 'id.qty_view' => 'inventory_line', 'id.qty_stock' => 'inventory_line', 'id.batch' => 'inventory_line', 'id.qty_regulated' => 'inventory_line', 'id.fk_warehouse' => 'inventory_line', 'id.rowid' => 'inventory_line', 'e.rowid' => 'warehouse', 'e.ref' => 'warehouse', 'e.description' => 'warehouse', 'e.lieu' => 'warehouse', 'e.address' => 'warehouse', 'e.zip' => 'warehouse', 'e.town' => 'warehouse',
435 'p.rowid' => "product", 'p.ref' => "product", 'p.fk_product_type' => "product", 'p.label' => "product", 'p.description' => "product", 'p.note' => "product",
436 'p.barcode' => "product", 'p.price' => "product", 'p.tva_tx' => 'product', 'p.tosell' => "product", 'p.tobuy' => "product", 'p.duration' => "product", 'p.datec' => 'product', 'p.tms' => 'product'
437 ); // We define here only fields that use another icon that the one defined into export_icon
438 if (isModEnabled('productbatch')) {
439 $this->export_fields_array[$r]['id.batch'] = 'Batch';
440 $this->export_TypeFields_array[$r]['id.batch'] = 'Text';
441 $this->export_entities_array[$r]['id.batch'] = 'inventory_line';
442 }
443 if (isModEnabled('barcode')) {
444 $this->export_entities_array[$r] = array_merge($this->export_entities_array[$r], array('p.barcode' => 'product'));
445 }
446 $this->export_aggregate_array[$r] = array('sm.value' => 'SUM'); // TODO Not used yet
447 $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.
448
449 $this->export_sql_start[$r] = 'SELECT DISTINCT ';
450 $this->export_sql_end[$r] = ' FROM '.MAIN_DB_PREFIX.'product as p, '.MAIN_DB_PREFIX.'inventory as i, '.MAIN_DB_PREFIX.'inventorydet as id, '.MAIN_DB_PREFIX.'entrepot as e';
451 $this->export_sql_end[$r] .= ' WHERE p.rowid = id.fk_product AND id.fk_inventory = i.rowid AND id.fk_warehouse = e.rowid';
452 $this->export_sql_end[$r] .= ' AND e.entity IN ('.getEntity('stock').')';
453
454
455 // Imports
456 //--------
457
458 $r = 0;
459
460 // Import warehouses
461 $r++;
462 $this->import_code[$r] = $this->rights_class.'_'.$r;
463 $this->import_label[$r] = "Warehouses"; // Translation key
464 $this->import_icon[$r] = "warehouse";
465 $this->import_entities_array[$r] = array(); // We define here only fields that use another icon that the one defined into import_icon
466 $this->import_tables_array[$r] = array('e' => MAIN_DB_PREFIX.'entrepot');
467 $this->import_tables_creator_array[$r] = array('e' => 'fk_user_author');
468 $this->import_fields_array[$r] = array('e.ref' => "LocationSummary*",
469 'e.description' => "DescWareHouse",
470 'e.lieu' => "LieuWareHouse",
471 'e.address' => "Address",
472 'e.zip' => 'Zip',
473 'e.fk_departement' => 'StateCode',
474 'e.fk_pays' => 'CountryCode',
475 'e.phone' => 'Phone',
476 'e.fax' => 'Fax',
477 'e.statut' => 'Status',
478 'e.fk_parent' => 'ParentWarehouse'
479 );
480
481 $this->import_convertvalue_array[$r] = array(
482 'e.fk_departement' => array('rule' => 'fetchidfromcodeid', 'classfile' => '/core/class/cstate.class.php', 'class' => 'Cstate', 'method' => 'fetch', 'dict' => 'DictionaryStateCode'),
483 'e.fk_pays' => array('rule' => 'fetchidfromcodeid', 'classfile' => '/core/class/ccountry.class.php', 'class' => 'Ccountry', 'method' => 'fetch', 'dict' => 'DictionaryCountry'),
484 'e.fk_parent' => array('rule' => 'fetchidfromref', 'classfile' => '/product/stock/class/entrepot.class.php', 'class' => 'Entrepot', 'method' => 'fetch', 'element' => 'ref')
485 );
486 $this->import_regex_array[$r] = array('e.statut' => '^[0|1]');
487 $this->import_examplevalues_array[$r] = array('e.ref' => "ALM001",
488 'e.description' => "Central Warehouse",
489 'e.lieu' => "Central",
490 'e.address' => "Route 66",
491 'e.zip' => '28080',
492 'e.fk_departement' => 'matches field "code_departement" in table "'.MAIN_DB_PREFIX.'c_departements"',
493 'e.fk_pays' => 'US/FR/DE etc. matches field "code" in table "'.MAIN_DB_PREFIX.'c_country"',
494 'e.phone' => '(+33)(0)123456789',
495 'e.fax' => '(+33)(0)123456790',
496 'e.statut' => '1',
497 'e.fk_parent' => 'id or ref of warehouse'
498 );
499 $this->import_updatekeys_array[$r] = array('p.ref' => 'Ref');
500
501 // Import stocks
502 $r++;
503 $this->import_code[$r] = $this->rights_class.'_'.$r;
504 $this->import_label[$r] = "Stocks"; // Translation key
505 $this->import_icon[$r] = "stock";
506 $this->import_entities_array[$r] = array(); // We define here only fields that use another icon that the one defined into import_icon
507 $this->import_tables_array[$r] = array('ps' => MAIN_DB_PREFIX.'product_stock');
508 $this->import_fields_array[$r] = array('ps.fk_product' => "Product*", 'ps.fk_entrepot' => "Warehouse*", 'ps.reel' => "Stock*");
509
510 $this->import_convertvalue_array[$r] = array(
511 'ps.fk_product' => array('rule' => 'fetchidfromref', 'classfile' => '/product/class/product.class.php', 'class' => 'Product', 'method' => 'fetch', 'element' => 'product'),
512 'ps.fk_entrepot' => array('rule' => 'fetchidfromref', 'classfile' => '/product/stock/class/entrepot.class.php', 'class' => 'Entrepot', 'method' => 'fetch', 'element' => 'ref')
513 );
514 $this->import_examplevalues_array[$r] = array(
515 'ps.fk_product' => "id or ref of product", 'ps.fk_entrepot' => "id or ref of warehouse", 'ps.reel' => "10"
516 );
517 $this->import_updatekeys_array[$r] = array('ps.fk_product' => 'Product', 'ps.fk_entrepot' => "Warehouse");
518 $this->import_run_sql_after_array[$r] = array( // Because we may change data that are denormalized, we must update dernormalized data after.
519 '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);'
520 );
521 }
522
523
532 public function init($options = '')
533 {
534 global $conf, $langs;
535
536 $result = $this->_load_tables('/install/mysql/', 'stock');
537 if ($result < 0) {
538 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')
539 }
540
541 // Permissions
542 $this->remove($options);
543
544 //ODT template
545 $src = DOL_DOCUMENT_ROOT.'/install/doctemplates/stocks/template_warehouse.odt';
546 $dirodt = DOL_DATA_ROOT.'/doctemplates/stocks';
547 $dest = $dirodt.'/template_warehouse.odt';
548
549 if (file_exists($src) && !file_exists($dest)) {
550 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
551 dol_mkdir($dirodt);
552 $result = dol_copy($src, $dest, '0', 0);
553 if ($result < 0) {
554 $langs->load("errors");
555 $this->error = $langs->trans('ErrorFailToCopyFile', $src, $dest);
556 return 0;
557 }
558 }
559
560 $sql = array();
561
562 $sql = array(
563 "DELETE FROM ".MAIN_DB_PREFIX."document_model WHERE nom = '".$this->db->escape($this->const[1][2])."' AND type = 'stock' AND entity = ".((int) $conf->entity),
564 "INSERT INTO ".MAIN_DB_PREFIX."document_model (nom, type, entity) VALUES('".$this->db->escape($this->const[1][2])."','stock',".((int) $conf->entity).")",
565 "DELETE FROM ".MAIN_DB_PREFIX."document_model WHERE nom = '".$this->db->escape($this->const[2][2])."' AND type = 'mouvement' AND entity = ".((int) $conf->entity),
566 "INSERT INTO ".MAIN_DB_PREFIX."document_model (nom, type, entity) VALUES('".$this->db->escape($this->const[2][2])."','mouvement',".((int) $conf->entity).")",
567 );
568
569 return $this->_init($sql, $options);
570 }
571}
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.
print $script_file $mode $langs defaultlang(is_numeric($duration_value) ? " delay=". $duration_value :"").(is_numeric($duration_value2) ? " after 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.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
dol_mkdir($dir, $dataroot='', $newmask='')
Creation of a directory (this can create recursive subdir)
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79
$conf db name
Only used if Module[ID]Name translation string is not found.
Definition repair.php:152