modExpenseReport.class.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php
  2. /* Copyright (C) 2011 Dimitri Mouillard <dmouillard@teclib.com>
  3. * Copyright (C) 2015 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2023 Alexandre Spangaro <aspangaro@easya.solutions>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * \defgroup expensereport Module expensereport
  21. * \brief Module to manage expense report. Replace old module Deplacement.
  22. * \file htdocs/core/modules/modExpenseReport.class.php
  23. * \ingroup expensereport
  24. * \brief Description and activation file for the module ExpenseReport
  25. */
  26. include_once DOL_DOCUMENT_ROOT."/core/modules/DolibarrModules.class.php";
  27. /**
  28. * Description and activation class for module ExpenseReport
  29. */
  30. class modExpenseReport extends DolibarrModules
  31. {
  32. /**
  33. * Constructor. Define names, constants, directories, boxes, permissions
  34. *
  35. * @param DoliDb $db Database handler
  36. */
  37. public function __construct($db)
  38. {
  39. global $conf, $user; // Required by some include code
  40. $this->db = $db;
  41. $this->numero = 770;
  42. $this->family = "hr";
  43. $this->module_position = '42';
  44. // Module label (no space allowed), used if translation string 'ModuleXXXName' not found (where XXX is value of numeric property 'numero' of module)
  45. $this->name = preg_replace('/^mod/i', '', get_class($this));
  46. // Module description, used if translation string 'ModuleXXXDesc' not found (where XXX is value of numeric property 'numero' of module)
  47. $this->description = "Manage and claim expense reports (transportation, meal, ...)";
  48. $this->version = 'dolibarr';
  49. $this->const_name = 'MAIN_MODULE_'.strtoupper($this->name);
  50. $this->picto = 'trip';
  51. // Data directories to create when module is enabled.
  52. $this->dirs = array("/expensereport/temp");
  53. $r = 0;
  54. // Config pages. Put here list of php page names stored in admmin directory used to setup module.
  55. $this->config_page_url = array('expensereport.php');
  56. // Dependencies
  57. $this->hidden = false; // A condition to hide module
  58. $this->depends = array(); // List of module class names as string that must be enabled if this module is enabled
  59. // $this->conflictwith = array("modDeplacement"); // Deactivate for access on old information
  60. $this->requiredby = array(); // List of modules id to disable if this one is disabled
  61. $this->phpmin = array(7, 0); // Minimum version of PHP required by module
  62. $this->need_dolibarr_version = array(3, 7); // Minimum version of Dolibarr required by module
  63. $this->langfiles = array("companies", "trips");
  64. // Constants
  65. $this->const = array(); // List of particular constants to add when module is enabled (key, 'chaine', value, desc, visible, 0 or 'allentities')
  66. $r = 0;
  67. $this->const[$r][0] = "EXPENSEREPORT_ADDON_PDF";
  68. $this->const[$r][1] = "chaine";
  69. $this->const[$r][2] = "standard";
  70. $this->const[$r][3] = 'Name of manager to build PDF expense reports documents';
  71. $this->const[$r][4] = 0;
  72. $r++;
  73. $this->const[$r][0] = "EXPENSEREPORT_ADDON";
  74. $this->const[$r][1] = "chaine";
  75. $this->const[$r][2] = "mod_expensereport_jade";
  76. $this->const[$r][3] = 'Name of manager to generate expense report ref number';
  77. $this->const[$r][4] = 0;
  78. $r++;
  79. $this->const[$r][0] = "MAIN_DELAY_EXPENSEREPORTS";
  80. $this->const[$r][1] = "chaine";
  81. $this->const[$r][2] = "15";
  82. $this->const[$r][3] = 'Tolerance delay (in days) before alert for expense reports to approve';
  83. $this->const[$r][4] = 0;
  84. $r++;
  85. $this->const[$r][0] = "MAIN_DELAY_EXPENSEREPORTS_TO_PAY";
  86. $this->const[$r][1] = "chaine";
  87. $this->const[$r][2] = "15";
  88. $this->const[$r][3] = 'Tolerance delay (in days) before alert for expense reports to pay';
  89. $this->const[$r][4] = 0;
  90. $r++;
  91. // Array to add new pages in new tabs
  92. $this->tabs[] = array();
  93. // Boxes
  94. $this->boxes = array(); // List of boxes
  95. $r = 0;
  96. // Permissions
  97. $this->rights = array(); // Permission array used by this module
  98. $this->rights_class = 'expensereport';
  99. $this->rights[$r][0] = 771;
  100. $this->rights[$r][1] = 'Read expense reports (yours and your subordinates)';
  101. $this->rights[$r][2] = 'r';
  102. $this->rights[$r][3] = 0;
  103. $this->rights[$r][4] = 'lire';
  104. $r++;
  105. $this->rights[$r][0] = 772;
  106. $this->rights[$r][1] = 'Create/modify expense reports';
  107. $this->rights[$r][2] = 'w';
  108. $this->rights[$r][3] = 0;
  109. $this->rights[$r][4] = 'creer';
  110. $r++;
  111. $this->rights[$r][0] = 773;
  112. $this->rights[$r][1] = 'Delete expense reports';
  113. $this->rights[$r][2] = 'd';
  114. $this->rights[$r][3] = 0;
  115. $this->rights[$r][4] = 'supprimer';
  116. $r++;
  117. $this->rights[$r][0] = 775;
  118. $this->rights[$r][1] = 'Approve expense reports';
  119. $this->rights[$r][2] = 'w';
  120. $this->rights[$r][3] = 0;
  121. $this->rights[$r][4] = 'approve';
  122. $r++;
  123. $this->rights[$r][0] = 776;
  124. $this->rights[$r][1] = 'Pay expense reports';
  125. $this->rights[$r][2] = 'w';
  126. $this->rights[$r][3] = 0;
  127. $this->rights[$r][4] = 'to_paid';
  128. $r++;
  129. $this->rights[$r][0] = 777;
  130. $this->rights[$r][1] = 'Read expense reports of everybody';
  131. $this->rights[$r][2] = 'r';
  132. $this->rights[$r][3] = 0;
  133. $this->rights[$r][4] = 'readall';
  134. $r++;
  135. $this->rights[$r][0] = 778;
  136. $this->rights[$r][1] = 'Create expense reports for everybody';
  137. $this->rights[$r][2] = 'w';
  138. $this->rights[$r][3] = 0;
  139. $this->rights[$r][4] = 'writeall_advance';
  140. $r++;
  141. $this->rights[$r][0] = 779;
  142. $this->rights[$r][1] = 'Export expense reports';
  143. $this->rights[$r][2] = 'r';
  144. $this->rights[$r][3] = 0;
  145. $this->rights[$r][4] = 'export';
  146. $r++;
  147. // Menus
  148. //-------
  149. $this->menu = 1; // This module add menu entries. They are coded into menu manager.
  150. // Exports
  151. $r = 0;
  152. $r++;
  153. $this->export_code[$r] = 'expensereport_'.$r;
  154. $this->export_label[$r] = 'ListTripsAndExpenses';
  155. $this->export_icon[$r] = 'trip';
  156. $this->export_permission[$r] = array(array("expensereport", "export"));
  157. $this->export_fields_array[$r] = array(
  158. 'd.rowid'=>"TripId", 'd.ref'=>'Ref', 'd.date_debut'=>'DateStart', 'd.date_fin'=>'DateEnd', 'd.date_create'=>'DateCreation', 'd.date_approve'=>'DateApprove',
  159. 'd.total_ht'=>"TotalHT", 'd.total_tva'=>'TotalVAT', 'd.total_ttc'=>'TotalTTC',
  160. 'd.fk_statut'=>'Status', 'd.paid'=>'Paid',
  161. 'd.note_private'=>'NotePrivate', 'd.note_public'=>'NotePublic', 'd.detail_cancel'=>'MOTIF_CANCEL', 'd.detail_refuse'=>'MOTIF_REFUS',
  162. 'ed.rowid'=>'LineId', 'tf.code'=>'Type', 'ed.date'=>'Date', 'ed.tva_tx'=>'VATRate',
  163. 'ed.qty'=>"Quantity", 'ed.value_unit'=>"UnitPriceHT",
  164. 'ed.total_ht'=>'TotalHT', 'ed.total_tva'=>'TotalVAT', 'ed.total_ttc'=>'TotalTTC', 'ed.comments'=>'Comment', 'p.rowid'=>'ProjectId', 'p.ref'=>'Ref',
  165. 'u.lastname'=>'Lastname', 'u.firstname'=>'Firstname', 'u.login'=>"Login",
  166. 'user_rib.iban_prefix' => 'IBAN', 'user_rib.bic' => 'BIC', 'user_rib.code_banque' => 'BankCode', 'user_rib.bank' => 'BankName', 'user_rib.proprio' => 'BankAccountOwner',
  167. 'user_rib.owner_address' => 'BankAccountOwnerAddress'
  168. );
  169. $this->export_TypeFields_array[$r] = array(
  170. 'd.rowid'=>'Numeric', 'd.ref'=>'Text', 'd.date_debut'=>'Date', 'd.date_fin'=>'Date', 'd.date_create'=>'Date', 'd.date_approve'=>'Date',
  171. 'd.total_ht'=>"Numeric", 'd.total_tva'=>'Numeric', 'd.total_ttc'=>'Numeric',
  172. 'd.fk_statut'=>"Numeric", 'd.paid'=>'Numeric',
  173. 'd.note_private'=>'Text', 'd.note_public'=>'Text', 'd.detail_cancel'=>'Text', 'd.detail_refuse'=>'Text',
  174. 'ed.rowid'=>'Numeric', 'tf.code'=>'Code', 'ed.date'=>'Date', 'ed.tva_tx'=>'Numeric',
  175. 'ed.qty'=>'Numeric', 'ed.value_unit'=>'Numeric',
  176. 'ed.total_ht'=>'Numeric', 'ed.total_tva'=>'Numeric', 'ed.total_ttc'=>'Numeric', 'ed.comments'=>'Text', 'p.rowid'=>'Numeric', 'p.ref'=>'Text',
  177. 'u.lastname'=>'Text', 'u.firstname'=>'Text', 'u.login'=>"Text",
  178. 'user_rib.iban_prefix' => 'Text', 'user_rib.bic' => 'Text', 'user_rib.code_banque' => 'Text', 'user_rib.bank' => 'Text', 'user_rib.proprio' => 'Text',
  179. 'user_rib.owner_address' => 'Text'
  180. );
  181. $this->export_entities_array[$r] = array(
  182. 'ed.rowid'=>'expensereport_line', 'ed.date'=>'expensereport_line',
  183. 'ed.qty'=>'expensereport_line', 'ed.value_unit'=>'expensereport_line',
  184. 'ed.tva_tx'=>'expensereport_line', 'ed.total_ht'=>'expensereport_line', 'ed.total_tva'=>'expensereport_line', 'ed.total_ttc'=>'expensereport_line',
  185. 'ed.comments'=>'expensereport_line', 'tf.code'=>'expensereport_line', 'p.project_ref'=>'expensereport_line', 'p.rowid'=>'project', 'p.ref'=>'project',
  186. 'u.lastname'=>'user', 'u.firstname'=>'user', 'u.login'=>'user',
  187. 'user_rib.iban_prefix' => 'user', 'user_rib.bic' => 'user', 'user_rib.code_banque' => 'user', 'user_rib.bank' => 'user', 'user_rib.proprio' => 'user',
  188. 'user_rib.owner_address' => 'user'
  189. );
  190. //$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');
  191. $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
  192. $keyforselect = 'expensereport';
  193. $keyforelement = 'expensereport';
  194. $keyforaliasextra = 'extra';
  195. include DOL_DOCUMENT_ROOT.'/core/extrafieldsinexport.inc.php';
  196. $keyforselect = 'user'; $keyforelement = 'user'; $keyforaliasextra = 'extrau';
  197. include DOL_DOCUMENT_ROOT.'/core/extrafieldsinexport.inc.php';
  198. $this->export_sql_start[$r] = 'SELECT DISTINCT ';
  199. $this->export_sql_end[$r] = ' FROM '.MAIN_DB_PREFIX.'expensereport as d';
  200. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'expensereport_extrafields as extra on d.rowid = extra.fk_object';
  201. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'user_rib as user_rib ON user_rib.fk_user = d.fk_user_author,';
  202. $this->export_sql_end[$r] .= ' '.MAIN_DB_PREFIX.'user as u';
  203. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'user_extrafields as extrau ON u.rowid = extrau.fk_object,';
  204. $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';
  205. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'projet as p ON ed.fk_projet = p.rowid';
  206. $this->export_sql_end[$r] .= ' WHERE ed.fk_expensereport = d.rowid AND d.fk_user_author = u.rowid';
  207. $this->export_sql_end[$r] .= ' AND d.entity IN ('.getEntity('expensereport').')';
  208. }
  209. /**
  210. * Function called when module is enabled.
  211. * The init function add constants, boxes, permissions and menus (defined in constructor) into Dolibarr database.
  212. * It also creates data directories.
  213. *
  214. * @param string $options Options
  215. * @return int 1 if OK, 0 if KO
  216. */
  217. public function init($options = '')
  218. {
  219. global $conf;
  220. $result = $this->_load_tables('/install/mysql/', 'expensereport');
  221. if ($result < 0) {
  222. return -1; // Do not activate module if error 'not allowed' returned when loading module SQL queries (the _load_table run sql with run_sql with the error allowed parameter set to 'default')
  223. }
  224. // Remove permissions and default values
  225. $this->remove($options);
  226. $sql = array(
  227. "DELETE FROM ".MAIN_DB_PREFIX."document_model WHERE nom = 'standard' AND type='expensereport' AND entity = ".((int) $conf->entity),
  228. "INSERT INTO ".MAIN_DB_PREFIX."document_model (nom, type, entity) VALUES('standard','expensereport',".((int) $conf->entity).")"
  229. );
  230. return $this->_init($sql, $options);
  231. }
  232. }