modProduct.class.php 51 KB

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