modProduct.class.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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@capnetworks.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. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 3 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. /**
  25. * \defgroup produit Module products
  26. * \brief Module to manage catalog of predefined products
  27. * \file htdocs/core/modules/modProduct.class.php
  28. * \ingroup produit
  29. * \brief File to describe module to manage catalog of predefined products
  30. */
  31. include_once DOL_DOCUMENT_ROOT .'/core/modules/DolibarrModules.class.php';
  32. /**
  33. * Class descriptor of Product module
  34. */
  35. class modProduct extends DolibarrModules
  36. {
  37. /**
  38. * Constructor. Define names, constants, directories, boxes, permissions
  39. *
  40. * @param DoliDB $db Database handler
  41. */
  42. function __construct($db)
  43. {
  44. global $conf, $mysoc;
  45. $this->db = $db;
  46. $this->numero = 50;
  47. $this->family = "products";
  48. $this->module_position = 20;
  49. // Module label (no space allowed), used if translation string 'ModuleXXXName' not found (where XXX is value of numeric property 'numero' of module)
  50. $this->name = preg_replace('/^mod/i','',get_class($this));
  51. $this->description = "Product management";
  52. // Possible values for version are: 'development', 'experimental', 'dolibarr' or version
  53. $this->version = 'dolibarr';
  54. $this->const_name = 'MAIN_MODULE_'.strtoupper($this->name);
  55. $this->picto='product';
  56. // Data directories to create when module is enabled
  57. $this->dirs = array("/product/temp");
  58. // Dependencies
  59. $this->hidden = false; // A condition to hide module
  60. $this->depends = array(); // List of module class names as string that must be enabled if this module is enabled
  61. $this->requiredby = array("modStock","modBarcode","modProductBatch"); // List of module ids to disable if this one is disabled
  62. $this->conflictwith = array(); // List of module class names as string this module is in conflict with
  63. $this->phpmin = array(5,4); // Minimum version of PHP required by module
  64. // Config pages
  65. $this->config_page_url = array("product.php@product");
  66. $this->langfiles = array("products","companies","stocks","bills");
  67. // Constants
  68. $this->const = array();
  69. $r=0;
  70. $this->const[$r][0] = "PRODUCT_CODEPRODUCT_ADDON";
  71. $this->const[$r][1] = "chaine";
  72. $this->const[$r][2] = "mod_codeproduct_leopard";
  73. $this->const[$r][3] = 'Module to control product codes';
  74. $this->const[$r][4] = 0;
  75. $r++;
  76. /*$this->const[$r][0] = "PRODUCT_ADDON_PDF";
  77. $this->const[$r][1] = "chaine";
  78. $this->const[$r][2] = "standard";
  79. $this->const[$r][3] = 'Default module for document generation';
  80. $this->const[$r][4] = 0;
  81. $r++;*/
  82. // Boxes
  83. $this->boxes = array(
  84. 0=>array('file'=>'box_produits.php','enabledbydefaulton'=>'Home'),
  85. 1=>array('file'=>'box_produits_alerte_stock.php','enabledbydefaulton'=>''),
  86. 2=>array('file'=>'box_graph_product_distribution.php','enabledbydefaulton'=>'Home')
  87. );
  88. // Permissions
  89. $this->rights = array();
  90. $this->rights_class = 'produit';
  91. $r=0;
  92. $this->rights[$r][0] = 31; // id de la permission
  93. $this->rights[$r][1] = 'Read products'; // libelle de la permission
  94. $this->rights[$r][2] = 'r'; // type de la permission (deprecie a ce jour)
  95. $this->rights[$r][3] = 0; // La permission est-elle une permission par defaut
  96. $this->rights[$r][4] = 'lire';
  97. $r++;
  98. $this->rights[$r][0] = 32; // id de la permission
  99. $this->rights[$r][1] = 'Create/modify products'; // libelle de la permission
  100. $this->rights[$r][2] = 'w'; // type de la permission (deprecie a ce jour)
  101. $this->rights[$r][3] = 0; // La permission est-elle une permission par defaut
  102. $this->rights[$r][4] = 'creer';
  103. $r++;
  104. $this->rights[$r][0] = 34; // id de la permission
  105. $this->rights[$r][1] = 'Delete products'; // libelle de la permission
  106. $this->rights[$r][2] = 'd'; // type de la permission (deprecie a ce jour)
  107. $this->rights[$r][3] = 0; // La permission est-elle une permission par defaut
  108. $this->rights[$r][4] = 'supprimer';
  109. $r++;
  110. $this->rights[$r][0] = 38; // Must be same permission than in service module
  111. $this->rights[$r][1] = 'Export products';
  112. $this->rights[$r][2] = 'r';
  113. $this->rights[$r][3] = 0;
  114. $this->rights[$r][4] = 'export';
  115. $r++;
  116. $this->rights[$r][0] = 39;
  117. $this->rights[$r][1] = 'Ignore minimum price';
  118. $this->rights[$r][2] = 'r';
  119. $this->rights[$r][3] = 0;
  120. $this->rights[$r][4] = 'ignore_price_min_advance';
  121. $r++;
  122. // Menus
  123. //-------
  124. $this->menu = 1; // This module add menu entries. They are coded into menu manager.
  125. /* We can't enable this here because it must be enabled in both product and service module and this create duplicate insert
  126. $r=0;
  127. $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
  128. 'type'=>'left', // This is a Left menu entry
  129. 'titre'=>'ProductVatMassChange',
  130. 'url'=>'/product/admin/product_tools.php?mainmenu=home&leftmenu=admintools',
  131. 'langs'=>'products', // Lang file to use (without .lang) by module. File must be in langs/code_CODE/ directory.
  132. 'position'=>300,
  133. '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.
  134. 'perms'=>'1', // Use 'perms'=>'$user->rights->mymodule->level1->level2' if you want your menu with a permission rules
  135. 'target'=>'',
  136. 'user'=>0); // 0=Menu for internal users, 1=external users, 2=both
  137. $r++;
  138. */
  139. // Exports
  140. //--------
  141. $r=0;
  142. $r++;
  143. $this->export_code[$r]=$this->rights_class.'_'.$r;
  144. $this->export_label[$r]="Products"; // Translation key (used only if key ExportDataset_xxx_z not found)
  145. $this->export_permission[$r]=array(array("produit","export"));
  146. $this->export_fields_array[$r]=array(
  147. 'p.rowid'=>"Id",'p.ref'=>"Ref",'p.label'=>"Label",'p.description'=>"Description",'p.url'=>"PublicUrl",'p.accountancy_code_sell'=>"ProductAccountancySellCode",
  148. 'p.accountancy_code_buy'=>"ProductAccountancyBuyCode",'p.note'=>"Note",'p.length'=>"Length",'p.width'=>"Width",'p.height'=>"Height",'p.surface'=>"Surface",
  149. 'p.volume'=>"Volume",'p.weight'=>"Weight",'p.customcode'=>'CustomCode','p.price_base_type'=>"PriceBase",'p.price'=>"UnitPriceHT",'p.price_ttc'=>"UnitPriceTTC",
  150. 'p.tva_tx'=>'VATRate','p.tosell'=>"OnSell",'p.tobuy'=>"OnBuy",'p.datec'=>'DateCreation','p.tms'=>'DateModification'
  151. );
  152. if (is_object($mysoc) && $mysoc->useNPR()) $this->export_fields_array[$r]['p.recuperableonly']='NPR';
  153. if (! empty($conf->stock->enabled)) $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'));
  154. if (! empty($conf->barcode->enabled)) $this->export_fields_array[$r]=array_merge($this->export_fields_array[$r],array('p.barcode'=>'BarCode'));
  155. if (! empty($conf->fournisseur->enabled) || !empty($conf->margin->enabled)) $this->export_fields_array[$r]=array_merge($this->export_fields_array[$r],array('p.cost_price'=>'CostPrice'));
  156. $keyforselect='product'; $keyforelement='product'; $keyforaliasextra='extra';
  157. include DOL_DOCUMENT_ROOT.'/core/extrafieldsinexport.inc.php';
  158. if (! empty($conf->fournisseur->enabled)) $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'));
  159. if (! empty($conf->global->EXPORTTOOL_CATEGORIES)) $this->export_fields_array[$r]=array_merge($this->export_fields_array[$r],array('group_concat(cat.label)'=>'Categories'));
  160. if (! empty($conf->global->MAIN_MULTILANGS)) $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'));
  161. if (! empty($conf->global->PRODUCT_USE_UNITS)) $this->export_fields_array[$r]['p.fk_unit'] = 'Unit';
  162. $this->export_TypeFields_array[$r]=array(
  163. 'p.ref'=>"Text",'p.label'=>"Text",'p.description'=>"Text",'p.url'=>"Text",'p.accountancy_code_sell'=>"Text",'p.accountancy_code_buy'=>"Text",
  164. 'p.note'=>"Text",'p.length'=>"Numeric",'p.width'=>"Numeric",'p.height'=>"Numeric",'p.surface'=>"Numeric",'p.volume'=>"Numeric",'p.weight'=>"Numeric",
  165. 'p.customcode'=>'Text','p.price_base_type'=>"Text",'p.price'=>"Numeric",'p.price_ttc'=>"Numeric",'p.tva_tx'=>'Numeric','p.tosell'=>"Boolean",
  166. 'p.tobuy'=>"Boolean",'p.datec'=>'Date','p.tms'=>'Date'
  167. );
  168. if (! empty($conf->stock->enabled)) $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'));
  169. if (! empty($conf->barcode->enabled)) $this->export_TypeFields_array[$r]=array_merge($this->export_TypeFields_array[$r],array('p.barcode'=>'Text'));
  170. if (! empty($conf->fournisseur->enabled)) $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'));
  171. if (! empty($conf->global->MAIN_MULTILANGS)) $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'));
  172. if (! empty($conf->global->EXPORTTOOL_CATEGORIES)) $this->export_TypeFields_array[$r]=array_merge($this->export_TypeFields_array[$r],array("group_concat(cat.label)"=>'Text'));
  173. $this->export_entities_array[$r]=array(); // We define here only fields that use another icon that the one defined into import_icon
  174. if (! empty($conf->global->EXPORTTOOL_CATEGORIES)) $this->export_entities_array[$r]=array_merge($this->export_entities_array[$r],array("group_concat(cat.label)"=>'category'));
  175. if (! empty($conf->stock->enabled)) $this->export_entities_array[$r]=array_merge($this->export_entities_array[$r],array('p.stock'=>'product','p.pmp'=>'product'));
  176. if (! empty($conf->barcode->enabled)) $this->export_entities_array[$r]=array_merge($this->export_entities_array[$r],array('p.barcode'=>'product'));
  177. if (! empty($conf->fournisseur->enabled)) $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'));
  178. if (! empty($conf->global->MAIN_MULTILANGS)) $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'));
  179. if (! empty($conf->global->EXPORTTOOL_CATEGORIES)) $this->export_dependencies_array[$r]=array('category'=>'p.rowid');
  180. $this->export_sql_start[$r]='SELECT DISTINCT ';
  181. $this->export_sql_end[$r] =' FROM '.MAIN_DB_PREFIX.'product as p';
  182. if (! empty($conf->global->EXPORTTOOL_CATEGORIES)) $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';
  183. if (! empty($conf->global->MAIN_MULTILANGS)) $this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'product_lang as l ON l.fk_product = p.rowid';
  184. $this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'product_extrafields as extra ON p.rowid = extra.fk_object';
  185. if (! empty($conf->fournisseur->enabled)) $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';
  186. $this->export_sql_end[$r] .=' WHERE p.fk_product_type = 0 AND p.entity IN ('.getEntity('product').')';
  187. if (! empty($conf->global->EXPORTTOOL_CATEGORIES)) $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"
  188. if (! empty($conf->global->PRODUIT_MULTIPRICES))
  189. {
  190. // Exports product multiprice
  191. $r++;
  192. $this->export_code[$r]=$this->rights_class.'_'.$r;
  193. $this->export_label[$r]="ProductsMultiPrice"; // Translation key (used only if key ExportDataset_xxx_z not found)
  194. $this->export_permission[$r]=array(array("produit","export"));
  195. $this->export_fields_array[$r]=array('p.rowid'=>"Id",'p.ref'=>"Ref",
  196. 'pr.price_base_type'=>"PriceLevelPriceBase",'pr.price_level'=>"PriceLevel",
  197. 'pr.price'=>"PriceLevelUnitPriceHT",'pr.price_ttc'=>"PriceLevelUnitPriceTTC",
  198. 'pr.price_min'=>"MinPriceLevelUnitPriceHT",'pr.price_min_ttc'=>"MinPriceLevelUnitPriceTTC",
  199. 'pr.tva_tx'=>'PriceLevelVATRate',
  200. 'pr.date_price'=>'DateCreation');
  201. if (is_object($mysoc) && $mysoc->useNPR()) $this->export_fields_array[$r]['pr.recuperableonly']='NPR';
  202. //$this->export_TypeFields_array[$r]=array(
  203. // 'p.ref'=>"Text",'p.label'=>"Text",'p.description'=>"Text",'p.url'=>"Text",'p.accountancy_code_sell'=>"Text",'p.accountancy_code_buy'=>"Text",
  204. // 'p.note'=>"Text",'p.length'=>"Numeric",'p.surface'=>"Numeric",'p.volume'=>"Numeric",'p.weight'=>"Numeric",'p.customcode'=>'Text',
  205. // 'p.price_base_type'=>"Text",'p.price'=>"Numeric",'p.price_ttc'=>"Numeric",'p.tva_tx'=>'Numeric','p.tosell'=>"Boolean",'p.tobuy'=>"Boolean",
  206. // 'p.datec'=>'Date','p.tms'=>'Date'
  207. //);
  208. $this->export_entities_array[$r]=array('p.rowid'=>"product",'p.ref'=>"product",
  209. 'pr.price_base_type'=>"product",'pr.price_level'=>"product",'pr.price'=>"product",
  210. 'pr.price_ttc'=>"product",
  211. 'pr.price_min'=>"product",'pr.price_min_ttc'=>"product",
  212. 'pr.tva_tx'=>'product',
  213. 'pr.recuperableonly'=>'product',
  214. 'pr.date_price'=>"product");
  215. $this->export_sql_start[$r]='SELECT DISTINCT ';
  216. $this->export_sql_end[$r] =' FROM '.MAIN_DB_PREFIX.'product as p';
  217. $this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'product_price as pr ON p.rowid = pr.fk_product';
  218. $this->export_sql_end[$r] .=' WHERE p.fk_product_type = 0 AND p.entity IN ('.getEntity('product').')';
  219. }
  220. if (! empty($conf->global->PRODUIT_SOUSPRODUITS))
  221. {
  222. // Exports virtual products
  223. $r++;
  224. $this->export_code[$r]=$this->rights_class.'_'.$r;
  225. $this->export_label[$r]="AssociatedProducts"; // Translation key (used only if key ExportDataset_xxx_z not found)
  226. $this->export_permission[$r]=array(array("produit","export"));
  227. $this->export_fields_array[$r]=array(
  228. 'p.rowid'=>"Id",'p.ref'=>"Ref",'p.label'=>"Label",'p.description'=>"Description",'p.url'=>"PublicUrl",
  229. 'p.accountancy_code_sell'=>"ProductAccountancySellCode",'p.accountancy_code_buy'=>"ProductAccountancyBuyCode",'p.note'=>"Note",
  230. 'p.length'=>"Length",'p.surface'=>"Surface",'p.volume'=>"Volume",'p.weight'=>"Weight",'p.customcode'=>'CustomCode',
  231. 'p.price_base_type'=>"PriceBase",'p.price'=>"UnitPriceHT",'p.price_ttc'=>"UnitPriceTTC",'p.tva_tx'=>'VATRate','p.tosell'=>"OnSell",
  232. 'p.tobuy'=>"OnBuy",'p.datec'=>'DateCreation','p.tms'=>'DateModification'
  233. );
  234. if (! empty($conf->stock->enabled)) $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'));
  235. if (! empty($conf->barcode->enabled)) $this->export_fields_array[$r]=array_merge($this->export_fields_array[$r],array('p.barcode'=>'BarCode'));
  236. $this->export_fields_array[$r]=array_merge($this->export_fields_array[$r],array('pa.qty'=>'Qty','pa.incdec'=>'ComposedProductIncDecStock'));
  237. $this->export_TypeFields_array[$r]=array(
  238. 'p.ref'=>"Text",'p.label'=>"Text",'p.description'=>"Text",'p.url'=>"Text",'p.accountancy_code_sell'=>"Text",'p.accountancy_code_buy'=>"Text",
  239. 'p.note'=>"Text",'p.length'=>"Numeric",'p.surface'=>"Numeric",'p.volume'=>"Numeric",'p.weight'=>"Numeric",'p.customcode'=>'Text',
  240. 'p.price_base_type'=>"Text",'p.price'=>"Numeric",'p.price_ttc'=>"Numeric",'p.tva_tx'=>'Numeric','p.tosell'=>"Boolean",'p.tobuy'=>"Boolean",
  241. 'p.datec'=>'Date','p.tms'=>'Date'
  242. );
  243. if (! empty($conf->stock->enabled)) $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'));
  244. if (! empty($conf->barcode->enabled)) $this->export_TypeFields_array[$r]=array_merge($this->export_TypeFields_array[$r],array('p.barcode'=>'Text'));
  245. $this->export_TypeFields_array[$r]=array_merge($this->export_TypeFields_array[$r],array('pa.qty'=>'Numeric'));
  246. $this->export_entities_array[$r]=array(
  247. 'p.rowid'=>"virtualproduct",'p.ref'=>"virtualproduct",'p.label'=>"virtualproduct",'p.description'=>"virtualproduct",'p.url'=>"virtualproduct",
  248. 'p.accountancy_code_sell'=>'virtualproduct','p.accountancy_code_buy'=>'virtualproduct','p.note'=>"virtualproduct",'p.length'=>"virtualproduct",
  249. 'p.surface'=>"virtualproduct",'p.volume'=>"virtualproduct",'p.weight'=>"virtualproduct",'p.customcode'=>'virtualproduct',
  250. 'p.price_base_type'=>"virtualproduct",'p.price'=>"virtualproduct",'p.price_ttc'=>"virtualproduct",'p.tva_tx'=>"virtualproduct",
  251. 'p.tosell'=>"virtualproduct",'p.tobuy'=>"virtualproduct",'p.datec'=>"virtualproduct",'p.tms'=>"virtualproduct"
  252. );
  253. if (! empty($conf->stock->enabled)) $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'));
  254. if (! empty($conf->barcode->enabled)) $this->export_entities_array[$r]=array_merge($this->export_entities_array[$r],array('p.barcode'=>'virtualproduct'));
  255. $this->export_entities_array[$r]=array_merge($this->export_entities_array[$r],array('pa.qty'=>"subproduct",'pa.incdec'=>'subproduct'));
  256. $keyforselect='product'; $keyforelement='product'; $keyforaliasextra='extra';
  257. include DOL_DOCUMENT_ROOT.'/core/extrafieldsinexport.inc.php';
  258. $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"));
  259. $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"));
  260. $this->export_sql_start[$r]='SELECT DISTINCT ';
  261. $this->export_sql_end[$r] =' FROM '.MAIN_DB_PREFIX.'product as p';
  262. $this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'product_extrafields as extra ON p.rowid = extra.fk_object,';
  263. $this->export_sql_end[$r] .=' '.MAIN_DB_PREFIX.'product_association as pa, '.MAIN_DB_PREFIX.'product as p2';
  264. $this->export_sql_end[$r] .=' WHERE p.fk_product_type = 0 AND p.entity IN ('.getEntity('product').')';
  265. $this->export_sql_end[$r] .=' AND p.rowid = pa.fk_product_pere AND p2.rowid = pa.fk_product_fils';
  266. }
  267. // Imports
  268. //--------
  269. $r=0;
  270. $r++;
  271. $this->import_code[$r]=$this->rights_class.'_'.$r;
  272. $this->import_label[$r]="Products"; // Translation key
  273. $this->import_icon[$r]=$this->picto;
  274. $this->import_entities_array[$r]=array(); // We define here only fields that use another icon that the one defined into import_icon
  275. $this->import_tables_array[$r]=array('p'=>MAIN_DB_PREFIX.'product','extra'=>MAIN_DB_PREFIX.'product_extrafields');
  276. $this->import_tables_creator_array[$r]=array('p'=>'fk_user_author'); // Fields to store import user id
  277. $this->import_fields_array[$r]=array(
  278. 'p.ref'=>"Ref*",'p.label'=>"Label*",'p.description'=>"Description",'p.url'=>"PublicUrl",'p.accountancy_code_sell'=>"ProductAccountancySellCode",
  279. 'p.accountancy_code_buy'=>"ProductAccountancyBuyCode",'p.note'=>"Note",'p.length'=>"Length",'p.surface'=>"Surface",'p.volume'=>"Volume",
  280. 'p.weight'=>"Weight",'p.duration'=>"Duration",'p.customcode'=>'CustomCode','p.price'=>"SellingPriceHT",'p.price_ttc'=>"SellingPriceTTC",
  281. 'p.tva_tx'=>'VATRate', 'p.tosell'=>"OnSell*",'p.tobuy'=>"OnBuy*",'p.fk_product_type'=>"Type*",'p.finished'=>'Nature','p.datec'=>'DateCreation'
  282. );
  283. if (! empty($conf->stock->enabled)) $this->import_fields_array[$r]=array_merge($this->import_fields_array[$r],array('p.seuil_stock_alerte'=>'StockLimit','p.desiredstock'=>'DesiredStock','p.pmp'=>'PMPValue'));
  284. if (! empty($conf->fournisseur->enabled) || !empty($conf->margin->enabled)) $this->import_fields_array[$r]=array_merge($this->import_fields_array[$r],array('p.cost_price'=>'CostPrice'));
  285. if (is_object($mysoc) && $mysoc->useNPR()) $this->import_fields_array[$r]=array_merge($this->import_fields_array[$r],array('p.recuperableonly'=>'NPR'));
  286. if (is_object($mysoc) && $mysoc->useLocalTax(1)) $this->import_fields_array[$r]=array_merge($this->import_fields_array[$r],array('p.localtax1_tx'=>'LT1', 'p.localtax1_type'=>'LT1Type'));
  287. if (is_object($mysoc) && $mysoc->useLocalTax(2)) $this->import_fields_array[$r]=array_merge($this->import_fields_array[$r],array('p.localtax2_tx'=>'LT2', 'p.localtax2_type'=>'LT2Type'));
  288. if (! empty($conf->barcode->enabled)) $this->import_fields_array[$r]=array_merge($this->import_fields_array[$r],array('p.barcode'=>'BarCode'));
  289. if (! empty($conf->global->PRODUCT_USE_UNITS)) $this->import_fields_array[$r]['p.fk_unit'] = 'Unit';
  290. // Add extra fields
  291. $import_extrafield_sample=array();
  292. $sql="SELECT name, label, fieldrequired FROM ".MAIN_DB_PREFIX."extrafields WHERE elementtype = 'product' AND entity IN (0, ".$conf->entity.')';
  293. $resql=$this->db->query($sql);
  294. if ($resql) // This can fail when class is used on old database (during migration for example)
  295. {
  296. while ($obj=$this->db->fetch_object($resql))
  297. {
  298. $fieldname='extra.'.$obj->name;
  299. $fieldlabel=ucfirst($obj->label);
  300. $this->import_fields_array[$r][$fieldname]=$fieldlabel.($obj->fieldrequired?'*':'');
  301. $import_extrafield_sample[$fieldname]=$fieldlabel;
  302. }
  303. }
  304. // End add extra fields
  305. $this->import_fieldshidden_array[$r]=array('extra.fk_object'=>'lastrowid-'.MAIN_DB_PREFIX.'product'); // aliastable.field => ('user->id' or 'lastrowid-'.tableparent)
  306. $this->import_regex_array[$r]=array('p.ref'=>'[^ ]','p.tosell'=>'^[0|1]$','p.tobuy'=>'^[0|1]$','p.fk_product_type'=>'^[0|1]$','p.datec'=>'^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]$','p.recuperableonly'=>'^[0|1]$');
  307. $import_sample=array('p.ref'=>"PREF123456",'p.label'=>"My product",'p.description'=>"This is a description example for record",'p.note'=>"Some note",'p.price'=>"100",'p.price_ttc'=>"110",'p.tva_tx'=>'10','p.tosell'=>"0 or 1",'p.tobuy'=>"0 or 1",'p.fk_product_type'=>"0 for product/1 for service",'p.finished'=>'','p.duration'=>"1y",'p.datec'=>'2008-12-31','p.recuperableonly'=>'0 or 1');
  308. $this->import_examplevalues_array[$r]=array_merge($import_sample,$import_extrafield_sample);
  309. $this->import_updatekeys_array[$r]=array('p.ref'=>'Ref','p.barcode'=>'BarCode');
  310. if (! empty($conf->fournisseur->enabled))
  311. {
  312. // Import suppliers prices (note: this code is duplicated into module service)
  313. $r++;
  314. $this->import_code[$r]=$this->rights_class.'_supplierprices';
  315. $this->import_label[$r]="SuppliersPricesOfProductsOrServices"; // Translation key
  316. $this->import_icon[$r]=$this->picto;
  317. $this->import_entities_array[$r]=array(); // We define here only fields that use another icon that the one defined into import_icon
  318. $this->import_tables_array[$r]=array('sp'=>MAIN_DB_PREFIX.'product_fournisseur_price');
  319. $this->import_tables_creator_array[$r]=array('sp'=>'fk_user');
  320. $this->import_fields_array[$r]=array(
  321. 'sp.fk_product'=>"ProductOrService*",
  322. 'sp.fk_soc'=>"Supplier*", 'sp.ref_fourn'=>'SupplierRef', 'sp.quantity'=>"QtyMin*", 'sp.tva_tx'=>'VATRate', 'sp.default_vat_code'=>'VATCode'
  323. );
  324. if (is_object($mysoc) && $mysoc->useNPR()) $this->import_fields_array[$r]=array_merge($this->import_fields_array[$r],array('sp.recuperableonly'=>'VATNPR'));
  325. if (is_object($mysoc) && $mysoc->useLocalTax(1)) $this->import_fields_array[$r]=array_merge($this->import_fields_array[$r],array('sp.localtax1_tx'=>'LT1', 'sp.localtax1_type'=>'LT1Type'));
  326. if (is_object($mysoc) && $mysoc->useLocalTax(2)) $this->import_fields_array[$r]=array_merge($this->import_fields_array[$r],array('sp.localtax2_tx'=>'LT2', 'sp.localtax2_type'=>'LT2Type'));
  327. $this->import_fields_array[$r]=array_merge($this->import_fields_array[$r],array(
  328. 'sp.price'=>"PriceQtyMinHT*",
  329. 'sp.unitprice'=>'UnitPriceHT*', // TODO Make this field not required and calculate it from price and qty
  330. 'sp.remise_percent'=>'DiscountQtyMin'
  331. ));
  332. $this->import_convertvalue_array[$r]=array(
  333. 'sp.fk_soc'=>array('rule'=>'fetchidfromref','classfile'=>'/societe/class/societe.class.php','class'=>'Societe','method'=>'fetch','element'=>'ThirdParty'),
  334. 'sp.fk_product'=>array('rule'=>'fetchidfromref','classfile'=>'/product/class/product.class.php','class'=>'Product','method'=>'fetch','element'=>'Product')
  335. );
  336. $this->import_examplevalues_array[$r]=array('sp.fk_product'=>"PREF123456",
  337. 'sp.fk_soc'=>"My Supplier",'sp.ref_fourn'=>"SupplierRef", 'sp.quantity'=>"1", 'sp.tva_tx'=>'21',
  338. 'sp.price'=>"50",
  339. 'sp.unitprice'=>'50',
  340. 'sp.remise_percent'=>'0'
  341. );
  342. $this->import_updatekeys_array[$r]=array('sp.fk_product'=>'ProductOrService','sp.ref_fourn'=>'SupplierRef','sp.fk_soc'=>'Supplier');
  343. }
  344. if (! empty($conf->global->PRODUIT_MULTIPRICES))
  345. {
  346. // Import product multiprice
  347. $r++;
  348. $this->import_code[$r]=$this->rights_class.'_multiprice';
  349. $this->import_label[$r]="ProductsOrServiceMultiPrice"; // Translation key
  350. $this->import_icon[$r]=$this->picto;
  351. $this->import_entities_array[$r]=array(); // We define here only fields that use another icon that the one defined into import_icon
  352. $this->import_tables_array[$r]=array('pr'=>MAIN_DB_PREFIX.'product_price');
  353. $this->import_tables_creator_array[$r]=array('pr'=>'fk_user_author'); // Fields to store import user id
  354. $this->import_fields_array[$r]=array('pr.fk_product'=>"ProductRowid*",
  355. 'pr.price_base_type'=>"PriceLevelPriceBase",'pr.price_level'=>"PriceLevel",
  356. 'pr.price'=>"PriceLevelUnitPriceHT",'pr.price_ttc'=>"PriceLevelUnitPriceTTC",
  357. 'pr.price_min'=>"MinPriceLevelUnitPriceHT",'pr.price_min_ttc'=>"MinPriceLevelUnitPriceTTC",
  358. 'pr.tva_tx'=>'PriceLevelVATRate',
  359. 'pr.date_price'=>'DateCreation*');
  360. if (is_object($mysoc) && $mysoc->useNPR()) $this->import_fields_array[$r]=array_merge($this->import_fields_array[$r],array('pr.recuperableonly'=>'NPR'));
  361. $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]$');
  362. $this->import_examplevalues_array[$r]=array('pr.fk_product'=>"1",
  363. 'pr.price_base_type'=>"HT",'pr.price_level'=>"1",
  364. 'pr.price'=>"100",'pr.price_ttc'=>"110",
  365. 'pr.price_min'=>"100",'pr.price_min_ttc'=>"110",
  366. 'pr.tva_tx'=>'20',
  367. 'pr.recuperableonly'=>'0',
  368. 'pr.date_price'=>'2013-04-10');
  369. }
  370. if (! empty($conf->global->MAIN_MULTILANGS))
  371. {
  372. $r++;
  373. $this->import_code[$r]=$this->rights_class.'_languages';
  374. $this->import_label[$r]="ProductsOrServicesTranslations";
  375. $this->import_icon[$r]=$this->picto;
  376. $this->import_entities_array[$r]=array(); // We define here only fields that use another icon that the one defined into import_icon
  377. $this->import_tables_array[$r]=array('l'=>MAIN_DB_PREFIX.'product_lang');
  378. // multiline translation, one line per translation
  379. $this->import_fields_array[$r]=array('l.fk_product'=>'Ref', 'l.lang'=>'Language', 'l.label'=>'TranslatedLabel', 'l.description'=>'TranslatedDescription');
  380. //$this->import_fields_array[$r]['l.note']='TranslatedNote';
  381. $this->import_convertvalue_array[$r]=array(
  382. 'l.fk_product'=>array('rule'=>'fetchidfromref','classfile'=>'/product/class/product.class.php','class'=>'Product','method'=>'fetch','element'=>'Product')
  383. );
  384. $this->import_examplevalues_array[$r]=array('l.fk_product'=>'MyProductRef','l.lang'=>'en_US','l.label'=>'Label in en_US','l.description'=>'Desc in en_US');
  385. $this->import_updatekeys_array[$r]=array('l.fk_product'=>'Ref','l.lang'=>'Language');
  386. }
  387. }
  388. /**
  389. * Function called when module is enabled.
  390. * The init function add constants, boxes, permissions and menus (defined in constructor) into Dolibarr database.
  391. * It also creates data directories
  392. *
  393. * @param string $options Options when enabling module ('', 'newboxdefonly', 'noboxes')
  394. * @return int 1 if OK, 0 if KO
  395. */
  396. function init($options='')
  397. {
  398. $this->remove($options);
  399. $sql = array();
  400. return $this->_init($sql,$options);
  401. }
  402. }