modExpenseReport.class.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. <?php
  2. /* Copyright (C) 2011 Dimitri Mouillard <dmouillard@teclib.com>
  3. * Copyright (C) 2015 Laurent Destailleur <eldy@users.sourceforge.net>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \defgroup expensereport Module expensereport
  20. * \brief Module to manage expense report. Replace old module Deplacement.
  21. */
  22. /**
  23. * \file htdocs/core/modules/modExpenseReport.class.php
  24. * \ingroup expensereport
  25. * \brief Description and activation file for module ExpenseReport
  26. */
  27. include_once(DOL_DOCUMENT_ROOT ."/core/modules/DolibarrModules.class.php");
  28. /**
  29. * Description and activation class for module ExpenseReport
  30. */
  31. class modExpenseReport extends DolibarrModules
  32. {
  33. /**
  34. * Constructor. Define names, constants, directories, boxes, permissions
  35. *
  36. * @param Database $db Database handler
  37. */
  38. function __construct($db)
  39. {
  40. global $conf;
  41. $this->db = $db;
  42. // Id for module (must be unique).
  43. // Use here a free id (See in Home -> System information -> Dolibarr for list of used modules id).
  44. $this->numero = 770;
  45. // Family can be 'crm','financial','hr','projects','products','ecm','technic','other'
  46. // It is used to group modules in module setup page
  47. $this->family = "hr";
  48. $this->module_position = 40;
  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. // Module description, used if translation string 'ModuleXXXDesc' not found (where XXX is value of numeric property 'numero' of module)
  52. $this->description = "Manage and claim expense reports (transportation, meal, ...)";
  53. // Possible values for version are: 'development', 'experimental', 'dolibarr' or version
  54. $this->version = 'dolibarr';
  55. // Key used in llx_const table to save module status enabled/disabled (where MYMODULE is value of property name of module in uppercase)
  56. $this->const_name = 'MAIN_MODULE_'.strtoupper($this->name);
  57. // Where to store the module in setup page (0=common,1=interface,2=others,3=very specific)
  58. $this->special = 0;
  59. // Name of image file used for this module.
  60. // If file is in theme/yourtheme/img directory under name object_pictovalue.png, use this->picto='pictovalue'
  61. // If file is in module/img directory under name object_pictovalue.png, use this->picto='pictovalue@module'
  62. $this->picto='trip';
  63. // Defined if the directory /mymodule/inc/triggers/ contains triggers or not
  64. $this->triggers = 0;
  65. // Data directories to create when module is enabled.
  66. // Example: this->dirs = array("/mymodule/temp");
  67. $this->dirs = array();
  68. $r=0;
  69. // Config pages. Put here list of php page names stored in admmin directory used to setup module.
  70. $this->config_page_url = array('expensereport.php');
  71. // Dependencies
  72. $this->depends = array(); // List of modules id that must be enabled if this module is enabled
  73. // $this->conflictwith = array("modDeplacement"); // Deactivate for access on old information
  74. $this->requiredby = array(); // List of modules id to disable if this one is disabled
  75. $this->phpmin = array(4,3); // Minimum version of PHP required by module
  76. $this->need_dolibarr_version = array(3,7); // Minimum version of Dolibarr required by module
  77. $this->langfiles = array("companies","trips");
  78. // Constants
  79. // Example: $this->const=array(0=>array('MYMODULE_MYNEWCONST1','chaine','myvalue','This is a constant to add',0),
  80. // 1=>array('MYMODULE_MYNEWCONST2','chaine','myvalue','This is another constant to add',0) );
  81. // 2=>array('MAIN_MODULE_MYMODULE_NEEDSMARTY','chaine',1,'Constant to say module need smarty',0)
  82. $this->const = array(); // List of particular constants to add when module is enabled (key, 'chaine', value, desc, visible, 0 or 'allentities')
  83. $r=0;
  84. $this->const[$r][0] = "EXPENSEREPORT_ADDON_PDF";
  85. $this->const[$r][1] = "chaine";
  86. $this->const[$r][2] = "standard";
  87. $this->const[$r][3] = 'Name of manager to build PDF expense reports documents';
  88. $this->const[$r][4] = 0;
  89. $r++;
  90. // Array to add new pages in new tabs
  91. $this->tabs = array();
  92. // where entity can be
  93. // 'thirdparty' to add a tab in third party view
  94. // 'intervention' to add a tab in intervention view
  95. // 'order_supplier' to add a tab in supplier order view
  96. // 'invoice_supplier' to add a tab in supplier invoice view
  97. // 'invoice' to add a tab in customer invoice view
  98. // 'order' to add a tab in customer order view
  99. // 'product' to add a tab in product view
  100. // 'stock' to add a tab in stock view
  101. // 'propal' to add a tab in propal view
  102. // 'member' to add a tab in fundation member view
  103. // 'contract' to add a tab in contract view
  104. // 'user' to add a tab in user view
  105. // 'group' to add a tab in group view
  106. // 'contact' to add a tab in contact view
  107. // Boxes
  108. $this->boxes = array(); // List of boxes
  109. $r=0;
  110. // Add here list of php file(s) stored in includes/boxes that contains class to show a box.
  111. // Example:
  112. //$this->boxes[$r][1] = "myboxa.php";
  113. //$r++;
  114. //$this->boxes[$r][1] = "myboxb.php";
  115. //$r++;
  116. // Permissions
  117. $this->rights = array(); // Permission array used by this module
  118. $this->rights_class = 'expensereport';
  119. $this->rights[1][0] = 771;
  120. $this->rights[1][1] = 'Read expense reports (yours and your subordinates)';
  121. $this->rights[1][2] = 'r';
  122. $this->rights[1][3] = 1;
  123. $this->rights[1][4] = 'lire';
  124. $this->rights[3][0] = 772;
  125. $this->rights[3][1] = 'Create/modify expense reports';
  126. $this->rights[3][2] = 'w';
  127. $this->rights[3][3] = 0;
  128. $this->rights[3][4] = 'creer';
  129. $this->rights[4][0] = 773;
  130. $this->rights[4][1] = 'Delete expense reports';
  131. $this->rights[4][2] = 'd';
  132. $this->rights[4][3] = 0;
  133. $this->rights[4][4] = 'supprimer';
  134. $this->rights[2][0] = 774;
  135. $this->rights[2][1] = 'Read all expense reports';
  136. $this->rights[2][2] = 'r';
  137. $this->rights[2][3] = 1;
  138. $this->rights[2][4] = 'readall';
  139. $this->rights[6][0] = 775;
  140. $this->rights[6][1] = 'Approve expense reports';
  141. $this->rights[6][2] = 'w';
  142. $this->rights[6][3] = 0;
  143. $this->rights[6][4] = 'approve';
  144. $this->rights[7][0] = 776;
  145. $this->rights[7][1] = 'Pay expense reports';
  146. $this->rights[7][2] = 'w';
  147. $this->rights[7][3] = 0;
  148. $this->rights[7][4] = 'to_paid';
  149. if (! empty($conf->global->DEPLACEMENT_TO_CLEAN))
  150. {
  151. $this->rights[8][0] = 777;
  152. $this->rights[8][1] = 'Synchroniser les NDF avec un compte courant';
  153. $this->rights[8][2] = 'w';
  154. $this->rights[8][3] = 0;
  155. $this->rights[8][4] = 'synchro';
  156. $this->rights[9][0] = 778;
  157. $this->rights[9][1] = 'Exporter les NDF au format CSV';
  158. $this->rights[9][2] = 'r';
  159. $this->rights[9][3] = 0;
  160. $this->rights[9][4] = 'export_csv';
  161. }
  162. $this->rights[5][0] = 779;
  163. $this->rights[5][1] = 'Export expense reports';
  164. $this->rights[5][2] = 'r';
  165. $this->rights[5][3] = 0;
  166. $this->rights[5][4] = 'export';
  167. // Exports
  168. $r=0;
  169. $r++;
  170. $this->export_code[$r]='expensereport_'.$r;
  171. $this->export_label[$r]='ListTripsAndExpenses';
  172. $this->export_icon[$r]='trip';
  173. $this->export_permission[$r]=array(array("expensereport","export"));
  174. $this->export_fields_array[$r]=array('d.rowid'=>"TripId",'d.ref'=>'Ref','d.date_debut'=>'DateStart','d.date_fin'=>'DateEnd','d.date_create'=>'DateCreation','d.date_approve'=>'DateApprove','d.total_ht'=>"TotalHT",'d.total_tva'=>'TotalVAT','d.total_ttc'=>'TotalTTC','d.note_private'=>'NotePrivate','d.note_public'=>'NotePublic','u.lastname'=>'Lastname','u.firstname'=>'Firstname','u.login'=>"Login",'ed.rowid'=>'LineId','tf.code'=>'Type','ed.date'=>'Date','ed.tva_tx'=>'VATRate','ed.total_ht'=>'TotalHT','ed.total_tva'=>'TotalVAT','ed.total_ttc'=>'TotalTTC','ed.comments'=>'Comment','p.rowid'=>'ProjectId','p.ref'=>'Ref');
  175. $this->export_entities_array[$r]=array('u.lastname'=>'user','u.firstname'=>'user','u.login'=>'user','ed.rowid'=>'expensereport_line','ed.date'=>'expensereport_line','ed.tva_tx'=>'expensereport_line','ed.total_ht'=>'expensereport_line','ed.total_tva'=>'expensereport_line','ed.total_ttc'=>'expensereport_line','ed.comments'=>'expensereport_line','tf.code'=>'expensereport_line','p.project_ref'=>'expensereport_line','p.rowid'=>'project','p.ref'=>'project');
  176. $this->export_alias_array[$r]=array('d.rowid'=>"idtrip",'d.type'=>"type",'d.note_private'=>'note_private','d.note_public'=>'note_public','u.lastname'=>'name','u.firstname'=>'firstname','u.login'=>'login');
  177. $this->export_dependencies_array[$r]=array('expensereport_line'=>'ed.rowid','type_fees'=>'tf.rowid'); // To add unique key if we ask a field of a child to avoid the DISTINCT to discard them
  178. $this->export_sql_start[$r]='SELECT DISTINCT ';
  179. $this->export_sql_end[$r] =' FROM '.MAIN_DB_PREFIX.'expensereport as d, '.MAIN_DB_PREFIX.'user as u,';
  180. $this->export_sql_end[$r] .=' '.MAIN_DB_PREFIX.'expensereport_det as ed LEFT JOIN '.MAIN_DB_PREFIX.'c_type_fees as tf ON ed.fk_c_type_fees = tf.id';
  181. $this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'projet as p ON ed.fk_projet = p.rowid';
  182. $this->export_sql_end[$r] .=' WHERE ed.fk_expensereport = d.rowid AND d.fk_user_author = u.rowid';
  183. $this->export_sql_end[$r] .=' AND d.entity IN ('.getEntity('expensereport',1).')';
  184. // Main menu entries
  185. $this->menu = array(); // List of menus to add
  186. $r=0;
  187. // Example to declare a Left Menu entry: fk_mainmenu=home,fk_leftmenu=admintools
  188. $this->menu[$r]=array( 'fk_menu'=>'fk_mainmenu=hrm', // Use r=value where r is index key used for the parent menu entry (higher parent must be a top menu entry)
  189. 'type'=>'left', // This is a Left menu entry
  190. 'titre'=>'TripsAndExpenses',
  191. 'mainmenu'=>'hrm',
  192. 'leftmenu'=>'expensereport',
  193. 'url'=>'/expensereport/index.php',
  194. 'langs'=>'trips', // Lang file to use (without .lang) by module. File must be in langs/code_CODE/ directory.
  195. 'position'=>100,
  196. 'enabled'=>'$conf->expensereport->enabled', // Define condition to show or hide menu entry. Use '$conf->mymodule->enabled' if entry must be visible if module is enabled.
  197. 'perms'=>'$user->rights->expensereport->lire', // Use 'perms'=>'$user->rights->mymodule->level1->level2' if you want your menu with a permission rules
  198. 'target'=>'',
  199. 'user'=>2); // 0=Menu for internal users, 1=external users, 2=both
  200. $r++;
  201. $this->menu[$r]=array( 'fk_menu'=>'fk_mainmenu=hrm,fk_leftmenu=expensereport', // Use r=value where r is index key used for the parent menu entry (higher parent must be a top menu entry)
  202. 'type'=>'left', // This is a Left menu entry
  203. 'titre'=>'New',
  204. 'mainmenu'=>'hrm',
  205. 'leftmenu'=>'expensereport_detailnew',
  206. 'url'=>'/expensereport/card.php?action=create',
  207. 'langs'=>'trips', // Lang file to use (without .lang) by module. File must be in langs/code_CODE/ directory.
  208. 'position'=>100,
  209. 'enabled'=>'$conf->expensereport->enabled', // Define condition to show or hide menu entry. Use '$conf->mymodule->enabled' if entry must be visible if module is enabled.
  210. 'perms'=>'$user->rights->expensereport->creer', // Use 'perms'=>'$user->rights->mymodule->level1->level2' if you want your menu with a permission rules
  211. 'target'=>'',
  212. 'user'=>2); // 0=Menu for internal users, 1=external users, 2=both
  213. $r++;
  214. $this->menu[$r]=array( 'fk_menu'=>'fk_mainmenu=hrm,fk_leftmenu=expensereport', // Use r=value where r is index key used for the parent menu entry (higher parent must be a top menu entry)
  215. 'type'=>'left', // This is a Left menu entry
  216. 'titre'=>'List',
  217. 'mainmenu'=>'hrm',
  218. 'leftmenu'=>'expensereport_detaillist',
  219. 'url'=>'/expensereport/list.php',
  220. 'langs'=>'trips', // Lang file to use (without .lang) by module. File must be in langs/code_CODE/ directory.
  221. 'position'=>100,
  222. 'enabled'=>'$conf->expensereport->enabled', // Define condition to show or hide menu entry. Use '$conf->mymodule->enabled' if entry must be visible if module is enabled.
  223. 'perms'=>'$user->rights->expensereport->lire', // Use 'perms'=>'$user->rights->mymodule->level1->level2' if you want your menu with a permission rules
  224. 'target'=>'',
  225. 'user'=>2); // 0=Menu for internal users, 1=external users, 2=both
  226. $r++;
  227. $this->menu[$r]=array( 'fk_menu'=>'fk_mainmenu=hrm,fk_leftmenu=expensereport_detaillist', // Use r=value where r is index key used for the parent menu entry (higher parent must be a top menu entry)
  228. 'type'=>'left', // This is a Left menu entry
  229. 'titre'=>'ListToApprove',
  230. 'mainmenu'=>'hrm',
  231. 'leftmenu'=>'expensereport_detaillist_approve',
  232. 'url'=>'/expensereport/list.php?search_status=2',
  233. 'langs'=>'trips', // Lang file to use (without .lang) by module. File must be in langs/code_CODE/ directory.
  234. 'position'=>100,
  235. 'enabled'=>'$conf->expensereport->enabled', // Define condition to show or hide menu entry. Use '$conf->mymodule->enabled' if entry must be visible if module is enabled.
  236. 'perms'=>'$user->rights->expensereport->approve', // Use 'perms'=>'$user->rights->mymodule->level1->level2' if you want your menu with a permission rules
  237. 'target'=>'',
  238. 'user'=>2); // 0=Menu for internal users, 1=external users, 2=both
  239. $r++;
  240. $this->menu[$r]=array( 'fk_menu'=>'fk_mainmenu=hrm,fk_leftmenu=expensereport', // Use r=value where r is index key used for the parent menu entry (higher parent must be a top menu entry)
  241. 'type'=>'left', // This is a Left menu entry
  242. 'titre'=>'Statistics',
  243. 'mainmenu'=>'hrm',
  244. 'leftmenu'=>'expensereport_detail',
  245. 'url'=>'/expensereport/stats/index.php',
  246. 'langs'=>'trips', // Lang file to use (without .lang) by module. File must be in langs/code_CODE/ directory.
  247. 'position'=>100,
  248. 'enabled'=>'$conf->expensereport->enabled', // Define condition to show or hide menu entry. Use '$conf->mymodule->enabled' if entry must be visible if module is enabled.
  249. 'perms'=>'$user->rights->expensereport->lire', // Use 'perms'=>'$user->rights->mymodule->level1->level2' if you want your menu with a permission rules
  250. 'target'=>'',
  251. 'user'=>2); // 0=Menu for internal users, 1=external users, 2=both
  252. $r++;
  253. // Disabled, not yet stable
  254. $this->menu[$r]=array( 'fk_menu'=>'fk_mainmenu=hrm,fk_leftmenu=expensereport', // Use r=value where r is index key used for the parent menu entry (higher parent must be a top menu entry)
  255. 'type'=>'left', // This is a Left menu entry
  256. 'titre'=>'ExportTripCSV',
  257. 'mainmenu'=>'hrm',
  258. 'leftmenu'=>'expensereport_detail',
  259. 'url'=>'/expensereport/export_csv.php',
  260. 'langs'=>'expensereport', // Lang file to use (without .lang) by module. File must be in langs/code_CODE/ directory.
  261. 'position'=>100,
  262. 'enabled'=>'$conf->global->DEPLACEMENT_TO_CLEAN', // Define condition to show or hide menu entry. Use '$conf->mymodule->enabled' if entry must be visible if module is enabled.
  263. 'perms'=>'$user->rights->expensereport->lire', // Use 'perms'=>'$user->rights->mymodule->level1->level2' if you want your menu with a permission rules
  264. 'target'=>'',
  265. 'user'=>2); // 0=Menu for internal users, 1=external users, 2=both
  266. $r++;
  267. $this->menu[$r]=array( 'fk_menu'=>'fk_mainmenu=hrm,fk_leftmenu=expensereport', // Use r=value where r is index key used for the parent menu entry (higher parent must be a top menu entry)
  268. 'type'=>'left', // This is a Left menu entry
  269. 'titre'=>'Synchro_Compta',
  270. 'mainmenu'=>'hrm',
  271. 'leftmenu'=>'expensereport_detail',
  272. 'url'=>'/expensereport/synchro_compta.php',
  273. 'langs'=>'expensereport', // Lang file to use (without .lang) by module. File must be in langs/code_CODE/ directory.
  274. 'position'=>100,
  275. 'enabled'=>'$conf->global->DEPLACEMENT_TO_CLEAN', // Define condition to show or hide menu entry. Use '$conf->mymodule->enabled' if entry must be visible if module is enabled.
  276. 'perms'=>'$user->rights->expensereport->lire', // Use 'perms'=>'$user->rights->mymodule->level1->level2' if you want your menu with a permission rules
  277. 'target'=>'',
  278. 'user'=>2); // 0=Menu for internal users, 1=external users, 2=both
  279. $r++;
  280. }
  281. /**
  282. * Function called when module is enabled.
  283. * The init function add constants, boxes, permissions and menus (defined in constructor) into Dolibarr database.
  284. * It also creates data directories.
  285. *
  286. * @param string $options Options
  287. * @return int 1 if OK, 0 if KO
  288. */
  289. function init($options='')
  290. {
  291. global $conf;
  292. // Remove permissions and default values
  293. $this->remove($options);
  294. $sql = array(
  295. "DELETE FROM ".MAIN_DB_PREFIX."document_model WHERE nom = 'standard' AND entity = ".$conf->entity,
  296. "INSERT INTO ".MAIN_DB_PREFIX."document_model (nom, type, entity) VALUES('standard','deplacement',".$conf->entity.")"
  297. );
  298. return $this->_init($sql,$options);
  299. }
  300. }