modProduct.class.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 = "Gestion des produits";
  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->special = 0;
  56. $this->picto='product';
  57. // Data directories to create when module is enabled
  58. $this->dirs = array("/product/temp");
  59. // Dependencies
  60. $this->depends = array();
  61. $this->requiredby = array("modStock","modBarcode","modProductBatch");
  62. // Config pages
  63. $this->config_page_url = array("product.php@product");
  64. $this->langfiles = array("products","companies","stocks","bills");
  65. // Constants
  66. $this->const = array();
  67. $r=0;
  68. $this->const[$r][0] = "PRODUCT_CODEPRODUCT_ADDON";
  69. $this->const[$r][1] = "chaine";
  70. $this->const[$r][2] = "mod_codeproduct_leopard";
  71. $this->const[$r][3] = 'Module to control product codes';
  72. $this->const[$r][4] = 0;
  73. $r++;
  74. // Boxes
  75. $this->boxes = array(
  76. 0=>array('file'=>'box_produits.php','enabledbydefaulton'=>'Home'),
  77. 1=>array('file'=>'box_produits_alerte_stock.php','enabledbydefaulton'=>''),
  78. 2=>array('file'=>'box_graph_product_distribution.php','enabledbydefaulton'=>'Home')
  79. );
  80. // Permissions
  81. $this->rights = array();
  82. $this->rights_class = 'produit';
  83. $r=0;
  84. $this->rights[$r][0] = 31; // id de la permission
  85. $this->rights[$r][1] = 'Lire les produits'; // libelle de la permission
  86. $this->rights[$r][2] = 'r'; // type de la permission (deprecie a ce jour)
  87. $this->rights[$r][3] = 1; // La permission est-elle une permission par defaut
  88. $this->rights[$r][4] = 'lire';
  89. $r++;
  90. $this->rights[$r][0] = 32; // id de la permission
  91. $this->rights[$r][1] = 'Creer/modifier les produits'; // libelle de la permission
  92. $this->rights[$r][2] = 'w'; // type de la permission (deprecie a ce jour)
  93. $this->rights[$r][3] = 0; // La permission est-elle une permission par defaut
  94. $this->rights[$r][4] = 'creer';
  95. $r++;
  96. $this->rights[$r][0] = 34; // id de la permission
  97. $this->rights[$r][1] = 'Supprimer les produits'; // libelle de la permission
  98. $this->rights[$r][2] = 'd'; // type de la permission (deprecie a ce jour)
  99. $this->rights[$r][3] = 0; // La permission est-elle une permission par defaut
  100. $this->rights[$r][4] = 'supprimer';
  101. $r++;
  102. $this->rights[$r][0] = 38; // Must be same permission than in service module
  103. $this->rights[$r][1] = 'Exporter les produits';
  104. $this->rights[$r][2] = 'r';
  105. $this->rights[$r][3] = 0;
  106. $this->rights[$r][4] = 'export';
  107. $r++;
  108. /* We can't enable this here because it must be enabled in both product and service module and this create duplicate insert
  109. $r=0;
  110. $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
  111. 'type'=>'left', // This is a Left menu entry
  112. 'titre'=>'ProductVatMassChange',
  113. 'url'=>'/product/admin/product_tools.php?mainmenu=home&leftmenu=admintools',
  114. 'langs'=>'products', // Lang file to use (without .lang) by module. File must be in langs/code_CODE/ directory.
  115. 'position'=>300,
  116. 'enabled'=>'$conf->product->enabled && $leftmenu=="admintools"', // 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.
  117. 'perms'=>'1', // Use 'perms'=>'$user->rights->mymodule->level1->level2' if you want your menu with a permission rules
  118. 'target'=>'',
  119. 'user'=>0); // 0=Menu for internal users, 1=external users, 2=both
  120. $r++;
  121. */
  122. // Exports
  123. //--------
  124. $r=0;
  125. $r++;
  126. $this->export_code[$r]=$this->rights_class.'_'.$r;
  127. $this->export_label[$r]="Products"; // Translation key (used only if key ExportDataset_xxx_z not found)
  128. $this->export_permission[$r]=array(array("produit","export"));
  129. $this->export_fields_array[$r]=array('p.rowid'=>"Id",'p.ref'=>"Ref",'p.label'=>"Label",'p.description'=>"Description",'p.url'=>"PublicUrl",'p.accountancy_code_sell'=>"ProductAccountancySellCode",'p.accountancy_code_buy'=>"ProductAccountancyBuyCode",'p.note'=>"Note",'p.length'=>"Length",'p.surface'=>"Surface",'p.volume'=>"Volume",'p.weight'=>"Weight",'p.customcode'=>'CustomCode','p.price_base_type'=>"PriceBase",'p.price'=>"UnitPriceHT",'p.price_ttc'=>"UnitPriceTTC",'p.tva_tx'=>'VATRate','p.tosell'=>"OnSell",'p.tobuy'=>"OnBuy",'p.datec'=>'DateCreation','p.tms'=>'DateModification');
  130. if ($mysoc->useNPR()) $this->export_fields_array[$r]['p.recuperableonly']='NPR';
  131. if (! empty($conf->stock->enabled)) $this->export_fields_array[$r]=array_merge($this->export_fields_array[$r],array('p.stock'=>'Stock','p.pmp'=>'PMPValue'));
  132. if (! empty($conf->barcode->enabled)) $this->export_fields_array[$r]=array_merge($this->export_fields_array[$r],array('p.barcode'=>'BarCode'));
  133. 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.unitprice'=>'SuppliersPrices'));
  134. $this->export_TypeFields_array[$r]=array('p.ref'=>"Text",'p.label'=>"Text",'p.description'=>"Text",'p.url'=>"Text",'p.accountancy_code_sell'=>"Text",'p.accountancy_code_buy'=>"Text",'p.note'=>"Text",'p.length'=>"Numeric",'p.surface'=>"Numeric",'p.volume'=>"Numeric",'p.weight'=>"Numeric",'p.customcode'=>'Text','p.price_base_type'=>"Text",'p.price'=>"Numeric",'p.price_ttc'=>"Numeric",'p.tva_tx'=>'Numeric','p.tosell'=>"Boolean",'p.tobuy'=>"Boolean",'p.datec'=>'Date','p.tms'=>'Date');
  135. if (! empty($conf->stock->enabled)) $this->export_TypeFields_array[$r]=array_merge($this->export_TypeFields_array[$r],array('p.stock'=>'Numeric','p.pmp'=>'Numeric'));
  136. if (! empty($conf->barcode->enabled)) $this->export_TypeFields_array[$r]=array_merge($this->export_TypeFields_array[$r],array('p.barcode'=>'Text'));
  137. 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'));
  138. $this->export_entities_array[$r]=array('p.rowid'=>"product",'p.ref'=>"product",'p.label'=>"product",'p.description'=>"product",'p.url'=>"product",'p.accountancy_code_sell'=>'product','p.accountancy_code_sell'=>'product','p.note'=>"product",'p.length'=>"product",'p.surface'=>"product",'p.volume'=>"product",'p.weight'=>"product",'p.customcode'=>'product','p.price_base_type'=>"product",'p.price'=>"product",'p.price_ttc'=>"product",'p.tva_tx'=>"product",'p.tosell'=>"product",'p.tobuy'=>"product",'p.datec'=>"product",'p.tms'=>"product");
  139. if (! empty($conf->stock->enabled)) $this->export_entities_array[$r]=array_merge($this->export_entities_array[$r],array('p.stock'=>'product','p.pmp'=>'product'));
  140. if (! empty($conf->barcode->enabled)) $this->export_entities_array[$r]=array_merge($this->export_entities_array[$r],array('p.barcode'=>'product'));
  141. if (! empty($conf->fournisseur->enabled)) $this->export_entities_array[$r]=array_merge($this->export_entities_array[$r],array('s.nom'=>'product','pf.ref_fourn'=>'product','pf.unitprice'=>'product'));
  142. $keyforselect='product'; $keyforelement='product'; $keyforaliasextra='extra';
  143. include DOL_DOCUMENT_ROOT.'/core/extrafieldsinexport.inc.php';
  144. $this->export_sql_start[$r]='SELECT DISTINCT ';
  145. $this->export_sql_end[$r] =' FROM '.MAIN_DB_PREFIX.'product as p';
  146. $this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'product_extrafields as extra ON p.rowid = extra.fk_object';
  147. 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';
  148. $this->export_sql_end[$r] .=' WHERE p.fk_product_type = 0 AND p.entity IN ('.getEntity("product", 1).')';
  149. if (! empty($conf->global->PRODUIT_MULTIPRICES))
  150. {
  151. // Exports product multiprice
  152. $r++;
  153. $this->export_code[$r]=$this->rights_class.'_'.$r;
  154. $this->export_label[$r]="ProductsMultiPrice"; // Translation key (used only if key ExportDataset_xxx_z not found)
  155. $this->export_permission[$r]=array(array("produit","export"));
  156. $this->export_fields_array[$r]=array('p.rowid'=>"Id",'p.ref'=>"Ref",
  157. 'pr.price_base_type'=>"PriceLevelPriceBase",'pr.price_level'=>"PriceLevel",
  158. 'pr.price'=>"PriceLevelUnitPriceHT",'pr.price_ttc'=>"PriceLevelUnitPriceTTC",
  159. 'pr.price_min'=>"MinPriceLevelUnitPriceHT",'pr.price_min_ttc'=>"MinPriceLevelUnitPriceTTC",
  160. 'pr.tva_tx'=>'PriceLevelVATRate',
  161. 'pr.date_price'=>'DateCreation');
  162. if ($mysoc->useNPR()) $this->export_fields_array[$r]['pr.recuperableonly']='NPR';
  163. //$this->export_TypeFields_array[$r]=array('p.ref'=>"Text",'p.label'=>"Text",'p.description'=>"Text",'p.url'=>"Text",'p.accountancy_code_sell'=>"Text",'p.accountancy_code_buy'=>"Text",'p.note'=>"Text",'p.length'=>"Numeric",'p.surface'=>"Numeric",'p.volume'=>"Numeric",'p.weight'=>"Numeric",'p.customcode'=>'Text','p.price_base_type'=>"Text",'p.price'=>"Numeric",'p.price_ttc'=>"Numeric",'p.tva_tx'=>'Numeric','p.tosell'=>"Boolean",'p.tobuy'=>"Boolean",'p.datec'=>'Date','p.tms'=>'Date');
  164. $this->export_entities_array[$r]=array('p.rowid'=>"product",'p.ref'=>"product",
  165. 'pr.price_base_type'=>"product",'pr.price_level'=>"product",'pr.price'=>"product",
  166. 'pr.price_ttc'=>"product",
  167. 'pr.price_min'=>"product",'pr.price_min_ttc'=>"product",
  168. 'pr.tva_tx'=>'product',
  169. 'pr.recuperableonly'=>'product',
  170. 'pr.date_price'=>"product");
  171. $this->export_sql_start[$r]='SELECT DISTINCT ';
  172. $this->export_sql_end[$r] =' FROM '.MAIN_DB_PREFIX.'product as p';
  173. $this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'product_price as pr ON p.rowid = pr.fk_product';
  174. $this->export_sql_end[$r] .=' WHERE p.fk_product_type = 0 AND p.entity IN ('.getEntity("product", 1).')';
  175. }
  176. // Imports
  177. //--------
  178. $r=0;
  179. $r++;
  180. $this->import_code[$r]=$this->rights_class.'_'.$r;
  181. $this->import_label[$r]="Products"; // Translation key
  182. $this->import_icon[$r]=$this->picto;
  183. $this->import_entities_array[$r]=array(); // We define here only fields that use another icon that the one defined into import_icon
  184. $this->import_tables_array[$r]=array('p'=>MAIN_DB_PREFIX.'product','extra'=>MAIN_DB_PREFIX.'product_extrafields');
  185. $this->import_tables_creator_array[$r]=array('p'=>'fk_user_author'); // Fields to store import user id
  186. $this->import_fields_array[$r]=array('p.ref'=>"Ref*",'p.label'=>"Label*",'p.description'=>"Description",'p.url'=>"PublicUrl",'p.accountancy_code_sell'=>"ProductAccountancySellCode",'p.accountancy_code_buy'=>"ProductAccountancyBuyCode",'p.note'=>"Note",'p.length'=>"Length",'p.surface'=>"Surface",'p.volume'=>"Volume",'p.weight'=>"Weight",'p.duration'=>"Duration",'p.customcode'=>'CustomCode','p.price'=>"SellingPriceHT",'p.price_ttc'=>"SellingPriceTTC",'p.tva_tx'=>'VAT','p.tosell'=>"OnSell*",'p.tobuy'=>"OnBuy*",'p.fk_product_type'=>"Type*",'p.finished'=>'Nature','p.datec'=>'DateCreation*');
  187. if (! empty($conf->barcode->enabled)) $this->import_fields_array[$r]=array_merge($this->import_fields_array[$r],array('p.barcode'=>'BarCode'));
  188. if ($mysoc->useNPR()) $this->import_fields_array[$r]=array_merge($this->import_fields_array[$r],array('p.recuperableonly'=>'NPR'));
  189. // Add extra fields
  190. $import_extrafield_sample=array();
  191. $sql="SELECT name, label, fieldrequired FROM ".MAIN_DB_PREFIX."extrafields WHERE elementtype = 'product' AND entity IN (0, ".$conf->entity.')';
  192. $resql=$this->db->query($sql);
  193. if ($resql) // This can fail when class is used on old database (during migration for example)
  194. {
  195. while ($obj=$this->db->fetch_object($resql))
  196. {
  197. $fieldname='extra.'.$obj->name;
  198. $fieldlabel=ucfirst($obj->label);
  199. $this->import_fields_array[$r][$fieldname]=$fieldlabel.($obj->fieldrequired?'*':'');
  200. $import_extrafield_sample[$fieldname]=$fieldlabel;
  201. }
  202. }
  203. // End add extra fields
  204. $this->import_fieldshidden_array[$r]=array('extra.fk_object'=>'lastrowid-'.MAIN_DB_PREFIX.'product'); // aliastable.field => ('user->id' or 'lastrowid-'.tableparent)
  205. $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]$');
  206. $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');
  207. $this->import_examplevalues_array[$r]=array_merge($import_sample,$import_extrafield_sample);
  208. if (! empty($conf->fournisseur->enabled))
  209. {
  210. // Import suppliers prices (note: this code is duplicated into module service)
  211. $r++;
  212. $this->import_code[$r]=$this->rights_class.'_supplierprices';
  213. $this->import_label[$r]="SuppliersPricesOfProductsOrServices"; // Translation key
  214. $this->import_icon[$r]=$this->picto;
  215. $this->import_entities_array[$r]=array(); // We define here only fields that use another icon that the one defined into import_icon
  216. $this->import_tables_array[$r]=array('sp'=>MAIN_DB_PREFIX.'product_fournisseur_price');
  217. $this->import_tables_creator_array[$r]=array('sp'=>'fk_user');
  218. $this->import_fields_array[$r]=array('sp.fk_product'=>"ProductOrService*",
  219. 'sp.fk_soc'=>"Supplier*", 'sp.ref_fourn'=>'SupplierRef', 'sp.quantity'=>"QtyMin*", 'sp.tva_tx'=>'VATRate',
  220. 'sp.price'=>"PriceQtyMinHT*",
  221. 'sp.unitprice'=>'UnitPriceHT*', // TODO Make this file not required and calculate it from price and qty
  222. 'sp.remise_percent'=>'DiscountQtyMin'
  223. );
  224. $this->import_convertvalue_array[$r]=array(
  225. 'sp.fk_soc'=>array('rule'=>'fetchidfromref','classfile'=>'/societe/class/societe.class.php','class'=>'Societe','method'=>'fetch','element'=>'ThirdParty'),
  226. 'sp.fk_product'=>array('rule'=>'fetchidfromref','classfile'=>'/product/class/product.class.php','class'=>'Product','method'=>'fetch','element'=>'Product')
  227. );
  228. $this->import_examplevalues_array[$r]=array('sp.fk_product'=>"PREF123456",
  229. 'sp.fk_soc'=>"My Supplier",'sp.ref_fourn'=>"SupplierRef", 'sp.quantity'=>"1", 'sp.tva_tx'=>'21',
  230. 'sp.price'=>"50",
  231. 'sp.unitprice'=>'50',
  232. 'sp.remise_percent'=>'0'
  233. );
  234. }
  235. if (! empty($conf->global->PRODUIT_MULTIPRICES))
  236. {
  237. // Import product multiprice
  238. $r++;
  239. $this->import_code[$r]=$this->rights_class.'_multiprice';
  240. $this->import_label[$r]="ProductsOrServiceMultiPrice"; // Translation key
  241. $this->import_icon[$r]=$this->picto;
  242. $this->import_entities_array[$r]=array(); // We define here only fields that use another icon that the one defined into import_icon
  243. $this->import_tables_array[$r]=array('pr'=>MAIN_DB_PREFIX.'product_price');
  244. $this->import_tables_creator_array[$r]=array('pr'=>'fk_user_author'); // Fields to store import user id
  245. $this->import_fields_array[$r]=array('pr.fk_product'=>"ProductRowid*",
  246. 'pr.price_base_type'=>"PriceLevelPriceBase",'pr.price_level'=>"PriceLevel",
  247. 'pr.price'=>"PriceLevelUnitPriceHT",'pr.price_ttc'=>"PriceLevelUnitPriceTTC",
  248. 'pr.price_min'=>"MinPriceLevelUnitPriceHT",'pr.price_min_ttc'=>"MinPriceLevelUnitPriceTTC",
  249. 'pr.tva_tx'=>'PriceLevelVATRate',
  250. 'pr.date_price'=>'DateCreation*');
  251. if ($mysoc->useNPR()) $this->import_fields_array[$r]=array_merge($this->import_fields_array[$r],array('pr.recuperableonly'=>'NPR'));
  252. $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]$');
  253. $this->import_examplevalues_array[$r]=array('pr.fk_product'=>"1",
  254. 'pr.price_base_type'=>"HT",'pr.price_level'=>"1",
  255. 'pr.price'=>"100",'pr.price_ttc'=>"110",
  256. 'pr.price_min'=>"100",'pr.price_min_ttc'=>"110",
  257. 'pr.tva_tx'=>'20',
  258. 'pr.recuperableonly'=>'0',
  259. 'pr.date_price'=>'2013-04-10');
  260. }
  261. }
  262. /**
  263. * Function called when module is enabled.
  264. * The init function add constants, boxes, permissions and menus (defined in constructor) into Dolibarr database.
  265. * It also creates data directories
  266. *
  267. * @param string $options Options when enabling module ('', 'newboxdefonly', 'noboxes')
  268. * @return int 1 if OK, 0 if KO
  269. */
  270. function init($options='')
  271. {
  272. $this->remove($options);
  273. $sql = array();
  274. return $this->_init($sql,$options);
  275. }
  276. }