dolibarr 22.0.5
modProduct.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3 * Copyright (C) 2004-2012 Laurent Destailleur <eldy@users.sourceforge.net>
4 * Copyright (C) 2004 Sebastien Di Cintio <sdicintio@ressource-toi.org>
5 * Copyright (C) 2004 Benoit Mortier <benoit.mortier@opensides.be>
6 * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@inodbox.com>
7 * Copyright (C) 2012-2013 Juanjo Menent <jmenent@2byte.es>
8 * Copyright (C) 2014 Christophe Battarel <contact@altairis.fr>
9 * Copyright (C) 2014 Cedric Gross <c.gross@kreiz-it.fr>
10 * Copyright (C) 2020-2021 Alexandre Spangaro <aspangaro@open-dsi.fr>
11 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
12 * Copyright (C) 2025 Frédéric France <frederic.france@free.fr>
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 3 of the License, or
17 * (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program. If not, see <https://www.gnu.org/licenses/>.
26 */
27
35include_once DOL_DOCUMENT_ROOT.'/core/modules/DolibarrModules.class.php';
36
37
42{
48 public function __construct($db)
49 {
50 global $conf, $mysoc;
51
52 $this->db = $db;
53 $this->numero = 50;
54
55 $this->family = "products";
56 $this->module_position = '26';
57 // Module label (no space allowed), used if translation string 'ModuleXXXName' not found (where XXX is value of numeric property 'numero' of module)
58 $this->name = preg_replace('/^mod/i', '', get_class($this));
59 $this->description = "Product management";
60
61 // Possible values for version are: 'development', 'experimental', 'dolibarr' or version
62 $this->version = 'dolibarr';
63
64 $this->const_name = 'MAIN_MODULE_'.strtoupper($this->name);
65 $this->picto = 'product';
66
67 // Data directories to create when module is enabled
68 $this->dirs = array("/product/temp");
69
70 // Dependencies
71 $this->hidden = false; // A condition to hide module
72 $this->depends = array(); // List of module class names as string that must be enabled if this module is enabled
73 $this->requiredby = array("modStock", "modBarcode", "modProductBatch", "modVariants", "modBom"); // List of module ids to disable if this one is disabled
74 $this->conflictwith = array(); // List of module class names as string this module is in conflict with
75 $this->phpmin = array(7, 0); // Minimum version of PHP required by module
76
77 // Config pages
78 $this->config_page_url = array("product.php@product");
79 $this->langfiles = array("products", "companies", "stocks", "bills");
80
81 // Constants
82 $this->const = [
83 [
84 "PRODUCT_CODEPRODUCT_ADDON",
85 "chaine",
86 "mod_codeproduct_leopard",
87 'Module to control product codes',
88 0,
89 ],
90 [
91
92 "PRODUCT_PRICE_UNIQ",
93 "chaine",
94 "1",
95 'pricing rule by default',
96 0,
97 ],
98 /*[
99 "PRODUCT_ADDON_PDF",
100 "chaine",
101 "standard",
102 'Default module for document generation',
103 0,
104 ],*/
105 ];
106
107 // Boxes
108 $this->boxes = array(
109 0 => array('file' => 'box_produits.php', 'enabledbydefaulton' => 'Home'),
110 1 => array('file' => 'box_produits_alerte_stock.php', 'enabledbydefaulton' => 'Home'),
111 2 => array('file' => 'box_graph_product_distribution.php', 'enabledbydefaulton' => 'Home')
112 );
113
114 // Permissions
115 $this->rights = array();
116 $this->rights_class = 'produit';
117 $r = 0;
118
119 $this->rights[$r][0] = 31; // id de la permission
120 $this->rights[$r][1] = 'Read products'; // libelle de la permission
121 $this->rights[$r][2] = 'r'; // type de la permission (deprecated)
122 $this->rights[$r][3] = 0; // La permission est-elle une permission par default
123 $this->rights[$r][4] = 'lire';
124 $r++;
125
126 $this->rights[$r][0] = 32; // id de la permission
127 $this->rights[$r][1] = 'Create/modify products'; // libelle de la permission
128 $this->rights[$r][2] = 'w'; // type de la permission (deprecated)
129 $this->rights[$r][3] = 0; // La permission est-elle une permission par default
130 $this->rights[$r][4] = 'creer';
131 $r++;
132
133 $this->rights[$r][0] = 33; // id de la permission
134 $this->rights[$r][1] = 'Read prices products'; // libelle de la permission
135 $this->rights[$r][2] = 'w'; // type de la permission (deprecated)
136 $this->rights[$r][3] = 0; // La permission est-elle une permission par default
137 $this->rights[$r][4] = 'product_advance';
138 $this->rights[$r][5] = 'read_prices';
139 $r++;
140
141 $this->rights[$r][0] = 35; // id de la permission
142 $this->rights[$r][1] = 'Read supplier prices'; // libelle de la permission
143 $this->rights[$r][2] = 'w'; // type de la permission (deprecated)
144 $this->rights[$r][3] = 0; // La permission est-elle une permission par default
145 $this->rights[$r][4] = 'product_advance';
146 $this->rights[$r][5] = 'read_supplier_prices';
147 $r++;
148
149 $this->rights[$r][0] = 34; // id de la permission
150 $this->rights[$r][1] = 'Delete products'; // libelle de la permission
151 $this->rights[$r][2] = 'd'; // type de la permission (deprecated)
152 $this->rights[$r][3] = 0; // La permission est-elle une permission par default
153 $this->rights[$r][4] = 'supprimer';
154 $r++;
155
156 $this->rights[$r][0] = 38; // Must be same permission than in service module
157 $this->rights[$r][1] = 'Export products';
158 $this->rights[$r][2] = 'r';
159 $this->rights[$r][3] = 0;
160 $this->rights[$r][4] = 'export';
161 $r++;
162
163 $this->rights[$r][0] = 39;
164 $this->rights[$r][1] = 'Ignore minimum price';
165 $this->rights[$r][2] = 'r';
166 $this->rights[$r][3] = 0;
167 $this->rights[$r][4] = 'ignore_price_min_advance';
168 $r++;
169
170 // Menus
171 //-------
172
173 $this->menu = 1; // This module adds menu entries. They are coded into menu manager.
174 /* We can't enable this here because it must be enabled in both product and service module and this creates duplicate inserts
175 $r = 0;
176 $this->menu[$r] = array(
177 // Use 'fk_mainmenu=xxx' or 'fk_mainmenu=xxx,fk_leftmenu=yyy' where xxx is mainmenucode and yyy is a leftmenucode
178 'fk_menu' => 'fk_mainmenu=home,fk_leftmenu=admintools',
179 // This is a Left menu entry
180 'type' => 'left',
181 'titre' => 'ProductVatMassChange',
182 'url' => '/product/admin/product_tools.php?mainmenu=home&leftmenu=admintools',
183 // Lang file to use (without .lang) by module. File must be in langs/code_CODE/ directory.
184 'langs' => 'products',
185 'position' => 300,
186 // Define condition to show or hide menu entry. Use '$conf->mymodule->enabled' if entry must be visible if module is enabled. Use '$leftmenu==\'system\'' to show if leftmenu system is selected.
187 'enabled' => 'isModEnabled("product") && preg_match(\'/^(admintools|all)/\',$leftmenu)',
188 // Use 'perms'=>'$user->hasRight("mymodule","level1","level2")' if you want your menu with a permission rules
189 'perms' => '1',
190 'target' => '',
191 // 0=Menu for internal users, 1=external users, 2=both
192 'user' => 0
193 );
194 $r++;
195 */
196
197 $usenpr = 0;
198 if (is_object($mysoc)) {
199 $usenpr = $mysoc->useNPR();
200 }
201
202 // Exports
203 //--------
204 $r = 0;
205
206 $alias_product_perentity = !getDolGlobalString('MAIN_PRODUCT_PERENTITY_SHARED') ? "p" : "ppe";
207
208 $r++;
209 $this->export_code[$r] = $this->rights_class.'_'.$r;
210 $this->export_label[$r] = "Products"; // Translation key (used only if key ExportDataset_xxx_z not found)
211 $this->export_permission[$r] = array(array("produit", "export"));
212 $this->export_fields_array[$r] = array(
213 'p.rowid' => "Id", 'p.ref' => "Ref", 'p.label' => "Label",
214 'p.fk_product_type' => 'Type', 'p.tosell' => "OnSell", 'p.tobuy' => "OnBuy",
215 'p.description' => "Description", 'p.url' => "PublicUrl",
216 'p.customcode' => 'CustomsCode', 'p.fk_country' => 'IDCountry',
217 $alias_product_perentity . '.accountancy_code_sell' => "ProductAccountancySellCode", $alias_product_perentity . '.accountancy_code_sell_intra' => "ProductAccountancySellIntraCode",
218 $alias_product_perentity . '.accountancy_code_sell_export' => "ProductAccountancySellExportCode", $alias_product_perentity . '.accountancy_code_buy' => "ProductAccountancyBuyCode",
219 $alias_product_perentity . '.accountancy_code_buy_intra' => "ProductAccountancyBuyIntraCode", $alias_product_perentity . '.accountancy_code_buy_export' => "ProductAccountancyBuyExportCode",
220 'p.note' => "NotePrivate", 'p.note_public' => 'NotePublic',
221 'p.weight' => "Weight", 'p.weight_units' => "WeightUnits", 'p.length' => "Length", 'p.length_units' => "LengthUnits", 'p.width' => "Width", 'p.width_units' => "WidthUnits", 'p.height' => "Height", 'p.height_units' => "HeightUnits",
222 'p.surface' => "Surface", 'p.surface_units' => "SurfaceUnits", 'p.volume' => "Volume", 'p.volume_units' => "VolumeUnits",
223 'p.duration' => "Duration",
224 'p.finished' => 'Nature',
225 'p.price_base_type' => "PriceBase", 'p.price' => "UnitPriceHT", 'p.price_ttc' => "UnitPriceTTC",
226 'p.price_min' => "MinPriceHT",'p.price_min_ttc' => "MinPriceTTC",
227 'p.tva_tx' => 'VATRate',
228 'p.datec' => 'DateCreation', 'p.tms' => 'DateModification'
229 );
230 if (is_object($mysoc) && $usenpr) {
231 $this->export_fields_array[$r]['p.recuperableonly'] = 'NPR';
232 }
233 if (isModEnabled("supplier_order") || isModEnabled("supplier_invoice") || isModEnabled('margin')) {
234 $this->export_fields_array[$r] = array_merge($this->export_fields_array[$r], array('p.cost_price' => 'CostPrice'));
235 }
236 if (isModEnabled('stock')) {
237 $this->export_fields_array[$r] = array_merge($this->export_fields_array[$r], array('e.ref' => 'DefaultWarehouse', 'p.tobatch' => 'ManageLotSerial', 'p.stock' => 'Stock', 'p.seuil_stock_alerte' => 'StockLimit', 'p.desiredstock' => 'DesiredStock', 'p.pmp' => 'PMPValue'));
238 }
239 if (isModEnabled('barcode')) {
240 $this->export_fields_array[$r] = array_merge($this->export_fields_array[$r], array('p.barcode' => 'BarCode'));
241 }
242 $keyforselect = 'product';
243 $keyforelement = 'product';
244 $keyforaliasextra = 'extra';
245 include DOL_DOCUMENT_ROOT.'/core/extrafieldsinexport.inc.php';
246 if (isModEnabled("supplier_order") || isModEnabled("supplier_invoice")) {
247 $this->export_fields_array[$r] = array_merge($this->export_fields_array[$r], array('s.nom' => 'Supplier', 'pf.ref_fourn' => 'SupplierRef', 'pf.quantity' => 'QtyMin', 'pf.remise_percent' => 'DiscountQtyMin', 'pf.unitprice' => 'BuyingPrice', 'pf.delivery_time_days' => 'NbDaysToDelivery'));
248 }
249 if (getDolGlobalString('EXPORTTOOL_CATEGORIES')) {
250 $this->export_fields_array[$r] = array_merge($this->export_fields_array[$r], array('group_concat(cat.label)' => 'Categories'));
251 }
252 if (getDolGlobalInt('MAIN_MULTILANGS')) {
253 $this->export_fields_array[$r] = array_merge($this->export_fields_array[$r], array('l.lang' => 'Language', 'l.label' => 'TranslatedLabel', 'l.description' => 'TranslatedDescription', 'l.note' => 'TranslatedNote'));
254 }
255 if (getDolGlobalInt('PRODUCT_USE_UNITS')) {
256 $this->export_fields_array[$r]['p.fk_unit'] = 'Unit';
257 }
258 $this->export_TypeFields_array[$r] = array(
259 'p.ref' => "Text", 'p.label' => "Text",
260 'p.fk_product_type' => 'Numeric', 'p.tosell' => "Boolean", 'p.tobuy' => "Boolean",
261 'p.description' => "Text", 'p.url' => "Text",
262 $alias_product_perentity . '.accountancy_code_sell' => "Text", $alias_product_perentity . '.accountancy_code_sell_intra' => "Text", $alias_product_perentity . '.accountancy_code_sell_export' => "Text",
263 $alias_product_perentity . '.accountancy_code_buy' => "Text", $alias_product_perentity . '.accountancy_code_buy_intra' => "Text", $alias_product_perentity . '.accountancy_code_buy_export' => "Text",
264 'p.note' => "Text", 'p.note_public' => "Text",
265 'p.weight' => "Numeric", 'p.length' => "Numeric", 'p.width' => "Numeric", 'p.height' => "Numeric", 'p.surface' => "Numeric", 'p.volume' => "Numeric",
266 'p.customcode' => 'Text',
267 'p.duration' => "Text",
268 'p.finished' => 'Numeric',
269 'p.price_base_type' => "Text", 'p.price' => "Numeric", 'p.price_ttc' => "Numeric",
270 'p.price_min' => "Numeric", 'p.price_min_ttc' => "Numeric",
271 'p.tva_tx' => 'Numeric',
272 'p.datec' => 'Date', 'p.tms' => 'Date'
273 );
274 if (isModEnabled('stock')) {
275 $this->export_TypeFields_array[$r] = array_merge($this->export_TypeFields_array[$r], array('e.ref' => 'Text', 'p.tobatch' => 'Numeric', 'p.stock' => 'Numeric', 'p.seuil_stock_alerte' => 'Numeric', 'p.desiredstock' => 'Numeric', 'p.pmp' => 'Numeric', 'p.cost_price' => 'Numeric'));
276 }
277 if (isModEnabled('barcode')) {
278 $this->export_TypeFields_array[$r] = array_merge($this->export_TypeFields_array[$r], array('p.barcode' => 'Text'));
279 }
280 if (isModEnabled("supplier_order") || isModEnabled("supplier_invoice")) {
281 $this->export_TypeFields_array[$r] = array_merge($this->export_TypeFields_array[$r], array('s.nom' => 'Text', 'pf.ref_fourn' => 'Text', 'pf.unitprice' => 'Numeric', 'pf.quantity' => 'Numeric', 'pf.remise_percent' => 'Numeric', 'pf.delivery_time_days' => 'Numeric'));
282 }
283 if (getDolGlobalInt('MAIN_MULTILANGS')) {
284 $this->export_TypeFields_array[$r] = array_merge($this->export_TypeFields_array[$r], array('l.lang' => 'Text', 'l.label' => 'Text', 'l.description' => 'Text', 'l.note' => 'Text'));
285 }
286 if (getDolGlobalString('EXPORTTOOL_CATEGORIES')) {
287 $this->export_TypeFields_array[$r] = array_merge($this->export_TypeFields_array[$r], array("group_concat(cat.label)" => 'Text'));
288 }
289 $this->export_entities_array[$r] = array(); // We define here only fields that use another icon that the one defined into import_icon
290 if (getDolGlobalString('EXPORTTOOL_CATEGORIES')) {
291 $this->export_entities_array[$r] = array_merge($this->export_entities_array[$r], array("group_concat(cat.label)" => 'category'));
292 }
293 if (isModEnabled('stock')) {
294 $this->export_entities_array[$r] = array_merge($this->export_entities_array[$r], array('p.stock' => 'product', 'p.pmp' => 'product'));
295 }
296 if (isModEnabled('barcode')) {
297 $this->export_entities_array[$r] = array_merge($this->export_entities_array[$r], array('p.barcode' => 'product'));
298 }
299 if (isModEnabled("supplier_order") || isModEnabled("supplier_invoice")) {
300 $this->export_entities_array[$r] = array_merge($this->export_entities_array[$r], array('s.nom' => 'product_supplier_ref', 'pf.ref_fourn' => 'product_supplier_ref', 'pf.unitprice' => 'product_supplier_ref', 'pf.quantity' => 'product_supplier_ref', 'pf.remise_percent' => 'product_supplier_ref', 'pf.delivery_time_days' => 'product_supplier_ref'));
301 }
302 if (getDolGlobalInt('MAIN_MULTILANGS')) {
303 $this->export_entities_array[$r] = array_merge($this->export_entities_array[$r], array('l.lang' => 'translation', 'l.label' => 'translation', 'l.description' => 'translation', 'l.note' => 'translation'));
304 }
305 if (getDolGlobalString('EXPORTTOOL_CATEGORIES')) {
306 $this->export_dependencies_array[$r] = array('category' => 'p.rowid');
307 }
308 if (isModEnabled('stock')) {
309 $this->export_entities_array[$r] = array_merge($this->export_entities_array[$r], array('p.stock' => 'product', 'p.pmp' => 'product'));
310 }
311 if (isModEnabled('barcode')) {
312 $this->export_entities_array[$r] = array_merge($this->export_entities_array[$r], array('p.barcode' => 'product'));
313 }
314 if (isModEnabled("supplier_order") || isModEnabled("supplier_invoice")) {
315 $this->export_entities_array[$r] = array_merge($this->export_entities_array[$r], array('s.nom' => 'product_supplier_ref', 'pf.ref_fourn' => 'product_supplier_ref', 'pf.unitprice' => 'product_supplier_ref', 'pf.quantity' => 'product_supplier_ref', 'pf.remise_percent' => 'product_supplier_ref', 'pf.delivery_time_days' => 'product_supplier_ref'));
316 }
317 if (getDolGlobalInt('MAIN_MULTILANGS')) {
318 $this->export_entities_array[$r] = array_merge($this->export_entities_array[$r], array('l.lang' => 'translation', 'l.label' => 'translation', 'l.description' => 'translation', 'l.note' => 'translation'));
319 }
320 if (getDolGlobalString('EXPORTTOOL_CATEGORIES')) {
321 $this->export_dependencies_array[$r] = array('category' => 'p.rowid');
322 }
323 $this->export_sql_start[$r] = 'SELECT DISTINCT ';
324 $this->export_sql_end[$r] = ' FROM '.MAIN_DB_PREFIX.'product as p';
325 if (getDolGlobalString('MAIN_PRODUCT_PERENTITY_SHARED')) {
326 $this->export_sql_end[$r] .= " LEFT JOIN " . MAIN_DB_PREFIX . "product_perentity as ppe ON ppe.fk_product = p.rowid AND ppe.entity = " . ((int) $conf->entity);
327 }
328 if (getDolGlobalString('EXPORTTOOL_CATEGORIES')) {
329 $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'categorie_product as cp ON cp.fk_product = p.rowid LEFT JOIN '.MAIN_DB_PREFIX.'categorie as cat ON cp.fk_categorie = cat.rowid';
330 }
331 if (getDolGlobalInt('MAIN_MULTILANGS')) {
332 $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product_lang as l ON l.fk_product = p.rowid';
333 }
334 $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product_extrafields as extra ON p.rowid = extra.fk_object';
335 if (isModEnabled("supplier_order") || isModEnabled("supplier_invoice")) {
336 $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product_fournisseur_price as pf ON pf.fk_product = p.rowid LEFT JOIN '.MAIN_DB_PREFIX.'societe s ON s.rowid = pf.fk_soc';
337 }
338 if (isModEnabled('stock')) {
339 $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'entrepot as e ON e.rowid = p.fk_default_warehouse';
340 }
341 $this->export_sql_end[$r] .= ' WHERE p.fk_product_type = 0 AND p.entity IN ('.getEntity('product').')';
342 if (getDolGlobalString('EXPORTTOOL_CATEGORIES')) {
343 $this->export_sql_order[$r] = ' GROUP BY p.rowid'; // FIXME The group by used a generic value to say "all fields in select except function fields"
344 }
345
346 if (getDolGlobalString('PRODUIT_MULTIPRICES') || getDolGlobalString('PRODUIT_CUSTOMER_PRICES_AND_MULTIPRICES')) {
347 // Exports product multiprice
348 $r++;
349 $this->export_code[$r] = $this->rights_class.'_'.$r;
350 $this->export_label[$r] = "ProductsMultiPrice"; // Translation key (used only if key ExportDataset_xxx_z not found)
351 $this->export_permission[$r] = array(array("produit", "export"));
352 $this->export_fields_array[$r] = array('p.rowid' => "Id", 'p.ref' => "Ref", 'p.label' => "Label",
353 'pr.price_base_type' => "PriceBase", 'pr.price_level' => "PriceLevel",
354 'pr.price' => "PriceLevelUnitPriceHT", 'pr.price_ttc' => "PriceLevelUnitPriceTTC",
355 'pr.price_min' => "MinPriceLevelUnitPriceHT", 'pr.price_min_ttc' => "MinPriceLevelUnitPriceTTC",
356 'pr.tva_tx' => 'PriceLevelVATRate',
357 'pr.date_price' => 'DateCreation');
358 if (is_object($mysoc) && $usenpr) {
359 $this->export_fields_array[$r]['pr.recuperableonly'] = 'NPR';
360 }
361 //$this->export_TypeFields_array[$r]=array(
362 // 'p.ref'=>"Text",'p.label'=>"Text",'p.description'=>"Text",'p.url'=>"Text",'p.accountancy_code_sell'=>"Text",'p.accountancy_code_buy'=>"Text",
363 // 'p.note'=>"Text",'p.length'=>"Numeric",'p.surface'=>"Numeric",'p.volume'=>"Numeric",'p.weight'=>"Numeric",'p.customcode'=>'Text',
364 // 'p.price_base_type'=>"Text",'p.price'=>"Numeric",'p.price_ttc'=>"Numeric",'p.tva_tx'=>'Numeric','p.tosell'=>"Boolean",'p.tobuy'=>"Boolean",
365 // 'p.datec'=>'Date','p.tms'=>'Date'
366 //);
367 $this->export_entities_array[$r] = array('p.rowid' => "product", 'p.ref' => "product", 'p.label' => "Label",
368 'pr.price_base_type' => "product", 'pr.price_level' => "product", 'pr.price' => "product",
369 'pr.price_ttc' => "product",
370 'pr.price_min' => "product", 'pr.price_min_ttc' => "product",
371 'pr.tva_tx' => 'product',
372 'pr.recuperableonly' => 'product',
373 'pr.date_price' => "product");
374 $this->export_sql_start[$r] = 'SELECT DISTINCT ';
375 $this->export_sql_end[$r] = ' FROM '.MAIN_DB_PREFIX.'product as p';
376 $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product_price as pr ON p.rowid = pr.fk_product AND pr.entity = '.$conf->entity; // export prices only for the current entity
377 $this->export_sql_end[$r] .= ' WHERE p.entity IN ('.getEntity('product').')'; // For product and service profile
378 $this->export_sql_end[$r] .= ' AND pr.date_price = (SELECT MAX(pr2.date_price) FROM '.MAIN_DB_PREFIX.'product_price as pr2 WHERE pr2.fk_product = pr.fk_product AND pr2.price_level = pr.price_level AND pr2.entity IN ('.getEntity('product').'))'; // export only latest prices not full history
379 $this->export_sql_end[$r] .= ' ORDER BY p.ref, pr.price_level';
380 }
381
382 if (getDolGlobalString('PRODUIT_CUSTOMER_PRICES') || getDolGlobalString('PRODUIT_CUSTOMER_PRICES_AND_MULTIPRICES')) {
383 // Exports product multiprice
384 $r++;
385 $this->export_code[$r] = $this->rights_class.'_'.$r;
386 $this->export_label[$r] = "ProductsPricePerCustomer"; // Translation key (used only if key ExportDataset_xxx_z not found)
387 $this->export_permission[$r] = array(array("produit", "export"));
388 $this->export_fields_array[$r] = array('p.rowid' => "Id", 'p.ref' => "Ref", 'p.label' => "Label",
389 's.nom' => 'ThirdParty',
390 's.code_client' => 'CodeClient',
391 'pr.date_begin' => "AppliedPricesFrom",
392 'pr.date_end' => "AppliedPricesTo",
393 'pr.price_base_type' => "PriceBase",
394 'pr.price' => "PriceUnitPriceHT", 'pr.price_ttc' => "PriceUnitPriceTTC",
395 'pr.price_min' => "MinPriceUnitPriceHT", 'pr.price_min_ttc' => "MinPriceUnitPriceTTC",
396 'pr.tva_tx' => 'PriceVATRate',
397 'pr.default_vat_code' => 'PriceVATCode',
398 'pr.discount_percent' => 'Discount',
399 'pr.datec' => 'DateCreation');
400 if (is_object($mysoc) && $usenpr) {
401 $this->export_fields_array[$r]['pr.recuperableonly'] = 'NPR';
402 }
403 $this->export_entities_array[$r] = array('p.rowid' => "product", 'p.ref' => "product", 'p.label' => "Label",
404 's.nom' => 'company',
405 's.code_client' => 'company',
406 'pr.date_begin' => "product",
407 'pr.date_end' => "product",
408 'pr.price_base_type' => "product", 'pr.price' => "product",
409 'pr.price_ttc' => "product",
410 'pr.price_min' => "product", 'pr.price_min_ttc' => "product",
411 'pr.tva_tx' => 'product',
412 'pr.default_vat_code' => 'product',
413 'pr.discount_percent' => 'product',
414 'pr.recuperableonly' => 'product',
415 'pr.datec' => "product");
416 $this->export_sql_start[$r] = 'SELECT DISTINCT ';
417 $this->export_sql_end[$r] = ' FROM '.MAIN_DB_PREFIX.'product as p';
418 $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product_customer_price as pr ON p.rowid = pr.fk_product AND pr.entity = '.$conf->entity; // export prices only for the current entity
419 $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'societe as s ON pr.fk_soc = s.rowid';
420 $this->export_sql_end[$r] .= ' WHERE p.entity IN ('.getEntity('product').')'; // For product and service profile
421 }
422
423 if (getDolGlobalString('PRODUIT_SOUSPRODUITS')) {
424 // Exports virtual products
425 $r++;
426 $this->export_code[$r] = $this->rights_class.'_'.$r;
427 $this->export_label[$r] = "AssociatedProducts"; // Translation key (used only if key ExportDataset_xxx_z not found)
428 $this->export_permission[$r] = array(array("produit", "export"));
429 $this->export_fields_array[$r] = array(
430 'p.rowid' => "Id", 'p.ref' => "Ref", 'p.label' => "Label", 'p.description' => "Description", 'p.url' => "PublicUrl",
431 $alias_product_perentity . '.accountancy_code_sell' => "ProductAccountancySellCode", $alias_product_perentity . '.accountancy_code_sell_intra' => "ProductAccountancySellIntraCode",
432 $alias_product_perentity . '.accountancy_code_sell_export' => "ProductAccountancySellExportCode", $alias_product_perentity . '.accountancy_code_buy' => "ProductAccountancyBuyCode",
433 $alias_product_perentity . '.accountancy_code_buy_intra' => "ProductAccountancyBuyIntraCode", $alias_product_perentity . '.accountancy_code_buy_export' => "ProductAccountancyBuyExportCode",
434 'p.note' => "NotePrivate", 'p.note_public' => 'NotePublic',
435 'p.weight' => "Weight", 'p.length' => "Length", 'p.surface' => "Surface", 'p.volume' => "Volume", 'p.customcode' => 'CustomsCode',
436 'p.price_base_type' => "PriceBase", 'p.price' => "UnitPriceHT", 'p.price_ttc' => "UnitPriceTTC", 'p.tva_tx' => 'VATRate', 'p.tosell' => "OnSell",
437 'p.tobuy' => "OnBuy", 'p.datec' => 'DateCreation', 'p.tms' => 'DateModification'
438 );
439 if (isModEnabled('stock')) {
440 $this->export_fields_array[$r] = array_merge($this->export_fields_array[$r], array('p.stock' => 'Stock', 'p.seuil_stock_alerte' => 'StockLimit', 'p.desiredstock' => 'DesiredStock', 'p.pmp' => 'PMPValue'));
441 }
442 if (isModEnabled('barcode')) {
443 $this->export_fields_array[$r] = array_merge($this->export_fields_array[$r], array('p.barcode' => 'BarCode'));
444 }
445 $this->export_fields_array[$r] = array_merge($this->export_fields_array[$r], array('pa.qty' => 'Qty', 'pa.incdec' => 'ComposedProductIncDecStock'));
446 $this->export_TypeFields_array[$r] = array(
447 'p.ref' => "Text", 'p.label' => "Text", 'p.description' => "Text", 'p.url' => "Text",
448 $alias_product_perentity . '.accountancy_code_sell' => "Text", $alias_product_perentity . '.accountancy_code_sell_intra' => "Text", $alias_product_perentity . '.accountancy_code_sell_export' => "Text",
449 $alias_product_perentity . '.accountancy_code_buy' => "Text", $alias_product_perentity . '.accountancy_code_buy_intra' => "Text", $alias_product_perentity . '.accountancy_code_buy_export' => "Text",
450 'p.note' => "Text", 'p.note_public' => "Text",
451 'p.weight' => "Numeric", 'p.length' => "Numeric", 'p.surface' => "Numeric", 'p.volume' => "Numeric", 'p.customcode' => 'Text',
452 'p.price_base_type' => "Text", 'p.price' => "Numeric", 'p.price_ttc' => "Numeric", 'p.tva_tx' => 'Numeric', 'p.tosell' => "Boolean", 'p.tobuy' => "Boolean",
453 'p.datec' => 'Date', 'p.tms' => 'Date'
454 );
455 if (isModEnabled('stock')) {
456 $this->export_TypeFields_array[$r] = array_merge($this->export_TypeFields_array[$r], array('p.stock' => 'Numeric', 'p.seuil_stock_alerte' => 'Numeric', 'p.desiredstock' => 'Numeric', 'p.pmp' => 'Numeric', 'p.cost_price' => 'Numeric'));
457 }
458 if (isModEnabled('barcode')) {
459 $this->export_TypeFields_array[$r] = array_merge($this->export_TypeFields_array[$r], array('p.barcode' => 'Text'));
460 }
461 $this->export_TypeFields_array[$r] = array_merge($this->export_TypeFields_array[$r], array('pa.qty' => 'Numeric'));
462 $this->export_entities_array[$r] = array(
463 'p.rowid' => "virtualproduct", 'p.ref' => "virtualproduct", 'p.label' => "virtualproduct", 'p.description' => "virtualproduct", 'p.url' => "virtualproduct",
464 $alias_product_perentity . '.accountancy_code_sell' => 'virtualproduct', $alias_product_perentity . '.accountancy_code_sell_intra' => 'virtualproduct', $alias_product_perentity . '.accountancy_code_sell_export' => 'virtualproduct',
465 $alias_product_perentity . '.accountancy_code_buy' => 'virtualproduct', $alias_product_perentity . '.accountancy_code_buy_intra' => 'virtualproduct', $alias_product_perentity . '.accountancy_code_buy_export' => 'virtualproduct',
466 'p.note' => "virtualproduct", 'p.length' => "virtualproduct",
467 'p.surface' => "virtualproduct", 'p.volume' => "virtualproduct", 'p.weight' => "virtualproduct", 'p.customcode' => 'virtualproduct',
468 'p.price_base_type' => "virtualproduct", 'p.price' => "virtualproduct", 'p.price_ttc' => "virtualproduct", 'p.tva_tx' => "virtualproduct",
469 'p.tosell' => "virtualproduct", 'p.tobuy' => "virtualproduct", 'p.datec' => "virtualproduct", 'p.tms' => "virtualproduct"
470 );
471 if (isModEnabled('stock')) {
472 $this->export_entities_array[$r] = array_merge($this->export_entities_array[$r], array('p.stock' => 'virtualproduct', 'p.seuil_stock_alerte' => 'virtualproduct', 'p.desiredstock' => 'virtualproduct', 'p.pmp' => 'virtualproduct'));
473 }
474 if (isModEnabled('barcode')) {
475 $this->export_entities_array[$r] = array_merge($this->export_entities_array[$r], array('p.barcode' => 'virtualproduct'));
476 }
477 $this->export_entities_array[$r] = array_merge($this->export_entities_array[$r], array('pa.qty' => "subproduct", 'pa.incdec' => 'subproduct'));
478 $keyforselect = 'product';
479 $keyforelement = 'product';
480 $keyforaliasextra = 'extra';
481 include DOL_DOCUMENT_ROOT.'/core/extrafieldsinexport.inc.php';
482 $this->export_fields_array[$r] = array_merge($this->export_fields_array[$r], array('p2.rowid' => "Id", 'p2.ref' => "Ref", 'p2.label' => "Label", 'p2.description' => "Description"));
483 $this->export_entities_array[$r] = array_merge($this->export_entities_array[$r], array('p2.rowid' => "subproduct", 'p2.ref' => "subproduct", 'p2.label' => "subproduct", 'p2.description' => "subproduct"));
484 $this->export_sql_start[$r] = 'SELECT DISTINCT ';
485 $this->export_sql_end[$r] = ' FROM '.MAIN_DB_PREFIX.'product as p';
486 if (getDolGlobalString('MAIN_PRODUCT_PERENTITY_SHARED')) {
487 $this->export_sql_end[$r] .= " LEFT JOIN " . MAIN_DB_PREFIX . "product_perentity as ppe ON ppe.fk_product = p.rowid AND ppe.entity = " . ((int) $conf->entity);
488 }
489 $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product_extrafields as extra ON p.rowid = extra.fk_object,';
490 $this->export_sql_end[$r] .= ' '.MAIN_DB_PREFIX.'product_association as pa, '.MAIN_DB_PREFIX.'product as p2';
491 $this->export_sql_end[$r] .= ' WHERE p.entity IN ('.getEntity('product').')'; // For product and service profile
492 $this->export_sql_end[$r] .= ' AND p.rowid = pa.fk_product_pere AND p2.rowid = pa.fk_product_fils';
493 }
494
495 // Imports
496 //--------
497 $r = 0;
498
499 // Import list of products
500
501 $r++;
502 $this->import_code[$r] = $this->rights_class.'_'.$r;
503 $this->import_label[$r] = "Products"; // Translation key
504 $this->import_icon[$r] = $this->picto;
505 $this->import_entities_array[$r] = array(); // We define here only fields that use a different icon from the one defined in import_icon
506 $this->import_tables_array[$r] = array('p' => MAIN_DB_PREFIX.'product', 'extra' => MAIN_DB_PREFIX.'product_extrafields');
507 $this->import_tables_creator_array[$r] = array('p' => 'fk_user_author'); // Fields to store import user id
508 $this->import_fields_array[$r] = array(
509 'p.ref' => "Ref*",
510 'p.label' => "Label*",
511 'p.fk_product_type' => "Type*",
512 'p.tosell' => "OnSell*",
513 'p.tobuy' => "OnBuy*",
514 'p.description' => "Description",
515 'p.url' => "PublicUrl",
516 'p.customcode' => 'CustomsCode',
517 'p.fk_country' => 'CountryCode',
518 'p.accountancy_code_sell' => "ProductAccountancySellCode",
519 'p.accountancy_code_sell_intra' => "ProductAccountancySellIntraCode",
520 'p.accountancy_code_sell_export' => "ProductAccountancySellExportCode",
521 'p.accountancy_code_buy' => "ProductAccountancyBuyCode",
522 'p.accountancy_code_buy_intra' => "ProductAccountancyBuyIntraCode",
523 'p.accountancy_code_buy_export' => "ProductAccountancyBuyExportCode",
524 'p.note_public' => "NotePublic",
525 'p.note' => "NotePrivate",
526 'p.weight' => "Weight",
527 'p.weight_units' => "WeightUnits",
528 'p.length' => "Length",
529 'p.length_units' => "LengthUnits",
530 'p.width' => "Width",
531 'p.width_units' => "WidthUnits",
532 'p.height' => "Height",
533 'p.height_units' => "HeightUnits",
534 'p.surface' => "Surface",
535 'p.surface_units' => "SurfaceUnits",
536 'p.volume' => "Volume",
537 'p.volume_units' => "VolumeUnits",
538 'p.duration' => "Duration", //duration of service
539 'p.finished' => 'Nature',
540 'p.price' => "SellingPriceHT", //without
541 'p.price_min' => "MinPrice",
542 'p.price_ttc' => "SellingPriceTTC", //with tax
543 'p.price_min_ttc' => "SellingMinPriceTTC",
544 'p.price_base_type' => "PriceBaseType", //price base: with-tax (TTC) or without (HT) tax. Displays accordingly in Product card
545 'p.tva_tx' => 'VATRate',
546 'p.default_vat_code' => 'VATCode', // to use the correct vat line when there is several lines with the same vat rate for your country
547 'p.datec' => 'DateCreation',
548 'p.cost_price' => "CostPrice"
549 );
550
551 $this->import_convertvalue_array[$r] = array(
552 'p.weight_units' => array(
553 'rule' => 'fetchscalefromcodeunits', // Switch this to fetchidfromcodeunits when we will store id instead of scale in product table
554 'classfile' => '/core/class/cunits.class.php',
555 'class' => 'CUnits',
556 'method' => 'fetch',
557 'units' => 'weight',
558 'dict' => 'DictionaryMeasuringUnits'
559 ),
560 'p.length_units' => array(
561 'rule' => 'fetchscalefromcodeunits', // Switch this to fetchidfromcodeunits when we will store id instead of scale in product table
562 'classfile' => '/core/class/cunits.class.php',
563 'class' => 'CUnits',
564 'method' => 'fetch',
565 'units' => 'size',
566 'dict' => 'DictionaryMeasuringUnits'
567 ),
568 'p.width_units' => array(
569 'rule' => 'fetchscalefromcodeunits', // Switch this to fetchidfromcodeunits when we will store id instead of scale in product table
570 'classfile' => '/core/class/cunits.class.php',
571 'class' => 'CUnits',
572 'method' => 'fetch',
573 'units' => 'size',
574 'dict' => 'DictionaryMeasuringUnits'
575 ),
576 'p.height_units' => array(
577 'rule' => 'fetchscalefromcodeunits', // Switch this to fetchidfromcodeunits when we will store id instead of scale in product table
578 'classfile' => '/core/class/cunits.class.php',
579 'class' => 'CUnits',
580 'method' => 'fetch',
581 'units' => 'size',
582 'dict' => 'DictionaryMeasuringUnits'
583 ),
584 'p.surface_units' => array(
585 'rule' => 'fetchscalefromcodeunits', // Switch this to fetchidfromcodeunits when we will store id instead of scale in product table
586 'classfile' => '/core/class/cunits.class.php',
587 'class' => 'CUnits',
588 'method' => 'fetch',
589 'units' => 'surface',
590 'dict' => 'DictionaryMeasuringUnits'
591 ),
592 'p.volume_units' => array(
593 'rule' => 'fetchscalefromcodeunits', // Switch this to fetchidfromcodeunits when we will store id instead of scale in product table
594 'classfile' => '/core/class/cunits.class.php',
595 'class' => 'CUnits',
596 'method' => 'fetch',
597 'units' => 'volume',
598 'dict' => 'DictionaryMeasuringUnits'
599 ),
600 'p.fk_country' => array(
601 'rule' => 'fetchidfromcodeid',
602 'classfile' => '/core/class/ccountry.class.php',
603 'class' => 'Ccountry',
604 'method' => 'fetch',
605 'dict' => 'DictionaryCountry'
606 ),
607 'p.finished' => array(
608 'rule' => 'fetchidfromcodeorlabel',
609 'classfile' => '/core/class/cproductnature.class.php',
610 'class' => 'CProductNature',
611 'method' => 'fetch',
612 'dict' => 'DictionaryProductNature'
613 ),
614 'p.accountancy_code_sell' => array('rule' => 'accountingaccount'),
615 'p.accountancy_code_sell_intra' => array('rule' => 'accountingaccount'),
616 'p.accountancy_code_sell_export' => array('rule' => 'accountingaccount'),
617 'p.accountancy_code_buy' => array('rule' => 'accountingaccount'),
618 'p.accountancy_code_buy_intra' => array('rule' => 'accountingaccount'),
619 'p.accountancy_code_buy_export' => array('rule' => 'accountingaccount'),
620 );
621
622 $this->import_regex_array[$r] = array(
623 'p.ref' => '[^ ]',
624 'p.price_base_type' => '\AHT\z|\ATTC\z',
625 'p.tosell' => '^[0|1]$',
626 'p.tobuy' => '^[0|1]$',
627 'p.fk_product_type' => '^[0|1]$',
628 'p.datec' => '^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]$',
629 'p.recuperableonly' => '^[0|1]$',
630 );
631
632 // Complete if Stock module enabled
633 if (isModEnabled('stock')) {
634 $this->import_fields_array[$r] = array_merge($this->import_fields_array[$r], array(
635 'p.fk_default_warehouse' => 'DefaultWarehouse',
636 'p.tobatch' => 'ManageLotSerial',
637 'p.seuil_stock_alerte' => 'StockLimit', //lower limit for warning
638 'p.pmp' => 'PMPValue', //weighted average price
639 'p.desiredstock' => 'DesiredStock'//desired stock for replenishment feature
640 ));
641
642 $this->import_regex_array[$r] = array_merge($this->import_regex_array[$r], array(
643 'p.tobatch' => '^[0|1|2]$'
644 ));
645
646 $this->import_convertvalue_array[$r] = array_merge($this->import_convertvalue_array[$r], array(
647 'p.fk_default_warehouse' => array(
648 'rule' => 'fetchidfromref',
649 'classfile' => '/product/stock/class/entrepot.class.php',
650 'class' => 'Entrepot',
651 'method' => 'fetch',
652 'element' => 'Warehouse'
653 )
654 ));
655 }
656
657 if (getDolGlobalString('PRODUCT_USE_CUSTOMER_PACKAGING')) {
658 $this->import_fields_array[$r] = array_merge($this->import_fields_array[$r], array(
659 'p.packaging' => 'PackagingForThisProductSell',
660 ));
661 }
662
663 if (isModEnabled("supplier_order") || isModEnabled("supplier_invoice") || isModEnabled('margin')) {
664 $this->import_fields_array[$r] = array_merge($this->import_fields_array[$r], array('p.cost_price' => 'CostPrice'));
665 }
666 if (is_object($mysoc) && $usenpr) {
667 $this->import_fields_array[$r] = array_merge($this->import_fields_array[$r], array('p.recuperableonly' => 'NPR'));
668 }
669 if (is_object($mysoc) && $mysoc->useLocalTax(1)) {
670 $this->import_fields_array[$r] = array_merge($this->import_fields_array[$r], array('p.localtax1_tx' => 'LT1', 'p.localtax1_type' => 'LT1Type'));
671 }
672 if (is_object($mysoc) && $mysoc->useLocalTax(2)) {
673 $this->import_fields_array[$r] = array_merge($this->import_fields_array[$r], array('p.localtax2_tx' => 'LT2', 'p.localtax2_type' => 'LT2Type'));
674 }
675 if (isModEnabled('barcode')) {
676 $this->import_fields_array[$r] = array_merge($this->import_fields_array[$r], array('p.barcode' => 'BarCode'));
677 }
678 if (getDolGlobalInt('PRODUCT_USE_UNITS')) {
679 $this->import_fields_array[$r]['p.fk_unit'] = 'Unit';
680 }
681
682 // Add extra fields
683 $import_extrafield_sample = array();
684 $sql = "SELECT name, label, fieldrequired FROM ".MAIN_DB_PREFIX."extrafields WHERE type <> 'separate' AND elementtype = 'product' AND entity IN (0, ".$conf->entity.")";
685 $resql = $this->db->query($sql);
686 if ($resql) { // This can fail when class is used on old database (during migration for example)
687 while ($obj = $this->db->fetch_object($resql)) {
688 $fieldname = 'extra.'.$obj->name;
689 $fieldlabel = ucfirst($obj->label);
690 $this->import_fields_array[$r][$fieldname] = $fieldlabel.($obj->fieldrequired ? '*' : '');
691 $import_extrafield_sample[$fieldname] = $fieldlabel;
692 }
693 }
694 // End add extra fields
695 $this->import_fieldshidden_array[$r] = array('extra.fk_object' => 'lastrowid-'.MAIN_DB_PREFIX.'product'); // aliastable.field => ('user->id' or 'lastrowid-'.tableparent)
696
697 // field order as per structure of table llx_product
698 $import_sample = array(
699 'p.ref' => "ref:PREF123456",
700 'p.datec' => dol_print_date(dol_now(), '%Y-%m-%d'),
701 'p.label' => "Product name in default language",
702 'p.description' => "Product description in default language",
703 'p.note_public' => "a public note (free text)",
704 'p.note' => "a private note (free text)",
705 'p.customcode' => 'customs code',
706 'p.fk_country' => 'FR',
707 'p.price' => "100",
708 'p.price_min' => "100",
709 'p.price_ttc' => "110",
710 'p.price_min_ttc' => "110",
711 'p.price_base_type' => "HT (show/use price excl. tax) / TTC (show/use price incl. tax)",
712 'p.tva_tx' => '10', // tax rate eg: 10. Must match numerically one of the tax rates defined for your country
713 'p.default_vat_code' => '', // to use the correct vat line when there is several lines with the same vat rate for your country
714 'p.tosell' => "0 (not for sale to customer, eg. raw material) / 1 (for sale)",
715 'p.tobuy' => "0 (not for purchase from supplier, eg. virtual product) / 1 (for purchase)",
716 'p.fk_product_type' => "0 (product) / 1 (service)",
717 'p.duration' => "eg. 365d/12m/1y",
718 'p.url' => 'link to product (no https)',
719 'p.accountancy_code_sell' => "",
720 'p.accountancy_code_sell_intra' => "",
721 'p.accountancy_code_sell_export' => "",
722 'p.accountancy_code_buy' => "",
723 'p.accountancy_code_buy_intra' => "",
724 'p.accountancy_code_buy_export' => "",
725 'p.weight' => "",
726 'p.weight_units' => 'kg', // Use a unit of measure from the dictionary. g/Kg/T etc....matches field "Short label" for unit type "weight" in table "' . MAIN_DB_PREFIX . 'c_units',
727 'p.length' => "",
728 'p.length_units' => 'm', // Use a unit of measure from the dictionary. m/cm/mm etc....matches field "Short label" for unit type "size" in table "' . MAIN_DB_PREFIX . 'c_units',
729 'p.width' => "",
730 'p.width_units' => 'm', // Use a unit of measure from the dictionary. m/cm/mm etc....matches field "Short label" for unit type "size" in table "' . MAIN_DB_PREFIX . 'c_units',
731 'p.height' => "",
732 'p.height_units' => 'm', // Use a unit of measure from the dictionary. m/cm/mm etc....matches field "Short label" for unit type "size" in table "' . MAIN_DB_PREFIX . 'c_units',
733 'p.surface' => "",
734 'p.surface_units' => 'm2', // Use a unit of measure from the dictionary. m2/cm2/mm2 etc....matches field "Short label" for unit type "surface" in table "' . MAIN_DB_PREFIX . 'c_units',
735 'p.volume' => "",
736 'p.volume_units' => 'm3', //Use a unit of measure from the dictionary. m3/cm3/mm3 etc....matches field "Short label" for unit type "volume" in table "' . MAIN_DB_PREFIX . 'c_units',
737 'p.finished' => '0 (raw material) / 1 (finished goods), matches field "code" in dictionary table "'.MAIN_DB_PREFIX.'c_product_nature"'
738 );
739 //clauses copied from import_fields_array
740 if (isModEnabled('stock')) {
741 $import_sample = array_merge($import_sample, array(
742 'p.tobatch' => "0 (don't use) / 1 (use batch) / 2 (use serial number)",
743 'p.seuil_stock_alerte' => '',
744 'p.pmp' => '0',
745 'p.desiredstock' => ''
746 ));
747 }
748 if (isModEnabled("supplier_order") || isModEnabled("supplier_invoice") || isModEnabled('margin')) {
749 $import_sample = array_merge($import_sample, array('p.cost_price' => '90'));
750 }
751 if (is_object($mysoc) && $usenpr) {
752 $import_sample = array_merge($import_sample, array('p.recuperableonly' => '0'));
753 }
754 if (is_object($mysoc) && $mysoc->useLocalTax(1)) {
755 $import_sample = array_merge($import_sample, array('p.localtax1_tx' => '', 'p.localtax1_type' => ''));
756 }
757 if (is_object($mysoc) && $mysoc->useLocalTax(2)) {
758 $import_sample = array_merge($import_sample, array('p.localtax2_tx' => '', 'p.localtax2_type' => ''));
759 }
760 if (isModEnabled('barcode')) {
761 $import_sample = array_merge($import_sample, array('p.barcode' => ''));
762 }
763 if (getDolGlobalInt('PRODUCT_USE_UNITS')) {
764 $import_sample = array_merge(
765 $import_sample,
766 array(
767 'p.fk_unit' => 'use a unit of measure from the dictionary. G/KG/M2/M3 etc....matches field "code" in table "'.MAIN_DB_PREFIX.'c_units"'
768 )
769 );
770
771 $this->import_convertvalue_array[$r] = array_merge($this->import_convertvalue_array[$r], array(
772 'p.fk_unit' => array(
773 'rule' => 'fetchidfromcodeorlabel',
774 'classfile' => '/core/class/cunits.class.php',
775 'class' => 'CUnits',
776 'method' => 'fetch',
777 'dict' => 'DictionaryUnits'
778 )
779 ));
780 }
781
782 if (getDolGlobalString('PRODUCT_USE_CUSTOMER_PACKAGING')) {
783 $import_sample = array_merge($import_sample, array(
784 'p.packaging' => "2",
785 ));
786 }
787
788 $this->import_examplevalues_array[$r] = array_merge($import_sample, $import_extrafield_sample);
789 $this->import_updatekeys_array[$r] = array('p.ref' => 'Ref');
790 if (isModEnabled('barcode')) {
791 $this->import_updatekeys_array[$r] = array_merge($this->import_updatekeys_array[$r], array('p.barcode' => 'BarCode')); //only show/allow barcode as update key if Barcode module enabled
792 }
793
794 if (getDolGlobalString('STOCK_ALLOW_ADD_LIMIT_STOCK_BY_WAREHOUSE')) {
795 // Import products limit and desired stock by product and warehouse
796 $r++;
797 $this->import_code[$r] = $this->rights_class.'_stock_by_warehouse';
798 $this->import_label[$r] = "ProductStockWarehouse"; // Translation key
799 $this->import_icon[$r] = $this->picto;
800 $this->import_entities_array[$r] = array(); // We define here only fields that use another icon that the one defined into import_icon
801 $this->import_tables_array[$r] = array('pwp' => MAIN_DB_PREFIX.'product_warehouse_properties');
802 $this->import_fields_array[$r] = array('pwp.fk_product' => "Product*",
803 'pwp.fk_entrepot' => "Warehouse*", 'pwp.seuil_stock_alerte' => "StockLimit",
804 'pwp.desiredstock' => "DesiredStock");
805 $this->import_regex_array[$r] = array(
806 'pwp.fk_product' => 'rowid@'.MAIN_DB_PREFIX.'product',
807 'pwp.fk_entrepot' => 'rowid@'.MAIN_DB_PREFIX.'entrepot',
808 );
809 $this->import_convertvalue_array[$r] = array(
810 'pwp.fk_product' => array('rule' => 'fetchidfromref', 'classfile' => '/product/class/product.class.php', 'class' => 'Product', 'method' => 'fetch', 'element' => 'Product')
811 ,'pwp.fk_entrepot' => array('rule' => 'fetchidfromref', 'classfile' => '/product/stock/class/entrepot.class.php', 'class' => 'Entrepot', 'method' => 'fetch', 'element' => 'Entrepot')
812 );
813 $this->import_examplevalues_array[$r] = array('pwp.fk_product' => "ref:PRODUCT_REF or id:123456",
814 'pwp.fk_entrepot' => "ref:WAREHOUSE_REF or id:123456",
815 'pwp.seuil_stock_alerte' => "100",
816 'pwp.desiredstock' => "110"
817 );
818 $this->import_updatekeys_array[$r] = array('pwp.fk_product' => 'Product', 'pwp.fk_entrepot' => 'Warehouse');
819 }
820
821 if (isModEnabled("supplier_order") || isModEnabled("supplier_invoice")) {
822 // Import suppliers prices (note: this code is duplicated in module Service)
823 $r++;
824 $this->import_code[$r] = $this->rights_class.'_supplierprices';
825 $this->import_label[$r] = "SuppliersPricesOfProductsOrServices"; // Translation key
826 $this->import_icon[$r] = $this->picto;
827 $this->import_entities_array[$r] = array(); // We define here only fields that use another icon that the one defined into import_icon
828 $this->import_tables_array[$r] = array('sp' => MAIN_DB_PREFIX.'product_fournisseur_price', 'extra' => MAIN_DB_PREFIX.'product_fournisseur_price_extrafields');
829 $this->import_tables_creator_array[$r] = array('sp' => 'fk_user');
830 $this->import_fields_array[$r] = array(//field order as per structure of table llx_product_fournisseur_price, without optional fields
831 'sp.fk_product' => "ProductOrService*",
832 'sp.fk_soc' => "Supplier*",
833 'sp.ref_fourn' => 'SupplierRef*',
834 'sp.quantity' => "QtyMin*",
835 'sp.tva_tx' => 'VATRate',
836 'sp.default_vat_code' => 'VATCode',
837 'sp.delivery_time_days' => 'NbDaysToDelivery',
838 'sp.supplier_reputation' => 'SupplierReputation',
839 'sp.status' => 'Status',
840 'sp.datec' => 'DateCreation'
841 );
842 if (is_object($mysoc) && $usenpr) {
843 $this->import_fields_array[$r] = array_merge($this->import_fields_array[$r], array('sp.recuperableonly' => 'VATNPR'));
844 }
845 if (is_object($mysoc) && $mysoc->useLocalTax(1)) {
846 $this->import_fields_array[$r] = array_merge($this->import_fields_array[$r], array('sp.localtax1_tx' => 'LT1', 'sp.localtax1_type' => 'LT1Type'));
847 }
848 if (is_object($mysoc) && $mysoc->useLocalTax(2)) {
849 $this->import_fields_array[$r] = array_merge($this->import_fields_array[$r], array('sp.localtax2_tx' => 'LT2', 'sp.localtax2_type' => 'LT2Type'));
850 }
851 $this->import_fields_array[$r] = array_merge($this->import_fields_array[$r], array(
852 'sp.price' => "PriceQtyMinHT*",
853 'sp.unitprice' => 'UnitPriceHT*', // TODO Make this field not required and calculate it from price and qty
854 'sp.remise_percent' => 'DiscountQtyMin'
855 ));
856
857 if (isModEnabled("multicurrency")) {
858 $this->import_fields_array[$r] = array_merge($this->import_fields_array[$r], array(
859 'sp.fk_multicurrency' => 'CurrencyCodeId', //ideally this should be automatically obtained from the CurrencyCode on the next line
860 'sp.multicurrency_code' => 'CurrencyCode',
861 'sp.multicurrency_tx' => 'CurrencyRate',
862 'sp.multicurrency_unitprice' => 'CurrencyUnitPrice',
863 'sp.multicurrency_price' => 'CurrencyPrice',
864 ));
865 }
866
867 if (getDolGlobalString('PRODUCT_USE_SUPPLIER_PACKAGING')) {
868 $this->import_fields_array[$r] = array_merge($this->import_fields_array[$r], array('sp.packaging' => 'PackagingForThisProduct'));
869 }
870
871 // Add extra fields
872 $import_extrafield_sample = array();
873 $sql = "SELECT name, label, fieldrequired FROM ".MAIN_DB_PREFIX."extrafields WHERE type <> 'separate' AND elementtype = 'product_fournisseur_price' AND entity IN (0, ".$conf->entity.")";
874 $resql = $this->db->query($sql);
875 if ($resql) { // This can fail when class is used on old database (during migration for example)
876 while ($obj = $this->db->fetch_object($resql)) {
877 $fieldname = 'extra.'.$obj->name;
878 $fieldlabel = ucfirst($obj->label);
879 $this->import_fields_array[$r][$fieldname] = $fieldlabel.($obj->fieldrequired ? '*' : '');
880 $import_extrafield_sample[$fieldname] = $fieldlabel;
881 }
882 }
883 // End add extra fields
884
885 // Add some field automatically (if they are not yet provided explicitly)
886 $this->import_fieldshidden_array[$r] = array(
887 'sp.datec' => 'const-'.dol_print_date(dol_now(), 'standard'),
888 'extra.fk_object' => 'lastrowid-'.MAIN_DB_PREFIX.'product_fournisseur_price'
889 ); // aliastable.field => ('user->id' or 'lastrowid-'.tableparent or 'const-xxxx')
890
891 $this->import_convertvalue_array[$r] = array(
892 'sp.fk_soc' => array('rule' => 'fetchidfromref', 'classfile' => '/societe/class/societe.class.php', 'class' => 'Societe', 'method' => 'fetch', 'element' => 'ThirdParty'),
893 'sp.fk_product' => array('rule' => 'fetchidfromref', 'classfile' => '/product/class/product.class.php', 'class' => 'Product', 'method' => 'fetch', 'element' => 'Product')
894 );
895
896 $this->import_regex_array[$r] = array(
897 'sp.datec' => '^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9]$'
898 );
899
900 $this->import_examplevalues_array[$r] = array(
901 'sp.fk_product' => "ref:PRODUCT_REF or id:123456",
902 'sp.fk_soc' => "My Supplier",
903 'sp.ref_fourn' => "XYZ-F123456",
904 'sp.quantity' => "5",
905 'sp.tva_tx' => '10',
906 'sp.default_vat_code' => '',
907 'sp.price' => "50",
908 'sp.unitprice' => '50',
909 'sp.remise_percent' => '0',
910 'sp.delivery_time_days' => '5',
911 'sp.supplier_reputation' => 'FAVORITE / NOTTHGOOD / DONOTORDER',
912 'sp.status' => '1',
913 'sp.datec' => dol_print_date(dol_now(), '%Y-%m-%d %H:%M:%S'),
914 );
915 if (is_object($mysoc) && $usenpr) {
916 $this->import_examplevalues_array[$r] = array_merge($this->import_examplevalues_array[$r], array('sp.recuperableonly' => ''));
917 }
918 if (is_object($mysoc) && $mysoc->useLocalTax(1)) {
919 $this->import_examplevalues_array[$r] = array_merge($this->import_examplevalues_array[$r], array('sp.localtax1_tx' => 'LT1', 'sp.localtax1_type' => 'LT1Type'));
920 }
921 if (is_object($mysoc) && $mysoc->useLocalTax(2)) {
922 $this->import_examplevalues_array[$r] = array_merge($this->import_examplevalues_array[$r], array('sp.localtax2_tx' => 'LT2', 'sp.localtax2_type' => 'LT2Type'));
923 }
924 $this->import_examplevalues_array[$r] = array_merge($this->import_examplevalues_array[$r], array(
925 'sp.price' => "50.00",
926 'sp.unitprice' => '10',
927 // TODO Make this field not required and calculate it from price and qty
928 'sp.remise_percent' => '20'
929 ));
930 if (isModEnabled("multicurrency")) {
931 $this->import_examplevalues_array[$r] = array_merge($this->import_examplevalues_array[$r], array(
932 'sp.fk_multicurrency' => 'eg: 2 = the rowid for code of multicurrency currency',
933 'sp.multicurrency_code' => 'GBP',
934 'sp.multicurrency_tx' => '1.12345',
935 'sp.multicurrency_unitprice' => '',
936 // TODO Make this field not required and calculate it from price and qty
937 'sp.multicurrency_price' => ''
938 ));
939 }
940 if (getDolGlobalString('PRODUCT_USE_SUPPLIER_PACKAGING')) {
941 $this->import_examplevalues_array[$r] = array_merge($this->import_examplevalues_array[$r], array(
942 'sp.packaging' => '10',
943 ));
944 }
945
946 $this->import_updatekeys_array[$r] = array('sp.fk_product' => 'ProductOrService', 'sp.ref_fourn' => 'SupplierRef', 'sp.fk_soc' => 'Supplier', 'sp.quantity' => "QtyMin");
947 }
948
949 if (getDolGlobalString('PRODUIT_MULTIPRICES') || getDolGlobalString('PRODUIT_CUSTOMER_PRICES_AND_MULTIPRICES')) {
950 // Import products multiprices
951 $r++;
952 $this->import_code[$r] = $this->rights_class.'_multiprice';
953 $this->import_label[$r] = "ProductsOrServiceMultiPrice"; // Translation key
954 $this->import_icon[$r] = $this->picto;
955 $this->import_entities_array[$r] = array(); // We define here only fields that use another icon that the one defined into import_icon
956 $this->import_tables_array[$r] = array('pr' => MAIN_DB_PREFIX.'product_price', 'extra' => MAIN_DB_PREFIX.'product_price_extrafields');
957 $this->import_tables_creator_array[$r] = array('pr' => 'fk_user_author'); // Fields to store import user id
958 $this->import_fields_array[$r] = array('pr.fk_product' => "ProductOrService*",
959 'pr.price_base_type' => "PriceBase", 'pr.price_level' => "PriceLevel",
960 'pr.price' => "PriceLevelUnitPriceHT", 'pr.price_ttc' => "PriceLevelUnitPriceTTC",
961 'pr.price_min' => "MinPriceLevelUnitPriceHT", 'pr.price_min_ttc' => "MinPriceLevelUnitPriceTTC",
962 'pr.date_price' => 'DateCreation*');
963 if (getDolGlobalString('PRODUIT_MULTIPRICES_USE_VAT_PER_LEVEL')) {
964 $this->import_fields_array[$r]['pr.tva_tx'] = 'VATRate';
965 }
966 if (is_object($mysoc) && $usenpr) {
967 $this->import_fields_array[$r] = array_merge($this->import_fields_array[$r], array('pr.recuperableonly' => 'NPR'));
968 }
969
970 // Add extra fields
971 $import_extrafield_sample = array();
972 $sql = "SELECT name, label, fieldrequired FROM ".MAIN_DB_PREFIX."extrafields WHERE type <> 'separate' AND elementtype = 'product_price' AND entity IN (0, ".$conf->entity.")";
973 $resql = $this->db->query($sql);
974 if ($resql) { // This can fail when class is used on old database (during migration for example)
975 while ($obj = $this->db->fetch_object($resql)) {
976 $fieldname = 'extra.'.$obj->name;
977 $fieldlabel = ucfirst($obj->label);
978 $this->import_fields_array[$r][$fieldname] = $fieldlabel.($obj->fieldrequired ? '*' : '');
979 $import_extrafield_sample[$fieldname] = $fieldlabel;
980 }
981 }
982 // End add extra fields
983 $this->import_fieldshidden_array[$r] = array('extra.fk_object' => 'lastrowid-'.MAIN_DB_PREFIX.'product_price'); // aliastable.field => ('user->id' or 'lastrowid-'.tableparent)
984
985 $this->import_regex_array[$r] = array(
986 'pr.datec' => '^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]$',
987 'pr.recuperableonly' => '^[0|1]$'
988 );
989
990 $this->import_convertvalue_array[$r] = array(
991 'pr.fk_product' => array('rule' => 'fetchidfromref', 'classfile' => '/product/class/product.class.php', 'class' => 'Product', 'method' => 'fetch', 'element' => 'Product')
992 );
993 $this->import_examplevalues_array[$r] = array('pr.fk_product' => "ref:PRODUCT_REF or id:123456",
994 'pr.price_base_type' => "HT (for excl tax) or TTC (for inc tax)", 'pr.price_level' => "1",
995 'pr.price' => "100", 'pr.price_ttc' => "110",
996 'pr.price_min' => "100", 'pr.price_min_ttc' => "110",
997 'pr.tva_tx' => '20',
998 'pr.recuperableonly' => '0',
999 'pr.date_price' => '2020-12-31');
1000 }
1001
1002 if (getDolGlobalInt('MAIN_MULTILANGS')) {
1003 // Import translations of product names and descriptions
1004 $r++;
1005 $this->import_code[$r] = $this->rights_class.'_languages';
1006 $this->import_label[$r] = "ProductsOrServicesTranslations";
1007 $this->import_icon[$r] = $this->picto;
1008 $this->import_entities_array[$r] = array(); // We define here only fields that use another icon that the one defined into import_icon
1009 $this->import_tables_array[$r] = array('l' => MAIN_DB_PREFIX.'product_lang');
1010 // multiline translation, one line per translation
1011 $this->import_fields_array[$r] = array('l.fk_product' => 'ProductOrService*', 'l.lang' => 'Language*', 'l.label' => 'TranslatedLabel', 'l.description' => 'TranslatedDescription');
1012 //$this->import_fields_array[$r]['l.note']='TranslatedNote';
1013 $this->import_convertvalue_array[$r] = array(
1014 'l.fk_product' => array('rule' => 'fetchidfromref', 'classfile' => '/product/class/product.class.php', 'class' => 'Product', 'method' => 'fetch', 'element' => 'Product')
1015 );
1016 $this->import_examplevalues_array[$r] = array('l.fk_product' => 'ref:PRODUCT_REF or id:123456', 'l.lang' => 'en_US', 'l.label' => 'Label in en_US', 'l.description' => 'Desc in en_US');
1017 $this->import_updatekeys_array[$r] = array('l.fk_product' => 'ProductOrService', 'l.lang' => 'Language');
1018 }
1019
1020 if (getDolGlobalInt('PRODUIT_SOUSPRODUITS')) {
1021 // Import products kit
1022 $r++;
1023 $this->import_code[$r] = $this->rights_class . '_' . $r;
1024 $this->import_label[$r] = "AssociatedProducts"; // Translation key
1025 $this->import_icon[$r] = $this->picto;
1026 $this->import_entities_array[$r] = array(); // We define here only fields that use another icon that the one defined into import_icon
1027 $this->import_tables_array[$r] = array('pa' => MAIN_DB_PREFIX . 'product_association');
1028 $this->import_fields_array[$r] = array('pa.fk_product_pere' => 'ParentProducts', 'pa.fk_product_fils' => 'ComposedProduct', 'pa.qty' => 'Qty', 'pa.incdec' => 'ComposedProductIncDecStock', 'pa.rang' => 'rang');
1029
1030 $this->import_convertvalue_array[$r] = array(
1031 'pa.fk_product_pere' => array('rule' => 'fetchidfromref', 'classfile' => '/product/class/product.class.php', 'class' => 'Product', 'method' => 'fetch', 'element' => 'Product'),
1032 'pa.fk_product_fils' => array('rule' => 'fetchidfromref', 'classfile' => '/product/class/product.class.php', 'class' => 'Product', 'method' => 'fetch', 'element' => 'Product')
1033 );
1034 $this->import_examplevalues_array[$r] = array(
1035 'pa.fk_product_pere' => "ref:PREF123456",
1036 'pa.fk_product_fils' => "ref:PREF123456",
1037 'pa.qty' => "100",
1038 'pa.incdec' => "0",
1039 'pa.rang' => "1");
1040 $this->import_regex_array[$r] = array('pa.fk_product_pere' => 'rowid@'.MAIN_DB_PREFIX.'product', 'pa.fk_product_fils' => 'rowid@'.MAIN_DB_PREFIX.'product');
1041 $this->import_updatekeys_array[$r] = array('pa.fk_product_pere' => 'ref parent', 'pa.fk_product_fils' => "ref enfant");
1042 }
1043 }
1044
1045
1054 public function init($options = '')
1055 {
1056 $this->remove($options);
1057
1058 $sql = array();
1059
1060 return $this->_init($sql, $options);
1061 }
1062}
Class DolibarrModules.
_init($array_sql, $options='')
Enables a module.
Class descriptor of Product module.
__construct($db)
Constructor.
init($options='')
Function called when module is enabled.
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_now($mode='auto')
Return date for now.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.
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:161