Passed
Push — main ( 4b1ce6...7d2b49 )
by Rafael
80:06
created

Product::__construct()   F

Complexity

Conditions 111

Size

Total Lines 924
Code Lines 692

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 111
eloc 692
nop 1
dl 0
loc 924
rs 3.3333
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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