modService.class.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. <?php
  2. /* Copyright (C) 2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2014 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. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. /**
  22. * \defgroup service Module services
  23. * \brief Module pour gerer le suivi de services predefinis
  24. * \file htdocs/core/modules/modService.class.php
  25. * \ingroup service
  26. * \brief Fichier de description et activation du module Service
  27. */
  28. include_once DOL_DOCUMENT_ROOT .'/core/modules/DolibarrModules.class.php';
  29. /**
  30. * Class to describe and enable module Service
  31. */
  32. class modService extends DolibarrModules
  33. {
  34. /**
  35. * Constructor. Define names, constants, directories, boxes, permissions
  36. *
  37. * @param DoliDB $db Database handler
  38. */
  39. function __construct($db)
  40. {
  41. global $conf;
  42. $this->db = $db;
  43. $this->numero = 53;
  44. $this->family = "products";
  45. $this->module_position = 30;
  46. // Module label (no space allowed), used if translation string 'ModuleXXXName' not found (where XXX is value of numeric property 'numero' of module)
  47. $this->name = preg_replace('/^mod/i','',get_class($this));
  48. $this->description = "Gestion des services";
  49. // Possible values for version are: 'development', 'experimental', 'dolibarr' or version
  50. $this->version = 'dolibarr';
  51. $this->const_name = 'MAIN_MODULE_'.strtoupper($this->name);
  52. $this->special = 0;
  53. $this->picto='service';
  54. // Data directories to create when module is enabled
  55. $this->dirs = array("/product/temp");
  56. // Dependancies
  57. $this->depends = array();
  58. $this->requiredby = array();
  59. // Config pages
  60. $this->config_page_url = array("product.php@product");
  61. $this->langfiles = array("products","companies","bills");
  62. // Constants
  63. $this->const = array();
  64. // Boxes
  65. $this->boxes = array(
  66. 0=>array('file'=>'box_services_contracts.php','enabledbydefaulton'=>'Home'),
  67. 1=>array('file'=>'box_graph_product_distribution.php','enabledbydefaulton'=>'Home')
  68. );
  69. // Permissions
  70. $this->rights = array();
  71. $this->rights_class = 'service';
  72. $r=0;
  73. $this->rights[$r][0] = 531; // id de la permission
  74. $this->rights[$r][1] = 'Lire les services'; // libelle de la permission
  75. $this->rights[$r][2] = 'r'; // type de la permission (deprecie a ce jour)
  76. $this->rights[$r][3] = 1; // La permission est-elle une permission par defaut
  77. $this->rights[$r][4] = 'lire';
  78. $r++;
  79. $this->rights[$r][0] = 532; // id de la permission
  80. $this->rights[$r][1] = 'Creer/modifier les services'; // libelle de la permission
  81. $this->rights[$r][2] = 'w'; // type de la permission (deprecie a ce jour)
  82. $this->rights[$r][3] = 0; // La permission est-elle une permission par defaut
  83. $this->rights[$r][4] = 'creer';
  84. $r++;
  85. $this->rights[$r][0] = 534; // id de la permission
  86. $this->rights[$r][1] = 'Supprimer les services'; // libelle de la permission
  87. $this->rights[$r][2] = 'd'; // type de la permission (deprecie a ce jour)
  88. $this->rights[$r][3] = 0; // La permission est-elle une permission par defaut
  89. $this->rights[$r][4] = 'supprimer';
  90. $r++;
  91. $this->rights[$r][0] = 538; // Must be same permission than in product module
  92. $this->rights[$r][1] = 'Exporter les services';
  93. $this->rights[$r][2] = 'r';
  94. $this->rights[$r][3] = 0;
  95. $this->rights[$r][4] = 'export';
  96. $r++;
  97. /* We can't enable this here because it must be enabled in both product and service module and this create duplicate insert
  98. $r=0;
  99. $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
  100. 'type'=>'left', // This is a Left menu entry
  101. 'titre'=>'ProductVatMassChange',
  102. 'url'=>'/product/admin/product_tools.php?mainmenu=home&leftmenu=admintools',
  103. 'langs'=>'products', // Lang file to use (without .lang) by module. File must be in langs/code_CODE/ directory.
  104. 'position'=>300,
  105. '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.
  106. 'perms'=>'1', // Use 'perms'=>'$user->rights->mymodule->level1->level2' if you want your menu with a permission rules
  107. 'target'=>'',
  108. 'user'=>0); // 0=Menu for internal users, 1=external users, 2=both
  109. $r++;
  110. */
  111. // Exports
  112. //--------
  113. $r=0;
  114. $r++;
  115. $this->export_code[$r]=$this->rights_class.'_'.$r;
  116. $this->export_label[$r]="Services"; // Translation key (used only if key ExportDataset_xxx_z not found)
  117. $this->export_permission[$r]=array(array("service","export"));
  118. $this->export_fields_array[$r]=array('p.rowid'=>"Id",'p.ref'=>"Ref",'p.label'=>"Label",'p.description'=>"Description",'p.accountancy_code_sell'=>"ProductAccountancySellCode",'p.accountancy_code_buy'=>"ProductAccountancyBuyCode",'p.note'=>"Note",'p.price_base_type'=>"PriceBase",'p.price'=>"UnitPriceHT",'p.price_ttc'=>"UnitPriceTTC",'p.tva_tx'=>'VATRate','p.tosell'=>"OnSell",'p.tobuy'=>"OnBuy",'p.duration'=>"Duration",'p.datec'=>'DateCreation','p.tms'=>'DateModification');
  119. if (! empty($conf->stock->enabled)) $this->export_fields_array[$r]=array_merge($this->export_fields_array[$r],array('p.stock'=>'Stock'));
  120. //$this->export_TypeFields_array[$r]=array('p.ref'=>"Text",'p.label'=>"Text",'p.description'=>"Text",'p.accountancy_code_sell'=>"Text",'p.accountancy_code_buy'=>"Text",'p.note'=>"Text",'p.price_base_type'=>"Text",'p.price'=>"Numeric",'p.price_ttc'=>"Numeric",'p.tva_tx'=>'Numeric','p.tosell'=>"Boolean",'p.duration'=>"Duree",'p.datec'=>'Date','p.tms'=>'Date');
  121. $this->export_TypeFields_array[$r]=array('p.ref'=>"Text",'p.label'=>"Text",'p.description'=>"Text",'p.accountancy_code_sell'=>"Text",'p.accountancy_code_buy'=>"Text",'p.note'=>"Text",'p.price_base_type'=>"Text",'p.price'=>"Numeric",'p.price_ttc'=>"Numeric",'p.tva_tx'=>'Numeric','p.tosell'=>"Boolean",'p.tobuy'=>"Boolean",'p.duration'=>"Duree",'p.datec'=>'Date','p.tms'=>'Date');
  122. if (! empty($conf->stock->enabled)) $this->export_TypeFields_array[$r]=array_merge($this->export_TypeFields_array[$r],array('p.stock'=>'Numeric'));
  123. if (! empty($conf->barcode->enabled)) $this->export_TypeFields_array[$r]=array_merge($this->export_TypeFields_array[$r],array('p.barcode'=>'Text'));
  124. $this->export_entities_array[$r]=array('p.rowid'=>"service",'p.ref'=>"service",'p.label'=>"service",'p.description'=>"service",'p.accountancy_code_sell'=>'service','p.note'=>"service",'p.price_base_type'=>"service",'p.price'=>"service",'p.price_ttc'=>"service",'p.tva_tx'=>"service",'p.tosell'=>"service",'p.tobuy'=>"service",'p.duration'=>"service",'p.datec'=>"service",'p.tms'=>"service");
  125. if (! empty($conf->stock->enabled)) $this->export_entities_array[$r]=array_merge($this->export_entities_array[$r],array('p.stock'=>'service'));
  126. if (! empty($conf->barcode->enabled)) $this->export_entities_array[$r]=array_merge($this->export_entities_array[$r],array('p.barcode'=>'service'));
  127. // Add extra fields
  128. $sql="SELECT name, label, type FROM ".MAIN_DB_PREFIX."extrafields WHERE elementtype = 'product'";
  129. $resql=$this->db->query($sql);
  130. if ($resql) // This can fail when class is used on old database (during migration for example)
  131. {
  132. while ($obj=$this->db->fetch_object($resql))
  133. {
  134. $fieldname='extra.'.$obj->name;
  135. $fieldlabel=ucfirst($obj->label);
  136. $typeFilter="Text";
  137. switch($obj->type)
  138. {
  139. case 'int':
  140. case 'double':
  141. case 'price':
  142. $typeFilter="Numeric";
  143. break;
  144. case 'date':
  145. case 'datetime':
  146. $typeFilter="Date";
  147. break;
  148. case 'boolean':
  149. $typeFilter="Boolean";
  150. break;
  151. case 'sellist':
  152. $tmp='';
  153. $tmpparam=unserialize($obj->param); // $tmp ay be array 'options' => array 'c_currencies:code_iso:code_iso' => null
  154. if ($tmpparam['options'] && is_array($tmpparam['options'])) $tmp=array_shift(array_keys($tmpparam['options']));
  155. if (preg_match('/[a-z0-9_]+:[a-z0-9_]+:[a-z0-9_]+/', $tmp)) $typeFilter="List:".$tmp;
  156. break;
  157. }
  158. $this->export_fields_array[$r][$fieldname]=$fieldlabel;
  159. $this->export_TypeFields_array[$r][$fieldname]=$typeFilter;
  160. $this->export_entities_array[$r][$fieldname]='product';
  161. }
  162. }
  163. // End add extra fields
  164. $this->export_sql_start[$r]='SELECT DISTINCT ';
  165. $this->export_sql_end[$r] =' FROM '.MAIN_DB_PREFIX.'product as p';
  166. $this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'product_extrafields as extra ON p.rowid = extra.fk_object';
  167. 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';
  168. $this->export_sql_end[$r] .=' WHERE p.fk_product_type = 1 AND p.entity IN ('.getEntity("product", 1).')';
  169. if (empty($conf->product->enabled)) // We enable next import templates only if module product not already enabled (to avoid duplicate entries)
  170. {
  171. if (! empty($conf->global->PRODUIT_MULTIPRICES))
  172. {
  173. // Exports product multiprice
  174. $r++;
  175. $this->export_code[$r]=$this->rights_class.'_'.$r;
  176. $this->export_label[$r]="ProductsMultiPrice"; // Translation key (used only if key ExportDataset_xxx_z not found)
  177. $this->export_permission[$r]=array(array("produit","export"));
  178. $this->export_fields_array[$r]=array('p.rowid'=>"Id",'p.ref'=>"Ref",
  179. 'pr.price_base_type'=>"PriceLevelPriceBase",'pr.price_level'=>"PriceLevel",
  180. 'pr.price'=>"PriceLevelUnitPriceHT",'pr.price_ttc'=>"PriceLevelUnitPriceTTC",
  181. 'pr.price_min'=>"MinPriceLevelUnitPriceHT",'pr.price_min_ttc'=>"MinPriceLevelUnitPriceTTC",
  182. 'pr.tva_tx'=>'PriceLevelVATRate',
  183. 'pr.date_price'=>'DateCreation');
  184. //$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');
  185. $this->export_entities_array[$r]=array('p.rowid'=>"product",'p.ref'=>"product",
  186. 'pr.price_base_type'=>"product",'pr.price_level'=>"product",'pr.price'=>"product",
  187. 'pr.price_ttc'=>"product",
  188. 'pr.price_min'=>"product",'pr.price_min_ttc'=>"product",
  189. 'pr.tva_tx'=>'product',
  190. 'pr.date_price'=>"product");
  191. $this->export_sql_start[$r]='SELECT DISTINCT ';
  192. $this->export_sql_end[$r] =' FROM '.MAIN_DB_PREFIX.'product as p';
  193. $this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'product_price as pr ON p.rowid = pr.fk_product';
  194. $this->export_sql_end[$r] .=' WHERE p.fk_product_type = 0 AND p.entity IN ('.getEntity("product", 1).')';
  195. }
  196. }
  197. // Imports
  198. //--------
  199. $r=0;
  200. $r++;
  201. $this->import_code[$r]=$this->rights_class.'_'.$r;
  202. $this->import_label[$r]="Products"; // Translation key
  203. $this->import_icon[$r]=$this->picto;
  204. $this->import_entities_array[$r]=array(); // We define here only fields that use another icon that the one defined into import_icon
  205. $this->import_tables_array[$r]=array('p'=>MAIN_DB_PREFIX.'product','extra'=>MAIN_DB_PREFIX.'product_extrafields');
  206. $this->import_tables_creator_array[$r]=array('p'=>'fk_user_author'); // Fields to store import user id
  207. $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*');
  208. if (! empty($conf->barcode->enabled)) $this->import_fields_array[$r]=array_merge($this->import_fields_array[$r],array('p.barcode'=>'BarCode'));
  209. // Add extra fields
  210. $sql="SELECT name, label, fieldrequired FROM ".MAIN_DB_PREFIX."extrafields WHERE elementtype = 'product' AND entity = ".$conf->entity;
  211. $resql=$this->db->query($sql);
  212. if ($resql) // This can fail when class is used on old database (during migration for example)
  213. {
  214. while ($obj=$this->db->fetch_object($resql))
  215. {
  216. $fieldname='extra.'.$obj->name;
  217. $fieldlabel=ucfirst($obj->label);
  218. $this->import_fields_array[$r][$fieldname]=$fieldlabel.($obj->fieldrequired?'*':'');
  219. }
  220. }
  221. // End add extra fields
  222. $this->import_fieldshidden_array[$r]=array('extra.fk_object'=>'lastrowid-'.MAIN_DB_PREFIX.'product'); // aliastable.field => ('user->id' or 'lastrowid-'.tableparent)
  223. $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]$');
  224. $this->import_examplevalues_array[$r]=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');
  225. if (empty($conf->product->enabled)) // We enable next import templates only if module product not already enabled (to avoid duplicate entries)
  226. {
  227. if (! empty($conf->fournisseur->enabled))
  228. {
  229. // Import suppliers prices (note: this code is duplicated into module product)
  230. $r++;
  231. $this->import_code[$r]=$this->rights_class.'_supplierprices';
  232. $this->import_label[$r]="SuppliersPricesOfProductsOrServices"; // Translation key
  233. $this->import_icon[$r]=$this->picto;
  234. $this->import_entities_array[$r]=array(); // We define here only fields that use another icon that the one defined into import_icon
  235. $this->import_tables_array[$r]=array('sp'=>MAIN_DB_PREFIX.'product_fournisseur_price');
  236. $this->import_tables_creator_array[$r]=array('sp'=>'fk_user');
  237. $this->import_fields_array[$r]=array('sp.fk_product'=>"ProductOrService*",
  238. 'sp.fk_soc'=>"Supplier*", 'sp.ref_fourn'=>'SupplierRef', 'sp.quantity'=>"QtyMin*", 'sp.tva_tx'=>'VATRate',
  239. 'sp.price'=>"PriceQtyMinHT*",
  240. 'sp.unitprice'=>'UnitPriceHT*', // TODO Make this file not required and calculate it from price and qty
  241. 'sp.remise_percent'=>'DiscountQtyMin'
  242. );
  243. $this->import_convertvalue_array[$r]=array(
  244. 'sp.fk_soc'=>array('rule'=>'fetchidfromref','classfile'=>'/societe/class/societe.class.php','class'=>'Societe','method'=>'fetch','element'=>'ThirdParty'),
  245. 'sp.fk_product'=>array('rule'=>'fetchidfromref','classfile'=>'/product/class/product.class.php','class'=>'Product','method'=>'fetch','element'=>'Product')
  246. );
  247. $this->import_examplevalues_array[$r]=array('sp.fk_product'=>"PREF123456",
  248. 'sp.fk_soc'=>"My Supplier",'sp.ref_fourn'=>"SupplierRef", 'sp.quantity'=>"1", 'sp.tva_tx'=>'21',
  249. 'sp.price'=>"50",
  250. 'sp.unitprice'=>'50',
  251. 'sp.remise_percent'=>'0'
  252. );
  253. }
  254. if (! empty($conf->global->PRODUIT_MULTIPRICES))
  255. {
  256. // Import product multiprice
  257. $r++;
  258. $this->import_code[$r]=$this->rights_class.'_multiprice';
  259. $this->import_label[$r]="ProductsOrServiceMultiPrice"; // Translation key
  260. $this->import_icon[$r]=$this->picto;
  261. $this->import_entities_array[$r]=array(); // We define here only fields that use another icon that the one defined into import_icon
  262. $this->import_tables_array[$r]=array('pr'=>MAIN_DB_PREFIX.'product_price');
  263. $this->import_tables_creator_array[$r]=array('pr'=>'fk_user_author'); // Fields to store import user id
  264. $this->import_fields_array[$r]=array('pr.fk_product'=>"ProductRowid*",
  265. 'pr.price_base_type'=>"PriceLevelPriceBase",'pr.price_level'=>"PriceLevel",
  266. 'pr.price'=>"PriceLevelUnitPriceHT",'pr.price_ttc'=>"PriceLevelUnitPriceTTC",
  267. 'pr.price_min'=>"MinPriceLevelUnitPriceHT",'pr.price_min_ttc'=>"MinPriceLevelUnitPriceTTC",
  268. 'pr.tva_tx'=>'PriceLevelVATRate',
  269. 'pr.date_price'=>'DateCreation*');
  270. $this->import_regex_array[$r]=array('pr.datec'=>'^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]$');
  271. $this->import_examplevalues_array[$r]=array('pr.fk_product'=>"1",
  272. 'pr.price_base_type'=>"HT",'pr.price_level'=>"1",
  273. 'pr.price'=>"100",'pr.price_ttc'=>"110",
  274. 'pr.price_min'=>"100",'pr.price_min_ttc'=>"110",
  275. 'pr.tva_tx'=>'19.6',
  276. 'pr.date_price'=>'2013-04-10');
  277. }
  278. }
  279. }
  280. /**
  281. * Function called when module is enabled.
  282. * The init function add constants, boxes, permissions and menus (defined in constructor) into Dolibarr database.
  283. * It also creates data directories
  284. *
  285. * @param string $options Options when enabling module ('', 'newboxdefonly', 'noboxes')
  286. * @return int 1 if OK, 0 if KO
  287. */
  288. function init($options='')
  289. {
  290. $this->remove($options);
  291. $sql = array();
  292. return $this->_init($sql,$options);
  293. }
  294. }