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