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 // Entity scope must be identical in the JOIN and in the MAX() subquery, else products whose latest price is in a shared entity are dropped
377 $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product_price as pr ON p.rowid = pr.fk_product AND pr.entity IN ('.getEntity('productprice').')';
378 $this->export_sql_end[$r] .= ' WHERE p.entity IN ('.getEntity('product').')'; // For product and service profile
379 $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('productprice').'))'; // export only latest prices not full history
380 $this->export_sql_end[$r] .= ' ORDER BY p.ref, pr.price_level';
381 }
382
383 if (getDolGlobalString('PRODUIT_CUSTOMER_PRICES') || getDolGlobalString('PRODUIT_CUSTOMER_PRICES_AND_MULTIPRICES')) {
384 // Exports product multiprice
385 $r++;
386 $this->export_code[$r] = $this->rights_class.'_'.$r;
387 $this->export_label[$r] = "ProductsPricePerCustomer"; // Translation key (used only if key ExportDataset_xxx_z not found)
388 $this->export_permission[$r] = array(array("produit", "export"));
389 $this->export_fields_array[$r] = array('p.rowid' => "Id", 'p.ref' => "Ref", 'p.label' => "Label",
390 's.nom' => 'ThirdParty',
391 's.code_client' => 'CodeClient',
392 'pr.date_begin' => "AppliedPricesFrom",
393 'pr.date_end' => "AppliedPricesTo",
394 'pr.price_base_type' => "PriceBase",
395 'pr.price' => "PriceUnitPriceHT", 'pr.price_ttc' => "PriceUnitPriceTTC",
396 'pr.price_min' => "MinPriceUnitPriceHT", 'pr.price_min_ttc' => "MinPriceUnitPriceTTC",
397 'pr.tva_tx' => 'PriceVATRate',
398 'pr.default_vat_code' => 'PriceVATCode',
399 'pr.discount_percent' => 'Discount',
400 'pr.datec' => 'DateCreation');
401 if (is_object($mysoc) && $usenpr) {
402 $this->export_fields_array[$r]['pr.recuperableonly'] = 'NPR';
403 }
404 $this->export_entities_array[$r] = array('p.rowid' => "product", 'p.ref' => "product", 'p.label' => "Label",
405 's.nom' => 'company',
406 's.code_client' => 'company',
407 'pr.date_begin' => "product",
408 'pr.date_end' => "product",
409 'pr.price_base_type' => "product", 'pr.price' => "product",
410 'pr.price_ttc' => "product",
411 'pr.price_min' => "product", 'pr.price_min_ttc' => "product",
412 'pr.tva_tx' => 'product',
413 'pr.default_vat_code' => 'product',
414 'pr.discount_percent' => 'product',
415 'pr.recuperableonly' => 'product',
416 'pr.datec' => "product");
417 $this->export_sql_start[$r] = 'SELECT DISTINCT ';
418 $this->export_sql_end[$r] = ' FROM '.MAIN_DB_PREFIX.'product as p';
419 $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
420 $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'societe as s ON pr.fk_soc = s.rowid';
421 $this->export_sql_end[$r] .= ' WHERE p.entity IN ('.getEntity('product').')'; // For product and service profile
422 }
423
424 if (getDolGlobalString('PRODUIT_SOUSPRODUITS')) {
425 // Exports virtual products
426 $r++;
427 $this->export_code[$r] = $this->rights_class.'_'.$r;
428 $this->export_label[$r] = "AssociatedProducts"; // Translation key (used only if key ExportDataset_xxx_z not found)
429 $this->export_permission[$r] = array(array("produit", "export"));
430 $this->export_fields_array[$r] = array(
431 'p.rowid' => "Id", 'p.ref' => "Ref", 'p.label' => "Label", 'p.description' => "Description", 'p.url' => "PublicUrl",
432 $alias_product_perentity . '.accountancy_code_sell' => "ProductAccountancySellCode", $alias_product_perentity . '.accountancy_code_sell_intra' => "ProductAccountancySellIntraCode",
433 $alias_product_perentity . '.accountancy_code_sell_export' => "ProductAccountancySellExportCode", $alias_product_perentity . '.accountancy_code_buy' => "ProductAccountancyBuyCode",
434 $alias_product_perentity . '.accountancy_code_buy_intra' => "ProductAccountancyBuyIntraCode", $alias_product_perentity . '.accountancy_code_buy_export' => "ProductAccountancyBuyExportCode",
435 'p.note' => "NotePrivate", 'p.note_public' => 'NotePublic',
436 'p.weight' => "Weight", 'p.length' => "Length", 'p.surface' => "Surface", 'p.volume' => "Volume", 'p.customcode' => 'CustomsCode',
437 'p.price_base_type' => "PriceBase", 'p.price' => "UnitPriceHT", 'p.price_ttc' => "UnitPriceTTC", 'p.tva_tx' => 'VATRate', 'p.tosell' => "OnSell",
438 'p.tobuy' => "OnBuy", 'p.datec' => 'DateCreation', 'p.tms' => 'DateModification'
439 );
440 if (isModEnabled('stock')) {
441 $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'));
442 }
443 if (isModEnabled('barcode')) {
444 $this->export_fields_array[$r] = array_merge($this->export_fields_array[$r], array('p.barcode' => 'BarCode'));
445 }
446 $this->export_fields_array[$r] = array_merge($this->export_fields_array[$r], array('pa.qty' => 'Qty', 'pa.incdec' => 'ComposedProductIncDecStock'));
447 $this->export_TypeFields_array[$r] = array(
448 'p.ref' => "Text", 'p.label' => "Text", 'p.description' => "Text", 'p.url' => "Text",
449 $alias_product_perentity . '.accountancy_code_sell' => "Text", $alias_product_perentity . '.accountancy_code_sell_intra' => "Text", $alias_product_perentity . '.accountancy_code_sell_export' => "Text",
450 $alias_product_perentity . '.accountancy_code_buy' => "Text", $alias_product_perentity . '.accountancy_code_buy_intra' => "Text", $alias_product_perentity . '.accountancy_code_buy_export' => "Text",
451 'p.note' => "Text", 'p.note_public' => "Text",
452 'p.weight' => "Numeric", 'p.length' => "Numeric", 'p.surface' => "Numeric", 'p.volume' => "Numeric", 'p.customcode' => 'Text',
453 'p.price_base_type' => "Text", 'p.price' => "Numeric", 'p.price_ttc' => "Numeric", 'p.tva_tx' => 'Numeric', 'p.tosell' => "Boolean", 'p.tobuy' => "Boolean",
454 'p.datec' => 'Date', 'p.tms' => 'Date'
455 );
456 if (isModEnabled('stock')) {
457 $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'));
458 }
459 if (isModEnabled('barcode')) {
460 $this->export_TypeFields_array[$r] = array_merge($this->export_TypeFields_array[$r], array('p.barcode' => 'Text'));
461 }
462 $this->export_TypeFields_array[$r] = array_merge($this->export_TypeFields_array[$r], array('pa.qty' => 'Numeric'));
463 $this->export_entities_array[$r] = array(
464 'p.rowid' => "virtualproduct", 'p.ref' => "virtualproduct", 'p.label' => "virtualproduct", 'p.description' => "virtualproduct", 'p.url' => "virtualproduct",
465 $alias_product_perentity . '.accountancy_code_sell' => 'virtualproduct', $alias_product_perentity . '.accountancy_code_sell_intra' => 'virtualproduct', $alias_product_perentity . '.accountancy_code_sell_export' => 'virtualproduct',
466 $alias_product_perentity . '.accountancy_code_buy' => 'virtualproduct', $alias_product_perentity . '.accountancy_code_buy_intra' => 'virtualproduct', $alias_product_perentity . '.accountancy_code_buy_export' => 'virtualproduct',
467 'p.note' => "virtualproduct", 'p.length' => "virtualproduct",
468 'p.surface' => "virtualproduct", 'p.volume' => "virtualproduct", 'p.weight' => "virtualproduct", 'p.customcode' => 'virtualproduct',
469 'p.price_base_type' => "virtualproduct", 'p.price' => "virtualproduct", 'p.price_ttc' => "virtualproduct", 'p.tva_tx' => "virtualproduct",
470 'p.tosell' => "virtualproduct", 'p.tobuy' => "virtualproduct", 'p.datec' => "virtualproduct", 'p.tms' => "virtualproduct"
471 );
472 if (isModEnabled('stock')) {
473 $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'));
474 }
475 if (isModEnabled('barcode')) {
476 $this->export_entities_array[$r] = array_merge($this->export_entities_array[$r], array('p.barcode' => 'virtualproduct'));
477 }
478 $this->export_entities_array[$r] = array_merge($this->export_entities_array[$r], array('pa.qty' => "subproduct", 'pa.incdec' => 'subproduct'));
479 $keyforselect = 'product';
480 $keyforelement = 'product';
481 $keyforaliasextra = 'extra';
482 include DOL_DOCUMENT_ROOT.'/core/extrafieldsinexport.inc.php';
483 $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"));
484 $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"));
485 $this->export_sql_start[$r] = 'SELECT DISTINCT ';
486 $this->export_sql_end[$r] = ' FROM '.MAIN_DB_PREFIX.'product as p';
487 if (getDolGlobalString('MAIN_PRODUCT_PERENTITY_SHARED')) {
488 $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);
489 }
490 $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product_extrafields as extra ON p.rowid = extra.fk_object,';
491 $this->export_sql_end[$r] .= ' '.MAIN_DB_PREFIX.'product_association as pa, '.MAIN_DB_PREFIX.'product as p2';
492 $this->export_sql_end[$r] .= ' WHERE p.entity IN ('.getEntity('product').')'; // For product and service profile
493 $this->export_sql_end[$r] .= ' AND p.rowid = pa.fk_product_pere AND p2.rowid = pa.fk_product_fils';
494 }
495
496 // Imports
497 //--------
498 $r = 0;
499
500 // Import list of products
501
502 $r++;
503 $this->import_code[$r] = $this->rights_class.'_'.$r;
504 $this->import_label[$r] = "Products"; // Translation key
505 $this->import_icon[$r] = $this->picto;
506 $this->import_entities_array[$r] = array(); // We define here only fields that use a different icon from the one defined in import_icon
507 $this->import_tables_array[$r] = array('p' => MAIN_DB_PREFIX.'product', 'extra' => MAIN_DB_PREFIX.'product_extrafields');
508 $this->import_tables_creator_array[$r] = array('p' => 'fk_user_author'); // Fields to store import user id
509 $this->import_fields_array[$r] = array(
510 'p.ref' => "Ref*",
511 'p.label' => "Label*",
512 'p.fk_product_type' => "Type*",
513 'p.tosell' => "OnSell*",
514 'p.tobuy' => "OnBuy*",
515 'p.description' => "Description",
516 'p.url' => "PublicUrl",
517 'p.customcode' => 'CustomsCode',
518 'p.fk_country' => 'CountryCode',
519 'p.accountancy_code_sell' => "ProductAccountancySellCode",
520 'p.accountancy_code_sell_intra' => "ProductAccountancySellIntraCode",
521 'p.accountancy_code_sell_export' => "ProductAccountancySellExportCode",
522 'p.accountancy_code_buy' => "ProductAccountancyBuyCode",
523 'p.accountancy_code_buy_intra' => "ProductAccountancyBuyIntraCode",
524 'p.accountancy_code_buy_export' => "ProductAccountancyBuyExportCode",
525 'p.note_public' => "NotePublic",
526 'p.note' => "NotePrivate",
527 'p.weight' => "Weight",
528 'p.weight_units' => "WeightUnits",
529 'p.length' => "Length",
530 'p.length_units' => "LengthUnits",
531 'p.width' => "Width",
532 'p.width_units' => "WidthUnits",
533 'p.height' => "Height",
534 'p.height_units' => "HeightUnits",
535 'p.surface' => "Surface",
536 'p.surface_units' => "SurfaceUnits",
537 'p.volume' => "Volume",
538 'p.volume_units' => "VolumeUnits",
539 'p.duration' => "Duration", //duration of service
540 'p.finished' => 'Nature',
541 'p.price' => "SellingPriceHT", //without
542 'p.price_min' => "MinPrice",
543 'p.price_ttc' => "SellingPriceTTC", //with tax
544 'p.price_min_ttc' => "SellingMinPriceTTC",
545 'p.price_base_type' => "PriceBaseType*", //price base: with-tax (TTC) or without (HT) tax. Displays accordingly in Product card
546 'p.tva_tx' => 'VATRate',
547 'p.default_vat_code' => 'VATCode', // to use the correct vat line when there is several lines with the same vat rate for your country
548 'p.datec' => 'DateCreation',
549 'p.cost_price' => "CostPrice"
550 );
551
552 $this->import_convertvalue_array[$r] = array(
553 'p.weight_units' => array(
554 'rule' => 'fetchscalefromcodeunits', // Switch this to fetchidfromcodeunits when we will store id instead of scale in product table
555 'classfile' => '/core/class/cunits.class.php',
556 'class' => 'CUnits',
557 'method' => 'fetch',
558 'units' => 'weight',
559 'dict' => 'DictionaryMeasuringUnits'
560 ),
561 'p.length_units' => array(
562 'rule' => 'fetchscalefromcodeunits', // Switch this to fetchidfromcodeunits when we will store id instead of scale in product table
563 'classfile' => '/core/class/cunits.class.php',
564 'class' => 'CUnits',
565 'method' => 'fetch',
566 'units' => 'size',
567 'dict' => 'DictionaryMeasuringUnits'
568 ),
569 'p.width_units' => array(
570 'rule' => 'fetchscalefromcodeunits', // Switch this to fetchidfromcodeunits when we will store id instead of scale in product table
571 'classfile' => '/core/class/cunits.class.php',
572 'class' => 'CUnits',
573 'method' => 'fetch',
574 'units' => 'size',
575 'dict' => 'DictionaryMeasuringUnits'
576 ),
577 'p.height_units' => array(
578 'rule' => 'fetchscalefromcodeunits', // Switch this to fetchidfromcodeunits when we will store id instead of scale in product table
579 'classfile' => '/core/class/cunits.class.php',
580 'class' => 'CUnits',
581 'method' => 'fetch',
582 'units' => 'size',
583 'dict' => 'DictionaryMeasuringUnits'
584 ),
585 'p.surface_units' => array(
586 'rule' => 'fetchscalefromcodeunits', // Switch this to fetchidfromcodeunits when we will store id instead of scale in product table
587 'classfile' => '/core/class/cunits.class.php',
588 'class' => 'CUnits',
589 'method' => 'fetch',
590 'units' => 'surface',
591 'dict' => 'DictionaryMeasuringUnits'
592 ),
593 'p.volume_units' => array(
594 'rule' => 'fetchscalefromcodeunits', // Switch this to fetchidfromcodeunits when we will store id instead of scale in product table
595 'classfile' => '/core/class/cunits.class.php',
596 'class' => 'CUnits',
597 'method' => 'fetch',
598 'units' => 'volume',
599 'dict' => 'DictionaryMeasuringUnits'
600 ),
601 'p.fk_country' => array(
602 'rule' => 'fetchidfromcodeid',
603 'classfile' => '/core/class/ccountry.class.php',
604 'class' => 'Ccountry',
605 'method' => 'fetch',
606 'dict' => 'DictionaryCountry'
607 ),
608 'p.finished' => array(
609 'rule' => 'fetchidfromcodeorlabel',
610 'classfile' => '/core/class/cproductnature.class.php',
611 'class' => 'CProductNature',
612 'method' => 'fetch',
613 'dict' => 'DictionaryProductNature'
614 ),
615 'p.accountancy_code_sell' => array('rule' => 'accountingaccount'),
616 'p.accountancy_code_sell_intra' => array('rule' => 'accountingaccount'),
617 'p.accountancy_code_sell_export' => array('rule' => 'accountingaccount'),
618 'p.accountancy_code_buy' => array('rule' => 'accountingaccount'),
619 'p.accountancy_code_buy_intra' => array('rule' => 'accountingaccount'),
620 'p.accountancy_code_buy_export' => array('rule' => 'accountingaccount'),
621 );
622
623 $this->import_regex_array[$r] = array(
624 'p.ref' => '[^ ]',
625 'p.price_base_type' => '\AHT\z|\ATTC\z',
626 'p.tosell' => '^[0|1]$',
627 'p.tobuy' => '^[0|1]$',
628 'p.fk_product_type' => '^[0|1]$',
629 'p.datec' => '^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]$',
630 'p.recuperableonly' => '^[0|1]$',
631 );
632
633 // Complete if Stock module enabled
634 if (isModEnabled('stock')) {
635 $this->import_fields_array[$r] = array_merge($this->import_fields_array[$r], array(
636 'p.fk_default_warehouse' => 'DefaultWarehouse',
637 'p.tobatch' => 'ManageLotSerial',
638 'p.seuil_stock_alerte' => 'StockLimit', //lower limit for warning
639 'p.pmp' => 'PMPValue', //weighted average price
640 'p.desiredstock' => 'DesiredStock'//desired stock for replenishment feature
641 ));
642
643 $this->import_regex_array[$r] = array_merge($this->import_regex_array[$r], array(
644 'p.tobatch' => '^[0|1|2]$'
645 ));
646
647 $this->import_convertvalue_array[$r] = array_merge($this->import_convertvalue_array[$r], array(
648 'p.fk_default_warehouse' => array(
649 'rule' => 'fetchidfromref',
650 'classfile' => '/product/stock/class/entrepot.class.php',
651 'class' => 'Entrepot',
652 'method' => 'fetch',
653 'element' => 'Warehouse'
654 )
655 ));
656 }
657
658 if (getDolGlobalString('PRODUCT_USE_CUSTOMER_PACKAGING')) {
659 $this->import_fields_array[$r] = array_merge($this->import_fields_array[$r], array(
660 'p.packaging' => 'PackagingForThisProductSell',
661 ));
662 }
663
664 if (isModEnabled("supplier_order") || isModEnabled("supplier_invoice") || isModEnabled('margin')) {
665 $this->import_fields_array[$r] = array_merge($this->import_fields_array[$r], array('p.cost_price' => 'CostPrice'));
666 }
667 if (is_object($mysoc) && $usenpr) {
668 $this->import_fields_array[$r] = array_merge($this->import_fields_array[$r], array('p.recuperableonly' => 'NPR'));
669 }
670 if (is_object($mysoc) && $mysoc->useLocalTax(1)) {
671 $this->import_fields_array[$r] = array_merge($this->import_fields_array[$r], array('p.localtax1_tx' => 'LT1', 'p.localtax1_type' => 'LT1Type'));
672 }
673 if (is_object($mysoc) && $mysoc->useLocalTax(2)) {
674 $this->import_fields_array[$r] = array_merge($this->import_fields_array[$r], array('p.localtax2_tx' => 'LT2', 'p.localtax2_type' => 'LT2Type'));
675 }
676 if (isModEnabled('barcode')) {
677 $this->import_fields_array[$r] = array_merge($this->import_fields_array[$r], array('p.barcode' => 'BarCode'));
678 }
679 if (getDolGlobalInt('PRODUCT_USE_UNITS')) {
680 $this->import_fields_array[$r]['p.fk_unit'] = 'Unit';
681 }
682
683 // Add extra fields
684 $import_extrafield_sample = array();
685 $sql = "SELECT name, label, fieldrequired FROM ".MAIN_DB_PREFIX."extrafields WHERE type <> 'separate' AND elementtype = 'product' AND entity IN (0, ".$conf->entity.")";
686 $resql = $this->db->query($sql);
687 if ($resql) { // This can fail when class is used on old database (during migration for example)
688 while ($obj = $this->db->fetch_object($resql)) {
689 $fieldname = 'extra.'.$obj->name;
690 $fieldlabel = ucfirst($obj->label);
691 $this->import_fields_array[$r][$fieldname] = $fieldlabel.($obj->fieldrequired ? '*' : '');
692 $import_extrafield_sample[$fieldname] = $fieldlabel;
693 }
694 }
695 // End add extra fields
696 $this->import_fieldshidden_array[$r] = array('extra.fk_object' => 'lastrowid-'.MAIN_DB_PREFIX.'product'); // aliastable.field => ('user->id' or 'lastrowid-'.tableparent)
697
698 // field order as per structure of table llx_product
699 $import_sample = array(
700 'p.ref' => "ref:PREF123456",
701 'p.datec' => dol_print_date(dol_now(), '%Y-%m-%d'),
702 'p.label' => "Product name in default language",
703 'p.description' => "Product description in default language",
704 'p.note_public' => "a public note (free text)",
705 'p.note' => "a private note (free text)",
706 'p.customcode' => 'customs code',
707 'p.fk_country' => 'FR',
708 'p.price' => "100",
709 'p.price_min' => "100",
710 'p.price_ttc' => "110",
711 'p.price_min_ttc' => "110",
712 'p.price_base_type' => "HT (show/use price excl. tax) / TTC (show/use price incl. tax)",
713 'p.tva_tx' => '10', // tax rate eg: 10. Must match numerically one of the tax rates defined for your country
714 'p.default_vat_code' => '', // to use the correct vat line when there is several lines with the same vat rate for your country
715 'p.tosell' => "0 (not for sale to customer, eg. raw material) / 1 (for sale)",
716 'p.tobuy' => "0 (not for purchase from supplier, eg. virtual product) / 1 (for purchase)",
717 'p.fk_product_type' => "0 (product) / 1 (service)",
718 'p.duration' => "eg. 365d/12m/1y",
719 'p.url' => 'link to product (no https)',
720 'p.accountancy_code_sell' => "",
721 'p.accountancy_code_sell_intra' => "",
722 'p.accountancy_code_sell_export' => "",
723 'p.accountancy_code_buy' => "",
724 'p.accountancy_code_buy_intra' => "",
725 'p.accountancy_code_buy_export' => "",
726 'p.weight' => "",
727 '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',
728 'p.length' => "",
729 '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',
730 'p.width' => "",
731 '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',
732 'p.height' => "",
733 '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',
734 'p.surface' => "",
735 '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',
736 'p.volume' => "",
737 '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',
738 'p.finished' => '0 (raw material) / 1 (finished goods), matches field "code" in dictionary table "'.MAIN_DB_PREFIX.'c_product_nature"'
739 );
740 //clauses copied from import_fields_array
741 if (isModEnabled('stock')) {
742 $import_sample = array_merge($import_sample, array(
743 'p.tobatch' => "0 (don't use) / 1 (use batch) / 2 (use serial number)",
744 'p.seuil_stock_alerte' => '',
745 'p.pmp' => '0',
746 'p.desiredstock' => ''
747 ));
748 }
749 if (isModEnabled("supplier_order") || isModEnabled("supplier_invoice") || isModEnabled('margin')) {
750 $import_sample = array_merge($import_sample, array('p.cost_price' => '90'));
751 }
752 if (is_object($mysoc) && $usenpr) {
753 $import_sample = array_merge($import_sample, array('p.recuperableonly' => '0'));
754 }
755 if (is_object($mysoc) && $mysoc->useLocalTax(1)) {
756 $import_sample = array_merge($import_sample, array('p.localtax1_tx' => '', 'p.localtax1_type' => ''));
757 }
758 if (is_object($mysoc) && $mysoc->useLocalTax(2)) {
759 $import_sample = array_merge($import_sample, array('p.localtax2_tx' => '', 'p.localtax2_type' => ''));
760 }
761 if (isModEnabled('barcode')) {
762 $import_sample = array_merge($import_sample, array('p.barcode' => ''));
763 }
764 if (getDolGlobalInt('PRODUCT_USE_UNITS')) {
765 $import_sample = array_merge(
766 $import_sample,
767 array(
768 '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"'
769 )
770 );
771
772 $this->import_convertvalue_array[$r] = array_merge($this->import_convertvalue_array[$r], array(
773 'p.fk_unit' => array(
774 'rule' => 'fetchidfromcodeorlabel',
775 'classfile' => '/core/class/cunits.class.php',
776 'class' => 'CUnits',
777 'method' => 'fetch',
778 'dict' => 'DictionaryUnits'
779 )
780 ));
781 }
782
783 if (getDolGlobalString('PRODUCT_USE_CUSTOMER_PACKAGING')) {
784 $import_sample = array_merge($import_sample, array(
785 'p.packaging' => "2",
786 ));
787 }
788
789 $this->import_examplevalues_array[$r] = array_merge($import_sample, $import_extrafield_sample);
790 $this->import_updatekeys_array[$r] = array('p.ref' => 'Ref');
791 if (isModEnabled('barcode')) {
792 $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
793 }
794
795 if (getDolGlobalString('STOCK_ALLOW_ADD_LIMIT_STOCK_BY_WAREHOUSE')) {
796 // Import products limit and desired stock by product and warehouse
797 $r++;
798 $this->import_code[$r] = $this->rights_class.'_stock_by_warehouse';
799 $this->import_label[$r] = "ProductStockWarehouse"; // Translation key
800 $this->import_icon[$r] = $this->picto;
801 $this->import_entities_array[$r] = array(); // We define here only fields that use another icon that the one defined into import_icon
802 $this->import_tables_array[$r] = array('pwp' => MAIN_DB_PREFIX.'product_warehouse_properties');
803 $this->import_fields_array[$r] = array('pwp.fk_product' => "Product*",
804 'pwp.fk_entrepot' => "Warehouse*", 'pwp.seuil_stock_alerte' => "StockLimit",
805 'pwp.desiredstock' => "DesiredStock");
806 $this->import_regex_array[$r] = array(
807 'pwp.fk_product' => 'rowid@'.MAIN_DB_PREFIX.'product',
808 'pwp.fk_entrepot' => 'rowid@'.MAIN_DB_PREFIX.'entrepot',
809 );
810 $this->import_convertvalue_array[$r] = array(
811 'pwp.fk_product' => array('rule' => 'fetchidfromref', 'classfile' => '/product/class/product.class.php', 'class' => 'Product', 'method' => 'fetch', 'element' => 'Product')
812 ,'pwp.fk_entrepot' => array('rule' => 'fetchidfromref', 'classfile' => '/product/stock/class/entrepot.class.php', 'class' => 'Entrepot', 'method' => 'fetch', 'element' => 'Entrepot')
813 );
814 $this->import_examplevalues_array[$r] = array('pwp.fk_product' => "ref:PRODUCT_REF or id:123456",
815 'pwp.fk_entrepot' => "ref:WAREHOUSE_REF or id:123456",
816 'pwp.seuil_stock_alerte' => "100",
817 'pwp.desiredstock' => "110"
818 );
819 $this->import_updatekeys_array[$r] = array('pwp.fk_product' => 'Product', 'pwp.fk_entrepot' => 'Warehouse');
820 }
821
822 if (isModEnabled("supplier_order") || isModEnabled("supplier_invoice")) {
823 // Import suppliers prices (note: this code is duplicated in module Service)
824 $r++;
825 $this->import_code[$r] = $this->rights_class.'_supplierprices';
826 $this->import_label[$r] = "SuppliersPricesOfProductsOrServices"; // Translation key
827 $this->import_icon[$r] = $this->picto;
828 $this->import_entities_array[$r] = array(); // We define here only fields that use another icon that the one defined into import_icon
829 $this->import_tables_array[$r] = array('sp' => MAIN_DB_PREFIX.'product_fournisseur_price', 'extra' => MAIN_DB_PREFIX.'product_fournisseur_price_extrafields');
830 $this->import_tables_creator_array[$r] = array('sp' => 'fk_user');
831 $this->import_fields_array[$r] = array(//field order as per structure of table llx_product_fournisseur_price, without optional fields
832 'sp.fk_product' => "ProductOrService*",
833 'sp.fk_soc' => "Supplier*",
834 'sp.ref_fourn' => 'SupplierRef*',
835 'sp.quantity' => "QtyMin*",
836 'sp.tva_tx' => 'VATRate',
837 'sp.default_vat_code' => 'VATCode',
838 'sp.delivery_time_days' => 'NbDaysToDelivery',
839 'sp.supplier_reputation' => 'SupplierReputation',
840 'sp.status' => 'Status',
841 'sp.datec' => 'DateCreation'
842 );
843 if (is_object($mysoc) && $usenpr) {
844 $this->import_fields_array[$r] = array_merge($this->import_fields_array[$r], array('sp.recuperableonly' => 'VATNPR'));
845 }
846 if (is_object($mysoc) && $mysoc->useLocalTax(1)) {
847 $this->import_fields_array[$r] = array_merge($this->import_fields_array[$r], array('sp.localtax1_tx' => 'LT1', 'sp.localtax1_type' => 'LT1Type'));
848 }
849 if (is_object($mysoc) && $mysoc->useLocalTax(2)) {
850 $this->import_fields_array[$r] = array_merge($this->import_fields_array[$r], array('sp.localtax2_tx' => 'LT2', 'sp.localtax2_type' => 'LT2Type'));
851 }
852 $this->import_fields_array[$r] = array_merge($this->import_fields_array[$r], array(
853 'sp.price' => "PriceQtyMinHT*",
854 'sp.unitprice' => 'UnitPriceHT*', // TODO Make this field not required and calculate it from price and qty
855 'sp.remise_percent' => 'DiscountQtyMin'
856 ));
857
858 if (isModEnabled("multicurrency")) {
859 $this->import_fields_array[$r] = array_merge($this->import_fields_array[$r], array(
860 'sp.fk_multicurrency' => 'CurrencyCodeId', //ideally this should be automatically obtained from the CurrencyCode on the next line
861 'sp.multicurrency_code' => 'CurrencyCode',
862 'sp.multicurrency_tx' => 'CurrencyRate',
863 'sp.multicurrency_unitprice' => 'CurrencyUnitPrice',
864 'sp.multicurrency_price' => 'CurrencyPrice',
865 ));
866 }
867
868 if (getDolGlobalString('PRODUCT_USE_SUPPLIER_PACKAGING')) {
869 $this->import_fields_array[$r] = array_merge($this->import_fields_array[$r], array('sp.packaging' => 'PackagingForThisProduct'));
870 }
871
872 // Add extra fields
873 $import_extrafield_sample = array();
874 $sql = "SELECT name, label, fieldrequired FROM ".MAIN_DB_PREFIX."extrafields WHERE type <> 'separate' AND elementtype = 'product_fournisseur_price' AND entity IN (0, ".$conf->entity.")";
875 $resql = $this->db->query($sql);
876 if ($resql) { // This can fail when class is used on old database (during migration for example)
877 while ($obj = $this->db->fetch_object($resql)) {
878 $fieldname = 'extra.'.$obj->name;
879 $fieldlabel = ucfirst($obj->label);
880 $this->import_fields_array[$r][$fieldname] = $fieldlabel.($obj->fieldrequired ? '*' : '');
881 $import_extrafield_sample[$fieldname] = $fieldlabel;
882 }
883 }
884 // End add extra fields
885
886 // Add some field automatically (if they are not yet provided explicitly)
887 $this->import_fieldshidden_array[$r] = array(
888 'sp.datec' => 'const-'.dol_print_date(dol_now(), 'standard'),
889 'extra.fk_object' => 'lastrowid-'.MAIN_DB_PREFIX.'product_fournisseur_price'
890 ); // aliastable.field => ('user->id' or 'lastrowid-'.tableparent or 'const-xxxx')
891
892 $this->import_convertvalue_array[$r] = array(
893 'sp.fk_soc' => array('rule' => 'fetchidfromref', 'classfile' => '/societe/class/societe.class.php', 'class' => 'Societe', 'method' => 'fetch', 'element' => 'ThirdParty'),
894 'sp.fk_product' => array('rule' => 'fetchidfromref', 'classfile' => '/product/class/product.class.php', 'class' => 'Product', 'method' => 'fetch', 'element' => 'Product')
895 );
896
897 $this->import_regex_array[$r] = array(
898 '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]$'
899 );
900
901 $this->import_examplevalues_array[$r] = array(
902 'sp.fk_product' => "ref:PRODUCT_REF or id:123456",
903 'sp.fk_soc' => "My Supplier",
904 'sp.ref_fourn' => "XYZ-F123456",
905 'sp.quantity' => "5",
906 'sp.tva_tx' => '10',
907 'sp.default_vat_code' => '',
908 'sp.price' => "50",
909 'sp.unitprice' => '50',
910 'sp.remise_percent' => '0',
911 'sp.delivery_time_days' => '5',
912 'sp.supplier_reputation' => 'FAVORITE / NOTTHGOOD / DONOTORDER',
913 'sp.status' => '1',
914 'sp.datec' => dol_print_date(dol_now(), '%Y-%m-%d %H:%M:%S'),
915 );
916 if (is_object($mysoc) && $usenpr) {
917 $this->import_examplevalues_array[$r] = array_merge($this->import_examplevalues_array[$r], array('sp.recuperableonly' => ''));
918 }
919 if (is_object($mysoc) && $mysoc->useLocalTax(1)) {
920 $this->import_examplevalues_array[$r] = array_merge($this->import_examplevalues_array[$r], array('sp.localtax1_tx' => 'LT1', 'sp.localtax1_type' => 'LT1Type'));
921 }
922 if (is_object($mysoc) && $mysoc->useLocalTax(2)) {
923 $this->import_examplevalues_array[$r] = array_merge($this->import_examplevalues_array[$r], array('sp.localtax2_tx' => 'LT2', 'sp.localtax2_type' => 'LT2Type'));
924 }
925 $this->import_examplevalues_array[$r] = array_merge($this->import_examplevalues_array[$r], array(
926 'sp.price' => "50.00",
927 'sp.unitprice' => '10',
928 // TODO Make this field not required and calculate it from price and qty
929 'sp.remise_percent' => '20'
930 ));
931 if (isModEnabled("multicurrency")) {
932 $this->import_examplevalues_array[$r] = array_merge($this->import_examplevalues_array[$r], array(
933 'sp.fk_multicurrency' => 'eg: 2 = the rowid for code of multicurrency currency',
934 'sp.multicurrency_code' => 'GBP',
935 'sp.multicurrency_tx' => '1.12345',
936 'sp.multicurrency_unitprice' => '',
937 // TODO Make this field not required and calculate it from price and qty
938 'sp.multicurrency_price' => ''
939 ));
940 }
941 if (getDolGlobalString('PRODUCT_USE_SUPPLIER_PACKAGING')) {
942 $this->import_examplevalues_array[$r] = array_merge($this->import_examplevalues_array[$r], array(
943 'sp.packaging' => '10',
944 ));
945 }
946
947 $this->import_updatekeys_array[$r] = array('sp.fk_product' => 'ProductOrService', 'sp.ref_fourn' => 'SupplierRef', 'sp.fk_soc' => 'Supplier', 'sp.quantity' => "QtyMin");
948 }
949
950 if (getDolGlobalString('PRODUIT_MULTIPRICES') || getDolGlobalString('PRODUIT_CUSTOMER_PRICES_AND_MULTIPRICES')) {
951 // Import products multiprices
952 $r++;
953 $this->import_code[$r] = $this->rights_class.'_multiprice';
954 $this->import_label[$r] = "ProductsOrServiceMultiPrice"; // Translation key
955 $this->import_icon[$r] = $this->picto;
956 $this->import_entities_array[$r] = array(); // We define here only fields that use another icon that the one defined into import_icon
957 $this->import_tables_array[$r] = array('pr' => MAIN_DB_PREFIX.'product_price', 'extra' => MAIN_DB_PREFIX.'product_price_extrafields');
958 $this->import_tables_creator_array[$r] = array('pr' => 'fk_user_author'); // Fields to store import user id
959 $this->import_fields_array[$r] = array('pr.fk_product' => "ProductOrService*",
960 'pr.price_base_type' => "PriceBase", 'pr.price_level' => "PriceLevel",
961 'pr.price' => "PriceLevelUnitPriceHT", 'pr.price_ttc' => "PriceLevelUnitPriceTTC",
962 'pr.price_min' => "MinPriceLevelUnitPriceHT", 'pr.price_min_ttc' => "MinPriceLevelUnitPriceTTC",
963 'pr.date_price' => 'DateCreation*');
964 if (getDolGlobalString('PRODUIT_MULTIPRICES_USE_VAT_PER_LEVEL')) {
965 $this->import_fields_array[$r]['pr.tva_tx'] = 'VATRate';
966 }
967 if (is_object($mysoc) && $usenpr) {
968 $this->import_fields_array[$r] = array_merge($this->import_fields_array[$r], array('pr.recuperableonly' => 'NPR'));
969 }
970
971 // Add extra fields
972 $import_extrafield_sample = array();
973 $sql = "SELECT name, label, fieldrequired FROM ".MAIN_DB_PREFIX."extrafields WHERE type <> 'separate' AND elementtype = 'product_price' AND entity IN (0, ".$conf->entity.")";
974 $resql = $this->db->query($sql);
975 if ($resql) { // This can fail when class is used on old database (during migration for example)
976 while ($obj = $this->db->fetch_object($resql)) {
977 $fieldname = 'extra.'.$obj->name;
978 $fieldlabel = ucfirst($obj->label);
979 $this->import_fields_array[$r][$fieldname] = $fieldlabel.($obj->fieldrequired ? '*' : '');
980 $import_extrafield_sample[$fieldname] = $fieldlabel;
981 }
982 }
983 // End add extra fields
984 $this->import_fieldshidden_array[$r] = array('extra.fk_object' => 'lastrowid-'.MAIN_DB_PREFIX.'product_price'); // aliastable.field => ('user->id' or 'lastrowid-'.tableparent)
985
986 $this->import_regex_array[$r] = array(
987 'pr.datec' => '^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]$',
988 'pr.recuperableonly' => '^[0|1]$'
989 );
990
991 $this->import_convertvalue_array[$r] = array(
992 'pr.fk_product' => array('rule' => 'fetchidfromref', 'classfile' => '/product/class/product.class.php', 'class' => 'Product', 'method' => 'fetch', 'element' => 'Product')
993 );
994 $this->import_examplevalues_array[$r] = array('pr.fk_product' => "ref:PRODUCT_REF or id:123456",
995 'pr.price_base_type' => "HT (for excl tax) or TTC (for inc tax)", 'pr.price_level' => "1",
996 'pr.price' => "100", 'pr.price_ttc' => "110",
997 'pr.price_min' => "100", 'pr.price_min_ttc' => "110",
998 'pr.tva_tx' => '20',
999 'pr.recuperableonly' => '0',
1000 'pr.date_price' => '2020-12-31');
1001 }
1002
1003 if (getDolGlobalInt('MAIN_MULTILANGS')) {
1004 // Import translations of product names and descriptions
1005 $r++;
1006 $this->import_code[$r] = $this->rights_class.'_languages';
1007 $this->import_label[$r] = "ProductsOrServicesTranslations";
1008 $this->import_icon[$r] = $this->picto;
1009 $this->import_entities_array[$r] = array(); // We define here only fields that use another icon that the one defined into import_icon
1010 $this->import_tables_array[$r] = array('l' => MAIN_DB_PREFIX.'product_lang');
1011 // multiline translation, one line per translation
1012 $this->import_fields_array[$r] = array('l.fk_product' => 'ProductOrService*', 'l.lang' => 'Language*', 'l.label' => 'TranslatedLabel', 'l.description' => 'TranslatedDescription');
1013 //$this->import_fields_array[$r]['l.note']='TranslatedNote';
1014 $this->import_convertvalue_array[$r] = array(
1015 'l.fk_product' => array('rule' => 'fetchidfromref', 'classfile' => '/product/class/product.class.php', 'class' => 'Product', 'method' => 'fetch', 'element' => 'Product')
1016 );
1017 $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');
1018 $this->import_updatekeys_array[$r] = array('l.fk_product' => 'ProductOrService', 'l.lang' => 'Language');
1019 }
1020
1021 if (getDolGlobalInt('PRODUIT_SOUSPRODUITS')) {
1022 // Import products kit
1023 $r++;
1024 $this->import_code[$r] = $this->rights_class . '_' . $r;
1025 $this->import_label[$r] = "AssociatedProducts"; // Translation key
1026 $this->import_icon[$r] = $this->picto;
1027 $this->import_entities_array[$r] = array(); // We define here only fields that use another icon that the one defined into import_icon
1028 $this->import_tables_array[$r] = array('pa' => MAIN_DB_PREFIX . 'product_association');
1029 $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');
1030
1031 $this->import_convertvalue_array[$r] = array(
1032 'pa.fk_product_pere' => array('rule' => 'fetchidfromref', 'classfile' => '/product/class/product.class.php', 'class' => 'Product', 'method' => 'fetch', 'element' => 'Product'),
1033 'pa.fk_product_fils' => array('rule' => 'fetchidfromref', 'classfile' => '/product/class/product.class.php', 'class' => 'Product', 'method' => 'fetch', 'element' => 'Product')
1034 );
1035 $this->import_examplevalues_array[$r] = array(
1036 'pa.fk_product_pere' => "ref:PREF123456",
1037 'pa.fk_product_fils' => "ref:PREF123456",
1038 'pa.qty' => "100",
1039 'pa.incdec' => "0",
1040 'pa.rang' => "1");
1041 $this->import_regex_array[$r] = array('pa.fk_product_pere' => 'rowid@'.MAIN_DB_PREFIX.'product', 'pa.fk_product_fils' => 'rowid@'.MAIN_DB_PREFIX.'product');
1042 $this->import_updatekeys_array[$r] = array('pa.fk_product_pere' => 'ref parent', 'pa.fk_product_fils' => "ref enfant");
1043 }
1044 }
1045
1046
1055 public function init($options = '')
1056 {
1057 $this->remove($options);
1058
1059 $sql = array();
1060
1061 return $this->_init($sql, $options);
1062 }
1063}
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