modProduct.class.php 52 KB

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