modProduct.class.php 51 KB

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